View Javadoc

1   package org.devaki.nextobjects.ui.workspace.models;
2   /*
3   
4   nextobjects Copyright (C) 2001-2005 Emmanuel Florent
5   
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by the
8   Free Software Foundation; either version 2 of the License, or (at your
9   option) any later version.
10  
11  This program is distributed in the hope that it will
12  be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
13  of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14  PURPOSE. See the GNU General Public License for more details.
15  
16  You should have received a copy of the GNU General Public License along
17  with this program; if not, write to the Free Software Foundation, Inc., 59
18  Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  
20  */
21  import java.util.Vector;
22  import java.util.Iterator;
23  import java.util.StringTokenizer;
24  import java.awt.GridBagConstraints;
25  import java.awt.GridBagLayout;
26  import java.awt.Insets;
27  import java.awt.event.ActionEvent;
28  import java.awt.event.ActionListener;
29  import java.awt.event.KeyAdapter;
30  import java.awt.event.KeyEvent;
31  import java.awt.event.MouseAdapter;
32  import java.awt.event.MouseEvent;
33  import javax.swing.BorderFactory;
34  import javax.swing.JPanel;
35  import javax.swing.JScrollPane;
36  import javax.swing.table.AbstractTableModel;
37  import javax.swing.DefaultListSelectionModel;
38  import javax.swing.table.JTableHeader;
39  import javax.swing.table.TableColumnModel;
40  import org.devaki.nextobjects.constants.CstImages;
41  import org.devaki.nextobjects.ui.components.CustomButton;
42  import org.devaki.nextobjects.ui.components.CustomTable;
43  import org.apache.maven.project.Contributor;
44  /***
45   * This class provide a jtable representinga set of Maven Contributor
46   * This class is used in the model edit window.
47   *
48   * @author <a href="mailto:eflorent@devaki.org">Emmanuel Florent</a>
49   */
50  public class ContributorEdit extends JPanel
51  {
52      /***
53       * Selected row in the jTable
54       */
55      private int selectedRow = -1;
56      /***
57       * The previous (UP) button
58       */
59      private CustomButton jButtonPrevious =
60          new CustomButton(CstImages.ICN_UP, "Move the selected field up", true);
61      /***
62       * The next (DOWN) button
63       */
64      private CustomButton jButtonNext =
65          new CustomButton(
66              CstImages.ICN_DOWN,
67              "Move the selected field down",
68              true);
69      /***
70       * The remove button
71       */
72      private CustomButton jButtonRemove =
73          new CustomButton("Remove", "Remove the selected field", true, false);
74      /***
75       * The new button
76       */
77      private CustomButton jButtonNew =
78          new CustomButton("New", "Create a new field", true, false);
79      /***
80       * The table for columns
81       */
82      private CustomTable jTableContributors =
83          new CustomTable(
84              false,
85              DefaultListSelectionModel.SINGLE_SELECTION,
86              false,
87              false);
88      /***
89       * The table model
90       */
91      private ObjectContributorTableModel model =
92          new ObjectContributorTableModel();
93      /***
94       * Constructor
95       *
96       */
97      public ContributorEdit()
98      {
99          super(new GridBagLayout());
100         // Define table model
101         this.jTableContributors.setModel(this.model);
102         // Set tool tips for the table header
103         this.jTableContributors.setTableHeader(
104             new ToolTipHeader(this.jTableContributors.getColumnModel()));
105         // Fix columns width
106         this.setColumnsWidth();
107         // 'jPanelGeneralFields'
108         this.add(
109             new JScrollPane(this.jTableContributors),
110             new GridBagConstraints(
111                 0,
112                 0,
113                 5,
114                 1,
115                 1.0,
116                 1.0,
117                 GridBagConstraints.CENTER,
118                 GridBagConstraints.BOTH,
119                 new Insets(5, 5, 0, 5),
120                 0,
121                 0));
122         this.add(
123             this.jButtonNew,
124             new GridBagConstraints(
125                 0,
126                 1,
127                 1,
128                 1,
129                 0.0,
130                 0.0,
131                 GridBagConstraints.CENTER,
132                 GridBagConstraints.NONE,
133                 new Insets(5, 5, 5, 5),
134                 0,
135                 0));
136         this.add(
137             this.jButtonRemove,
138             new GridBagConstraints(
139                 1,
140                 1,
141                 1,
142                 1,
143                 0.0,
144                 0.0,
145                 GridBagConstraints.CENTER,
146                 GridBagConstraints.NONE,
147                 new Insets(5, 0, 5, 5),
148                 0,
149                 0));
150         this.add(
151             this.jButtonPrevious,
152             new GridBagConstraints(
153                 2,
154                 1,
155                 1,
156                 1,
157                 1.0,
158                 0.0,
159                 GridBagConstraints.EAST,
160                 GridBagConstraints.NONE,
161                 new Insets(5, 0, 5, 5),
162                 0,
163                 0));
164         this.add(
165             this.jButtonNext,
166             new GridBagConstraints(
167                 3,
168                 1,
169                 1,
170                 1,
171                 0.0,
172                 0.0,
173                 GridBagConstraints.CENTER,
174                 GridBagConstraints.NONE,
175                 new Insets(5, 0, 5, 5),
176                 0,
177                 0));
178         // Define table model
179         this.jTableContributors.setModel(this.model);
180         // Set tool tips for the table header
181         this.jTableContributors.setTableHeader(
182             new ToolTipHeader(this.jTableContributors.getColumnModel()));
183         this.jButtonPrevious.addActionListener(new ActionListener()
184         {
185             // When calling 'jButtonPrevious', invert the current field with the
186             // previous field in 'jTableField'
187             public final void actionPerformed(final ActionEvent e)
188             {
189                 // It can be done, only if the current field is not the first in
190                 // 'jTableField'
191                 if (selectedRow > 0)
192                 {
193                     model.insertContributor(
194                         (Contributor) model.getContributors().elementAt(
195                             selectedRow - 1),
196                         selectedRow + 1);
197                     model.removeField(selectedRow - 1);
198                     jTableContributors.setSelectedRow(--selectedRow);
199                 }
200             }
201         });
202         this.jButtonNext.addActionListener(new ActionListener()
203         {
204             // When calling 'jButtonNext', invert the current
205             // field with the next field in 'jTableField'
206             public final void actionPerformed(final ActionEvent e)
207             {
208                 // It can be done, only if the current field is not the last in
209                 // 'jTableField' and that it is different from -1
210                 if ((selectedRow > -1)
211                     && (selectedRow < (jTableContributors.getRowCount() - 1)))
212                 {
213                     model.insertContributor(
214                         (Contributor) model.getContributors().elementAt(
215                             selectedRow),
216                         selectedRow + 2);
217                     model.removeField(selectedRow);
218                     jTableContributors.setSelectedRow(++selectedRow);
219                 }
220             }
221         });
222         this.jButtonRemove.addActionListener(new ActionListener()
223         {
224             // When calling 'jButtonRemove', remove the current field
225             public final void actionPerformed(final ActionEvent e)
226             {
227                 model.removeField(selectedRow);
228                 // If the removed field was in first position and
229                 // that there is always fields, set 'selectedRow' to zero
230                 if ((jTableContributors.getRowCount() > 0)
231                     && (selectedRow == 0))
232                 {
233                     jTableContributors.setSelectedRow(0);
234                 }
235                 // Else decrement 'selectedRow' and apply it to 'jTableField'
236                 else
237                 {
238                     jTableContributors.setSelectedRow(--selectedRow);
239                 }
240                 // FIX : Reset the columns width due to an unknow resize call
241                 //setColumnsWidth();
242                 // Disable 'jButtonRemove' if there is no selected row
243                 if (selectedRow == -1)
244                 {
245                     jButtonRemove.setEnabled(false);
246                 }
247             }
248         });
249         this.jButtonNew.addActionListener(new ActionListener()
250         {
251             // When calling 'jButtonNew', add a field
252             public final void actionPerformed(final ActionEvent e)
253             {
254                 // Create a new 'Dependency' object
255                 Contributor dep = new Contributor();
256                 // Add it to 'jTableField'
257                 model.addContributor(dep);
258                 // Update 'selectedRow'
259                 selectedRow = jTableContributors.getRowCount() - 1;
260                 // Set the selected field in 'jTableField'
261                 jTableContributors.setSelectedRow(selectedRow);
262                 // Enable 'jButtonRemove'
263                 jButtonRemove.setEnabled(true);
264             }
265         });
266         // MOUSE
267         this.jTableContributors.addMouseListener(new MouseAdapter()
268         {
269             // When clicking on 'jTableField'...
270             public final void mouseClicked(final MouseEvent e)
271             {
272                 // Set 'selectedRow'
273                 selectedRow = jTableContributors.getSelectedRow();
274                 // Enable 'jButtonRemove'
275                 jButtonRemove.setEnabled(true);
276             }
277         });
278         // KEY
279         this.jTableContributors.addKeyListener(new KeyAdapter()
280         {
281             // When selecting a row of 'jTableField' using the keyboard...
282             public final void keyReleased(final KeyEvent e)
283             {
284                 // Set 'selectedRow'
285                 selectedRow = jTableContributors.getSelectedRow();
286                 // Enable 'jButtonRemove'
287                 jButtonRemove.setEnabled(true);
288             }
289         });
290         this.setBorder(BorderFactory.createTitledBorder("Contributors"));
291         // Fix columns width
292         this.setColumnsWidth();
293     }
294     /***
295      * Unused: Cancel edition if 'DataDictionaryEdit' was
296      * edited in its previous call
297      */
298     public final void cancelCellEditing()
299     {
300         if (this.jTableContributors.isEditing())
301         {
302             int tmpInt1 = this.jTableContributors.getEditingRow();
303             int tmpInt2 = this.jTableContributors.getEditingColumn();
304             this
305                 .jTableContributors
306                 .getCellEditor(tmpInt1, tmpInt2)
307                 .cancelCellEditing();
308         }
309     }
310     /***
311      * Reset 'jTableField' columns width
312      */
313     private void setColumnsWidth()
314     {
315         for (int i = 0; i < this.model.getColumnCount(); i++)
316         {
317             this.jTableContributors.getColumnModel().getColumn(
318                 i).setPreferredWidth(
319                 this.model.getColumnSize(i));
320         }
321     }
322     /***
323      * set the ObjectContributorTableModel
324      * @param pModel ObjectContributorTableModel
325      */
326     protected final void setModel(final ObjectContributorTableModel pModel)
327     {
328         this.model = pModel;
329     }
330     /***
331      * get the ObjectContributorTableModel
332      * @return ObjectContributorTableModel
333      */
334     protected final ObjectContributorTableModel getModel()
335     {
336         return model;
337     }
338     /***
339      * This class define a model to edit the object columns
340      */
341     public final class ObjectContributorTableModel extends AbstractTableModel
342     {
343         /*** The data stored to be restored with cancel button */
344         private Vector oldData = new Vector();
345         /***
346          * The column name
347          */
348         private final String[] columnNames =
349             { "name", "email", "organization", "roles" };
350         /***
351          * The column class/types
352          */
353         private final Class[] columnClasses =
354             { String.class, String.class, String.class, String.class, };
355         /***
356          * The column size
357          */
358         private final int[] columnSizes = { 185, 185, 62, 60 };
359         /***
360          * The columns
361          */
362         private Vector contributors = new Vector(1);
363         /***
364          * Return the data contained in the JTable
365          * @return colums
366          */
367         public final Vector getContributors()
368         {
369             return contributors;
370         }
371         /***
372          * Reset the TableModel to its old values
373          */
374         public final void resetContributors()
375         {
376             cancelCellEditing();
377             this.contributors = new Vector(oldData);
378         }
379         /***
380          * Set data in the JTable
381          * @param pData the columns
382          */
383         public final void setContributors(final Vector pData)
384         {
385             // store the old data
386             oldData.removeAllElements();
387             for (int i = 0; i < pData.size(); i++)
388             {
389                 oldData.addElement(new Contributor());
390             }
391             //fix the new data.
392             this.contributors = new Vector(pData);
393         }
394         /***
395          * Add a field
396          * @param pContributor the column
397          */
398         public final void addContributor(final Contributor pContributor)
399         {
400             this.contributors.addElement(pContributor);
401             this.fireTableRowsInserted(
402                 this.contributors.size() - 1,
403                 this.contributors.size() - 1);
404         }
405         /***
406          * Add a field at a particular index
407          * @param pContributor the column
408          * @param i index
409          */
410         public final void insertContributor(
411             final Contributor pContributor,
412             final int i)
413         {
414             this.contributors.insertElementAt(pContributor, i);
415             this.fireTableRowsInserted(
416                 this.contributors.size() - 1,
417                 this.contributors.size() - 1);
418         }
419         /***
420          * Remove a field at a particular index
421          * @param i index
422          */
423         public final void removeField(final int i)
424         {
425             if (i >= 0 && i < contributors.size())
426             {
427                 this.contributors.removeElementAt(i);
428                 this.fireTableRowsDeleted(
429                     this.contributors.size() - 1,
430                     this.contributors.size() - 1);
431             }
432         }
433         /***
434          * Return the number of columns
435          * @return count
436          */
437         public final int getColumnCount()
438         {
439             return this.columnNames.length;
440         }
441         /***
442          * Return the number of lines
443          * @return the count of row, number of columns.
444          */
445         public final int getRowCount()
446         {
447             return this.contributors.size();
448         }
449         /***
450          * Return the name of the column at the specified index
451          * @param i index
452          * @return column
453          */
454         public final String getColumnName(final int i)
455         {
456             return this.columnNames[i];
457         }
458         /***
459          * Return the class of the column at the specified index
460          * @param i index
461          * @return class
462          */
463         public final Class getColumnClass(final int i)
464         {
465             return this.columnClasses[i];
466         }
467         /***
468          * Return the size of the column at the specified index
469          * @param i index
470          * @return the size
471          */
472         public final int getColumnSize(final int i)
473         {
474             return this.columnSizes[i];
475         }
476         /***
477          * Return the value at the specified row and column
478          * @param row row index
479          * @param col column index
480          * @return the value
481          */
482         public final Object getValueAt(final int row, final int col)
483         {
484             Contributor newContributor =
485                 (Contributor) contributors.elementAt(row);
486             switch (col)
487             {
488                 case 0 :
489                     return newContributor.getName();
490                 case 1 :
491                     return newContributor.getEmail();
492                 case 2 :
493                     return newContributor.getOrganization();
494                 case 3 :
495                     StringBuffer strRoles = new StringBuffer();
496                     Iterator itRoles = newContributor.getRoles().iterator();
497                     while (itRoles.hasNext())
498                     {
499                         strRoles.append(itRoles.next().toString());
500                         strRoles.append(";");
501                     }
502                     return strRoles;
503                 default :
504                     return null;
505             }
506         }
507         /***
508          * Set the specified value at the specified row and column
509          * @param value value
510          * @param row row index
511          * @param col column index
512          */
513         public final void setValueAt(
514             final Object value,
515             final int row,
516             final int col)
517         {
518             Contributor newContributor =
519                 (Contributor) contributors.elementAt(row);
520             switch (col)
521             {
522                 case 0 :
523                     if (value.toString().length() != 0)
524                     {
525                         newContributor.setName(value.toString());
526                     }
527                     break;
528                 case 1 :
529                     newContributor.setEmail(value.toString());
530                     break;
531                 case 2 :
532                     newContributor.setOrganization(value.toString());
533                     break;
534                 case 3 :
535                     StringTokenizer tknizer =
536                         new StringTokenizer(value.toString(), ";");
537                     while (tknizer.hasMoreTokens())
538                     {
539                         newContributor.addRole(tknizer.nextToken());
540                     }
541                     break;
542             }
543             // Replace the previous element
544             this.contributors.setElementAt(newContributor, row);
545             this.fireTableDataChanged();
546             // Set the selected row for the jTableField
547             jTableContributors.setSelectedRow(row);
548         }
549         /***
550          * Return that every cell is editable
551          * @param row row index
552          * @param col column index
553          * @return is editable
554          */
555         public final boolean isCellEditable(final int row, final int col)
556         {
557             return true;
558         }
559     } // End of class 'EntityFieldTableModel'
560     /***
561      * Define the tooltips of the 'jTableField' header
562      */
563     private class ToolTipHeader extends JTableHeader
564     {
565         /***
566          * The tooltips
567          */
568         private String[] toolTips =
569             {
570                 " The full name of the contributor.",
571                 " The email address of the contributor.",
572                 " The organization to which the contributor belongs.",
573                 " The roles the contributor plays in the project." };
574         /***
575         * Construct a new 'ToolTipHeader' object
576         * @param pModel the TableColumnModel
577         */
578         public ToolTipHeader(final TableColumnModel pModel)
579         {
580             super(pModel);
581         }
582         /***
583          * Return the tool tip depending on the mouse cursor position
584          * @param e the event
585          * @return the tooltip
586          */
587         public final String getToolTipText(final MouseEvent e)
588         {
589             int col = this.columnAtPoint(e.getPoint());
590             int modelCol = this.getTable().convertColumnIndexToModel(col);
591             return toolTips[modelCol];
592         }
593     }
594 }