KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.awt.Component JavaDoc;
23 import java.awt.Dimension JavaDoc;
24 import java.awt.Font JavaDoc;
25 import java.beans.PropertyChangeEvent JavaDoc;
26 import java.beans.PropertyChangeListener JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Collection JavaDoc;
29 import java.util.LinkedHashSet JavaDoc;
30 import java.util.List JavaDoc;
31 import javax.swing.AbstractListModel JavaDoc;
32 import javax.swing.BorderFactory JavaDoc;
33 import javax.swing.Icon JavaDoc;
34 import javax.swing.ImageIcon JavaDoc;
35 import javax.swing.JList JavaDoc;
36 import javax.swing.ListCellRenderer JavaDoc;
37 import javax.swing.SwingUtilities JavaDoc;
38 import org.netbeans.editor.LocaleSupport;
39 import org.netbeans.modules.editor.hints.HintsUI;
40 import org.netbeans.spi.editor.hints.Fix;
41 import org.netbeans.spi.editor.hints.LazyFixList;
42 import org.openide.awt.HtmlRenderer;
43
44 /**
45 * @author Miloslav Metelka, Dusan Balek
46 * @version 1.00
47 */

48
49 public class ListCompletionView extends JList JavaDoc implements ListCellRenderer JavaDoc {
50
51     private final HtmlRenderer.Renderer defaultRenderer = HtmlRenderer.createRenderer();
52     private Font JavaDoc font;
53     private Icon JavaDoc icon = new ImageIcon JavaDoc (org.openide.util.Utilities.loadImage("org/netbeans/modules/editor/hints/resources/suggestion.gif")); // NOI18N
54

55     public ListCompletionView() {
56         setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
57         font = getFont();
58         if (font.getSize() < 15 ) {
59             font = font.deriveFont(font.getSize2D() + 1);
60         }
61         
62         setFont( font );
63         setCellRenderer( this );
64         setBorder( BorderFactory.createEmptyBorder() );
65         getAccessibleContext().setAccessibleName(LocaleSupport.getString("ACSN_CompletionView"));
66         getAccessibleContext().setAccessibleDescription(LocaleSupport.getString("ACSD_CompletionView"));
67     }
68
69     public Component JavaDoc getListCellRendererComponent(JList JavaDoc list, Object JavaDoc value, int index, boolean isSelected, boolean cellHasFocus) {
70         Component JavaDoc c = defaultRenderer.getListCellRendererComponent( list, value instanceof Fix ? ((Fix) value).getText() : value.toString(), index, isSelected, cellHasFocus );
71         defaultRenderer.setIcon( icon );
72         defaultRenderer.setParentFocused(true);
73         defaultRenderer.setRenderStyle(HtmlRenderer.STYLE_CLIP);
74         c.setBackground(list.getBackground());
75         return c;
76     }
77     
78     public void setResult(LazyFixList data) {
79         if (data != null) {
80             Model JavaDoc model = new Model JavaDoc(data);
81             
82             setModel(model);
83             if (model.fixes != null && !model.fixes.isEmpty()) {
84                 setSelectedIndex(0);
85             }
86         }
87     }
88
89     /** Force the list to ignore the visible-row-count property */
90     public Dimension JavaDoc getPreferredScrollableViewportSize() {
91         return getPreferredSize();
92     }
93
94     public void up() {
95         if (getModel().getSize() > 0) {
96             setSelectedIndex(Math.max(getSelectedIndex() - 1, 0));
97             ensureIndexIsVisible(getSelectedIndex());
98             repaint();
99         }
100     }
101
102     public void down() {
103         int lastInd = getModel().getSize() - 1;
104         if (lastInd >= 0) {
105             setSelectedIndex(Math.min(getSelectedIndex() + 1, lastInd));
106             ensureIndexIsVisible(getSelectedIndex());
107             validate();
108         }
109     }
110
111     public void pageUp() {
112         if (getModel().getSize() > 0) {
113             int pageSize = Math.max(getLastVisibleIndex() - getFirstVisibleIndex(), 0);
114             int ind = Math.max(getSelectedIndex() - pageSize, 0);
115
116             setSelectedIndex(ind);
117             ensureIndexIsVisible(ind);
118         }
119     }
120
121     public void pageDown() {
122         int lastInd = getModel().getSize() - 1;
123         if (lastInd >= 0) {
124             int pageSize = Math.max(getLastVisibleIndex() - getFirstVisibleIndex(), 0);
125             int ind = Math.min(getSelectedIndex() + pageSize, lastInd);
126
127             setSelectedIndex(ind);
128             ensureIndexIsVisible(ind);
129         }
130     }
131
132     public void begin() {
133         if (getModel().getSize() > 0) {
134             setSelectedIndex(0);
135             ensureIndexIsVisible(0);
136         }
137     }
138
139     public void end() {
140         int lastInd = getModel().getSize() - 1;
141         if (lastInd >= 0) {
142             setSelectedIndex(lastInd);
143             ensureIndexIsVisible(lastInd);
144         }
145     }
146
147     static class Model extends AbstractListModel JavaDoc implements PropertyChangeListener JavaDoc {
148
149         private LazyFixList data;
150         private List JavaDoc<Fix> fixes;
151         private boolean computed;
152         
153
154         static final long serialVersionUID = 3292276783870598274L;
155
156         public Model(LazyFixList data) {
157             this.data = data;
158             data.addPropertyChangeListener(this);
159             update();
160         }
161
162         private synchronized void update() {
163             computed = data.isComputed();
164             if (computed)
165                 fixes = new ArrayList JavaDoc<Fix>(new LinkedHashSet JavaDoc<Fix>(data.getFixes()));
166             else
167                 data.getFixes();
168         }
169         
170         public synchronized int getSize() {
171             return computed ? fixes.size() : 1;
172         }
173
174         public synchronized Object JavaDoc getElementAt(int index) {
175             if (!computed) {
176                 return "computing...";
177             } else {
178                 return fixes.get(index);
179             }
180         }
181
182         public void propertyChange(PropertyChangeEvent JavaDoc evt) {
183 // update();
184
SwingUtilities.invokeLater(new Runnable JavaDoc() {
185                 public void run() {
186                     HintsUI.getDefault().removePopups();
187                     HintsUI.getDefault().showPopup();
188                 }
189             });
190         }
191
192     }
193
194 }
195
Popular Tags