1 //
2 // $Id: FilePanelJTable.java 95 2007-05-02 03:27:05 +0000 (mer., 02 mai 2007)
3 // /C=DE/ST=Baden-Wuerttemberg/O=ISDN4Linux/OU=Fritz
4 // Elfert/CN=svn-felfert@isdn4linux.de/emailAddress=fritz@fritz-elfert.de $
5 //
6 // jupload - A file upload applet.
7 // Copyright 2007 The JUpload Team
8 //
9 // Created: ?
10 // Creator: William JinHua Kwong
11 // Last modified: $Date: 2015-03-10 20:59:42 +0100 (mar., 10 mars 2015) $
12 //
13 // This program is free software; you can redistribute it and/or modify it under
14 // the terms of the GNU General Public License as published by the Free Software
15 // Foundation; either version 2 of the License, or (at your option) any later
16 // version. This program is distributed in the hope that it will be useful, but
17 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
19 // details. You should have received a copy of the GNU General Public License
20 // along with this program; if not, write to the Free Software Foundation, Inc.,
21 // 675 Mass Ave, Cambridge, MA 02139, USA.
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 * This class is the JTable that display file information to the users. Data is
41 * handled by the wjhk.jupload2.gui.FilePanelDataModel2 class.
42 */
43 public class FilePanelJFlatTable extends JTable implements MouseListener {
44
45 /** A generated serialVersionUID, to avoid warning during compilation */
46 private static final long serialVersionUID = 5422667664740339798L;
47
48 protected int sortedColumnIndex = -1;
49
50 protected boolean sortedColumnAscending = true;
51
52 // The current UploadPolicy
53 UploadPolicy uploadPolicy;
54
55 // The current DataModel
56 FilePanelFlatDataModel2 filePanelDataModel;
57
58 /**
59 * Creates a new instance.
60 *
61 * @param jup The parent upload panel.
62 * @param uploadPolicy The policy for retrieval of various settings.
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 // setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
73
74 JTableHeader header = getTableHeader();
75 header.setDefaultRenderer(new SortHeaderRenderer());
76 // We add the mouse listener on the header (to manage column sorts) and
77 // on the main part (to manage the contextual popup menu)
78 header.addMouseListener(this);
79 addMouseListener(jup.getMouseListener());
80 }
81
82 /**
83 * Set the model. Forces the model to be a FilePanelDataModel2. This method
84 * calls the {@link JTable#setModel(javax.swing.table.TableModel)} method.
85 *
86 * @param filePanelDataModel
87 */
88 public void setModel(FilePanelFlatDataModel2 filePanelDataModel) {
89 super.setModel(filePanelDataModel);
90 this.filePanelDataModel = filePanelDataModel;
91 }
92
93 /**
94 * Retrieve the currently sorted column.
95 *
96 * @return the index of the currently sorted column.
97 */
98 public int getSortedColumnIndex() {
99 return this.sortedColumnIndex;
100 }
101
102 /**
103 * Retrieve the current sort order.
104 *
105 * @return true, if the current sort order is ascending, false otherwise.
106 */
107 public boolean isSortedColumnAscending() {
108 return this.sortedColumnAscending;
109 }
110
111 /**
112 * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
113 */
114 public void mouseReleased(MouseEvent event) {
115 // Displays the contextual menu ?
116 this.uploadPolicy.getContext().getUploadPanel().maybeOpenPopupMenu(
117 event);
118 }
119
120 /**
121 * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
122 */
123 public void mousePressed(MouseEvent event) {
124 // Displays the contextual menu ?
125 this.uploadPolicy.getContext().getUploadPanel().maybeOpenPopupMenu(
126 event);
127 }
128
129 /**
130 * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
131 */
132 public void mouseClicked(MouseEvent event) {
133 // Is this a double-click ?
134 if (event.getClickCount() == 2) {
135 // Let's open the 'big' preview, if we're in picture mode.
136 // We should have one selected row. Let's check it, you never knows
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 // We did not open the displays the contextual menu. So we do what
146 // we have to do: sort the clicked column
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 * @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
165 */
166 public void mouseEntered(MouseEvent event) {
167 // Nothing to do.
168 }
169
170 /**
171 * @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
172 */
173 public void mouseExited(MouseEvent event) {
174 // Nothing to do.
175 }
176
177 /**
178 * @see javax.swing.JTable#valueChanged(javax.swing.event.ListSelectionEvent)
179 */
180 @Override
181 public void valueChanged(ListSelectionEvent e) {
182 super.valueChanged(e);
183 // Ignore extra messages, and no action before initialization.
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 // if one file is selected, we let the current upload policy reacts.
194 // Otherwise, we don't do anything.
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 }