Java Code Examples for javax.swing.Action#setEnabled()

The following examples show how to use javax.swing.Action#setEnabled() . 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: GeneralActionTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void testIconIsCorrect() throws Exception {
    if (true) return; // disabled right now
    
    myListenerCounter = 0;
    myIconResourceCounter = 0;
    Action a = readAction();
    
    assertTrue("Always enabled", a.isEnabled());
    a.setEnabled(false);
    assertTrue("Still Always enabled", a.isEnabled());
    
    
    assertEquals("No icon in menu", Boolean.TRUE, a.getValue("noIconInMenu"));
    
    if (a instanceof ContextAwareAction) {
        fail("Should not be context sensitive, otherwise it would have to implement equal correctly: " + a);
    }
    
    a.actionPerformed(null);
}
 
Example 2
Source File: MainFrame.java    From ramus with GNU General Public License v3.0 6 votes vote down vote up
private void setEnableActionsX(final String[] actions) {
    final Collection<Action> c = this.actions.values();
    final Iterator<Action> it = c.iterator();
    while (it.hasNext()) {
        final Action action = it.next();
        if (!isAllwayEnable(action)) {
            boolean b = false;
            for (final String s : actions) {
                if (s.equals(action.getValue(Action.ACTION_COMMAND_KEY)))
                    b = true;
            }
            if (b) {
                if (!action.isEnabled())
                    action.setEnabled(true);
            } else {
                if (action.isEnabled())
                    action.setEnabled(false);
            }
        }
    }
}
 
Example 3
Source File: StatefulActionProcessorTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/**
 * Checks that custom enable action is enabled on time, instantiated
 * only when the guard becomes true.
 */
public void testCustomEnableAction() {
    Action a = Actions.forID("Foo", "test.CustomEnableAction");
    assertNotNull(a);
    assertFalse("No data in lookup", a.isEnabled());
    ActionModel3 mod = new ActionModel3();
    lookupContent.add(mod);
    // still not enabled
    assertFalse("Property not set", a.isEnabled());
    mod.boolProp = true;
    mod.supp.firePropertyChange("boolProp", null, null);
    assertFalse("Action property not set", a.isEnabled());
    Action inst = instance;
    inst.setEnabled(true);
    assertTrue("Delegate must update enable", a.isEnabled());
}
 
Example 4
Source File: DiagramScene.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public Action createGotoAction(final Figure f) {
    final DiagramScene diagramScene = this;
    Action a = new AbstractAction() {

        public void actionPerformed(ActionEvent e) {
            diagramScene.gotoFigure(f);
        }
    };

    a.setEnabled(true);
    a.putValue(Action.SMALL_ICON, new ColorIcon(f.getColor()));
    String name = f.getLines()[0];

    name += " (";

    if (f.getCluster() != null) {
        name += "B" + f.getCluster().toString();
    }
    if (!this.getFigureWidget(f).isVisible()) {
        if (f.getCluster() != null) {
            name += ", ";
        }
        name += "hidden";
    }
    name += ")";
    a.putValue(Action.NAME, name);
    return a;
}
 
Example 5
Source File: AlwaysEnabledActionTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testContextAwareActionsShareDelegate() throws Exception {
    myListenerCounter = 0;
    myIconResourceCounter = 0;
    Action a = readAction("testDelegate.instance");

    assertContextAware(a);
    assertNull("No real action created yet", MyAction.last);
    
    Action cca1 = ((ContextAwareAction)a).createContextAwareInstance(new AbstractLookup(new InstanceContent()));
    Action cca2 = ((ContextAwareAction)a).createContextAwareInstance(new AbstractLookup(new InstanceContent()));

    cca1.actionPerformed(new ActionEvent(this, 0, "kuk"));
    assertEquals("Action not invoked as it is disabled", 0, myListenerCalled);
    assertNotNull("real action created", MyAction.last);
    Action lastCca1 = MyAction.last;
    
    lastCca1.setEnabled(true);
    cca1.actionPerformed(new ActionEvent(this, 0, "kuk"));
    assertEquals("Action invoked as no longer disabled", 1, myListenerCalled);

    cca2.actionPerformed(new ActionEvent(this, 0, "kuk"));

    Action lastCca2 = MyAction.last;
    assertEquals("MyAction created just once", lastCca1, lastCca2);
    
    assertEquals("Action invoked as it remains enabled", 2, myListenerCalled);
}
 
Example 6
Source File: DiagramScene.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
public Action createGotoAction(final Figure f) {
    final DiagramScene diagramScene = this;
    Action a = new AbstractAction() {

        public void actionPerformed(ActionEvent e) {
            diagramScene.gotoFigure(f);
        }
    };

    a.setEnabled(true);
    a.putValue(Action.SMALL_ICON, new ColorIcon(f.getColor()));
    String name = f.getLines()[0];

    name += " (";

    if (f.getCluster() != null) {
        name += "B" + f.getCluster().toString();
    }
    if (!this.getFigureWidget(f).isVisible()) {
        if (f.getCluster() != null) {
            name += ", ";
        }
        name += "hidden";
    }
    name += ")";
    a.putValue(Action.NAME, name);
    return a;
}
 
Example 7
Source File: StatefulActionProcessorTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Check that action with action check enables only after the actual instance enables.
 */
public void testCustomEnableActionInstantiation() {
    assertEquals("Not pre-created", 0, created);
    Action a = Actions.forID("Foo", "test.CustomEnableAction");
    assertNotNull(a);
    assertEquals("Not direcly created from layer", 0, created);
    a.isEnabled();
    assertEquals("Not created unless data present", 0, created);
    
    ActionModel3 mod = new ActionModel3();
    lookupContent.add(mod);
    
    // still not enabled
    assertFalse(a.isEnabled());
    assertEquals("Not created unless guard is set", 0, created);
    
    mod.boolProp = true;
    mod.supp.firePropertyChange("boolProp", null, null);
    // should instantiate the action just because of the property change on guard,
    // now the action decides the final state.
    assertEquals("Must be created to evaluate enabled state", 1, created);
    
    Action inst = instance;
    assertNotNull(inst);
    
    inst.setEnabled(true);
    assertSame("Same instance for repeated enable", inst, instance);
    
    a.actionPerformed(new ActionEvent(this, 0, "cmd"));
    assertSame("Same instance for invocation and enable eval", inst, instance);
    assertNotNull(received);
    assertEquals("cmd", received.getActionCommand());
}
 
Example 8
Source File: Actions.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Create action for changing scheduling info for supplied nodes.
 *
 * <p>
 * If one of the nodes does not support schedule handling, the action is disabled!</p>
 *
 * @param nodes
 * @return
 */
private static Action getScheduleAction(final TaskNode... taskNodes) {
    // Check the selected nodes - if one of the selected nodes does not
    // support scheduling - don't offer it in the menu
    boolean hasSchedule = true;
    for (TaskNode tn : taskNodes) {
        if (!tn.getTask().hasSchedule()) {
            hasSchedule = false;
            break;
        }
    }

    IssueScheduleInfo schedule = null;
    if (taskNodes.length == 1) {
        schedule = taskNodes[0].getTask().getSchedule();
    }
    final DashboardUtils.SchedulingMenu scheduleMenu = DashboardUtils.createScheduleMenu(schedule);

    //TODO weak listener??
    final ChangeListener listener = new ChangeListener() {
        @Override
        public void stateChanged(ChangeEvent e) {
            for (TaskNode taskNode : taskNodes) {
                if (taskNode.getTask().hasSchedule()) {
                    taskNode.getTask().setSchedule(scheduleMenu.getScheduleInfo());
                }
            }
            scheduleMenu.removeChangeListener(this);
        }
    };
    scheduleMenu.addChangeListener(listener);
    Action menuAction = scheduleMenu.getMenuAction();
    menuAction.setEnabled(hasSchedule);
    return menuAction;
}
 
Example 9
Source File: FigureWidget.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public JPopupMenu getPopupMenu(Widget widget, Point point) {
    JPopupMenu menu = diagramScene.createPopupMenu();
    menu.addSeparator();

    build(menu, getFigure(), this, false, diagramScene);
    menu.addSeparator();
    build(menu, getFigure(), this, true, diagramScene);

    if (getFigure().getSubgraphs() != null) {
        menu.addSeparator();
        JMenu subgraphs = new JMenu("Subgraphs");
        menu.add(subgraphs);

        final GraphViewer viewer = Lookup.getDefault().lookup(GraphViewer.class);
        for (final InputGraph subgraph : getFigure().getSubgraphs()) {
            Action a = new AbstractAction() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    viewer.view(subgraph, true);
                }
            };

            a.setEnabled(true);
            a.putValue(Action.NAME, subgraph.getName());
            subgraphs.add(a);
        }
    }

    return menu;
}
 
Example 10
Source File: FileChooserUI.java    From rapidminer-studio with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public Action getNewFolderAction() {
	Action newFolderAction = new NewFolderAction();
	// Note: Don't return null for readOnly, it might
	// break older apps.
	if (UIManager.getBoolean("FileChooser.readOnly")) {
		newFolderAction.setEnabled(false);
	}
	return newFolderAction;
}
 
Example 11
Source File: FormValidator.java    From pentaho-reporting with GNU Lesser General Public License v2.1 5 votes vote down vote up
public final void handleValidate() {
  final Action confirmAction = getConfirmAction();
  if ( confirmAction == null || enabled == false ) {
    return;
  }

  if ( performValidate() == false ) {
    confirmAction.setEnabled( false );
  } else {
    confirmAction.setEnabled( true );
  }
}
 
Example 12
Source File: InstanceDataObjectModuleTest8.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/** Currently fails.
 * Same as #7, but reloading is done quickly (one write mutex, no pause).
 */
public void testFixedSettingsChangeInstanceAfterFastReload() throws Exception {
    twiddle(m2, TWIDDLE_ENABLE);
    DataObject obj1;
    try {
        obj1 = findIt("Services/Misc/inst-8.settings");
        ERR.log("Found obj1: " + obj1);
        assertEquals("No saved state for inst-8.settings", null, FileUtil.toFile(obj1.getPrimaryFile()));
        InstanceCookie inst1 = (InstanceCookie)obj1.getCookie(InstanceCookie.class);
        ERR.log("There is a cookie: " + inst1);
        assertNotNull("Had an instance", inst1);
        Action a1 = (Action)inst1.instanceCreate();
        assertEquals("Correct action class", "test2.SomeAction", a1.getClass().getName());
        assertTrue("Old version of action", a1.isEnabled());
        
        ERR.log("Action created" + a1);
        
        // Make some change which should cause it to be written to disk:
        synchronized (this) {
            ERR.log("In sync block");
            a1.setEnabled(false);
            ERR.log("setEnabled(false)");
            // Cf. InstanceDataObject.SettingsInstance.SAVE_DELAY = 2000:
            ERR.log("Waiting");
            wait (60000);
            ERR.log("Waiting done");
            assertTrue ("Really was saved", instanceSaved);
        }
        /*
        File saved = new File(new File(new File(systemDir, "Services"), "Misc"), "inst-8.settings");
        assertTrue("Wrote to disk: " + saved, saved.isFile());
         */
        /*
        File saved = FileUtil.toFile(obj1.getPrimaryFile());
        assertNotNull("Wrote to disk; expecting: " + new File(new File(new File(systemDir, "Services"), "Misc"), "inst-8.settings"),
            saved);
         */
        ERR.log("Twidle reload");
        twiddle(m2, TWIDDLE_RELOAD);
        ERR.log("TWIDDLE_RELOAD done");
        NbLoaderPool.waitFinished();
        ERR.log("pool refeshed");
        DataObject obj2 = findIt("Services/Misc/inst-8.settings");
        ERR.log("Data object for inst-8: " + obj2);
        assertSameDataObject ("same data object", obj1, obj2);
        InstanceCookie inst2 = (InstanceCookie)obj2.getCookie(InstanceCookie.class);
        ERR.log("Cookie from the object: " + inst2);
        assertNotNull("Had an instance", inst2);
        assertTrue("InstanceCookie changed", inst1 != inst2);
        Action a2 = (Action)inst2.instanceCreate();
        ERR.log("Action2 created: " + a2);
        assertTrue("Action changed", a1 != a2);
        assertTrue("Correct action", "SomeAction".equals(a2.getValue(Action.NAME)));
        assertTrue("New version of action", !a2.isEnabled());
    } finally {
        ERR.log("Final disable");
        twiddle(m2, TWIDDLE_DISABLE);
        ERR.log("Final disable done");
    }
    // Now make sure it has no cookie.
    NbLoaderPool.waitFinished();
    ERR.log("loader pool node refreshed");
    DataObject obj3 = findIt("Services/Misc/inst-8.settings");
    ERR.log("Third data object: " + obj3);
    assertSameDataObject ("same data object2", obj1, obj3);
    InstanceCookie inst3 = (InstanceCookie)obj3.getCookie(InstanceCookie.class);
    ERR.log("Cookie is here: " + inst3);
    assertNull("Had instance", inst3);
}
 
Example 13
Source File: ActionManager.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
public void setActionEnabled(String name, boolean enabled)
{
    Action action = getAction(name);
    if(action != null)
        action.setEnabled(enabled);
}
 
Example 14
Source File: AntJUnitTestMethodNode.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@NbBundle.Messages({
    "LBL_RerunTest=Run Again",
    "LBL_DebugTest=Debug"
})
@Override
public Action[] getActions(boolean context) {
    List<Action> actions = new ArrayList<Action>();
    Action preferred = getPreferredAction();
    if (preferred != null) {
        actions.add(preferred);
    }
    // Method node might belong to an inner class
    FileObject testFO = ((JUnitTestcase) testcase).getClassFileObject(true);
    if (testFO == null) {
        Logger.getLogger(AntJUnitTestMethodNode.class.getName()).log(Level.INFO, "Test running process was probably abnormally interrupted. Could not locate FileObject for {0}", testcase.toString());
        for (Action prefAction : actions) {
            prefAction.setEnabled(false);
        }
    } else {
        boolean parameterized = false;
        try {
            String text = testFO.asText();
            if (text != null) {
                text = text.replaceAll("\n", "").replaceAll(" ", "");
                if ((text.contains("@RunWith") || text.contains("@org.junit.runner.RunWith")) //NOI18N
                        && text.contains("Parameterized.class)")) {  //NOI18N
                    parameterized = true;
                }
            }
        } catch (IOException ex) {
            Exceptions.printStackTrace(ex);
        }

        if (!parameterized) {
            ActionProvider actionProvider = OutputUtils.getActionProvider(testFO);
            if (actionProvider != null) {
                List supportedActions = Arrays.asList(actionProvider.getSupportedActions());

                SingleMethod methodSpec = new SingleMethod(testFO, testcase.getName());
                Lookup nodeContext = Lookups.singleton(methodSpec);
                if (supportedActions.contains(COMMAND_RUN_SINGLE_METHOD)
                        && actionProvider.isActionEnabled(COMMAND_RUN_SINGLE_METHOD, nodeContext)) {
                    actions.add(new TestMethodNodeAction(actionProvider, nodeContext, COMMAND_RUN_SINGLE_METHOD, Bundle.LBL_RerunTest()));
                }
                if (supportedActions.contains(COMMAND_DEBUG_SINGLE_METHOD)
                        && actionProvider.isActionEnabled(COMMAND_DEBUG_SINGLE_METHOD, nodeContext)) {
                    actions.add(new TestMethodNodeAction(actionProvider, nodeContext, COMMAND_DEBUG_SINGLE_METHOD, Bundle.LBL_DebugTest()));
                }
            }
        }
    }
    actions.addAll(Arrays.asList(super.getActions(context)));

    return actions.toArray(new Action[actions.size()]);
}
 
Example 15
Source File: AlwaysEnabledActionTest.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public void testIconIsCorrect() throws Exception {
    myListenerCounter = 0;
    myIconResourceCounter = 0;
    Action a = readAction("testIconIsCorrect.instance");
    
    assertNotNull("Action created", a);
    assertEquals("No myListener called", 0, myListenerCounter);
    assertEquals("No myIconURL called", 0, myIconResourceCounter);
    
    Object name = a.getValue(a.NAME);
    Object mnem = a.getValue(a.MNEMONIC_KEY);
    Object smallIcon = a.getValue(a.SMALL_ICON);
    if (smallIcon instanceof Icon) {
        Icon icon = (Icon) smallIcon;
        assertEquals("Icon height", 32, icon.getIconHeight());
        assertEquals("Icon widht", 32, icon.getIconWidth());
    } else {
        fail("Icon shall be Icon: " + smallIcon);
    }
        
    assertEquals("Right localized name", "Icon &Name Action", name);
    assertEquals("Mnemonic is N", (int)'N', mnem);
    assertNotNull("small icon present", smallIcon);

    assertEquals("once icon called", 1, myIconResourceCounter);

    
    Object base = a.getValue("iconBase"); 
    assertEquals("iconBase attribute is delegated", 2, myIconResourceCounter);
 
    assertTrue("Always enabled", a.isEnabled());
    a.setEnabled(false);
    assertTrue("Still Always enabled", a.isEnabled());

    a.actionPerformed(new ActionEvent(this, 0, "kuk"));

    assertEquals("Listener invoked", 1, myListenerCounter);
    
    
    assertEquals("No icon in menu", Boolean.TRUE, a.getValue("noIconInMenu"));

    assertContextAware(a);
}
 
Example 16
Source File: ActionManager.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
public void setActionEnabled(String name, boolean enabled)
{
    Action action = getAction(name);
    if(action != null)
        action.setEnabled(enabled);
}
 
Example 17
Source File: ActionManager.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
public void setActionEnabled(String name, boolean enabled)
{
    Action action = getAction(name);
    if(action != null)
        action.setEnabled(enabled);
}
 
Example 18
Source File: AbstractGradleExecutor.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private static void enableAction(Action a) {
    if (a != null) a.setEnabled(true);
}
 
Example 19
Source File: AbstractGradleExecutor.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private static void disableAction(Action a) {
    if (a != null) a.setEnabled(false);
}
 
Example 20
Source File: DateTimeBrowser.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
private void addMenus( JMenuBar menuBar) {
    //
    // Create all the menus.
    //
    JMenu fileMenu = new JMenu("File");
    JMenu viewMenu = new JMenu("View");
    //
    // Add them to the menubar in order.
    //
    menuBar.add( fileMenu );
    menuBar.add( viewMenu );
    //
    // Create action objects and menu items.
    //
    Action open = new OpenAction();
    JMenuItem jmiOpen = new JMenuItem( open );
    Action exit = new ExitAction();
    JMenuItem jmiExit = new JMenuItem( exit );
    //
    // Next Menu
    //
    Action getter = new GetterAction();
    jmiGetter = new JMenuItem( getter );
    getter.setEnabled( true );
    //
    Action hex = new HexAction();
    jmiHex = new JMenuItem( hex );
    hex.setEnabled( true );
    //
    Action date = new DateAction();
    jmiDate = new JMenuItem( date );
    date.setEnabled( true );
    //
    Action cal = new CalAction();
    jmiCal = new JMenuItem( cal );
    cal.setEnabled( true );
    //
    // Build the file menu.
    //
    fileMenu.add( jmiOpen );
    fileMenu.addSeparator();
    fileMenu.add( jmiExit );
    //
    // Build the view menu.
    //
    viewMenu.add( jmiGetter );
    viewMenu.add( jmiHex );
    viewMenu.add( jmiDate );
    viewMenu.add( jmiCal );
    //
    // *temp Developer's code
    //
    // jmiGetter.setEnabled( false );
    //
    // JMenuItem getter2 = new JMenuItem( "getter2" );
    // getter2.addActionListener( new myMouseListener() );
    // viewMenu.add( getter2 );
}