1 package org.devaki.nextobjects.ui.workspace.models;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 import java.util.Vector;
22 import java.awt.GridBagConstraints;
23 import java.awt.GridBagLayout;
24 import java.awt.Insets;
25 import java.awt.event.ActionEvent;
26 import java.awt.event.ActionListener;
27 import java.awt.event.KeyAdapter;
28 import java.awt.event.KeyEvent;
29 import java.awt.event.MouseAdapter;
30 import java.awt.event.MouseEvent;
31 import javax.swing.BorderFactory;
32 import javax.swing.JPanel;
33 import javax.swing.JScrollPane;
34 import javax.swing.table.AbstractTableModel;
35 import javax.swing.DefaultListSelectionModel;
36 import javax.swing.table.JTableHeader;
37 import javax.swing.table.TableColumnModel;
38 import org.devaki.nextobjects.constants.CstImages;
39 import org.devaki.nextobjects.ui.components.CustomButton;
40 import org.devaki.nextobjects.ui.components.CustomTable;
41 import org.apache.maven.project.License;
42 /***
43 * This class provide a jtable representing a set of License
44 * Here the columns are build upon the Maven License
45 * object. This class is used in the model edit window.
46 *
47 * @author <a href="mailto:eflorent@devaki.org">Emmanuel Florent</a>
48 */
49 public class LicenseEdit extends JPanel
50 {
51 /***
52 * Selected row in the jTable
53 */
54 private int selectedRow = -1;
55 /***
56 * The previous (UP) button
57 */
58 private CustomButton jButtonPrevious =
59 new CustomButton(CstImages.ICN_UP, "Move the selected field up", true);
60 /***
61 * The next (DOWN) button
62 */
63 private CustomButton jButtonNext =
64 new CustomButton(
65 CstImages.ICN_DOWN,
66 "Move the selected field down",
67 true);
68 /***
69 * The remove button
70 */
71 private CustomButton jButtonRemove =
72 new CustomButton("Remove", "Remove the selected field", true, false);
73 /***
74 * The new button
75 */
76 private CustomButton jButtonNew =
77 new CustomButton("New", "Create a new field", true, false);
78 /***
79 * The table for columns
80 */
81 private CustomTable jTableLicenses =
82 new CustomTable(
83 false,
84 DefaultListSelectionModel.SINGLE_SELECTION,
85 false,
86 false);
87 /***
88 * The table model
89 */
90 private ObjectLicenseTableModel model = new ObjectLicenseTableModel();
91 /***
92 * Constructor
93 *
94 */
95 public LicenseEdit()
96 {
97 super(new GridBagLayout());
98
99 this.jTableLicenses.setModel(this.model);
100
101 this.jTableLicenses.setTableHeader(
102 new ToolTipHeader(this.jTableLicenses.getColumnModel()));
103
104 this.setColumnsWidth();
105
106 this.add(
107 new JScrollPane(this.jTableLicenses),
108 new GridBagConstraints(
109 0,
110 0,
111 5,
112 1,
113 1.0,
114 1.0,
115 GridBagConstraints.CENTER,
116 GridBagConstraints.BOTH,
117 new Insets(5, 5, 0, 5),
118 0,
119 0));
120 this.add(
121 this.jButtonNew,
122 new GridBagConstraints(
123 0,
124 1,
125 1,
126 1,
127 0.0,
128 0.0,
129 GridBagConstraints.CENTER,
130 GridBagConstraints.NONE,
131 new Insets(5, 5, 5, 5),
132 0,
133 0));
134 this.add(
135 this.jButtonRemove,
136 new GridBagConstraints(
137 1,
138 1,
139 1,
140 1,
141 0.0,
142 0.0,
143 GridBagConstraints.CENTER,
144 GridBagConstraints.NONE,
145 new Insets(5, 0, 5, 5),
146 0,
147 0));
148 this.add(
149 this.jButtonPrevious,
150 new GridBagConstraints(
151 2,
152 1,
153 1,
154 1,
155 1.0,
156 0.0,
157 GridBagConstraints.EAST,
158 GridBagConstraints.NONE,
159 new Insets(5, 0, 5, 5),
160 0,
161 0));
162 this.add(
163 this.jButtonNext,
164 new GridBagConstraints(
165 3,
166 1,
167 1,
168 1,
169 0.0,
170 0.0,
171 GridBagConstraints.CENTER,
172 GridBagConstraints.NONE,
173 new Insets(5, 0, 5, 5),
174 0,
175 0));
176
177 this.jTableLicenses.setModel(this.model);
178
179 this.jTableLicenses.setTableHeader(
180 new ToolTipHeader(this.jTableLicenses.getColumnModel()));
181
182
183 this.jButtonPrevious.addActionListener(new ActionListener()
184 {
185
186
187 public final void actionPerformed(final ActionEvent e)
188 {
189
190
191 if (selectedRow > 0)
192 {
193 model.insertLicense(
194 (License) model.getLicenses().elementAt(
195 selectedRow - 1),
196 selectedRow + 1);
197 model.removeField(selectedRow - 1);
198 jTableLicenses.setSelectedRow(--selectedRow);
199 }
200 }
201 });
202 this.jButtonNext.addActionListener(new ActionListener()
203 {
204
205
206 public final void actionPerformed(final ActionEvent e)
207 {
208
209
210 if ((selectedRow > -1)
211 && (selectedRow < (jTableLicenses.getRowCount() - 1)))
212 {
213 model.insertLicense(
214 (License) model.getLicenses().elementAt(selectedRow),
215 selectedRow + 2);
216 model.removeField(selectedRow);
217 jTableLicenses.setSelectedRow(++selectedRow);
218 }
219 }
220 });
221 this.jButtonRemove.addActionListener(new ActionListener()
222 {
223
224 public final void actionPerformed(final ActionEvent e)
225 {
226 model.removeField(selectedRow);
227
228
229 if ((jTableLicenses.getRowCount() > 0) && (selectedRow == 0))
230 {
231 jTableLicenses.setSelectedRow(0);
232 }
233
234 else
235 {
236 jTableLicenses.setSelectedRow(--selectedRow);
237 }
238
239
240
241 if (selectedRow == -1)
242 {
243 jButtonRemove.setEnabled(false);
244 }
245 }
246 });
247 this.jButtonNew.addActionListener(new ActionListener()
248 {
249
250 public final void actionPerformed(final ActionEvent e)
251 {
252
253 License dep = new License();
254
255 model.addLicense(dep);
256
257 selectedRow = jTableLicenses.getRowCount() - 1;
258
259 jTableLicenses.setSelectedRow(selectedRow);
260
261 jButtonRemove.setEnabled(true);
262 }
263 });
264
265 this.jTableLicenses.addMouseListener(new MouseAdapter()
266 {
267
268 public final void mouseClicked(final MouseEvent e)
269 {
270
271 selectedRow = jTableLicenses.getSelectedRow();
272
273 jButtonRemove.setEnabled(true);
274 }
275 });
276
277 this.jTableLicenses.addKeyListener(new KeyAdapter()
278 {
279
280 public final void keyReleased(final KeyEvent e)
281 {
282
283 selectedRow = jTableLicenses.getSelectedRow();
284
285 jButtonRemove.setEnabled(true);
286 }
287 });
288 this.setBorder(BorderFactory.createTitledBorder("Licenses"));
289
290 this.setColumnsWidth();
291 }
292 /***
293 * Unused: Cancel edition if 'DataDictionaryEdit' was
294 * edited in its previous call
295 */
296 public final void cancelCellEditing()
297 {
298 if (this.jTableLicenses.isEditing())
299 {
300 int tmpInt1 = this.jTableLicenses.getEditingRow();
301 int tmpInt2 = this.jTableLicenses.getEditingColumn();
302 this
303 .jTableLicenses
304 .getCellEditor(tmpInt1, tmpInt2)
305 .cancelCellEditing();
306 }
307 }
308 /***
309 * Reset 'jTableField' columns width
310 */
311 private void setColumnsWidth()
312 {
313 for (int i = 0; i < this.model.getColumnCount(); i++)
314 {
315 this.jTableLicenses.getColumnModel().getColumn(
316 i).setPreferredWidth(
317 this.model.getColumnSize(i));
318 }
319 }
320 /***
321 * Set the Object License Table Model
322 * @param pModel the Object License Table Model
323 */
324 protected final void setModel(final ObjectLicenseTableModel pModel)
325 {
326 this.model = pModel;
327 }
328 /***
329 * Fet the Object License Table Model
330 * @return the Object License Table Model
331 */
332 protected final ObjectLicenseTableModel getModel()
333 {
334 return model;
335 }
336 /***
337 * This class define a model to edit the object columns
338 */
339 public final class ObjectLicenseTableModel extends AbstractTableModel
340 {
341 /*** The data stored to be restored with cancel button */
342 private Vector oldData = new Vector();
343 /***
344 * The column name
345 */
346 private final String[] columnNames = { "name", "URL", "distribution", };
347 /***
348 * The column class/types
349 */
350 private final Class[] columnClasses =
351 { String.class, String.class, String.class };
352 /***
353 * The column size
354 */
355 private final int[] columnSizes = { 160, 183, 150 };
356 /***
357 * The columns
358 */
359 private Vector licenses = new Vector(1);
360 /***
361 * Return the data contained in the JTable
362 * @return colums
363 */
364 public final Vector getLicenses()
365 {
366 return licenses;
367 }
368 /***
369 * Reset the TableModel to its old values
370 */
371 public final void resetLicenses()
372 {
373 cancelCellEditing();
374 this.licenses = new Vector(oldData);
375 }
376 /***
377 * Set data in the JTable
378 * @param pData the columns
379 */
380 public final void setLicenses(final Vector pData)
381 {
382
383 oldData.removeAllElements();
384 for (int i = 0; i < pData.size(); i++)
385 {
386 oldData.addElement(new License());
387 }
388
389 this.licenses = new Vector(pData);
390 }
391 /***
392 * Add a dependency
393 * @param pDependency the column
394 */
395 public final void addLicense(final License pDependency)
396 {
397 this.licenses.addElement(pDependency);
398 this.fireTableRowsInserted(
399 this.licenses.size() - 1,
400 this.licenses.size() - 1);
401 }
402 /***
403 * Add a field at a particular index
404 * @param pLicense the column
405 * @param i index
406 */
407 public final void insertLicense(final License pLicense, final int i)
408 {
409 this.licenses.insertElementAt(pLicense, i);
410 this.fireTableRowsInserted(
411 this.licenses.size() - 1,
412 this.licenses.size() - 1);
413 }
414 /***
415 * Remove a field at a particular index
416 * @param i index
417 */
418 public final void removeField(final int i)
419 {
420 if (i >= 0 && i < licenses.size())
421 {
422 this.licenses.removeElementAt(i);
423 this.fireTableRowsDeleted(
424 this.licenses.size() - 1,
425 this.licenses.size() - 1);
426 }
427 }
428 /***
429 * Return the number of columns
430 * @return count
431 */
432 public final int getColumnCount()
433 {
434 return this.columnNames.length;
435 }
436 /***
437 * Return the number of lines
438 * @return the count of row, number of columns.
439 */
440 public final int getRowCount()
441 {
442 return this.licenses.size();
443 }
444 /***
445 * Return the name of the column at the specified index
446 * @param i index
447 * @return column
448 */
449 public final String getColumnName(final int i)
450 {
451 return this.columnNames[i];
452 }
453 /***
454 * Return the class of the column at the specified index
455 * @param i index
456 * @return class
457 */
458 public final Class getColumnClass(final int i)
459 {
460 return this.columnClasses[i];
461 }
462 /***
463 * Return the size of the column at the specified index
464 * @param i index
465 * @return the size
466 */
467 public final int getColumnSize(final int i)
468 {
469 return this.columnSizes[i];
470 }
471 /***
472 * Return the value at the specified row and column
473 * @param row row index
474 * @param col column index
475 * @return the value
476 */
477 public final Object getValueAt(final int row, final int col)
478 {
479 License newLicense = (License) licenses.elementAt(row);
480 switch (col)
481 {
482 case 0 :
483 return newLicense.getName();
484 case 1 :
485 return newLicense.getUrl();
486 case 2 :
487 return newLicense.getDistribution();
488 default :
489 return null;
490 }
491 }
492 /***
493 * Set the specified value at the specified row and column
494 * @param value value
495 * @param row row index
496 * @param col column index
497 */
498 public final void setValueAt(
499 final Object value,
500 final int row,
501 final int col)
502 {
503 License newLicense = (License) licenses.elementAt(row);
504 switch (col)
505 {
506 case 0 :
507 if (value.toString().length() != 0)
508 {
509 newLicense.setName(value.toString());
510 }
511 break;
512 case 1 :
513 newLicense.setUrl(value.toString());
514 break;
515 case 2 :
516 newLicense.setDistribution(value.toString());
517 break;
518 }
519
520 this.licenses.setElementAt(newLicense, row);
521 this.fireTableDataChanged();
522
523 jTableLicenses.setSelectedRow(row);
524 }
525 /***
526 * Return that every cell is editable
527 * @param row row index
528 * @param col column index
529 * @return is editable
530 */
531 public final boolean isCellEditable(final int row, final int col)
532 {
533 return true;
534 }
535 }
536 /***
537 * Define the tooltips of the 'jTableField' header
538 */
539 private class ToolTipHeader extends JTableHeader
540 {
541 /***
542 * The tooltips
543 */
544 private String[] toolTips =
545 {
546 "The full legal name of the license.",
547 "The official url for the license text.",
548 "The primary method by which this project may be distributed.\n"
549 + "repo: may be downloaded from the Maven repository.\n"
550 + "manual: user must manually download and install the "
551 + "dependency." };
552 /***
553 * Construct a new 'ToolTipHeader' object
554 * @param pModel the TableColumnModel
555 */
556 public ToolTipHeader(final TableColumnModel pModel)
557 {
558 super(pModel);
559 }
560 /***
561 * Return the tool tip depending on the mouse cursor position
562 * @param e the event
563 * @return the tooltip
564 */
565 public final String getToolTipText(final MouseEvent e)
566 {
567 int col = this.columnAtPoint(e.getPoint());
568 int modelCol = this.getTable().convertColumnIndexToModel(col);
569 return toolTips[modelCol];
570 }
571 }
572 }