KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > editor > ext > JDCPopupPanel


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.editor.ext;
21
22 import java.awt.Component JavaDoc;
23 import java.awt.Dimension JavaDoc;
24 import java.awt.Rectangle JavaDoc;
25 import java.beans.PropertyChangeEvent JavaDoc;
26 import java.beans.PropertyChangeListener JavaDoc;
27 import java.awt.event.KeyEvent JavaDoc;
28 import java.awt.event.FocusListener JavaDoc;
29 import java.awt.event.FocusAdapter JavaDoc;
30 import java.awt.event.FocusEvent JavaDoc;
31 import java.awt.event.ActionListener JavaDoc;
32 import java.awt.event.ActionEvent JavaDoc;
33
34 import javax.swing.JComponent JavaDoc;
35 import javax.swing.JPanel JavaDoc;
36 import javax.swing.text.JTextComponent JavaDoc;
37 import javax.swing.KeyStroke JavaDoc;
38 import javax.swing.SwingUtilities JavaDoc;
39 import javax.swing.Action JavaDoc;
40 import javax.swing.AbstractAction JavaDoc;
41
42 import org.netbeans.editor.PopupManager;
43 import org.netbeans.editor.ext.ExtEditorUI;
44 import org.netbeans.editor.Utilities;
45 import org.netbeans.editor.BaseKit;
46 import org.netbeans.editor.ext.CompletionJavaDoc;
47 import org.netbeans.editor.ext.ExtKit;
48 import javax.swing.plaf.TextUI JavaDoc;
49 import javax.swing.text.Keymap JavaDoc;
50 import javax.swing.text.EditorKit JavaDoc;
51 import java.util.Iterator JavaDoc;
52 import java.util.ArrayList JavaDoc;
53 import java.util.List JavaDoc;
54 import org.netbeans.editor.SettingsChangeEvent;
55 import org.netbeans.editor.SettingsChangeListener;
56 import org.netbeans.editor.Settings;
57
58
59 /**
60  * Invisible panel that contains code completion panel and javadoc panel,
61  * computes preferred size and provides access to these both components.
62  *
63  * @author Martin Roskanin
64  * @since 03/2002
65  */

66 public class JDCPopupPanel extends JPanel JavaDoc implements PropertyChangeListener JavaDoc, SettingsChangeListener {
67     
68     private ExtCompletionPane completion;
69     
70     private ExtEditorUI extEditorUI;
71     private FocusListener JavaDoc focusL;
72     private List JavaDoc keyActionPairsList;
73     
74     // Completion can create documentation pane
75
//private Completion documentationProvider;
76
private JavaDocPane javadoc;
77     // last size of completion result
78
private int lastSize = -1;
79     
80     // gap between javadoc and code completion window
81
private static final int WINDOW_GAP = 1;
82     
83     private static final String JavaDoc POPUP_HIDE = "jdc-popup-hide"; //NOI18N
84

85     private static final String JavaDoc COMPLETION_UP = "completion-up"; //NOI18N
86
private static final String JavaDoc COMPLETION_DOWN = "completion-down"; //NOI18N
87
private static final String JavaDoc COMPLETION_PGUP = "completion-pgup"; //NOI18N
88
private static final String JavaDoc COMPLETION_PGDN = "completion-pgdn"; //NOI18N
89
private static final String JavaDoc COMPLETION_BEGIN = "completion-begin"; //NOI18N
90
private static final String JavaDoc COMPLETION_END = "completion-end"; //NOI18N
91

92     private static final String JavaDoc JAVADOC_UP = "javadoc-up"; //NOI18N
93
private static final String JavaDoc JAVADOC_DOWN = "javadoc-down"; //NOI18N
94
private static final String JavaDoc JAVADOC_PGUP = "javadoc-pgup"; //NOI18N
95
private static final String JavaDoc JAVADOC_PGDN = "javadoc-pgdn"; //NOI18N
96
private static final String JavaDoc JAVADOC_BEGIN = "javadoc-begin"; //NOI18N
97
private static final String JavaDoc JAVADOC_END = "javadoc-end"; //NOI18N
98
private static final String JavaDoc JAVADOC_LEFT = "javadoc-left"; //NOI18N
99
private static final String JavaDoc JAVADOC_RIGHT = "javadoc-right"; //NOI18N
100
private static final String JavaDoc JAVADOC_BACK = "javadoc-back"; //NOI18N
101
private static final String JavaDoc JAVADOC_FORWARD = "javadoc-forward"; //NOI18N
102
private static final String JavaDoc JAVADOC_OPEN_IN_BROWSER = "javadoc-open-in-browser"; //NOI18N
103
private static final String JavaDoc JAVADOC_OPEN_SOURCE = "javadoc-open-source"; //NOI18N
104

105     public static final String JavaDoc COMPLETION_SUBSTITUTE_TEXT= "completion-substitute-text"; //NOI18N
106
public static final String JavaDoc COMPLETION_SUBSTITUTE_TEXT_SIMPLE= "completion-substitute-text-simple"; //NOI18N
107
private static final String JavaDoc COMPLETION_SUBSTITUTE_TEXT_KEEP_POPUP_OPEN = "completion-substitute-text-keep-popup-open"; //NOI18N
108
private static final String JavaDoc COMPLETION_SUBSTITUTE_COMMON_TEXT = "completion-substitutecommon-text"; //NOI18N
109

110     private static final int ACTION_POPUP_HIDE = 1;
111     
112     private static final int ACTION_COMPLETION_UP = 2;
113     private static final int ACTION_COMPLETION_DOWN = 3;
114     private static final int ACTION_COMPLETION_PGUP = 4;
115     private static final int ACTION_COMPLETION_PGDN = 5;
116     private static final int ACTION_COMPLETION_BEGIN = 6;
117     private static final int ACTION_COMPLETION_END = 7;
118      
119     private static final int ACTION_JAVADOC_UP = 8;
120     private static final int ACTION_JAVADOC_DOWN = 9;
121     private static final int ACTION_JAVADOC_PGUP = 10;
122     private static final int ACTION_JAVADOC_PGDN = 11;
123     private static final int ACTION_JAVADOC_BEGIN = 12;
124     private static final int ACTION_JAVADOC_END = 13;
125     private static final int ACTION_JAVADOC_LEFT = 14;
126     private static final int ACTION_JAVADOC_RIGHT = 15;
127     private static final int ACTION_JAVADOC_BACK = 16;
128     private static final int ACTION_JAVADOC_FORWARD = 17;
129     private static final int ACTION_JAVADOC_OPEN_IN_BROWSER = 18;
130     private static final int ACTION_JAVADOC_OPEN_SOURCE = 19;
131
132     private static final int ACTION_COMPLETION_SUBSTITUTE_TEXT = 20;
133     private static final int ACTION_COMPLETION_SUBSTITUTE_TEXT_SIMPLE = 21;
134     private static final int ACTION_COMPLETION_SUBSTITUTE_TEXT_KEEP_POPUP_OPEN = 22;
135     private static final int ACTION_COMPLETION_SUBSTITUTE_COMMON_TEXT = 23;
136     
137     /** Creates a new instance of JDCPopupPanel */
138     public JDCPopupPanel(ExtEditorUI extEditorUI, ExtCompletionPane completion, Completion documentationProvider) {
139         super();
140         this.completion = completion;
141         //this.documentationProvider = documentationProvider;
142
this.extEditorUI = extEditorUI;
143         this.keyActionPairsList = new ArrayList JavaDoc();
144         
145         setLayout(null);
146         setOpaque(false); // make panel background invisible
147

148         focusL = new FocusAdapter JavaDoc() {
149             public void focusLost(FocusEvent JavaDoc evt) {
150                 SwingUtilities.invokeLater( new Runnable JavaDoc() {
151                     public void run() {
152                         if (isVisible()) {
153                             JTextComponent JavaDoc component = JDCPopupPanel.this.extEditorUI.getComponent();
154                             if (component != null) {
155                                 java.awt.Window JavaDoc w = SwingUtilities.windowForComponent(
156                                 component);
157                                 Component JavaDoc focusOwner = (w == null) ?
158                                 null : w.getFocusOwner();
159                                 
160                                 if (focusOwner == null) {
161                                     setVisible(false);
162                                 }
163                                 
164                                 boolean docO = javadoc != null && focusOwner == JDCPopupPanel.this.getJavaDocView();
165                                 boolean docA = javadoc != null && JDCPopupPanel.this.getJavaDocPane().getComponent().isAncestorOf(focusOwner);
166                                 boolean comO = focusOwner == JDCPopupPanel.this.getCompletionView();
167                                 if (docO || comO || docA) {
168                                     component.requestFocus();
169                                 }else if ( focusOwner != component ) {
170                                     setVisible(false); // completion, javadoc and component don't own the focus
171
}
172                             }
173                         }
174                     }
175                 } );
176             }
177         };
178         
179         
180         synchronized (extEditorUI.getComponentLock()) {
181             // if component already installed in ExtEditorUI simulate installation
182
JTextComponent JavaDoc component = extEditorUI.getComponent();
183             if (component != null) {
184                 propertyChange(new PropertyChangeEvent JavaDoc(extEditorUI,
185                 ExtEditorUI.COMPONENT_PROPERTY, null, component));
186             }
187             extEditorUI.addPropertyChangeListener(this);
188         }
189         
190         super.setVisible(false);
191         
192         Settings.addSettingsChangeListener(this);
193     }
194
195     /** Returns completion pane */
196     public ExtCompletionPane getCompletionPane(){
197         return completion;
198     }
199     
200     private void clearJavadocContent(){
201         CompletionJavaDoc cjd = extEditorUI.getCompletionJavaDoc();
202         if (cjd!=null){
203             cjd.clearContent();
204         }
205     }
206
207     private JavaDocView getJavaDocView(){
208         CompletionJavaDoc cjd = extEditorUI.getCompletionJavaDoc();
209         if (cjd!=null){
210             return cjd.getJavaDocView();
211         }
212         return null;
213     }
214     
215     private CompletionView getCompletionView(){
216         Completion c = extEditorUI.getCompletion();
217         if (c!=null){
218             return c.getView();
219         }
220         return null;
221     }
222     
223     /** Returns javadoc pane */
224     public JavaDocPane getJavaDocPane(){
225         if (javadoc == null) {
226             Completion c = extEditorUI.getCompletion();
227             if (c!=null){
228                 javadoc = c.getJavaDocPane();
229             }
230         }
231         return javadoc;
232     }
233     
234     /** Returns minimum size that this component can occupy. Because javadoc can be omitted,
235      * the minimum size is the minimum size of code completion */

236     public Dimension JavaDoc getMinimumSize(){
237         return completion.getComponent().getMinimumSize();
238     }
239     
240     /** Returns the maximum size of this component. Maximum size is computed from
241      * javadoc's and completion's maximum size */

242     public Dimension JavaDoc getMaximumSize(){
243         Dimension JavaDoc compDim = completion.getComponent().getMaximumSize();
244         Dimension JavaDoc javadocDim = new Dimension JavaDoc();
245         if (javadoc != null) {
246             javadocDim = javadoc.getComponent().getMaximumSize();
247         }
248         return new Dimension JavaDoc(Math.max(compDim.width,javadocDim.width), compDim.height+javadocDim.height);
249     }
250
251     /** Returns the preferred size of this component. Preferred size is computed from
252      * javadoc's and completion's preferred size trimmed to fit maximum size */

253     public Dimension JavaDoc getPreferredSize(){
254         Dimension JavaDoc ret = new Dimension JavaDoc();
255         Dimension JavaDoc compPref = completion.getComponent().getPreferredSize();
256         Dimension JavaDoc compMax = completion.getComponent().getMaximumSize();
257         
258         Dimension JavaDoc javadocPref = new Dimension JavaDoc();
259         Dimension JavaDoc javadocMax = new Dimension JavaDoc();
260         if (javadoc != null) {
261             javadocPref = javadoc.getComponent().getPreferredSize();
262             javadocMax = javadoc.getComponent().getMaximumSize();
263         }
264         
265         ret.width = Math.min(compPref.width, compMax.width) + Math.min(javadocPref.width, javadocMax.width);
266         ret.height = Math.min(compPref.height, compMax.height) + Math.min(javadocPref.height, javadocMax.height);
267         
268         return ret;
269     }
270     
271     /** Sets completion pane visiblility */
272     public void setCompletionVisible(boolean visible){
273         completion.setVisible(visible);
274         setVisible(visible);
275     }
276     
277     /** Sets javadoc pane visibility */
278     public void setJavaDocVisible(boolean visible){
279         getJavaDocPane().getComponent().setVisible(visible);
280         if (visible || !getCompletionPane().isVisible()){
281             setVisible(visible);
282         }
283     }
284     
285     /** Sets this popup comonent visibility */
286     public void setVisible(boolean visible){
287         if (visible){
288             if (Utilities.getLastActiveComponent() != extEditorUI.getComponent()){
289                 return; // bugfix of #43230
290
}
291
292             boolean docv = javadoc != null && javadoc.getComponent().isVisible();
293             Completion c = extEditorUI.getCompletion();
294             if (c!=null){
295                 CompletionQuery.Result result = c.getLastResult();
296                 int resultSize = (result == null) ? -1 : result.getData().size();
297                 if (lastSize!=resultSize || !isVisible() || docv){
298                     extEditorUI.getPopupManager().install(this);
299                     lastSize = resultSize;
300                 }
301             }
302             if (docv || completion.isVisible()){
303                 super.setVisible(visible);
304             }
305         } else {
306             lastSize = -1;
307             clearJavadocContent();
308             Completion c = extEditorUI.getCompletion();
309             if (c!=null){
310                 c.completionCancel();
311             }
312             if (javadoc != null) {
313                 javadoc.getComponent().setVisible(visible);
314             }
315             completion.getComponent().setVisible(visible);
316             super.setVisible(visible);
317             PopupManager pm = extEditorUI.getPopupManager();
318             if (pm!=null) pm.uninstall(this);
319         }
320     }
321     
322     /** Setting size of popup panel. The height and size is computed to fit the best place on the screen */
323     public void setSize(int width, int height){
324         PopupManager.Placement placement = (PopupManager.Placement)getClientProperty(
325             PopupManager.Placement.class);
326         
327         Dimension JavaDoc completionMinSize = completion.getComponent().getMinimumSize();
328         if (completionMinSize.height > height) { // cannot fit
329
putClientProperty(PopupManager.Placement.class, null);
330         }
331
332         // first we will set size to completion and then if there will be space for javadoc we will display it.
333
completion.getComponent().setSize(width, height);
334         
335         Dimension JavaDoc javaDocMinSize = new Dimension JavaDoc();
336         if (javadoc != null) {
337             javaDocMinSize = javadoc.getComponent().getMinimumSize();
338         }
339         Dimension JavaDoc completionMaxSize = completion.getComponent().getMaximumSize();
340         
341         Rectangle JavaDoc completionBounds = new Rectangle JavaDoc(completion.getComponent().getSize());
342         Rectangle JavaDoc javadocBounds = new Rectangle JavaDoc();
343         if (javadoc != null) {
344             javadocBounds = new Rectangle JavaDoc(javadoc.getComponent().getMaximumSize());
345         }
346
347         boolean showJavaDoc = true;
348         if (javadoc == null || javadoc.getComponent().isVisible() == false) {
349             showJavaDoc = false;
350         }
351         
352         CompletionJavaDoc completionJavaDoc = extEditorUI.getCompletionJavaDoc();
353         if (completionJavaDoc!=null){
354             if(completionJavaDoc.autoPopup()){
355                 showJavaDoc = javadoc != null;
356             }
357         }
358                 
359         boolean showCompletion = getCompletionPane().isVisible();
360         
361         if (!showCompletion){
362             completionMinSize.height = 0;
363             completionMinSize.width = 0;
364             completionBounds = new Rectangle JavaDoc();
365         }else{
366             completionBounds.width = Math.min(completionMaxSize.width,completionBounds.width);
367             completionBounds.height = Math.min(completionMaxSize.height,completionBounds.height);
368         }
369         
370         // do not show javaDoc. There is no space available
371
if ((javaDocMinSize.height+completionMinSize.height)>height) showJavaDoc = false;
372         
373         if (showJavaDoc) {
374             
375             if ((completionBounds.height + javadocBounds.height) > height ){
376                 // javadocBounds.height should be resized
377
completionBounds.height = Math.max(Math.min((int)(height/2),completionBounds.height), completionMinSize.height);
378                 completionBounds.height = Math.min(130,completionBounds.height); // [PENDING] - maybe this should be in options
379
javadocBounds.height = Math.min((height - completionBounds.height - WINDOW_GAP ), javadocBounds.height);
380             }
381             
382             if (placement == PopupManager.Below) {
383                 completionBounds.y = 0;
384                 javadocBounds.y = completionBounds.height + WINDOW_GAP;
385             }else{
386                 completionBounds.y = javadocBounds.height + WINDOW_GAP;
387                 javadocBounds.y = 0;
388             }
389             
390         }
391         
392         //width algorhitm
393
if (width < javaDocMinSize.width){
394             showJavaDoc = false;
395         }else{
396             // going to compute the width of both windows
397
javadocBounds.width = Math.min(width,javadocBounds.width);
398             
399             JTextComponent JavaDoc component = extEditorUI.getComponent();
400             Rectangle JavaDoc extBounds = extEditorUI.getExtentBounds();
401             Rectangle JavaDoc caretRect = new Rectangle JavaDoc();
402             try {
403                 caretRect = component.getUI().modelToView(
404                         component, component.getCaret().getDot(), javax.swing.text.Position.Bias.Forward);
405             } catch(javax.swing.text.BadLocationException JavaDoc ble){
406             }
407             
408             if (width - javadocBounds.width < caretRect.x - extBounds.x) {
409                 if (caretRect.x - extBounds.x + completionBounds.width < width) {
410                     completionBounds.x = caretRect.x - extBounds.x - width + javadocBounds.width;
411                 } else {
412                     completionBounds.x = javadocBounds.width - completionBounds.width;
413                 }
414             } else {
415                 completionBounds.x = 0;
416             }
417         }
418         
419         
420         completion.setVisible(false);
421         boolean isJavadocVisible = getJavaDocPane().getComponent().isVisible();
422         if (isJavadocVisible && !showJavaDoc) getJavaDocPane().getComponent().setVisible(false);
423         
424         remove(completion.getComponent());
425         if (javadoc != null) {
426             remove(javadoc.getComponent());
427         }
428         
429         if (showJavaDoc) {
430             if (showCompletion){
431                 completion.getComponent().setBounds(completionBounds);
432                 getJavaDocPane().getComponent().setBounds(javadocBounds);
433                 super.setBounds(0,0,Math.max(completionBounds.width,javadocBounds.width),completionBounds.height+javadocBounds.height+WINDOW_GAP);
434                 add(completion.getComponent());
435                 add(getJavaDocPane().getComponent());
436                 completion.setVisible(true);
437                 if (isJavadocVisible) getJavaDocPane().getComponent().setVisible(true);
438             }else{
439                 javadocBounds.x = 0; javadocBounds.y = 0;
440                 getJavaDocPane().getComponent().setBounds(javadocBounds);
441                 super.setBounds(0,0,javadocBounds.width,javadocBounds.height);
442                 add(getJavaDocPane().getComponent());
443                 if (isJavadocVisible) getJavaDocPane().getComponent().setVisible(true);
444             }
445         }else{
446             completionBounds.x = 0; completionBounds.y = 0;
447             completion.getComponent().setBounds(completionBounds);
448             super.setBounds(0,0,completionBounds.width,completionBounds.height+WINDOW_GAP);
449             add(completion.getComponent());
450             completion.setVisible(true);
451         }
452     }
453     
454     
455     public void setSize(Dimension JavaDoc d){
456         setSize(d.width, d.height);
457     }
458     
459     /** Refreshes the code completion content */
460     public void refresh(){
461         setVisible(true);
462     }
463     
464     private void performJavaDocAction(KeyStroke JavaDoc ks){
465         ActionListener JavaDoc act = getJavaDocPane().getJavadocDisplayComponent().getActionForKeyStroke(ks);
466         if (act!=null){
467             act.actionPerformed(new ActionEvent JavaDoc(getJavaDocPane().getJavadocDisplayComponent(), ActionEvent.ACTION_PERFORMED, "")); //NOI18N
468
getJavaDocPane().getJavadocDisplayComponent().repaint();
469         }
470     }
471     
472     /** Attempt to find the editor keystroke for the given editor action. */
473     private KeyStroke JavaDoc[] findEditorKeys(String JavaDoc editorActionName, KeyStroke JavaDoc defaultKey) {
474         // This method is implemented due to the issue
475
// #25715 - Attempt to search keymap for the keybinding that logically corresponds to the action
476
KeyStroke JavaDoc[] ret = new KeyStroke JavaDoc[] { defaultKey };
477         if (editorActionName != null && extEditorUI != null) {
478             JTextComponent JavaDoc component = extEditorUI.getComponent();
479             if (component != null) {
480                 TextUI JavaDoc ui = component.getUI();
481                 Keymap JavaDoc km = component.getKeymap();
482                 if (ui != null && km != null) {
483                     EditorKit JavaDoc kit = ui.getEditorKit(component);
484                     if (kit instanceof BaseKit) {
485                         Action JavaDoc a = ((BaseKit)kit).getActionByName(editorActionName);
486                         if (a != null) {
487                             KeyStroke JavaDoc[] keys = km.getKeyStrokesForAction(a);
488                             if (keys != null && keys.length > 0) {
489                                 ret = keys;
490                             }
491                         }
492                     }
493                 }
494             }
495         }
496
497         return ret;
498     }
499     
500     private void registerKeybinding(int action, String JavaDoc actionName, KeyStroke JavaDoc stroke, String JavaDoc editorActionName){
501         KeyStroke JavaDoc[] keys = findEditorKeys(editorActionName, stroke);
502         for (int i = 0; i < keys.length; i++) {
503             getInputMap().put(keys[i], actionName);
504             keyActionPairsList.add(actionName); // add action-name
505
keyActionPairsList.add(keys[i]); // add keystroke
506
}
507
508         getActionMap().put(actionName, new JDCPopupAction(action));
509     }
510     
511     private void unregisterKeybinding(String JavaDoc actionName, KeyStroke JavaDoc stroke) {
512         getInputMap().remove(stroke);
513         getActionMap().remove(actionName);
514     }
515     
516     private void installKeybindings() {
517         // Register escape key
518
registerKeybinding(ACTION_POPUP_HIDE, POPUP_HIDE,
519         KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
520         ExtKit.escapeAction
521         );
522
523         // Register up key
524
registerKeybinding(ACTION_COMPLETION_UP, COMPLETION_UP,
525         KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0),
526         BaseKit.upAction
527         );
528
529         // Register down key
530
registerKeybinding(ACTION_COMPLETION_DOWN, COMPLETION_DOWN,
531         KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0),
532         BaseKit.downAction
533         );
534
535         // Register PgDn key
536
registerKeybinding(ACTION_COMPLETION_PGDN, COMPLETION_PGDN,
537         KeyStroke.getKeyStroke(KeyEvent.VK_PAGE_DOWN, 0),
538         BaseKit.pageDownAction
539         );
540
541         // Register PgUp key
542
registerKeybinding(ACTION_COMPLETION_PGUP, COMPLETION_PGUP,
543         KeyStroke.getKeyStroke(KeyEvent.VK_PAGE_UP, 0),
544         BaseKit.pageUpAction
545         );
546
547         // Register home key
548
registerKeybinding(ACTION_COMPLETION_BEGIN, COMPLETION_BEGIN,
549         KeyStroke.getKeyStroke(KeyEvent.VK_HOME, 0),
550         BaseKit.beginLineAction
551         );
552
553         // Register end key
554
registerKeybinding(ACTION_COMPLETION_END, COMPLETION_END,
555         KeyStroke.getKeyStroke(KeyEvent.VK_END, 0),
556         BaseKit.endLineAction
557         );
558
559         // Register javadoc up key
560
registerKeybinding(ACTION_JAVADOC_UP, JAVADOC_UP,
561         KeyStroke.getKeyStroke(KeyEvent.VK_UP, KeyEvent.SHIFT_MASK),
562         null
563         );
564
565         // Register javadoc down key
566
registerKeybinding(ACTION_JAVADOC_DOWN, JAVADOC_DOWN,
567         KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, KeyEvent.SHIFT_MASK),
568         null
569         );
570
571         // Register javadoc PgDn key
572
registerKeybinding(ACTION_JAVADOC_PGDN, JAVADOC_PGDN,
573         KeyStroke.getKeyStroke(KeyEvent.VK_PAGE_DOWN, KeyEvent.SHIFT_MASK),
574         null
575         );
576
577         // Register javadoc PgUp key
578
registerKeybinding(ACTION_JAVADOC_PGUP, JAVADOC_PGUP,
579         KeyStroke.getKeyStroke(KeyEvent.VK_PAGE_UP, KeyEvent.SHIFT_MASK),
580         null
581         );
582
583         // Register javadoc home key
584
registerKeybinding(ACTION_JAVADOC_BEGIN, JAVADOC_BEGIN,
585         KeyStroke.getKeyStroke(KeyEvent.VK_HOME, KeyEvent.SHIFT_MASK),
586         null
587         );
588
589         // Register javadoc end key
590
registerKeybinding(ACTION_JAVADOC_END, JAVADOC_END,
591         KeyStroke.getKeyStroke(KeyEvent.VK_END, KeyEvent.SHIFT_MASK),
592         null
593         );
594
595         // Register javadoc left key
596
registerKeybinding(ACTION_JAVADOC_LEFT, JAVADOC_LEFT,
597         KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, KeyEvent.SHIFT_MASK),
598         null
599         );
600
601         // Register javadoc right key
602
registerKeybinding(ACTION_JAVADOC_RIGHT, JAVADOC_RIGHT,
603         KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, KeyEvent.SHIFT_MASK),
604         null
605         );
606
607         // Register javadoc back key
608
registerKeybinding(ACTION_JAVADOC_BACK, JAVADOC_BACK,
609         KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, KeyEvent.ALT_MASK),
610         null
611         );
612
613         // Register javadoc forward key
614
registerKeybinding(ACTION_JAVADOC_FORWARD, JAVADOC_FORWARD,
615         KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, KeyEvent.ALT_MASK),
616         null
617         );
618
619         // Register open in external browser key
620
registerKeybinding(ACTION_JAVADOC_OPEN_IN_BROWSER, JAVADOC_OPEN_IN_BROWSER,
621         KeyStroke.getKeyStroke(KeyEvent.VK_F1, KeyEvent.ALT_MASK | KeyEvent.SHIFT_MASK),
622         null
623         );
624
625         // Register open the source in editor key
626
registerKeybinding(ACTION_JAVADOC_OPEN_SOURCE, JAVADOC_OPEN_SOURCE,
627         KeyStroke.getKeyStroke(KeyEvent.VK_O, KeyEvent.ALT_MASK | KeyEvent.CTRL_MASK),
628         null
629         );
630         
631         // Register substituteText text and close popup window
632
registerKeybinding(ACTION_COMPLETION_SUBSTITUTE_TEXT, COMPLETION_SUBSTITUTE_TEXT,
633         KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0),
634         null
635         );
636         
637         // Register simple substituteText text and close popup window
638
registerKeybinding(ACTION_COMPLETION_SUBSTITUTE_TEXT_SIMPLE, COMPLETION_SUBSTITUTE_TEXT_SIMPLE,
639         KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, KeyEvent.CTRL_MASK),
640         null
641         );
642
643         // Register substituteText text and keep popup window open
644
registerKeybinding(ACTION_COMPLETION_SUBSTITUTE_TEXT_KEEP_POPUP_OPEN, COMPLETION_SUBSTITUTE_TEXT_KEEP_POPUP_OPEN,
645         KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, KeyEvent.SHIFT_MASK),
646         null
647         );
648
649         // Register substituteCommonText
650
registerKeybinding(ACTION_COMPLETION_SUBSTITUTE_COMMON_TEXT, COMPLETION_SUBSTITUTE_COMMON_TEXT,
651         KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0),
652         null
653         );
654         
655     }
656      
657     private void uninstallKeybindings() {
658         for (Iterator JavaDoc it = keyActionPairsList.iterator(); it.hasNext();) {
659             unregisterKeybinding(
660                 (String JavaDoc)it.next(), // action-name
661
(KeyStroke JavaDoc)it.next() // keystroke
662
);
663         }
664         keyActionPairsList.clear();
665     }
666                 
667     
668     public void propertyChange(PropertyChangeEvent JavaDoc evt) {
669         String JavaDoc propName = evt.getPropertyName();
670         
671         if (ExtEditorUI.COMPONENT_PROPERTY.equals(propName)) {
672             if (evt.getNewValue() != null) { // just installed
673
JTextComponent JavaDoc component = extEditorUI.getComponent();
674                 installKeybindings();
675                 component.addFocusListener(focusL);
676                 
677             } else { // just deinstalled
678
JTextComponent JavaDoc component = (JTextComponent JavaDoc)evt.getOldValue();
679                 uninstallKeybindings();
680                 component.removeFocusListener(focusL);
681             }
682             
683         }
684     }
685     
686     public void settingsChange(SettingsChangeEvent evt) {
687         // Refresh keybindings
688
uninstallKeybindings();
689         installKeybindings();
690     }
691     
692     private class JDCPopupAction extends AbstractAction JavaDoc {
693         private int action;
694         
695         private JDCPopupAction(int action) {
696             this.action = action;
697         }
698         
699         public void actionPerformed(java.awt.event.ActionEvent JavaDoc actionEvent) {
700             switch (action) {
701                 case ACTION_POPUP_HIDE:
702                     setVisible(false);
703                     break;
704                 case ACTION_COMPLETION_UP:
705                     if (completion.isVisible()){
706                         getCompletionView().up();
707                     }else{
708                         performJavaDocAction(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0));
709                     }
710                     break;
711                 case ACTION_COMPLETION_DOWN:
712                     if (completion.isVisible()){
713                         getCompletionView().down();
714                     }else {
715                         performJavaDocAction(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0));
716                     }
717                     break;
718                 case ACTION_COMPLETION_PGUP:
719                     if (completion.isVisible()){
720                         getCompletionView().pageUp();
721                     } else{
722                         performJavaDocAction(KeyStroke.getKeyStroke(KeyEvent.VK_PAGE_UP, 0));
723                     }
724                     break;
725                 case ACTION_COMPLETION_PGDN:
726                     if (completion.isVisible()){
727                         getCompletionView().pageDown();
728                     }else{
729                         performJavaDocAction(KeyStroke.getKeyStroke(KeyEvent.VK_PAGE_DOWN, 0));
730                     }
731                     break;
732                 case ACTION_COMPLETION_BEGIN:
733                     if (completion.isVisible()){
734                         getCompletionView().begin();
735                     }else{
736                         performJavaDocAction(KeyStroke.getKeyStroke(KeyEvent.VK_HOME, 0));
737                     }
738                     break;
739                 case ACTION_COMPLETION_END:
740                     if (completion.isVisible()){
741                         getCompletionView().end();
742                     }else{
743                         performJavaDocAction(KeyStroke.getKeyStroke(KeyEvent.VK_END, 0));
744                     }
745                     break;
746                 case ACTION_JAVADOC_UP:
747                     performJavaDocAction(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0));
748                     break;
749                 case ACTION_JAVADOC_DOWN:
750                     performJavaDocAction(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0));
751                     break;
752                 case ACTION_JAVADOC_PGUP:
753                     performJavaDocAction(KeyStroke.getKeyStroke(KeyEvent.VK_PAGE_UP, 0));
754                     break;
755                 case ACTION_JAVADOC_PGDN:
756                     performJavaDocAction(KeyStroke.getKeyStroke(KeyEvent.VK_PAGE_DOWN, 0));
757                     break;
758                 case ACTION_JAVADOC_BEGIN:
759                     performJavaDocAction(KeyStroke.getKeyStroke(KeyEvent.VK_HOME, 0));
760                     break;
761                 case ACTION_JAVADOC_END:
762                     performJavaDocAction(KeyStroke.getKeyStroke(KeyEvent.VK_END, 0));
763                     break;
764                 case ACTION_JAVADOC_LEFT:
765                     performJavaDocAction(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0));
766                     break;
767                 case ACTION_JAVADOC_RIGHT:
768                     performJavaDocAction(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0));
769                     break;
770                 case ACTION_JAVADOC_BACK: {
771                     CompletionJavaDoc cjd = extEditorUI.getCompletionJavaDoc();
772                     if (cjd!=null){
773                         cjd.backHistory();
774                     }
775                 }
776                 break;
777                 case ACTION_JAVADOC_FORWARD: {
778                     CompletionJavaDoc cjd = extEditorUI.getCompletionJavaDoc();
779                     if (cjd!=null){
780                         cjd.forwardHistory();
781                     }
782                 }
783                 break;
784                 case ACTION_JAVADOC_OPEN_IN_BROWSER: {
785                     CompletionJavaDoc cjd = extEditorUI.getCompletionJavaDoc();
786                     if (cjd!=null && cjd.isExternalJavaDocMounted()){
787                         cjd.openInExternalBrowser();
788                     }
789                 }
790                 break;
791                 case ACTION_JAVADOC_OPEN_SOURCE: {
792                     CompletionJavaDoc cjd = extEditorUI.getCompletionJavaDoc();
793                     if (cjd!=null){
794                         cjd.goToSource();
795                     }
796                 }
797                 break;
798                 case ACTION_COMPLETION_SUBSTITUTE_TEXT: {
799                     Completion cc = extEditorUI.getCompletion();
800                     if (cc != null && cc.isPaneVisible()) {
801                         if (cc.substituteText(false)) {
802                             cc.setPaneVisible(false);
803                         } else {
804                             cc.refresh(false);
805                         }
806                     }
807                 }
808                 break;
809                 case ACTION_COMPLETION_SUBSTITUTE_TEXT_SIMPLE: {
810                     Completion cc = extEditorUI.getCompletion();
811                     if (cc != null && cc.isPaneVisible()) {
812                         if (cc.substituteSimpleText()) {
813                             cc.setPaneVisible(false);
814                         } else {
815                             cc.refresh(false);
816                         }
817                     }
818                 }
819                 break;
820                 case ACTION_COMPLETION_SUBSTITUTE_TEXT_KEEP_POPUP_OPEN: {
821                     Completion cc = extEditorUI.getCompletion();
822                     if (cc != null && cc.isPaneVisible()) {
823                         if (cc.substituteText( true )) {
824                         } else {
825                             cc.refresh(false);
826                         }
827                     }
828                 }
829                 break;
830                 case ACTION_COMPLETION_SUBSTITUTE_COMMON_TEXT: {
831                     Completion cc = extEditorUI.getCompletion();
832                     if (cc != null && cc.isPaneVisible()) {
833                         cc.refresh(false);
834                         cc.substituteCommonText();
835                     }
836                 }
837                 break;
838                 
839             }
840         }
841         
842     }
843     
844
845     
846 }
847
Popular Tags