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.util.List;
23  import java.util.Iterator;
24  import java.util.StringTokenizer;
25  import java.awt.GridBagConstraints;
26  import java.awt.GridBagLayout;
27  import java.awt.Insets;
28  import java.awt.event.KeyAdapter;
29  import java.awt.event.KeyEvent;
30  import java.awt.event.MouseAdapter;
31  import java.awt.event.MouseEvent;
32  import javax.swing.BorderFactory;
33  import javax.swing.JPanel;
34  import javax.swing.JScrollPane;
35  import javax.swing.table.AbstractTableModel;
36  import javax.swing.DefaultListSelectionModel;
37  import javax.swing.table.JTableHeader;
38  import javax.swing.table.TableColumnModel;
39  import org.devaki.nextobjects.ui.components.CustomTable;
40  import org.apache.maven.project.UnitTest; 
41  
42  import org.apache.maven.project.Resource;
43  /***
44   * This class provid ability to edit UnitTest resources
45   *
46   * @author <a href="mailto:eflorent@devaki.org">Emmanuel Florent</a>
47   * @todo add root Xcludes of unitTest (includes?,excludes?,resources?)
48   * @TODO : check this class doesn't work 
49   */
50  public class UnitTestEdit extends JPanel
51  {
52      /***
53       * Selected row in the jTable
54       */
55      private int selectedRow = -1;
56      /***
57       * The table for columns
58       */
59      private CustomTable jTableUnitTests =
60          new CustomTable(
61              false,
62              DefaultListSelectionModel.SINGLE_SELECTION,
63              false,
64              false);
65      /***
66       * The table model
67       */
68      private ObjectUnitTestTableModel model = new ObjectUnitTestTableModel();
69      /***
70       * Constructor
71       *
72       */
73      public UnitTestEdit()
74      {
75          super(new GridBagLayout());
76          // Define table model
77          this.jTableUnitTests.setModel(this.model);
78          // Set tool tips for the table header
79          this.jTableUnitTests.setTableHeader(
80              new ToolTipHeader(this.jTableUnitTests.getColumnModel()));
81          // Fix columns width
82          this.setColumnsWidth();
83          // 'jPanelGeneralFields'
84          this.add(
85              new JScrollPane(this.jTableUnitTests),
86              new GridBagConstraints(
87                  0,
88                  0,
89                  5,
90                  1,
91                  1.0,
92                  1.0,
93                  GridBagConstraints.CENTER,
94                  GridBagConstraints.BOTH,
95                  new Insets(5, 5, 0, 5),
96                  0,
97                  0));
98          // Define table model
99          this.jTableUnitTests.setModel(this.model);
100         // Set tool tips for the table header
101         this.jTableUnitTests.setTableHeader(
102             new ToolTipHeader(this.jTableUnitTests.getColumnModel()));
103         // Fix columns width
104         //this.setColumnsWidth();
105         // MOUSE
106         this.jTableUnitTests.addMouseListener(new MouseAdapter()
107         {
108             // When clicking on 'jTableField'...
109             public final void mouseClicked(final MouseEvent e)
110             {
111                 // Set 'selectedRow'
112                 selectedRow = jTableUnitTests.getSelectedRow();
113                 // Enable 'jButtonRemove'
114             }
115         });
116         // KEY
117         this.jTableUnitTests.addKeyListener(new KeyAdapter()
118         {
119             // When selecting a row of 'jTableField' using the keyboard...
120             public final void keyReleased(final KeyEvent e)
121             {
122                 // Set 'selectedRow'
123                 selectedRow = jTableUnitTests.getSelectedRow();
124                 // Enable 'jButtonRemove'
125             }
126         });
127         this.setBorder(BorderFactory.createTitledBorder("UnitTest"));
128         // Fix columns width
129         this.setColumnsWidth();
130     }
131     /***
132      * Unused: Cancel edition if 'DataDictionaryEdit' was
133      * edited in its previous call
134      */
135     public final void cancelCellEditing()
136     {
137         if (this.jTableUnitTests.isEditing())
138         {
139             int tmpInt1 = this.jTableUnitTests.getEditingRow();
140             int tmpInt2 = this.jTableUnitTests.getEditingColumn();
141             this
142                 .jTableUnitTests
143                 .getCellEditor(tmpInt1, tmpInt2)
144                 .cancelCellEditing();
145         }
146     }
147     /***
148      * Reset 'jTableField' columns width
149      */
150     private void setColumnsWidth()
151     {
152         for (int i = 0; i < this.model.getColumnCount(); i++)
153         {
154             this.jTableUnitTests.getColumnModel().getColumn(
155                 i).setPreferredWidth(
156                 this.model.getColumnSize(i));
157         }
158     }
159     /***
160      * This class define a model to edit the object columns
161      */
162     public class ObjectUnitTestTableModel extends AbstractTableModel
163     {
164         /*** The data stored to be restored with cancel button */
165         private Vector oldData = new Vector();
166         /***
167          * The column name
168          */
169         private final String[] columnNames =
170             { "Directory", "Target Path", "Includes", "Excludes" };
171         /***
172          * The column class/types
173          */
174         private final Class[] columnClasses =
175             { String.class, String.class, String.class, String.class };
176         /***
177          * The column size
178          */
179         private final int[] columnSizes = { 167, 165, 165, 185 };
180         /***
181          * The columns
182          */
183         private Vector unitTestRes = new Vector(1);
184         /***
185          * Return the data contained in the JTable
186          * @return colums
187          */
188         public final Vector getUnitTestRes()
189         {
190             return unitTestRes;
191         }
192         /***
193          * Reset the TableModel to its old values
194          */
195         public final void resetUnitTests()
196         {
197             cancelCellEditing();
198             this.unitTestRes = new Vector(oldData);
199         }
200         /***
201          * Set data in the JTable
202          * @param pData the columns
203          */
204         public final void setUnitTests(final List pData)
205         {
206             this.unitTestRes = new Vector(pData);
207         }
208         /***
209          * Add a field
210          * @param pResource the columns
211          */
212         public final void addUnitTest(final Resource pResource)
213         {
214             this.unitTestRes.addElement(pResource);
215             this.fireTableRowsInserted(
216                 this.unitTestRes.size() - 1,
217                 this.unitTestRes.size() - 1);
218         }
219         /***
220          * Add a field at a particular index
221          * @param pColumn the column
222          * @param i index
223          */
224         public final void insertUnitTest(final Resource pColumn, final int i)
225         {
226             this.unitTestRes.insertElementAt(pColumn, i);
227             this.fireTableRowsInserted(
228                 this.unitTestRes.size() - 1,
229                 this.unitTestRes.size() - 1);
230         }
231         /***
232          * Remove a field at a particular index
233          * @param i index
234          */
235         public final void removeField(final int i)
236         {
237             if (i >= 0 && i < unitTestRes.size())
238             {
239                 this.unitTestRes.removeElementAt(i);
240                 this.fireTableRowsDeleted(
241                     this.unitTestRes.size() - 1,
242                     this.unitTestRes.size() - 1);
243             }
244         }
245         /***
246          * Return the number of columns
247          * @return count
248          */
249         public final int getColumnCount()
250         {
251             return this.columnNames.length;
252         }
253         /***
254          * Return the number of lines
255          * @return the count of row, number of columns.
256          */
257         public final int getRowCount()
258         {
259             return this.unitTestRes.size();
260         }
261         /***
262          * Return the name of the column at the specified index
263          * @param i index
264          * @return column
265          */
266         public final String getColumnName(final int i)
267         {
268             return this.columnNames[i];
269         }
270         /***
271          * Return the class of the column at the specified index
272          * @param i index
273          * @return class
274          */
275         public final Class getColumnClass(final int i)
276         {
277             return this.columnClasses[i];
278         }
279         /***
280          * Return the size of the column at the specified index
281          * @param i index
282          * @return the size
283          */
284         public final int getColumnSize(final int i)
285         {
286             return this.columnSizes[i];
287         }
288         /***
289          * Return the value at the specified row and column
290          * @param row row index
291          * @param col column index
292          * @return the value
293          */
294         public final Object getValueAt(final int row, final int col)
295         {
296             Resource newUnitTest = (Resource) unitTestRes.elementAt(row);
297             if (newUnitTest == null)
298             {
299                 newUnitTest = new Resource();
300             }
301             switch (col)
302             {
303                 case 0 :
304                     return newUnitTest.getDirectory();
305                 case 1 :
306                     return newUnitTest.getTargetPath();
307                 case 2 :
308                     StringBuffer str = new StringBuffer();
309                     Iterator itIncludes = newUnitTest.getIncludes().iterator();
310                     while (itIncludes.hasNext())
311                     {
312                         str.append(itIncludes.next().toString());
313                         str.append(";");
314                     }
315                     return str;
316                 case 3 :
317                     StringBuffer strExclude = new StringBuffer();
318                     Iterator itExcludes = newUnitTest.getExcludes().iterator();
319                     while (itExcludes.hasNext())
320                     {
321                         strExclude.append(itExcludes.next().toString());
322                         strExclude.append(";");
323                     }
324                     return strExclude;
325                 default :
326                     return null;
327             }
328         }
329         /***
330          * Set the specified value at the specified row and column
331          * @param value value
332          * @param row row index
333          * @param col column index
334          */
335         public final void setValueAt(
336             final Object value,
337             final int row,
338             final int col)
339         {
340             StringTokenizer tknizer = null;
341             Resource newUnitTest = (Resource) unitTestRes.elementAt(row);
342             switch (col)
343             {
344                 case 0 :
345                     if (value.toString().length() != 0)
346                     {
347                         newUnitTest.setDirectory(value.toString());
348                     }
349                     break;
350                 case 1 :
351                     newUnitTest.setTargetPath(value.toString());
352                     break;
353                 case 2 :
354                     newUnitTest.getIncludes().clear();
355                     tknizer = new StringTokenizer(value.toString(), ";");
356                     while (tknizer.hasMoreTokens())
357                     {
358                         newUnitTest.addInclude(tknizer.nextToken());
359                     }
360                     break;
361                 case 3 :
362                     newUnitTest.getExcludes().clear();
363                     tknizer = new StringTokenizer(value.toString(), ";");
364                     while (tknizer.hasMoreTokens())
365                     {
366                         newUnitTest.addExclude(tknizer.nextToken());
367                     }
368                     break;
369                 default :
370                     break;
371             }
372             // Replace the previous element
373             this.unitTestRes.setElementAt(newUnitTest, row);
374             this.fireTableDataChanged();
375             // Set the selected row for the jTableField
376             jTableUnitTests.setSelectedRow(row);
377         }
378         /***
379          * Return that every cell is editable
380          * @param row row index
381          * @param col column index
382          * @return is editable
383          */
384         public final boolean isCellEditable(final int row, final int col)
385         {
386             return true;
387         }
388     } // End of class 'EntityFieldTableModel'
389     /***
390      * Define the tooltips of the 'jTableField' header
391      */
392     private class ToolTipHeader extends JTableHeader
393     {
394         /***
395          * The tooltips
396          */
397         private String[] toolTips =
398             {
399                 "Describe the resources associated with unit tests",
400                 "Describe the includes unit tests.",
401                 "Describe the excludes unit tests" };
402         /***
403         * Construct a new 'ToolTipHeader' object
404         * @param pModel the TableColumnModel
405         */
406         public ToolTipHeader(final TableColumnModel pModel)
407         {
408             super(pModel);
409         }
410         /***
411          * Return the tool tip depending on the mouse cursor position
412          * @param e the event
413          * @return the tooltip
414          */
415         public final String getToolTipText(final MouseEvent e)
416         {
417             int col = this.columnAtPoint(e.getPoint());
418             int modelCol = this.getTable().convertColumnIndexToModel(col);
419             return toolTips[modelCol];
420         }
421     }
422     /***
423      * Return the model object
424      * @return object mapping resources
425      */
426     public final ObjectUnitTestTableModel getModel()
427     {
428         return model;
429     }
430     /***
431      * Set the model object
432      * @param pModel an object mapping resources
433      */
434     public final void setModel(final ObjectUnitTestTableModel pModel)
435     {
436         this.model = pModel;
437     }
438 }