KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > editor > hints > borrowed > ScrollCompletionPane


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.hints.borrowed;
21
22
23 import java.awt.Color JavaDoc;
24 import java.awt.Dimension JavaDoc;
25 import java.awt.event.KeyEvent JavaDoc;
26
27 import javax.swing.*;
28 import javax.swing.BorderFactory JavaDoc;
29 import javax.swing.event.ListSelectionListener JavaDoc;
30 import javax.swing.plaf.TextUI JavaDoc;
31 import javax.swing.text.JTextComponent JavaDoc;
32 import javax.swing.text.Keymap JavaDoc;
33 import javax.swing.text.EditorKit JavaDoc;
34
35 import org.netbeans.editor.*;
36 import org.netbeans.editor.ext.ExtSettingsDefaults;
37 import org.netbeans.editor.ext.ExtSettingsNames;
38 import org.netbeans.spi.editor.hints.Fix;
39 import org.netbeans.spi.editor.hints.LazyFixList;
40
41 /**
42 * Pane displaying the completion view and accompanying components
43 * like label for title etc.
44 *
45 * @author Miloslav Metelka, Martin Roskanin, Dusan Balek
46 * @version 1.00
47 */

48
49 public class ScrollCompletionPane extends JScrollPane implements SettingsChangeListener {
50     
51     private static final String JavaDoc COMPLETION_UP = "completion-up"; //NOI18N
52
private static final String JavaDoc COMPLETION_DOWN = "completion-down"; //NOI18N
53
private static final String JavaDoc COMPLETION_PGUP = "completion-pgup"; //NOI18N
54
private static final String JavaDoc COMPLETION_PGDN = "completion-pgdn"; //NOI18N
55
private static final String JavaDoc COMPLETION_BEGIN = "completion-begin"; //NOI18N
56
private static final String JavaDoc COMPLETION_END = "completion-end"; //NOI18N
57

58     private static final int ACTION_COMPLETION_UP = 1;
59     private static final int ACTION_COMPLETION_DOWN = 2;
60     private static final int ACTION_COMPLETION_PGUP = 3;
61     private static final int ACTION_COMPLETION_PGDN = 4;
62     private static final int ACTION_COMPLETION_BEGIN = 5;
63     private static final int ACTION_COMPLETION_END = 6;
64
65     private JTextComponent JavaDoc component;
66
67     private ListCompletionView view;
68     private JLabel topLabel;
69
70     private Dimension JavaDoc minSize;
71     private Dimension JavaDoc maxSize;
72     private Dimension JavaDoc scrollBarSize;
73
74     public ScrollCompletionPane(JTextComponent JavaDoc component, LazyFixList fixes, String JavaDoc title, ListSelectionListener JavaDoc listener) {
75         this.component = component;
76         
77         // Compute size of the scrollbars
78
Dimension JavaDoc smallSize = super.getPreferredSize();
79         setHorizontalScrollBarPolicy(HORIZONTAL_SCROLLBAR_NEVER);
80         setVerticalScrollBarPolicy(VERTICAL_SCROLLBAR_ALWAYS);
81         scrollBarSize = super.getPreferredSize();
82         scrollBarSize.width -= smallSize.width;
83         scrollBarSize.height -= smallSize.height;
84         setVerticalScrollBarPolicy(VERTICAL_SCROLLBAR_AS_NEEDED);
85         setBorder(
86             BorderFactory.createCompoundBorder(
87                 BorderFactory.createLineBorder(java.awt.SystemColor.controlDkShadow),
88                 BorderFactory.createEmptyBorder(4, 4, 4, 4)));
89         setViewportBorder( null );
90         Settings.addSettingsChangeListener(this);
91         settingsChange(null); // initialize sizes
92

93         // Add the completion view
94
view = new ListCompletionView();
95         setBackground(view.getBackground());
96 // view.addListSelectionListener(listener);
97
view.setResult(fixes);
98         resetViewSize();
99         setViewportView(view);
100
101         setTitle(title);
102         installKeybindings();
103         setFocusable (false);
104         view.setFocusable (false);
105     }
106     
107     public ListCompletionView getView() {
108         return view;
109     }
110
111     public void reset(LazyFixList fixes, String JavaDoc title) {
112         view.setResult(fixes);
113         resetViewSize();
114         setTitle(title);
115     }
116
117     public Fix getSelectedFix() {
118         Object JavaDoc ret = view.getSelectedValue();
119         return ret instanceof Fix ? (Fix) ret : null;
120     }
121     
122     public void settingsChange(SettingsChangeEvent evt) {
123         Class JavaDoc kitClass = Utilities.getKitClass(component);
124         if (kitClass != null) {
125             minSize = (Dimension JavaDoc)SettingsUtil.getValue(kitClass,
126                       ExtSettingsNames.COMPLETION_PANE_MIN_SIZE,
127                       ExtSettingsDefaults.defaultCompletionPaneMinSize);
128             setMinimumSize(minSize);
129
130             maxSize = (Dimension JavaDoc)SettingsUtil.getValue(kitClass,
131                       ExtSettingsNames.COMPLETION_PANE_MAX_SIZE,
132                       ExtSettingsDefaults.defaultCompletionPaneMaxSize);
133             setMaximumSize(maxSize);
134         }
135     }
136
137     public Dimension JavaDoc getPreferredSize() {
138         Dimension JavaDoc ps = super.getPreferredSize();
139
140         /* Add size of the vertical scrollbar by default. This could be improved
141         * to be done only if the height exceeds the bounds. */

142         int width = ps.width + scrollBarSize.width;
143         boolean displayHorizontalScrollbar = width > maxSize.width;
144         width = Math.max(Math.max(width, minSize.width),
145                             getTitleComponentPreferredSize().width);
146         width = Math.min(width, maxSize.width);
147
148         int height = displayHorizontalScrollbar ? ps.height + scrollBarSize.height : ps.height;
149         height = Math.min(height, maxSize.height);
150         height = Math.max(height, minSize.height);
151         return new Dimension JavaDoc(width, height);
152     }
153
154     private void resetViewSize() {
155         Dimension JavaDoc viewSize = view.getPreferredSize();
156         if (viewSize.width > maxSize.width - scrollBarSize.width) {
157             viewSize.width = maxSize.width - scrollBarSize.width;
158             view.setPreferredSize(viewSize);
159         }
160     }
161     
162     private void setTitle(String JavaDoc title) {
163         if (title == null) {
164             if (topLabel != null) {
165                 setColumnHeader(null);
166                 topLabel = null;
167             }
168         } else {
169             if (topLabel != null) {
170                 topLabel.setText(title);
171             } else {
172                 topLabel = new JLabel(title);
173                 topLabel.setForeground(Color.blue);
174                 topLabel.setBorder(BorderFactory.createEmptyBorder(0, 2, 0, 2));
175                 setColumnHeaderView(topLabel);
176             }
177         }
178     }
179
180     private Dimension JavaDoc getTitleComponentPreferredSize() {
181         return topLabel != null ? topLabel.getPreferredSize() : new Dimension JavaDoc();
182     }
183
184     /** Attempt to find the editor keystroke for the given editor action. */
185     private KeyStroke[] findEditorKeys(String JavaDoc editorActionName, KeyStroke defaultKey) {
186         // This method is implemented due to the issue
187
// #25715 - Attempt to search keymap for the keybinding that logically corresponds to the action
188
KeyStroke[] ret = new KeyStroke[] { defaultKey };
189         if (component != null) {
190             TextUI JavaDoc ui = component.getUI();
191             Keymap JavaDoc km = component.getKeymap();
192             if (ui != null && km != null) {
193                 EditorKit JavaDoc kit = ui.getEditorKit(component);
194                 if (kit instanceof BaseKit) {
195                     Action a = ((BaseKit)kit).getActionByName(editorActionName);
196                     if (a != null) {
197                         KeyStroke[] keys = km.getKeyStrokesForAction(a);
198                         if (keys != null && keys.length > 0) {
199                             ret = keys;
200                         }
201                     }
202                 }
203             }
204         }
205         return ret;
206     }
207
208     private void registerKeybinding(int action, String JavaDoc actionName, KeyStroke stroke, String JavaDoc editorActionName){
209         KeyStroke[] keys = findEditorKeys(editorActionName, stroke);
210         for (int i = 0; i < keys.length; i++) {
211             getInputMap().put(keys[i], actionName);
212         }
213         getActionMap().put(actionName, new CompletionPaneAction(action));
214     }
215
216     private void installKeybindings() {
217         // Register up key
218
registerKeybinding(ACTION_COMPLETION_UP, COMPLETION_UP,
219         KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0),
220         BaseKit.upAction
221         );
222
223         // Register down key
224
registerKeybinding(ACTION_COMPLETION_DOWN, COMPLETION_DOWN,
225         KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0),
226         BaseKit.downAction
227         );
228
229         // Register PgDn key
230
registerKeybinding(ACTION_COMPLETION_PGDN, COMPLETION_PGDN,
231         KeyStroke.getKeyStroke(KeyEvent.VK_PAGE_DOWN, 0),
232         BaseKit.pageDownAction
233         );
234
235         // Register PgUp key
236
registerKeybinding(ACTION_COMPLETION_PGUP, COMPLETION_PGUP,
237         KeyStroke.getKeyStroke(KeyEvent.VK_PAGE_UP, 0),
238         BaseKit.pageUpAction
239         );
240
241         // Register home key
242
registerKeybinding(ACTION_COMPLETION_BEGIN, COMPLETION_BEGIN,
243         KeyStroke.getKeyStroke(KeyEvent.VK_HOME, 0),
244         BaseKit.beginLineAction
245         );
246
247         // Register end key
248
registerKeybinding(ACTION_COMPLETION_END, COMPLETION_END,
249         KeyStroke.getKeyStroke(KeyEvent.VK_END, 0),
250         BaseKit.endLineAction
251         );
252     }
253
254     private class CompletionPaneAction extends AbstractAction {
255         private int action;
256
257         private CompletionPaneAction(int action) {
258             this.action = action;
259         }
260
261         public void actionPerformed(java.awt.event.ActionEvent JavaDoc actionEvent) {
262             switch (action) {
263                 case ACTION_COMPLETION_UP:
264                     view.up();
265                     break;
266                 case ACTION_COMPLETION_DOWN:
267                     view.down();
268                     break;
269                 case ACTION_COMPLETION_PGUP:
270                     view.pageUp();
271                     break;
272                 case ACTION_COMPLETION_PGDN:
273                         view.pageDown();
274                     break;
275                 case ACTION_COMPLETION_BEGIN:
276                         view.begin();
277                     break;
278                 case ACTION_COMPLETION_END:
279                         view.end();
280                     break;
281             }
282         }
283     }
284 }
285
Popular Tags