KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.awt.event.ActionEvent JavaDoc;
24 import java.awt.event.ItemEvent JavaDoc;
25 import java.awt.event.ItemListener JavaDoc;
26 import java.awt.event.KeyEvent JavaDoc;
27 import java.util.Arrays JavaDoc;
28 import java.util.Collections JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.Map JavaDoc;
33 import java.util.MissingResourceException JavaDoc;
34 import java.util.ResourceBundle JavaDoc;
35 import javax.swing.JMenu JavaDoc;
36 import javax.swing.JMenuItem JavaDoc;
37 import javax.swing.JPopupMenu JavaDoc;
38 import javax.swing.Action JavaDoc;
39 import javax.swing.JCheckBoxMenuItem JavaDoc;
40 import javax.swing.KeyStroke JavaDoc;
41 import javax.swing.text.Document JavaDoc;
42 import javax.swing.text.JTextComponent JavaDoc;
43 import javax.swing.text.TextAction JavaDoc;
44 import javax.swing.text.Keymap JavaDoc;
45 import org.netbeans.editor.ActionFactory;
46 import org.netbeans.editor.EditorUI;
47 import org.netbeans.editor.ext.ExtKit;
48 import org.openide.awt.DynamicMenuContent;
49 import org.openide.loaders.DataFolder;
50 import org.openide.util.actions.SystemAction;
51 import org.openide.util.actions.Presenter;
52 import org.openide.actions.UndoAction;
53 import org.openide.actions.RedoAction;
54 import org.openide.windows.TopComponent;
55 import org.netbeans.editor.BaseKit;
56 import org.netbeans.editor.Utilities;
57 import org.netbeans.editor.BaseAction;
58 import org.netbeans.editor.MacroDialogSupport;
59 import org.netbeans.editor.Settings;
60 import org.netbeans.editor.SettingsNames;
61 import org.netbeans.editor.ext.ExtSettingsNames;
62 import org.netbeans.modules.editor.impl.ActionsList;
63 import org.netbeans.modules.editor.impl.PopupMenuActionsProvider;
64 import org.netbeans.modules.editor.options.BaseOptions;
65 import org.netbeans.modules.editor.options.OptionUtilities;
66 import org.netbeans.modules.editor.options.AllOptionsFolder;
67 import org.netbeans.modules.editor.options.MacrosEditorPanel;
68 import org.openide.ErrorManager;
69 import org.openide.NotifyDescriptor;
70 import org.openide.awt.Mnemonics;
71 import org.openide.filesystems.FileObject;
72 import org.openide.filesystems.FileStateInvalidException;
73 import org.openide.util.ContextAwareAction;
74 import org.openide.util.Lookup;
75 import org.openide.util.NbBundle;
76
77 /**
78 * Java editor kit with appropriate document
79 *
80 * @author Miloslav Metelka
81 * @version 1.00
82 */

83
84 public class NbEditorKit extends ExtKit {
85
86     /** Action property that stores the name of the corresponding nb-system-action */
87     public static final String JavaDoc SYSTEM_ACTION_CLASS_NAME_PROPERTY = "systemActionClassName"; // NOI18N
88

89     static final long serialVersionUID =4482122073483644089L;
90     
91     private static final Map JavaDoc contentTypeTable;
92
93     /** Name of the action for generating of Go To popup menu*/
94     public static final String JavaDoc generateGoToPopupAction = "generate-goto-popup"; // NOI18N
95

96     /** Name of the action for generating of code folding popup menu*/
97     public static final String JavaDoc generateFoldPopupAction = "generate-fold-popup"; // NOI18N
98

99     private static final NbUndoAction nbUndoActionDef = new NbUndoAction();
100     private static final NbRedoAction nbRedoActionDef = new NbRedoAction();
101     
102     private Map JavaDoc systemAction2editorAction = new HashMap JavaDoc();
103     
104     static {
105         contentTypeTable = new HashMap JavaDoc();
106         contentTypeTable.put("org.netbeans.modules.properties.syntax.PropertiesKit", "text/x-properties"); // NOI18N
107
contentTypeTable.put("org.netbeans.modules.web.core.syntax.JSPKit", "text/x-jsp"); // NOI18N
108
contentTypeTable.put("org.netbeans.modules.css.text.syntax.CSSEditorKit", "text/css"); // new - open source package // NOI18N
109
contentTypeTable.put("org.netbeans.modules.xml.css.editor.CSSEditorKit", "text/css"); // old - close source package // NOI18N
110
contentTypeTable.put("org.netbeans.modules.xml.text.syntax.DTDKit", "text/x-dtd"); // NOI18N
111
contentTypeTable.put("org.netbeans.modules.xml.text.syntax.XMLKit", "text/xml"); // NOI18N
112
contentTypeTable.put("org.netbeans.modules.corba.idl.editor.coloring.IDLKit", "text/x-idl"); // NOI18N
113
}
114     
115     public NbEditorKit() {
116         super();
117 // new Throwable("NbEditorKit: " + getClass()).printStackTrace();
118
NbEditorSettingsInitializer.init();
119     }
120
121     public Document JavaDoc createDefaultDocument() {
122         Document JavaDoc doc = new NbEditorDocument(this.getClass());
123         Object JavaDoc mimeType = doc.getProperty("mimeType"); //NOI18N
124
if (mimeType == null){
125             doc.putProperty("mimeType", getContentType()); //NOI18N
126
}
127         return doc;
128     }
129
130     /**
131      * Do any locking necessary prior evaluation of tooltip annotations.
132      * <br>
133      * This method will always be followed by {@link #toolTipAnnotationsUnlock()}
134      * by using <code>try ... finally</code>.
135      * <br>
136      * This method is called prior read locking of the document.
137      */

138     protected void toolTipAnnotationsLock(Document JavaDoc doc) {
139     }
140
141     /**
142      * Release any locking requested previously by {@link #toolTipAnnotationsLock()}.
143      * <br>
144      * This method is called after read unlocking of the document.
145      */

146     protected void toolTipAnnotationsUnlock(Document JavaDoc doc) {
147     }
148
149     protected EditorUI createEditorUI() {
150         return new NbEditorUI();
151     }
152
153     protected Action JavaDoc[] createActions() {
154         Action JavaDoc[] nbEditorActions = new Action JavaDoc[] {
155                                        new NbBuildPopupMenuAction(),
156                                        new NbStopMacroRecordingAction(),
157                                        nbUndoActionDef,
158                                        nbRedoActionDef,
159                                        new NbBuildToolTipAction(),
160                                        new NbToggleLineNumbersAction(),
161                                        new ToggleToolbarAction(),
162                                        new NbGenerateGoToPopupAction(),
163                                        new GenerateFoldPopupAction()
164                                    };
165         return TextAction.augmentList(super.createActions(), nbEditorActions);
166     }
167
168
169     protected void addSystemActionMapping(String JavaDoc editorActionName, Class JavaDoc systemActionClass) {
170         Action JavaDoc a = getActionByName(editorActionName);
171         if (a != null) {
172             a.putValue(SYSTEM_ACTION_CLASS_NAME_PROPERTY, systemActionClass.getName());
173         }
174         systemAction2editorAction.put(systemActionClass.getName(), editorActionName);
175     }
176     
177     protected void updateActions() {
178         addSystemActionMapping(cutAction, org.openide.actions.CutAction.class);
179         addSystemActionMapping(copyAction, org.openide.actions.CopyAction.class);
180         addSystemActionMapping(pasteAction, org.openide.actions.PasteAction.class);
181         // #69077 - DeleteAction now delegates to deleteNextCharAction
182
addSystemActionMapping(deleteNextCharAction, org.openide.actions.DeleteAction.class);
183         addSystemActionMapping(showPopupMenuAction, org.openide.actions.PopupAction.class);
184
185         addSystemActionMapping(findAction, org.openide.actions.FindAction.class);
186         addSystemActionMapping(replaceAction, org.openide.actions.ReplaceAction.class);
187         addSystemActionMapping(gotoAction, org.openide.actions.GotoAction.class);
188
189         addSystemActionMapping(undoAction, org.openide.actions.UndoAction.class);
190         addSystemActionMapping(redoAction, org.openide.actions.RedoAction.class);
191     }
192
193     private boolean isInheritorOfNbEditorKit(){
194         Class JavaDoc clz = this.getClass();
195         while(clz.getSuperclass() != null){
196             clz = clz.getSuperclass();
197             if (NbEditorKit.class == clz) return true;
198         }
199         return false;
200     }
201     
202     public String JavaDoc getContentType() {
203         if (isInheritorOfNbEditorKit()){
204             ErrorManager.getDefault().log(ErrorManager.WARNING,
205                 "Warning: KitClass "+this.getClass().getName()+" doesn't override the method getContentType."); //NOI18N
206
}
207         return (contentTypeTable.containsKey(this.getClass().getName())) ?
208             (String JavaDoc)contentTypeTable.get(this.getClass().getName()) : "text/"+this.getClass().getName().replace('.','_'); //NOI18N
209
}
210
211     private static ResourceBundle JavaDoc getBundleFromName (String JavaDoc name) {
212         ResourceBundle JavaDoc bundle = null;
213         if (name != null) {
214             try {
215                 bundle = NbBundle.getBundle (name);
216             } catch (MissingResourceException JavaDoc mre) {
217                 //ErrorManager.getDefault ().notify (mre);
218
}
219         }
220         return bundle;
221     }
222     
223     
224     public static class ToggleToolbarAction extends BaseAction {
225
226         public ToggleToolbarAction() {
227             super(ExtKit.toggleToolbarAction);
228             putValue ("helpID", ToggleToolbarAction.class.getName ()); // NOI18N
229
}
230
231         public void actionPerformed(ActionEvent JavaDoc evt, JTextComponent JavaDoc target) {
232             boolean toolbarVisible = AllOptionsFolder.getDefault().isToolbarVisible();
233             AllOptionsFolder.getDefault().setToolbarVisible(!toolbarVisible);
234         }
235         
236         public JMenuItem JavaDoc getPopupMenuItem(JTextComponent JavaDoc target) {
237             JCheckBoxMenuItem JavaDoc item = new JCheckBoxMenuItem JavaDoc(
238                     NbBundle.getBundle(BaseOptions.class).getString("PROP_base_toolbarVisible"),
239                     AllOptionsFolder.getDefault().isToolbarVisible());
240             item.addItemListener( new ItemListener JavaDoc() {
241                 public void itemStateChanged(ItemEvent JavaDoc e) {
242                     actionPerformed(null,null);
243                 }
244             });
245             return item;
246         }
247         
248         protected Class JavaDoc getShortDescriptionBundleClass() {
249             return BaseKit.class;
250         }
251         
252     }
253     
254     
255     public class NbBuildPopupMenuAction extends BuildPopupMenuAction {
256
257         static final long serialVersionUID =-8623762627678464181L;
258
259         protected JPopupMenu JavaDoc createPopupMenu(JTextComponent JavaDoc component) {
260             // to make keyboard navigation (Up/Down keys) inside popup work, we
261
// must use JPopupMenuPlus instead of JPopupMenu
262
return new org.openide.awt.JPopupMenuPlus();
263         }
264
265         protected JPopupMenu JavaDoc buildPopupMenu(JTextComponent JavaDoc component) {
266             EditorUI ui = Utilities.getEditorUI(component);
267             if (!ui.hasExtComponent()) {
268                 return null;
269             }
270             
271             JPopupMenu JavaDoc pm = createPopupMenu(component);
272             
273             String JavaDoc mimeType = NbEditorUtilities.getMimeType(component);
274             List JavaDoc l = PopupMenuActionsProvider.getPopupMenuItems(mimeType);
275             
276             if (l.isEmpty()){
277                 l = (List JavaDoc)Settings.getValue(Utilities.getKitClass(component),
278                     (ui == null || ui.hasExtComponent())
279                         ? ExtSettingsNames.POPUP_MENU_ACTION_NAME_LIST
280                         : ExtSettingsNames.DIALOG_POPUP_MENU_ACTION_NAME_LIST
281                 );
282             }
283             
284             if (l != null) {
285                 for (Iterator JavaDoc i = l.iterator(); i.hasNext(); ) {
286                     Object JavaDoc obj = i.next();
287                     
288                     if (obj == null || obj instanceof javax.swing.JSeparator JavaDoc) {
289                         addAction(component, pm, (String JavaDoc)null);
290                     } else if (obj instanceof String JavaDoc) {
291                         addAction(component, pm, (String JavaDoc)obj);
292                     } else if (obj instanceof Action JavaDoc) {
293                         addAction(component, pm, (Action JavaDoc)obj);
294                     } else if (obj instanceof DataFolder) {
295                         pm.add(new LayerSubFolderMenu(component, ((DataFolder) obj).getPrimaryFile()));
296                     }
297                 }
298             }
299             
300             return pm;
301         }
302
303         private Lookup getContextLookup(java.awt.Component JavaDoc component){
304             Lookup lookup = null;
305             for (java.awt.Component JavaDoc c = component; c != null; c = c.getParent()) {
306                 if (c instanceof Lookup.Provider) {
307                     lookup = ((Lookup.Provider)c).getLookup ();
308                     if (lookup != null) {
309                         break;
310                     }
311                 }
312             }
313             return lookup;
314         }
315         
316         private Action JavaDoc translateContextLookupAction(Lookup contextLookup, Action JavaDoc action) {
317             if (action instanceof ContextAwareAction && contextLookup != null){
318                 action = ((org.openide.util.ContextAwareAction)action)
319                 .createContextAwareInstance(contextLookup);
320             }
321             return action;
322         }
323         
324         private JMenuItem JavaDoc createLocalizedMenuItem(Action JavaDoc action) {
325             JMenuItem JavaDoc item;
326             if (action instanceof Presenter.Popup) {
327                 item = ((Presenter.Popup)action).getPopupPresenter();
328             } else {
329                 item = new JMenuItem JavaDoc(action);
330                 Mnemonics.setLocalizedText(item, item.getText());
331                 if (item.getIcon() != null) item.setIcon(null); //filter out icons
332
}
333             return item;
334         }
335         
336         private void assignAccelerator(Keymap JavaDoc km, Action JavaDoc action, JMenuItem JavaDoc item) {
337             if (item.getAccelerator() == null){
338                 KeyStroke JavaDoc ks = (KeyStroke JavaDoc)action.getValue(Action.ACCELERATOR_KEY);
339                 if (ks!=null) {
340                     item.setAccelerator(ks);
341                 } else {
342                     // Try to get the accelerator from keymap
343
if (km != null) {
344                         KeyStroke JavaDoc[] keys = km.getKeyStrokesForAction(action);
345                         if (keys != null && keys.length > 0) {
346                             item.setAccelerator(keys[0]);
347                         }
348                     }
349                 }
350             }
351         }
352         
353         protected void addAction(JTextComponent JavaDoc component, JPopupMenu JavaDoc popupMenu, Action JavaDoc action) {
354             Lookup contextLookup = getContextLookup(component);
355             
356             // issue #69688
357
if (contextLookup == null &&
358                     systemAction2editorAction.containsKey(action.getClass().getName())){
359                 addAction(component, popupMenu, (String JavaDoc) systemAction2editorAction.get(action.getClass().getName()));
360                 return;
361             }
362             
363             action = translateContextLookupAction(contextLookup, action);
364
365             if (action != null) {
366                 JMenuItem JavaDoc item = createLocalizedMenuItem(action);
367                 if (item instanceof DynamicMenuContent) {
368                     Component JavaDoc[] cmps = ((DynamicMenuContent)item).getMenuPresenters();
369                     for (int i = 0; i < cmps.length; i++) {
370                         popupMenu.add(cmps[i]);
371                     }
372                 } else {
373                     item.setEnabled(action.isEnabled());
374                     Object JavaDoc helpID = action.getValue ("helpID"); // NOI18N
375
if (helpID != null && (helpID instanceof String JavaDoc)) {
376                         item.putClientProperty ("HelpID", helpID); // NOI18N
377
}
378                     assignAccelerator(component.getKeymap(), action, item);
379                     debugPopupMenuItem(item, action);
380                     popupMenu.add(item);
381                 }
382             }
383         }
384         
385         private void addTopComponentActions(JTextComponent JavaDoc component, JPopupMenu JavaDoc popupMenu) {
386             Lookup contextLookup = getContextLookup(component);
387             // Get the cloneable-editor instance
388
TopComponent tc = NbEditorUtilities.getOuterTopComponent(component);
389             if (tc != null) {
390                 // Add all the actions
391
Action JavaDoc[] actions = tc.getActions();
392                 Component JavaDoc[] comps = org.openide.util.Utilities.actionsToPopup(actions, contextLookup).getComponents();
393                 for (int i = 0; i < comps.length; i++) {
394                     popupMenu.add(comps[i]);
395                 }
396             }
397         }
398
399         protected void addAction(JTextComponent JavaDoc component, JPopupMenu JavaDoc popupMenu,
400         String JavaDoc actionName) {
401             if (actionName != null) { // try if it's an action class name
402
// Check for the TopComponent actions
403
if (TopComponent.class.getName().equals(actionName)) {
404                     addTopComponentActions(component, popupMenu);
405                     return;
406
407                 } else { // not cloneable-editor actions
408

409                     // Try to load the action class
410
Class JavaDoc saClass = null;
411                     try {
412                         ClassLoader JavaDoc loader = (ClassLoader JavaDoc)Lookup.getDefault().lookup(ClassLoader JavaDoc.class);
413                         saClass = Class.forName(actionName, false, loader);
414                     } catch (Throwable JavaDoc t) {
415                     }
416                     
417                     if (saClass != null && SystemAction.class.isAssignableFrom(saClass)) {
418                         Lookup contextLookup = getContextLookup(component);
419                         Action JavaDoc action = SystemAction.get(saClass);
420                         action = translateContextLookupAction(contextLookup, action);
421                         
422                         JMenuItem JavaDoc item = createLocalizedMenuItem(action);
423                         if (item != null) {
424                             if (item instanceof DynamicMenuContent) {
425                                 Component JavaDoc[] cmps = ((DynamicMenuContent)item).getMenuPresenters();
426                                 for (int i = 0; i < cmps.length; i++) {
427                                     popupMenu.add(cmps[i]);
428                                 }
429                             } else {
430                                 if (!(item instanceof JMenu JavaDoc)) {
431                                     assignAccelerator(
432                                          (Keymap JavaDoc)Lookup.getDefault().lookup(Keymap JavaDoc.class),
433                                          action,
434                                          item
435                                     );
436                                 }
437                                 debugPopupMenuItem(item, action);
438                                 popupMenu.add(item);
439                             }
440                         }
441
442                         return;
443                     }
444                 }
445
446             }
447
448             super.addAction(component, popupMenu, actionName);
449
450         }
451
452
453     }
454
455     public class NbStopMacroRecordingAction extends ActionFactory.StopMacroRecordingAction {
456         
457         private BaseOptions bo;
458         
459         private Map JavaDoc getKBMap(){
460             Map JavaDoc ret;
461             List JavaDoc list = bo.getKeyBindingList();
462             if( list.size() > 0 &&
463             ( list.get( 0 ) instanceof Class JavaDoc || list.get( 0 ) instanceof String JavaDoc )
464             ) {
465                 list.remove( 0 ); //remove kit class name
466
}
467             ret = OptionUtilities.makeKeyBindingsMap(list);
468             return ret;
469         }
470         
471         protected MacroDialogSupport getMacroDialogSupport(Class JavaDoc kitClass){
472             return new NbMacroDialogSupport(kitClass);
473         }
474         
475         
476         private class NbMacroDialogSupport extends MacroDialogSupport{
477             
478             public NbMacroDialogSupport( Class JavaDoc kitClass ) {
479                 super(kitClass);
480             }
481             
482             public void actionPerformed(ActionEvent JavaDoc evt) {
483                 bo = BaseOptions.getOptions(NbEditorKit.this.getClass());
484                 Map JavaDoc oldMacroMap = null;
485                 Map JavaDoc oldKBMap = null;
486                 if (bo != null){
487                     oldMacroMap = bo.getMacroMap();
488                     oldKBMap = getKBMap();
489                 }
490
491                 super.actionPerformed(evt);
492
493                 if (bo != null){
494                     Map JavaDoc newMacroMap = bo.getMacroMap();
495                     bo.setMacroDiffMap(OptionUtilities.getMapDiff(oldMacroMap, newMacroMap, true));
496                     bo.setKeyBindingsDiffMap(OptionUtilities.getMapDiff(oldKBMap, getKBMap(), true));
497                     bo.setMacroMap(newMacroMap,false);
498                     bo.setKeyBindingList(bo.getKeyBindingList(), false);
499                 }
500             }
501             
502             protected int showConfirmDialog(String JavaDoc macroName){
503                 NotifyDescriptor confirm = new NotifyDescriptor.Confirmation(
504                 NbBundle.getMessage(MacrosEditorPanel.class,"MEP_Overwrite", macroName),
505                 NotifyDescriptor.YES_NO_CANCEL_OPTION,
506                 NotifyDescriptor.WARNING_MESSAGE
507                 );
508                 org.openide.DialogDisplayer.getDefault().notify(confirm);
509                 return ((Integer JavaDoc)confirm.getValue()).intValue();
510             }
511             
512         }
513         
514     }
515         
516     public static class NbUndoAction extends ActionFactory.UndoAction {
517
518         public void actionPerformed(ActionEvent JavaDoc evt, JTextComponent JavaDoc target) {
519             // Delegate to system undo action
520
UndoAction ua = (UndoAction)SystemAction.get(UndoAction.class);
521             if (ua != null && ua.isEnabled()) {
522                 ua.actionPerformed(evt);
523             }
524         }
525
526     }
527
528     public static class NbRedoAction extends ActionFactory.RedoAction {
529
530         public void actionPerformed(ActionEvent JavaDoc evt, JTextComponent JavaDoc target) {
531             // Delegate to system redo action
532
RedoAction ra = (RedoAction)SystemAction.get(RedoAction.class);
533             if (ra != null && ra.isEnabled()) {
534                 ra.actionPerformed(evt);
535             }
536         }
537
538     }
539
540     /** Switch visibility of line numbers in editor */
541     public class NbToggleLineNumbersAction extends ActionFactory.ToggleLineNumbersAction {
542
543         
544         public NbToggleLineNumbersAction() {
545         }
546         
547         protected boolean isLineNumbersVisible() {
548             return AllOptionsFolder.getDefault().getLineNumberVisible();
549         }
550         
551         protected void toggleLineNumbers() {
552             boolean numbersVisible = AllOptionsFolder.getDefault().getLineNumberVisible();
553             AllOptionsFolder.getDefault().setLineNumberVisible(!numbersVisible);
554         }
555         
556         public void actionPerformed(ActionEvent JavaDoc evt, JTextComponent JavaDoc target) {
557             toggleLineNumbers();
558         }
559         
560         
561     }
562
563     public static class NbGenerateGoToPopupAction extends BaseAction {
564
565         public NbGenerateGoToPopupAction() {
566             super(generateGoToPopupAction);
567             putValue(BaseAction.NO_KEYBINDING, Boolean.TRUE);
568         }
569
570         public void actionPerformed(ActionEvent JavaDoc evt, JTextComponent JavaDoc target) {
571         }
572
573         protected Class JavaDoc getShortDescriptionBundleClass() {
574             return NbEditorKit.class;
575         }
576         
577     }
578
579
580     public static class NbBuildToolTipAction extends BuildToolTipAction {
581
582         public void actionPerformed(ActionEvent JavaDoc evt, JTextComponent JavaDoc target) {
583             if (target != null) {
584                 NbToolTip.buildToolTip(target);
585             }
586         }
587
588     }
589     
590     public static class GenerateFoldPopupAction extends BaseAction {
591
592         private boolean addSeparatorBeforeNextAction;
593
594         public GenerateFoldPopupAction() {
595             super(generateFoldPopupAction);
596             putValue(BaseAction.NO_KEYBINDING, Boolean.TRUE);
597         }
598
599         protected Class JavaDoc getShortDescriptionBundleClass() {
600             return NbEditorKit.class;
601         }
602         
603         public void actionPerformed(ActionEvent JavaDoc evt, JTextComponent JavaDoc target) {
604         }
605
606         private void addAcceleretors(Action JavaDoc a, JMenuItem JavaDoc item, JTextComponent JavaDoc target){
607             // Try to get the accelerator
608
Keymap JavaDoc km = (target == null) ? BaseKit.getKit(BaseKit.class).getKeymap() :
609                     target.getKeymap();
610             if (km != null) {
611                 KeyStroke JavaDoc[] keys = km.getKeyStrokesForAction(a);
612                 if (keys != null && keys.length > 0) {
613                     boolean added = false;
614                     for (int i = 0; i<keys.length; i++){
615                         if ((keys[i].getKeyCode() == KeyEvent.VK_MULTIPLY) ||
616                             keys[i].getKeyCode() == KeyEvent.VK_ADD){
617                             item.setAccelerator(keys[i]);
618                             added = true;
619                             break;
620                         }
621                     }
622                     if (added == false) item.setAccelerator(keys[0]);
623                 }
624             }
625         }
626
627         protected String JavaDoc getItemText(JTextComponent JavaDoc target, String JavaDoc actionName, Action JavaDoc a) {
628             String JavaDoc itemText;
629             if (a instanceof BaseAction) {
630                 itemText = ((BaseAction)a).getPopupMenuText(target);
631             } else {
632                 itemText = actionName;
633             }
634             return itemText;
635         }
636         
637         
638         protected void addAction(JTextComponent JavaDoc target, JMenu JavaDoc menu,
639         String JavaDoc actionName) {
640             if (addSeparatorBeforeNextAction) {
641                 addSeparatorBeforeNextAction = false;
642                 menu.addSeparator();
643             }
644
645             BaseKit kit = (target == null) ? BaseKit.getKit(BaseKit.class) : Utilities.getKit(target);
646             if (!(kit instanceof BaseKit)) { //bugfix of #45101
647
kit = BaseKit.getKit(BaseKit.class);
648                 target = null;
649             }
650             if (kit == null) return;
651             boolean foldingEnabled = (target == null) ? false :
652                 ((Boolean JavaDoc)Settings.getValue(Utilities.getKitClass(target), SettingsNames.CODE_FOLDING_ENABLE)).booleanValue();
653             Action JavaDoc a = kit.getActionByName(actionName);
654             if (a != null) {
655                 JMenuItem JavaDoc item = null;
656                 if (a instanceof BaseAction) {
657                     item = ((BaseAction)a).getPopupMenuItem(target);
658                 }
659                 if (item == null) {
660                     String JavaDoc itemText = getItemText(target, actionName, a);
661                     if (itemText != null) {
662                         item = new JMenuItem JavaDoc(itemText);
663                         item.addActionListener(a);
664                         Mnemonics.setLocalizedText(item, itemText);
665                         addAcceleretors(a, item, target);
666                         item.setEnabled(a.isEnabled() && foldingEnabled);
667                         Object JavaDoc helpID = a.getValue ("helpID"); // NOI18N
668
if (helpID != null && (helpID instanceof String JavaDoc))
669                             item.putClientProperty ("HelpID", helpID); // NOI18N
670
}
671                 }
672
673                 if (item != null) {
674                     menu.add(item);
675                 }
676
677             } else { // action-name is null, add the separator
678
menu.addSeparator();
679             }
680         }
681         
682         protected void setAddSeparatorBeforeNextAction(boolean addSeparator) {
683             this.addSeparatorBeforeNextAction = addSeparator;
684         }
685         
686         protected void addAdditionalItems(JTextComponent JavaDoc target, JMenu JavaDoc menu){
687             setAddSeparatorBeforeNextAction(false);
688         }
689         
690         public JMenuItem JavaDoc getPopupMenuItem(JTextComponent JavaDoc target) {
691             String JavaDoc menuText = org.openide.util.NbBundle.getBundle (NbEditorKit.class).
692                 getString("Menu/View/CodeFolds");
693             JMenu JavaDoc menu = new JMenu JavaDoc(menuText);
694             Mnemonics.setLocalizedText(menu, menuText);
695             setAddSeparatorBeforeNextAction(false);
696             addAction(target, menu, BaseKit.collapseFoldAction);
697             addAction(target, menu, BaseKit.expandFoldAction);
698             setAddSeparatorBeforeNextAction(true);
699             addAction(target, menu, BaseKit.collapseAllFoldsAction);
700             addAction(target, menu, BaseKit.expandAllFoldsAction);
701             // By default add separator before next actions (can be overriden if unwanted)
702
setAddSeparatorBeforeNextAction(true);
703             if (target != null) addAdditionalItems(target, menu);
704             return menu;
705         }
706     
707     }
708
709     
710     private static final class LayerSubFolderMenu extends JMenu JavaDoc {
711
712         private static String JavaDoc getLocalizedName(FileObject f) {
713             try {
714                 return f.getFileSystem().getStatus().annotateName(
715                     f.getNameExt(),
716                     Collections.singleton(f));
717             } catch (FileStateInvalidException e) {
718                 return f.getNameExt();
719             }
720         }
721         
722         public LayerSubFolderMenu(JTextComponent JavaDoc target, FileObject folder) {
723             this(target, getLocalizedName(folder), ActionsList.convert(Arrays.asList(folder.getChildren())));
724         }
725         
726         private LayerSubFolderMenu(JTextComponent JavaDoc target, String JavaDoc text, List JavaDoc items) {
727             super();
728             Mnemonics.setLocalizedText(this, text);
729             
730             for (Iterator JavaDoc i = items.iterator(); i.hasNext(); ) {
731                 Object JavaDoc obj = i.next();
732                 
733                 if (obj == null || obj instanceof javax.swing.JSeparator JavaDoc) {
734                     addSeparator();
735                 } else if (obj instanceof String JavaDoc) {
736                     addAction(target, this, (String JavaDoc)obj);
737                 } else if (obj instanceof Action JavaDoc) {
738                     addAction(target, this, (Action JavaDoc)obj);
739                 } else if (obj instanceof DataFolder) {
740                     this.add(new LayerSubFolderMenu(target, ((DataFolder) obj).getPrimaryFile()));
741                 }
742             }
743         }
744
745         private static void addAcceleretors(Action JavaDoc a, JMenuItem JavaDoc item, JTextComponent JavaDoc target) {
746             // Try to get the accelerator
747
Keymap JavaDoc km = (target == null) ? BaseKit.getKit(BaseKit.class).getKeymap() :
748                     target.getKeymap();
749             if (km != null) {
750                 KeyStroke JavaDoc[] keys = km.getKeyStrokesForAction(a);
751                 if (keys != null && keys.length > 0) {
752                     boolean added = false;
753                     for (int i = 0; i<keys.length; i++){
754                         if ((keys[i].getKeyCode() == KeyEvent.VK_MULTIPLY) ||
755                             keys[i].getKeyCode() == KeyEvent.VK_ADD){
756                             item.setAccelerator(keys[i]);
757                             added = true;
758                             break;
759                         }
760                     }
761                     if (added == false) {
762                         item.setAccelerator(keys[0]);
763                     }
764                 }else if (a!=null){
765                     KeyStroke JavaDoc ks = (KeyStroke JavaDoc)a.getValue(Action.ACCELERATOR_KEY);
766                     if (ks!=null) {
767                         item.setAccelerator(ks);
768                     }
769                 }
770             }
771         }
772
773         private static String JavaDoc getItemText(JTextComponent JavaDoc target, String JavaDoc actionName, Action JavaDoc a) {
774             String JavaDoc itemText;
775             if (a instanceof BaseAction) {
776                 itemText = ((BaseAction)a).getPopupMenuText(target);
777             } else {
778                 Object JavaDoc value = a.getValue(BaseAction.POPUP_MENU_TEXT);
779                 itemText = (value instanceof String JavaDoc) ? (String JavaDoc)value : actionName;
780             }
781             return itemText;
782         }
783
784         private static void addAction(JTextComponent JavaDoc target, JMenu JavaDoc menu, String JavaDoc actionName) {
785             assert target != null : "The parameter target must not be null"; //NOI18N
786
assert menu != null : "The parameter menu must not be null"; //NOI18N
787
assert actionName != null : "The parameter actionName must not be null";//NOI18N
788

789             BaseKit kit = Utilities.getKit(target);
790             if (kit == null) return;
791             Action JavaDoc a = kit.getActionByName(actionName);
792             if (a != null) {
793                 addAction(target, menu, a);
794             }
795         }
796         
797         
798         private static void addAction(JTextComponent JavaDoc target, JMenu JavaDoc menu, Action JavaDoc action) {
799             assert target != null : "The parameter target must not be null"; //NOI18N
800
assert menu != null : "The parameter menu must not be null"; //NOI18N
801
assert action != null : "The parameter action must not be null"; //NOI18N
802

803             JMenuItem JavaDoc item = null;
804             if (action instanceof BaseAction) {
805                 item = ((BaseAction)action).getPopupMenuItem(target);
806             }
807             
808             if (item == null) {
809                 String JavaDoc actionName = (String JavaDoc) action.getValue(Action.NAME);
810                 String JavaDoc itemText = getItemText(target, actionName, action);
811                 if (itemText != null) {
812                     item = new JMenuItem JavaDoc(itemText);
813                     item.addActionListener(action);
814                     Mnemonics.setLocalizedText(item, itemText);
815                     addAcceleretors(action, item, target);
816                     item.setEnabled(action.isEnabled());
817                     Object JavaDoc helpID = action.getValue ("helpID"); // NOI18N
818
if (helpID != null && (helpID instanceof String JavaDoc)) {
819                         item.putClientProperty ("HelpID", helpID); // NOI18N
820
}
821                 }
822             }
823
824             if (item != null) {
825                 menu.add(item);
826             }
827         }
828     }
829 }
830
Popular Tags