KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > refactoring > spi > impl > RefactoringSubMenuAction


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.refactoring.spi.impl;
20
21 import java.io.IOException JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import javax.swing.*;
25 import javax.swing.text.JTextComponent JavaDoc;
26 import javax.swing.text.TextAction JavaDoc;
27 import org.openide.awt.Actions;
28 import org.openide.awt.JMenuPlus;
29 import org.openide.awt.Mnemonics;
30 import org.openide.cookies.InstanceCookie;
31 import org.openide.filesystems.FileObject;
32 import org.openide.filesystems.FileSystem;
33 import org.openide.filesystems.Repository;
34 import org.openide.loaders.DataFolder;
35 import org.openide.loaders.DataObject;
36 import org.openide.nodes.Node;
37 import org.openide.util.Lookup;
38 import org.openide.util.LookupListener;
39 import org.openide.util.NbBundle;
40 import org.openide.util.actions.Presenter;
41
42 /**
43  *
44  * @author Martin Matula
45  */

46 public final class RefactoringSubMenuAction extends TextAction JavaDoc implements Presenter.Menu, Presenter.Popup {
47     private final boolean showIcons;
48     
49     public static RefactoringSubMenuAction create(FileObject o) {
50         return new RefactoringSubMenuAction(true);
51     }
52     
53     public static JMenu createMenu() {
54         RefactoringSubMenuAction action = new RefactoringSubMenuAction(true);
55         return (JMenu) action.getMenuPresenter();
56     }
57     
58     /** Creates a new instance of TestMenu */
59     RefactoringSubMenuAction(boolean showIcons) {
60         super(NbBundle.getMessage(RefactoringSubMenuAction.class, "LBL_Action"));
61         this.showIcons = showIcons;
62     }
63     
64     public void actionPerformed(java.awt.event.ActionEvent JavaDoc e) {
65     }
66     
67     public javax.swing.JMenuItem JavaDoc getMenuPresenter() {
68         return new SubMenu();
69     }
70     
71     public javax.swing.JMenuItem JavaDoc getPopupPresenter() {
72         return getMenuPresenter();
73     }
74     
75     public boolean equals(Object JavaDoc o) {
76         return (o instanceof RefactoringSubMenuAction);
77     }
78     
79     public int hashCode() {
80         return 1;
81     }
82     
83     private class SubMenu extends JMenuPlus implements LookupListener {
84         private ArrayList JavaDoc actions = null;
85         private Lookup.Result nodes = null;
86         private boolean nodesChanged = true;
87         
88         public SubMenu() {
89             super((String JavaDoc) RefactoringSubMenuAction.this.getValue(Action.NAME));
90             if (showIcons)
91                 setMnemonic(NbBundle.getMessage(RefactoringSubMenuAction.class, "LBL_ActionMnemonic").charAt(0));
92         }
93         
94         /** Gets popup menu. Overrides superclass. Adds lazy menu items creation. */
95         public JPopupMenu getPopupMenu() {
96             if (actions == null) {
97                 createMenuItems();
98             }
99             return super.getPopupMenu();
100         }
101         
102         /** Creates items when actually needed. */
103         private void createMenuItems() {
104             actions = new ArrayList JavaDoc();
105             removeAll();
106             FileSystem dfs = Repository.getDefault().getDefaultFileSystem();
107             FileObject fo = dfs.findResource("Menu/Refactoring"); // NOI18N
108
DataFolder df = DataFolder.findFolder(fo);
109                 
110             if (df != null) {
111                 DataObject actionObjects[] = df.getChildren();
112                 for (int i = 0; i < actionObjects.length; i++) {
113                     InstanceCookie ic = (InstanceCookie) actionObjects[i].getCookie(InstanceCookie.class);
114                     if (ic == null) continue;
115                     Object JavaDoc instance;
116                     try {
117                         instance = ic.instanceCreate();
118                     } catch (IOException JavaDoc e) {
119                         // ignore
120
e.printStackTrace();
121                         continue;
122                     } catch (ClassNotFoundException JavaDoc e) {
123                         // ignore
124
e.printStackTrace();
125                         continue;
126                     }
127                     if (instance instanceof RefactoringGlobalAction) {
128                         // if the action is the refactoring action, pass it information
129
// whether it is in editor, popup or main menu
130
actions.add(instance);
131                         JMenuItem mi = new JMenuItem();
132                         Actions.connect(mi, (Action JavaDoc) instance, true);
133                         if (!showIcons)
134                             mi.setIcon(null);
135                         add(mi);
136                     } else if (instance instanceof JSeparator) {
137                         add((JSeparator) instance);
138                     } else if (instance instanceof Presenter.Popup) {
139                         JMenuItem temp = ((Presenter.Popup)instance).getPopupPresenter();
140                         if (!showIcons)
141                             temp.setIcon(null);
142                         add(temp);
143                     } else if (instance instanceof Action JavaDoc) {
144                         JMenuItem mi = new JMenuItem();
145                         Actions.connect(mi, (Action JavaDoc) instance, true);
146                         if (!showIcons)
147                             mi.setIcon(null);
148                         add(mi);
149                     }
150                 }
151             }
152         }
153         
154         private void setText(JMenuItem t, Action JavaDoc a) {
155             String JavaDoc name = (String JavaDoc) a.getValue(Action.NAME);
156             int i = Mnemonics.findMnemonicAmpersand(name);
157             
158             if (i < 0)
159                 t.setText(name);
160             else
161                 t.setText(name.substring(0, i) + name.substring(i + 1));
162         }
163         
164         public void resultChanged(org.openide.util.LookupEvent ev) {
165             nodesChanged = true;
166         }
167         
168     }
169 }
170
Popular Tags