001: /*
002: * Sun Public License Notice
003: *
004: * The contents of this file are subject to the Sun Public License
005: * Version 1.0 (the "License"). You may not use this file except in
006: * compliance with the License. A copy of the License is available at
007: * http://www.sun.com/
008: *
009: * The Original Code is NetBeans. The Initial Developer of the Original
010: * Code is Sun Microsystems, Inc. Portions Copyright 1997-2000 Sun
011: * Microsystems, Inc. All Rights Reserved.
012: */
013:
014: package org.netbeans.editor.ext;
015:
016: import java.awt.Dimension;
017: import java.util.List;
018:
019: import javax.swing.AbstractListModel;
020: import javax.swing.JList;
021: import javax.swing.ListCellRenderer;
022:
023: import org.netbeans.editor.LocaleSupport;
024:
025: /**
026: * Code completion view component interface. It best fits the <tt>JList</tt>
027: * but some users may require something else e.g. JTable.
028: *
029: * @author Miloslav Metelka
030: * @version 1.00
031: */
032:
033: public class ListCompletionView extends JList implements CompletionView {
034:
035: public ListCompletionView() {
036: this (null);
037: }
038:
039: public ListCompletionView(ListCellRenderer renderer) {
040: setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
041: if (renderer != null) {
042: setCellRenderer(renderer);
043: }
044: getAccessibleContext().setAccessibleName(
045: LocaleSupport.getString("ACSN_CompletionView"));
046: getAccessibleContext().setAccessibleDescription(
047: LocaleSupport.getString("ACSD_CompletionView"));
048: }
049:
050: /** Populate the view with the result from a query. */
051: public void setResult(CompletionQuery.Result result) {
052: if (result != null) {
053: setResult(result.getData());
054: }
055: }
056:
057: public void setResult(List data) {
058: if (data != null) {
059: setModel(new Model(data));
060: if (data.size() > 0) {
061: setSelectedIndex(0);
062: }
063: }
064: }
065:
066: /** Force the list to ignore the visible-row-count property */
067: public Dimension getPreferredScrollableViewportSize() {
068: return getPreferredSize();
069: }
070:
071: public void up() {
072: if (getModel().getSize() > 0) {
073: setSelectedIndex(getSelectedIndex() - 1);
074: ensureIndexIsVisible(getSelectedIndex());
075: }
076: }
077:
078: public void down() {
079: int lastInd = getModel().getSize() - 1;
080: if (lastInd >= 0) {
081: setSelectedIndex(Math.min(getSelectedIndex() + 1, lastInd));
082: ensureIndexIsVisible(getSelectedIndex());
083: }
084: }
085:
086: public void pageUp() {
087: if (getModel().getSize() > 0) {
088: int pageSize = Math.max(getLastVisibleIndex()
089: - getFirstVisibleIndex(), 0);
090: int firstInd = Math.max(getFirstVisibleIndex() - pageSize,
091: 0);
092: int ind = Math.max(getSelectedIndex() - pageSize, firstInd);
093:
094: ensureIndexIsVisible(firstInd);
095: setSelectedIndex(ind);
096: ensureIndexIsVisible(ind);
097: }
098: }
099:
100: public void pageDown() {
101: int lastInd = getModel().getSize() - 1;
102: if (lastInd >= 0) {
103: int pageSize = Math.max(getLastVisibleIndex()
104: - getFirstVisibleIndex(), 0);
105: lastInd = Math.max(Math.min(getLastVisibleIndex()
106: + pageSize, lastInd), 0);
107: int ind = Math.max(Math.min(getSelectedIndex() + pageSize,
108: lastInd), 0);
109:
110: ensureIndexIsVisible(lastInd);
111: setSelectedIndex(ind);
112: ensureIndexIsVisible(ind);
113: }
114: }
115:
116: public void begin() {
117: if (getModel().getSize() > 0) {
118: setSelectedIndex(0);
119: ensureIndexIsVisible(0);
120: }
121: }
122:
123: public void end() {
124: int lastInd = getModel().getSize() - 1;
125: if (lastInd >= 0) {
126: setSelectedIndex(lastInd);
127: ensureIndexIsVisible(lastInd);
128: }
129: }
130:
131: static class Model extends AbstractListModel {
132:
133: List data;
134:
135: static final long serialVersionUID = 3292276783870598274L;
136:
137: public Model(List data) {
138: this .data = data;
139: }
140:
141: public int getSize() {
142: return data.size();
143: }
144:
145: public Object getElementAt(int index) {
146: return (index >= 0 && index < data.size()) ? data
147: .get(index) : null;
148: }
149:
150: List getData() {
151: return data;
152: }
153:
154: }
155:
156: }
|