View Javadoc

1   /*
2   
3   nextobjects Copyright (C) 2001-2005 Emmanuel Florent
4   
5   This program is free software; you can redistribute it and/or modify
6   it under the terms of the GNU General Public License as published by the
7   Free Software Foundation; either version 2 of the License, or (at your
8   option) any later version.
9   
10  This program is distributed in the hope that it will
11  be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
12  of MERCHANTABILITY or FITNESS FOR A PARTICULAR
13  PURPOSE. See the GNU General Public License for more details.
14  
15  You should have received a copy of the GNU General Public License along
16  with this program; if not, write to the Free Software Foundation, Inc., 59
17  Temple Place - Suite 330, Boston, MA 02111-1307, USA.
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          // Define table model
98          this.jTableDependencies.setModel(this.model);
99          // Set tool tips for the table header
100         this.jTableDependencies.setTableHeader(
101             new ToolTipHeader(this.jTableDependencies.getColumnModel()));
102         // Fix columns width
103         this.setColumnsWidth();
104         // 'jPanelGeneralFields'
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         // Define table model
176         this.jTableDependencies.setModel(this.model);
177         // Set tool tips for the table header
178         this.jTableDependencies.setTableHeader(
179             new ToolTipHeader(this.jTableDependencies.getColumnModel()));
180         // Fix columns width
181         //this.setColumnsWidth();
182         this.jButtonPrevious.addActionListener(new ActionListener()
183         {
184             // When calling 'jButtonPrevious', invert the current field with the
185             // previous field in 'jTableField'
186             public final void actionPerformed(final ActionEvent e)
187             {
188                 // It can be done, only if the current field is not the first in
189                 // 'jTableField'
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             // When calling 'jButtonNext', invert the current
204             //field with the next field in 'jTableField'
205             public final void actionPerformed(final ActionEvent e)
206             {
207                 // It can be done, only if the current field is not the last in
208                 // 'jTableField' and that it is different from -1
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             // When calling 'jButtonRemove', remove the current field
224             public final void actionPerformed(final ActionEvent e)
225             {
226                 model.removeField(selectedRow);
227                 // If the removed field was in first position and that
228                 // there is always fields, set 'selectedRow' to zero
229                 if ((jTableDependencies.getRowCount() > 0)
230                     && (selectedRow == 0))
231                 {
232                     jTableDependencies.setSelectedRow(0);
233                 }
234                 // Else decrement 'selectedRow' and apply it to 'jTableField'
235                 else
236                 {
237                     jTableDependencies.setSelectedRow(--selectedRow);
238                 }
239                 // FIX : Reset the columns width due to an unknow resize call
240                 //setColumnsWidth();
241                 // Disable 'jButtonRemove' if there is no selected row
242                 if (selectedRow == -1)
243                 {
244                     jButtonRemove.setEnabled(false);
245                 }
246             }
247         });
248         this.jButtonNew.addActionListener(new ActionListener()
249         {
250             // When calling 'jButtonNew', add a field
251             public final void actionPerformed(final ActionEvent e)
252             {
253                 // Create a new 'Dependency' object
254                 Dependency dep = new Dependency();
255                 // Add it to 'jTableField'
256                 model.addDependency(dep);
257                 // Update 'selectedRow'
258                 selectedRow = jTableDependencies.getRowCount() - 1;
259                 // Set the selected field in 'jTableField'
260                 jTableDependencies.setSelectedRow(selectedRow);
261                 // Enable 'jButtonRemove'
262                 jButtonRemove.setEnabled(true);
263             }
264         });
265         // MOUSE
266         this.jTableDependencies.addMouseListener(new MouseAdapter()
267         {
268             // When clicking on 'jTableField'...
269             public final void mouseClicked(final MouseEvent e)
270             {
271                 // Set 'selectedRow'
272                 selectedRow = jTableDependencies.getSelectedRow();
273                 // Enable 'jButtonRemove'
274                 jButtonRemove.setEnabled(true);
275             }
276         });
277         // KEY
278         this.jTableDependencies.addKeyListener(new KeyAdapter()
279         {
280             // When selecting a row of 'jTableField' using the keyboard...
281             public final void keyReleased(final KeyEvent e)
282             {
283                 // Set 'selectedRow'
284                 selectedRow = jTableDependencies.getSelectedRow();
285                 // Enable 'jButtonRemove'
286                 jButtonRemove.setEnabled(true);
287             }
288         });
289         this.setBorder(BorderFactory.createTitledBorder("Dependencies"));
290         // Fix columns width
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             // store the old data
384             oldData.removeAllElements();
385             for (int i = 0; i < pData.size(); i++)
386             {
387                 oldData.addElement(new Dependency());
388             }
389             //fix the new data.
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             // Replace the previous element
527             this.dependencies.setElementAt(newDependency, row);
528             this.fireTableDataChanged();
529             // Set the selected row for the jTableField
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     } // End of class 'EntityFieldTableModel'
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 }