17 |
ilm |
1 |
/*
|
|
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
|
|
3 |
*
|
|
|
4 |
* Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
|
|
|
5 |
*
|
|
|
6 |
* The contents of this file are subject to the terms of the GNU General Public License Version 3
|
|
|
7 |
* only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
|
|
|
8 |
* copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
|
|
|
9 |
* language governing permissions and limitations under the License.
|
|
|
10 |
*
|
|
|
11 |
* When distributing the software, include this License Header Notice in each file.
|
|
|
12 |
*/
|
|
|
13 |
|
|
|
14 |
package org.openconcerto.ui.table;
|
|
|
15 |
|
|
|
16 |
import org.openconcerto.utils.JImage;
|
|
|
17 |
|
|
|
18 |
import java.awt.Component;
|
|
|
19 |
import java.awt.event.ItemEvent;
|
|
|
20 |
import java.awt.event.ItemListener;
|
|
|
21 |
import java.awt.event.MouseAdapter;
|
|
|
22 |
import java.awt.event.MouseEvent;
|
|
|
23 |
import java.net.URL;
|
156 |
ilm |
24 |
import java.util.ArrayList;
|
17 |
ilm |
25 |
import java.util.List;
|
|
|
26 |
|
|
|
27 |
import javax.swing.AbstractCellEditor;
|
|
|
28 |
import javax.swing.JTable;
|
|
|
29 |
import javax.swing.table.DefaultTableCellRenderer;
|
|
|
30 |
import javax.swing.table.TableCellEditor;
|
|
|
31 |
import javax.swing.table.TableCellRenderer;
|
|
|
32 |
|
|
|
33 |
public class IconTableCellRenderer extends AbstractCellEditor implements TableCellEditor, TableCellRenderer, ItemListener {
|
|
|
34 |
|
|
|
35 |
private int va;
|
156 |
ilm |
36 |
private final List<JImage> images = new ArrayList<>();
|
17 |
ilm |
37 |
IconCellRenderer renderer;
|
|
|
38 |
|
|
|
39 |
/**
|
|
|
40 |
* @param icons list de String, ex: c:\niceicon.png
|
|
|
41 |
*/
|
|
|
42 |
public IconTableCellRenderer(List<URL> icons) {
|
|
|
43 |
for (int i = 0; i < icons.size(); i++) {
|
|
|
44 |
URL filenameUrl = icons.get(i);
|
|
|
45 |
JImage img = new JImage(filenameUrl);
|
|
|
46 |
img.setCenterImage(true);
|
|
|
47 |
img.check();
|
|
|
48 |
img.addMouseListener(new MouseAdapter() {
|
156 |
ilm |
49 |
@Override
|
17 |
ilm |
50 |
public void mousePressed(MouseEvent e) {
|
|
|
51 |
super.mousePressed(e);
|
|
|
52 |
IconTableCellRenderer.this.va++;
|
|
|
53 |
if (IconTableCellRenderer.this.va >= IconTableCellRenderer.this.images.size()) {
|
|
|
54 |
IconTableCellRenderer.this.va = 0;
|
|
|
55 |
}
|
|
|
56 |
stopCellEditing();
|
|
|
57 |
}
|
|
|
58 |
});
|
|
|
59 |
this.images.add(img);
|
|
|
60 |
}
|
|
|
61 |
this.renderer = new IconCellRenderer(this.images);
|
|
|
62 |
}
|
|
|
63 |
|
|
|
64 |
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
|
149 |
ilm |
65 |
if (value == null) {
|
|
|
66 |
// Bug remonté sur Linux
|
|
|
67 |
value = Integer.valueOf(0);
|
|
|
68 |
}
|
17 |
ilm |
69 |
this.va = ((Integer) value).intValue();
|
156 |
ilm |
70 |
return this.images.get(this.va);
|
17 |
ilm |
71 |
}
|
|
|
72 |
|
|
|
73 |
public Object getCellEditorValue() {
|
156 |
ilm |
74 |
return Integer.valueOf(this.va);
|
17 |
ilm |
75 |
}
|
|
|
76 |
|
|
|
77 |
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
|
|
|
78 |
return this.renderer.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
|
|
|
79 |
}
|
|
|
80 |
|
|
|
81 |
public void itemStateChanged(ItemEvent e) {
|
156 |
ilm |
82 |
// nothing
|
17 |
ilm |
83 |
}
|
|
|
84 |
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
class IconCellRenderer extends DefaultTableCellRenderer {
|
156 |
ilm |
88 |
private List<JImage> images;
|
17 |
ilm |
89 |
|
|
|
90 |
public IconCellRenderer(List<JImage> images) {
|
|
|
91 |
super();
|
|
|
92 |
this.images = images;
|
|
|
93 |
this.setHorizontalAlignment(CENTER);
|
|
|
94 |
this.setBorder(null);
|
|
|
95 |
this.setIconTextGap(0);
|
|
|
96 |
this.setHorizontalTextPosition(0);
|
|
|
97 |
}
|
|
|
98 |
|
|
|
99 |
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
|
156 |
ilm |
100 |
if (value == null) {
|
|
|
101 |
// Bug remonté sur Linux
|
|
|
102 |
value = Integer.valueOf(0);
|
|
|
103 |
}
|
17 |
ilm |
104 |
int val = ((Integer) value).intValue();
|
|
|
105 |
final JImage image = this.images.get(val);
|
|
|
106 |
this.setIcon(image.getImageIcon());
|
41 |
ilm |
107 |
TableCellRendererUtils.setColors(image, table, isSelected);
|
|
|
108 |
TableCellRendererUtils.setColors(this, table, isSelected);
|
17 |
ilm |
109 |
return this;
|
|
|
110 |
}
|
|
|
111 |
|
|
|
112 |
}
|