KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > projectimport > eclipse > wizard > ProjectSelectionPanel


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.projectimport.eclipse.wizard;
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.Collection JavaDoc;
27 import java.util.HashSet JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.Set JavaDoc;
30 import java.util.Stack JavaDoc;
31 import java.util.TreeSet JavaDoc;
32 import java.util.logging.Level JavaDoc;
33 import java.util.logging.Logger JavaDoc;
34 import javax.swing.AbstractCellEditor JavaDoc;
35 import javax.swing.JCheckBox JavaDoc;
36 import javax.swing.JFileChooser JavaDoc;
37 import javax.swing.JPanel JavaDoc;
38 import javax.swing.JTable JavaDoc;
39 import javax.swing.ListSelectionModel JavaDoc;
40 import javax.swing.UIManager JavaDoc;
41 import javax.swing.event.DocumentEvent JavaDoc;
42 import javax.swing.event.DocumentListener JavaDoc;
43 import javax.swing.table.AbstractTableModel JavaDoc;
44 import javax.swing.table.TableCellEditor JavaDoc;
45 import javax.swing.table.TableCellRenderer JavaDoc;
46 import org.netbeans.modules.projectimport.LoggerFactory;
47 import org.netbeans.modules.projectimport.ProjectImporterException;
48 import org.netbeans.modules.projectimport.eclipse.EclipseProject;
49 import org.netbeans.modules.projectimport.eclipse.Workspace;
50 import org.netbeans.modules.projectimport.eclipse.WorkspaceFactory;
51 import org.openide.DialogDescriptor;
52 import org.openide.DialogDisplayer;
53 import org.openide.NotifyDescriptor;
54
55 /**
56  * Represent "Project to import" step(panel) in the Eclipse importer wizard.
57  *
58  * @author mkrauskopf
59  */

60 final class ProjectSelectionPanel extends JPanel JavaDoc {
61     
62     /**
63      * Logger for this class
64      */

65     private static final Logger JavaDoc logger =
66             LoggerFactory.getDefault().createLogger(ProjectSelectionPanel.class);
67     
68     /** Renderer for projects */
69     private class ProjectCellRenderer extends JCheckBox JavaDoc
70             implements TableCellRenderer JavaDoc {
71         public Component JavaDoc getTableCellRendererComponent(JTable JavaDoc table, Object JavaDoc value,
72                 boolean isSelected, boolean hasFocus, int row, int column) {
73             EclipseProject project = projects[row];
74             setColors(this, isSelected);
75             setText(project.getName());
76             setSelected(selectedProjects.contains(project) ||
77                     requiredProjects.contains(project));
78             setToolTipText(null);
79             if (project.hasJavaNature() && !requiredProjects.contains(project)) {
80                 setEnabled(true);
81             } else {
82                 // required and non-java project are disabled
83
setEnabled(false);
84                 if (!project.hasJavaNature()) {
85                     setToolTipText(ProjectImporterWizard.getMessage(
86                             "MSG_NonJavaProject", project.getName())); // NOI18N
87
}
88             }
89             return this;
90         }
91     }
92     
93     private void setColors(Component JavaDoc c, boolean isSelected) {
94         c.setBackground(UIManager.getColor(isSelected ?
95             "Table.selectionBackground" : "Table.background")); // NOI18N
96
c.setForeground(UIManager.getColor(isSelected ?
97             "Table.selectionForeground" : "Table.foreground")); // NOI18N
98
}
99     
100     private class ProjectCellEditor extends AbstractCellEditor JavaDoc
101             implements TableCellEditor JavaDoc {
102         
103         private JCheckBox JavaDoc checkBox;
104         
105         public Object JavaDoc getCellEditorValue() {
106             return Boolean.valueOf(checkBox.isSelected());
107         }
108         
109         public Component JavaDoc getTableCellEditorComponent(JTable JavaDoc table, Object JavaDoc value,
110                 boolean isSelected, int row, int column) {
111             EclipseProject project = projects[row];
112             checkBox = new JCheckBox JavaDoc(project.getName(),
113                     ((Boolean JavaDoc) value).booleanValue());
114             setColors(checkBox, isSelected);
115             checkBox.addActionListener(new ActionListener JavaDoc() {
116                 public void actionPerformed(ActionEvent JavaDoc ev) {
117                     fireEditingStopped();
118                 }
119             });
120             return checkBox;
121         }
122         
123         public boolean shouldSelectCell(java.util.EventObject JavaDoc anEvent) {
124             return true;
125         }
126     }
127     
128     /** All projects in a workspace. */
129     private EclipseProject[] projects;
130     
131     /**
132      * Projects selected by user. So it counts the projects which were selected
133      * by user and then became required (so became disabled). But project which
134      * weren't checked but are required are not members of this set.
135      * This all servers for remembering checked project when working with
136      * project dependencies.
137      */

138     private Set JavaDoc/*<EclipseProject>*/ selectedProjects;
139     
140     /**
141      * All projects we need to import (involving projects which selected
142      * projects depend on.
143      */

144     private Set JavaDoc requiredProjects;
145     
146     /** Error message displayed by wizard. */
147     private String JavaDoc errorMessage;
148     
149     private class ProjectTableModel extends AbstractTableModel JavaDoc {
150         public Object JavaDoc getValueAt(int rowIndex, int columnIndex) {
151             EclipseProject project = projects[rowIndex];
152             return Boolean.valueOf(selectedProjects.contains(project) ||
153                     requiredProjects.contains(project));
154         }
155         
156         public int getRowCount() {
157             return projects != null ? projects.length : 0;
158         }
159         
160         public int getColumnCount() {
161             return 1;
162         }
163         
164         public Class JavaDoc getColumnClass(int columnIndex) {
165             return Boolean JavaDoc.class;
166         }
167         
168         public boolean isCellEditable(int rowIndex, int columnIndex) {
169             return (projects[rowIndex].hasJavaNature() &&
170                     !requiredProjects.contains(projects[rowIndex]));
171         }
172         
173         public void setValueAt(Object JavaDoc aValue, int rowIndex, int columnIndex) {
174             EclipseProject project = projects[rowIndex];
175             assert projects != null;
176             if (((Boolean JavaDoc) aValue).booleanValue()) {
177                 selectedProjects.add(project);
178             } else {
179                 selectedProjects.remove(project);
180             }
181             solveDependencies();
182             fireTableDataChanged();
183             projectTable.getSelectionModel().setLeadSelectionIndex(rowIndex);
184             updateValidity();
185         }
186     }
187     
188     /** Updates panel validity. */
189     public void updateValidity() {
190         if (selectedProjects == null || selectedProjects.isEmpty()) {
191             // user has to select at least one project
192
setErrorMessage(ProjectImporterWizard.getMessage(
193                     "MSG_ProjectIsNotChosed")); // NOI18N
194
return;
195         }
196         String JavaDoc parent = destination.getText();
197         for (Iterator JavaDoc it = allProjects().iterator(); it.hasNext(); ) {
198             EclipseProject prj = (EclipseProject) it.next();
199             String JavaDoc destDir = parent + "/" + prj.getName(); // NOI18N
200
if (new File JavaDoc(destDir).exists()) {
201                 setErrorMessage(ProjectImporterWizard.getMessage(
202                         "MSG_ProjectExist", prj.getName())); // NOI18N
203
return;
204             }
205         }
206         setErrorMessage(null);
207     }
208     
209     /** Returns both selected and required projects */
210     private Collection JavaDoc allProjects() {
211         Collection JavaDoc all = new HashSet JavaDoc(selectedProjects);
212         all.addAll(requiredProjects);
213         return all;
214     }
215     
216     // Helper for recursion check
217
private final Stack JavaDoc/*<EclipseProject>*/ solved = new Stack JavaDoc();
218     private EclipseProject currentRoot;
219     
220     /**
221      * Solves project dependencies. Fills up <code>requiredProjects</code> as
222      * needed.
223      */

224     private void solveDependencies() {
225         requiredProjects.clear();
226         if (selectedProjects == null || selectedProjects.isEmpty()) {
227             return;
228         }
229         for (Iterator JavaDoc it = selectedProjects.iterator(); it.hasNext(); ) {
230             EclipseProject selProject = (EclipseProject) it.next();
231             assert selProject != null;
232             solved.push(selProject);
233             currentRoot = selProject;
234             fillUpRequiredProjects(selProject);
235             EclipseProject poped = (EclipseProject) solved.pop();
236             assert poped.equals(currentRoot);
237             assert solved.isEmpty();
238             currentRoot = null;
239         }
240     }
241     
242     private void fillUpRequiredProjects(EclipseProject project) {
243         Set JavaDoc children = project.getProjects();
244         if (children == null || children.isEmpty()) {
245             return;
246         }
247         for (Iterator JavaDoc it = children.iterator(); it.hasNext(); ) {
248             EclipseProject child = (EclipseProject) it.next();
249             assert child != null;
250             if (solved.contains(child)) {
251                 recursionDetected(child);
252                 return;
253             }
254             requiredProjects.add(child);
255             solved.push(child);
256             fillUpRequiredProjects(child);
257             EclipseProject poped = (EclipseProject) solved.pop();
258             assert poped.equals(child);
259         }
260     }
261     
262     private void recursionDetected(EclipseProject start) {
263         int where = solved.search(start);
264         assert where != -1 : "Cannot find start of the cycle."; // NOI18N
265
EclipseProject rootOfCycle =
266                 (EclipseProject) solved.get(solved.size() - where);
267         StringBuffer JavaDoc cycle = new StringBuffer JavaDoc();
268         for (Iterator JavaDoc it = solved.iterator(); it.hasNext(); ) {
269             cycle.append(((EclipseProject)it.next()).getName()).append(" --> "); // NOI18N
270
}
271         cycle.append(rootOfCycle.getName()).append(" --> ..."); // NOI18N
272
logger.warning("Cycle dependencies was detected. Detected cycle: " + cycle); // NOI18N
273
NotifyDescriptor d = new DialogDescriptor.Message(
274                 ProjectImporterWizard.getMessage("MSG_CycleDependencies", cycle.toString()), // NOI18N
275
NotifyDescriptor.WARNING_MESSAGE);
276         DialogDisplayer.getDefault().notify(d);
277     }
278     
279     /** Creates new form ProjectSelectionPanel */
280     public ProjectSelectionPanel() {
281         initComponents();
282         init();
283         destination.getDocument().addDocumentListener(new DocumentListener JavaDoc() {
284             public void insertUpdate(DocumentEvent JavaDoc e) { updateValidity(); }
285             public void removeUpdate(DocumentEvent JavaDoc e) { updateValidity(); }
286             public void changedUpdate(DocumentEvent JavaDoc e) {}
287         });
288         updateValidity();
289     }
290     
291     private void init() {
292         projectTable.setModel(new ProjectTableModel());
293         projectTable.setTableHeader(null);
294         projectTable.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
295         projectTable.setDefaultRenderer(Boolean JavaDoc.class, new ProjectCellRenderer());
296         projectTable.setDefaultEditor(Boolean JavaDoc.class, new ProjectCellEditor());
297         projectTableSP.getViewport().setBackground(projectTable.getBackground());
298         destination.setText(System.getProperty("user.home")); // NOI18N
299
}
300     
301     /** Loads project from workspace in the given <code>workspaceDir</code>. */
302     void loadProjects(String JavaDoc workspaceDir) {
303         Workspace workspace = null;
304         try {
305             workspace = WorkspaceFactory.getInstance().load(workspaceDir);
306         } catch (ProjectImporterException e) {
307             setErrorMessage(ProjectImporterWizard.getMessage(
308                     "MSG_WorkspaceIsInvalid", workspaceDir)); // NOI18N
309
logger.log(Level.FINE, "ProjectImporterException catched", e); // NOI18N
310
return;
311         }
312         Set JavaDoc wsPrjs = new TreeSet JavaDoc(workspace.getProjects());
313         projects = new EclipseProject[wsPrjs.size()];
314         int i = 0;
315         for (Iterator JavaDoc it = wsPrjs.iterator(); it.hasNext(); ) {
316             projects[i++] = (EclipseProject) it.next();
317         }
318         selectedProjects = new HashSet JavaDoc();
319         requiredProjects = new HashSet JavaDoc();
320         if (projects.length == 0) {
321             setErrorMessage(ProjectImporterWizard.getMessage(
322                     "MSG_WorkspaceIsEmpty", workspaceDir)); // NOI18N
323
} else {
324             updateValidity();
325         }
326     }
327     
328     /** Returns projects selected by selection panel */
329     Set JavaDoc getProjects() {
330         return selectedProjects;
331     }
332     
333     /**
334      * Returns number of projects which will be imported (including both
335      * required and selected projects)
336      */

337     int getNumberOfImportedProject() {
338         return allProjects().size();
339     }
340     
341     /**
342      * Returns destination directory where new NetBeans projects will be stored.
343      */

344     String JavaDoc getDestination() {
345         return destination.getText();
346     }
347     
348     void setErrorMessage(String JavaDoc newMessage) {
349         String JavaDoc oldMessage = this.errorMessage;
350         this.errorMessage = newMessage;
351         firePropertyChange("errorMessage", oldMessage, newMessage); // NOI18N
352
}
353     
354     /** This method is called from within the constructor to
355      * initialize the form.
356      * WARNING: Do NOT modify this code. The content of this method is
357      * always regenerated by the Form Editor.
358      */

359     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
360
private void initComponents() {
361         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
362
363         choosePanel = new javax.swing.JPanel JavaDoc();
364         destination = new javax.swing.JTextField JavaDoc();
365         chooseDestButton = new javax.swing.JButton JavaDoc();
366         prjLocationLBL = new javax.swing.JLabel JavaDoc();
367         projectPanel = new javax.swing.JPanel JavaDoc();
368         projectListLabel = new javax.swing.JLabel JavaDoc();
369         projectTableSP = new javax.swing.JScrollPane JavaDoc();
370         projectTable = new javax.swing.JTable JavaDoc();
371
372         setLayout(new java.awt.BorderLayout JavaDoc(0, 12));
373
374         setBorder(javax.swing.BorderFactory.createEmptyBorder(5, 5, 5, 5));
375         choosePanel.setLayout(new java.awt.GridBagLayout JavaDoc());
376
377         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
378         gridBagConstraints.gridx = 1;
379         gridBagConstraints.gridy = 0;
380         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
381         gridBagConstraints.weightx = 1.0;
382         choosePanel.add(destination, gridBagConstraints);
383
384         org.openide.awt.Mnemonics.setLocalizedText(chooseDestButton, org.openide.util.NbBundle.getMessage(ProjectSelectionPanel.class, "CTL_BrowseButton_B"));
385         chooseDestButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
386             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
387                 chooseDestButtonActionPerformed(evt);
388             }
389         });
390
391         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
392         gridBagConstraints.gridx = 2;
393         gridBagConstraints.gridy = 0;
394         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
395         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 11, 0, 0);
396         choosePanel.add(chooseDestButton, gridBagConstraints);
397
398         prjLocationLBL.setLabelFor(destination);
399         org.openide.awt.Mnemonics.setLocalizedText(prjLocationLBL, org.openide.util.NbBundle.getMessage(ProjectSelectionPanel.class, "LBL_LocationOfNBProjects"));
400         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
401         gridBagConstraints.gridx = 0;
402         gridBagConstraints.gridy = 0;
403         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 0, 12);
404         choosePanel.add(prjLocationLBL, gridBagConstraints);
405
406         add(choosePanel, java.awt.BorderLayout.SOUTH);
407
408         projectPanel.setLayout(new java.awt.GridBagLayout JavaDoc());
409
410         projectListLabel.setLabelFor(projectTable);
411         org.openide.awt.Mnemonics.setLocalizedText(projectListLabel, org.openide.util.NbBundle.getMessage(ProjectSelectionPanel.class, "LBL_ProjectsToImport"));
412         projectListLabel.setVerticalTextPosition(javax.swing.SwingConstants.TOP);
413         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
414         gridBagConstraints.gridx = 0;
415         gridBagConstraints.gridy = 0;
416         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
417         gridBagConstraints.weightx = 1.0;
418         projectPanel.add(projectListLabel, gridBagConstraints);
419
420         projectTable.setShowHorizontalLines(false);
421         projectTable.setShowVerticalLines(false);
422         projectTableSP.setViewportView(projectTable);
423
424         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
425         gridBagConstraints.gridx = 0;
426         gridBagConstraints.gridy = 1;
427         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
428         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
429         gridBagConstraints.weighty = 1.0;
430         gridBagConstraints.insets = new java.awt.Insets JavaDoc(2, 0, 0, 0);
431         projectPanel.add(projectTableSP, gridBagConstraints);
432
433         add(projectPanel, java.awt.BorderLayout.CENTER);
434
435     }// </editor-fold>//GEN-END:initComponents
436

437     private void chooseDestButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_chooseDestButtonActionPerformed
438
JFileChooser JavaDoc chooser = new JFileChooser JavaDoc();
439         chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
440         int ret = chooser.showOpenDialog(this);
441         if (ret == JFileChooser.APPROVE_OPTION) {
442             destination.setText(chooser.getSelectedFile().getAbsolutePath());
443         }
444     }//GEN-LAST:event_chooseDestButtonActionPerformed
445

446     // Variables declaration - do not modify//GEN-BEGIN:variables
447
private javax.swing.JButton JavaDoc chooseDestButton;
448     private javax.swing.JPanel JavaDoc choosePanel;
449     private javax.swing.JTextField JavaDoc destination;
450     private javax.swing.JLabel JavaDoc prjLocationLBL;
451     private javax.swing.JLabel JavaDoc projectListLabel;
452     private javax.swing.JPanel JavaDoc projectPanel;
453     private javax.swing.JTable JavaDoc projectTable;
454     private javax.swing.JScrollPane JavaDoc projectTableSP;
455     // End of variables declaration//GEN-END:variables
456
}
457
Popular Tags