KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > earproject > ui > wizards > PanelModuleDetectionVisual


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.wizards;
21
22 import java.awt.Component JavaDoc;
23 import java.awt.event.ActionEvent JavaDoc;
24 import java.awt.event.ActionListener JavaDoc;
25 import java.io.File JavaDoc;
26 import java.util.EventObject JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.HashSet JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.Map JavaDoc;
31 import java.util.Set JavaDoc;
32 import java.util.Vector JavaDoc;
33 import javax.swing.JComboBox JavaDoc;
34 import javax.swing.JFileChooser JavaDoc;
35 import javax.swing.JPanel JavaDoc;
36 import javax.swing.JTable JavaDoc;
37 import javax.swing.event.CellEditorListener JavaDoc;
38 import javax.swing.event.ChangeEvent JavaDoc;
39 import javax.swing.event.ChangeListener JavaDoc;
40 import javax.swing.event.EventListenerList JavaDoc;
41 import javax.swing.table.DefaultTableModel JavaDoc;
42 import javax.swing.table.TableCellEditor JavaDoc;
43 import javax.swing.table.TableCellRenderer JavaDoc;
44 import javax.swing.table.TableColumn JavaDoc;
45 import javax.swing.table.TableColumnModel JavaDoc;
46 import org.netbeans.modules.j2ee.earproject.ModuleType;
47 import org.netbeans.spi.project.support.ant.PropertyUtils;
48 import org.openide.WizardDescriptor;
49 import org.openide.filesystems.FileObject;
50 import org.openide.filesystems.FileUtil;
51 import org.openide.util.NbBundle;
52
53 /**
54  * @author Martin Krauskopf
55  */

56 public class PanelModuleDetectionVisual extends JPanel JavaDoc {
57     
58     private final Vector JavaDoc<Vector JavaDoc<String JavaDoc>> modules = new Vector JavaDoc<Vector JavaDoc<String JavaDoc>>();
59     private static final int REL_PATH_INDEX = 0;
60     private static final int TYPE_INDEX = 1;
61     private final Set JavaDoc<ChangeListener JavaDoc> listeners = new HashSet JavaDoc<ChangeListener JavaDoc>(1);
62     
63     // Location of Enterprise Application to be imported, chosen on the previous panel.
64
private File JavaDoc eaLocation;
65     
66     public PanelModuleDetectionVisual() {
67         initComponents();
68         initModuleTable();
69         // Provide a name in the title bar.
70
setName(getMessage("LBL_IW_ApplicationModulesStep"));
71         putClientProperty("NewProjectWizard_Title", getMessage("TXT_ImportProject"));
72         getAccessibleContext().setAccessibleDescription(getMessage("ACS_NWP1_NamePanel_A11YDesc"));
73     }
74     
75     public void addChangeListener(ChangeListener JavaDoc l) {
76         synchronized (listeners) {
77             listeners.add(l);
78         }
79     }
80     
81     public void removeChangeListener(ChangeListener JavaDoc l) {
82         synchronized (listeners) {
83             listeners.remove(l);
84         }
85     }
86     
87     protected void fireChangeEvent() {
88         Iterator JavaDoc<ChangeListener JavaDoc> it;
89         synchronized (listeners) {
90             it = new HashSet JavaDoc<ChangeListener JavaDoc>(listeners).iterator();
91         }
92         ChangeEvent JavaDoc ev = new ChangeEvent JavaDoc(this);
93         while (it.hasNext()) {
94             it.next().stateChanged(ev);
95         }
96     }
97     
98     private void initModuleTable() {
99         Vector JavaDoc<String JavaDoc> colNames = new Vector JavaDoc<String JavaDoc>();
100         colNames.add(getMessage("LBL_IW_Module"));
101         colNames.add(getMessage("LBL_IW_Type"));
102         DefaultTableModel JavaDoc moduleTableModel = new DefaultTableModel JavaDoc(modules, colNames);
103         moduleTable.setModel(moduleTableModel);
104         TableColumnModel JavaDoc tcm = moduleTable.getColumnModel();
105         TableColumn JavaDoc tc = tcm.getColumn(1);
106         ModuleTypeRenderer renderer = new ModuleTypeRenderer();
107         tc.setCellRenderer(renderer);
108         tc.setCellEditor(new ModuleTypeEditor());
109         moduleTable.setRowHeight((int) renderer.getPreferredSize().getHeight());
110         moduleSP.getViewport().setBackground(moduleTable.getBackground());
111     }
112     
113     void read(WizardDescriptor settings) {
114         File JavaDoc newEALocation = (File JavaDoc) settings.getProperty(WizardProperties.SOURCE_ROOT);
115         assert newEALocation != null : "Location is not available!";
116         if (!newEALocation.equals(eaLocation)) {
117             // reset all set of modules
118
this.modules.removeAllElements();
119         }
120         eaLocation = newEALocation;
121         FileObject eaLocationFO = FileUtil.toFileObject(eaLocation);
122         Map JavaDoc<FileObject, ModuleType> modules = ModuleType.detectModules(eaLocationFO);
123         for (FileObject moduleDir : modules.keySet()) {
124             addModuleToTable(FileUtil.toFile(moduleDir));
125         }
126         getModuleTableModel().fireTableDataChanged();
127     }
128     
129     boolean valid(WizardDescriptor wizardDescriptor) {
130         File JavaDoc project = getProject(wizardDescriptor);
131         for (Vector JavaDoc<String JavaDoc> module : modules) {
132             if (isModuleForbidden(project, module.get(REL_PATH_INDEX))) {
133                 wizardDescriptor.putProperty("WizardPanel_errorMessage", //NOI18N
134
getMessage("MSG_ModuleLocationAlreadyExists")); //NOI18N
135
return false;
136             }
137         }
138         wizardDescriptor.putProperty("WizardPanel_errorMessage", null); // NOI18N
139
return true;
140     }
141     
142     /** Get the project directory. */
143     private File JavaDoc getProject(WizardDescriptor wizardDescriptor) {
144         return (File JavaDoc) wizardDescriptor.getProperty(WizardProperties.PROJECT_DIR);
145     }
146     
147     // #87604
148
/** Return <code>true</code> if the module location already exists in the project directory. */
149     private boolean isModuleForbidden(File JavaDoc project, String JavaDoc module) {
150         String JavaDoc moduleName = new File JavaDoc(project, module).getName();
151         File JavaDoc forbiddenLocation = new File JavaDoc(project, moduleName);
152         return forbiddenLocation.exists();
153     }
154     
155     void store(WizardDescriptor wd) {
156         Map JavaDoc<FileObject, ModuleType> userModules =
157                 new HashMap JavaDoc<FileObject, ModuleType>();
158         for (Vector JavaDoc<String JavaDoc> module : modules) {
159             String JavaDoc description = module.get(TYPE_INDEX);
160             for (ModuleType type : ModuleType.values()) {
161                 if (type.getDescription().equals(description)) {
162                     File JavaDoc moduleDir = new File JavaDoc(eaLocation, module.get(REL_PATH_INDEX));
163                     FileObject moduleDirFO = FileUtil.toFileObject(FileUtil.normalizeFile(moduleDir));
164                     assert moduleDirFO != null;
165                     userModules.put(moduleDirFO, type);
166                     break;
167                 }
168             }
169         }
170         wd.putProperty(WizardProperties.USER_MODULES, userModules);
171     }
172     
173     private DefaultTableModel JavaDoc getModuleTableModel() {
174         return (DefaultTableModel JavaDoc) moduleTable.getModel();
175     }
176     
177     private void addModuleToTable(final File JavaDoc moduleF) {
178         String JavaDoc relPath = PropertyUtils.relativizeFile(eaLocation, moduleF);
179         for (Vector JavaDoc<String JavaDoc> module : modules) {
180             if (relPath.equals(module.get(REL_PATH_INDEX))) {
181                 // already added
182
return;
183             }
184         }
185         Vector JavaDoc<String JavaDoc> row = new Vector JavaDoc<String JavaDoc>();
186         row.add(relPath);
187         row.add(getModuleType(relPath).getDescription());
188         modules.add(row);
189         fireChangeEvent();
190     }
191     
192     private static final String JavaDoc getMessage(String JavaDoc bundleKey) {
193         return NbBundle.getMessage(PanelModuleDetectionVisual.class, bundleKey);
194     }
195     
196     /** This method is called from within the constructor to
197      * initialize the form.
198      * WARNING: Do NOT modify this code. The content of this method is
199      * always regenerated by the Form Editor.
200      */

201     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
202
private void initComponents() {
203         appModulesLabel = new javax.swing.JLabel JavaDoc();
204         moduleSP = new javax.swing.JScrollPane JavaDoc();
205         moduleTable = new javax.swing.JTable JavaDoc();
206         addModuleButton = new javax.swing.JButton JavaDoc();
207         removeModuleButton = new javax.swing.JButton JavaDoc();
208
209         appModulesLabel.setLabelFor(moduleTable);
210         org.openide.awt.Mnemonics.setLocalizedText(appModulesLabel, org.openide.util.NbBundle.getMessage(PanelModuleDetectionVisual.class, "LBL_IW_ApplicationModules"));
211         appModulesLabel.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(PanelModuleDetectionVisual.class, "ACSD_LBL_IW_ApplicationModules"));
212
213         moduleSP.setViewportView(moduleTable);
214
215         moduleSP.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(PanelModuleDetectionVisual.class, "ACSN_CTL_AppModules"));
216         moduleSP.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(PanelModuleDetectionVisual.class, "ACSD_CTL_AppModules"));
217
218         org.openide.awt.Mnemonics.setLocalizedText(addModuleButton, org.openide.util.NbBundle.getMessage(PanelModuleDetectionVisual.class, "LBL_IW_Add"));
219         addModuleButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
220             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
221                 addModuleButtonActionPerformed(evt);
222             }
223         });
224
225         addModuleButton.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(PanelModuleDetectionVisual.class, "ACSD_LBL_IW_Add"));
226
227         org.openide.awt.Mnemonics.setLocalizedText(removeModuleButton, org.openide.util.NbBundle.getMessage(PanelModuleDetectionVisual.class, "LBL_IW_Remove"));
228         removeModuleButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
229             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
230                 removeModuleButtonActionPerformed(evt);
231             }
232         });
233
234         removeModuleButton.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(PanelModuleDetectionVisual.class, "ACSD_LBL_IW_Remove"));
235
236         org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
237         this.setLayout(layout);
238         layout.setHorizontalGroup(
239             layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
240             .add(layout.createSequentialGroup()
241                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
242                     .add(appModulesLabel)
243                     .add(moduleSP, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 296, Short.MAX_VALUE))
244                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
245                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
246                     .add(addModuleButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 83, Short.MAX_VALUE)
247                     .add(removeModuleButton)))
248         );
249
250         layout.linkSize(new java.awt.Component JavaDoc[] {addModuleButton, removeModuleButton}, org.jdesktop.layout.GroupLayout.HORIZONTAL);
251
252         layout.setVerticalGroup(
253             layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
254             .add(layout.createSequentialGroup()
255                 .add(appModulesLabel)
256                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
257                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
258                     .add(layout.createSequentialGroup()
259                         .add(addModuleButton)
260                         .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
261                         .add(removeModuleButton)
262                         .addContainerGap())
263                     .add(moduleSP, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 270, Short.MAX_VALUE)))
264         );
265     }// </editor-fold>//GEN-END:initComponents
266

267     private void removeModuleButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_removeModuleButtonActionPerformed
268
int row = moduleTable.getSelectedRow();
269         if (row != -1) {
270             modules.remove(row);
271             getModuleTableModel().fireTableRowsDeleted(row, row);
272             fireChangeEvent();
273         }
274     }//GEN-LAST:event_removeModuleButtonActionPerformed
275

276     private void addModuleButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_addModuleButtonActionPerformed
277
JFileChooser JavaDoc chooser = new JFileChooser JavaDoc(eaLocation);
278         chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
279         if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
280             if (eaLocation.equals(chooser.getSelectedFile())) {
281                 // XXX show some dialog to the user that Enterprise Application
282
// itself cannot be added
283
return;
284             }
285             addModuleToTable(chooser.getSelectedFile());
286             getModuleTableModel().fireTableDataChanged();
287         }
288     }//GEN-LAST:event_addModuleButtonActionPerformed
289

290     private ModuleType getModuleType(final String JavaDoc relPath) {
291         ModuleType type = null;
292         File JavaDoc dir = FileUtil.normalizeFile(new File JavaDoc(eaLocation, relPath));
293         FileObject dirFO = FileUtil.toFileObject(dir);
294         if (dirFO != null) {
295             type = ModuleType.detectModuleType(dirFO);
296         }
297         return type == null ? ModuleType.WEB : type; // WEB is default if detection fails;
298
}
299     
300     // Variables declaration - do not modify//GEN-BEGIN:variables
301
private javax.swing.JButton JavaDoc addModuleButton;
302     private javax.swing.JLabel JavaDoc appModulesLabel;
303     private javax.swing.JScrollPane JavaDoc moduleSP;
304     private javax.swing.JTable JavaDoc moduleTable;
305     private javax.swing.JButton JavaDoc removeModuleButton;
306     // End of variables declaration//GEN-END:variables
307

308     private static final class ModuleTypeRenderer extends JComboBox JavaDoc
309             implements TableCellRenderer JavaDoc {
310         
311         ModuleTypeRenderer() {
312             for (ModuleType type : ModuleType.values()) {
313                 addItem(type.getDescription());
314             }
315         }
316         
317         public Component JavaDoc getTableCellRendererComponent(JTable JavaDoc table, Object JavaDoc value,
318                 boolean isSelected, boolean hasFocus, int row, int column) {
319             
320             if (isSelected) {
321                 setForeground(table.getSelectionForeground());
322                 super.setBackground(table.getSelectionBackground());
323             } else {
324                 setForeground(table.getForeground());
325                 setBackground(table.getBackground());
326             }
327             
328             String JavaDoc moduleType = (String JavaDoc) value;
329             setSelectedItem(moduleType);
330             return this;
331         }
332         
333     }
334     
335     private class ModuleTypeEditor extends JComboBox JavaDoc implements TableCellEditor JavaDoc {
336         
337         protected EventListenerList JavaDoc listenerList = new EventListenerList JavaDoc();
338         protected ChangeEvent JavaDoc changeEvent = new ChangeEvent JavaDoc(this);
339         
340         ModuleTypeEditor() {
341             for (ModuleType type : ModuleType.values()) {
342                 addItem(type.getDescription());
343             }
344             addActionListener(new ActionListener JavaDoc() {
345                 public void actionPerformed(ActionEvent JavaDoc event) {
346                     fireEditingStopped();
347                 }
348             });
349         }
350         
351         public void addCellEditorListener(CellEditorListener JavaDoc listener) {
352             listenerList.add(CellEditorListener JavaDoc.class, listener);
353         }
354         
355         public void removeCellEditorListener(CellEditorListener JavaDoc listener) {
356             listenerList.remove(CellEditorListener JavaDoc.class, listener);
357         }
358         
359         protected void fireEditingStopped() {
360             CellEditorListener JavaDoc listener;
361             Object JavaDoc[] listeners = listenerList.getListenerList();
362             for (int i = 0; i < listeners.length; i++) {
363                 if (listeners[i] == CellEditorListener JavaDoc.class) {
364                     listener = (CellEditorListener JavaDoc) listeners[i + 1];
365                     listener.editingStopped(changeEvent);
366                 }
367             }
368         }
369         
370         protected void fireEditingCanceled() {
371             CellEditorListener JavaDoc listener;
372             Object JavaDoc[] listeners = listenerList.getListenerList();
373             for (int i = 0; i < listeners.length; i++) {
374                 if (listeners[i] == CellEditorListener JavaDoc.class) {
375                     listener = (CellEditorListener JavaDoc) listeners[i + 1];
376                     listener.editingCanceled(changeEvent);
377                 }
378             }
379         }
380         
381         public void cancelCellEditing() {
382             fireEditingCanceled();
383         }
384         
385         public boolean stopCellEditing() {
386             fireEditingStopped();
387             return true;
388         }
389         
390         public boolean isCellEditable(EventObject JavaDoc event) {
391             return true;
392         }
393         
394         public boolean shouldSelectCell(EventObject JavaDoc event) {
395             return true;
396         }
397         
398         public Object JavaDoc getCellEditorValue() {
399             return getSelectedItem();
400         }
401         
402         public Component JavaDoc getTableCellEditorComponent(JTable JavaDoc table, Object JavaDoc value,
403                 boolean isSelected, int row, int column) {
404             String JavaDoc moduleType = (String JavaDoc) value;
405             setSelectedItem(moduleType);
406             return this;
407         }
408         
409     }
410     
411 }
412
Popular Tags