KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > earproject > ui > customizer > VisualArchiveIncludesSupport


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.j2ee.earproject.ui.customizer;
21
22 import java.awt.Component JavaDoc;
23 import java.awt.Dialog JavaDoc;
24 import java.awt.event.ActionEvent JavaDoc;
25 import java.awt.event.ActionListener JavaDoc;
26 import java.io.File JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Arrays JavaDoc;
29 import java.util.Collection JavaDoc;
30 import java.util.List JavaDoc;
31 import javax.swing.DefaultListSelectionModel JavaDoc;
32 import javax.swing.JButton JavaDoc;
33 import javax.swing.JFileChooser JavaDoc;
34 import javax.swing.JTable JavaDoc;
35 import javax.swing.ListSelectionModel JavaDoc;
36 import javax.swing.event.ListSelectionEvent JavaDoc;
37 import javax.swing.event.ListSelectionListener JavaDoc;
38 import javax.swing.event.TableModelEvent JavaDoc;
39 import javax.swing.event.TableModelListener JavaDoc;
40 import javax.swing.table.AbstractTableModel JavaDoc;
41 import javax.swing.table.DefaultTableCellRenderer JavaDoc;
42 import org.netbeans.api.java.project.JavaProjectConstants;
43 import org.netbeans.api.project.Project;
44 import org.netbeans.api.project.ant.AntArtifact;
45 import org.netbeans.api.project.libraries.Library;
46 import org.netbeans.modules.j2ee.api.ejbjar.EjbProjectConstants;
47 import org.netbeans.modules.web.api.webmodule.WebProjectConstants;
48 import org.openide.DialogDescriptor;
49 import org.openide.DialogDisplayer;
50 import org.openide.util.NbBundle;
51
52 /** Handles adding and removing of additional war content.
53  */

54 final class VisualArchiveIncludesSupport {
55     
56     final Project master;
57     final String JavaDoc j2eePlatform;
58     final JTable JavaDoc classpathTable;
59     final JButton JavaDoc addJarButton;
60     final JButton JavaDoc addLibraryButton;
61     final JButton JavaDoc addArtifactButton;
62     final JButton JavaDoc removeButton;
63     
64     private final ClasspathTableModel classpathModel;
65     private Object JavaDoc[][] data;
66
67     private final List JavaDoc<ActionListener JavaDoc> actionListeners = new ArrayList JavaDoc<ActionListener JavaDoc>();
68     
69     public VisualArchiveIncludesSupport(Project master,
70                                     String JavaDoc j2eePlatform,
71                                     JTable JavaDoc classpathTable,
72                                     JButton JavaDoc addJarButton,
73                                     JButton JavaDoc addLibraryButton,
74                                     JButton JavaDoc addArtifactButton,
75                                     JButton JavaDoc removeButton) {
76
77         // Remember all buttons
78
this.classpathTable = classpathTable;
79         this.classpathModel = new ClasspathTableModel();
80         this.classpathTable.setModel(classpathModel);
81         this.classpathTable.getColumnModel().getColumn(0).setHeaderValue(NbBundle.getMessage(VisualArchiveIncludesSupport.class, "TXT_Archive_Item"));
82         this.classpathTable.getColumnModel().getColumn(1).setHeaderValue(NbBundle.getMessage(VisualArchiveIncludesSupport.class, "TXT_Archive_PathInArchive"));
83         this.classpathTable.getColumnModel().getColumn(0).setCellRenderer(new ClassPathCellRenderer());
84         this.classpathTable.getColumnModel().getColumn(1).setCellRenderer(new DefaultTableCellRenderer JavaDoc() {
85             public Component JavaDoc getTableCellRendererComponent(JTable JavaDoc table, Object JavaDoc value, boolean isSelected,
86                     boolean hasFocus, int row, int column) {
87                 if (value != null) {
88                     setToolTipText(value.toString());
89                 }
90                 return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
91             }
92         });
93
94         this.classpathTable.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
95
96         this.addJarButton = addJarButton;
97         this.addLibraryButton = addLibraryButton;
98         this.addArtifactButton = addArtifactButton;
99         this.removeButton = removeButton;
100                        
101         this.master = master;
102         this.j2eePlatform = j2eePlatform;
103
104         // Register the listeners
105
ClasspathSupportListener csl = new ClasspathSupportListener();
106         
107         // On all buttons
108
addJarButton.addActionListener(csl);
109         addLibraryButton.addActionListener(csl);
110         addArtifactButton.addActionListener(csl);
111         removeButton.addActionListener(csl);
112         // On list selection
113
classpathTable.getSelectionModel().addListSelectionListener(csl);
114
115         classpathModel.addTableModelListener(csl);
116
117         // Set the initial state of the buttons
118
csl.valueChanged(null);
119     }
120     
121     public void setVisualWarItems(List JavaDoc<VisualClassPathItem> items) {
122         Object JavaDoc data[][] = new Object JavaDoc[items.size()][2];
123         this.data = data;
124         for (int i = 0; i < items.size(); i++) {
125             classpathModel.setValueAt(items.get(i), i, 0);
126             String JavaDoc pathInWAR = items.get(i).getPathInEAR();
127             classpathModel.setValueAt(pathInWAR, i, 1);
128         }
129         
130         classpathModel.fireTableDataChanged();
131     }
132     
133     public List JavaDoc<VisualClassPathItem> getVisualWarItems() {
134         List JavaDoc<VisualClassPathItem> items = new ArrayList JavaDoc<VisualClassPathItem>();
135         for (int i = 0; i < data.length; i++) {
136             items.add((VisualClassPathItem) classpathModel.getValueAt(i, 0));
137         }
138         return items;
139     }
140     
141     public void addTableModelListener(TableModelListener JavaDoc tml) {
142         classpathModel.addTableModelListener(tml);
143     }
144     
145     public void removeTableModelListener(TableModelListener JavaDoc tml) {
146         classpathModel.removeTableModelListener(tml);
147     }
148     
149     /** Action listeners will be informed when the value of the
150      * list changes.
151      */

152     public void addActionListener( ActionListener JavaDoc listener ) {
153         actionListeners.add( listener );
154     }
155     
156     public void removeActionListener( ActionListener JavaDoc listener ) {
157         actionListeners.remove( listener );
158     }
159     
160     private void fireActionPerformed() {
161         List JavaDoc<ActionListener JavaDoc> listeners;
162         
163         synchronized (this) {
164              listeners = new ArrayList JavaDoc<ActionListener JavaDoc>( actionListeners );
165         }
166         
167         ActionEvent JavaDoc ae = new ActionEvent JavaDoc( this, 0, null );
168         
169         for (ActionListener JavaDoc al : listeners) {
170             al.actionPerformed( ae );
171         }
172     }
173         
174     // Private methods ---------------------------------------------------------
175

176     private Collection JavaDoc<Object JavaDoc> getLibraries () {
177         List JavaDoc<Object JavaDoc> list = new ArrayList JavaDoc<Object JavaDoc>();
178         for (VisualClassPathItem vcpi : getVisualWarItems()) {
179             if (vcpi.getType() == VisualClassPathItem.Type.LIBRARY) {
180                 list.add(vcpi.getObject());
181             }
182         }
183         return list;
184     }
185     
186     private void addLibraries (Library[] libraries) {
187         if (libraries.length > 0) {
188             List JavaDoc<Library> newLibList = new ArrayList JavaDoc<Library>(Arrays.asList(libraries));
189             classpathTable.clearSelection();
190             int n0 = data.length;
191             for (int i = 0; i < n0; i++) {
192                 VisualClassPathItem item = (VisualClassPathItem) data[i][0];
193                 if(item.getType() == VisualClassPathItem.Type.LIBRARY &&
194                         newLibList.remove(item.getObject())) {
195                     classpathTable.addRowSelectionInterval(i, i);
196                 }
197             }
198             int n = newLibList.size();
199             if (n > 0) {
200                 Object JavaDoc[][] newData = new Object JavaDoc[n0 + n][2];
201                 for (int i = 0; i < n0; i++) {
202                     newData[i] = data[i];
203                 }
204                 for (int i = 0; i < n; i++) {
205                     Library library = newLibList.get(i);
206                     VisualClassPathItem item = VisualClassPathItem.createLibrary(library);
207                     newData[n0 + i][0] = item;
208                     newData[n0 + i][1] = VisualClassPathItem.PATH_IN_EAR;
209                 }
210
211                 data = newData;
212                 classpathModel.fireTableRowsInserted(n0, n0 + n - 1);
213                 classpathTable.addRowSelectionInterval(n0, n0 + n - 1);
214             }
215
216             fireActionPerformed();
217         }
218
219     }
220
221     private void addJarFiles( File JavaDoc files[] ) {
222         Object JavaDoc[][] newData = new Object JavaDoc[data.length + files.length][2];
223         for (int i = 0; i < data.length; i++) {
224             newData[i] = data[i];
225         }
226         for (int i = 0; i < files.length; i++) {
227             VisualClassPathItem jarFile = VisualClassPathItem.createJAR(files[i]);
228             newData[data.length + i][0] = jarFile;
229             newData[data.length + i][1] = jarFile.getPathInEAR();
230         }
231         
232         data = newData;
233         classpathModel.fireTableRowsInserted(data.length, data.length + files.length - 1);
234         
235         fireActionPerformed();
236     }
237     
238     private void addArtifacts( AntArtifact artifacts[] ) {
239         Object JavaDoc[][] newData = new Object JavaDoc[data.length + artifacts.length][2];
240         for (int i = 0; i < data.length; i++) {
241             newData[i] = data[i];
242         }
243         for (int i = 0; i < artifacts.length; i++) {
244             VisualClassPathItem vcpi = VisualClassPathItem.createArtifact(artifacts[i]);
245             newData[data.length + i][0] = vcpi;
246             newData[data.length + i][1] = vcpi.getPathInEAR();
247         }
248         
249         data = newData;
250         classpathModel.fireTableRowsInserted(data.length, data.length + artifacts.length - 1);
251         
252         fireActionPerformed();
253     }
254     
255     private void removeElements() {
256         ListSelectionModel JavaDoc sm = classpathTable.getSelectionModel();
257         int index = sm.getMinSelectionIndex();
258         if (sm.isSelectionEmpty()) {
259             assert false : "Remove button should be disabled"; // NOI18N
260
}
261         Collection JavaDoc<Object JavaDoc> elements = new ArrayList JavaDoc<Object JavaDoc>();
262         final int n0 = data.length;
263         for (int i = 0; i < n0; i++) {
264             if (!sm.isSelectedIndex(i)) {
265                 elements.add(data[i]);
266             }
267         }
268         final int n = elements.size();
269         data = elements.toArray(new Object JavaDoc[n][2]);
270         classpathModel.fireTableRowsDeleted(elements.size(), n0 - 1);
271
272         if (index >= n) {
273             index = n - 1;
274         }
275         sm.setSelectionInterval(index, index);
276
277         fireActionPerformed();
278     }
279     
280     // Private innerclasses ----------------------------------------------------
281

282     private class ClasspathSupportListener implements ActionListener JavaDoc, ListSelectionListener JavaDoc, TableModelListener JavaDoc {
283         // Implementation of ActionListener ------------------------------------
284

285         /** Handles button events
286          */

287         public void actionPerformed( ActionEvent JavaDoc e ) {
288             Object JavaDoc source = e.getSource();
289             if ( source == addJarButton ) {
290                 // Let user search for the Jar file
291
JFileChooser JavaDoc chooser = new JFileChooser JavaDoc();
292                 chooser.setFileSelectionMode( JFileChooser.FILES_ONLY );
293                 chooser.setMultiSelectionEnabled( true );
294                 chooser.setDialogTitle( NbBundle.getMessage( VisualArchiveIncludesSupport.class, "LBL_CustomizeCompile_Classpath_AddJar_JButton" ) ); // NOI18N
295
//chooser.setFileFilter( ProjectDirFilter.INSTANCE );
296
chooser.setAcceptAllFileFilterUsed( false );
297                 
298                 int option = chooser.showOpenDialog( null ); // Show the chooser
299

300                 if ( option == JFileChooser.APPROVE_OPTION ) {
301                     File JavaDoc files[] = chooser.getSelectedFiles();
302                     addJarFiles( files );
303                 }
304             } else if ( source == addLibraryButton ) {
305                 LibrariesChooser panel = new LibrariesChooser(getLibraries(), j2eePlatform);
306                 Object JavaDoc[] options = new Object JavaDoc[] {
307                     NbBundle.getMessage (VisualArchiveIncludesSupport.class,"LBL_AddLibrary"),
308                     DialogDescriptor.CANCEL_OPTION
309                 };
310                 DialogDescriptor desc = new DialogDescriptor(panel,NbBundle.getMessage( VisualArchiveIncludesSupport.class, "LBL_CustomizeCompile_Classpath_AddLibrary" ),
311                     true, options, options[0], DialogDescriptor.DEFAULT_ALIGN,null,null);
312                 Dialog JavaDoc dlg = DialogDisplayer.getDefault().createDialog(desc);
313                 dlg.setVisible(true);
314                 if (desc.getValue() == options[0]) {
315                    addLibraries (panel.getSelectedLibraries());
316                 }
317                 dlg.dispose();
318             } else if ( source == addArtifactButton ) {
319 // AntArtifact artifacts[] = AntArtifactChooser.showDialog(JavaProjectConstants.ARTIFACT_TYPE_JAR, master);
320
// XXX this is hardcoded
321
AntArtifact artifacts[] = AntArtifactChooser.showDialog(master,
322                     new String JavaDoc[] {
323                         EjbProjectConstants.ARTIFACT_TYPE_J2EE_MODULE_IN_EAR_ARCHIVE,
324                         WebProjectConstants.ARTIFACT_TYPE_WAR_EAR_ARCHIVE,
325                         JavaProjectConstants.ARTIFACT_TYPE_JAR });
326          // AntArtifact artifacts[] = AntArtifactChooser.showDialog(JavaProjectConstants.ARTIFACT_TYPE_JAR, master);
327
if ( artifacts != null ) {
328                     addArtifacts( artifacts );
329                 }
330             } else if ( source == removeButton ) {
331                 removeElements();
332             }
333         }
334         
335         // ListSelectionModel --------------------------------------------------
336

337         /** Handles changes in the selection
338          */

339         public void valueChanged( ListSelectionEvent JavaDoc e ) {
340             DefaultListSelectionModel JavaDoc sm = (DefaultListSelectionModel JavaDoc) classpathTable.getSelectionModel();
341             int index = sm.getMinSelectionIndex();
342             
343             // remove enabled only if selection is not empty
344
boolean remove = index != -1;
345             // and when the selection does not contain unremovable item
346
if (remove) {
347                 VisualClassPathItem vcpi = (VisualClassPathItem) classpathModel.getValueAt(index, 0);
348                 if (!vcpi.canDelete()) {
349                     remove = false;
350                 }
351             }
352                         
353             removeButton.setEnabled(remove);
354         }
355         
356         // TableModelListener --------------------------------------
357
public void tableChanged(TableModelEvent JavaDoc e) {
358             if (e.getColumn() == 1) {
359                 VisualClassPathItem cpItem = (VisualClassPathItem) classpathModel.getValueAt(e.getFirstRow(), 0);
360                 cpItem.setPathInEAR((String JavaDoc) classpathModel.getValueAt(e.getFirstRow(), 1));
361                 
362                 fireActionPerformed();
363             }
364         }
365
366     }
367     
368     private static class ClassPathCellRenderer extends DefaultTableCellRenderer JavaDoc {
369         public Component JavaDoc getTableCellRendererComponent(JTable JavaDoc table, Object JavaDoc value, boolean isSelected, boolean hasFocus, int row, int column) {
370             if (value instanceof VisualClassPathItem) {
371                 final VisualClassPathItem item = (VisualClassPathItem) value;
372                 setIcon(item.getIcon());
373                 // XXX integrate this into the generic VCPI...
374
// setToolTipText(item.getToolTipText());
375
}
376             final String JavaDoc s = value == null ? null : value.toString();
377             return super.getTableCellRendererComponent(table, s, isSelected, false, row, column);
378         }
379     }
380
381     class ClasspathTableModel extends AbstractTableModel JavaDoc {
382         public int getColumnCount() {
383             return 2; //classpath item name, item location within WAR
384
}
385
386         public int getRowCount() {
387             if (data == null) {
388                 return 0;
389             }
390             return data.length;
391         }
392
393         public Object JavaDoc getValueAt(int row, int col) {
394             return data[row][col];
395         }
396
397         public boolean isCellEditable(int row, int col) {
398             return col == 1;
399         }
400
401         public void setValueAt(Object JavaDoc value, int row, int col) {
402             data[row][col] = value;
403             fireTableCellUpdated(row, col);
404         }
405     }
406
407 }
408
Popular Tags