1 /* |
1 /* |
2 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved. |
2 * Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * |
4 * |
5 * This code is free software; you can redistribute it and/or modify it |
5 * This code is free software; you can redistribute it and/or modify it |
6 * under the terms of the GNU General Public License version 2 only, as |
6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. Oracle designates this |
7 * published by the Free Software Foundation. Oracle designates this |
30 import java.awt.*; |
30 import java.awt.*; |
31 import java.awt.event.*; |
31 import java.awt.event.*; |
32 import java.awt.peer.ListPeer; |
32 import java.awt.peer.ListPeer; |
33 import java.util.Arrays; |
33 import java.util.Arrays; |
34 |
34 |
35 final class LWListPeer |
35 /** |
36 extends LWComponentPeer<List, LWListPeer.ScrollableJList> |
36 * Lightweight implementation of {@link ListPeer}. |
|
37 */ |
|
38 final class LWListPeer extends LWComponentPeer<List, LWListPeer.ScrollableJList> |
37 implements ListPeer { |
39 implements ListPeer { |
|
40 |
|
41 /** |
|
42 * The default number of visible rows. |
|
43 */ |
|
44 private static final int DEFAULT_VISIBLE_ROWS = 4; // From java.awt.List, |
|
45 |
|
46 /** |
|
47 * This text is used for cell bounds calculation. |
|
48 */ |
|
49 private static final String TEXT = "0123456789abcde"; |
38 |
50 |
39 LWListPeer(final List target, final PlatformComponent platformComponent) { |
51 LWListPeer(final List target, final PlatformComponent platformComponent) { |
40 super(target, platformComponent); |
52 super(target, platformComponent); |
41 if (!getTarget().isBackgroundSet()) { |
53 if (!getTarget().isBackgroundSet()) { |
42 getTarget().setBackground(SystemColor.text); |
54 getTarget().setBackground(SystemColor.text); |
133 : ListSelectionModel.SINGLE_SELECTION); |
145 : ListSelectionModel.SINGLE_SELECTION); |
134 } |
146 } |
135 } |
147 } |
136 |
148 |
137 @Override |
149 @Override |
|
150 public Dimension getPreferredSize() { |
|
151 return getMinimumSize(); |
|
152 } |
|
153 |
|
154 @Override |
|
155 public Dimension getMinimumSize() { |
|
156 return getMinimumSize(DEFAULT_VISIBLE_ROWS); |
|
157 } |
|
158 |
|
159 @Override |
138 public Dimension getPreferredSize(final int rows) { |
160 public Dimension getPreferredSize(final int rows) { |
139 return getMinimumSize(rows); |
161 return getMinimumSize(rows); |
140 } |
162 } |
141 |
163 |
142 @Override |
164 @Override |
143 public Dimension getMinimumSize(final int rows) { |
165 public Dimension getMinimumSize(final int rows) { |
144 synchronized (getDelegateLock()) { |
166 synchronized (getDelegateLock()) { |
145 final int margin = 2; |
167 final Dimension size = getCellSize(); |
146 final int space = 1; |
168 size.height *= rows; |
147 |
169 // Always take vertical scrollbar into account. |
148 // TODO: count ScrollPane's scrolling elements if any. |
170 final JScrollBar vbar = getDelegate().getVerticalScrollBar(); |
149 final FontMetrics fm = getFontMetrics(getFont()); |
171 size.width += vbar != null ? vbar.getMinimumSize().width : 0; |
150 final int itemHeight = (fm.getHeight() - fm.getLeading()) + (2 * space); |
172 // JScrollPane and JList insets |
151 |
173 final Insets pi = getDelegate().getInsets(); |
152 return new Dimension(20 + (fm == null ? 10 * 15 : fm.stringWidth("0123456789abcde")), |
174 final Insets vi = getDelegate().getView().getInsets(); |
153 (fm == null ? 10 : itemHeight) * rows + (2 * margin)); |
175 size.width += pi.left + pi.right + vi.left + vi.right; |
154 } |
176 size.height += pi.top + pi.bottom + vi.top + vi.bottom; |
|
177 return size; |
|
178 } |
|
179 } |
|
180 |
|
181 private Dimension getCellSize() { |
|
182 final JList<String> jList = getDelegate().getView(); |
|
183 final ListCellRenderer<? super String> cr = jList.getCellRenderer(); |
|
184 final Component cell = cr.getListCellRendererComponent(jList, TEXT, 0, |
|
185 false, false); |
|
186 return cell.getPreferredSize(); |
155 } |
187 } |
156 |
188 |
157 private void revalidate() { |
189 private void revalidate() { |
158 synchronized (getDelegateLock()) { |
190 synchronized (getDelegateLock()) { |
159 getDelegate().getView().invalidate(); |
191 getDelegate().getView().invalidate(); |
163 |
195 |
164 final class ScrollableJList extends JScrollPane implements ListSelectionListener { |
196 final class ScrollableJList extends JScrollPane implements ListSelectionListener { |
165 |
197 |
166 private boolean skipStateChangedEvent; |
198 private boolean skipStateChangedEvent; |
167 |
199 |
168 private DefaultListModel<Object> model = |
200 private final DefaultListModel<String> model = |
169 new DefaultListModel<Object>() { |
201 new DefaultListModel<String>() { |
170 @Override |
202 @Override |
171 public void add(final int index, final Object element) { |
203 public void add(final int index, final String element) { |
172 if (index == -1) { |
204 if (index == -1) { |
173 addElement(element); |
205 addElement(element); |
174 } else { |
206 } else { |
175 super.add(index, element); |
207 super.add(index, element); |
176 } |
208 } |
179 |
211 |
180 private int[] oldSelectedIndices = new int[0]; |
212 private int[] oldSelectedIndices = new int[0]; |
181 |
213 |
182 ScrollableJList() { |
214 ScrollableJList() { |
183 getViewport().setScrollMode(JViewport.SIMPLE_SCROLL_MODE); |
215 getViewport().setScrollMode(JViewport.SIMPLE_SCROLL_MODE); |
184 final JList<Object> list = new JListDelegate(); |
216 final JList<String> list = new JListDelegate(); |
185 list.addListSelectionListener(this); |
217 list.addListSelectionListener(this); |
186 |
218 |
187 getViewport().setView(list); |
219 getViewport().setView(list); |
188 |
220 |
189 // Pull the items from the target. |
221 // Pull the items from the target. |
221 } |
253 } |
222 oldSelectedIndices = source.getSelectedIndices(); |
254 oldSelectedIndices = source.getSelectedIndices(); |
223 } |
255 } |
224 } |
256 } |
225 |
257 |
226 public JList getView() { |
258 public JList<String> getView() { |
227 return (JList) getViewport().getView(); |
259 return (JList<String>) getViewport().getView(); |
228 } |
260 } |
229 |
261 |
230 public DefaultListModel<Object> getModel() { |
262 public DefaultListModel<String> getModel() { |
231 return model; |
263 return model; |
232 } |
264 } |
233 |
265 |
234 @Override |
266 @Override |
235 public void setEnabled(final boolean enabled) { |
267 public void setEnabled(final boolean enabled) { |
270 super.processMouseEvent(e); |
302 super.processMouseEvent(e); |
271 if (e.getID() == MouseEvent.MOUSE_CLICKED && e.getClickCount() == 2) { |
303 if (e.getID() == MouseEvent.MOUSE_CLICKED && e.getClickCount() == 2) { |
272 final int index = locationToIndex(e.getPoint()); |
304 final int index = locationToIndex(e.getPoint()); |
273 if (0 <= index && index < getModel().getSize()) { |
305 if (0 <= index && index < getModel().getSize()) { |
274 LWListPeer.this.postEvent(new ActionEvent(getTarget(), ActionEvent.ACTION_PERFORMED, |
306 LWListPeer.this.postEvent(new ActionEvent(getTarget(), ActionEvent.ACTION_PERFORMED, |
275 getModel().getElementAt(index).toString(), e.getWhen(), e.getModifiers())); |
307 getModel().getElementAt(index), e.getWhen(), e.getModifiers())); |
276 } |
308 } |
277 } |
309 } |
278 } |
310 } |
279 |
311 |
280 @Override |
312 @Override |
281 protected void processKeyEvent(final KeyEvent e) { |
313 protected void processKeyEvent(final KeyEvent e) { |
282 super.processKeyEvent(e); |
314 super.processKeyEvent(e); |
283 if (e.getID() == KeyEvent.KEY_PRESSED && e.getKeyCode() == KeyEvent.VK_ENTER) { |
315 if (e.getID() == KeyEvent.KEY_PRESSED && e.getKeyCode() == KeyEvent.VK_ENTER) { |
284 final Object selectedValue = getSelectedValue(); |
316 final String selectedValue = getSelectedValue(); |
285 if(selectedValue != null){ |
317 if(selectedValue != null){ |
286 LWListPeer.this.postEvent(new ActionEvent(getTarget(), ActionEvent.ACTION_PERFORMED, |
318 LWListPeer.this.postEvent(new ActionEvent(getTarget(), ActionEvent.ACTION_PERFORMED, |
287 selectedValue.toString(), e.getWhen(), e.getModifiers())); |
319 selectedValue, e.getWhen(), e.getModifiers())); |
288 } |
320 } |
289 } |
321 } |
290 } |
322 } |
291 |
323 |
292 //Needed for Autoscroller. |
324 //Needed for Autoscroller. |