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: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.visualweb.project.jsf.actions;
043:
044: import java.util.Collection;
045: import org.netbeans.modules.visualweb.project.jsf.api.Importable;
046: import java.util.Iterator;
047: import javax.swing.Action;
048: import javax.swing.JComponent;
049: import javax.swing.JMenu;
050: import javax.swing.JMenuItem;
051: import javax.swing.event.MenuEvent;
052: import javax.swing.event.MenuListener;
053: import org.netbeans.spi.project.ui.support.MainProjectSensitiveActions;
054: import org.openide.NotifyDescriptor;
055: import org.openide.util.Lookup;
056: import org.openide.NotifyDescriptor.Message;
057: import org.openide.awt.Actions;
058: import org.openide.util.actions.SystemAction;
059: import org.openide.util.actions.Presenter.Menu;
060: import org.openide.awt.StatusDisplayer;
061: import org.openide.DialogDisplayer;
062: import org.openide.awt.JMenuPlus;
063: import org.openide.util.HelpCtx;
064: import org.openide.util.NbBundle;
065: import org.openide.util.actions.CallableSystemAction;
066: import org.openide.awt.Mnemonics;
067:
068: /** Allows the user to import an item
069: *
070: * @author Tor Norbye
071: */
072: public final class ImportAction extends CallableSystemAction implements
073: Menu {
074:
075: static final long serialVersionUID = 1L;
076:
077: /** Constructs an import action */
078: public ImportAction() {
079: }
080:
081: public String getName() {
082: return NbBundle.getMessage(ImportAction.class,
083: "LBL_ImportAction"); // NOI18N
084: }
085:
086: public HelpCtx getHelpCtx() {
087: return HelpCtx.DEFAULT_HELP;
088: // If you will provide context help then use:
089: // return new HelpCtx(ChangeViewAction.class);
090: }
091:
092: protected boolean asynchronous() {
093: return false;
094: }
095:
096: /* Returns a submneu that will present this action in a Menu.
097: * @return the JMenuItem representation for this action
098: */
099: public JMenuItem getMenuPresenter() {
100: JMenu mainItem = new JMenuPlus();
101: Mnemonics.setLocalizedText(mainItem, getName());
102: mainItem
103: .setIcon(SystemAction.get(ImportAction.class).getIcon());
104: HelpCtx.setHelpIDString(mainItem, ImportAction.class.getName());
105: mainItem.addMenuListener(new MainItemListener());
106: return mainItem;
107: }
108:
109: /* Returns a submneu that will present this action in a PopupMenu.
110: * @return the JMenuItem representation for this action
111: */
112: public JMenuItem getPopupPresenter() {
113: JMenu mainItem = new JMenuPlus();
114: Mnemonics.setLocalizedText(mainItem, getName());
115: HelpCtx.setHelpIDString(mainItem, ImportAction.class.getName());
116: mainItem.addMenuListener(new MainItemListener());
117: return mainItem;
118: }
119:
120: public void performAction() {
121: // all functionality is accomplished by menu listeners
122: }
123:
124: /** Listens to selection of the main import item and expands it
125: * into a submenu listing available import types
126: */
127: private static final class MainItemListener implements MenuListener {
128: public void menuCanceled(MenuEvent e) {
129: }
130:
131: public void menuDeselected(MenuEvent e) {
132: JMenu menu = (JMenu) e.getSource();
133: menu.removeAll();
134: }
135:
136: public void menuSelected(MenuEvent e) {
137: JMenu menu = (JMenu) e.getSource();
138:
139: // Add the import items to the menu
140: Collection<Importable> importables = (Collection<Importable>) Lookup
141: .getDefault().lookupAll(Importable.class);
142: for (Importable importable : importables) {
143: JMenuItem item = createMenuItem(importable);
144: menu.add(item);
145: }
146:
147: menu
148: .add(createAddFileMenuItem(ImportFileAction.TYPE_IMAGE));
149: menu
150: .add(createAddFileMenuItem(ImportFileAction.TYPE_STYLESHEET));
151: menu.add(createAddFileMenuItem(ImportFileAction.TYPE_JAVA));
152: menu
153: .add(createAddFileMenuItem(ImportFileAction.TYPE_OTHER));
154: }
155:
156: private JMenuItem createMenuItem(Importable type) {
157: Action action = MainProjectSensitiveActions
158: .mainProjectSensitiveAction(type, type
159: .getDisplayName(), null);
160: return new JMenuItem(action);
161: }
162:
163: // <TEMP>
164: private JMenuItem createAddFileMenuItem(int type) {
165: return new JMenuItem(ImportFileAction.createAction(type));
166: }
167: // </TEMP>
168: }
169: }
|