/** * Accepts only cookies that can provide <code>Menu</code>. * @param cookie an <code>InstanceCookie</code> to test * @return true if the cookie can provide accepted instances */ protected @Override InstanceCookie acceptCookie(InstanceCookie cookie) throws IOException, ClassNotFoundException { // [pnejedly] Don't try to optimize this by InstanceCookie.Of // It will load the classes few ms later from instanceCreate // anyway and more instanceOf calls take longer Class c = cookie.instanceClass(); boolean action = Action.class.isAssignableFrom (c); if (action) { cookie.instanceCreate(); } boolean is = Presenter.Menu.class.isAssignableFrom (c) || JMenuItem.class.isAssignableFrom (c) || JSeparator.class.isAssignableFrom (c) || action; return is ? cookie : null; }
/** * Accepts only cookies that can provide <code>Toolbar</code>. * @param cookie an <code>InstanceCookie</code> to test * @return true if the cookie can provide accepted instances */ @Override protected InstanceCookie acceptCookie (InstanceCookie cookie) throws IOException, ClassNotFoundException { boolean is; boolean action; if (cookie instanceof InstanceCookie.Of) { InstanceCookie.Of of = (InstanceCookie.Of)cookie; action = of.instanceOf (Action.class); is = of.instanceOf (Component.class) || of.instanceOf (Presenter.Toolbar.class) || action; } else { Class c = cookie.instanceClass(); action = Action.class.isAssignableFrom (c); is = Component.class.isAssignableFrom(c) || Presenter.Toolbar.class.isAssignableFrom(c) || action; } if (action) { cookie.instanceCreate(); } return is ? cookie : null; }
public void testManualRefreshPreference() throws IOException { Preferences pref = NbPreferences.root().node("org/openide/actions/FileSystemRefreshAction"); assertFalse("Not set", pref.getBoolean("manual", false)); FileObject fo = FileUtil.toFileObject(getWorkDir()); Lookup lkp = Lookups.singleton(DataFolder.findFolder(fo).getNodeDelegate()); FileSystemAction fsa = FileSystemAction.get(FileSystemAction.class); Action a = fsa.createContextAwareInstance(lkp); assertEquals("Menu presenter ", true, a instanceof Presenter.Menu); Presenter.Menu pm = (Presenter.Menu)a; DynamicMenuContent submenu = (DynamicMenuContent)pm.getMenuPresenter(); assertEquals("No submenu", 0, submenu.getMenuPresenters().length); pref.putBoolean("manual", true); DynamicMenuContent submenu2 = (DynamicMenuContent)pm.getMenuPresenter(); assertEquals("One action", 1, submenu2.getMenuPresenters().length); }
private static boolean setShowEditorToolbar( boolean show ) { boolean res = true; Action toggleEditorToolbar = FileUtil.getConfigObject( "Editors/Actions/toggle-toolbar.instance", Action.class ); //NOI18N if( null != toggleEditorToolbar ) { if( toggleEditorToolbar instanceof Presenter.Menu ) { JMenuItem menuItem = ((Presenter.Menu)toggleEditorToolbar).getMenuPresenter(); if( menuItem instanceof JCheckBoxMenuItem ) { JCheckBoxMenuItem checkBoxMenu = ( JCheckBoxMenuItem ) menuItem; res = checkBoxMenu.isSelected(); if( checkBoxMenu.isSelected() != show ) { try { toggleEditorToolbar.actionPerformed( new ActionEvent( menuItem, 0, "")); //NOII18N } catch( Exception ex ) { //don't worry too much if it isn't working, we're just trying to be helpful here Logger.getLogger( EditorOnlyDisplayer.class.getName()).log( Level.FINE, null, ex ); } } } } } return res; }
public void testActionPerformed() { instanceContent.add(sqlExecution); Component tp = ((Presenter.Toolbar)action).getToolbarPresenter(); assertTrue("The toolbar presenter should be a JButton", tp instanceof JButton); JButton button = (JButton)tp; button.doClick(); assertTrue("Should perform the action when SQLExecution in the context", baseAction.actionPeformedCount == 1); instanceContent.remove(sqlExecution); button.doClick(); assertTrue("Should not perform the action when no SQLExecution in the context", baseAction.actionPeformedCount == 1); instanceContent.add(sqlExecution); button.doClick(); assertTrue("Should perform the action when SQLExecution in the context", baseAction.actionPeformedCount == 2); }
public void testIsPopupVisible() throws Exception { ToolsAction ta = ToolsAction.get(ToolsAction.class); Lookup lkp = Lookups.singleton(this); FileObject fo = FileUtil.createFolder(FileUtil.getConfigRoot(), "UI/ToolActions"); assertNotNull("ToolActions folder found", fo); fo.createFolder("Cat1").createData("org-openide-actions-SaveAction.instance").setAttribute("position", 100); Action a = ta.createContextAwareInstance(lkp); assertTrue("It is menu presenter", a instanceof Presenter.Popup); Presenter.Popup pp = (Presenter.Popup)a; JMenuItem item = pp.getPopupPresenter(); assertTrue("Item is enabled", item.isEnabled()); DynamicMenuContent dmc = (DynamicMenuContent)item; assertEquals("One presenter to delegte to", 1, dmc.getMenuPresenters().length); }
public void testInlineIsNotBlocked() throws Exception { ToolsAction ta = ToolsAction.get(ToolsAction.class); Lookup lkp = Lookups.singleton(this); FileObject fo = FileUtil.createFolder(FileUtil.getConfigRoot(), "UI/ToolActions"); assertNotNull("ToolActions folder found", fo); fo.createFolder("Cat3").createData(BlockingAction.class.getName().replace('.', '-') + ".instance").setAttribute("position", 100); Action a = ta.createContextAwareInstance(lkp); assertTrue("It is menu presenter", a instanceof Presenter.Menu); Presenter.Menu mp = (Presenter.Menu)a; JMenuItem item = mp.getMenuPresenter(); assertTrue("Item is enabled", item.isEnabled()); DynamicMenuContent dmc = (DynamicMenuContent)item; final JComponent[] arr = dmc.getMenuPresenters(); assertEquals("One presenter to delegte to", 1, arr.length); assertFalse("Disabled", arr[0].isEnabled()); }
/** Updates the <code>MenuBar</code> represented by this folder. * * @param cookies array of instance cookies for the folder * @return the updated <code>MenuBar</code> representee */ protected Object createInstance (InstanceCookie[] cookies) throws IOException, ClassNotFoundException { final LinkedList ll = new LinkedList (); allInstances (cookies, ll); final MenuBar mb = MenuBar.this; cleanUp(); //remove the stuff we've added last time // fill with new content Iterator it = ll.iterator (); while (it.hasNext()) { Object obj = it.next (); if (obj instanceof Presenter.Toolbar) { obj = ((Presenter.Toolbar)obj).getToolbarPresenter(); if (obj instanceof JButton) { // tune the presenter a bit ((JButton)obj).setBorderPainted(false); } } if (obj instanceof Component) addComponent((Component) obj); } mb.validate(); mb.repaint(); return mb; }
/** * Accepts only cookies that can provide <code>Toolbar</code>. * @param cookie an <code>InstanceCookie</code> to test * @return true if the cookie can provide accepted instances */ protected InstanceCookie acceptCookie (InstanceCookie cookie) throws java.io.IOException, ClassNotFoundException { boolean is; if (cookie instanceof InstanceCookie.Of) { InstanceCookie.Of of = (InstanceCookie.Of)cookie; is = of.instanceOf (Component.class) || of.instanceOf (Presenter.Toolbar.class) || of.instanceOf (Action.class); } else { Class c = cookie.instanceClass(); is = Component.class.isAssignableFrom(c) || Presenter.Toolbar.class.isAssignableFrom(c) || Action.class.isAssignableFrom (c); } return is ? cookie : null; }
protected JToolBar createToolbar() { JToolBar toolbar = new Toolbar(getShortName()); String toolbarActionsPath = "Office/" + getShortName() + "/Toolbar"; List<? extends Action> toolbarActions = Utilities.actionsForPath(toolbarActionsPath); for (Action action : toolbarActions) { if (action == null) { toolbar.addSeparator(); } else if (action instanceof Presenter.Toolbar) { Component actionComponent = ((Presenter.Toolbar) action).getToolbarPresenter(); toolbar.add(actionComponent); } else { JButton newButton = toolbar.add(action); if (newButton.getToolTipText() == null) { String label = (String) action.getValue("displayName"); newButton.setToolTipText(label); } } } return toolbar; }
public static JPopupMenu createPopupMenu(String path) { JPopupMenu popUp = new JPopupMenu(); List<? extends Action> actions = Utilities.actionsForPath(path); if(actions.isEmpty()) return null; for(Action action : actions) { if(action instanceof Presenter.Popup) { popUp.add(((Presenter.Popup)action).getPopupPresenter()); } else if (action == null) { popUp.addSeparator(); } else { popUp.add(createActionPopUpItem(action)); } } return popUp; }
private static JMenuItem getPopupMenuItem(Action action) { JMenuItem popupItem = null; if (action instanceof BaseAction) { popupItem = ((BaseAction) action).getPopupMenuItem(null); } if (popupItem == null) { if (action instanceof Presenter.Popup) { popupItem = ((Presenter.Popup) action).getPopupPresenter(); } } return popupItem; }
private void fillToolBar() { actionsBar.removeAll(); for (Action action : getToolbarActions()) { if (action instanceof Presenter.Toolbar) { actionsBar.add(((Presenter.Toolbar) action).getToolbarPresenter()); } } actionsBar.revalidate(); actionsBar.repaint(); }
private static void resolveInstance(Object instance, List<JComponent> result) throws IOException { if (instance instanceof Presenter.Popup) { JMenuItem temp = ((Presenter.Popup) instance).getPopupPresenter(); result.add(temp); } else if (instance instanceof JSeparator) { result.add(null); } else if (instance instanceof JComponent) { result.add((JComponent) instance); } else if (instance instanceof Action) { Actions.MenuItem mi = new Actions.MenuItem((Action) instance, true); result.add(mi); } else { throw new IOException(String.format("Unsupported instance: %s, class: %s", instance, instance.getClass())); // NOI18N } }
private JMenuItem item(Action a, boolean menu) { if (menu) { if (a instanceof Presenter.Menu) { return ((Presenter.Menu) a).getMenuPresenter(); } else { return new JMenuItem(a); } } else { if (a instanceof Presenter.Popup) { return ((Presenter.Popup)a).getPopupPresenter(); } else { return new JMenuItem(a); } } }
/** * Creates a menu item from an action. * * @param action an action * @return JMenuItem */ public static JMenuItem toMenuItem(Action action) { JMenuItem item; if (action instanceof Presenter.Menu) { item = ((Presenter.Menu) action).getMenuPresenter(); } else { item = new JMenuItem(); Actions.connect(item, action, false); } return item; }
/** Accepts only cookies that can provide a <code>Component</code> * or a <code>Presenter.Toolbar</code>. * @param cookie the instance cookie to test * @return true if the cookie is accepted. */ protected @Override InstanceCookie acceptCookie(InstanceCookie cookie) throws IOException, ClassNotFoundException { Class cls = cookie.instanceClass(); boolean is = Component.class.isAssignableFrom(cls) || Presenter.Toolbar.class.isAssignableFrom(cls) || Action.class.isAssignableFrom(cls); return is ? cookie : null; }
private JMenuItem createmenuItem(Action action) { JMenuItem item; if (action instanceof Presenter.Menu) { item = ((Presenter.Menu) action).getMenuPresenter(); } else if (action instanceof Presenter.Popup) { item = ((Presenter.Popup) action).getPopupPresenter(); } else { item = new JMenuItem(); Actions.connect(item, action, true); } return item; }
/** * Creates a menu item from an action. * * @param action an action * @return JMenuItem */ public static JMenuItem toMenuItem(Action action) { JMenuItem item; if (action instanceof Presenter.Menu) { item = ((Presenter.Menu) action).getMenuPresenter(); } else if (action instanceof Presenter.Popup) { item = ((Presenter.Popup) action).getPopupPresenter(); } else { item = new JMenuItem(); Actions.connect(item, action, false); } return item; }
private JMenuItem[] createMenu (Collection coll) { if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ( "\n--> CollectSystemAction.createMenu: ( " + coll + " )"); ArrayList items = new ArrayList (); Iterator it = coll.iterator(); while (it.hasNext ()) { SystemAction a = (SystemAction) it.next(); if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ( "-*- CollectSystemAction.createMenu: next action " + a + " -- " + ( a.isEnabled() ? "<enabled>" : "[disabled]" ) ); if ( a.isEnabled() ) { JMenuItem item = null; if (a instanceof Presenter.Popup) { item = ((Presenter.Popup)a).getPopupPresenter (); } if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("-*- CollectSystemAction.createMenu: menu item = " + item); // test if we obtained the item if (item != null) { items.add (item); } } } if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("<-- CollectSystemAction.createMenu: all items = " + items + "\n"); JMenuItem[] array = new JMenuItem [items.size ()]; items.toArray (array); return array; }
/** 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 void testPreferencesDefaultAction() throws Exception { Action a = readAction("testPreferencesDefault.instance"); assertNotNull(a); Preferences prefsNode = Preferences.userRoot().node("myNode"); prefsNode.remove("myKey"); assertNull("Expected null as preference value", prefsNode.get("myKey", null)); JMenuItem item = ((Presenter.Menu) a).getMenuPresenter(); TestCase.assertTrue("Expected to be selected", item.isSelected()); }
private void checkPreferencesAction(Action a, Preferences prefsNode) throws Exception { prefsNode.putBoolean("myKey", true); prefsNode.sync(); class L implements PreferenceChangeListener { boolean notified; public synchronized void preferenceChange(PreferenceChangeEvent evt) { notified = true; notifyAll(); } public synchronized void waitFor() throws Exception { while (!notified) { wait(); } notified = false; } } L listener = new L(); // Verify value assertTrue("Expected true as preference value", prefsNode.getBoolean("myKey", false)); TestCase.assertTrue("Expected to be instance of Presenter.Menu", a instanceof Presenter.Menu); JMenuItem item = ((Presenter.Menu) a).getMenuPresenter(); TestCase.assertTrue("Expected to be selected", item.isSelected()); prefsNode.addPreferenceChangeListener(listener); prefsNode.putBoolean("myKey", false); prefsNode.sync(); listener.waitFor(); TestCase.assertFalse("Expected to not be selected", item.isSelected()); a.actionPerformed(null); // new ActionEvent(null, 0, "")); listener.waitFor(); TestCase.assertTrue("Expected to be selected", item.isSelected()); prefsNode.putBoolean("myKey", false); prefsNode.sync(); listener.waitFor(); }
public void testPresenterCanBeGCedIssue47314 () throws Exception { javax.swing.JMenuItem item = ((Presenter.Popup) clone).getPopupPresenter(); Reference<?> itemref = new WeakReference<Object>(item); item = null; Reference<?> ref = new WeakReference<Object>(clone); clone = null; assertGC ("Item can disappear", itemref); assertGC ("Clone can disappear", ref); }
private void initializeToolBar(String id) { viewerToolBar.removeAll(); for (Action a : LookupUtils.lookupActions(String.format("CompilationViewer/%s/Actions", id), activeViewer.getLookup())) { if (a instanceof Presenter.Toolbar) { viewerToolBar.add(((Presenter.Toolbar) a).getToolbarPresenter()); } else { viewerToolBar.add(a); } } viewerToolBar.updateUI(); }
/** Accepts only cookies that can provide a <code>Component</code> * or a <code>Presenter.Toolbar</code>. * @param cookie the instance cookie to test * @return true if the cookie is accepted. */ protected InstanceCookie acceptCookie (InstanceCookie cookie) throws IOException, ClassNotFoundException { Class cls = cookie.instanceClass(); return Component.class.isAssignableFrom(cls) || Presenter.Toolbar.class.isAssignableFrom(cls) ? cookie : null; }
/** * Accepts only cookies that can provide <code>Menu</code>. * @param cookie an <code>InstanceCookie</code> to test * @return true if the cookie can provide accepted instances */ protected InstanceCookie acceptCookie(InstanceCookie cookie) throws IOException, ClassNotFoundException { // [pnejedly] Don't try to optimize this by InstanceCookie.Of // It will load the classes few ms later from instanceCreate // anyway and more instanceOf calls take longer Class c = cookie.instanceClass(); boolean is = Presenter.Menu.class.isAssignableFrom (c) || JMenuItem.class.isAssignableFrom (c) || JSeparator.class.isAssignableFrom (c) || Action.class.isAssignableFrom (c); return is ? cookie : null; }
public DelegateAction (Action a, Lookup actionContext) { if (! (a instanceof Presenter.Menu)) { throw new IllegalStateException ("Should be menu presenter: " + a); // NOI18N } if (! (a instanceof Presenter.Toolbar)) { throw new IllegalStateException ("Should be toolbar presenter: " + a); // NOI18N } this.delegate = a; this.lookup = actionContext; }
/** * Test of createContextAwareInstance method, of class ActiveConfigAction. */ @RandomlyFails public void testCreateContextAwareInstance() throws Exception { P p = PF.toCreate; OpenProjects.getDefault().open(new Project[] { p }, false); OpenProjects.getDefault().setMainProject(p); ActiveConfigAction instance = new ActiveConfigAction(); Action result = instance;//.createContextAwareInstance(actionContext); assertTrue("menu: " + result, result instanceof Presenter.Menu); Presenter.Menu menu = (Presenter.Menu)result; JMenuItem item = menu.getMenuPresenter(); assertNotNull(item); assertTrue("Enabled", item.isEnabled()); final DynamicMenuContent m = (DynamicMenuContent)item; assertEquals("One", 1, m.getMenuPresenters().length); holder = item; OpenProjects.getDefault().close(new Project[] { p }); assertNull("NO project selected", OpenProjects.getDefault().getMainProject()); WeakReference<Object> ref = new WeakReference<Object>(p); p = null; PF.toCreate = null; //Await refresh final Logger log = Logger.getLogger(ActiveConfigAction.class.getName()); final Level origLogLevel = log.getLevel(); final FH handler = new FH(); log.setLevel(Level.FINEST); log.addHandler(handler); try { handler.get(30, TimeUnit.SECONDS); } finally { log.setLevel(origLogLevel); log.removeHandler(handler); } //Rebuild the current pop up menu SwingUtilities.invokeAndWait(new Runnable() { @Override public void run() { m.getMenuPresenters(); } }); assertGC("Reference can go away", ref); }
private void doTestRefreshAfterBeingHidden(boolean clone, boolean menu) throws IOException { InstanceContent ic = new InstanceContent(); Lookup context = new AbstractLookup(ic); Action instance; if (clone) { Action a = create(Lookup.EMPTY); instance = ((ContextAwareAction)a).createContextAwareInstance(context); } else { instance = create(context); } if (!(instance instanceof Presenter.Popup)) { // cannot test, skipping return; } CharSequence log1 = Log.enable("org.netbeans.modules.project.ui.actions", Level.FINER); assertFalse("Disabled", instance.isEnabled()); if (!log1.toString().contains("Refreshing")) { fail("Should be refreshing: " + log1); } JMenuItem item = item(instance, menu); JMenu jmenu = new JMenu(); jmenu.addNotify(); assertTrue("Peer created", jmenu.isDisplayable()); jmenu.getPopupMenu().addNotify(); assertTrue("Peer for popup", jmenu.getPopupMenu().isDisplayable()); item.addPropertyChangeListener(this); jmenu.add(item); assertEquals("anncessor properly changes, this means the actions framework is activated", 1, ancEvent); assertFalse("Not enabled", item.isEnabled()); 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); ic.add(p); assertTrue("enabled", item.isEnabled()); assertEquals("One change", 1, change); if (menu) { item.removeNotify(); CharSequence log2 = Log.enable("org.netbeans.modules.project.ui.actions", Level.FINER); ic.remove(p); ic.add(p2); if (log2.length() > 0) { fail("Nothing shall happen:\n" + log2); } } // irrelevant for popups }
/** Creates list of menu items that should be used for given * data object. * @param en enumeration of SystemAction that should be added * into the menu if enabled and if not duplicated */ static JMenuItem[] createMenu (Enumeration en, boolean popUp, Lookup lookup) { en = new RemoveDuplicatesEnumeration (en); ArrayList items = new ArrayList (); while (en.hasMoreElements ()) { Action a = (Action)en.nextElement (); // Retrieve context sensitive action instance if possible. if(lookup != null && a instanceof ContextAwareAction) { a = ((ContextAwareAction)a).createContextAwareInstance(lookup); } boolean enabled = false; try { enabled = a.isEnabled(); } catch (RuntimeException e) { ErrorManager em = ErrorManager.getDefault(); em.annotate(e, ErrorManager.UNKNOWN, "Guilty action: " + a.getClass().getName(), null, null, null); // NOI18N em.notify(e); } if (enabled) { JMenuItem item = null; if (popUp) { if (a instanceof Presenter.Popup) { item = ((Presenter.Popup)a).getPopupPresenter (); } } else { if (a instanceof Presenter.Menu) { item = ((Presenter.Menu)a).getMenuPresenter (); } } // test if we obtained the item if (item != null) { items.add (item); } } } JMenuItem[] array = new JMenuItem [items.size ()]; items.toArray (array); return array; }
public javax.swing.JMenuItem getMenuPresenter() { return ((Presenter.Menu)delegate).getMenuPresenter (); }
public java.awt.Component getToolbarPresenter() { return ((Presenter.Toolbar)delegate).getToolbarPresenter (); }