KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.File JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.HashSet JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Set JavaDoc;
28 import javax.swing.event.ChangeEvent JavaDoc;
29 import javax.swing.event.ChangeListener JavaDoc;
30 import org.netbeans.modules.projectimport.ProjectImporterException;
31 import org.netbeans.modules.projectimport.eclipse.ProjectFactory;
32 import org.openide.ErrorManager;
33 import org.openide.WizardDescriptor;
34 import org.openide.filesystems.FileUtil;
35
36 /**
37  * Iterates on the sequence of Eclipse wizard panels.
38  *
39  * @author mkrauskopf
40  */

41 final class EclipseWizardIterator implements
42         WizardDescriptor.Iterator, ChangeListener JavaDoc {
43     
44     private String JavaDoc errorMessage;
45     private SelectionWizardPanel workspacePanel;
46     private ProjectWizardPanel projectPanel;
47     private ImporterWizardPanel current;
48     
49     private boolean hasNext;
50     private boolean hasPrevious;
51     
52     /** Registered ChangeListeners */
53     private List JavaDoc changeListeners;
54     
55     /** Initialize and create an instance. */
56     EclipseWizardIterator() {
57         workspacePanel = new SelectionWizardPanel();
58         workspacePanel.addChangeListener(this);
59         projectPanel = new ProjectWizardPanel();
60         projectPanel.addChangeListener(this);
61         current = workspacePanel;
62     }
63     
64     /** Returns projects selected by selection panel */
65     Set JavaDoc getProjects() {
66         if (workspacePanel.isWorkspaceChosen()) {
67             return projectPanel.getProjects();
68         } else {
69             Set JavaDoc prjs = new HashSet JavaDoc();
70             try {
71                 File JavaDoc projectDirF = FileUtil.normalizeFile(new File JavaDoc(workspacePanel.getProjectDir()));
72                 prjs.add(ProjectFactory.getInstance().load(projectDirF));
73             } catch (ProjectImporterException e) {
74                 ErrorManager.getDefault().log(ErrorManager.ERROR,
75                         "ProjectImporterException catched: " + e); // NOI18N
76
e.printStackTrace();
77             }
78             return prjs;
79         }
80     }
81     
82     /**
83      * Returns number of projects which will be imported (including both
84      * required and selected projects)
85      */

86     int getNumberOfImportedProject() {
87         return (workspacePanel.isWorkspaceChosen() ?
88             projectPanel.getNumberOfImportedProject() : 1);
89     }
90     
91     /**
92      * Returns destination directory where new NetBeans projects will be stored.
93      */

94     String JavaDoc getDestination() {
95         return (workspacePanel.isWorkspaceChosen() ?
96             projectPanel.getDestination() :
97             workspacePanel.getProjectDestinationDir());
98     }
99     
100     /**
101      * Returns whether selected projects should be imported recursively or not.
102      */

103     boolean getRecursively() {
104         return workspacePanel.isWorkspaceChosen();
105     }
106     
107     public void addChangeListener(ChangeListener JavaDoc l) {
108         if (changeListeners == null) {
109             changeListeners = new ArrayList JavaDoc(2);
110         }
111         changeListeners.add(l);
112     }
113     
114     public void removeChangeListener(ChangeListener JavaDoc l) {
115         if (changeListeners != null) {
116             if (changeListeners.remove(l) && changeListeners.isEmpty()) {
117                 changeListeners = null;
118             }
119         }
120     }
121     
122     protected void fireChange() {
123         if (changeListeners != null) {
124             ChangeEvent JavaDoc e = new ChangeEvent JavaDoc(this);
125             for (Iterator JavaDoc i = changeListeners.iterator(); i.hasNext(); ) {
126                 ((ChangeListener JavaDoc) i.next()).stateChanged(e);
127             }
128         }
129     }
130     
131     public void previousPanel() {
132         if (current == projectPanel) {
133             current = workspacePanel;
134             hasPrevious = false;
135             hasNext = true;
136             updateErrorMessage();
137         }
138     }
139     
140     public void nextPanel() {
141         if (current == workspacePanel) {
142             projectPanel.loadProjects(workspacePanel.getWorkspaceDir());
143             current = projectPanel;
144             hasPrevious = true;
145             hasNext = false;
146             updateErrorMessage();
147         }
148     }
149     
150     public String JavaDoc name() {
151         return (current == workspacePanel) ?
152             (workspacePanel.isWorkspaceChosen() ?
153                 ImporterWizardPanel.WORKSPACE_LOCATION_STEP :
154                 ImporterWizardPanel.PROJECT_SELECTION_STEP) :
155                 ImporterWizardPanel.PROJECTS_SELECTION_STEP;
156     }
157     
158     public boolean hasPrevious() {
159         return hasPrevious;
160     }
161     
162     public boolean hasNext() {
163         return hasNext;
164     }
165     
166     public WizardDescriptor.Panel current() {
167         return current;
168     }
169     
170     public void stateChanged(javax.swing.event.ChangeEvent JavaDoc e) {
171         if (current == workspacePanel && current.isValid()) {
172             if (workspacePanel.isWorkspaceChosen()) {
173                 hasNext = true;
174             } else {
175                 hasNext = false;
176             }
177         }
178         updateErrorMessage();
179     }
180     
181     void updateErrorMessage() {
182         errorMessage = current.getErrorMessage();
183         fireChange();
184     }
185     
186     String JavaDoc getErrorMessage() {
187         return errorMessage;
188     }
189 }
190
Popular Tags