KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > editor > completion > CompletionScrollPane


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.completion;
21
22
23 import java.awt.Color JavaDoc;
24 import java.awt.Dimension JavaDoc;
25 import java.awt.Point JavaDoc;
26 import java.awt.Rectangle JavaDoc;
27 import java.awt.event.ActionEvent JavaDoc;
28 import java.awt.event.KeyEvent JavaDoc;
29 import java.awt.event.MouseListener JavaDoc;
30 import java.util.List JavaDoc;
31 import javax.swing.AbstractAction JavaDoc;
32 import javax.swing.Action JavaDoc;
33 import javax.swing.BorderFactory JavaDoc;
34 import javax.swing.JLabel JavaDoc;
35 import javax.swing.JScrollPane JavaDoc;
36 import javax.swing.KeyStroke JavaDoc;
37 import javax.swing.event.ListSelectionListener JavaDoc;
38 import javax.swing.plaf.TextUI JavaDoc;
39 import javax.swing.text.JTextComponent JavaDoc;
40 import javax.swing.text.Keymap JavaDoc;
41 import javax.swing.text.EditorKit JavaDoc;
42 import org.netbeans.editor.BaseKit;
43 import org.netbeans.editor.ext.ExtKit;
44 import org.netbeans.spi.editor.completion.CompletionItem;
45
46 /**
47 * Pane displaying the completion view and accompanying components
48 * like label for title etc.
49 *
50 * @author Miloslav Metelka, Martin Roskanin, Dusan Balek
51 * @version 1.00
52 */

53
54 public class CompletionScrollPane extends JScrollPane JavaDoc {
55     
56     private static final String JavaDoc ESCAPE = "escape"; //NOI18N
57
private static final String JavaDoc COMPLETION_UP = "completion-up"; //NOI18N
58
private static final String JavaDoc COMPLETION_DOWN = "completion-down"; //NOI18N
59
private static final String JavaDoc COMPLETION_PGUP = "completion-pgup"; //NOI18N
60
private static final String JavaDoc COMPLETION_PGDN = "completion-pgdn"; //NOI18N
61
private static final String JavaDoc COMPLETION_BEGIN = "completion-begin"; //NOI18N
62
private static final String JavaDoc COMPLETION_END = "completion-end"; //NOI18N
63

64     private static final int ACTION_ESCAPE = 0;
65     private static final int ACTION_COMPLETION_UP = 1;
66     private static final int ACTION_COMPLETION_DOWN = 2;
67     private static final int ACTION_COMPLETION_PGUP = 3;
68     private static final int ACTION_COMPLETION_PGDN = 4;
69     private static final int ACTION_COMPLETION_BEGIN = 5;
70     private static final int ACTION_COMPLETION_END = 6;
71
72     private CompletionJList view;
73     
74     private List JavaDoc dataObj;
75     
76     private JLabel JavaDoc topLabel;
77     
78     public CompletionScrollPane(JTextComponent JavaDoc editorComponent,
79     ListSelectionListener JavaDoc listSelectionListener, MouseListener JavaDoc mouseListener) {
80         
81         setHorizontalScrollBarPolicy(HORIZONTAL_SCROLLBAR_NEVER);
82         setVerticalScrollBarPolicy(VERTICAL_SCROLLBAR_AS_NEEDED);
83         
84         // Use maximumSize property to store the limit of the preferred size
85
setMaximumSize(CompletionSettings.INSTANCE.completionPopupMaximumSize());
86         // At least 2 items; do -1 for title height
87
int maxVisibleRowCount = Math.max(2,
88             getMaximumSize().height / CompletionLayout.COMPLETION_ITEM_HEIGHT - 1);
89
90         // Add the completion view
91
view = new CompletionJList(maxVisibleRowCount, mouseListener);
92         if (listSelectionListener != null) {
93             view.addListSelectionListener(listSelectionListener);
94         }
95         setViewportView(view);
96         installKeybindings(editorComponent);
97     }
98     
99     public void setData(List JavaDoc data, String JavaDoc title, int selectedIndex) {
100         dataObj = data;
101         view.setData(data);
102         view.setSelectedIndex(selectedIndex);
103         Point JavaDoc p = view.indexToLocation(selectedIndex);
104         if (p != null)
105             view.scrollRectToVisible(new Rectangle JavaDoc(p));
106         setTitle(title);
107         // Force the viewport preferred size to be taken into account
108
// Otherwise the scroll pane attempts to retain its size
109
// so e.g. if the number of visible rows increases so the vertical
110
// scrollbar would be needed the scrollpane does not increase
111
// its preferred size.
112
// Resetting of viewport fixes the problem.
113
setViewportView(getViewport().getView());
114     }
115     
116     public CompletionItem getSelectedCompletionItem() {
117         Object JavaDoc ret = view.getSelectedValue();
118         return ret instanceof CompletionItem ? (CompletionItem) ret : null;
119     }
120     
121     public Dimension JavaDoc getPreferredSize() {
122         Dimension JavaDoc prefSize = super.getPreferredSize();
123         Dimension JavaDoc labelSize = topLabel != null ? topLabel.getPreferredSize() : new Dimension JavaDoc(0, 0);
124         Dimension JavaDoc maxSize = getMaximumSize();
125         if (labelSize.width > prefSize.width) {
126             prefSize.width = labelSize.width;
127         }
128         if (prefSize.width > maxSize.width) {
129             prefSize.width = maxSize.width;
130         }
131         // Height is covered by maxVisibleRowCount value
132
return prefSize;
133     }
134     
135     private void setTitle(String JavaDoc title) {
136         if (title == null) {
137             if (topLabel != null) {
138                 setColumnHeader(null);
139                 topLabel = null;
140             }
141         } else {
142             if (topLabel != null) {
143                 topLabel.setText(title);
144             } else {
145                 topLabel = new JLabel JavaDoc(title);
146                 topLabel.setForeground(Color.blue);
147                 topLabel.setBorder(BorderFactory.createEmptyBorder(0, 2, 0, 2));
148                 setColumnHeaderView(topLabel);
149             }
150         }
151     }
152
153     /** Attempt to find the editor keystroke for the given editor action. */
154     private KeyStroke JavaDoc[] findEditorKeys(String JavaDoc editorActionName, KeyStroke JavaDoc defaultKey, JTextComponent JavaDoc component) {
155         // This method is implemented due to the issue
156
// #25715 - Attempt to search keymap for the keybinding that logically corresponds to the action
157
KeyStroke JavaDoc[] ret = new KeyStroke JavaDoc[] { defaultKey };
158         if (component != null) {
159             TextUI JavaDoc ui = component.getUI();
160             Keymap JavaDoc km = component.getKeymap();
161             if (ui != null && km != null) {
162                 EditorKit JavaDoc kit = ui.getEditorKit(component);
163                 if (kit instanceof BaseKit) {
164                     Action JavaDoc a = ((BaseKit)kit).getActionByName(editorActionName);
165                     if (a != null) {
166                         KeyStroke JavaDoc[] keys = km.getKeyStrokesForAction(a);
167                         if (keys != null && keys.length > 0) {
168                             ret = keys;
169                         }
170                     }
171                 }
172             }
173         }
174         return ret;
175     }
176
177     private void registerKeybinding(int action, String JavaDoc actionName, KeyStroke JavaDoc stroke, String JavaDoc editorActionName, JTextComponent JavaDoc component){
178         KeyStroke JavaDoc[] keys = findEditorKeys(editorActionName, stroke, component);
179         for (int i = 0; i < keys.length; i++) {
180             getInputMap().put(keys[i], actionName);
181         }
182         getActionMap().put(actionName, new CompletionPaneAction(action));
183     }
184
185     private void installKeybindings(JTextComponent JavaDoc component) {
186     // Register Escape key
187
registerKeybinding(ACTION_ESCAPE, ESCAPE,
188         KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
189         ExtKit.escapeAction, component);
190
191         // Register up key
192
registerKeybinding(ACTION_COMPLETION_UP, COMPLETION_UP,
193         KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0),
194         BaseKit.upAction, component);
195
196         // Register down key
197
registerKeybinding(ACTION_COMPLETION_DOWN, COMPLETION_DOWN,
198         KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0),
199         BaseKit.downAction, component);
200
201         // Register PgDn key
202
registerKeybinding(ACTION_COMPLETION_PGDN, COMPLETION_PGDN,
203         KeyStroke.getKeyStroke(KeyEvent.VK_PAGE_DOWN, 0),
204         BaseKit.pageDownAction, component);
205
206         // Register PgUp key
207
registerKeybinding(ACTION_COMPLETION_PGUP, COMPLETION_PGUP,
208         KeyStroke.getKeyStroke(KeyEvent.VK_PAGE_UP, 0),
209         BaseKit.pageUpAction, component);
210
211         // Register home key
212
registerKeybinding(ACTION_COMPLETION_BEGIN, COMPLETION_BEGIN,
213         KeyStroke.getKeyStroke(KeyEvent.VK_HOME, 0),
214         BaseKit.beginLineAction, component);
215
216         // Register end key
217
registerKeybinding(ACTION_COMPLETION_END, COMPLETION_END,
218         KeyStroke.getKeyStroke(KeyEvent.VK_END, 0),
219         BaseKit.endLineAction, component);
220     }
221
222     List JavaDoc testGetData() {
223         return dataObj;
224     }
225     
226     private class CompletionPaneAction extends AbstractAction JavaDoc {
227         private int action;
228
229         private CompletionPaneAction(int action) {
230             this.action = action;
231         }
232
233         public void actionPerformed(ActionEvent JavaDoc actionEvent) {
234             switch (action) {
235         case ACTION_ESCAPE:
236             CompletionImpl.get().hideCompletion(false);
237             break;
238                 case ACTION_COMPLETION_UP:
239                     view.up();
240                     break;
241                 case ACTION_COMPLETION_DOWN:
242                     view.down();
243                     break;
244                 case ACTION_COMPLETION_PGUP:
245                     view.pageUp();
246                     break;
247                 case ACTION_COMPLETION_PGDN:
248                         view.pageDown();
249                     break;
250                 case ACTION_COMPLETION_BEGIN:
251                         view.begin();
252                     break;
253                 case ACTION_COMPLETION_END:
254                         view.end();
255                     break;
256             }
257         }
258     }
259 }
260
Popular Tags