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