001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.editor.ext;
043:
044: import java.awt.Dimension;
045: import java.util.Collections;
046: import java.util.LinkedList;
047: import java.util.List;
048: import java.util.ResourceBundle;
049: import javax.swing.JList;
050: import javax.swing.ListCellRenderer;
051: import javax.swing.AbstractListModel;
052: import org.openide.util.NbBundle;
053:
054: /**
055: * Code completion view component interface. It best fits the <tt>JList</tt>
056: * but some users may require something else e.g. JTable.
057: *
058: * @author Miloslav Metelka
059: * @version 1.00
060: */
061:
062: public class ListCompletionView extends JList implements CompletionView {
063:
064: ListCellRenderer renderer;
065: ListCellRenderer defaultRenderer;
066:
067: public ListCompletionView() {
068: this (null);
069: }
070:
071: public ListCompletionView(ListCellRenderer renderer) {
072: setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
073: this .renderer = renderer;
074: defaultRenderer = getCellRenderer();
075: /*
076: if (renderer != null) {
077: setCellRenderer(renderer);
078: }
079: */
080: ResourceBundle bundle = NbBundle
081: .getBundle(org.netbeans.editor.BaseKit.class);
082: getAccessibleContext().setAccessibleName(
083: bundle.getString("ACSN_CompletionView"));
084: getAccessibleContext().setAccessibleDescription(
085: bundle.getString("ACSD_CompletionView"));
086:
087: }
088:
089: /** Populate the view with the result from a query. */
090: public void setResult(CompletionQuery.Result result) {
091: if (result != null) {
092: setResult(result.getData());
093: } else {
094: setResult(Collections.EMPTY_LIST);
095: }
096: }
097:
098: public void setResult(List data) {
099: if (data != null) {
100: if (data.size() == 0) {
101: setCellRenderer(defaultRenderer);
102: data = new LinkedList();
103: // data.add("<" + LocaleSupport.getString("no-matching-item-found") + ">"); // NOI18N
104: setModel(new Model(data));
105: clearSelection();
106: } else {
107: if (renderer != null) {
108: setCellRenderer(renderer);
109: }
110: setModel(new Model(data));
111: setSelectedIndex(0);
112: }
113: }
114: }
115:
116: public void displayWaitStatus() {
117: if (getCellRenderer() == defaultRenderer) {
118: List data = new LinkedList();
119: data.add(NbBundle.getBundle(
120: org.netbeans.editor.BaseKit.class).getString(
121: "please-wait")); // NOI18N
122: setModel(new Model(data));
123: clearSelection();
124: }
125: }
126:
127: public boolean showingData() {
128: return getCellRenderer() == renderer;
129: }
130:
131: /** Force the list to ignore the visible-row-count property */
132: public Dimension getPreferredScrollableViewportSize() {
133: return getPreferredSize();
134: }
135:
136: public void up() {
137: if (getModel().getSize() > 0) {
138: setSelectedIndex(getSelectedIndex() - 1);
139: ensureIndexIsVisible(getSelectedIndex());
140: }
141: }
142:
143: public void down() {
144: int lastInd = getModel().getSize() - 1;
145: if (lastInd >= 0) {
146: setSelectedIndex(Math.min(getSelectedIndex() + 1, lastInd));
147: ensureIndexIsVisible(getSelectedIndex());
148: }
149: }
150:
151: public void pageUp() {
152: if (getModel().getSize() > 0) {
153: int pageSize = Math.max(getLastVisibleIndex()
154: - getFirstVisibleIndex(), 0);
155: int ind = Math.max(getSelectedIndex() - pageSize, 0);
156:
157: setSelectedIndex(ind);
158: ensureIndexIsVisible(ind);
159: }
160: }
161:
162: public void pageDown() {
163: int lastInd = getModel().getSize() - 1;
164: if (lastInd >= 0) {
165: int pageSize = Math.max(getLastVisibleIndex()
166: - getFirstVisibleIndex(), 0);
167: int ind = Math.min(getSelectedIndex() + pageSize, lastInd);
168:
169: setSelectedIndex(ind);
170: ensureIndexIsVisible(ind);
171: }
172: }
173:
174: public void begin() {
175: if (getModel().getSize() > 0) {
176: setSelectedIndex(0);
177: ensureIndexIsVisible(0);
178: }
179: }
180:
181: public void end() {
182: int lastInd = getModel().getSize() - 1;
183: if (lastInd >= 0) {
184: setSelectedIndex(lastInd);
185: ensureIndexIsVisible(lastInd);
186: }
187: }
188:
189: public void setVisible(boolean visible) {
190: // ??? never called
191: // System.err.println("ListCompletionView.setVisible(" + visible + ")");
192: super .setVisible(visible);
193: }
194:
195: static class Model extends AbstractListModel {
196:
197: List data;
198:
199: static final long serialVersionUID = 3292276783870598274L;
200:
201: public Model(List data) {
202: this .data = data;
203: }
204:
205: public int getSize() {
206: return data.size();
207: }
208:
209: public Object getElementAt(int index) {
210: return (index >= 0 && index < data.size()) ? data
211: .get(index) : null;
212: }
213:
214: List getData() {
215: return data;
216: }
217:
218: }
219:
220: }
|