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: package org.netbeans.modules.mercurial.ui.clone;
042:
043: import java.awt.Dialog;
044: import java.awt.event.ActionEvent;
045: import java.awt.event.ActionListener;
046: import java.io.File;
047: import java.io.IOException;
048: import java.util.LinkedList;
049: import java.util.List;
050: import java.util.logging.Level;
051: import javax.swing.BorderFactory;
052: import javax.swing.JFileChooser;
053: import javax.swing.SwingUtilities;
054: import org.netbeans.api.project.Project;
055: import org.netbeans.api.project.ProjectInformation;
056: import org.netbeans.api.project.ProjectManager;
057: import org.netbeans.api.project.ProjectUtils;
058: import org.netbeans.api.project.ui.OpenProjects;
059: import org.netbeans.modules.mercurial.HgProgressSupport;
060: import org.netbeans.spi.project.ui.support.ProjectChooser;
061: import org.openide.DialogDescriptor;
062: import org.openide.DialogDisplayer;
063: import org.openide.filesystems.FileObject;
064: import org.openide.filesystems.FileUtil;
065: import org.openide.util.HelpCtx;
066: import org.openide.util.NbBundle;
067: import org.netbeans.modules.mercurial.Mercurial;
068:
069: /**
070: *
071: * @author Tomas Stupka
072: */
073: public class CloneCompleted implements ActionListener {
074:
075: private final File workingFolder;
076: private final boolean openProject;
077:
078: private CloneCompletedPanel panel;
079: private Dialog dialog;
080: private Project projectToBeOpened;
081:
082: public CloneCompleted(File workingFolder) {
083: this .openProject = true;
084: this .workingFolder = workingFolder;
085: }
086:
087: public void scanForProjects(HgProgressSupport support) {
088:
089: List<Project> clonedProjects = new LinkedList<Project>();
090: File normalizedWorkingFolder = FileUtil
091: .normalizeFile(workingFolder);
092: FileObject fo = FileUtil.toFileObject(normalizedWorkingFolder);
093: if (fo != null) {
094: clonedProjects = ProjectUtilities.scanForProjects(fo);
095: }
096:
097: panel = new CloneCompletedPanel();
098: panel.openButton.addActionListener(this );
099: panel.createButton.addActionListener(this );
100: panel.closeButton.addActionListener(this );
101: panel.setBorder(BorderFactory.createEmptyBorder(6, 6, 6, 6));
102: panel.againCheckBox.setVisible(openProject == false);
103: String title = NbBundle.getMessage(CloneAction.class, "BK3008"); // NOI18N
104: DialogDescriptor descriptor = new DialogDescriptor(panel, title);
105: descriptor.setModal(true);
106:
107: // move buttons from dialog to descriptor
108: panel.remove(panel.openButton);
109: panel.remove(panel.createButton);
110: panel.remove(panel.closeButton);
111:
112: Object[] options = null;
113: if (clonedProjects.size() > 1) {
114: String msg = NbBundle.getMessage(CloneAction.class,
115: "BK3009", new Integer(clonedProjects.size())); // NOI18N
116: panel.jLabel1.setText(msg);
117: options = new Object[] { panel.openButton,
118: panel.closeButton };
119: } else if (clonedProjects.size() == 1) {
120: Project project = (Project) clonedProjects.iterator()
121: .next();
122: projectToBeOpened = project;
123: ProjectInformation projectInformation = ProjectUtils
124: .getInformation(project);
125: String projectName = projectInformation.getDisplayName();
126: String msg = NbBundle.getMessage(CloneAction.class,
127: "BK3011", projectName); // NOI18N
128: panel.jLabel1.setText(msg);
129: panel.openButton.setText(NbBundle.getMessage(
130: CloneAction.class, "BK3012")); // NOI18N
131: options = new Object[] { panel.openButton,
132: panel.closeButton };
133: } else {
134: String msg = NbBundle.getMessage(CloneAction.class,
135: "BK3010"); // NOI18N
136: panel.jLabel1.setText(msg);
137: options = new Object[] { panel.createButton,
138: panel.closeButton };
139: }
140:
141: descriptor.setMessageType(DialogDescriptor.INFORMATION_MESSAGE);
142: descriptor.setOptions(options);
143: descriptor.setClosingOptions(options);
144: descriptor.setHelpCtx(new HelpCtx(CloneCompletedPanel.class));
145: dialog = DialogDisplayer.getDefault().createDialog(descriptor);
146: dialog.getAccessibleContext().setAccessibleDescription(
147: NbBundle.getMessage(CloneAction.class,
148: "ACSD_CloneCompleted_Dialog")); // NOI18N
149: if (support != null && support.isCanceled()) {
150: return;
151: }
152: SwingUtilities.invokeLater(new Runnable() {
153:
154: public void run() {
155: dialog.setVisible(true);
156: }
157: });
158: }
159:
160: public void actionPerformed(ActionEvent e) {
161: Object src = e.getSource();
162: dialog.setVisible(false);
163: if (panel.openButton.equals(src)) {
164: // show project chooser
165: if (projectToBeOpened == null) {
166: JFileChooser chooser = ProjectChooser.projectChooser();
167: chooser.setCurrentDirectory(workingFolder);
168: chooser.setMultiSelectionEnabled(true);
169: chooser.showOpenDialog(dialog);
170: File[] projectDirs = chooser.getSelectedFiles();
171: for (int i = 0; i < projectDirs.length; i++) {
172: File projectDir = projectDirs[i];
173: FileObject projectFolder = FileUtil
174: .toFileObject(projectDir);
175: if (projectFolder != null) {
176: try {
177: if (projectFolder != null) {
178: Project p = ProjectManager.getDefault()
179: .findProject(projectFolder);
180: openProject(p);
181: }
182: } catch (IOException e1) {
183: Throwable cause = new Throwable(NbBundle
184: .getMessage(CloneAction.class,
185: "BK1014", projectFolder));
186: e1.initCause(cause);
187: Mercurial.LOG.log(Level.INFO, null, e1);
188: }
189: }
190: }
191: } else {
192: openProject(projectToBeOpened);
193: }
194: } else if (panel.createButton.equals(src)) {
195: ProjectUtilities.newProjectWizard(workingFolder);
196: }
197: }
198:
199: private void openProject(Project p) {
200: if (p == null)
201: return;
202:
203: Project[] projects = new Project[] { p };
204: OpenProjects.getDefault().open(projects, false);
205:
206: // set as main project and expand
207: OpenProjects.getDefault().setMainProject(p);
208: ProjectUtilities.selectAndExpandProject(p);
209: }
210: }
|