KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > editor > ext > 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.editor.ext;
21
22 import java.awt.Dimension JavaDoc;
23 import java.util.Collections JavaDoc;
24 import java.util.LinkedList JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.ResourceBundle JavaDoc;
27 import javax.swing.JList JavaDoc;
28 import javax.swing.ListCellRenderer JavaDoc;
29 import javax.swing.AbstractListModel JavaDoc;
30 import org.openide.util.NbBundle;
31
32
33 /**
34 * Code completion view component interface. It best fits the <tt>JList</tt>
35 * but some users may require something else e.g. JTable.
36 *
37 * @author Miloslav Metelka
38 * @version 1.00
39 */

40
41 public class ListCompletionView extends JList JavaDoc implements CompletionView {
42
43     ListCellRenderer JavaDoc renderer;
44     ListCellRenderer JavaDoc defaultRenderer;
45     
46     public ListCompletionView() {
47         this(null);
48     }
49
50     public ListCompletionView(ListCellRenderer JavaDoc renderer) {
51         setSelectionMode( javax.swing.ListSelectionModel.SINGLE_SELECTION );
52         this.renderer = renderer;
53         defaultRenderer = getCellRenderer();
54         /*
55         if (renderer != null) {
56             setCellRenderer(renderer);
57         }
58          */

59         ResourceBundle JavaDoc bundle = NbBundle.getBundle(org.netbeans.editor.BaseKit.class);
60         getAccessibleContext().setAccessibleName(bundle.getString("ACSN_CompletionView"));
61         getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_CompletionView"));
62         
63         
64     }
65
66     /** Populate the view with the result from a query. */
67     public void setResult(CompletionQuery.Result result) {
68         if (result != null) {
69             setResult(result.getData());
70         } else {
71             setResult(Collections.EMPTY_LIST);
72         }
73     }
74     
75     public void setResult(List JavaDoc data) {
76         if (data != null) {
77             if (data.size() == 0) {
78                 setCellRenderer(defaultRenderer);
79                 data = new LinkedList JavaDoc();
80 // data.add("<" + LocaleSupport.getString("no-matching-item-found") + ">"); // NOI18N
81
setModel(new Model JavaDoc(data));
82                 clearSelection();
83             } else {
84                 if (renderer != null) {
85                     setCellRenderer(renderer);
86                 }
87                 setModel(new Model JavaDoc(data));
88                 setSelectedIndex(0);
89             }
90         }
91     }
92
93     public void displayWaitStatus() {
94         if (getCellRenderer() == defaultRenderer) {
95             List JavaDoc data = new LinkedList JavaDoc();
96             data.add(NbBundle.getBundle(org.netbeans.editor.BaseKit.class).
97                     getString("please-wait")); // NOI18N
98
setModel(new Model JavaDoc(data));
99             clearSelection();
100         }
101     }
102     
103     public boolean showingData() {
104         return getCellRenderer() == renderer;
105     }
106     
107     /** Force the list to ignore the visible-row-count property */
108     public Dimension JavaDoc getPreferredScrollableViewportSize() {
109         return getPreferredSize();
110     }
111
112     public void up() {
113         if (getModel().getSize() > 0) {
114             setSelectedIndex(getSelectedIndex() - 1);
115             ensureIndexIsVisible(getSelectedIndex());
116         }
117     }
118
119     public void down() {
120         int lastInd = getModel().getSize() - 1;
121         if (lastInd >= 0) {
122             setSelectedIndex(Math.min(getSelectedIndex() + 1, lastInd));
123             ensureIndexIsVisible(getSelectedIndex());
124         }
125     }
126
127     public void pageUp() {
128         if (getModel().getSize() > 0) {
129             int pageSize = Math.max(getLastVisibleIndex() - getFirstVisibleIndex(), 0);
130             int ind = Math.max(getSelectedIndex() - pageSize, 0);
131
132             setSelectedIndex(ind);
133             ensureIndexIsVisible(ind);
134         }
135     }
136
137     public void pageDown() {
138         int lastInd = getModel().getSize() - 1;
139         if (lastInd >= 0) {
140             int pageSize = Math.max(getLastVisibleIndex() - getFirstVisibleIndex(), 0);
141             int ind = Math.min(getSelectedIndex() + pageSize, lastInd);
142
143             setSelectedIndex(ind);
144             ensureIndexIsVisible(ind);
145         }
146     }
147
148     public void begin() {
149         if (getModel().getSize() > 0) {
150             setSelectedIndex(0);
151             ensureIndexIsVisible(0);
152         }
153     }
154
155     public void end() {
156         int lastInd = getModel().getSize() - 1;
157         if (lastInd >= 0) {
158             setSelectedIndex(lastInd);
159             ensureIndexIsVisible(lastInd);
160         }
161     }
162
163     public void setVisible(boolean visible) {
164         // ??? never called
165
// System.err.println("ListCompletionView.setVisible(" + visible + ")");
166
super.setVisible(visible);
167     }
168     
169     static class Model extends AbstractListModel JavaDoc {
170
171         List JavaDoc data;
172
173         static final long serialVersionUID = 3292276783870598274L;
174
175         public Model(List JavaDoc data) {
176             this.data = data;
177         }
178
179         public int getSize() {
180             return data.size();
181         }
182
183         public Object JavaDoc getElementAt(int index) {
184             return (index >= 0 && index < data.size()) ? data.get(index) : null;
185         }
186
187         List JavaDoc getData() {
188             return data;
189         }
190
191     }
192
193 }
194
Popular Tags