Debian Codegraph @

src/com/jidesoft/hints/AbstractListIntelliHints.java

raw
1 /*
2 * @(#)AbstractListIntelliHints.java 7/24/2005
3 *
4 * Copyright 2002 - 2005 JIDE Software Inc. All rights reserved.
5 */
6 package com.jidesoft.hints;
7
8 import com.jidesoft.swing.JideScrollPane;
9 import com.jidesoft.swing.Sticky;
10
11 import javax.swing.*;
12 import javax.swing.text.JTextComponent;
13 import java.awt.*;
14 import java.awt.event.KeyEvent;
15 import java.util.Vector;
16
17
18 /**
19 * <code>AbstractListIntelliHints</code> extends AbstractIntelliHints and further implement most of the methods in
20 * interface {@link com.jidesoft.hints.IntelliHints}. In this class, it assumes the hints can be represented as a JList,
21 * so it used JList in the hints popup.
22 *
23 * @author Santhosh Kumar T - [email protected]
24 * @author JIDE Software, Inc.
25 */
26 public abstract class AbstractListIntelliHints extends AbstractIntelliHints {
27 private JList _list;
28 protected KeyStroke[] _keyStrokes;
29 private JideScrollPane _scroll;
30
31 /**
32 * Creates a Completion for JTextComponent
33 *
34 * @param textComponent
35 */
36 public AbstractListIntelliHints(JTextComponent textComponent) {
37 super(textComponent);
38 }
39
40 public JComponent createHintsComponent() {
41 JPanel panel = new JPanel(new BorderLayout());
42
43 _list = createList();
44 new Sticky(_list);
45 _scroll = new JideScrollPane(getList());
46 getList().setFocusable(false);
47
48 _scroll.setHorizontalScrollBarPolicy(JideScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
49 _scroll.setBorder(BorderFactory.createEmptyBorder());
50 _scroll.getVerticalScrollBar().setFocusable(false);
51 _scroll.getHorizontalScrollBar().setFocusable(false);
52
53 panel.add(_scroll, BorderLayout.CENTER);
54 return panel;
55 }
56
57 /**
58 * Creates the list to display the hints. By default, we create a JList using the code below.
59 * <code><pre>
60 * return new JList() {
61 * public int getVisibleRowCount() {
62 * int size = getModel().getSize();
63 * return size < super.getVisibleRowCount() ? size : super.getVisibleRowCount();
64 * }
65 * <p/>
66 * public Dimension getPreferredScrollableViewportSize() {
67 * if (getModel().getSize() == 0) {
68 * return new Dimension(0, 0);
69 * }
70 * else {
71 * return super.getPreferredScrollableViewportSize();
72 * }
73 * }
74 * };
75 * </pre></code>
76 *
77 * @return the list.
78 */
79 protected JList createList() {
80 return new JList() {
81 @Override
82 public int getVisibleRowCount() {
83 int size = getModel().getSize();
84 return size < super.getVisibleRowCount() ? size : super.getVisibleRowCount();
85 }
86
87 @Override
88 public Dimension getPreferredScrollableViewportSize() {
89 if (getModel().getSize() == 0) {
90 return new Dimension(0, 0);
91 }
92 else {
93 return super.getPreferredScrollableViewportSize();
94 }
95 }
96 };
97 }
98
99 /**
100 * Gets the list.
101 *
102 * @return the list.
103 */
104 protected JList getList() {
105 return _list;
106 }
107
108 /**
109 * Sets the list data.
110 *
111 * @param objects
112 */
113 protected void setListData(Object[] objects) {
114 if (getList() == null) {
115 return;
116 }
117 resetSelection();
118 getList().setListData(objects);
119
120 // update the view so that isViewSizeSet flag in JViewport is reset to false
121 if (_scroll != null) {
122 _scroll.setViewportView(getList());
123 }
124
125 }
126
127 /**
128 * Sets the list data.
129 *
130 * @param objects
131 */
132 protected void setListData(Vector<?> objects) {
133 if (getList() == null) {
134 return;
135 }
136 resetSelection();
137 getList().setListData(objects);
138 // update the view so that isViewSizeSet flag in JViewport is reset to false
139 if (_scroll != null) {
140 _scroll.setViewportView(getList());
141 }
142 }
143
144 private void resetSelection() {
145 if (getList() == null) {
146 return;
147 }
148 getList().getSelectionModel().setAnchorSelectionIndex(-1); // has to call setAnchor first
149 getList().getSelectionModel().setLeadSelectionIndex(-1);
150 getList().getSelectionModel().clearSelection();
151 }
152
153 public Object getSelectedHint() {
154 return getList() == null ? null : getList().getSelectedValue();
155 }
156
157 @Override
158 public JComponent getDelegateComponent() {
159 return getList();
160 }
161
162 /**
163 * Gets the delegate keystrokes. Since we know the hints popup is a JList, we return eight keystrokes so that they
164 * can be delegate to the JList. Those keystrokes are DOWN, UP, PAGE_DOWN, PAGE_UP, HOME and END.
165 *
166 * @return the keystrokes that will be delegated to the JList when hints popup is visible.
167 */
168 @Override
169 public KeyStroke[] getDelegateKeyStrokes() {
170 if (_keyStrokes == null) {
171 _keyStrokes = new KeyStroke[8];
172 _keyStrokes[0] = KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0);
173 _keyStrokes[1] = KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0);
174 _keyStrokes[2] = KeyStroke.getKeyStroke(KeyEvent.VK_PAGE_DOWN, 0);
175 _keyStrokes[3] = KeyStroke.getKeyStroke(KeyEvent.VK_PAGE_UP, 0);
176 _keyStrokes[4] = KeyStroke.getKeyStroke(KeyEvent.VK_PAGE_DOWN, KeyEvent.CTRL_DOWN_MASK);
177 _keyStrokes[5] = KeyStroke.getKeyStroke(KeyEvent.VK_PAGE_UP, KeyEvent.CTRL_DOWN_MASK);
178 _keyStrokes[6] = KeyStroke.getKeyStroke(KeyEvent.VK_HOME, KeyEvent.CTRL_DOWN_MASK);
179 _keyStrokes[7] = KeyStroke.getKeyStroke(KeyEvent.VK_END, KeyEvent.CTRL_DOWN_MASK);
180 }
181 return _keyStrokes;
182 }
183 }