Skip to content

Commit

Permalink
[Icons] Merge in latest from nasa/master
Browse files Browse the repository at this point in the history
  • Loading branch information
VWoeltjen committed Sep 17, 2013
2 parents 0103b60 + e9d24e0 commit 211691b
Show file tree
Hide file tree
Showing 15 changed files with 449 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,21 @@ public void setEntityManagerProperties(Properties p) {
}

public void activate(ComponentContext context) throws IOException {
setEntityManagerProperties(getPersistenceProperties());
Properties persistenceProperties = getPersistenceProperties();
setEntityManagerProperties(persistenceProperties);
new InternalDBPersistenceAccess().setPersistenceService(this);
checkDatabaseVersion();

// Check for configuration of the polling interval (default is 3s)
long pollingInterval = 3000;
try {
String intervalString = persistenceProperties.getProperty("mct.database_pollInterval");
if (intervalString != null) {
pollingInterval = Long.parseLong(intervalString);
}
} catch (NumberFormatException nfe) {
// Stick with the default
}

Timer databasePollingTimer = new Timer();
databasePollingTimer.schedule(new TimerTask() {
Expand All @@ -143,7 +155,7 @@ public void run() {
InternalDBPersistenceAccess.getService().updateComponentsFromDatabase();
}

}, Calendar.getInstance().getTime(), 3000);
}, Calendar.getInstance().getTime(), pollingInterval);

}

Expand Down
108 changes: 108 additions & 0 deletions mctcore/src/main/java/gov/nasa/arc/mct/gui/ContextAwareButton.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*******************************************************************************
* Mission Control Technologies, Copyright (c) 2009-2012, United States Government
* as represented by the Administrator of the National Aeronautics and Space
* Administration. All rights reserved.
*
* The MCT platform is licensed under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*
* MCT includes source code licensed under additional open source licenses. See
* the MCT Open Source Licenses file included with this distribution or the About
* MCT Licenses dialog available at runtime from the MCT Help menu for additional
* information.
*******************************************************************************/
package gov.nasa.arc.mct.gui;

import gov.nasa.arc.mct.util.MCTIcons;

import javax.swing.Action;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;

/**
* A button containing a context aware action.
* Sets its enabled/disabled state in response to context
* changes. Must be notified of these context changes explicitly.
*
* @author vwoeltje
*
*/
public class ContextAwareButton extends JButton {
private static final long serialVersionUID = -2380576132488962171L;

/**
* Create a new button for the specified action.
* @param action the action to perform when the button is clicked
*/
public ContextAwareButton(ContextAwareAction action) {
this(action, null);
}

/**
* Create a new button for the specified action.
* Provide an initial context, used to determine
* visible/enabled states (and typically to also
* inform the actual behavior of the button.)
*
* @param action the action to perform when the button is clicked
* @param context the context for this action
*/
public ContextAwareButton(ContextAwareAction action, ActionContext context) {
super(action);
setContext(context);
}

@Override
public void setIcon(Icon icon) {
if (icon != null && icon instanceof ImageIcon) {
super.setIcon(MCTIcons.processIcon((ImageIcon) icon, .9f, .9f, .9f, true));
super.setPressedIcon(MCTIcons.processIcon((ImageIcon) icon, 1f, 1f, 1f, true));
} else {
super.setIcon(icon);
}
}

@Override
public void setText(String text) {
// If text is not the action name, make a tool tip
Object name = getAction().getValue(Action.NAME);
if (name != null && !name.equals(text)) {
this.setToolTipText(name.toString());
}
super.setText(text);
}

/**
* Set the context for the current action.
* Button will enable/disable based on the action's
* state with regard to the current context.
*
* This method should be called whenever the action's
* context may have meaningfully changed. Otherwise,
* the action performed by the button may be outdated.
*
* @param context the context for this action
*/
public void setContext(ActionContext context) {
if (context != null) {
Action a = getAction();
if (a instanceof ContextAwareAction) {
ContextAwareAction action = (ContextAwareAction) a;
setVisible(action.canHandle(context));
setEnabled(action.isEnabled());
}
} else {
setVisible(false);
setEnabled(false);
}
}
}
30 changes: 9 additions & 21 deletions mctcore/src/main/java/gov/nasa/arc/mct/gui/SettingsButton.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import gov.nasa.arc.mct.util.MCTIcons;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;

Expand All @@ -47,34 +46,18 @@ public class SettingsButton extends JToggleButton {
private static final Icon CONFIG_DESELECTED =
MCTIcons.processIcon(
new ImageIcon(SettingsButton.class.getResource("/icons/mct_icon_config.png")),
0.9f, 0.9f, 0.9f, false);
0.9f, 0.9f, 0.9f, true);
private static final Icon CONFIG_SELECTED =
MCTIcons.processIcon(
new ImageIcon(SettingsButton.class.getResource("/icons/mct_icon_config.png")),
1f, 1f, 1f, false);
private static final Icon CONFIG_DISABLED = new Icon() {
@Override
public void paintIcon(Component c, Graphics g, int x, int y) {
// Do not paint when disabled
}

@Override
public int getIconWidth() {
return CONFIG_SELECTED.getIconWidth();
}

@Override
public int getIconHeight() {
return CONFIG_SELECTED.getIconHeight();
}
};
1f, 1f, 1f, true);

private static final Color SELECTED_BACKGROUND = new Color(193, 193, 193);
private static final Color SELECTED_BORDER = new Color(138, 138, 138);
private static final Color FOCUS_BORDER = new Color(138, 138, 200);

private static final Dimension PREFERRED_SIZE =
new Dimension(19, 17);
new Dimension(17, 17);

/**
* Create a new configuration button.
Expand All @@ -86,11 +69,16 @@ public SettingsButton() {
setBorder(BorderFactory.createEmptyBorder());
setOpaque(false);
setIcon(CONFIG_DESELECTED);
setDisabledIcon(CONFIG_DISABLED);
setSelectedIcon(CONFIG_SELECTED);
setSelected(false);
}

@Override
public void setEnabled(boolean enabled) {
setVisible(enabled);
super.setEnabled(enabled);
}

@Override
public void paintComponent(Graphics g) {
if (isSelected()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,18 @@ public static boolean hasRole(User user, String role) {
* @param runtimeUser the user
* @return true if this components owner can be changed.
*/
public static boolean canChangeOwner(AbstractComponent component, User runtimeUser) {

public static boolean canChangeOwner(AbstractComponent component, User runtimeUser) {
// TODO: Consider moving this to Policy?
// First, rule out bootstrap components - changing ownership of these could result in major loss of functionality
for (AbstractComponent bootstrap : PlatformAccess.getPlatform().getBootstrapComponents()) {
if (bootstrap.getComponentId().equals(component.getComponentId())) {
return false;
}
}

// Otherwise, consider ownership and role rules
String componentOwner = component.getOriginalOwner() == null ? component.getOwner() : component.getOriginalOwner();
if (componentOwner.equals(runtimeUser.getUserId()) || "admin".equals(runtimeUser.getUserId())) {
if (componentOwner.equals(runtimeUser.getUserId()) || "admin".equals(runtimeUser.getUserId())) {
return true;
} else {
return RoleAccess.hasRole(runtimeUser, componentOwner);
Expand Down
126 changes: 126 additions & 0 deletions mctcore/src/test/java/gov/nasa/arc/mct/gui/TestContextAwareButton.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package gov.nasa.arc.mct.gui;

import java.awt.image.BufferedImage;

import javax.swing.Action;
import javax.swing.Icon;
import javax.swing.ImageIcon;

import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class TestContextAwareButton {

private static final String ACTION_NAME = "Mock";

@Mock private ContextAwareAction action;
@Mock private ActionContext context;

@BeforeMethod
public void setup() {
MockitoAnnotations.initMocks(this);
Mockito.when(action.getValue(Action.NAME)).thenReturn(ACTION_NAME);
}

@Test
public void testNoContext() {
// Button should not be visible if no context has been provided
ContextAwareButton button = new ContextAwareButton(action);
Assert.assertFalse(button.isEnabled());
Assert.assertFalse(button.isVisible());

// Should behave similarly if context is explicitly null
button = new ContextAwareButton(action, null);
Assert.assertFalse(button.isEnabled());
Assert.assertFalse(button.isVisible());
}

@Test
public void testContext() {
// Should invoke canHandle for the specified context
new ContextAwareButton(action, context);
Mockito.verify(action).canHandle(context);
Mockito.verify(action, Mockito.atLeastOnce()).isEnabled();
}

@Test
public void testSetContext() {
// Should invoke canHandle for the specified context
new ContextAwareButton(action).setContext(context);
Mockito.verify(action).canHandle(context);
Mockito.verify(action, Mockito.atLeastOnce()).isEnabled();
}

@Test (dataProvider="generateStates")
public void testState(boolean canHandle, boolean isEnabled, boolean useConstructor) {
// Set up states
Mockito.when(action.canHandle(context)).thenReturn(canHandle);
Mockito.when(action.isEnabled()).thenReturn(isEnabled);

// Make the button
ContextAwareButton button = useConstructor ?
new ContextAwareButton(action, context) :
new ContextAwareButton(action);
if (!useConstructor) {
button.setContext(context);
}

// Verify visibility/enabled states match what the action reports
Assert.assertEquals(button.isVisible(), canHandle);
Assert.assertEquals(button.isEnabled(), isEnabled);
}

@Test
public void testToolTip() {
// Tool tip should show action name if text set to something else
ContextAwareButton button = new ContextAwareButton(action);

// Should not have a tool tip by default
Assert.assertTrue(button.getToolTipText() == null || button.getToolTipText().isEmpty());

// Setting text to same action name should not change tool tip
button.setText(ACTION_NAME);
Assert.assertTrue(button.getToolTipText() == null || button.getToolTipText().isEmpty());

// Setting text to some other name should change tool tip
button.setText("Something else");
Assert.assertEquals(button.getToolTipText(), ACTION_NAME);
}

@Test
public void testSetIcon() {
// Should not have an icon by default
Assert.assertNull(new ContextAwareButton(action).getIcon());

// Should customize (change) icon if it can (only for ImageIcons)
Icon icon = new ImageIcon(new BufferedImage(12,12,BufferedImage.TYPE_INT_ARGB));
Mockito.when(action.getValue(Action.SMALL_ICON)).thenReturn(icon);
Mockito.when(action.getValue(Action.LARGE_ICON_KEY)).thenReturn(icon);
Assert.assertNotNull(new ContextAwareButton(action).getIcon());
Assert.assertNotSame(new ContextAwareButton(action).getIcon(), icon);

// Should use (but not customize) regular icon
icon = Mockito.mock(Icon.class);
Mockito.when(action.getValue(Action.SMALL_ICON)).thenReturn(icon);
Mockito.when(action.getValue(Action.LARGE_ICON_KEY)).thenReturn(icon);
Assert.assertEquals(new ContextAwareButton(action).getIcon(), icon);
}

@DataProvider
public Object[][] generateStates() {
Object[][] result = new Object[8][];
for (int i = 0; i < result.length; i++) {
result[i] = new Object[]{
(i & 1) != 0, // canHandle
(i & 2) != 0, // isEnabled
(i & 4) != 0 // useConstructor
};
}
return result;
}
}
Loading

0 comments on commit 211691b

Please sign in to comment.