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.awt.GridBagConstraints;
22 import java.awt.GridBagLayout;
23 import java.awt.Insets;
24 import java.awt.event.ActionEvent;
25 import java.awt.event.ActionListener;
26 import java.awt.event.KeyAdapter;
27 import java.awt.event.KeyEvent;
28 import java.awt.event.MouseAdapter;
29 import java.awt.event.MouseEvent;
30 import java.util.Iterator;
31 import java.util.StringTokenizer;
32 import java.util.Vector;
33 import javax.swing.BorderFactory;
34 import javax.swing.DefaultListSelectionModel;
35 import javax.swing.JPanel;
36 import javax.swing.JScrollPane;
37 import javax.swing.table.AbstractTableModel;
38 import javax.swing.table.JTableHeader;
39 import javax.swing.table.TableColumnModel;
40 import org.apache.maven.project.Resource;
41 import org.devaki.nextobjects.constants.CstImages;
42 import org.devaki.nextobjects.ui.components.CustomButton;
43 import org.devaki.nextobjects.ui.components.CustomTable;
44 /***
45 * This class provide a jtable representing Maven's Resources
46 *
47 * @author <a href="mailto:eflorent@devaki.org">Emmanuel Florent</a>
48 */
49 public class ResourceEdit 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 jTableResources =
82 new CustomTable(
83 false,
84 DefaultListSelectionModel.SINGLE_SELECTION,
85 false,
86 false);
87 /***
88 * The table model
89 */
90 private ObjectResourceTableModel model = new ObjectResourceTableModel();
91 /***
92 * Constructor
93 *
94 */
95 public ResourceEdit()
96 {
97 super(new GridBagLayout());
98
99 this.jTableResources.setModel(this.model);
100
101 this.jTableResources.setTableHeader(
102 new ToolTipHeader(this.jTableResources.getColumnModel()));
103
104 this.setColumnsWidth();
105
106 this.add(
107 new JScrollPane(this.jTableResources),
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.jTableResources.setModel(this.model);
178
179 this.jTableResources.setTableHeader(
180 new ToolTipHeader(this.jTableResources.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.insertResource(
194 (Resource) model.getResources().elementAt(
195 selectedRow - 1),
196 selectedRow + 1);
197 model.removeField(selectedRow - 1);
198 jTableResources.setSelectedRow(--selectedRow);
199 }
200 }
201 });
202 this.jButtonNext.addActionListener(new ActionListener()
203 {
204 public final void actionPerformed(final ActionEvent e)
205 {
206
207
208 if ((selectedRow > -1)
209 && (selectedRow < (jTableResources.getRowCount() - 1)))
210 {
211 model.insertResource(
212 (Resource) model.getResources().elementAt(selectedRow),
213 selectedRow + 2);
214 model.removeField(selectedRow);
215 jTableResources.setSelectedRow(++selectedRow);
216 }
217 }
218 });
219 this.jButtonRemove.addActionListener(new ActionListener()
220 {
221
222 public final void actionPerformed(final ActionEvent e)
223 {
224 model.removeField(selectedRow);
225
226
227 if ((jTableResources.getRowCount() > 0) && (selectedRow == 0))
228 {
229 jTableResources.setSelectedRow(0);
230 }
231
232 else
233 {
234 jTableResources.setSelectedRow(--selectedRow);
235 }
236
237
238
239 if (selectedRow == -1)
240 {
241 jButtonRemove.setEnabled(false);
242 }
243 }
244 });
245 this.jButtonNew.addActionListener(new ActionListener()
246 {
247
248 public final void actionPerformed(final ActionEvent e)
249 {
250
251 Resource dep = new Resource();
252
253 model.addResource(dep);
254
255 selectedRow = jTableResources.getRowCount() - 1;
256
257 jTableResources.setSelectedRow(selectedRow);
258
259 jButtonRemove.setEnabled(true);
260 }
261 });
262
263 this.jTableResources.addMouseListener(new MouseAdapter()
264 {
265
266 public final void mouseClicked(final MouseEvent e)
267 {
268
269 selectedRow = jTableResources.getSelectedRow();
270
271 jButtonRemove.setEnabled(true);
272 }
273 });
274
275 this.jTableResources.addKeyListener(new KeyAdapter()
276 {
277
278 public final void keyReleased(final KeyEvent e)
279 {
280
281 selectedRow = jTableResources.getSelectedRow();
282
283 jButtonRemove.setEnabled(true);
284 }
285 });
286 this.setBorder(BorderFactory.createTitledBorder("Resources"));
287
288 this.setColumnsWidth();
289 }
290 /***
291 * Unused: Cancel edition if 'DataDictionaryEdit' was
292 * edited in its previous call
293 */
294 public final void cancelCellEditing()
295 {
296 if (this.jTableResources.isEditing())
297 {
298 int tmpInt1 = this.jTableResources.getEditingRow();
299 int tmpInt2 = this.jTableResources.getEditingColumn();
300 this
301 .jTableResources
302 .getCellEditor(tmpInt1, tmpInt2)
303 .cancelCellEditing();
304 }
305 }
306 /***
307 * Reset 'jTableField' columns width
308 */
309 private void setColumnsWidth()
310 {
311 for (int i = 0; i < this.model.getColumnCount(); i++)
312 {
313 this.jTableResources.getColumnModel().getColumn(
314 i).setPreferredWidth(
315 this.model.getColumnSize(i));
316 }
317 }
318 /***
319 * This class define a model to edit the object columns
320 */
321 public class ObjectResourceTableModel extends AbstractTableModel
322 {
323 /*** The data stored to be restored with cancel button */
324 private Vector oldData = new Vector();
325 /***
326 * The column name
327 */
328 private final String[] columnNames =
329 { "directory", "target path", "includes", "excludes" };
330 /***
331 * The column class/types
332 */
333 private final Class[] columnClasses =
334 { String.class, String.class, String.class, String.class };
335 /***
336 * The column size
337 */
338 private final int[] columnSizes = { 170, 165, 80, 80 };
339 /***
340 * The columns
341 */
342 private Vector resources = new Vector(1);
343 /***
344 * Return the data contained in the JTable
345 * @return colums
346 */
347 public final Vector getResources()
348 {
349 return resources;
350 }
351 /***
352 * Reset the TableModel to its old values
353 */
354 public final void resetResources()
355 {
356 cancelCellEditing();
357 this.resources = new Vector(oldData);
358 }
359 /***
360 * Set data in the JTable
361 * @param pData the columns
362 */
363 public final void setResources(final Vector pData)
364 {
365
366 oldData.removeAllElements();
367 for (int i = 0; i < pData.size(); i++)
368 {
369 oldData.addElement(new Resource());
370 }
371
372 this.resources = new Vector(pData);
373 }
374 /***
375 * Add a field
376 * @param pResource the column
377 */
378 public final void addResource(final Resource pResource)
379 {
380 this.resources.addElement(pResource);
381 this.fireTableRowsInserted(
382 this.resources.size() - 1,
383 this.resources.size() - 1);
384 }
385 /***
386 * Add a field at a particular index
387 * @param pColumn the column
388 * @param i index
389 */
390 public final void insertResource(final Resource pColumn, final int i)
391 {
392 this.resources.insertElementAt(pColumn, i);
393 this.fireTableRowsInserted(
394 this.resources.size() - 1,
395 this.resources.size() - 1);
396 }
397 /***
398 * Remove a field at a particular index
399 * @param i index
400 */
401 public final void removeField(final int i)
402 {
403 if (i >= 0 && i < resources.size())
404 {
405 this.resources.removeElementAt(i);
406 this.fireTableRowsDeleted(
407 this.resources.size() - 1,
408 this.resources.size() - 1);
409 }
410 }
411 /***
412 * Return the number of columns
413 * @return count
414 */
415 public final int getColumnCount()
416 {
417 return this.columnNames.length;
418 }
419 /***
420 * Return the number of lines
421 * @return the count of row, number of columns.
422 */
423 public final int getRowCount()
424 {
425 return this.resources.size();
426 }
427 /***
428 * Return the name of the column at the specified index
429 * @param i index
430 * @return column
431 */
432 public final String getColumnName(final int i)
433 {
434 return this.columnNames[i];
435 }
436 /***
437 * Return the class of the column at the specified index
438 * @param i index
439 * @return class
440 */
441 public final Class getColumnClass(final int i)
442 {
443 return this.columnClasses[i];
444 }
445 /***
446 * Return the size of the column at the specified index
447 * @param i index
448 * @return the size
449 */
450 public final int getColumnSize(final int i)
451 {
452 return this.columnSizes[i];
453 }
454 /***
455 * Return the value at the specified row and column
456 * @param row row index
457 * @param col column index
458 * @return the value
459 */
460 public final Object getValueAt(final int row, final int col)
461 {
462 Resource newResource = (Resource) resources.elementAt(row);
463 switch (col)
464 {
465 case 0 :
466 return newResource.getDirectory();
467 case 1 :
468 return newResource.getTargetPath();
469 case 2 :
470 StringBuffer str = new StringBuffer();
471 Iterator itIncludes = newResource.getIncludes().iterator();
472 while (itIncludes.hasNext())
473 {
474 str.append(itIncludes.next().toString());
475 str.append(";");
476 }
477 return str;
478 case 3 :
479 StringBuffer strExclude = new StringBuffer();
480 Iterator itExcludes = newResource.getExcludes().iterator();
481 while (itExcludes.hasNext())
482 {
483 strExclude.append(itExcludes.next().toString());
484 strExclude.append(";");
485 }
486 return strExclude;
487 default :
488 return null;
489 }
490 }
491 /***
492 * Set the specified value at the specified row and column
493 * @param value value
494 * @param row row index
495 * @param col column index
496 */
497 public final void setValueAt(
498 final Object value,
499 final int row,
500 final int col)
501 {
502 Resource newResource = (Resource) resources.elementAt(row);
503 StringTokenizer tknizer = null;
504 switch (col)
505 {
506 case 0 :
507 newResource.setDirectory(value.toString());
508 break;
509 case 1 :
510 newResource.setTargetPath(value.toString());
511 break;
512 case 2 :
513 newResource.getIncludes().clear();
514 tknizer = new StringTokenizer(value.toString(), ";");
515 while (tknizer.hasMoreTokens())
516 {
517 newResource.addInclude(tknizer.nextToken());
518 }
519 break;
520 case 3 :
521 newResource.getExcludes().clear();
522 tknizer = new StringTokenizer(value.toString(), ";");
523 while (tknizer.hasMoreTokens())
524 {
525 newResource.addExclude(tknizer.nextToken());
526 }
527 break;
528 default :
529 break;
530 }
531
532 this.resources.setElementAt(newResource, row);
533 this.fireTableDataChanged();
534
535 jTableResources.setSelectedRow(row);
536 }
537 /***
538 * Return that every cell is editable
539 * @param row row index
540 * @param col column index
541 * @return is editable
542 */
543 public final boolean isCellEditable(final int row, final int col)
544 {
545 return true;
546 }
547 }
548 /***
549 * Define the tooltips of the 'jTableField' header
550 */
551 private class ToolTipHeader extends JTableHeader
552 {
553 /***
554 * The tooltips
555 */
556 private String[] toolTips =
557 {
558 "Related build",
559 "Describe the directory where the resource is stored.",
560 "Describe the resource target path. For example, if you"
561 + " want that resource appear into a specific package "
562 + "(org.apache.maven.messages ), you must specify "
563 + "this element with this value: "
564 + "org/apache/maven/messages",
565 "Describe pattern to includes of the resource (Ant Pattern)",
566 "Describe pattern to excludes of the resource (Ant Pattern)" };
567 /***
568 * Construct a new 'ToolTipHeader' object
569 * @param pModel the TableColumnModel
570 */
571 public ToolTipHeader(final TableColumnModel pModel)
572 {
573 super(pModel);
574 }
575 /***
576 * Return the tool tip depending on the mouse cursor position
577 * @param e the event
578 * @return the tooltip
579 */
580 public final String getToolTipText(final MouseEvent e)
581 {
582 int col = this.columnAtPoint(e.getPoint());
583 int modelCol = this.getTable().convertColumnIndexToModel(col);
584 return toolTips[modelCol];
585 }
586 }
587 /***
588 * Return the resources associated with the jTable
589 * @return model the resources as table model
590 */
591 public final ObjectResourceTableModel getModel()
592 {
593 return model;
594 }
595 /***
596 * Set resources associated to the jTable
597 * @param pModel the table model
598 */
599 public final void setModel(final ObjectResourceTableModel pModel)
600 {
601 this.model = pModel;
602 }
603 }