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.awt.GridBagConstraints;
23  import java.awt.GridBagLayout;
24  import java.awt.Insets;
25  import java.awt.event.KeyAdapter;
26  import java.awt.event.KeyEvent;
27  import java.awt.event.MouseAdapter;
28  import java.awt.event.MouseEvent;
29  import javax.swing.BorderFactory;
30  import javax.swing.JPanel;
31  import javax.swing.JScrollPane;
32  import javax.swing.table.AbstractTableModel;
33  import javax.swing.DefaultListSelectionModel;
34  import javax.swing.table.JTableHeader;
35  import javax.swing.table.TableColumnModel;
36  import org.apache.maven.project.Build;
37  import org.devaki.nextobjects.ui.components.CustomTable;
38  /***
39   * This class provide the build panel of the Model Editor
40   * @author <a href="mailto:eflorent@devaki.org">Emmanuel Florent</a>
41   */
42  public class BuildEdit extends JPanel
43  {
44      /***
45       * Selected row in the jTable
46       */
47      private int selectedRow = 0;
48      /***
49       * The table for columns
50       * the project do not yet offer the ability of managing
51       * ( DefaultListSelectionModel.SINGLE_SELECTION )
52       * multiples build as maven do.
53       */
54      private CustomTable jTableBuilds =
55          new CustomTable(
56              false,
57              DefaultListSelectionModel.SINGLE_SELECTION,
58              false,
59              false);
60      /***
61      * The table model
62      */
63      private ObjectBuildTableModel model = new ObjectBuildTableModel();
64      /***
65       * Constructor
66       *
67       */
68      public BuildEdit()
69      {
70          super(new GridBagLayout());
71          // Define table model
72          this.jTableBuilds.setModel(this.model);
73          // Set tool tips for the table header
74          this.jTableBuilds.setTableHeader(
75              new ToolTipHeader(this.jTableBuilds.getColumnModel()));
76          // Fix columns width
77          this.setColumnsWidth();
78          // 'jPanelGeneralFields'
79          this.add(
80              new JScrollPane(this.jTableBuilds),
81              new GridBagConstraints(
82                  0,
83                  0,
84                  5,
85                  1,
86                  1.0,
87                  1.0,
88                  GridBagConstraints.CENTER,
89                  GridBagConstraints.BOTH,
90                  new Insets(5, 5, 0, 5),
91                  0,
92                  0));
93          // Define table model
94          this.jTableBuilds.setModel(this.model);
95          // Set tool tips for the table header
96          this.jTableBuilds.setTableHeader(
97              new ToolTipHeader(this.jTableBuilds.getColumnModel()));
98          // Fix columns width
99          this.setColumnsWidth();
100         // MOUSE
101         this.jTableBuilds.addMouseListener(new MouseAdapter()
102         {
103             // When clicking on 'jTableField'...
104             public final void mouseClicked(final MouseEvent e)
105             {
106                 // Set 'selectedRow'
107                 selectedRow = jTableBuilds.getSelectedRow();
108                 // Enable 'jButtonRemove'
109             }
110         });
111         // KEY
112         this.jTableBuilds.addKeyListener(new KeyAdapter()
113         {
114             // When selecting a row of 'jTableField' using the keyboard...
115             public final void keyReleased(final KeyEvent e)
116             {
117                 // Set 'selectedRow'
118                 selectedRow = jTableBuilds.getSelectedRow();
119                 // Enable 'jButtonRemove'
120             }
121         });
122         this.setBorder(BorderFactory.createTitledBorder("Build"));
123         // Fix columns width
124         this.setColumnsWidth();
125     }
126     /***
127      * Unused: Cancel edition if 'DataDictionaryEdit' was
128      * edited in its previous call
129      */
130     public final void cancelCellEditing()
131     {
132         if (this.jTableBuilds.isEditing())
133         {
134             int tmpInt1 = this.jTableBuilds.getEditingRow();
135             int tmpInt2 = this.jTableBuilds.getEditingColumn();
136             this
137                 .jTableBuilds
138                 .getCellEditor(tmpInt1, tmpInt2)
139                 .cancelCellEditing();
140         }
141     }
142     /***
143      * Reset 'jTableField' columns width
144      */
145     private void setColumnsWidth()
146     {
147         for (int i = 0; i < this.model.getColumnCount(); i++)
148         {
149             this.jTableBuilds.getColumnModel().getColumn(i).setPreferredWidth(
150                 this.model.getColumnSize(i));
151         }
152     }
153     /***
154      * This class define a model to edit the object columns
155      */
156     public class ObjectBuildTableModel extends AbstractTableModel
157     {
158         /*** The data stored to be restored with cancel button */
159         private Vector oldData = new Vector();
160         /***
161          * The column name
162          */
163         private final String[] columnNames =
164             {
165                 "nag email adress",
166                 "source directory",
167                 "unit test source directory",
168                 "aspectSource directory" };
169         /***
170          * The column class/types
171          */
172         private final Class[] columnClasses =
173             { String.class, String.class, String.class, String.class };
174         /***
175          * The column size
176          */
177         private final int[] columnSizes = { 165, 165, 165, 160 };
178         /***
179          * The columns
180          */
181         private Vector builds = new Vector(1);
182         /***
183          * Return the data contained in the JTable
184          * @return colums
185          */
186         public final Vector getBuilds()
187         {
188             return builds;
189         }
190         /***
191          * Reset the TableModel to its old values
192          */
193         public final void resetBuilds()
194         {
195             cancelCellEditing();
196             this.builds = new Vector(oldData);
197         }
198         /***
199          * Set data in the JTable
200          * @param pData the columns
201          */
202         public final void setBuild(final Vector pData)
203         {
204             // store the old data
205             oldData.removeAllElements();
206             for (int i = 0; i < pData.size(); i++)
207             {
208                 oldData.addElement(new Build());
209             }
210             //fix the new data.
211             this.builds = new Vector(pData);
212         }
213         /***
214          * Return the number of columns
215          * @return count
216          */
217         public final int getColumnCount()
218         {
219             return this.columnNames.length;
220         }
221         /***
222          * Return the number of lines
223          * @return the count of row, number of columns.
224          */
225         public final int getRowCount()
226         {
227             return 1;
228         }
229         /***
230          * Return the name of the column at the specified index
231          * @param i index
232          * @return column
233          */
234         public final String getColumnName(final int i)
235         {
236             return this.columnNames[i];
237         }
238         /***
239          * Return the class of the column at the specified index
240          * @param i index
241          * @return class
242          */
243         public final Class getColumnClass(final int i)
244         {
245             return this.columnClasses[i];
246         }
247         /***
248          * Return the size of the column at the specified index
249          * @param i index
250          * @return the size
251          */
252         public final int getColumnSize(final int i)
253         {
254             return this.columnSizes[i];
255         }
256         /***
257          * Return the value at the specified row and column
258          * @param row row index
259          * @param col column index
260          * @return the value
261          */
262         public final Object getValueAt(final int row, final int col)
263         {
264             Build newBuild = (Build) builds.elementAt(0);
265             switch (col)
266             {
267                 case 0 :
268                     return newBuild.getNagEmailAddress();
269                 case 1 :
270                     return newBuild.getSourceDirectory();
271                 case 2 :
272                     return newBuild.getUnitTestSourceDirectory();
273                 case 3 :
274                     return newBuild.getAspectSourceDirectory();
275                 default :
276                     return null;
277             }
278         }
279         /***
280          * Set the specified value at the specified row and column
281          * @param value value
282          * @param row row index
283          * @param col column index
284          */
285         public final void setValueAt(
286             final Object value,
287             final int row,
288             final int col)
289         {
290             Build newBuild = (Build) builds.elementAt(row);
291             switch (col)
292             {
293                 case 0 :
294                     newBuild.setNagEmailAddress(value.toString());
295                     break;
296                 case 1 :
297                     newBuild.setSourceDirectory(value.toString());
298                     break;
299                 case 2 :
300                     newBuild.setUnitTestSourceDirectory(value.toString());
301                     break;
302                 case 3 :
303                     newBuild.setAspectSourceDirectory(value.toString());
304                     break;
305                 default :
306                     break;
307             }
308             // Replace the previous element
309             this.builds.setElementAt(newBuild, 0);
310             this.fireTableDataChanged();
311             // Set the selected row for the jTableField
312             jTableBuilds.setSelectedRow(0);
313         }
314         /***
315          * Return that every cell is editable
316          * @param row row index
317          * @param col column index
318          * @return is editable
319          */
320         public final boolean isCellEditable(final int row, final int col)
321         {
322             return row == 0;
323         }
324     } // End of class 'EntityFieldTableModel'
325     /***
326      * Define the tooltips of the 'jTableField' header
327      */
328     private class ToolTipHeader extends JTableHeader
329     {
330         /***
331          * The tooltips
332          * This tooltips are caught from the maven docs.
333          * Thus it was at time of coding this constructor may be
334          * out of date as it was wrotten on the basis of Maven v. 1.0-rc1
335          */
336         private String[] toolTips =
337             {
338                 " An address to which notifications regarding the status"
339                     + " of builds for this project can be sent. This is"
340                     + " intended for use by tools which do unattended"
341                     + " builds, for example those providing for continuous"
342                     + " integration. Currently this is used by the"
343                     + " maven:gump-descriptor  target.",
344                 "This element specifies a directory containing the source"
345                     + " of the project. The generated build system will"
346                     + " compile the source in this directory when the project"
347                     + " is built",
348                 " This element specifies a directory containing the unit test"
349                     + " source of the project. The generated build system will"
350                     + " compile these directories when the project is being"
351                     + " tested. The unit tests must use the JUnit framework",
352                 " This element specifies a directory containing Aspect sources"
353                     + " of the project. The generated build system will compile"
354                     + " the Aspects in this directory when the project is built"
355                     + " if Aspects have been enabled (see the Aspectj goals"
356                     + " document)." };
357         /***
358         * Construct a new 'ToolTipHeader' object
359         * @param pModel the TableColumnModel
360         */
361         public ToolTipHeader(final TableColumnModel pModel)
362         {
363             super(pModel);
364         }
365         /***
366          * Return the tool tip depending on the mouse cursor position
367          * @param e the event
368          * @return the tooltip
369          */
370         public final String getToolTipText(final MouseEvent e)
371         {
372             int col = this.columnAtPoint(e.getPoint());
373             int modelCol = this.getTable().convertColumnIndexToModel(col);
374             return toolTips[modelCol];
375         }
376     }
377     /***
378      * Accessor to the ObjectBuildTableModel
379      * @return the ObjectBuildTableModel
380      */
381     public final ObjectBuildTableModel getModel()
382     {
383         return model;
384     }
385     /***
386      * Accessor to the ObjectBuildTableModel
387      * @param pModel the ObjectBuildTableModel
388      */
389     public final void setModel(final ObjectBuildTableModel pModel)
390     {
391         this.model = pModel;
392     }
393 }