KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > editor > NbSelectInPopupAction


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.editor;
20
21 import java.awt.event.ActionEvent JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import javax.swing.Action JavaDoc;
25 import javax.swing.JMenu JavaDoc;
26 import javax.swing.JMenuItem JavaDoc;
27 import javax.swing.JPopupMenu JavaDoc;
28 import javax.swing.JSeparator JavaDoc;
29
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.util.HelpCtx;
37 import org.openide.util.NbBundle;
38 import org.openide.util.actions.Presenter;
39 import org.openide.util.actions.SystemAction;
40
41
42 /**
43  *
44  * @author Dusan Balek
45  */

46 public final class NbSelectInPopupAction extends SystemAction implements Presenter.Popup {
47
48     private ArrayList JavaDoc actions = null;
49
50     public String JavaDoc getName() {
51         return NbBundle.getMessage(NbSelectInPopupAction.class, "Editors/text/base/Popup/org-netbeans-modules-editor-NbSelectInPopupAction.instance"); // NOI18N
52
}
53     
54     public void actionPerformed(ActionEvent JavaDoc ev) {
55         // do nothing - should never be called
56
}
57
58     public HelpCtx getHelpCtx() {
59         return HelpCtx.DEFAULT_HELP;
60     }
61     
62     public javax.swing.JMenuItem JavaDoc getPopupPresenter() {
63         return new SubMenu(getName());
64     }
65
66     public class SubMenu extends JMenu JavaDoc {
67
68         public SubMenu(String JavaDoc s){
69             super(s);
70         }
71         
72         /** Gets popup menu. Overrides superclass. Adds lazy menu items creation. */
73         public JPopupMenu JavaDoc getPopupMenu() {
74             JPopupMenu JavaDoc pm = super.getPopupMenu();
75             pm.removeAll();
76             FileSystem dfs = Repository.getDefault().getDefaultFileSystem();
77             FileObject fo = dfs.findResource("Actions/Window/SelectDocumentNode"); // NOI18N
78
DataFolder df = fo != null ? DataFolder.findFolder(fo) : null;
79             
80             if (df != null) {
81                 DataObject actionObjects[] = df.getChildren();
82                 for (int i = 0; i < actionObjects.length; i++) {
83                     InstanceCookie ic = (InstanceCookie) actionObjects[i].getCookie(InstanceCookie.class);
84                     if (ic == null) continue;
85                     Object JavaDoc instance;
86                     try {
87                         instance = ic.instanceCreate();
88                     } catch (IOException JavaDoc e) {
89                         // ignore
90
e.printStackTrace();
91                         continue;
92                     } catch (ClassNotFoundException JavaDoc e) {
93                         // ignore
94
e.printStackTrace();
95                         continue;
96                     }
97                     if (instance instanceof JSeparator JavaDoc) {
98                         pm.add((JSeparator JavaDoc) instance);
99                     } else if (instance instanceof Presenter.Popup) {
100                         pm.add(((Presenter.Popup)instance).getPopupPresenter());
101                     } else if (instance instanceof Presenter.Menu) {
102                         JMenuItem JavaDoc temp = ((Presenter.Menu)instance).getMenuPresenter();
103                         temp.setIcon(null);
104                         pm.add(temp);
105                     } else if (instance instanceof Action JavaDoc) {
106                         JMenuItem JavaDoc temp = new JMenuItem JavaDoc((Action JavaDoc)instance);
107                         temp.setIcon(null);
108                         pm.add(temp);
109                     }
110                 }
111             }
112             pm.pack();
113             return pm;
114         }
115     }
116 }
117
Popular Tags