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 java.awt.Component;
|
|
|
17 |
import java.awt.GridBagConstraints;
|
|
|
18 |
import java.awt.GridBagLayout;
|
|
|
19 |
import java.awt.Insets;
|
|
|
20 |
import java.awt.Rectangle;
|
|
|
21 |
import java.awt.event.ActionEvent;
|
|
|
22 |
import java.awt.event.ActionListener;
|
|
|
23 |
import java.awt.event.MouseEvent;
|
|
|
24 |
import java.awt.event.MouseListener;
|
|
|
25 |
import java.util.List;
|
|
|
26 |
|
|
|
27 |
import javax.swing.Action;
|
|
|
28 |
import javax.swing.BorderFactory;
|
|
|
29 |
import javax.swing.ImageIcon;
|
|
|
30 |
import javax.swing.JButton;
|
|
|
31 |
import javax.swing.JLabel;
|
|
|
32 |
import javax.swing.JPanel;
|
|
|
33 |
import javax.swing.JPopupMenu;
|
|
|
34 |
import javax.swing.JTable;
|
|
|
35 |
import javax.swing.table.JTableHeader;
|
|
|
36 |
import javax.swing.table.TableCellRenderer;
|
|
|
37 |
import javax.swing.table.TableColumn;
|
|
|
38 |
|
|
|
39 |
public class CloseTableHeaderRenderer extends JPanel implements TableCellRenderer, MouseListener {
|
|
|
40 |
private ImageIcon icon1 = new ImageIcon(this.getClass().getResource("remove.png"));
|
|
|
41 |
private ImageIcon icon2 = new ImageIcon(this.getClass().getResource("remove2.png"));
|
|
|
42 |
private final JButton b = new JButton(icon1);
|
|
|
43 |
private JTableHeader header;
|
|
|
44 |
private JTable editedTable;
|
|
|
45 |
private int editedColumn;
|
|
|
46 |
private JLabel label = new JLabel();
|
|
|
47 |
private List popupActions;
|
|
|
48 |
|
|
|
49 |
public CloseTableHeaderRenderer(final Action buttonAction, List popupActions) {
|
|
|
50 |
|
|
|
51 |
this.popupActions=popupActions;
|
|
|
52 |
this.setBorder(BorderFactory.createEtchedBorder());
|
|
|
53 |
this.setLayout(new GridBagLayout());
|
|
|
54 |
GridBagConstraints c = new GridBagConstraints();
|
|
|
55 |
c.insets = new Insets(0, 2, 0, 1);
|
|
|
56 |
c.gridx = 0;
|
|
|
57 |
c.fill = GridBagConstraints.HORIZONTAL;
|
|
|
58 |
c.weightx = 1;
|
|
|
59 |
this.add(label, c);
|
|
|
60 |
|
|
|
61 |
c.weightx = 0;
|
|
|
62 |
c.gridx++;
|
|
|
63 |
b.setBorder(null);
|
|
|
64 |
b.setFocusable(false);
|
|
|
65 |
b.setOpaque(false);
|
|
|
66 |
b.setMargin(new Insets(1, 1, 1, 1));
|
|
|
67 |
|
|
|
68 |
b.addActionListener(new ActionListener() {
|
|
|
69 |
public void actionPerformed(ActionEvent e) {
|
|
|
70 |
buttonAction.actionPerformed(e);
|
|
|
71 |
|
|
|
72 |
}
|
|
|
73 |
});
|
|
|
74 |
this.add(b, c);
|
|
|
75 |
|
|
|
76 |
}
|
|
|
77 |
|
|
|
78 |
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
|
|
|
79 |
|
|
|
80 |
|
|
|
81 |
this.header = table.getTableHeader();
|
|
|
82 |
header.setReorderingAllowed(false);
|
|
|
83 |
this.editedTable = table;
|
|
|
84 |
this.editedColumn = column;
|
|
|
85 |
this.label.setText(value.toString());
|
|
|
86 |
this.header.removeMouseListener(this);
|
|
|
87 |
this.header.addMouseListener(this);
|
|
|
88 |
return this;
|
|
|
89 |
}
|
|
|
90 |
public void destroy(){
|
|
|
91 |
|
|
|
92 |
this.header.removeMouseListener(this);
|
|
|
93 |
this.editedTable=null;
|
|
|
94 |
this.header=null;
|
|
|
95 |
}
|
|
|
96 |
|
|
|
97 |
public void mouseClicked(MouseEvent e) {
|
|
|
98 |
// TODO Auto-generated method stub
|
|
|
99 |
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
public void mouseEntered(MouseEvent e) {
|
|
|
103 |
// TODO Auto-generated method stub
|
|
|
104 |
|
|
|
105 |
}
|
|
|
106 |
|
|
|
107 |
public void mouseExited(MouseEvent e) {
|
|
|
108 |
// TODO Auto-generated method stub
|
|
|
109 |
|
|
|
110 |
}
|
|
|
111 |
|
|
|
112 |
public void mousePressed(MouseEvent e) {
|
|
|
113 |
|
|
|
114 |
if (header == null)
|
|
|
115 |
return;
|
|
|
116 |
|
|
|
117 |
int col = header.columnAtPoint(e.getPoint());
|
|
|
118 |
|
|
|
119 |
if(col<0)
|
|
|
120 |
return;
|
|
|
121 |
// System.out.println(col);
|
|
|
122 |
// System.out.println(this.getBounds());
|
|
|
123 |
TableColumn tcol = this.editedTable.getColumnModel().getColumn(col);
|
|
|
124 |
System.out.println(tcol.getHeaderValue());
|
|
|
125 |
int x = 0;
|
|
|
126 |
for (int i = 0; i < editedColumn; i++) {
|
|
|
127 |
TableColumn acol = this.editedTable.getColumnModel().getColumn(i);
|
|
|
128 |
x += acol.getWidth();
|
|
|
129 |
}
|
|
|
130 |
// System.out.println("x:" + x);
|
|
|
131 |
// System.out.println("b:" + b.getBounds());
|
|
|
132 |
System.out.println("p:" + (e.getX() - x) + "," + e.getY());
|
|
|
133 |
System.out.println("p:"+ getBounds());
|
|
|
134 |
boolean test = b.getBounds().contains(e.getX() - x, e.getY());
|
|
|
135 |
System.out.println(test);
|
|
|
136 |
|
|
|
137 |
if (test) {
|
|
|
138 |
b.doClick();
|
|
|
139 |
} else {
|
|
|
140 |
if (e.getButton() == MouseEvent.BUTTON3 && new Rectangle(-getBounds().x, -getBounds().y).contains(e.getX() - x, e.getY())) {
|
|
|
141 |
|
|
|
142 |
JPopupMenu pop=new JPopupMenu();
|
|
|
143 |
|
|
|
144 |
for (int i = 0; i < popupActions.size(); i++) {
|
|
|
145 |
|
|
|
146 |
pop.add((Action) popupActions.get(i));
|
|
|
147 |
}
|
|
|
148 |
pop.show(this.header,e.getX(),e.getY());
|
|
|
149 |
}
|
|
|
150 |
}
|
|
|
151 |
|
|
|
152 |
}
|
|
|
153 |
|
|
|
154 |
public void mouseReleased(MouseEvent e) {
|
|
|
155 |
// TODO Auto-generated method stub
|
|
|
156 |
|
|
|
157 |
}
|
|
|
158 |
|
|
|
159 |
}
|