001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * Portions Copyrighted 2007 Sun Microsystems, Inc.
027: */
028: package org.netbeans.modules.cnd.modelui.switcher;
029:
030: import java.util.ArrayList;
031: import java.util.Collection;
032: import java.util.List;
033: import javax.swing.Action;
034: import javax.swing.JMenu;
035: import javax.swing.JMenuItem;
036: import javax.swing.JPopupMenu;
037: import org.openide.nodes.Node;
038: import org.openide.util.HelpCtx;
039: import org.openide.util.Lookup;
040: import org.openide.util.NbBundle;
041: import org.openide.util.actions.NodeAction;
042: import org.openide.util.actions.Presenter;
043: import org.openide.util.lookup.Lookups;
044:
045: /**
046: *
047: * @author Alexander Simon, Vladimir Voskresensky
048: */
049: public class CodeAssistanceSubmenuAction extends NodeAction {
050:
051: private LazyPopupMenu popupMenu;
052: private final Collection<Action> items = new ArrayList(5);
053:
054: @Override
055: public JMenuItem getPopupPresenter() {
056: createSubMenu();
057: return popupMenu;
058: }
059:
060: @Override
061: public JMenuItem getMenuPresenter() {
062: createSubMenu();
063: return popupMenu;
064: }
065:
066: private void createSubMenu() {
067: if (popupMenu == null) {
068: popupMenu = new LazyPopupMenu(getName(), items);
069: }
070: items.clear();
071: boolean enabled = false;
072: for (Action action : addActionsFromLayers()) {
073: items.add(action);
074: enabled = true;
075: }
076: popupMenu.setEnabled(enabled);
077: }
078:
079: private List<Action> addActionsFromLayers() {
080: String path = "NativeProjects/Actions"; // NOI18N
081: List<Action> popup = new ArrayList<Action>();
082: Lookup look = Lookups.forPath(path);
083: for (Object next : look.lookupAll(Object.class)) {
084: if (next instanceof Action) {
085: popup.add((Action) next);
086: }
087: }
088: return popup;
089: }
090:
091: protected void performAction(Node[] activatedNodes) {
092: }
093:
094: protected boolean enable(Node[] activatedNodes) {
095: return true;
096: }
097:
098: public String getName() {
099: return NbBundle.getMessage(CodeAssistanceSubmenuAction.class,
100: "LBL_CodeAssistanceAction_Name"); // NOI18N
101: }
102:
103: public HelpCtx getHelpCtx() {
104: return HelpCtx.DEFAULT_HELP;
105: }
106:
107: private final static class LazyPopupMenu extends JMenu {
108: private final Collection<Action> items;
109:
110: public LazyPopupMenu(String name, Collection<Action> items) {
111: super (name);
112: assert items != null : "array must be inited";
113: this .items = items;
114: }
115:
116: @Override
117: public synchronized JPopupMenu getPopupMenu() {
118: super .removeAll();
119: for (Action action : items) {
120: if (action instanceof Presenter.Popup) {
121: JMenuItem item = ((Presenter.Popup) action)
122: .getPopupPresenter();
123: add(item);
124: } else if (action instanceof Presenter.Menu) {
125: JMenuItem item = ((Presenter.Menu) action)
126: .getMenuPresenter();
127: add(item);
128: } else {
129: add(action);
130: }
131: }
132: return super.getPopupMenu();
133: }
134: }
135: }
|