001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.projectimport.eclipse.wizard;
043:
044: import java.io.File;
045: import java.util.ArrayList;
046: import java.util.HashSet;
047: import java.util.Iterator;
048: import java.util.List;
049: import java.util.Set;
050: import javax.swing.event.ChangeEvent;
051: import javax.swing.event.ChangeListener;
052: import org.netbeans.modules.projectimport.ProjectImporterException;
053: import org.netbeans.modules.projectimport.eclipse.ProjectFactory;
054: import org.openide.ErrorManager;
055: import org.openide.WizardDescriptor;
056: import org.openide.filesystems.FileUtil;
057:
058: /**
059: * Iterates on the sequence of Eclipse wizard panels.
060: *
061: * @author mkrauskopf
062: */
063: final class EclipseWizardIterator implements WizardDescriptor.Iterator,
064: ChangeListener {
065:
066: private String errorMessage;
067: private SelectionWizardPanel workspacePanel;
068: private ProjectWizardPanel projectPanel;
069: private ImporterWizardPanel current;
070:
071: private boolean hasNext;
072: private boolean hasPrevious;
073:
074: /** Registered ChangeListeners */
075: private List changeListeners;
076:
077: /** Initialize and create an instance. */
078: EclipseWizardIterator() {
079: workspacePanel = new SelectionWizardPanel();
080: workspacePanel.addChangeListener(this );
081: projectPanel = new ProjectWizardPanel();
082: projectPanel.addChangeListener(this );
083: current = workspacePanel;
084: }
085:
086: /** Returns projects selected by selection panel */
087: Set getProjects() {
088: if (workspacePanel.isWorkspaceChosen()) {
089: return projectPanel.getProjects();
090: } else {
091: Set prjs = new HashSet();
092: try {
093: File projectDirF = FileUtil.normalizeFile(new File(
094: workspacePanel.getProjectDir()));
095: prjs
096: .add(ProjectFactory.getInstance().load(
097: projectDirF));
098: } catch (ProjectImporterException e) {
099: ErrorManager.getDefault().log(ErrorManager.ERROR,
100: "ProjectImporterException catched: " + e); // NOI18N
101: e.printStackTrace();
102: }
103: return prjs;
104: }
105: }
106:
107: /**
108: * Returns number of projects which will be imported (including both
109: * required and selected projects)
110: */
111: int getNumberOfImportedProject() {
112: return (workspacePanel.isWorkspaceChosen() ? projectPanel
113: .getNumberOfImportedProject() : 1);
114: }
115:
116: /**
117: * Returns destination directory where new NetBeans projects will be stored.
118: */
119: String getDestination() {
120: return (workspacePanel.isWorkspaceChosen() ? projectPanel
121: .getDestination() : workspacePanel
122: .getProjectDestinationDir());
123: }
124:
125: /**
126: * Returns whether selected projects should be imported recursively or not.
127: */
128: boolean getRecursively() {
129: return workspacePanel.isWorkspaceChosen();
130: }
131:
132: public void addChangeListener(ChangeListener l) {
133: if (changeListeners == null) {
134: changeListeners = new ArrayList(2);
135: }
136: changeListeners.add(l);
137: }
138:
139: public void removeChangeListener(ChangeListener l) {
140: if (changeListeners != null) {
141: if (changeListeners.remove(l) && changeListeners.isEmpty()) {
142: changeListeners = null;
143: }
144: }
145: }
146:
147: protected void fireChange() {
148: if (changeListeners != null) {
149: ChangeEvent e = new ChangeEvent(this );
150: for (Iterator i = changeListeners.iterator(); i.hasNext();) {
151: ((ChangeListener) i.next()).stateChanged(e);
152: }
153: }
154: }
155:
156: public void previousPanel() {
157: if (current == projectPanel) {
158: current = workspacePanel;
159: hasPrevious = false;
160: hasNext = true;
161: updateErrorMessage();
162: }
163: }
164:
165: public void nextPanel() {
166: if (current == workspacePanel) {
167: projectPanel.loadProjects(workspacePanel.getWorkspaceDir());
168: current = projectPanel;
169: hasPrevious = true;
170: hasNext = false;
171: updateErrorMessage();
172: }
173: }
174:
175: public String name() {
176: return (current == workspacePanel) ? (workspacePanel
177: .isWorkspaceChosen() ? ImporterWizardPanel.WORKSPACE_LOCATION_STEP
178: : ImporterWizardPanel.PROJECT_SELECTION_STEP)
179: : ImporterWizardPanel.PROJECTS_SELECTION_STEP;
180: }
181:
182: public boolean hasPrevious() {
183: return hasPrevious;
184: }
185:
186: public boolean hasNext() {
187: return hasNext;
188: }
189:
190: public WizardDescriptor.Panel current() {
191: return current;
192: }
193:
194: public void stateChanged(javax.swing.event.ChangeEvent e) {
195: if (current == workspacePanel && current.isValid()) {
196: if (workspacePanel.isWorkspaceChosen()) {
197: hasNext = true;
198: } else {
199: hasNext = false;
200: }
201: }
202: updateErrorMessage();
203: }
204:
205: void updateErrorMessage() {
206: errorMessage = current.getErrorMessage();
207: fireChange();
208: }
209:
210: String getErrorMessage() {
211: return errorMessage;
212: }
213: }
|