Added
Link Here
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 2010 Oracle and/or its affiliates. All rights reserved. |
5 |
* |
6 |
* Oracle and Java are registered trademarks of Oracle and/or its affiliates. |
7 |
* Other names may be trademarks of their respective owners. |
8 |
* |
9 |
* The contents of this file are subject to the terms of either the GNU |
10 |
* General Public License Version 2 only ("GPL") or the Common |
11 |
* Development and Distribution License("CDDL") (collectively, the |
12 |
* "License"). You may not use this file except in compliance with the |
13 |
* License. You can obtain a copy of the License at |
14 |
* http://www.netbeans.org/cddl-gplv2.html |
15 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
16 |
* specific language governing permissions and limitations under the |
17 |
* License. When distributing the software, include this License Header |
18 |
* Notice in each file and include the License file at |
19 |
* nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this |
20 |
* particular file as subject to the "Classpath" exception as provided |
21 |
* by Oracle in the GPL Version 2 section of the License file that |
22 |
* accompanied this code. If applicable, add the following below the |
23 |
* License Header, with the fields enclosed by brackets [] replaced by |
24 |
* your own identifying information: |
25 |
* "Portions Copyrighted [year] [name of copyright owner]" |
26 |
* |
27 |
* If you wish your version of this file to be governed by only the CDDL |
28 |
* or only the GPL Version 2, indicate your decision by adding |
29 |
* "[Contributor] elects to include this software in this distribution |
30 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a |
31 |
* single choice of license, a recipient has the option to distribute |
32 |
* your version of this file under either the CDDL, the GPL Version 2 or |
33 |
* to extend the choice of license to its licensees as provided above. |
34 |
* However, if you add GPL Version 2 code and therefore, elected the GPL |
35 |
* Version 2 license, then the option applies only if the new code is |
36 |
* made subject to such option by the copyright holder. |
37 |
* |
38 |
* Contributor(s): |
39 |
* |
40 |
* Portions Copyrighted 2009 Sun Microsystems, Inc. |
41 |
*/ |
42 |
|
43 |
package org.netbeans.modules.viewmodel; |
44 |
|
45 |
import java.awt.BorderLayout; |
46 |
import java.awt.Component; |
47 |
import java.awt.Point; |
48 |
import java.awt.event.MouseEvent; |
49 |
import java.util.ArrayList; |
50 |
import java.util.EventObject; |
51 |
import java.util.List; |
52 |
import javax.swing.BoxLayout; |
53 |
import javax.swing.JLabel; |
54 |
import javax.swing.JTable; |
55 |
import javax.swing.JTextField; |
56 |
import javax.swing.JToolTip; |
57 |
import javax.swing.event.CellEditorListener; |
58 |
import javax.swing.table.TableCellEditor; |
59 |
import javax.swing.table.TableCellRenderer; |
60 |
import org.netbeans.junit.NbTestCase; |
61 |
import org.netbeans.spi.viewmodel.ColumnModel; |
62 |
import org.netbeans.spi.viewmodel.Model; |
63 |
import org.netbeans.spi.viewmodel.ModelListener; |
64 |
import org.netbeans.spi.viewmodel.Models; |
65 |
import org.netbeans.spi.viewmodel.TableModel; |
66 |
import org.netbeans.spi.viewmodel.TableRendererModel; |
67 |
import org.netbeans.spi.viewmodel.TreeModel; |
68 |
import org.netbeans.spi.viewmodel.UnknownTypeException; |
69 |
import org.openide.util.Exceptions; |
70 |
import org.openide.util.RequestProcessor; |
71 |
|
72 |
/** |
73 |
* Test of TableRendererModel. |
74 |
* |
75 |
* @author Martin Entlicher |
76 |
*/ |
77 |
public class TableRendererTest extends NbTestCase { |
78 |
|
79 |
private OutlineTable ot; |
80 |
|
81 |
public TableRendererTest(String name) { |
82 |
super(name); |
83 |
} |
84 |
|
85 |
|
86 |
private void setUpModel() { |
87 |
Model mr = new TableRendererModelImpl(); |
88 |
|
89 |
ArrayList l = new ArrayList (); |
90 |
l.add(mr); |
91 |
l.add(new ColumnModelImpl("col1")); |
92 |
l.add(new ColumnModelImpl("col2")); |
93 |
Models.CompoundModel mcm = Models.createCompoundModel(l); |
94 |
OutlineTable tt = BasicTest.createView(mcm); |
95 |
|
96 |
RequestProcessor rp = tt.currentTreeModelRoot.getRootNode().getRequestProcessor(); |
97 |
BasicTest.waitFinished (rp); |
98 |
|
99 |
this.ot = tt; |
100 |
//root = tt.getExplorerManager ().getRootContext (); |
101 |
} |
102 |
|
103 |
public void testRenderers() { |
104 |
setUpModel(); |
105 |
JTable t = ot.treeTable.getTable(); |
106 |
ot.revalidate(); |
107 |
|
108 |
javax.swing.JFrame f = new javax.swing.JFrame(); |
109 |
f.setLayout(new BorderLayout()); |
110 |
f.add(ot, BorderLayout.CENTER); |
111 |
f.setSize(600, 500); |
112 |
f.setVisible(true); |
113 |
//while (f.isVisible()) { |
114 |
try { |
115 |
Thread.sleep(333); |
116 |
} catch (InterruptedException ex) { |
117 |
Exceptions.printStackTrace(ex); |
118 |
} |
119 |
//} |
120 |
|
121 |
System.out.println("table rows = "+t.getRowCount()); |
122 |
TableCellRenderer tcr = t.getCellRenderer(0, 0); |
123 |
Component c = tcr.getTableCellRendererComponent(t, null, true, true, 0, 0); |
124 |
//System.err.println("c = "+c); |
125 |
assertTrue("Renderer component = "+c, c instanceof RendererComponent); |
126 |
assertEquals("Renderer of 0:DN", ((RendererComponent) c).getText()); |
127 |
|
128 |
tcr = t.getCellRenderer(0, 1); |
129 |
c = tcr.getTableCellRendererComponent(t, null, true, true, 0, 1); |
130 |
assertEquals("Renderer of 0:col1", ((RendererComponent) c).getText()); |
131 |
|
132 |
tcr = t.getCellRenderer(0, 2); |
133 |
c = tcr.getTableCellRendererComponent(t, null, true, true, 0, 2); |
134 |
assertEquals("Renderer of 0:col2", ((RendererComponent) c).getText()); |
135 |
|
136 |
tcr = t.getCellRenderer(1, 0); |
137 |
c = tcr.getTableCellRendererComponent(t, null, true, true, 1, 0); |
138 |
assertFalse("Renderer component = "+c, c instanceof RendererComponent); |
139 |
|
140 |
tcr = t.getCellRenderer(1, 1); |
141 |
c = tcr.getTableCellRendererComponent(t, null, true, true, 1, 1); |
142 |
assertFalse("Renderer component = "+c, c instanceof RendererComponent); |
143 |
|
144 |
tcr = t.getCellRenderer(2, 1); |
145 |
c = tcr.getTableCellRendererComponent(t, null, true, true, 2, 1); |
146 |
assertEquals("Renderer of 2:col1", ((RendererComponent) c).getText()); |
147 |
|
148 |
tcr = t.getCellRenderer(6, 0); |
149 |
c = tcr.getTableCellRendererComponent(t, null, true, true, 6, 0); |
150 |
assertEquals("Renderer of 6:DN", ((RendererComponent) c).getText()); |
151 |
|
152 |
tcr = t.getCellRenderer(8, 2); |
153 |
c = tcr.getTableCellRendererComponent(t, null, true, true, 8, 2); |
154 |
assertEquals("Renderer of 8:col2", ((RendererComponent) c).getText()); |
155 |
} |
156 |
|
157 |
public void testEditors() { |
158 |
setUpModel(); |
159 |
JTable t = ot.treeTable.getTable(); |
160 |
ot.revalidate(); |
161 |
|
162 |
javax.swing.JFrame f = new javax.swing.JFrame(); |
163 |
f.setLayout(new BorderLayout()); |
164 |
f.add(ot, BorderLayout.CENTER); |
165 |
f.setSize(600, 500); |
166 |
f.setVisible(true); |
167 |
//while (f.isVisible()) { |
168 |
try { |
169 |
Thread.sleep(333); |
170 |
} catch (InterruptedException ex) { |
171 |
Exceptions.printStackTrace(ex); |
172 |
} |
173 |
//} |
174 |
|
175 |
System.out.println("table rows = "+t.getRowCount()); |
176 |
TableCellEditor tce = t.getCellEditor(0, 0); |
177 |
assertTrue(tce+"is not editable.", tce.isCellEditable(getMouseClickAt(t, 0, 0))); |
178 |
Component c = tce.getTableCellEditorComponent(t, null, true, 0, 0); |
179 |
//System.err.println("c = "+c); |
180 |
assertTrue("Editor component = "+c, c instanceof EditorComponent); |
181 |
assertEquals("Editor of 0:DN", ((EditorComponent) c).getText()); |
182 |
|
183 |
tce = t.getCellEditor(0, 1); |
184 |
assertTrue(tce+"is not editable.", tce.isCellEditable(getMouseClickAt(t, 0, 1))); |
185 |
c = tce.getTableCellEditorComponent(t, null, true, 0, 1); |
186 |
assertTrue("Editor component = "+c, c instanceof EditorComponent); |
187 |
assertEquals("Editor of 0:col1", ((EditorComponent) c).getText()); |
188 |
|
189 |
tce = t.getCellEditor(0, 2); |
190 |
assertTrue(tce+"is not editable.", tce.isCellEditable(getMouseClickAt(t, 0, 2))); |
191 |
c = tce.getTableCellEditorComponent(t, null, true, 0, 2); |
192 |
assertTrue("Editor component = "+c, c instanceof EditorComponent); |
193 |
assertEquals("Editor of 0:col2", ((EditorComponent) c).getText()); |
194 |
|
195 |
tce = t.getCellEditor(1, 0); |
196 |
assertFalse(tce+"is editable.", tce.isCellEditable(getMouseClickAt(t, 1, 0))); |
197 |
c = tce.getTableCellEditorComponent(t, null, true, 1, 0); |
198 |
assertFalse("Editor component = "+c, c instanceof EditorComponent); |
199 |
|
200 |
tce = t.getCellEditor(1, 2); |
201 |
assertFalse(tce+"is editable.", tce.isCellEditable(getMouseClickAt(t, 1, 2))); |
202 |
c = tce.getTableCellEditorComponent(t, null, true, 1, 2); |
203 |
assertFalse("Editor component = "+c, c instanceof EditorComponent); |
204 |
|
205 |
tce = t.getCellEditor(3, 1); |
206 |
assertTrue(tce+"is not editable.", tce.isCellEditable(getMouseClickAt(t, 3, 1))); |
207 |
c = tce.getTableCellEditorComponent(t, null, true, 3, 1); |
208 |
assertTrue("Editor component = "+c, c instanceof EditorComponent); |
209 |
assertEquals("Editor of 3:col1", ((EditorComponent) c).getText()); |
210 |
|
211 |
tce = t.getCellEditor(6, 0); |
212 |
assertTrue(tce+"is not editable.", tce.isCellEditable(getMouseClickAt(t, 6, 0))); |
213 |
c = tce.getTableCellEditorComponent(t, null, true, 6, 0); |
214 |
assertTrue("Editor component = "+c, c instanceof EditorComponent); |
215 |
assertEquals("Editor of 6:DN", ((EditorComponent) c).getText()); |
216 |
|
217 |
tce = t.getCellEditor(9, 2); |
218 |
assertTrue(tce+"is not editable.", tce.isCellEditable(getMouseClickAt(t, 9, 2))); |
219 |
c = tce.getTableCellEditorComponent(t, null, true, 9, 2); |
220 |
assertTrue("Editor component = "+c, c instanceof EditorComponent); |
221 |
assertEquals("Editor of 9:col2", ((EditorComponent) c).getText()); |
222 |
} |
223 |
|
224 |
public void testTooltip() { |
225 |
setUpModel(); |
226 |
JTable t = ot.treeTable.getTable(); |
227 |
ot.revalidate(); |
228 |
|
229 |
javax.swing.JFrame f = new javax.swing.JFrame(); |
230 |
f.setLayout(new BorderLayout()); |
231 |
f.add(ot, BorderLayout.CENTER); |
232 |
f.setSize(600, 500); |
233 |
f.setVisible(true); |
234 |
//while (f.isVisible()) { |
235 |
try { |
236 |
Thread.sleep(333); |
237 |
} catch (InterruptedException ex) { |
238 |
Exceptions.printStackTrace(ex); |
239 |
} |
240 |
//} |
241 |
|
242 |
MouseEvent event = getMouseClickAt(t, 0, 0); |
243 |
String tipText = t.getToolTipText(event); |
244 |
JToolTip tip = t.createToolTip(); |
245 |
tip.setTipText(tipText); |
246 |
assertTrue("Bad ToolTip class: "+tip, tip instanceof RendererComponent.ToolTipComponent); |
247 |
assertEquals("ToolTip for Renderer of 0:DN", tip.getTipText()); |
248 |
|
249 |
event = getMouseClickAt(t, 1, 1); |
250 |
tipText = t.getToolTipText(event); |
251 |
tip = t.createToolTip(); |
252 |
tip.setTipText(tipText); |
253 |
assertFalse("Bad ToolTip class: "+tip, tip instanceof RendererComponent.ToolTipComponent); |
254 |
|
255 |
event = getMouseClickAt(t, 2, 2); |
256 |
tipText = t.getToolTipText(event); |
257 |
tip = t.createToolTip(); |
258 |
tip.setTipText(tipText); |
259 |
assertTrue("Bad ToolTip class: "+tip, tip instanceof RendererComponent.ToolTipComponent); |
260 |
assertEquals("ToolTip for Renderer of 2:col2", tip.getTipText()); |
261 |
} |
262 |
|
263 |
private MouseEvent getMouseClickAt(JTable t, int row, int col) { |
264 |
Point p = t.getCellRect(row, col, false).getLocation(); |
265 |
MouseEvent me = new MouseEvent(t, 1000, System.currentTimeMillis(), 0, p.x, p.y, 1, false); |
266 |
return me; |
267 |
} |
268 |
|
269 |
private static class RendererComponent extends JLabel { |
270 |
|
271 |
private String s; |
272 |
|
273 |
public RendererComponent(String s) { |
274 |
super(s); |
275 |
this.s = s; |
276 |
} |
277 |
|
278 |
@Override |
279 |
public String getToolTipText() { |
280 |
return "ToolTip for "+s; |
281 |
} |
282 |
|
283 |
@Override |
284 |
public JToolTip createToolTip() { |
285 |
return new ToolTipComponent(); |
286 |
} |
287 |
|
288 |
private static class ToolTipComponent extends JToolTip { |
289 |
|
290 |
} |
291 |
|
292 |
} |
293 |
|
294 |
private static final class CellRendererImpl implements TableCellRenderer { |
295 |
|
296 |
private Object node; |
297 |
private String columnID; |
298 |
|
299 |
public CellRendererImpl(Object node, String columnID) { |
300 |
this.node = node; |
301 |
this.columnID = columnID; |
302 |
} |
303 |
|
304 |
public Object getNode() { |
305 |
return node; |
306 |
} |
307 |
|
308 |
public String getColumnID() { |
309 |
return columnID; |
310 |
} |
311 |
|
312 |
@Override |
313 |
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { |
314 |
return new RendererComponent("Renderer of "+node.toString()+":"+columnID); |
315 |
} |
316 |
|
317 |
} |
318 |
|
319 |
private static class EditorComponent extends JTextField { |
320 |
public EditorComponent(String s) { |
321 |
super(s); |
322 |
} |
323 |
} |
324 |
|
325 |
private static final class CellEditorImpl implements TableCellEditor { |
326 |
|
327 |
private Object node; |
328 |
private String columnID; |
329 |
|
330 |
public CellEditorImpl(Object node, String columnID) { |
331 |
this.node = node; |
332 |
this.columnID = columnID; |
333 |
} |
334 |
|
335 |
public Object getNode() { |
336 |
return node; |
337 |
} |
338 |
|
339 |
public String getColumnID() { |
340 |
return columnID; |
341 |
} |
342 |
|
343 |
@Override |
344 |
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) { |
345 |
return new EditorComponent("Editor of "+node.toString()+":"+columnID); |
346 |
} |
347 |
|
348 |
@Override |
349 |
public Object getCellEditorValue() { |
350 |
throw new UnsupportedOperationException("Not supported yet."); |
351 |
} |
352 |
|
353 |
@Override |
354 |
public boolean isCellEditable(EventObject anEvent) { |
355 |
return true; |
356 |
} |
357 |
|
358 |
@Override |
359 |
public boolean shouldSelectCell(EventObject anEvent) { |
360 |
return true; |
361 |
} |
362 |
|
363 |
@Override |
364 |
public boolean stopCellEditing() { |
365 |
throw new UnsupportedOperationException("Not supported yet."); |
366 |
} |
367 |
|
368 |
@Override |
369 |
public void cancelCellEditing() { |
370 |
throw new UnsupportedOperationException("Not supported yet."); |
371 |
} |
372 |
|
373 |
@Override |
374 |
public void addCellEditorListener(CellEditorListener l) { |
375 |
} |
376 |
|
377 |
@Override |
378 |
public void removeCellEditorListener(CellEditorListener l) { |
379 |
} |
380 |
} |
381 |
|
382 |
private static final class TableRendererModelImpl implements TableRendererModel, TreeModel, TableModel { |
383 |
|
384 |
private List<ModelListener> listeners = new ArrayList<ModelListener>(); |
385 |
|
386 |
// TableRendererModel |
387 |
|
388 |
@Override |
389 |
public boolean canRenderCell(Object node, String columnID) throws UnknownTypeException { |
390 |
return Integer.parseInt((String) node) % 2 == 0; // Use this renderer for even rows only. |
391 |
} |
392 |
|
393 |
@Override |
394 |
public TableCellRenderer getCellRenderer(Object node, String columnID) throws UnknownTypeException { |
395 |
if (!(Integer.parseInt((String) node) % 2 == 0)) { |
396 |
throw new IllegalStateException("Trying to get renderer even if we can not provide it node = "+node); |
397 |
} |
398 |
return new CellRendererImpl(node, columnID); |
399 |
} |
400 |
|
401 |
@Override |
402 |
public boolean canEditCell(Object node, String columnID) throws UnknownTypeException { |
403 |
return Integer.parseInt((String) node) % 3 == 0; // Use this renderer for every third row only. |
404 |
} |
405 |
|
406 |
@Override |
407 |
public TableCellEditor getCellEditor(Object node, String columnID) throws UnknownTypeException { |
408 |
if (!(Integer.parseInt((String) node) % 3 == 0)) { |
409 |
throw new IllegalStateException("Trying to get editor even if we can not provide it node = "+node); |
410 |
} |
411 |
return new CellEditorImpl(node, columnID); |
412 |
} |
413 |
|
414 |
@Override |
415 |
public void addModelListener(ModelListener l) { |
416 |
listeners.add(l); |
417 |
} |
418 |
|
419 |
@Override |
420 |
public void removeModelListener(ModelListener l) { |
421 |
listeners.remove(l); |
422 |
} |
423 |
|
424 |
// TreeModel |
425 |
|
426 |
@Override |
427 |
public Object getRoot() { |
428 |
return ROOT; |
429 |
} |
430 |
|
431 |
@Override |
432 |
public Object[] getChildren(Object parent, int from, int to) throws UnknownTypeException { |
433 |
if (ROOT.equals(parent)) { |
434 |
return new String[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" }; |
435 |
} |
436 |
throw new UnknownTypeException(parent); |
437 |
} |
438 |
|
439 |
@Override |
440 |
public boolean isLeaf(Object node) throws UnknownTypeException { |
441 |
return !ROOT.equals(node); |
442 |
} |
443 |
|
444 |
@Override |
445 |
public int getChildrenCount(Object node) throws UnknownTypeException { |
446 |
return Integer.MAX_VALUE; |
447 |
} |
448 |
|
449 |
// TableModel |
450 |
|
451 |
@Override |
452 |
public Object getValueAt(Object node, String columnID) throws UnknownTypeException { |
453 |
return ((String) node) + " " + columnID; |
454 |
} |
455 |
|
456 |
@Override |
457 |
public boolean isReadOnly(Object node, String columnID) throws UnknownTypeException { |
458 |
return Integer.parseInt((String) node) % 2 == 0; |
459 |
} |
460 |
|
461 |
@Override |
462 |
public void setValueAt(Object node, String columnID, Object value) throws UnknownTypeException { |
463 |
throw new UnsupportedOperationException("Not supported yet."); |
464 |
} |
465 |
|
466 |
} |
467 |
|
468 |
private static class ColumnModelImpl extends ColumnModel { |
469 |
|
470 |
private String id; |
471 |
|
472 |
ColumnModelImpl(String id) { |
473 |
this.id = id; |
474 |
} |
475 |
|
476 |
@Override |
477 |
public Class getType() { |
478 |
return String.class; |
479 |
} |
480 |
|
481 |
@Override |
482 |
public String getDisplayName() { |
483 |
return "Test Column "+id; |
484 |
} |
485 |
|
486 |
@Override |
487 |
public String getID() { |
488 |
return id; |
489 |
} |
490 |
|
491 |
} |
492 |
} |