001: /*
002: * @(#)AbstractListIntelliHints.java 7/24/2005
003: *
004: * Copyright 2002 - 2005 JIDE Software Inc. All rights reserved.
005: */
006: package com.jidesoft.hints;
007:
008: import com.jidesoft.swing.JideScrollPane;
009: import com.jidesoft.swing.Sticky;
010:
011: import javax.swing.*;
012: import javax.swing.text.JTextComponent;
013: import java.awt.*;
014: import java.awt.event.KeyEvent;
015: import java.util.Vector;
016:
017: /**
018: * <code>AbstractListIntelliHints</code> extends AbstractIntelliHints and further
019: * implement most of the methods in interface {@link com.jidesoft.hints.IntelliHints}. In this class, it assumes the
020: * hints can be represented as a JList, so it used JList in the hints popup.
021: *
022: * @author Santhosh Kumar T - santhosh@in.fiorano.com
023: * @author JIDE Software, Inc.
024: */
025: public abstract class AbstractListIntelliHints extends
026: AbstractIntelliHints {
027: private JList _list;
028: protected KeyStroke[] _keyStrokes;
029: private JideScrollPane _scroll;
030:
031: /**
032: * Creates a Completion for JTextComponent
033: *
034: * @param textComponent
035: */
036: public AbstractListIntelliHints(JTextComponent textComponent) {
037: super (textComponent);
038: }
039:
040: public JComponent createHintsComponent() {
041: JPanel panel = new JPanel(new BorderLayout());
042:
043: _list = createList();
044: new Sticky(_list);
045: _scroll = new JideScrollPane(getList());
046: getList().setFocusable(false);
047:
048: _scroll
049: .setHorizontalScrollBarPolicy(JideScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
050: _scroll.setBorder(BorderFactory.createEmptyBorder());
051: _scroll.getVerticalScrollBar().setFocusable(false);
052: _scroll.getHorizontalScrollBar().setFocusable(false);
053:
054: panel.add(_scroll, BorderLayout.CENTER);
055: return panel;
056: }
057:
058: /**
059: * Creates the list to display the hints. By default, we create a JList using the code below.
060: * <code><pre>
061: * return new JList() {
062: * public int getVisibleRowCount() {
063: * int size = getModel().getSize();
064: * return size < super.getVisibleRowCount() ? size : super.getVisibleRowCount();
065: * }
066: * <p/>
067: * public Dimension getPreferredScrollableViewportSize() {
068: * if (getModel().getSize() == 0) {
069: * return new Dimension(0, 0);
070: * }
071: * else {
072: * return super.getPreferredScrollableViewportSize();
073: * }
074: * }
075: * };
076: * </pre></code>
077: *
078: * @return the list.
079: */
080: protected JList createList() {
081: return new JList() {
082: @Override
083: public int getVisibleRowCount() {
084: int size = getModel().getSize();
085: return size < super .getVisibleRowCount() ? size : super
086: .getVisibleRowCount();
087: }
088:
089: @Override
090: public Dimension getPreferredScrollableViewportSize() {
091: if (getModel().getSize() == 0) {
092: return new Dimension(0, 0);
093: } else {
094: return super .getPreferredScrollableViewportSize();
095: }
096: }
097: };
098: }
099:
100: /**
101: * Gets the list.
102: *
103: * @return the list.
104: */
105: protected JList getList() {
106: return _list;
107: }
108:
109: /**
110: * Sets the list data.
111: *
112: * @param objects
113: */
114: protected void setListData(Object[] objects) {
115: resetSelection();
116: getList().setListData(objects);
117:
118: // update the view so that isViewSizeSet flag in JViewport is reset to false
119: if (_scroll != null) {
120: _scroll.setViewportView(getList());
121: }
122:
123: }
124:
125: /**
126: * Sets the list data.
127: *
128: * @param objects
129: */
130: protected void setListData(Vector<?> objects) {
131: resetSelection();
132: getList().setListData(objects);
133: // update the view so that isViewSizeSet flag in JViewport is reset to false
134: if (_scroll != null) {
135: _scroll.setViewportView(getList());
136: }
137: }
138:
139: private void resetSelection() {
140: getList().getSelectionModel().setAnchorSelectionIndex(-1); // has to call setAnchor first
141: getList().getSelectionModel().setLeadSelectionIndex(-1);
142: getList().getSelectionModel().clearSelection();
143: }
144:
145: public Object getSelectedHint() {
146: return getList().getSelectedValue();
147: }
148:
149: @Override
150: public JComponent getDelegateComponent() {
151: return getList();
152: }
153:
154: /**
155: * Gets the delegate keystrokes. Since we know the hints popup is a JList, we return eight
156: * keystrokes so that they can be delegate to the JList. Those keystrokes are
157: * DOWN, UP, PAGE_DOWN, PAGE_UP, HOME and END.
158: *
159: * @return the keystokes that will be delegated to the JList when hints popup is visible.
160: */
161: @Override
162: public KeyStroke[] getDelegateKeyStrokes() {
163: if (_keyStrokes == null) {
164: _keyStrokes = new KeyStroke[6];
165: _keyStrokes[0] = KeyStroke
166: .getKeyStroke(KeyEvent.VK_DOWN, 0);
167: _keyStrokes[1] = KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0);
168: _keyStrokes[2] = KeyStroke.getKeyStroke(
169: KeyEvent.VK_PAGE_DOWN, 0);
170: _keyStrokes[3] = KeyStroke.getKeyStroke(
171: KeyEvent.VK_PAGE_UP, 0);
172: _keyStrokes[4] = KeyStroke
173: .getKeyStroke(KeyEvent.VK_HOME, 0);
174: _keyStrokes[5] = KeyStroke.getKeyStroke(KeyEvent.VK_END, 0);
175: }
176: return _keyStrokes;
177: }
178: }
|