Source code: medi/swing/AbstractDataPanel.java
1 /*
2 * AbstractDataPanel.java
3 *
4 * Created on 16 aprile 2002, 10.41
5 Medi - A media archiver. Archives media files for easy management.
6 Copyright (C) 2002 Antonio Petrelli
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
22 Contact me at: brenmcguire@users.sourceforge.net
23 */
24
25 package medi.swing;
26
27 import medi.base.Provider;
28 import javatools.db.*;
29 import medi.swing.util.TableUtils;
30 import java.util.Iterator;
31 import medi.base.util.VolumeMounter;
32
33 /**
34 * It is a generic panel to show information about data relative to another
35 * entity, such as sessions, author etc.
36 * @author Antonio Petrelli
37 * @version 0.0.1
38 */
39 public abstract class AbstractDataPanel extends javax.swing.JPanel {
40
41 /** The provider to use.
42 */
43 protected Provider prv;
44
45 /** Creates new form AbstractDataPanel */
46 public AbstractDataPanel() {
47 initComponents();
48 initTableSizes();
49 initSelectionListeners();
50 panDataDetailsView = new DataDetailsPanel();
51 sppDataView.setRightComponent(panDataDetailsView);
52 }
53
54 /** Sets the provider to use.
55 * @param pPrv The provider to use.
56 */
57 public void setProvider(Provider pPrv) {
58 prv = pPrv;
59 panDataDetailsView.setProvider(prv);
60 }
61
62 /** Sets the volume mounter to use.
63 * @param pVmount The volume mounter.
64 */
65 public void setVolumeMounter(VolumeMounter pVmount) {
66 panDataDetailsView.setVolumeMounter(pVmount);
67 }
68
69 /** Shows contained items.
70 * @throws DbException If something goes wrong.
71 */
72 public abstract void showItems() throws DbException;
73
74 /** Fills the table with a given DbIterator.
75 * @param rowIt The iterator containing the data.
76 * @throws DbException If something goes wrong.
77 */
78 protected void fillTable(DbIterator rowIt) throws DbException {
79 try {
80 TableUtils.fillSortedTable(tabDataList, rowIt, dataIndexes, 0);
81 }
82 catch (DbException e) {
83 System.out.println(e.getMessage());
84 }
85 initDataTableSizes();
86 }
87
88 /** Links a new data ID in different ways, depending on the derived class.
89 * @param dataID The data ID to link.
90 * @throws DbException If something goes wrong.
91 */
92 protected abstract void linkNewData(Long dataID) throws DbException;
93
94 /** This method is called from within the constructor to
95 * initialize the form.
96 * WARNING: Do NOT modify this code. The content of this method is
97 * always regenerated by the Form Editor.
98 */
99 private void initComponents() {//GEN-BEGIN:initComponents
100 sppDataView = new javax.swing.JSplitPane();
101 panDataList = new javax.swing.JPanel();
102 scpDataList = new javax.swing.JScrollPane();
103 tabDataList = new javax.swing.JTable();
104 lblDataList = new javax.swing.JLabel();
105 panButData = new javax.swing.JPanel();
106 panInButData = new javax.swing.JPanel();
107 butAddData = new javax.swing.JButton();
108 butDeleteData = new javax.swing.JButton();
109
110 setLayout(new java.awt.BorderLayout());
111
112 sppDataView.setDividerLocation(200);
113 sppDataView.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);
114 panDataList.setLayout(new java.awt.BorderLayout());
115
116 tabDataList.setModel(buildDataModel());
117 tabDataList.setToolTipText(java.util.ResourceBundle.getBundle("res/MediBundle").getString("Data_list"));
118 tabDataList.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
119 scpDataList.setViewportView(tabDataList);
120
121 panDataList.add(scpDataList, java.awt.BorderLayout.CENTER);
122
123 lblDataList.setText(java.util.ResourceBundle.getBundle("res/MediBundle").getString("Data_contained"));
124 lblDataList.setPreferredSize(new java.awt.Dimension(107, 20));
125 panDataList.add(lblDataList, java.awt.BorderLayout.NORTH);
126
127 panButData.setLayout(new java.awt.BorderLayout());
128
129 panInButData.setLayout(new java.awt.GridLayout(2, 0));
130
131 butAddData.setText(java.util.ResourceBundle.getBundle("res/MediBundle").getString("Add"));
132 butAddData.setToolTipText(java.util.ResourceBundle.getBundle("res/MediBundle").getString("Creates_a_new_empty_data_item"));
133 butAddData.setMaximumSize(new java.awt.Dimension(85, 23));
134 butAddData.setPreferredSize(new java.awt.Dimension(95, 23));
135 butAddData.addActionListener(new java.awt.event.ActionListener() {
136 public void actionPerformed(java.awt.event.ActionEvent evt) {
137 butAddDataActionPerformed(evt);
138 }
139 });
140
141 panInButData.add(butAddData);
142
143 butDeleteData.setText(java.util.ResourceBundle.getBundle("res/MediBundle").getString("Delete"));
144 butDeleteData.setToolTipText(java.util.ResourceBundle.getBundle("res/MediBundle").getString("Deletes_a_data_item"));
145 butDeleteData.setMaximumSize(new java.awt.Dimension(85, 23));
146 butDeleteData.setPreferredSize(new java.awt.Dimension(85, 23));
147 butDeleteData.addActionListener(new java.awt.event.ActionListener() {
148 public void actionPerformed(java.awt.event.ActionEvent evt) {
149 butDeleteDataActionPerformed(evt);
150 }
151 });
152
153 panInButData.add(butDeleteData);
154
155 panButData.add(panInButData, java.awt.BorderLayout.NORTH);
156
157 panDataList.add(panButData, java.awt.BorderLayout.EAST);
158
159 sppDataView.setTopComponent(panDataList);
160
161 add(sppDataView, java.awt.BorderLayout.CENTER);
162
163 }//GEN-END:initComponents
164
165 private void butApplyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_butApplyActionPerformed
166 // Add your handling code here:
167 }//GEN-LAST:event_butApplyActionPerformed
168
169 private void butDeleteDataActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_butDeleteDataActionPerformed
170 // Add your handling code here:
171 int selRow, result;
172 Long dataID;
173
174 result = javax.swing.JOptionPane.showConfirmDialog(null,
175 java.util.ResourceBundle.getBundle("res/MediBundle").getString("Are_you_sure_to_delete_this_data?"),
176 java.util.ResourceBundle.getBundle("res/MediBundle").getString("Confirm_data_deletion"), javax.swing.JOptionPane.YES_NO_OPTION,
177 javax.swing.JOptionPane.QUESTION_MESSAGE);
178
179 if (result != javax.swing.JOptionPane.YES_OPTION)
180 return;
181
182 try {
183 selRow = tabDataList.getSelectedRow();
184 if (selRow >= 0) {
185 dataID = (Long) dataSorter.getIndex(selRow);
186 prv.removeData(dataID);
187 dataSorter.removeRow(selRow);
188 }
189 }
190 catch (DbException e) {
191 System.out.println(e.getMessage());
192 }
193 }//GEN-LAST:event_butDeleteDataActionPerformed
194
195 private void butAddDataActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_butAddDataActionPerformed
196 // Add your handling code here:
197 java.text.SimpleDateFormat formatter;
198 Long dataID;
199 Object[] tempVector;
200 javatools.util.FormattedDate currentDate;
201
202 formatter = prv.getDateFormat();
203 try {
204 currentDate = new javatools.util.FormattedDate();
205 dataID = new Long(prv.createNewData(new Integer(0), java.util.ResourceBundle.getBundle("res/MediBundle").getString("<Unnamed>"), java.util.ResourceBundle.getBundle("res/MediBundle").getString("<Unnamed>"),
206 formatter.format(currentDate.getDate()), new Long(0L)));
207 this.linkNewData(dataID);
208 /*tempVector = new Object[5];
209 tempVector[0] = "<Unnamed>";
210 tempVector[1] = "<Unnamed>";
211 tempVector[2] = "";
212 tempVector[3] = currentDate;
213 tempVector[4] = new javatools.util.FormattedLong(0L);
214 dataModel.addRow(tempVector, dataID); */
215 showItems();
216 }
217 catch (DbException e) {
218 System.out.println(e.getMessage());
219 }
220 }//GEN-LAST:event_butAddDataActionPerformed
221
222 // Variables declaration - do not modify//GEN-BEGIN:variables
223 private javax.swing.JLabel lblDataList;
224 private javax.swing.JPanel panButData;
225 private javax.swing.JButton butAddData;
226 private javax.swing.JScrollPane scpDataList;
227 private javax.swing.JPanel panDataList;
228 private javax.swing.JTable tabDataList;
229 private javax.swing.JButton butDeleteData;
230 private javax.swing.JPanel panInButData;
231 private javax.swing.JSplitPane sppDataView;
232 // End of variables declaration//GEN-END:variables
233
234 private javatools.swing.table.IndexedTableModel dataModel;
235 private javatools.swing.table.IndexedTableSorter dataSorter;
236 private DataDetailsPanel panDataDetailsView;
237
238 private int[] dataIndexes = {2, 3, 4, 5, 6};
239
240 private void initSelectionListeners() {
241 javax.swing.ListSelectionModel rowSM;
242
243 rowSM = tabDataList.getSelectionModel();
244 rowSM.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
245 public void valueChanged(javax.swing.event.ListSelectionEvent e) {
246 //Ignore extra messages.
247 int selectedRow;
248 Long tempDataID;
249
250 if (e.getValueIsAdjusting()) return;
251
252 javax.swing.ListSelectionModel lsm =
253 (javax.swing.ListSelectionModel)e.getSource();
254 if (!lsm.isSelectionEmpty()) {
255 selectedRow = lsm.getMinSelectionIndex();
256 panDataDetailsView.setDataIDs(buildDataIndexes(tabDataList.getSelectedRows()));
257 panDataDetailsView.showData();
258 }
259 }
260 });
261 }
262
263 private Long[] buildDataIndexes(int[] selRows) {
264 int i, numIndexes;
265 Long[] indexes;
266
267 numIndexes = selRows.length;
268 indexes = new Long[numIndexes];
269 for (i=0; i < numIndexes; i++) {
270 indexes[i] = (Long) dataSorter.getIndex(selRows[i]);
271 }
272 return indexes;
273 }
274
275 private void initTableSizes() {
276 initDataTableSizes();
277 }
278
279 private void initDataTableSizes() {
280 javax.swing.table.TableColumn column;
281
282 column = tabDataList.getColumnModel().getColumn(0);
283 column.setPreferredWidth(200);
284 column.setWidth(200);
285 column = tabDataList.getColumnModel().getColumn(1);
286 column.setPreferredWidth(300);
287 column.setWidth(300);
288 column = tabDataList.getColumnModel().getColumn(2);
289 column.setPreferredWidth(100);
290 column.setWidth(100);
291 column = tabDataList.getColumnModel().getColumn(3);
292 column.setPreferredWidth(200);
293 column.setWidth(200);
294 column = tabDataList.getColumnModel().getColumn(4);
295 column.setPreferredWidth(100);
296 column.setWidth(100);
297 }
298
299 private javatools.swing.table.IndexedTableSorter buildDataModel() {
300 dataModel = buildGenericDataModel();
301 dataSorter = new javatools.swing.table.IndexedTableSorter(dataModel);
302 dataSorter.addMouseListenerToHeaderInTable(tabDataList);
303 return dataSorter;
304 }
305
306 private javatools.swing.table.IndexedTableModel buildGenericDataModel() {
307 return new javatools.swing.table.IndexedTableModel(
308 new Object [][] {
309
310 },
311 new String [] {
312 java.util.ResourceBundle.getBundle("res/MediBundle").getString("Name"), java.util.ResourceBundle.getBundle("res/MediBundle").getString("Filename"), java.util.ResourceBundle.getBundle("res/MediBundle").getString("Type"), java.util.ResourceBundle.getBundle("res/MediBundle").getString("Date"), java.util.ResourceBundle.getBundle("res/MediBundle").getString("Dimension")
313 }
314 ) {
315 Class[] types = new Class [] {
316 java.lang.String.class, java.lang.String.class,
317 java.lang.String.class, javatools.util.FormattedDate.class,
318 javatools.util.FormattedNumber.class
319 };
320
321 boolean[] canEdit = new boolean [] {
322 false, false, false, false, false
323 };
324
325 public Class getColumnClass(int columnIndex) {
326 return types [columnIndex];
327 }
328
329 public boolean isCellEditable(int rowIndex, int columnIndex) {
330 return canEdit [columnIndex];
331 }
332 };
333 }
334 }