KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > editor > ext > 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.editor.ext;
21
22
23 import java.awt.Color JavaDoc;
24 import java.awt.Component JavaDoc;
25 import java.awt.Dimension JavaDoc;
26 import java.awt.event.ActionEvent JavaDoc;
27 import java.awt.event.ActionListener JavaDoc;
28 import java.awt.event.KeyEvent JavaDoc;
29 import java.awt.event.MouseAdapter JavaDoc;
30 import java.awt.event.MouseListener JavaDoc;
31 import java.awt.event.MouseEvent JavaDoc;
32 import java.beans.PropertyChangeListener JavaDoc;
33 import java.beans.PropertyChangeEvent JavaDoc;
34 import java.util.Iterator JavaDoc;
35 import java.util.Map JavaDoc;
36
37 import javax.swing.Action JavaDoc;
38 import javax.swing.ActionMap JavaDoc;
39 import javax.swing.BorderFactory JavaDoc;
40 import javax.swing.InputMap JavaDoc;
41 import javax.swing.JComponent JavaDoc;
42 import javax.swing.JScrollPane JavaDoc;
43 import javax.swing.JLabel JavaDoc;
44 import javax.swing.JList JavaDoc;
45 import javax.swing.KeyStroke JavaDoc;
46 import javax.swing.SwingUtilities JavaDoc;
47 import javax.swing.text.JTextComponent JavaDoc;
48
49 import org.netbeans.editor.BaseKit;
50 import org.netbeans.editor.Settings;
51 import org.netbeans.editor.SettingsUtil;
52 import org.netbeans.editor.SettingsChangeListener;
53 import org.netbeans.editor.SettingsChangeEvent;
54 import org.netbeans.editor.Utilities;
55
56
57 /**
58 * Pane displaying the completion view and accompanying components
59 * like label for title etc.
60 *
61 * @author Miloslav Metelka, Martin Roskanin
62 * @version 1.00
63 */

64
65 public class ScrollCompletionPane extends JScrollPane JavaDoc implements ExtCompletionPane,
66     PropertyChangeListener JavaDoc, SettingsChangeListener {
67
68     private ExtEditorUI extEditorUI;
69
70     private JComponent JavaDoc view;
71
72     private JLabel JavaDoc topLabel;
73
74     private Dimension JavaDoc minSize;
75
76     private Dimension JavaDoc maxSize;
77
78     private ViewMouseListener viewMouseL;
79
80     private Dimension JavaDoc scrollBarSize;
81     
82     private Dimension JavaDoc minSizeDefault;
83
84     public ScrollCompletionPane(ExtEditorUI extEditorUI) {
85         this.extEditorUI = extEditorUI;
86
87         // Compute size of the scrollbars
88
Dimension JavaDoc smallSize = getPreferredSize();
89         setHorizontalScrollBarPolicy(HORIZONTAL_SCROLLBAR_ALWAYS);
90         setVerticalScrollBarPolicy(VERTICAL_SCROLLBAR_ALWAYS);
91         scrollBarSize = getPreferredSize();
92         scrollBarSize.width -= smallSize.width;
93         scrollBarSize.height -= smallSize.height;
94         setHorizontalScrollBarPolicy(HORIZONTAL_SCROLLBAR_AS_NEEDED);
95         setVerticalScrollBarPolicy(VERTICAL_SCROLLBAR_AS_NEEDED);
96
97         // Make it invisible initially
98
super.setVisible(false);
99
100         // Add the title component
101
installTitleComponent();
102
103         // Add the completion view
104
CompletionView completionView = extEditorUI.getCompletion().getView();
105         if (completionView instanceof JComponent JavaDoc) {
106             view = (JComponent JavaDoc)completionView;
107             setViewportView(view);
108         }
109
110         // Prevent the bug with displaying without the scrollbar
111
getViewport().setMinimumSize(new Dimension JavaDoc(4,4));
112
113         Settings.addSettingsChangeListener(this);
114
115         viewMouseL = new ViewMouseListener();
116         synchronized (extEditorUI.getComponentLock()) {
117             // if component already installed in ExtEditorUI simulate installation
118
JTextComponent JavaDoc component = extEditorUI.getComponent();
119             if (component != null) {
120                 propertyChange(new PropertyChangeEvent JavaDoc(extEditorUI,
121                                                        ExtEditorUI.COMPONENT_PROPERTY, null, component));
122             }
123
124             extEditorUI.addPropertyChangeListener(this);
125         }
126         
127         putClientProperty ("HelpID", ScrollCompletionPane.class.getName ()); // !!! NOI18N
128
}
129     
130     
131     public void settingsChange(SettingsChangeEvent evt) {
132         Class JavaDoc kitClass = Utilities.getKitClass(extEditorUI.getComponent());
133
134         if (kitClass != null) {
135             minSize = (Dimension JavaDoc)SettingsUtil.getValue(kitClass,
136                       ExtSettingsNames.COMPLETION_PANE_MIN_SIZE,
137                       ExtSettingsDefaults.defaultCompletionPaneMinSize);
138             minSizeDefault = new Dimension JavaDoc(minSize);
139             setMinimumSize(minSize);
140             
141             maxSize = (Dimension JavaDoc)SettingsUtil.getValue(kitClass,
142                       ExtSettingsNames.COMPLETION_PANE_MAX_SIZE,
143                       ExtSettingsDefaults.defaultCompletionPaneMaxSize);
144             setMaximumSize(maxSize);
145             
146         }
147     }
148
149     
150     public void propertyChange(PropertyChangeEvent JavaDoc evt) {
151         String JavaDoc propName = evt.getPropertyName();
152
153         if (ExtEditorUI.COMPONENT_PROPERTY.equals(propName)) {
154             if (evt.getNewValue() != null) { // just installed
155

156                 settingsChange(null);
157
158                 if (view != null) {
159                     // Add mouse listener
160
view.addMouseListener(viewMouseL);
161                 }
162
163                 
164             } else { // just deinstalled
165

166                 if (view != null) {
167                      // Unregister Escape key
168
view.removeMouseListener(viewMouseL);
169                 }
170             }
171         }
172     }
173     
174     public void setVisible(boolean visible){
175         //new RuntimeException("ScrollCompletionPane.setVisible(" + visible + ")").printStackTrace();
176
if (view instanceof JList JavaDoc) {
177             JList JavaDoc listView = (JList JavaDoc)view;
178             listView.ensureIndexIsVisible(listView.getSelectedIndex());
179         }
180         
181         super.setVisible(visible);
182     }
183     
184     public void refresh() {
185         if (view instanceof JList JavaDoc) {
186             JList JavaDoc listView = (JList JavaDoc)view;
187             listView.ensureIndexIsVisible(listView.getSelectedIndex());
188         }
189         
190         SwingUtilities.invokeLater( // !!! ? is it needed
191
new Runnable JavaDoc() {
192                 public void run() {
193                     if (isShowing()) { // #18810
194
// extEditorUI.getPopupManager().reset(extEditorUI.getComponent());
195
revalidate();
196                     }
197                 }
198             }
199         );
200     }
201
202     /** Set the title of the pane according to the completion query results. */
203     public void setTitle(String JavaDoc title) {
204         topLabel.setText(title);
205     }
206
207     
208     protected void installTitleComponent() {
209         topLabel = new JLabel JavaDoc();
210         topLabel.setForeground(Color.blue);
211         topLabel.setBorder(BorderFactory.createEmptyBorder(0, 2, 0, 2));
212         setColumnHeaderView(topLabel);
213     }
214
215     protected Dimension JavaDoc getTitleComponentPreferredSize() {
216         return topLabel.getPreferredSize();
217     }
218     
219     public void setSize(int width, int height){
220         int maxWidth = width;
221         int maxHeight = height;
222
223         minSize.width = minSizeDefault.width;
224         minSize.height = minSizeDefault.height;
225         setMinimumSize(minSize);
226         
227         Dimension JavaDoc ps = getPreferredSize();
228
229         /* Add size of the vertical scrollbar by default. This could be improved
230         * to be done only if the height exceeds the bounds. */

231         ps.width += scrollBarSize.width;
232         ps.width = Math.max(Math.max(ps.width, minSize.width),
233                             getTitleComponentPreferredSize().width);
234
235         maxWidth = Math.min(maxWidth, maxSize.width);
236         maxHeight = Math.min(maxHeight, maxSize.height);
237         boolean displayHorizontalScrollbar = (ps.width-scrollBarSize.width)>maxWidth;
238
239         if (ps.width > maxWidth) {
240             ps.width = maxWidth;
241             if (displayHorizontalScrollbar){
242                 ps.height += scrollBarSize.height; // will show horizontal scrollbar
243
minSize.height += scrollBarSize.height;
244                 setMinimumSize(minSize);
245             }
246             
247         }
248
249         ps.height = Math.min(Math.max(ps.height, minSize.height), maxHeight);
250         super.setSize(ps.width, ps.height);
251     }
252     
253     public void setSize(Dimension JavaDoc d){
254         setSize(d.width, d.height);
255     }
256     
257     public JComponent JavaDoc getComponent() {
258         return this;
259     }
260     
261     class ViewMouseListener extends MouseAdapter JavaDoc {
262
263         public void mouseClicked(MouseEvent JavaDoc evt) {
264             if (SwingUtilities.isLeftMouseButton(evt)) {
265                 JTextComponent JavaDoc component = extEditorUI.getComponent();
266                 if( component != null && evt.getClickCount() == 2 ) {
267                     JDCPopupPanel jdc = ExtUtilities.getJDCPopupPanel(component);
268                     if (jdc != null) {
269                         Action JavaDoc a = jdc.getActionMap().get(JDCPopupPanel.COMPLETION_SUBSTITUTE_TEXT);
270                         if (a != null) {
271                             a.actionPerformed(new ActionEvent JavaDoc(component, ActionEvent.ACTION_PERFORMED, "")); // NOI18N
272
}
273                     }
274                 }
275             }
276         }
277     }
278
279 }
280
Popular Tags