KickJava   Java API By Example, From Geeks To Geeks.

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


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.Image JavaDoc;
24 import java.awt.Insets JavaDoc;
25 import java.awt.event.ActionEvent JavaDoc;
26 import java.awt.event.ActionListener JavaDoc;
27 import java.awt.event.InputEvent JavaDoc;
28 import java.awt.event.KeyEvent JavaDoc;
29 import java.awt.event.MouseEvent JavaDoc;
30 import java.awt.event.MouseListener JavaDoc;
31 import java.awt.event.MouseMotionListener JavaDoc;
32 import java.beans.PropertyChangeListener JavaDoc;
33 import java.lang.ref.Reference JavaDoc;
34 import java.lang.ref.WeakReference JavaDoc;
35 import java.util.ArrayList JavaDoc;
36 import java.util.Collections JavaDoc;
37 import java.util.HashMap JavaDoc;
38 import java.util.Iterator JavaDoc;
39 import java.util.List JavaDoc;
40 import java.util.Map JavaDoc;
41 import javax.swing.AbstractAction JavaDoc;
42 import javax.swing.AbstractButton JavaDoc;
43 import javax.swing.Action JavaDoc;
44 import javax.swing.ImageIcon JavaDoc;
45 import javax.swing.InputMap JavaDoc;
46 import javax.swing.JButton JavaDoc;
47 import javax.swing.JComponent JavaDoc;
48 import javax.swing.JSeparator JavaDoc;
49 import javax.swing.JToolBar JavaDoc;
50 import javax.swing.KeyStroke JavaDoc;
51 import javax.swing.SwingUtilities JavaDoc;
52 import javax.swing.UIManager JavaDoc;
53 import javax.swing.plaf.TextUI JavaDoc;
54 import javax.swing.plaf.ToolBarUI JavaDoc;
55 import javax.swing.text.DefaultEditorKit JavaDoc;
56 import javax.swing.text.EditorKit JavaDoc;
57 import javax.swing.text.JTextComponent JavaDoc;
58 import javax.swing.text.Keymap JavaDoc;
59 import org.netbeans.api.editor.mimelookup.MimeLookup;
60 import org.netbeans.api.editor.mimelookup.MimePath;
61 import org.netbeans.editor.BaseAction;
62 import org.netbeans.editor.BaseKit;
63 import org.netbeans.editor.MultiKeyBinding;
64 import org.netbeans.editor.Settings;
65 import org.netbeans.editor.SettingsChangeEvent;
66 import org.netbeans.editor.SettingsChangeListener;
67 import org.netbeans.editor.SettingsNames;
68 import org.netbeans.editor.Utilities;
69 import org.netbeans.modules.editor.impl.ToolbarActionsProvider;
70 import org.netbeans.modules.editor.options.AllOptionsFolder;
71 import org.netbeans.modules.editor.options.BaseOptions;
72 import org.openide.filesystems.FileChangeAdapter;
73 import org.openide.filesystems.FileChangeListener;
74 import org.openide.filesystems.FileEvent;
75 import org.openide.filesystems.FileObject;
76 import org.openide.filesystems.FileUtil;
77 import org.openide.filesystems.Repository;
78 import org.openide.loaders.DataObject;
79 import org.openide.nodes.Node;
80 import org.openide.util.ContextAwareAction;
81 import org.openide.util.Lookup;
82 import org.openide.util.actions.Presenter;
83 import org.openide.util.lookup.Lookups;
84 import org.openide.util.lookup.ProxyLookup;
85
86 /**
87  * Editor toolbar component.
88  * <br>
89  * Toolbar contents are obtained by merging of
90  * Editors/mime-type/Toolbars/Default
91  *
92  * @author Miloslav Metelka
93  * @version 1.00
94  */

95
96 /* package */ final class NbEditorToolBar extends JToolBar JavaDoc implements SettingsChangeListener {
97     
98     /** Flag for testing the sorting support by debugging messages. */
99     private static final boolean debugSort
100         = Boolean.getBoolean("netbeans.debug.editor.toolbar.sort"); // NOI18N
101

102     private static final Insets JavaDoc BUTTON_INSETS = new Insets JavaDoc(2, 1, 0, 1);
103     
104     // An empty lookup. Can't use Lookup.EMPTY as this can be returned by clients.
105
private static final Lookup NO_ACTION_CONTEXT = Lookups.fixed();
106     
107     private FileChangeListener moduleRegListener;
108
109     /** Runnable for returning the focus back to the last active text component. */
110     private static final Runnable JavaDoc returnFocusRunnable
111         = new Runnable JavaDoc() {
112             public void run() {
113                 Component JavaDoc c = Utilities.getLastActiveComponent();
114                 if (c != null) {
115                     c.requestFocus();
116                 }
117             }
118         };
119        
120     private static final ActionListener JavaDoc sharedActionListener
121         = new ActionListener JavaDoc() {
122             public void actionPerformed(ActionEvent JavaDoc evt) {
123                 SwingUtilities.invokeLater(returnFocusRunnable);
124             }
125         };
126
127     /** Shared mouse listener used for setting the border painting property
128      * of the toolbar buttons and for invoking the popup menu.
129      */

130     private static final MouseListener JavaDoc sharedMouseListener
131         = new org.openide.awt.MouseUtils.PopupMouseAdapter() {
132             public void mouseEntered(MouseEvent JavaDoc evt) {
133                 Object JavaDoc src = evt.getSource();
134                 
135                 if (src instanceof AbstractButton JavaDoc) {
136                     AbstractButton JavaDoc button = (AbstractButton JavaDoc)evt.getSource();
137                     if (button.isEnabled()) {
138                         button.setContentAreaFilled(true);
139                         button.setBorderPainted(true);
140                     }
141                 }
142             }
143             
144             public void mouseExited(MouseEvent JavaDoc evt) {
145                 Object JavaDoc src = evt.getSource();
146                 if (src instanceof AbstractButton JavaDoc)
147                 {
148                     AbstractButton JavaDoc button = (AbstractButton JavaDoc)evt.getSource();
149                     button.setContentAreaFilled(false);
150                     button.setBorderPainted(false);
151                 }
152             }
153             
154             protected void showPopup(MouseEvent JavaDoc evt) {
155             }
156         };
157        
158
159     
160     /** Text component for which the toolbar gets constructed. */
161     private Reference JavaDoc componentRef;
162     
163     private boolean presentersAdded;
164
165     private boolean addListener = true;
166     
167     private static final String JavaDoc NOOP_ACTION_KEY = "noop-action-key"; //NOI18N
168
private static final Action JavaDoc NOOP_ACTION = new NoOpAction();
169     
170    
171     public NbEditorToolBar(JTextComponent JavaDoc component) {
172         this.componentRef = new WeakReference JavaDoc(component);
173         
174         setFloatable(false);
175         //mkleint - instead of here, assign the border in CloneableEditor and MultiView module.
176
// // special border installed by core or no border if not available
177
// Border b = (Border)UIManager.get("Nb.Editor.Toolbar.border"); //NOI18N
178
// setBorder(b);
179
addMouseListener(sharedMouseListener);
180         Settings.addSettingsChangeListener(this);
181         settingsChange(null);
182
183         installModulesInstallationListener();
184         installNoOpActionMappings();
185     }
186
187     // issue #69642
188
private void installNoOpActionMappings(){
189         InputMap JavaDoc im = getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
190         // cut
191
KeyStroke JavaDoc[] keys = findEditorKeys(DefaultEditorKit.cutAction, KeyStroke.getKeyStroke(KeyEvent.VK_X, InputEvent.CTRL_MASK));
192         for (int i = 0; i < keys.length; i++) {
193             im.put(keys[i], NOOP_ACTION_KEY);
194         }
195         // copy
196
keys = findEditorKeys(DefaultEditorKit.copyAction, KeyStroke.getKeyStroke(KeyEvent.VK_C, InputEvent.CTRL_MASK));
197         for (int i = 0; i < keys.length; i++) {
198             im.put(keys[i], NOOP_ACTION_KEY);
199         }
200         // delete
201
keys = findEditorKeys(DefaultEditorKit.deleteNextCharAction, KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0)); //NOI18N
202
for (int i = 0; i < keys.length; i++) {
203             im.put(keys[i], NOOP_ACTION_KEY);
204         }
205         // paste
206
keys = findEditorKeys(DefaultEditorKit.pasteAction, KeyStroke.getKeyStroke(KeyEvent.VK_V, InputEvent.CTRL_MASK));
207         for (int i = 0; i < keys.length; i++) {
208             im.put(keys[i], NOOP_ACTION_KEY);
209         }
210         
211         getActionMap().put(NOOP_ACTION_KEY, NOOP_ACTION);
212     }
213     
214     /** See issue #57773 for details. Toolbar should be updated with possible changes after
215        module install/uninstall */

216     private void installModulesInstallationListener(){
217         moduleRegListener = new FileChangeAdapter() {
218             public void fileChanged(FileEvent fe){
219                 //some module installed/uninstalled. Refresh toolbar content
220
Runnable JavaDoc r = new Runnable JavaDoc() {
221                         public void run() {
222                             if (AllOptionsFolder.getDefault().isToolbarVisible()){
223                                 checkPresentersRemoved();
224                                 checkPresentersAdded();
225                             }
226                         }
227                      };
228                 Utilities.runInEventDispatchThread(r);
229             }
230         };
231
232         FileObject moduleRegistry = Repository.getDefault().getDefaultFileSystem().findResource("Modules"); //NOI18N
233

234         if (moduleRegistry !=null){
235             moduleRegistry.addFileChangeListener(
236                 FileUtil.weakFileChangeListener(moduleRegListener, moduleRegistry));
237         }
238     }
239     
240     public String JavaDoc getUIClassID() {
241         //For GTK and Aqua look and feels, we provide a custom toolbar UI -
242
//but we cannot override this globally or it will cause problems for
243
//the form editor & other things
244
if (UIManager.get("Nb.Toolbar.ui") != null) { //NOI18N
245
return "Nb.Toolbar.ui"; //NOI18N
246
} else {
247             return super.getUIClassID();
248         }
249     }
250     
251     public String JavaDoc getName() {
252         //Required for Aqua L&F toolbar UI
253
return "editorToolbar"; // NOI18N
254
}
255     
256     public void setUI(ToolBarUI JavaDoc ui){
257         addListener = false;
258         super.setUI(ui);
259         addListener = true;
260     }
261     
262     public synchronized void addMouseListener(MouseListener JavaDoc l){
263         if (addListener){
264             super.addMouseListener(l);
265         }
266     }
267     
268     public synchronized void addMouseMotionListener(MouseMotionListener JavaDoc l){
269         if (addListener){
270             super.addMouseMotionListener(l);
271         }
272     }
273     
274     public void settingsChange(SettingsChangeEvent evt) {
275         final boolean visible = isToolBarVisible();
276     final JTextComponent JavaDoc c = getComponent();
277         final boolean keyBindingsChanged =
278                 evt!=null &&
279                 SettingsNames.KEY_BINDING_LIST.equals(evt.getSettingName()) &&
280                 c != null
281         && evt.getKitClass() == Utilities.getKitClass(c);
282         Runnable JavaDoc r = new Runnable JavaDoc() {
283                 public void run() {
284                     if (visible) {
285                         checkPresentersAdded();
286                         if (keyBindingsChanged){ //#62487
287
installNoOpActionMappings();
288                             int componentCount = getComponentCount();
289                             String JavaDoc mimeType = NbEditorUtilities.getMimeType(c);
290                             Map JavaDoc keybsMap = getKeyBindingMap(mimeType);
291                             Component JavaDoc comps[] = getComponents();
292                             for (int i=0; i<comps.length; i++){
293                                 Component JavaDoc comp = comps[i];
294                                 if (comp instanceof JButton JavaDoc){
295                                     JButton JavaDoc button = (JButton JavaDoc)comp;
296                                     Action JavaDoc action = button.getAction();
297                                     if (action == null){
298                                         continue;
299                                     }
300                                     String JavaDoc actionName = (String JavaDoc) action.getValue(Action.NAME);
301                                     if (actionName == null){
302                                         continue;
303                                     }
304                                     
305                                     String JavaDoc tooltipText = button.getToolTipText();
306                                     if (tooltipText!=null){
307                                         int index = tooltipText.indexOf("("); //NOI18N
308
if (index > 0){
309                                             tooltipText = tooltipText.substring(0, index-1);
310                                         }
311                                     }
312                                     
313                                     MultiKeyBinding mkb = (MultiKeyBinding)keybsMap.get(actionName);
314                                     if (mkb != null){
315                                         button.setToolTipText(tooltipText
316                                             + " (" + getMnemonic(mkb) + ")"); // NOI18N
317
} else {
318                                         button.setToolTipText(tooltipText);
319                                     }
320                                 }
321                             }
322                         }
323                     } else {
324                         checkPresentersRemoved();
325                     }
326                     setVisible(visible);
327                 }
328              };
329         Utilities.runInEventDispatchThread(r);
330     }
331     
332     private void checkPresentersAdded() {
333         if (!presentersAdded) {
334             presentersAdded = true;
335             addPresenters();
336         }
337     }
338     
339     private void checkPresentersRemoved() {
340         presentersAdded = false;
341         removeAll();
342     }
343
344     private static boolean isToolBarVisible() {
345         return AllOptionsFolder.getDefault().isToolbarVisible();
346     }
347     
348     /** Utility method for getting the mnemonic of the multi key binding.
349      * @param binding multi key binding
350      * @return mnemonic for the binding.
351      */

352     private static String JavaDoc getMnemonic(MultiKeyBinding binding) {
353         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
354         if (binding.keys != null) { // multiple keys
355
for (int i = 0; i < binding.keys.length; i++) {
356                 if (i > 0) {
357                     sb.append(' ');
358                 }
359                 sb.append(getKeyMnemonic(binding.keys[i]));
360             }
361
362         } else { // multiple keys
363
sb.append(getKeyMnemonic(binding.key));
364         }
365         return sb.toString();
366     }
367
368     /**
369      * Get the mnemonic for a keystroke.
370      * @param key a keystroke
371      * @return mnemonic of tghe keystroke.
372      */

373     private static String JavaDoc getKeyMnemonic(KeyStroke JavaDoc key) {
374         String JavaDoc sk = org.openide.util.Utilities.keyToString(key);
375         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
376         int mods = key.getModifiers();
377         if ((mods & KeyEvent.CTRL_MASK) != 0) {
378             sb.append("Ctrl+"); // NOI18N
379
}
380         if ((mods & KeyEvent.ALT_MASK) != 0) {
381             sb.append("Alt+"); // NOI18N
382
}
383         if ((mods & KeyEvent.SHIFT_MASK) != 0) {
384             sb.append("Shift+"); // NOI18N
385
}
386         if ((mods & KeyEvent.META_MASK) != 0) {
387             sb.append("Meta+"); // NOI18N
388
}
389         
390         int i = sk.indexOf('-');
391         if (i != -1) {
392             sk = sk.substring(i + 1);
393         }
394         sb.append(sk);
395         
396         return sb.toString();
397     }
398
399     private static Map JavaDoc/*<String, MultiKeyBinding>*/ getKeyBindingMap(String JavaDoc mimeType) {
400         Map JavaDoc retMap = new HashMap JavaDoc();
401         List JavaDoc keybList = getKeyBindingList(mimeType);
402         Iterator JavaDoc it = keybList.iterator();
403         while(it.hasNext()){
404             Object JavaDoc obj = it.next();
405             if (obj instanceof MultiKeyBinding){
406                 MultiKeyBinding keyb = (MultiKeyBinding)obj;
407                 retMap.put(keyb.actionName, keyb);
408             }
409         }
410         return retMap;
411     }
412     
413     private static List JavaDoc getKeyBindingList(String JavaDoc mimeType) {
414         List JavaDoc keyBindingsList = new ArrayList JavaDoc();
415
416         AllOptionsFolder aof = AllOptionsFolder.getDefault();
417         if (aof != null) {
418             List JavaDoc gkbl = aof.getKeyBindingList();
419             if (gkbl != null) {
420                 keyBindingsList.addAll(gkbl);
421             }
422         }
423
424         if (mimeType != null) {
425             BaseOptions options = (BaseOptions) MimeLookup.getLookup(MimePath.parse(mimeType)).lookup(BaseOptions.class);
426             if (options != null) {
427                 List JavaDoc kbl = options.getKeyBindingList();
428                 if (kbl != null) {
429                     keyBindingsList.addAll(kbl);
430                 }
431             }
432         }
433         
434         return keyBindingsList;
435         
436     }
437     
438     private JTextComponent JavaDoc getComponent() {
439     return (JTextComponent JavaDoc)componentRef.get();
440     }
441     
442     /** Add the presenters (usually buttons) for the contents of the toolbar
443      * contained in the base and mime folders.
444      * @param baseFolder folder that corresponds to "text/base"
445      * @param mimeFolder target mime type folder.
446      * @param toolbar toolbar being constructed.
447      */

448     private void addPresenters() {
449         JTextComponent JavaDoc c = getComponent();
450         String JavaDoc mimeType = c == null ? null : NbEditorUtilities.getMimeType(c);
451         
452         if (mimeType == null) {
453             return; // Probably no component or it's not loaded properly
454
}
455
456         List JavaDoc keybindings = null;
457         Lookup actionContext = null;
458         List JavaDoc items = ToolbarActionsProvider.getToolbarItems(mimeType);
459         
460         // COMPAT: The ToolbarsActionsProvider treats 'text/base' in a special way. It
461
// will list only items registered for this particular mime type, but won't
462
// inherit anything else. The 'text/base' is normally empty, but could be
463
// used by some legacy code.
464
List JavaDoc oldTextBaseItems = ToolbarActionsProvider.getToolbarItems("text/base"); //NOI18N
465
if (oldTextBaseItems.size() > 0) {
466             items = new ArrayList JavaDoc(items);
467             items.add(new JSeparator JavaDoc());
468             items.addAll(oldTextBaseItems);
469         }
470         
471         for(Object JavaDoc item : items) {
472             if (item instanceof JSeparator JavaDoc) {
473                 addSeparator();
474                 continue;
475             }
476             
477             if (item instanceof String JavaDoc) {
478                 EditorKit JavaDoc kit = c.getUI().getEditorKit(c);
479                 if (kit instanceof BaseKit) {
480                     Action JavaDoc a = ((BaseKit) kit).getActionByName((String JavaDoc) item);
481                     if (a != null) {
482                         item = a;
483                     } else {
484                         // unknown action
485
continue;
486                     }
487                 }
488             }
489             
490             if (item instanceof ContextAwareAction) {
491                 if (actionContext == null) {
492                     Lookup context = createActionContext(c);
493                     actionContext = context == null ? NO_ACTION_CONTEXT : context;
494                 }
495                 
496                 if (actionContext != NO_ACTION_CONTEXT) {
497                     Action JavaDoc caa = ((ContextAwareAction) item).createContextAwareInstance(actionContext);
498                     
499                     // use the context aware instance only if it implements Presenter.Toolbar
500
// or is a Component else fall back to the original object
501
if (caa instanceof Presenter.Toolbar || caa instanceof Component JavaDoc) {
502                         item = caa;
503                     }
504                 }
505             }
506             
507             if (item instanceof Presenter.Toolbar) {
508                 Component JavaDoc presenter = ((Presenter.Toolbar) item).getToolbarPresenter();
509                 if (presenter != null) {
510                     item = presenter;
511                 }
512             }
513             
514             if (item instanceof Component JavaDoc) {
515                 add((Component JavaDoc)item);
516             } else if (item instanceof Action JavaDoc) {
517                 // Wrap action to execute on the proper text component
518
// because the default fallback in TextAction.getTextComponent()
519
// might not work properly if the focus was switched
520
// to e.g. a JTextField and then toolbar was clicked.
521
Action JavaDoc a = new WrapperAction(componentRef, (Action JavaDoc) item);
522                 
523                 // Try to find an icon if not present
524
updateIcon(a);
525
526                 // Add the action and let the JToolbar to creat a presenter for it
527
item = add(a);
528             } else {
529                 // Some sort of crappy item -> ignore
530
continue;
531             }
532
533             if (item instanceof AbstractButton JavaDoc) {
534                 AbstractButton JavaDoc button = (AbstractButton JavaDoc)item;
535                 processButton(button);
536                 
537                 if (keybindings == null) {
538                     List JavaDoc l = getKeyBindingList(mimeType);
539                     keybindings = l == null ? Collections.emptyList() : l;
540                 }
541                 updateTooltip(button, keybindings);
542             }
543         }
544     }
545     
546     // XXX: this is actually wierd, because it changes the action's properties
547
// perhaps we should just update the presenter, but should not touch the
548
// action itself
549
private static void updateIcon(Action JavaDoc a) {
550         Object JavaDoc icon = a.getValue(Action.SMALL_ICON);
551         if (icon == null) {
552             String JavaDoc resourceId = (String JavaDoc)a.getValue(BaseAction.ICON_RESOURCE_PROPERTY);
553             if (resourceId == null) { // use default icon
554
resourceId = "org/netbeans/modules/editor/resources/default.gif"; // NOI18N
555
}
556             Image JavaDoc img = org.openide.util.Utilities.loadImage(resourceId);
557             if (img != null) {
558                 a.putValue(Action.SMALL_ICON, new ImageIcon JavaDoc(img));
559             }
560         }
561     }
562
563     private static void updateTooltip(AbstractButton JavaDoc b, List JavaDoc keybindings) {
564         Action JavaDoc a = b.getAction();
565         String JavaDoc actionName = a == null ? null : (String JavaDoc) a.getValue(Action.NAME);
566         
567         if (actionName == null) {
568             // perhaps no action at all
569
return;
570         }
571         
572         for (Iterator JavaDoc kbIt = keybindings.iterator(); kbIt.hasNext();) {
573             Object JavaDoc o = kbIt.next();
574             if (o instanceof MultiKeyBinding) {
575                 MultiKeyBinding binding = (MultiKeyBinding)o;
576                 if (actionName.equals(binding.actionName)) {
577                     b.setToolTipText(b.getToolTipText() + " (" + getMnemonic(binding) + ")"); // NOI18N
578
break; // multiple shortcuts ?
579
}
580             }
581         }
582     }
583     
584     /**
585      * Not private because of the tests.
586      */

587     static Lookup createActionContext(JTextComponent JavaDoc c) {
588         Lookup nodeLookup = null;
589         DataObject dobj = (c != null) ? NbEditorUtilities.getDataObject(c.getDocument()) : null;
590         if (dobj != null && dobj.isValid()) {
591             nodeLookup = dobj.getNodeDelegate().getLookup();
592         }
593
594         Lookup ancestorLookup = null;
595         for (java.awt.Component JavaDoc comp = c; comp != null; comp = comp.getParent()) {
596             if (comp instanceof Lookup.Provider) {
597                 Lookup lookup = ((Lookup.Provider)comp).getLookup ();
598                 if (lookup != null) {
599                     ancestorLookup = lookup;
600                     break;
601                 }
602             }
603         }
604
605         if (nodeLookup == null) {
606             return ancestorLookup;
607         } else if (ancestorLookup == null) {
608             return nodeLookup;
609         }
610         assert nodeLookup != null && ancestorLookup != null;
611
612         Node node = (Node)nodeLookup.lookup(Node.class);
613         boolean ancestorLookupContainsNode = ancestorLookup.lookup(
614                 new Lookup.Template(Node.class)).allInstances().contains(node);
615
616         if (ancestorLookupContainsNode) {
617             return ancestorLookup;
618         } else {
619             return new ProxyLookup(new Lookup[] { nodeLookup, ancestorLookup });
620         }
621     }
622
623     private void processButton(AbstractButton JavaDoc button) {
624         button.setContentAreaFilled(false);
625         button.setBorderPainted(false);
626         button.addActionListener(sharedActionListener);
627         button.setMargin(BUTTON_INSETS);
628         if (button instanceof AbstractButton JavaDoc) {
629             button.addMouseListener(sharedMouseListener);
630         }
631         //fix of issue #69642. Focus shouldn't stay in toolbar
632
button.setFocusable(false);
633     }
634
635     /** Attempt to find the editor keystroke for the given action. */
636     private KeyStroke JavaDoc[] findEditorKeys(String JavaDoc editorActionName, KeyStroke JavaDoc defaultKey) {
637         KeyStroke JavaDoc[] ret = new KeyStroke JavaDoc[] { defaultKey };
638         JTextComponent JavaDoc comp = getComponent();
639         if (editorActionName != null && comp != null) {
640             TextUI JavaDoc ui = comp.getUI();
641             Keymap JavaDoc km = comp.getKeymap();
642             if (ui != null && km != null) {
643                 EditorKit JavaDoc kit = ui.getEditorKit(comp);
644                 if (kit instanceof BaseKit) {
645                     Action JavaDoc a = ((BaseKit)kit).getActionByName(editorActionName);
646                     if (a != null) {
647                         KeyStroke JavaDoc[] keys = km.getKeyStrokesForAction(a);
648                         if (keys != null && keys.length > 0) {
649                             ret = keys;
650                         } else {
651                             // try kit's keymap
652
Keymap JavaDoc km2 = ((BaseKit)kit).getKeymap();
653                             KeyStroke JavaDoc[] keys2 = km2.getKeyStrokesForAction(a);
654                             if (keys2 != null && keys2.length > 0) {
655                                 ret = keys2;
656                             }
657                         }
658                     }
659                 }
660             }
661         }
662         return ret;
663     }
664     
665     /** No operation action - do nothing when invoked
666      * issue #69642
667      */

668     private static final class NoOpAction extends AbstractAction JavaDoc{
669         public NoOpAction(){
670         }
671         public void actionPerformed(ActionEvent JavaDoc e) {
672         }
673     } // End of NoOpAction class
674

675     private static final class WrapperAction implements Action JavaDoc {
676         
677         private final Reference JavaDoc componentRef;
678         
679         private final Action JavaDoc delegate;
680         
681         WrapperAction(Reference JavaDoc componentRef, Action JavaDoc delegate) {
682             this.componentRef = componentRef;
683             assert (delegate != null);
684             this.delegate = delegate;
685         }
686         
687         public Object JavaDoc getValue(String JavaDoc key) {
688             return delegate.getValue(key);
689         }
690
691         public void putValue(String JavaDoc key, Object JavaDoc value) {
692             delegate.putValue(key, value);
693         }
694
695         public void setEnabled(boolean b) {
696             delegate.setEnabled(b);
697         }
698
699         public boolean isEnabled() {
700             return delegate.isEnabled();
701         }
702
703         public void addPropertyChangeListener(PropertyChangeListener JavaDoc listener) {
704             delegate.addPropertyChangeListener(listener);
705         }
706
707         public void removePropertyChangeListener(PropertyChangeListener JavaDoc listener) {
708             delegate.removePropertyChangeListener(listener);
709         }
710
711         public void actionPerformed(ActionEvent JavaDoc e) {
712             JTextComponent JavaDoc c = (JTextComponent JavaDoc)componentRef.get();
713             if (c != null) { // Override action event to text component
714
e = new ActionEvent JavaDoc(c, e.getID(), e.getActionCommand());
715             }
716             delegate.actionPerformed(e);
717         }
718     } // End of WrapperAction class
719
}
720
Popular Tags