forked from kptran/mct
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Icons] Merge in latest from nasa/master
- Loading branch information
Showing
15 changed files
with
449 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
mctcore/src/main/java/gov/nasa/arc/mct/gui/ContextAwareButton.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
126 changes: 126 additions & 0 deletions
126
mctcore/src/test/java/gov/nasa/arc/mct/gui/TestContextAwareButton.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.