Java Code Examples for javax.swing.Action#addPropertyChangeListener()
The following examples show how to use
javax.swing.Action#addPropertyChangeListener() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ContextManagerTest.java From netbeans with Apache License 2.0 | 7 votes |
public void testSurviveFocusChange() throws Exception { InstanceContent ic = new InstanceContent(); Lookup lkp = new AbstractLookup(ic); Action clone = ((ContextAwareAction) Actions.forID("cat", "survive")).createContextAwareInstance(lkp); L listener = new L(); clone.addPropertyChangeListener(listener); assertFalse("Disabled", clone.isEnabled()); Object val = Integer.valueOf(1); ic.add(val); assertTrue("Enabled now", clone.isEnabled()); assertEquals("One change", 1, listener.cnt); ic.remove(val); assertTrue("Still Enabled", clone.isEnabled()); Survival.value = 0; clone.actionPerformed(new ActionEvent(this, 0, "")); assertEquals("Added one", 1, Survival.value); }
Example 2
Source File: LinkButton.java From pgptool with GNU General Public License v3.0 | 6 votes |
@Override public void setAction(Action newAction) { if (action != null) { action.removePropertyChangeListener(listener); } action = newAction; if (newAction != null) { setEnabled(newAction.isEnabled()); newAction.addPropertyChangeListener(listener); setText((String) action.getValue(Action.NAME)); } else { setEnabled(false); } }
Example 3
Source File: EditorFrame.java From settlers-remake with MIT License | 6 votes |
/** * Create a menu item for a specific action * * @param action * The action * @param menuActionName * The name of the action * @param menu * The menu to add the action */ private void createMenuItemForAction(final Action action, String menuActionName, JMenu menu) { final JMenuItem it; Boolean displayAsCheckbox = (Boolean) action.getValue(EditorFrame.DISPLAY_CHECKBOX); if (displayAsCheckbox != null && displayAsCheckbox) { it = createCheckboxMenuItemForAction(action); menu.add(it); } else { it = menu.add(action); } action.addPropertyChangeListener(evt -> { if (Action.NAME.equals(evt.getPropertyName())) { it.setText((String) evt.getNewValue()); } }); it.setText((String) action.getValue(Action.NAME)); String shortcut = this.shortcut.getProperty(menuActionName); if (shortcut != null) { it.setAccelerator( KeyStroke.getKeyStroke(shortcut)); } }
Example 4
Source File: CallbackActionTest.java From netbeans with Apache License 2.0 | 6 votes |
public void testCopyLikeProblem() throws Exception { FileObject fo = folder.getFileObject("testCopyLike.instance"); Object obj = fo.getAttribute("instanceCreate"); if (!(obj instanceof Action)) { fail("Shall create an action: " + obj); } InstanceContent ic = new InstanceContent(); AbstractLookup l = new AbstractLookup(ic); ActionMap map = new ActionMap(); map.put("copy-to-clipboard", new MyAction()); ic.add(map); CntListener list = new CntListener(); Action clone = ((ContextAwareAction)obj).createContextAwareInstance(l); clone.addPropertyChangeListener(list); assertTrue("Enabled", clone.isEnabled()); ic.remove(map); assertFalse("Disabled", clone.isEnabled()); list.assertCnt("one change", 1); }
Example 5
Source File: Actions.java From netbeans with Apache License 2.0 | 6 votes |
public static synchronized Action deleteProject() { final Action a = new ProjectAction( ActionProvider.COMMAND_DELETE, NbBundle.getMessage(Actions.class, "LBL_DeleteProjectAction_Name"), null, null ); try { final Action delete = org.openide.awt.Actions.forID("Edit", "org.openide.actions.DeleteAction"); a.putValue(Action.ACCELERATOR_KEY, delete.getValue(Action.ACCELERATOR_KEY)); delete.addPropertyChangeListener(new PropertyChangeListener() { public @Override void propertyChange(PropertyChangeEvent evt) { a.putValue(Action.ACCELERATOR_KEY, delete.getValue(Action.ACCELERATOR_KEY)); } }); } catch (Exception x) { Exceptions.printStackTrace(x); } return a; }
Example 6
Source File: DelegateAction.java From netbeans with Apache License 2.0 | 6 votes |
protected void setDelegate(Action delegate){ if (this.delegate == delegate) return; if (delegate == this) throw new IllegalStateException("Cannot delegate on the same action"); // NOI18N if (this.delegate != null){ this.delegate.removePropertyChangeListener(pcl); } if (delegate != null) { delegate.addPropertyChangeListener(pcl); } this.delegate = delegate; setEnabled((delegate != null) ? delegate.isEnabled() : false); }
Example 7
Source File: GlobalManagerTest.java From netbeans with Apache License 2.0 | 6 votes |
public void testActionsUpdatedWhenActionMapChanges() throws Exception { ContextAwareAction a = Actions.callback("ahoj", null, true, "Ahoj!", "no/icon.png", true); final InstanceContent ic = new InstanceContent(); Lookup lkp = new AbstractLookup(ic); ActionMap a1 = new ActionMap(); ActionMap a2 = new ActionMap(); a2.put("ahoj", new Enabled()); ic.add(a1); Action clone = a.createContextAwareInstance(lkp); class L implements PropertyChangeListener { int cnt; public void propertyChange(PropertyChangeEvent evt) { cnt++; } } L listener = new L(); clone.addPropertyChangeListener(listener); assertFalse("Not enabled", isEnabled(clone)); ic.set(Collections.singleton(a2), null); assertTrue("Enabled now", isEnabled(clone)); assertEquals("one change", 1, listener.cnt); }
Example 8
Source File: PresenterUpdater.java From netbeans with Apache License 2.0 | 6 votes |
public void setActiveAction(Action a) { if (a == action) { // In this case there is in fact no extra context a = null; } synchronized (this) { if (a != contextAction) { if (LOG.isLoggable(Level.FINE)) { LOG.fine("setActiveAction(): from " + contextAction + " to " + a + "\n"); // NOI18N } contextAction = a; if (a != null && !listenedContextActions.contains(a)) { listenedContextActions.add(a); a.addPropertyChangeListener(WeakListeners.propertyChange(this, a)); if (LOG.isLoggable(Level.FINE)) { LOG.fine("setActiveAction(): started listening on " + a + "\n"); // NOI18N } } updatePresenter(null); // Update presenter completely } } }
Example 9
Source File: CallbackSystemAction.java From netbeans with Apache License 2.0 | 6 votes |
public boolean isEnabled() { Action a = findAction(); if (a == null) { a = delegate; } // 40915 - hold last action weakly Action last = lastRef == null ? null : lastRef.get(); if (a != last) { if (last != null) { last.removePropertyChangeListener(weakL); } lastRef = new WeakReference<Action>(a); a.addPropertyChangeListener(weakL); } return a.isEnabled(); }
Example 10
Source File: PresenterUpdater.java From netbeans with Apache License 2.0 | 5 votes |
private PresenterUpdater(int type, Action action) { if (action == null) { throw new IllegalArgumentException("action must not be null"); // NOI18N } this.type = type; this.actionName = (String) action.getValue(Action.NAME); this.action = action; if (type == TOOLBAR) { presenter = new JButton(); useActionSelectedProperty = false; } else { // MENU or POPUP useActionSelectedProperty = (action.getValue(AbstractEditorAction.PREFERENCES_KEY_KEY) != null); if (useActionSelectedProperty) { presenter = new LazyJCheckBoxMenuItem(); presenter.setSelected(isActionSelected()); } else { presenter = new LazyJMenuItem(); } } action.addPropertyChangeListener(WeakListeners.propertyChange(this, action)); if (type == MENU) { listenedContextActions = new WeakSet<Action>(); EditorRegistryWatcher.get().registerPresenterUpdater(this); // Includes notification of active component } else { listenedContextActions = null; } presenter.addActionListener(this); updatePresenter(null); // Not active yet => mark updates pending }
Example 11
Source File: UndoRedoActionTest.java From netbeans with Apache License 2.0 | 5 votes |
private void doUndoRedoTest(UndoRedo.Manager man, boolean testCounts) { me = new MyEdit(); man.undoableEditHappened(new UndoableEditEvent(this, me)); assertTrue("Can undo", man.canUndo()); this.ur = man; InstanceContent ic = new InstanceContent(); AbstractLookup lkp = new AbstractLookup(ic); Action u = undoAction(lkp); Action r = redoAction(lkp); assertFalse("Not enabled", u.isEnabled()); assertFalse("Not enabledR", r.isEnabled()); MyEdit lu = new MyEdit(); MyEdit lr = new MyEdit(); u.addPropertyChangeListener(lu); r.addPropertyChangeListener(lr); ic.add(this); assertTrue("Action is enabled", u.isEnabled()); assertEquals("One change", 1, lu.cnt); assertEquals("No redo change", 0, lr.cnt); assertEquals("Undo presentation", "&Undo My Undo", u.getValue(Action.NAME)); u.actionPerformed(new ActionEvent(this, 0, "")); if (testCounts) { assertEquals("my edit undone", 1, me.undo); assertFalse("No more undo", man.canUndo()); assertTrue("But redo", man.canRedo()); assertEquals("Another undo change", 2, lu.cnt); assertEquals("New redo change", 1, lr.cnt); assertTrue("Redo action enabled", r.isEnabled()); assertEquals("Redo presentation correct", "&Redo My Redo", r.getValue(Action.NAME)); } r.actionPerformed(new ActionEvent(this, 0, "")); assertFalse("Redo action no longer enabled", r.isEnabled()); }
Example 12
Source File: ExplorerActionsImplTest.java From netbeans with Apache License 2.0 | 5 votes |
@RandomlyFails // NB-Core-Build #9619, #9847, #9998, #10014 public void testPasteActionGetDelegatesBlocks() throws Exception { N root = new N(); final N ch1 = new N(); final N ch2 = new N(); final N ch3 = new N(); PT mockPaste = new PT(); ch3.pasteTypes = Collections.<PasteType>singletonList(mockPaste); root.getChildren().add(new Node[] { ch1, ch2, ch3 }); final ExplorerManager em = new ExplorerManager(); em.setRootContext(root); em.setSelectedNodes(new Node[] { root }); Action action = ExplorerUtils.actionPaste(em); em.waitActionsFinished(); assertFalse("Not enabled", action.isEnabled()); action.addPropertyChangeListener(this); assertNull("No delegates yet", action.getValue("delegates")); em.setSelectedNodes(new Node[] { ch3 }); Object ret = action.getValue("delegates"); assertNotNull("Delegates are updated", ret); Object[] arr = (Object[])ret; assertEquals("One item in there", 1, arr.length); if (err != null) { throw err; } }
Example 13
Source File: ContextActionTest.java From netbeans with Apache License 2.0 | 5 votes |
private ActionsInfraHid.WaitPCL doBasicUsageWithEnabler(Action operateOn) throws Exception { // Check enablement logic. ActionsInfraHid.WaitPCL l = new ActionsInfraHid.WaitPCL("enabled"); operateOn.addPropertyChangeListener(l); assertFalse(getIsEnabled(operateOn)); activate(new Lookup[] {n1}); assertFalse("We need two nodes to become enabled", l.changed()); l.gotit = 0; assertFalse("and there is just one", getIsEnabled(operateOn)); activate(new Lookup[] {n1, n2}); assertTrue("Ok, now we are enabled", l.changed()); l.gotit = 0; assertTrue("Yes", getIsEnabled(operateOn)); activate(new Lookup[] {n2}); assertTrue("Disabled again", l.changed()); l.gotit = 0; assertFalse("Disabled", getIsEnabled(operateOn)); activate(new Lookup[] {n3}); assertFalse(l.changed()); l.gotit = 0; assertFalse(getIsEnabled(operateOn)); activate(new Lookup[] {n3}); assertFalse("Again not changed", l.changed()); l.gotit = 0; assertFalse(getIsEnabled(operateOn)); activate(new Lookup[] {n1}); assertFalse(l.changed()); l.gotit = 0; assertFalse(getIsEnabled(operateOn)); activate(new Lookup[] {n1}); assertFalse("No change", l.changed()); l.gotit = 0; assertFalse(getIsEnabled(operateOn)); activate(new Lookup[] {n1, n2}); assertTrue("now there is enabledment", l.changed()); l.gotit = 0; assertTrue(getIsEnabled(operateOn)); return l; }
Example 14
Source File: WrapperBookmarkAction.java From netbeans with Apache License 2.0 | 5 votes |
public WrapperBookmarkAction(Action originalAction) { this.originalAction = originalAction; putValue(Action.NAME, originalAction.getValue(Action.SHORT_DESCRIPTION)); putValue(Action.SHORT_DESCRIPTION, Actions.cutAmpersand( (String) originalAction.getValue(Action.SHORT_DESCRIPTION))); putValue(Action.SMALL_ICON, ImageUtilities.loadImageIcon( (String) originalAction.getValue(BaseAction.ICON_RESOURCE_PROPERTY),false)); putValue("noIconInMenu", Boolean.TRUE); // NOI18N // Re-add the property as SystemAction.putValue() is final // putValue(BaseAction.ICON_RESOURCE_PROPERTY, getValue(BaseAction.ICON_RESOURCE_PROPERTY)); updateEnabled(); originalAction.addPropertyChangeListener(this); }
Example 15
Source File: ContextActionTest.java From netbeans with Apache License 2.0 | 5 votes |
public void testContextInstancesAreIndependent() throws Exception { System.out.println("testContextInstancesAreIndependent"); A a = new A(); assertNull(Utilities.actionsGlobalContext().lookup(String.class)); //sanity check InstanceContent ic = new InstanceContent(); Lookup l = new AbstractLookup(ic); Action a3 = a.createContextAwareInstance(l); assertFalse(a3.isEnabled()); PCL pcl = new PCL(); a3.addPropertyChangeListener(pcl); setContent("fuddle"); a.assertNotPerformed(); assertTrue(a.isEnabled()); assertFalse(a3.isEnabled()); synchronized (a3) { //should time out if test is going to pass a3.wait(TIMEOUT); } synchronized (pcl) { pcl.wait(TIMEOUT); } pcl.assertNotFired(); ic.set(Collections.singleton("boo"), null); synchronized (a3) { a3.wait(TIMEOUT); } synchronized (pcl) { pcl.wait(TIMEOUT); } pcl.assertEnabledChangedTo(true); clearContent(); assertTrue(a3.isEnabled()); assertFalse(a.isEnabled()); }
Example 16
Source File: ContextActionTest.java From netbeans with Apache License 2.0 | 5 votes |
public void testMergeHandlesDisplayNameCorrectly() throws Exception { System.out.println("testMergeHandlesDisplayNameCorrectly"); F f = new F(); G g = new G(); // MergeAction m = new MergeAction(new ContextAction[]{f, g}); Action m = ContextAction.merge(f, g); m.addPropertyChangeListener(new PCL()); Thing thing1 = new Thing("tx1"); Thing thing2 = new Thing("tx2"); OtherThing otherThing1 = new OtherThing("ox1"); OtherThing otherThing2 = new OtherThing("ox2"); assertEquals("Do something", m.getValue(Action.NAME)); assertFalse(m.isEnabled()); setContent(thing1); assertTrue(m.isEnabled()); assertEquals("Do something to tx1", m.getValue(Action.NAME)); setContent(otherThing1); assertTrue(m.isEnabled()); assertEquals("Do something else to ox1", m.getValue(Action.NAME)); setContent(thing1); assertTrue(m.isEnabled()); assertEquals("Do something to tx1", m.getValue(Action.NAME)); setContent(thing2); assertTrue(m.isEnabled()); assertEquals("Do something to tx2", m.getValue(Action.NAME)); clearContent(); setContent(thing1, thing2); assertEquals("Do something to 2 Things", m.getValue(Action.NAME)); setContent(new Object()); assertFalse(m.isEnabled()); assertEquals("Do something", m.getValue(Action.NAME)); setContent(otherThing1, otherThing2); assertEquals("Do something else to 2 OtherThings", m.getValue(Action.NAME)); }
Example 17
Source File: LookupSensitiveActionBase.java From netbeans with Apache License 2.0 | 4 votes |
public void testStackOverFlow() throws IOException { InstanceContent ic = new InstanceContent(); Lookup context = new AbstractLookup(ic); boolean clone = false; Action instance; if (clone) { Action a = create(Lookup.EMPTY); instance = ((ContextAwareAction)a).createContextAwareInstance(context); } else { instance = create(context); } FileObject pfo = TestSupport.createTestProject(FileUtil.createMemoryFileSystem().getRoot(), "yaya"); FileObject pf2 = TestSupport.createTestProject(FileUtil.createMemoryFileSystem().getRoot(), "blabla"); MockServices.setServices(TestSupport.TestProjectFactory.class); Project p = ProjectManager.getDefault().findProject(pfo); Project p2 = ProjectManager.getDefault().findProject(pf2); if (p instanceof TestSupport.TestProject) { enhanceProject((TestSupport.TestProject)p); } if (p2 instanceof TestSupport.TestProject) { enhanceProject((TestSupport.TestProject)p2); } assertNotNull("Project found", p); assertNotNull("Project2 found", p2); OpenProjects.getDefault().open(new Project[] { p }, false); assertFalse("Disabled1", instance.isEnabled()); instance.addPropertyChangeListener(this); ic.add(p); assertTrue("Enabled", instance.isEnabled()); assertEquals("One change", 1, change); class Q implements PropertyChangeListener { Action i; int cnt; @Override public void propertyChange(PropertyChangeEvent evt) { if ("enabled".equals(evt.getPropertyName())) { cnt++; /* What is this for? Often fails during unit tests (but tests pass). assertTrue("enabled in listener", i.isEnabled()); */ } } } Q q = new Q(); q.i = instance; ic.remove(p); instance.removePropertyChangeListener(this); ic.add(p); instance.addPropertyChangeListener(q); assertTrue("Enabled", instance.isEnabled()); assertEquals("One call", 1, q.cnt); }
Example 18
Source File: TopComponentGetLookupTest.java From netbeans with Apache License 2.0 | 4 votes |
public void testActionMapFromFocusedOneButNotOwnButton() { ContextAwareAction a = Actions.callback("find", null, false, "Find", null, false); class Act extends AbstractAction { int cnt; Action check; @Override public void actionPerformed(ActionEvent ev) { cnt++; } } Act act1 = new Act(); Act act3 = new Act(); Action action = a; act1.check = action; act3.check = action; final JButton disabled = new JButton(); top.add(BorderLayout.CENTER, disabled); final JButton f = new JButton(action); class L implements PropertyChangeListener { private int cnt; @Override public void propertyChange(PropertyChangeEvent evt) { assertEquals("enabled", evt.getPropertyName()); cnt++; } } L listener = new L(); action.addPropertyChangeListener(listener); top.add(BorderLayout.SOUTH, f); defaultFocusManager.setC(top); disabled.getActionMap().put("find", act3); top.open(); top.requestActive(); assertFalse("Disabled by default", action.isEnabled()); assertFalse("Button disabled too", f.isEnabled()); assertEquals("no change yet", 0, listener.cnt); defaultFocusManager.setC(disabled); assertEquals("One change", 1, listener.cnt); assertTrue("Still enabled", action.isEnabled()); assertTrue("Button enabled too", f.isEnabled()); f.getModel().addChangeListener(new ChangeListener() { @Override public void stateChanged(ChangeEvent e) { if (f.getModel().isPressed()) { defaultFocusManager.setC(f); } else { defaultFocusManager.setC(disabled); } } }); f.doClick(); assertEquals("Not Delegated to act1", 0, act1.cnt); assertEquals("Delegated to act3", 1, act3.cnt); }
Example 19
Source File: CallbackSystemActionTest.java From netbeans with Apache License 2.0 | 4 votes |
public void testLookupOfStateInActionMap() throws Exception { class MyAction extends AbstractAction implements ActionPerformer { int actionPerformed; int performAction; public void actionPerformed(ActionEvent ev) { actionPerformed++; } public void performAction(SystemAction a) { performAction++; } } MyAction action = new MyAction(); ActionMap map = new ActionMap(); CallbackSystemAction system = (CallbackSystemAction)SystemAction.get(SurviveFocusChgCallbackAction.class); system.setActionPerformer(null); map.put(system.getActionMapKey(), action); Action clone; // // Without action map // clone = system.createContextAwareInstance(Lookup.EMPTY); assertTrue("Action should not be enabled if no callback provided", !clone.isEnabled()); system.setActionPerformer(action); assertTrue("Is enabled, because it has a performer", clone.isEnabled()); system.setActionPerformer(null); assertTrue("Is disabled, because the performer has been unregistered", !clone.isEnabled()); // // test with actionmap // action.setEnabled(false); Lookup context = Lookups.singleton(map); clone = system.createContextAwareInstance(context); MockPropertyChangeListener listener = new MockPropertyChangeListener(); clone.addPropertyChangeListener(listener); assertTrue("Not enabled now", !clone.isEnabled()); action.setEnabled(true); assertTrue("Clone is enabled because the action in ActionMap is", clone.isEnabled()); listener.assertEventCount(1); system.setActionPerformer(action); clone.actionPerformed(new ActionEvent(this, 0, "")); assertEquals("MyAction.actionPerformed invoked", 1, action.actionPerformed); assertEquals("MyAction.performAction is not invoked", 0, action.performAction); action.setEnabled(false); assertTrue("Clone is disabled because the action in ActionMap is", !clone.isEnabled()); listener.assertEventCount(1); clone.actionPerformed(new ActionEvent(this, 0, "")); assertEquals("MyAction.actionPerformed invoked again", 2, action.actionPerformed); assertEquals("MyAction.performAction is not invoked, remains 0", 0, action.performAction); }
Example 20
Source File: ContextActionTest.java From netbeans with Apache License 2.0 | 4 votes |
public void testContextDelegate() throws Exception { TestData data = new TestData(); data.setStateValue(true); TestData otherData = new TestData(); Action baseA = Actions.forID("Test", "CheckedTest"); // must change main action Lookup after the main delegate exists, // so it gets property change and changes its Action.SELECTED_KEY // otherwise the .map is null and action just delegates to Fallback activate(Lookups.fixed(data)); assertSame(data, Utilities.actionsGlobalContext().lookup(TestData.class)); InstanceContent ic = new InstanceContent(); Lookup context = new AbstractLookup(ic); ic.add(data); assertTrue(getIsEnabled(baseA)); assertTrue(getIsChecked(baseA)); Action actionA = ((ContextAwareAction)baseA).createContextAwareInstance(context); assertTrue(getIsEnabled(actionA)); assertTrue(getIsChecked(actionA)); // let's have completely different local context: Lookup context2 = Lookups.fixed(otherData); Action actionB = ((ContextAwareAction)baseA).createContextAwareInstance(context2); assertTrue(getIsEnabled(actionB)); // in this context, action should be enabled, but UNchecked. assertFalse(getIsChecked(actionB)); class PCL implements PropertyChangeListener { Set<String> propChanges = Collections.synchronizedSet(new HashSet<>()); @Override public void propertyChange(PropertyChangeEvent evt) { String n = evt.getPropertyName(); if (n != null) { propChanges.add(n); } } } PCL listenerBase = new PCL(); PCL listenerA = new PCL(); PCL listenerB = new PCL(); baseA.addPropertyChangeListener(listenerBase); actionA.addPropertyChangeListener(listenerA); actionB.addPropertyChangeListener(listenerB); TestData data3 = new TestData(); // the data has property false, so actionA should fire & change, not the other ones ic.set(Collections.singleton(data3), null); // also potentially replans to AWT, so the pending change event from lookup // will be probably processed. assertFalse(getIsChecked(actionA)); assertTrue(getIsChecked(baseA)); assertTrue(listenerBase.propChanges.isEmpty()); assertTrue(listenerB.propChanges.isEmpty()); assertTrue(listenerA.propChanges.contains(Action.SELECTED_KEY)); listenerA.propChanges.clear(); otherData.setStateValue(true); // again sync with AWT assertTrue(getIsChecked(baseA)); assertTrue(getIsChecked(actionB)); assertTrue(listenerBase.propChanges.isEmpty()); assertTrue(listenerA.propChanges.isEmpty()); assertTrue(listenerB.propChanges.contains(Action.SELECTED_KEY)); }