1:
37:
38: package ;
39:
40: import ;
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46: import ;
47: import ;
48: import ;
49: import ;
50: import ;
51: import ;
52: import ;
53:
54: import ;
55: import ;
56: import ;
57: import ;
58: import ;
59:
60: public class SwingListPeer
61: extends SwingComponentPeer
62: implements ListPeer
63: {
64:
65:
70: private class SwingList
71: extends JScrollPane
72: implements SwingComponent
73: {
74:
75: SwingList(Component comp)
76: {
77: super(comp, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
78: JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
79: }
80:
81:
86: public JComponent getJComponent()
87: {
88: return this;
89: }
90:
91:
97: public void handleMouseEvent(MouseEvent ev)
98: {
99: ev.setSource(this);
100: dispatchEvent(ev);
101: }
102:
103:
106: public boolean isLightweight()
107: {
108: return false;
109: }
110:
111:
117: public void handleMouseMotionEvent(MouseEvent ev)
118: {
119: processMouseMotionEvent(ev);
120: }
121:
122:
127: public void handleKeyEvent(KeyEvent ev)
128: {
129: processKeyEvent(ev);
130: }
131:
132:
137: public void handleFocusEvent(FocusEvent ev)
138: {
139: processFocusEvent(ev);
140: }
141:
142:
143:
149: public Point getLocationOnScreen()
150: {
151: return SwingListPeer.this.getLocationOnScreen();
152: }
153:
154:
161: public boolean isShowing()
162: {
163: boolean retVal = false;
164: if (SwingListPeer.this.awtComponent != null)
165: retVal = SwingListPeer.this.awtComponent.isShowing();
166: return retVal;
167: }
168:
169:
178: public Image createImage(int w, int h)
179: {
180: return SwingListPeer.this.createImage(w, h);
181: }
182:
183: public Graphics getGraphics()
184: {
185: return SwingListPeer.this.getGraphics();
186: }
187:
188: public Container getParent()
189: {
190: Container par = null;
191: if (SwingListPeer.this.awtComponent != null)
192: par = SwingListPeer.this.awtComponent.getParent();
193: return par;
194: }
195: }
196:
197:
200: private JList jList;
201:
202: private DefaultListModel listModel;
203:
204: public SwingListPeer(List list)
205: {
206: super();
207: listModel = new DefaultListModel();
208: jList = new JList(listModel);
209: SwingList swingList = new SwingList(jList);
210: init(list, swingList);
211:
212:
213: String[] items = list.getItems();
214: for (int i = 0 ; i < items.length; i++)
215: addItem(items[i], i);
216: }
217:
218: public void add(String item, int index)
219: {
220: if (listModel != null)
221: listModel.add(index, item);
222: }
223:
224: public void addItem(String item, int index)
225: {
226: if (listModel != null)
227: listModel.add(index, item);
228: }
229:
230: public void clear()
231: {
232: if (listModel != null)
233: listModel.clear();
234: }
235:
236: public void delItems(int startIndex, int endIndex)
237: {
238: if (listModel != null)
239: listModel.removeRange(startIndex, endIndex);
240: }
241:
242: public void deselect(int index)
243: {
244: if (jList != null)
245: {
246: jList.getSelectionModel().removeSelectionInterval(index, index);
247: }
248: }
249:
250: public Dimension getMinimumSize(int s)
251: {
252: Dimension d = null;
253: if (jList != null)
254: {
255: d = jList.getComponent(s).getMinimumSize();
256: }
257: return d;
258: }
259:
260: public Dimension getPreferredSize(int s)
261: {
262: Dimension d = null;
263: if (jList != null)
264: {
265: d = jList.getComponent(s).getPreferredSize();
266: }
267: return d;
268: }
269:
270: public int[] getSelectedIndexes()
271: {
272: int[] sel = null;
273: if (jList != null)
274: {
275: sel = jList.getSelectedIndices();
276: }
277: return sel;
278: }
279:
280: public void makeVisible(int index)
281: {
282: if (jList != null)
283: {
284: Component comp = jList.getComponent(index);
285: jList.scrollRectToVisible(comp.getBounds());
286: }
287: }
288:
289: public Dimension minimumSize(int s)
290: {
291: Dimension d = null;
292: if (jList != null)
293: {
294: d = jList.getComponent(s).getMinimumSize();
295: }
296: return d;
297: }
298:
299: public Dimension preferredSize(int s)
300: {
301: Dimension d = null;
302: if (jList != null)
303: {
304: d = jList.getComponent(s).getPreferredSize();
305: }
306: return d;
307: }
308:
309: public void removeAll()
310: {
311: if (jList != null)
312: {
313: jList.removeAll();
314: }
315: }
316:
317: public void select(int index)
318: {
319: if (jList != null)
320: {
321: jList.setSelectedIndex(index);
322: }
323: }
324:
325: public void setMultipleMode(boolean multi)
326: {
327: if (jList != null)
328: {
329: jList.setSelectionMode(multi
330: ? ListSelectionModel.MULTIPLE_INTERVAL_SELECTION
331: : ListSelectionModel.SINGLE_SELECTION);
332: }
333: }
334:
335: public void setMultipleSelections(boolean multi)
336: {
337: if (jList != null)
338: {
339: jList.setSelectionMode(multi
340: ? ListSelectionModel.MULTIPLE_INTERVAL_SELECTION
341: : ListSelectionModel.SINGLE_SELECTION);
342: }
343: }
344:
345: public void reshape(int x, int y, int width, int height)
346: {
347: if (swingComponent != null)
348: {
349: swingComponent.getJComponent().setBounds(x, y, width, height);
350: swingComponent.getJComponent().validate();
351: }
352: }
353:
354: protected void peerPaint(Graphics g, boolean update)
355: {
356: super.peerPaint(g, update);
357: jList.doLayout();
358: jList.list();
359:
360: Rectangle r = getBounds();
361: g.setColor(Color.RED);
362: g.drawRect(r.x, r.y, r.width, r.height);
363: }
364: }