1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package wjhk.jupload2.gui.filepanel;
24
25 import java.awt.Cursor;
26 import java.awt.event.MouseEvent;
27 import java.awt.event.MouseListener;
28 import java.util.Date;
29
30 import javax.swing.JTable;
31 import javax.swing.ListSelectionModel;
32 import javax.swing.event.ListSelectionEvent;
33 import javax.swing.table.JTableHeader;
34 import javax.swing.table.TableColumnModel;
35
36 import wjhk.jupload2.gui.JUploadPanel;
37 import wjhk.jupload2.policies.UploadPolicy;
38
39
40
41
42
43 public class FilePanelJFlatTable extends JTable implements MouseListener {
44
45
46 private static final long serialVersionUID = 5422667664740339798L;
47
48 protected int sortedColumnIndex = -1;
49
50 protected boolean sortedColumnAscending = true;
51
52
53 UploadPolicy uploadPolicy;
54
55
56 FilePanelFlatDataModel2 filePanelDataModel;
57
58
59
60
61
62
63
64 public FilePanelJFlatTable(JUploadPanel jup, UploadPolicy uploadPolicy) {
65 this.uploadPolicy = uploadPolicy;
66
67 setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
68 setDefaultRenderer(Long.class, new SizeRenderer(uploadPolicy));
69 setDefaultRenderer(Date.class, new DateRenderer(uploadPolicy));
70 setDefaultRenderer(String.class, new NameRenderer());
71
72
73
74 JTableHeader header = getTableHeader();
75 header.setDefaultRenderer(new SortHeaderRenderer());
76
77
78 header.addMouseListener(this);
79 addMouseListener(jup.getMouseListener());
80 }
81
82
83
84
85
86
87
88 public void setModel(FilePanelFlatDataModel2 filePanelDataModel) {
89 super.setModel(filePanelDataModel);
90 this.filePanelDataModel = filePanelDataModel;
91 }
92
93
94
95
96
97
98 public int getSortedColumnIndex() {
99 return this.sortedColumnIndex;
100 }
101
102
103
104
105
106
107 public boolean isSortedColumnAscending() {
108 return this.sortedColumnAscending;
109 }
110
111
112
113
114 public void mouseReleased(MouseEvent event) {
115
116 this.uploadPolicy.getContext().getUploadPanel().maybeOpenPopupMenu(
117 event);
118 }
119
120
121
122
123 public void mousePressed(MouseEvent event) {
124
125 this.uploadPolicy.getContext().getUploadPanel().maybeOpenPopupMenu(
126 event);
127 }
128
129
130
131
132 public void mouseClicked(MouseEvent event) {
133
134 if (event.getClickCount() == 2) {
135
136
137
138 int selectedRow = getSelectedRow();
139 if (selectedRow >= 0) {
140 this.uploadPolicy.onFileSelected(this.filePanelDataModel
141 .getFileDataAt(selectedRow));
142 }
143 } else if (!this.uploadPolicy.getContext().getUploadPanel()
144 .maybeOpenPopupMenu(event)) {
145
146
147 TableColumnModel colModel = getColumnModel();
148 int index = colModel.getColumnIndexAtX(event.getX());
149 int modelIndex = colModel.getColumn(index).getModelIndex();
150
151 FilePanelFlatDataModel2 model = (FilePanelFlatDataModel2) getModel();
152 if (model.isSortable(modelIndex)) {
153 if (this.sortedColumnIndex == index) {
154 this.sortedColumnAscending = !this.sortedColumnAscending;
155 }
156 this.sortedColumnIndex = index;
157
158 model.sortColumn(modelIndex, this.sortedColumnAscending);
159 }
160 }
161 }
162
163
164
165
166 public void mouseEntered(MouseEvent event) {
167
168 }
169
170
171
172
173 public void mouseExited(MouseEvent event) {
174
175 }
176
177
178
179
180 @Override
181 public void valueChanged(ListSelectionEvent e) {
182 super.valueChanged(e);
183
184 if (e.getValueIsAdjusting() || this.uploadPolicy == null)
185 return;
186
187
188 ListSelectionModel lsm = (ListSelectionModel) e.getSource();
189 if (lsm.isSelectionEmpty()) {
190 this.uploadPolicy.onFileSelected(null);
191 } else {
192 int selectedRow = lsm.getMinSelectionIndex();
193
194
195 if (selectedRow == lsm.getMaxSelectionIndex()) {
196 Cursor previousCursor = this.uploadPolicy.setWaitCursor();
197 this.uploadPolicy.onFileSelected(this.filePanelDataModel
198 .getFileDataAt(selectedRow));
199 this.uploadPolicy.setCursor(previousCursor);
200 }
201 }
202 }
203 }