1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
|
/*
Wotonomy: OpenStep design patterns for pure Java applications.
Copyright (C) 2000 Intersect Software Corporation
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, see http://www.gnu.org
*/
package net.wotonomy.ui.swing.components;
import java.awt.Color;
import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Enumeration;
import java.util.EventObject;
import java.util.Vector;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JTree;
import javax.swing.ListCellRenderer;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import javax.swing.event.CellEditorListener;
import javax.swing.event.ChangeEvent;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
import javax.swing.tree.TreeCellEditor;
import javax.swing.tree.TreeCellRenderer;
/**
* A cell renderer that displays icons in addition to text, and additionally is
* an editor in case you want to click the icon to trigger some kind of action.
* You probably should override both getStringForContext and getIconForContext
* to achieve your desired results. To receive mouse clicks, set the same
* instance of the renderer as the editor for the same component.<br>
* <br>
*
* One notable addition is that this class is an action event broadcaster.
* ActionEvents are broadcast when the mouse is clicked on the button with an
* action event containing a user-configurable string that defaults to CLICKED.
* <br>
* <br>
*
* The renderer itself can be used as a JComponent if you need something like a
* JLabel that allows you to click on the icon. You will want to call setIcon
* and setText to configure the component since the renderer method would not be
* called. (If you add an instance of the renderer to a container, you cannnot
* use the same instance as an editor in a table, tree, or list.)
*
* @author michael@mpowers.net
* @version $Revision: 904 $ $Date: 2006-02-18 18:19:05 -0500 (Sat, 18 Feb 2006)
* $
*/
public class IconCellRenderer extends JPanel implements TableCellRenderer, TableCellEditor, TreeCellRenderer,
TreeCellEditor, ListCellRenderer, Runnable, ActionListener, MouseListener {
public static final String CLICKED = "CLICKED";
/**
* The panel that is re-used to render everything. This is returned by
* getRendererForContext.
*/
protected JPanel rendererPanel;
protected JLabel rendererLabel;
protected JButton rendererButton;
/**
* The panel that is used to receive mouse clicks. It must be a different
* component from rendererPanel. This is returned by getEditorForContext.
*/
protected JPanel editorPanel;
protected JLabel editorLabel;
protected JButton editorButton;
private Object lastKnownValue;
private JComponent lastKnownComponent;
// do as DefaultTableCellRenderer does
private Border noFocusBorder;
private Border treeFocusBorder;
private Color unselectedForeground;
private Color unselectedBackground;
private Vector actionListeners;
private String actionCommand;
private Vector cellEditorListeners;
private boolean editable;
private boolean clickable;
/**
* Default constructor.
*/
public IconCellRenderer() {
editable = true;
clickable = true;
noFocusBorder = new EmptyBorder(1, 1, 1, 1);
treeFocusBorder = new LineBorder(UIManager.getColor("Tree.selectionBorderColor"));
setActionCommand(CLICKED);
rendererPanel = new JPanel();
rendererPanel.setLayout(new GridBagLayout());
editorPanel = this;
editorPanel.setLayout(new GridBagLayout());
// set up constraints
GridBagConstraints imageConstraints = new GridBagConstraints();
imageConstraints.gridx = 0;
GridBagConstraints labelConstraints = new GridBagConstraints();
labelConstraints.fill = GridBagConstraints.HORIZONTAL;
labelConstraints.gridx = 1;
labelConstraints.weightx = 1.0;
labelConstraints.ipadx = 1;
labelConstraints.insets = new Insets(0, 1, 0, 0); // sweat the pixel
// make the editor panel go away when not in use
// and pass through all mouse events to container
// this is not very useful since editorLabel and editorButton
// get all of the events
editorPanel.addMouseListener(this);
rendererLabel = new JLabel();
rendererLabel.setOpaque(false);
rendererPanel.add(rendererLabel, labelConstraints);
editorLabel = new JLabel();
editorLabel.setText(""); // default state
editorLabel.setOpaque(false);
editorPanel.add(editorLabel, labelConstraints);
unselectedForeground = rendererLabel.getForeground();
unselectedBackground = rendererLabel.getBackground();
rendererButton = new JButton();
rendererButton.setBorder(null);
rendererButton.setBorderPainted(false);
rendererButton.setContentAreaFilled(false);
rendererButton.setFocusPainted(false);
rendererButton.setMargin(new Insets(0, 0, 0, 0));
rendererPanel.add(rendererButton, imageConstraints);
editorButton = new JButton();
editorButton.setEnabled(clickable); // default state
editorButton.setIcon(null); // default state
editorButton.setBorder(null);
editorButton.setBorderPainted(false);
editorButton.setContentAreaFilled(false);
editorButton.setFocusPainted(false);
editorButton.setMargin(new Insets(0, 0, 0, 0));
editorPanel.add(editorButton, imageConstraints);
editorButton.addActionListener(this);
// add these in order to dispatch the MouseEvents
// to the lastKnownComponent, and proper management of
// DnD operations
editorLabel.addMouseListener(this);
editorButton.addMouseListener(this);
}
/**
* Returns the text string currently displayed in the editor component.
*/
public String getText() {
return editorLabel.getText();
}
/**
* Sets the text string displayed in the editor component. Default is an empty
* string.
*/
public void setText(String aString) {
editorLabel.setText(aString);
}
/**
* Returns the icon currently displayed in the editor component.
*/
public Icon getIcon() {
return editorButton.getIcon();
}
/**
* Sets the icon currently displayed in the editor component. Default is null.
*/
public void setIcon(Icon anIcon) {
editorButton.setIcon(anIcon);
if (!isClickable()) {
editorButton.setDisabledIcon(anIcon);
}
}
/**
* Returns whether the editor component's label text is editable.
*/
public boolean isEditable() {
return editable;
}
/**
* Sets whether the editor component's label text is editable. Default is true.
* Editable text is not yet implemented.
*/
public void setEditable(boolean isEditable) {
editable = isEditable;
}
/**
* Returns whether the editor component's icon is clickable.
*/
public boolean isClickable() {
return clickable;
}
/**
* Sets whether the editor component's icon is clickable. Default is true.
*/
public void setClickable(boolean isClickable) {
clickable = isClickable;
editorButton.setEnabled(clickable);
}
/**
* Returns the component from getRendererForContext.
*/
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected,
boolean cellHasFocus) {
lastKnownComponent = list;
return getRendererForContext(list, value, index, 0, isSelected, cellHasFocus, false, true);
}
/**
* Returns the component from getRendererForContext.
*/
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
int row, int column) {
lastKnownComponent = table;
return getRendererForContext(table, value, row, column, isSelected, hasFocus, false, true);
}
/**
* Returns the component from getRendererForContext.
*/
public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded,
boolean leaf, int row, boolean hasFocus) {
lastKnownComponent = tree;
return getRendererForContext(tree, value, row, 0, selected, hasFocus, expanded, leaf);
}
/**
* Returns getEditorForContext with the same parameters with hasFocus true.
*/
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
lastKnownValue = value;
lastKnownComponent = table;
return getEditorForContext(table, value, row, column, isSelected, true, false, true);
}
/**
* Returns the component from getEditorForContext with hasFocus true.
*/
public Component getTreeCellEditorComponent(JTree tree, Object value, boolean isSelected, boolean expanded,
boolean leaf, int row) {
lastKnownValue = value;
lastKnownComponent = tree;
return getEditorForContext(tree, value, row, 0, isSelected, true, expanded, leaf);
}
/**
* This default implementation returns a JPanel that is configured by calling
* configureComponentForContext.
*
* @return An component that is used to render content.
*/
public Component getRendererForContext(JComponent container, Object value, int row, int column, boolean isSelected,
boolean hasFocus, boolean isExpanded, boolean isLeaf) {
configureComponentForContext(rendererPanel, rendererButton, rendererLabel, container, value, row, column,
isSelected, hasFocus, isExpanded, isLeaf);
return rendererPanel;
}
/**
* This method returns a separate component that should be visually identical to
* the renderer component. We can't simply reuse the renderer component because
* the renderer is still used to paint the table while the editor component is
* displayed. Clicks are received on this component. This default implementation
* returns a JPanel that is configured by calling configureComponentForContext.
*
* @return A component used to receive clicks on the cell.
*/
public Component getEditorForContext(JComponent container, Object value, int row, int column, boolean isSelected,
boolean hasFocus, boolean isExpanded, boolean isLeaf) {
configureComponentForContext(editorPanel, editorButton, editorLabel, container, value, row, column, true,
hasFocus, isExpanded, isLeaf); // editor should always be selected
return editorPanel;
}
/**
* Called to configure components
*/
protected void configureComponentForContext(JPanel component, JButton iconButton, JLabel label,
JComponent container, Object value, int row, int column, boolean isSelected, boolean hasFocus,
boolean isExpanded, boolean isLeaf) {
if (hasFocus) {
if (container instanceof JTable) {
component.setBorder(UIManager.getBorder("Table.focusCellHighlightBorder"));
} else {
component.setBorder(noFocusBorder);
}
if (container instanceof JTree) // was: (false)
{
label.setBorder(treeFocusBorder);
} else {
label.setBorder(noFocusBorder);
}
} else {
label.setBorder(noFocusBorder);
component.setBorder(noFocusBorder);
}
if (isSelected) {
if (container instanceof JTree) {
label.setOpaque(true);
label.setForeground(UIManager.getColor("Tree.selectionForeground"));
label.setBackground(UIManager.getColor("Tree.selectionBackground"));
component.setBackground(container.getBackground());
} else if (container instanceof JTable) {
label.setOpaque(false);
label.setForeground(((JTable) container).getSelectionForeground());
component.setBackground(((JTable) container).getSelectionBackground());
} else {
label.setOpaque(false);
label.setForeground(UIManager.getColor("Table.selectionForeground"));
component.setBackground(UIManager.getColor("Table.selectionBackground"));
}
} else {
label.setOpaque(false);
label.setForeground(container.getForeground());
component.setBackground(container.getBackground());
}
label.setFont(container.getFont());
Icon icon = getIconForContext(container, value, row, column, isSelected, hasFocus, isExpanded, isLeaf);
iconButton.setIcon(icon);
if (!isClickable()) {
iconButton.setDisabledIcon(icon);
}
String text = getStringForContext(container, value, row, column, isSelected, hasFocus, isExpanded, isLeaf);
if ((text == null) || ("".equals(text))) {
if (!label.getText().equals(""))
label.setText("");
} else {
if (!label.getText().equals(text))
label.setText(text);
}
}
/**
* Override this method to provide an icon for the renderer. This default
* implementation returns null.
*
* @return An icon to be displayed in the cell, or null to omit the icon from
* the cell.
*/
public Icon getIconForContext(JComponent container, Object value, int row, int column, boolean isSelected,
boolean hasFocus, boolean isExpanded, boolean isLeaf) {
return null;
}
/**
* Override this method to provide a string for the renderer. This default
* implementation returns toString on the value parameter, or null if the value
* is null.
*
* @return A string to be displayed in the cell.
*/
public String getStringForContext(JComponent container, Object value, int row, int column, boolean isSelected,
boolean hasFocus, boolean isExpanded, boolean isLeaf) {
if (value == null)
return null;
return value.toString();
}
/**
* Adds the specified listener to the list of listeners to be notified when the
* button receives a click.
*/
public void addActionListener(ActionListener aListener) {
if (actionListeners == null) {
actionListeners = new Vector(2);
}
actionListeners.add(aListener);
}
/**
* Removes the specified listener from the list of listeners to be notified when
* the button receives a click.
*/
public void removeActionListener(ActionListener aListener) {
actionListeners.remove(aListener);
}
/**
* Broadcasts the specified action event to all listeners.
*/
protected void fireActionEvent(ActionEvent anActionEvent) {
if (actionListeners == null)
return;
// vector's enumeration is not fail-fast
Enumeration e = actionListeners.elements();
while (e.hasMoreElements()) {
((ActionListener) e.nextElement()).actionPerformed(anActionEvent);
}
}
/**
* Returns the action command broadcast when this icon receives a click.
* Defaults to CLICKED.
*/
public String getActionCommand() {
return actionCommand;
}
/**
* Sets the action command broadcast when this table receives a double click.
*/
public void setActionCommand(String anActionCommand) {
actionCommand = anActionCommand;
}
// interface CellEditor
/**
* Returns lastKnownValue, although this should not be called.
*/
public Object getCellEditorValue() {
return lastKnownValue;
}
/**
* Returns true.
*/
public boolean isCellEditable(EventObject anEvent) {
return true;
}
/**
* Returns true.
*/
public boolean shouldSelectCell(EventObject anEvent) {
return true;
}
/**
* Fires an editing stopped event and returns true.
*/
public boolean stopCellEditing() {
ChangeEvent event = new ChangeEvent(this);
if (cellEditorListeners != null) {
// vector's enumeration is not fail-fast
Enumeration e = cellEditorListeners.elements();
while (e.hasMoreElements()) {
// broadcast editing cancelled since no value is edited
((CellEditorListener) e.nextElement()).editingCanceled(event);
}
}
lastKnownComponent = null;
return true;
}
/**
* Fires an editing cancelled event and returns true.
*/
public void cancelCellEditing() {
// HACK: cancelCellEditing() causes for the dragGesture
// to be NOT recognized AT ALL since on the next MOUSE_PRESSED
// the cell editor first needs to startEditing() [if in the tree
// the CellEditorListener is a BasicTreeUI class]
// (before the drag gesture event can be recognized).
// Also the lastKnownComponent should not be set to null,
// none of the mouse events won't dispathced to the lastKnownComponent
// in that case.
// Not calling it at all does seem to fix it, but what are the
// consequences???
// Trying to workaround this might solve it, but it introduces having
// an extra listener (a MouseMotionListnener), which might be wasteful
// (i.e. only if a Mouse_dragged event has been initiated, but DragGesture
// hasn't been recognized, postpone calling this till finish the DnD event)
// But what if do DnD and not exited ??? The mouseExited() is not called
// anyway until the DnD event is done.
ChangeEvent event = new ChangeEvent(this);
if (cellEditorListeners == null)
return;
// vector's enumeration is not fail-fast
Enumeration e = cellEditorListeners.elements();
while (e.hasMoreElements()) {
((CellEditorListener) e.nextElement()).editingCanceled(event);
}
// DO not nullify this
lastKnownComponent = null;
}
/**
* Adds the specified listener to the list of listeners to be notified when the
* table receives a double click.
*/
public void addCellEditorListener(CellEditorListener aListener) {
if (cellEditorListeners == null) {
cellEditorListeners = new Vector(2);
}
cellEditorListeners.add(aListener);
}
/**
* Removes the specified listener from the list of listeners to be notified when
* the table receives a double click.
*/
public void removeCellEditorListener(CellEditorListener aListener) {
cellEditorListeners.remove(aListener);
}
// interface ActionListener
/**
* Puts ourself on the end of the event queue for firing our action event to all
* listeners.
*/
public void actionPerformed(ActionEvent evt) {
// commented out in order NOT to set lastKnownComponent to null, since
// if this object is inside a table or tree, relying on getCellEditorValue()
// to return the currently edited object
// cancelCellEditing();
SwingUtilities.invokeLater(this);
}
// interface Runnable
/**
* Fires the action event to all listeners. This is triggered by a click on the
* icon.
*/
public void run() {
fireActionEvent(new ActionEvent(this, 0, getActionCommand()));
}
// interface MouseListener
/**
* Passes through editor mouse clicks to last known component. (left click only)
*/
public void mouseClicked(MouseEvent e) {
if (lastKnownComponent != null) {
Object source = e.getSource();
if (source != null) {
if (source == editorPanel) {
lastKnownComponent
.dispatchEvent(SwingUtilities.convertMouseEvent(editorPanel, e, lastKnownComponent));
} else if (source == editorLabel) {
lastKnownComponent
.dispatchEvent(SwingUtilities.convertMouseEvent(editorLabel, e, lastKnownComponent));
}
else if (source == editorButton) {
lastKnownComponent
.dispatchEvent(SwingUtilities.convertMouseEvent(editorButton, e, lastKnownComponent));
}
}
}
}
/**
* Passes through editor right-mouse (popup trigger) mouse events to last known
* component. Needed for possible displaying of popup menus on right click
*/
public void mousePressed(MouseEvent e) {
if (e.isPopupTrigger()) {
if (lastKnownComponent != null) {
Object source = e.getSource();
if (source != null) {
if (source == editorPanel) {
lastKnownComponent
.dispatchEvent(SwingUtilities.convertMouseEvent(editorPanel, e, lastKnownComponent));
} else if (source == editorLabel) {
lastKnownComponent
.dispatchEvent(SwingUtilities.convertMouseEvent(editorLabel, e, lastKnownComponent));
}
else if (source == editorButton) {
lastKnownComponent
.dispatchEvent(SwingUtilities.convertMouseEvent(editorButton, e, lastKnownComponent));
}
}
}
}
}
/**
* Does nothing.
*/
public void mouseReleased(MouseEvent e) {
if (e.isPopupTrigger()) {
if (lastKnownComponent != null) {
Object source = e.getSource();
if (source != null) {
if (source == editorPanel) {
lastKnownComponent
.dispatchEvent(SwingUtilities.convertMouseEvent(editorPanel, e, lastKnownComponent));
}
else if (source == editorLabel) {
lastKnownComponent
.dispatchEvent(SwingUtilities.convertMouseEvent(editorLabel, e, lastKnownComponent));
}
else if (source == editorButton) {
lastKnownComponent
.dispatchEvent(SwingUtilities.convertMouseEvent(editorButton, e, lastKnownComponent));
}
}
}
}
}
/**
* Does nothing.
*/
public void mouseEntered(MouseEvent e) {
}
/**
* Cancels cell editing.
*/
public void mouseExited(MouseEvent e) {
Object source = e.getSource();
if (source != null && source instanceof JComponent) {
// need to convert the Point from the source's coordinate system to
// editorPanel's coordinate system.
// (note that simple editorPanel.contains(e.getPoint()) fails if source is
// editorButton)
Point convertedPoint = SwingUtilities.convertPoint((JComponent) source, e.getPoint(), editorPanel);
// check if exited from editorButton, but still inside the editorPanel (works
// for editorLabel as well)
if (!editorPanel.contains(convertedPoint)) {
// This was getting called before, but it interfers with the DnD operation
cancelCellEditing();
}
}
}
/*
* This might be redundant public void cleanUp(){
*
* //since cancelCellEditing() was never called call it now cancelCellEditing();
* stopCellEditing();
*
* editorButton.removeActionListener( this ); editorPanel.removeMouseListener(
* this ); editorLabel.removeMouseListener( this );
* editorButton.removeMouseListener( this ); lastKnownComponent = null;
* lastKnownValue = null; }
*/
}
|