KickJava   Java API By Example, From Geeks To Geeks.

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


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
20 package org.netbeans.modules.editor;
21
22 import java.awt.Component JavaDoc;
23 import javax.swing.Action JavaDoc;
24 import javax.swing.ActionMap JavaDoc;
25 import javax.swing.JComponent JavaDoc;
26 import javax.swing.JMenu JavaDoc;
27 import javax.swing.JMenuItem JavaDoc;
28 import javax.swing.JPopupMenu JavaDoc;
29 import javax.swing.JSeparator JavaDoc;
30 import javax.swing.text.JTextComponent JavaDoc;
31 import org.netbeans.editor.BaseAction;
32 import org.netbeans.editor.BaseKit;
33 import org.netbeans.editor.Settings;
34 import org.netbeans.editor.SettingsNames;
35 import org.netbeans.editor.Utilities;
36 import org.openide.util.HelpCtx;
37 import org.openide.util.NbBundle;
38 import org.openide.awt.DynamicMenuContent;
39 import org.openide.util.actions.Presenter;
40
41 /**
42  * Code Folding action displayed under Menu/View/
43  *
44  * @author Martin Roskanin
45  */

46 public class NbCodeFoldingAction extends GlobalContextAction implements Presenter.Menu{
47
48     
49     /** Creates a new instance of NbCodeFoldingAction */
50     public NbCodeFoldingAction() {
51     }
52     
53     public final HelpCtx getHelpCtx() {
54         return HelpCtx.DEFAULT_HELP;
55     }
56     
57     public String JavaDoc getName() {
58         return NbBundle.getBundle(NbCodeFoldingAction.class).getString(
59             "Menu/View/CodeFolds"); //NOI18N
60
}
61
62     public void resultChanged(org.openide.util.LookupEvent ev) {
63     }
64     
65     public boolean isEnabled() {
66         return false;
67     }
68
69     /** Get a menu item that can present this action in a {@link javax.swing.JMenu}.
70     * @return the representation for this action
71     */

72     public JMenuItem JavaDoc getMenuPresenter(){
73         return new CodeFoldsMenu(getName());
74     }
75     
76     private static JTextComponent JavaDoc getComponent(){
77         return Utilities.getFocusedComponent();
78     }
79     
80     public void actionPerformed (java.awt.event.ActionEvent JavaDoc ev){
81     }
82     
83     private BaseKit getKit(){
84         JTextComponent JavaDoc component = getComponent();
85         return (component == null) ? BaseKit.getKit(NbEditorKit.class) : Utilities.getKit(component);
86     }
87     
88     private static Object JavaDoc getSettingValue(BaseKit kit, String JavaDoc settingName) {
89         return Settings.getValue(kit.getClass(), settingName);
90     }
91
92     /** Get the value of the boolean setting from the <code>Settings</code>
93      * @param settingName name of the setting to get.
94      */

95     private static boolean getSettingBoolean(BaseKit kit, String JavaDoc settingName) {
96         Boolean JavaDoc val = (Boolean JavaDoc)getSettingValue(kit, settingName);
97         return (val != null) ? val.booleanValue() : false;
98     }
99     
100
101     private boolean isFoldingEnabledInSettings(BaseKit kit){
102         return getSettingBoolean(kit, SettingsNames.CODE_FOLDING_ENABLE);
103     }
104     
105     
106     public class CodeFoldsMenu extends JMenu JavaDoc implements DynamicMenuContent {
107         public CodeFoldsMenu(){
108             super();
109         }
110         
111         public CodeFoldsMenu(String JavaDoc s){
112             super(s);
113             //#40585 fix start - setting the empty, transparent icon for the menu item to align it correctly with other items
114
//setIcon(new ImageIcon(org.openide.util.Utilities.loadImage("org/netbeans/modules/editor/resources/empty.gif"))); //NOI18N
115
//#40585 fix end
116
org.openide.awt.Mnemonics.setLocalizedText(this, s);
117         }
118
119         public JComponent JavaDoc[] getMenuPresenters() {
120             return new JComponent JavaDoc[] { this };
121         }
122         
123         public JComponent JavaDoc[] synchMenuPresenters(JComponent JavaDoc[] items) {
124             getPopupMenu();
125             return items;
126         }
127         
128         public JPopupMenu JavaDoc getPopupMenu(){
129             JPopupMenu JavaDoc pm = super.getPopupMenu();
130             pm.removeAll();
131             boolean enable = false;
132             BaseKit bKit = getKit();
133             if (bKit==null) bKit = BaseKit.getKit(NbEditorKit.class);
134             if (bKit!=null){
135                 Action JavaDoc action = bKit.getActionByName(NbEditorKit.generateFoldPopupAction);
136                 if (action instanceof BaseAction){
137                     boolean foldingAvailable = isFoldingEnabledInSettings(bKit);
138                     JTextComponent JavaDoc component = Utilities.getFocusedComponent();
139                     if (foldingAvailable){
140                         ActionMap JavaDoc contextActionmap = getContextActionMap();
141                         if (contextActionmap!=null){
142                             foldingAvailable = contextActionmap.get(BaseKit.collapseFoldAction) != null &&
143                                 component != null;
144
145                             if (!foldingAvailable){
146                                 bKit = BaseKit.getKit(NbEditorKit.class);
147                                 if (bKit!=null){
148                                     Action JavaDoc defaultAction = bKit.getActionByName(NbEditorKit.generateFoldPopupAction);
149                                     if (defaultAction instanceof BaseAction) action = defaultAction;
150                                 }
151                             }
152                         }
153                     }
154
155                     JMenu JavaDoc menu = (JMenu JavaDoc)((BaseAction)action).getPopupMenuItem(foldingAvailable ? component : null);
156                     if (menu!=null){
157                         Component JavaDoc comps[] = menu.getMenuComponents();
158                         for (int i=0; i<comps.length; i++){
159                             pm.add(comps[i]);
160                             if (comps[i].isEnabled() && !(comps[i] instanceof JSeparator JavaDoc)) {
161                                 enable = true;
162                             }
163                         }
164                     }
165                 }
166             }
167             setEnabled(enable);
168             pm.pack();
169             return pm;
170         }
171     }
172     
173 }
174
Popular Tags