# HG changeset patch # Parent 2ec9db219998d01fb90073f8d4bc005e60786338 diff --git a/openide.awt/apichanges.xml b/openide.awt/apichanges.xml --- a/openide.awt/apichanges.xml +++ b/openide.awt/apichanges.xml @@ -50,6 +50,23 @@ AWT API + + + Support "selected" icons in Actions and Actions.checkbox + + + + + +

+ Handle _selected, _rolloverSelected and _disabledSelected + icon resources in Actions.connect. + Actions.checkbox on a Toolbar uses them if they are available. +

+
+ + +
New @ActionReference annotations diff --git a/openide.awt/manifest.mf b/openide.awt/manifest.mf --- a/openide.awt/manifest.mf +++ b/openide.awt/manifest.mf @@ -2,5 +2,5 @@ OpenIDE-Module: org.openide.awt OpenIDE-Module-Localizing-Bundle: org/openide/awt/Bundle.properties AutoUpdate-Essential-Module: true -OpenIDE-Module-Specification-Version: 7.31 +OpenIDE-Module-Specification-Version: 7.32 diff --git a/openide.awt/src/org/openide/awt/Actions.java b/openide.awt/src/org/openide/awt/Actions.java --- a/openide.awt/src/org/openide/awt/Actions.java +++ b/openide.awt/src/org/openide/awt/Actions.java @@ -219,15 +219,21 @@ } /** Connects buttons to action. If the action supplies value for "iconBase" - * key from getValue(String) with a path to icons the methods setIcon, - * setPressedIcon, setDisabledIcon and setRolloverIcon will be called on the + * key from getValue(String) with a path to icons, the methods set*Icon + * will be called on the * button with loaded icons using the iconBase. E.g. if the value for "iconBase" - * will be "com/mycompany/myIcon.gif" following images will be tried "com/mycompany/myIcon.gif" - * for setIcon, "com/mycompany/myIcon_pressed.gif" for setPressedIcon, - * "com/mycompany/myIcon_disabled.gif" for setDisabledIcon and - * "com/mycompany/myIcon_rollover.gif" for setRolloverIcon. SystemAction has - * special support for iconBase - please check {@link SystemAction#iconResource} - * for more details. + * is "com/mycompany/myIcon.gif" then the following images are tried + *
    + *
  • setIcon with "com/mycompany/myIcon.gif"
  • + *
  • setPressedIcon with "com/mycompany/myIcon_pressed.gif"
  • + *
  • setDisabledIcon with "com/mycompany/myIcon_disabled.gif"
  • + *
  • setRolloverIcon with "com/mycompany/myIcon_rollover.gif"
  • + *
  • setSelectedIcon with "com/mycompany/myIcon_selected.gif"
  • + *
  • setRolloverSelectedIcon with "com/mycompany/myIcon_rolloverSelected.gif"
  • + *
  • setDisabledSelectedIcon with "com/mycompany/myIcon_disabledSelected.gif"
  • + *
+ * SystemAction has special support for iconBase - please check + * {@link SystemAction#iconResource} for more details. * You can supply an alternative implementation * for this method by implementing method * {@link ButtonActionConnector#connect(AbstractButton, Action)} and @@ -236,6 +242,7 @@ * @param button the button * @param action the action * @since 3.29 + * @since 7.32 for set*SelectedIcon */ public static void connect(AbstractButton button, Action action) { for (ButtonActionConnector bac : buttonActionConnectors()) { @@ -1025,6 +1032,21 @@ } else if (imgIcon != null) { button.setDisabledIcon(ImageUtilities.createDisabledIcon(imgIcon)); } + + ImageIcon sImgIcon = ImageUtilities.loadImageIcon(insertBeforeSuffix(b, "_selected"), true); // NOI18N + if (sImgIcon != null) { + button.setSelectedIcon(sImgIcon); + } + + sImgIcon = ImageUtilities.loadImageIcon(insertBeforeSuffix(b, "_rolloverSelected"), true); // NOI18N + if (sImgIcon != null) { + button.setRolloverSelectedIcon(sImgIcon); + } + + sImgIcon = ImageUtilities.loadImageIcon(insertBeforeSuffix(b, "_disabledSelected"), true); // NOI18N + if (sImgIcon != null) { + button.setDisabledSelectedIcon(sImgIcon); + } } } diff --git a/openide.awt/src/org/openide/awt/AlwaysEnabledAction.java b/openide.awt/src/org/openide/awt/AlwaysEnabledAction.java --- a/openide.awt/src/org/openide/awt/AlwaysEnabledAction.java +++ b/openide.awt/src/org/openide/awt/AlwaysEnabledAction.java @@ -17,16 +17,19 @@ import java.util.prefs.PreferenceChangeListener; import java.util.prefs.Preferences; import javax.swing.AbstractAction; +import javax.swing.AbstractButton; import javax.swing.Action; import javax.swing.Icon; import javax.swing.JCheckBoxMenuItem; import javax.swing.JMenuItem; +import javax.swing.JToggleButton; import org.openide.util.ContextAwareAction; import org.openide.util.ImageUtilities; import org.openide.util.Lookup; import org.openide.util.LookupEvent; import org.openide.util.LookupListener; import org.openide.util.NbPreferences; +import org.openide.util.WeakSet; import org.openide.util.actions.Presenter; import org.openide.util.actions.ActionInvoker; @@ -256,7 +259,7 @@ } static final class CheckBox extends AlwaysEnabledAction - implements Presenter.Menu, Presenter.Popup, PreferenceChangeListener, LookupListener + implements Presenter.Menu, Presenter.Popup, Presenter.Toolbar, PreferenceChangeListener, LookupListener { private static final long serialVersionUID = 1L; @@ -271,6 +274,8 @@ private JCheckBoxMenuItem popupItem; + private WeakSet toolbarItems; + private Preferences preferencesNode; private Lookup.Result preferencesNodeResult; @@ -311,6 +316,17 @@ return popupItem; } + public AbstractButton getToolbarPresenter() { + if(toolbarItems == null) { + toolbarItems = new WeakSet(4); + } + AbstractButton b = new DefaultIconToggleButton(); + toolbarItems.add(b); + b.setSelected(isPreferencesSelected()); + Actions.connect(b, this); + return b; + } + public void preferenceChange(PreferenceChangeEvent pce) { updateItemsSelected(); } @@ -351,6 +367,11 @@ if (popupItem != null) { popupItem.setSelected(selected); } + if (toolbarItems != null) { + for(AbstractButton b : toolbarItems) { + b.setSelected(selected); + } + } } private synchronized Preferences prefs() { @@ -423,4 +444,25 @@ } + /** + * A button that provides a default icon when no text and no custom icon have been set. + * Copied from Toolbar.java and made a toggle button. + */ + static class DefaultIconToggleButton extends JToggleButton { + private Icon unknownIcon; + + @Override + public Icon getIcon() { + Icon retValue = super.getIcon(); + if( null == retValue && (null == getText() || getText().isEmpty()) ) { + if (unknownIcon == null) { + unknownIcon = ImageUtilities.loadImageIcon("org/openide/awt/resources/unknown.gif", false); //NOI18N + //unknownIcon = ImageUtilities.loadImageIcon("org/openide/loaders/unknown.gif", false); //NOI18N + } + retValue = unknownIcon; + } + return retValue; + } + } + } diff --git a/openide.loaders/src/org/openide/loaders/unknown.gif b/openide.awt/src/org/openide/awt/resources/unknown.gif copy from openide.loaders/src/org/openide/loaders/unknown.gif copy to openide.awt/src/org/openide/awt/resources/unknown.gif diff --git a/openide.awt/test/unit/src/org/openide/awt/ActionsTest.java b/openide.awt/test/unit/src/org/openide/awt/ActionsTest.java --- a/openide.awt/test/unit/src/org/openide/awt/ActionsTest.java +++ b/openide.awt/test/unit/src/org/openide/awt/ActionsTest.java @@ -60,6 +60,7 @@ import org.netbeans.junit.MockServices; import org.netbeans.junit.NbTestCase; import org.openide.util.HelpCtx; +import org.openide.util.ImageUtilities; import org.openide.util.Lookup; import org.openide.util.Utilities; import org.openide.util.actions.SystemAction; @@ -77,10 +78,15 @@ // colors of the testing images in this order: // (test recognizes the icon by the white/black colors in specified positions :-))) - // testIcon.gif - // testIcon_rollover.gif - // testIcon_pressed.gif - // testIcon_disabled.gif + // FIRST EIGHT (original) SECOND EIGHT (selected) + // 0 testIcon.gif 8 testIcon_selected.gif + // 1 testIcon_rollover.gif 9 testIcon_rolloverSelected.gif + // 2 testIcon_pressed.gif 10 --not-used-- + // 3 testIcon_disabled.gif 11 testIcon_disabledSelected.gif + // 4 testIcon24.gif 12 testIcon24_selected.gif + // 5 testIcon24_rollover.gif 13 testIcon24_rolloverSelected.gif + // 6 testIcon24_pressed.gif 14 --not-used-- + // 7 testIcon24_disabled.gif 15 testIcon24_disabledSelected.gif private static int[][] RESULT_COLORS_00 = { {255, 255, 255}, {0, 0, 0}, @@ -90,6 +96,14 @@ {0, 0, 0}, {255, 255, 255}, {0, 0, 0}, + {255, 255, 255}, + {0, 0, 0}, + {255, 255, 255}, + {0, 0, 0}, + {255, 255, 255}, + {0, 0, 0}, + {255, 255, 255}, + {0, 0, 0}, }; private static int[][] RESULT_COLORS_01 = { {255, 255, 255}, @@ -100,6 +114,14 @@ {255, 255, 255}, {0, 0, 0}, {0, 0, 0}, + {255, 255, 255}, + {255, 255, 255}, + {0, 0, 0}, + {0, 0, 0}, + {255, 255, 255}, + {255, 255, 255}, + {0, 0, 0}, + {0, 0, 0}, }; private static int[][] RESULT_COLORS_11 = { {255, 255, 255}, @@ -110,6 +132,32 @@ {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, + {255, 255, 255}, + {255, 255, 255}, + {255, 255, 255}, + {255, 255, 255}, + {0, 0, 0}, + {0, 0, 0}, + {0, 0, 0}, + {0, 0, 0}, + }; + private static int[][] RESULT_COLORS_10 = { + {255, 255, 255}, + {255, 255, 255}, + {255, 255, 255}, + {255, 255, 255}, + {255, 255, 255}, + {255, 255, 255}, + {255, 255, 255}, + {255, 255, 255}, + {0, 0, 0}, + {0, 0, 0}, + {0, 0, 0}, + {0, 0, 0}, + {0, 0, 0}, + {0, 0, 0}, + {0, 0, 0}, + {0, 0, 0}, }; @@ -124,22 +172,36 @@ public void testIconsAction() throws Exception { JButton jb = new JButton(); Actions.connect(jb, new TestAction()); - + Icon icon = jb.getIcon(); assertNotNull(icon); checkIfLoadedCorrectIcon(icon, jb, 0, "Enabled icon"); - + Icon rolloverIcon = jb.getRolloverIcon(); assertNotNull(rolloverIcon); checkIfLoadedCorrectIcon(rolloverIcon, jb, 1, "Rollover icon"); - + Icon pressedIcon = jb.getPressedIcon(); assertNotNull(pressedIcon); checkIfLoadedCorrectIcon(pressedIcon, jb, 2, "Pressed icon"); - + Icon disabledIcon = jb.getDisabledIcon(); assertNotNull(disabledIcon); checkIfLoadedCorrectIcon(disabledIcon, jb, 3, "Disabled icon"); + + Icon selectedIcon = jb.getSelectedIcon(); + assertNotNull(selectedIcon); + checkIfLoadedCorrectIcon(selectedIcon, jb, 8, "Selected icon"); + + Icon rolloverSelectedIcon = jb.getRolloverSelectedIcon(); + assertNotNull(rolloverSelectedIcon); + checkIfLoadedCorrectIcon(rolloverSelectedIcon, jb, 9, "RolloverSelected icon"); + + // no pressedSelected + + Icon disabledSelectedIcon = jb.getDisabledSelectedIcon(); + assertNotNull(disabledSelectedIcon); + checkIfLoadedCorrectIcon(disabledSelectedIcon, jb, 11, "DisabledSelected icon"); } /** @@ -180,19 +242,47 @@ Icon icon = jb.getIcon(); assertNotNull(icon); - checkIfLoadedCorrectIcon(icon, jb, 4, "Enabled icon"); + checkIfLoadedCorrectIcon(icon, jb, 4, "Enabled icon24"); Icon rolloverIcon = jb.getRolloverIcon(); assertNotNull(rolloverIcon); - checkIfLoadedCorrectIcon(rolloverIcon, jb, 5, "Rollover icon"); + checkIfLoadedCorrectIcon(rolloverIcon, jb, 5, "Rollover icon24"); Icon pressedIcon = jb.getPressedIcon(); assertNotNull(pressedIcon); - checkIfLoadedCorrectIcon(pressedIcon, jb, 6, "Pressed icon"); + checkIfLoadedCorrectIcon(pressedIcon, jb, 6, "Pressed icon24"); Icon disabledIcon = jb.getDisabledIcon(); assertNotNull(disabledIcon); - checkIfLoadedCorrectIcon(disabledIcon, jb, 7, "Disabled icon"); + checkIfLoadedCorrectIcon(disabledIcon, jb, 7, "Disabled icon24"); + + Icon selectedIcon = jb.getSelectedIcon(); + assertNotNull(selectedIcon); + checkIfLoadedCorrectIcon(selectedIcon, jb, 12, "Selected icon24"); + + Icon rolloverSelectedIcon = jb.getRolloverSelectedIcon(); + assertNotNull(rolloverSelectedIcon); + checkIfLoadedCorrectIcon(rolloverSelectedIcon, jb, 13, "RolloverSelected icon24"); + + // no pressedSelected + + Icon disabledSelectedIcon = jb.getDisabledSelectedIcon(); + assertNotNull(disabledSelectedIcon); + checkIfLoadedCorrectIcon(disabledSelectedIcon, jb, 15, "DisabledSelected icon24"); + } + + /** + * Tests that "unknownIcon" is used if no iconBase + */ + public void testToggleButtonUnknownIcon() throws Exception { + AbstractButton b = new AlwaysEnabledAction.DefaultIconToggleButton(); + Action action = new TestAction(); + action.putValue("iconBase", null); + Actions.connect(b, action); + Icon icon = b.getIcon(); + assertNotNull("null ToggleButton icon", icon); + Icon expectedIcon = ImageUtilities.loadImageIcon("org/openide/awt/resources/unknown.gif", false); //NOI18N + assertEquals("unkownIcon not used", expectedIcon, icon); } /** @@ -437,6 +527,7 @@ checkIfIconOk(icon, c, 0, 0, RESULT_COLORS_00[rowToCheck], nameOfIcon); checkIfIconOk(icon, c, 0, 1, RESULT_COLORS_01[rowToCheck], nameOfIcon); checkIfIconOk(icon, c, 1, 1, RESULT_COLORS_11[rowToCheck], nameOfIcon); + checkIfIconOk(icon, c, 1, 0, RESULT_COLORS_10[rowToCheck], nameOfIcon); } /** diff --git a/openide.awt/test/unit/src/org/openide/awt/data/testIcon24_disabledSelected.gif b/openide.awt/test/unit/src/org/openide/awt/data/testIcon24_disabledSelected.gif new file mode 100644 index 0000000000000000000000000000000000000000..c2468dab1a99e4adcc1ad7f5359fa2558754447f GIT binary patch literal 66 zc${jEB<6*IEY0o;B1u-&M0|1UE8y^4w diff --git a/openide.awt/test/unit/src/org/openide/awt/data/testIcon_disabledSelected.gif b/openide.awt/test/unit/src/org/openide/awt/data/testIcon_disabledSelected.gif new file mode 100644 index 0000000000000000000000000000000000000000..c30776d1f5ddbc9272dcd27c7aae09c33f557288 GIT binary patch literal 73 zc${