private static void close(final Project prj) { Mutex.EVENT.readAccess(new Mutex.Action<Object>() { public Object run() { LifecycleManager.getDefault().saveAll(); Action closeAction = CommonProjectActions.closeProjectAction(); closeAction = closeAction instanceof ContextAwareAction ? ((ContextAwareAction) closeAction).createContextAwareInstance(Lookups.fixed(new Object[] {prj})) : null; if (closeAction != null && closeAction.isEnabled()) { closeAction.actionPerformed(new ActionEvent(prj, -1, "")); // NOI18N } else { //fallback: OpenProjects.getDefault().close(new Project[] {prj}); } return null; } }); }
public void testOperationActions() throws Exception { // #72397 final NbModuleProject project = generateStandaloneModule("module"); cgpi.setProject(project); DialogDisplayerImpl dd = (DialogDisplayerImpl) Lookup.getDefault().lookup(DialogDisplayer.class); FileObject lock = FileUtil.createData(project.getProjectDirectory(), "build/testuserdir/lock"); RandomAccessFile raf = new RandomAccessFile(FileUtil.toFile(lock), "rw"); FileLock lck = raf.getChannel().lock(); EventQueue.invokeAndWait(new Runnable() { @Override public void run() { ((ContextAwareAction) CommonProjectActions.deleteProjectAction()).createContextAwareInstance(Lookups.singleton(project)).actionPerformed(null); } }); assertNotNull("warning message emitted", dd.getLastNotifyDescriptor()); assertEquals("warning message emitted", dd.getLastNotifyDescriptor().getMessage(), Bundle.ERR_ModuleIsBeingRun()); dd.reset(); lck.release(); raf.close(); lock.delete(); EventQueue.invokeAndWait(new Runnable() { @Override public void run() { CommonProjectActions.deleteProjectAction().actionPerformed(null); } }); assertNull("no warning message", dd.getLastNotifyDescriptor()); }
private void assertAction(String text, Action action, boolean b, Node n) throws Exception { final Action clone = action instanceof ContextAwareAction ? ((ContextAwareAction)action).createContextAwareInstance(n.getLookup()) : action; assertTrue("Expecting " + text + " but was " + action, action.getClass().getName().contains(text)); class Is implements Runnable { boolean is; public void run() { is = clone.isEnabled(); } } Is enabled = new Is(); SwingUtilities.invokeAndWait(enabled); assertEquals("Enabled? " + text + " and: " + b, b, enabled.is); }
@ActionID(id = "org.netbeans.modules.maven.customPopup", category = "Project") @ActionRegistration(displayName = "#LBL_Custom_Run", lazy=false) @ActionReferences({ @ActionReference(position = 1400, path = "Projects/org-netbeans-modules-maven/Actions"), @ActionReference(position = 250, path = "Loaders/text/x-maven-pom+xml/Actions"), @ActionReference(position = 1296, path = "Loaders/text/x-java/Actions"), @ActionReference(position = 1821, path = "Editors/text/x-java/Popup") }) @Messages({"LBL_Custom_Run=Run Maven", "LBL_Custom_Run_File=Run Maven"}) public static ContextAwareAction customPopupActions() { return new ConditionallyShownAction() { protected @Override Action forProject(Project p, FileObject fo) { ActionProviderImpl ap = p.getLookup().lookup(ActionProviderImpl.class); return ap != null ? ap.new CustomPopupActions(triggeredOnFile, triggeredOnPom, fo) : null; } }; }
@ActionID(id = "org.netbeans.modules.maven.closeSubprojects", category = "Project") @ActionRegistration(displayName = "#ACT_CloseRequired", lazy=false) @ActionReference(position = 2000, path = "Projects/org-netbeans-modules-maven/Actions") @Messages("ACT_CloseRequired=Close Required Projects") public static ContextAwareAction closeSubprojectsAction() { return new ConditionallyShownAction() { protected @Override Action forProject(Project p, FileObject fo) { NbMavenProjectImpl project = p.getLookup().lookup(NbMavenProjectImpl.class); if (project != null && NbMavenProject.TYPE_POM.equalsIgnoreCase(project.getProjectWatcher().getPackagingType())) { return new CloseSubprojectsAction(project); } else { return null; } } }; }
public void testActionStatusUpdatedOnLookupChange() throws Exception { final InstanceContent content = new InstanceContent(); Lookup lkp = new AbstractLookup( content ); final SaveAsCapable saveAsImpl = new SaveAsCapable() { public void saveAs(FileObject folder, String name) throws IOException { throw new UnsupportedOperationException("Not supported yet."); } }; TopComponent tc = new TopComponent( lkp ); editorMode.dockInto( tc ); tc.open(); tc.requestActive(); assertTrue(Arrays.asList(WindowManager.getDefault().getOpenedTopComponents(editorMode)).contains(tc)); ContextAwareAction action = SaveAsAction.create(); assertFalse( "action is disabled for editor windows without SaveAsCapable in their Lookup", action.isEnabled() ); Action a = action.createContextAwareInstance( tc.getLookup() ); content.add( saveAsImpl ); assertTrue( "action is enabled for editor windows with SaveAsCapable in their Lookup", a.isEnabled() ); content.remove( saveAsImpl ); assertFalse( "action is disabled for editor windows without SaveAsCapable in their Lookup", a.isEnabled() ); }
/** Requests refresh of our lookup everytime component is chagned. */ public void propertyChange(java.beans.PropertyChangeEvent evt) { if (TopComponent.Registry.PROP_ACTIVATED.equals (evt.getPropertyName())) { org.openide.util.Utilities.actionsGlobalContext ().lookup (javax.swing.ActionMap.class); } if ("permanentFocusOwner".equals(evt.getPropertyName())) { Component[] arr = { (Component)evt.getNewValue() }; if (arr[0] instanceof AbstractButton) { Action a = ((AbstractButton)arr[0]).getAction(); if (a instanceof ContextAwareAction) { // ignore focus change into a button with our action return; } } blickActionMap(null, arr); } }
public void testCanTCGarbageCollectWhenActionInMap() { TopComponent tc = new TopComponent(); class CAA extends AbstractAction implements ContextAwareAction { public void actionPerformed(ActionEvent arg0) { throw new UnsupportedOperationException("Not supported yet."); } public Action createContextAwareInstance(Lookup actionContext) { return this; } } ContextAwareAction del = new CAA(); ContextAwareAction context = Actions.context(Integer.class, true, true, del, null, "DisplayName", null, true); Action a = context.createContextAwareInstance(tc.getLookup()); tc.getActionMap().put("key", a); WeakReference<Object> ref = new WeakReference<Object>(tc); tc = null; a = null; del = null; context = null; assertGC("Can the component GC?", ref); }
public void testCanTCGarbageCollectWhenActionInMapAndAssignLookup() { TopComponent tc = new TopComponent(); class CAA extends AbstractAction implements ContextAwareAction { public void actionPerformed(ActionEvent arg0) { throw new UnsupportedOperationException("Not supported yet."); } public Action createContextAwareInstance(Lookup actionContext) { return this; } } tc.associateLookup(Lookups.fixed(tc.getActionMap(), tc)); ContextAwareAction del = new CAA(); ContextAwareAction context = Actions.context(Integer.class, true, true, del, null, "DisplayName", null, true); Action a = context.createContextAwareInstance(tc.getLookup()); tc.getActionMap().put("key", a); WeakReference<Object> ref = new WeakReference<Object>(tc); tc = null; a = null; del = null; context = null; assertGC("Can the component GC?", ref); }
public void testContextAction() throws Exception { Action a = Actions.forID("Tools", "on.int"); assertTrue("It is context aware action", a instanceof ContextAwareAction); InstanceContent ic = new InstanceContent(); AbstractLookup lkp = new AbstractLookup(ic); Action clone = ((ContextAwareAction) a).createContextAwareInstance(lkp); NumberLike ten = new NumberLike(10); ic.add(ten); assertEquals("Number lover!", clone.getValue(Action.NAME)); clone.actionPerformed(new ActionEvent(this, 300, "")); assertEquals("Global Action not called", 10, Context.cnt); ic.remove(ten); clone.actionPerformed(new ActionEvent(this, 200, "")); assertEquals("Global Action stays same", 10, Context.cnt); }
public void testMultiContextAction() throws Exception { ContextAwareAction a = (ContextAwareAction) Actions.forID("Tools", "on.numbers"); InstanceContent ic = new InstanceContent(); AbstractLookup lkp = new AbstractLookup(ic); Action clone = a.createContextAwareInstance(lkp); NumberLike ten = new NumberLike(10); NumberLike three = new NumberLike(3); ic.add(ten); ic.add(three); assertEquals("Number lover!", clone.getValue(Action.NAME)); clone.actionPerformed(new ActionEvent(this, 300, "")); assertEquals("Global Action not called", 13, MultiContext.cnt); ic.remove(ten); clone.actionPerformed(new ActionEvent(this, 200, "")); assertEquals("Adds 3", 16, MultiContext.cnt); ic.remove(three); assertFalse("It is disabled", clone.isEnabled()); clone.actionPerformed(new ActionEvent(this, 200, "")); assertEquals("No change", 16, MultiContext.cnt); }
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); }
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); }
public void testListenerGCed () throws Exception { InstanceContent ic = new InstanceContent(); lkp = new AbstractLookup(ic); Lookup.Result<Integer> lookupResult = lkp.lookupResult(Integer.class); Action action = ((ContextAwareAction) Actions.forID("cat", "survive")).createContextAwareInstance(lkp); Action fallbackAction = ((GeneralAction.DelegateAction) action).fallback; WeakReference<Action> fallbackActionRef = new WeakReference<Action>(fallbackAction); WeakReference<Action> clone = new WeakReference<Action>(action); cm = ContextManager.findManager(lkp, true); WeakReference lsetRef = new WeakReference<Object>(cm.findLSet(Integer.class)); action = null; assertGC("Action should be GCed", clone); fallbackAction = null; assertGC("Fallback action should be GCed", fallbackActionRef); assertGC("Action LSet Should be GCed", lsetRef); lookupResult.allInstances(); }
public void testBasicUsageWithEnablerFromLayer() throws Exception { FileObject folder; folder = FileUtil.getConfigFile("actions/support/test"); assertNotNull("testing layer is loaded: ", folder); FileObject fo = folder.getFileObject("testContextEnabler.instance"); Object obj = fo.getAttribute("instanceCreate"); if (!(obj instanceof ContextAwareAction)) { fail("Shall create an action: " + obj); } ContextAwareAction caa = (ContextAwareAction)obj; Action action = caa.createContextAwareInstance(lookupProxy); doBasicUsageWithEnabler(action); }
public void testContextAction() throws Exception { Context.cnt = 0; FileObject fo = FileUtil.getConfigFile( "actions/support/test/testInjectContext.instance" ); assertNotNull("File found", fo); Object obj = fo.getAttribute("instanceCreate"); assertNotNull("Attribute present", obj); assertTrue("It is context aware action", obj instanceof ContextAwareAction); ContextAwareAction a = (ContextAwareAction)obj; InstanceContent ic = new InstanceContent(); AbstractLookup lkp = new AbstractLookup(ic); Action clone = a.createContextAwareInstance(lkp); ic.add(10); assertEquals("Number lover!", clone.getValue(Action.NAME)); clone.actionPerformed(new ActionEvent(this, 300, "")); assertEquals("Global Action not called", 10, Context.cnt); ic.remove(10); clone.actionPerformed(new ActionEvent(this, 200, "")); assertEquals("Global Action stays same", 10, Context.cnt); }
public void testOwnContextAction() throws Exception { MultiContext.cnt = 0; FileObject fo = FileUtil.getConfigFile( "actions/support/test/testOwnContext.instance" ); assertNotNull("File found", fo); Object obj = fo.getAttribute("instanceCreate"); assertNotNull("Attribute present", obj); assertTrue("It is action", obj instanceof Action); assertFalse("It is not context aware action: " + obj, obj instanceof ContextAwareAction); Action a = (Action)obj; InstanceContent ic = contextI; ic.add(10); assertEquals("Number lover!", a.getValue(Action.NAME)); a.actionPerformed(new ActionEvent(this, 300, "")); assertEquals("Global Action not called", 10, MultiContext.cnt); ic.remove(10); a.actionPerformed(new ActionEvent(this, 200, "")); assertEquals("Global Action stays same", 10, MultiContext.cnt); }
public JMenuItem getPopupPresenter() { JMenuItem prop = new Actions.MenuItem(this, false); Action customizeAction = SystemAction.get(CustomizeAction.class); // Retrieve context sensitive action instance if possible. if (lookup != null) { customizeAction = ((ContextAwareAction) customizeAction).createContextAwareInstance(lookup); } if (customizeAction.isEnabled()) { JInlineMenu mi = new JInlineMenu(); mi.setMenuItems(new JMenuItem[] { new Actions.MenuItem(customizeAction, false), prop }); return mi; } else { for (Node n : nodes()) { for (Node.PropertySet ps : n.getPropertySets()) { if (ps.getProperties().length > 0) { // OK, we have something to show! return prop; } } } // else nothing to show, so show nothing return new JInlineMenu(); } }
@ActionID(id = "org.netbeans.modules.maven.buildWithDependencies", category = "Project") @ActionRegistration(displayName = "#ACT_Build_Deps", lazy=false) @ActionReference(position = 500, path = "Projects/org-netbeans-modules-maven/Actions") @Messages("ACT_Build_Deps=Build with Dependencies") public static ContextAwareAction buildWithDependenciesAction() { return (ContextAwareAction) ProjectSensitiveActions.projectCommandAction(BUILD_WITH_DEPENDENCIES, ACT_Build_Deps(), null); }
/** Tests whether the cut, copy (callback) actions are enabled/disabled * in the right time, see # */ public void testCutCopyActionsEnabling() throws Exception { assertTrue ("Can run only in AWT thread", java.awt.EventQueue.isDispatchThread()); TestNode enabledNode = new TestNode(true, true, true); TestNode disabledNode = new TestNode(false, false, false); manager.setRootContext(new TestRoot( new Node[] {enabledNode, disabledNode})); Action copy = ((ContextAwareAction)SystemAction.get(CopyAction.class)).createContextAwareInstance(context); Action cut = ((ContextAwareAction)SystemAction.get(CutAction.class)).createContextAwareInstance(context); assertTrue("Copy action has to be disabled", !copy.isEnabled()); assertTrue("Cut action has to be disabled", !cut.isEnabled()); manager.setSelectedNodes(new Node[] {enabledNode}); manager.waitActionsFinished(); assertTrue("Copy action has to be enabled", copy.isEnabled()); assertTrue("Cut action has to be enabled", cut.isEnabled()); copy.actionPerformed (new java.awt.event.ActionEvent (this, 0, "waitFinished")); assertEquals ("clipboardCopy invoked", 1, enabledNode.countCopy); cut.actionPerformed (new java.awt.event.ActionEvent (this, 0, "waitFinished")); assertEquals ("clipboardCut invoked", 1, enabledNode.countCut); manager.setSelectedNodes(new Node[] {disabledNode}); manager.waitActionsFinished(); assertTrue("Copy action has to be disabled", !copy.isEnabled()); assertTrue("Cut action has to be disabled", !cut.isEnabled()); }
@RandomlyFails // NB-Core-Build #8351 (from ExplorerActionsCompatTest): Now it gets enabled public void testDeleteAction () throws Exception { TestNode enabledNode = new TestNode(true, true, true); TestNode enabledNode2 = new TestNode(true, true, true); TestNode disabledNode = new TestNode(false, false, false); manager.setRootContext(new TestRoot( new Node[] {enabledNode, enabledNode2, disabledNode})); Action delete = ((ContextAwareAction)SystemAction.get(org.openide.actions.DeleteAction.class)).createContextAwareInstance(context); assertTrue ("By default delete is disabled", !delete.isEnabled()); manager.setSelectedNodes(new Node[] { enabledNode }); manager.waitActionsFinished(); assertTrue ("Now it gets enabled", delete.isEnabled ()); manager.setSelectedNodes (new Node[] { disabledNode }); manager.waitActionsFinished(); assertTrue ("Is disabled", !delete.isEnabled ()); manager.setSelectedNodes(new Node[] { manager.getRootContext() }); manager.waitActionsFinished(); assertTrue ("Delete is enabled on root", delete.isEnabled ()); manager.setSelectedNodes(new Node[] { manager.getRootContext(), enabledNode }); manager.waitActionsFinished(); assertTrue ("But is disabled on now, as one selected node is child of another", !delete.isEnabled ()); manager.setSelectedNodes (new Node[] { enabledNode, enabledNode2 }); manager.waitActionsFinished(); assertTrue ("It gets enabled", delete.isEnabled ()); delete.actionPerformed(new java.awt.event.ActionEvent (this, 0, "waitFinished")); assertEquals ("Destoy was called", 1, enabledNode.countDelete); assertEquals ("Destoy was called", 1, enabledNode2.countDelete); }
Action[] getInitActions(org.netbeans.modules.versioning.core.spi.VCSContext ctx) { String category = (String) map.get("actionsCategory"); // NOI18N List<? extends Action> l = Utilities.actionsForPath("Versioning/" + category + "/Actions/Unversioned"); // NOI18N List<Action> ret = new ArrayList<Action>(l.size()); for (Action action : l) { if(action instanceof ContextAwareAction) { ret.add(((ContextAwareAction)action).createContextAwareInstance(Lookups.singleton(ctx))); } else { ret.add(action); } } return ret.toArray(new Action[ret.size()]); }
private Action getSystemAction(JTextComponent c) { if (systemAction == null) { Action ea = getEditorAction(c); if (ea != null) { String saClassName = (String)ea.getValue(NbEditorKit.SYSTEM_ACTION_CLASS_NAME_PROPERTY); if (saClassName != null) { Class saClass; try { saClass = Class.forName(saClassName); } catch (Throwable t) { saClass = null; } if (saClass != null) { systemAction = SystemAction.get(saClass); if (systemAction instanceof ContextAwareAction){ Lookup lookup = getContextLookup(c); if (lookup!=null){ systemAction = ((ContextAwareAction)systemAction).createContextAwareInstance(lookup); } } } } } } return systemAction; }
private void selectInProjectsView(final DataFolder destinationFolder) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { ContextAwareAction action = FileUtil.getConfigObject("Actions/Window/SelectDocumentNode/org-netbeans-modules-project-ui-SelectInProjects.instance", ContextAwareAction.class); //NOI18N if(action != null) { Action contextAction = action.createContextAwareInstance(Lookups.fixed(destinationFolder)); contextAction.actionPerformed(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, null)); } } }); }
public static void renameProject(Project p, Object caller) { if( p == null) return; ContextAwareAction action = (ContextAwareAction) CommonProjectActions.renameProjectAction(); Lookup ctx = Lookups.singleton(p); Action ctxAction = action.createContextAwareInstance(ctx); ctxAction.actionPerformed(new ActionEvent(caller, 0, "")); // NOI18N }
public static SystemActionBridge createAction(Action action, String name, Lookup context, String actionPathPrefix) { Action delegateAction = action; if (context != null && action instanceof ContextAwareAction) { action = ((ContextAwareAction) action).createContextAwareInstance(context); } return new SystemActionBridge(action, delegateAction, name, actionPathPrefix); }
public void testSaveAsActionDisabledForNonEditorWindows() throws Exception { ContextAwareAction action = SaveAsAction.create(); viewWithoutSaveAs.requestActive(); assertTrue( TopComponent.getRegistry().getActivated() == viewWithoutSaveAs ); assertFalse( "action is disabled when SaveAsCapable is not present in active TC lookup", action.isEnabled() ); }
public void testSaveAsActionDisabledForViewsWithSaveAsCapable() throws Exception { ContextAwareAction action = SaveAsAction.create(); viewWithSaveAs.requestActive(); assertTrue( TopComponent.getRegistry().getActivated() == viewWithSaveAs ); assertFalse( "action is disabled other window than editor is activated", action.isEnabled() ); }
public void testSaveAsActionEnabledForEditorsOnly() throws Exception { ContextAwareAction action = SaveAsAction.create(); editorWithSaveAs.requestActive(); assertTrue( TopComponent.getRegistry().getActivated() == editorWithSaveAs ); assertTrue( "action is enabled for editor windows with SaveAsCapable in their Lookup", action.isEnabled() ); }
public void testSaveAsActionDisabledForEditorsWithoutSaveAsCapable() throws Exception { ContextAwareAction action = SaveAsAction.create(); editorWithoutSaveAs.requestActive(); assertTrue( TopComponent.getRegistry().getActivated() == editorWithoutSaveAs ); assertFalse( "action is disabled for editor windows without SaveAsCapable in their Lookup", action.isEnabled() ); }
Action[] getInitActions(VCSContext ctx) { String category = (String) map.get("actionsCategory"); // NOI18N List<? extends Action> l = Utilities.actionsForPath("Versioning/" + category + "/Actions/Unversioned"); // NOI18N List<Action> ret = new ArrayList<Action>(l.size()); for (Action action : l) { if(action instanceof ContextAwareAction) { ret.add(((ContextAwareAction)action).createContextAwareInstance(Lookups.singleton(ctx))); } else { ret.add(action); } } return ret.toArray(new Action[ret.size()]); }
private static void callAction(Action a, Node node, ActionEvent actionEvent) { if (a instanceof ContextAwareAction) { a = ((ContextAwareAction)a).createContextAwareInstance(node.getLookup()); } if (a == null) { return; } a.actionPerformed(actionEvent); }
/** * Run an action as if it were in the context menu of a project. */ private void runContextMenuAction(Action a, Project p) { if (a instanceof ContextAwareAction) { Lookup l = Lookups.singleton(p); a = ((ContextAwareAction) a).createContextAwareInstance(l); } a.actionPerformed(null); }
public @Override Action createContextAwareInstance(Lookup ctx) { Project p = ctx.lookup(Project.class); if (p == null) { return this; } if (!CoverageManager.INSTANCE.isEnabled(p)) { // or could show it anyway in case provider is present return this; } return ((ContextAwareAction) CoverageActionFactory.createCollectorAction(null, null)).createContextAwareInstance(ctx); }
/** Warms up tools action popup menu item. */ private static void warmUpToolsPopupMenuItem() { SystemAction toolsAction = SystemAction.get(ToolsAction.class); if(toolsAction instanceof ContextAwareAction) { // Here is important to create proper lookup // to warm up Tools sub actions. Lookup lookup = new org.openide.util.lookup.ProxyLookup( new Lookup[] { // This part of lookup causes warm up of Node (cookie) actions. new AbstractNode(Children.LEAF).getLookup(), // This part of lookup causes warm up of Callback actions. new TopComponent().getLookup() } ); Action action = ((ContextAwareAction)toolsAction) .createContextAwareInstance(lookup); if(action instanceof Presenter.Popup) { JMenuItem toolsMenuItem = ((Presenter.Popup)action) .getPopupPresenter(); if(toolsMenuItem instanceof Runnable) { // This actually makes the warm up. // See ToolsAction.Popup impl. ((Runnable)toolsMenuItem).run(); } } } }
public static ContextAwareAction callback( String key, Action defaultDelegate, Lookup context, boolean surviveFocusChange, boolean async ) { if (key == null) { throw new NullPointerException(); } return new DelegateAction(null, key, context, defaultDelegate, surviveFocusChange, async); }
private static <T> ContextAwareAction _context(Map map, Class<T> dataType, Lookup context) { ContextSelection sel = readSelection(map.get("selectionType")); // NOI18N Performer<T> perf = new Performer<T>(map); boolean survive = Boolean.TRUE.equals(map.get("surviveFocusChange")); // NOI18N return new ContextAction<T>( perf, sel, context, dataType, survive ); }
private static ActionListener bindToContext(ActionListener a, Lookup context) { if (context != null) { if (a instanceof ContextAwareAction) { return ((ContextAwareAction)a).createContextAwareInstance(context); } } return a; }
private static Action translateContextLookupAction(Lookup contextLookup, Action action) { if (action instanceof ContextAwareAction && contextLookup != null){ action = ((org.openide.util.ContextAwareAction)action) .createContextAwareInstance(contextLookup); // May return null } return action; }
public void testCallbackAction() throws Exception { Callback.cnt = 0; ContextAwareAction a = (ContextAwareAction) Actions.forID("Tools", "my.action"); class MyAction extends AbstractAction { int cnt; @Override public void actionPerformed(ActionEvent e) { cnt += e.getID(); } } MyAction my = new MyAction(); ActionMap m = new ActionMap(); m.put("klic", my); InstanceContent ic = new InstanceContent(); AbstractLookup lkp = new AbstractLookup(ic); Action clone = a.createContextAwareInstance(lkp); ic.add(m); assertEquals("I am context", clone.getValue(Action.NAME)); clone.actionPerformed(new ActionEvent(this, 300, "")); assertEquals("Local Action called", 300, my.cnt); assertEquals("Global Action not called", 0, Callback.cnt); ic.remove(m); clone.actionPerformed(new ActionEvent(this, 200, "")); assertEquals("Local Action stays", 300, my.cnt); assertEquals("Global Action ncalled", 200, Callback.cnt); }