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.wizards;
042:
043: import java.awt.Component;
044: import java.awt.Dialog;
045: import java.text.MessageFormat;
046: import javax.swing.JComponent;
047: import javax.swing.event.ChangeEvent;
048: import javax.swing.event.ChangeListener;
049: import org.openide.DialogDisplayer;
050: import org.openide.WizardDescriptor;
051: import org.openide.util.HelpCtx;
052: import org.openide.util.actions.CallableSystemAction;
053:
054: import java.util.List;
055: import java.io.File;
056: import org.netbeans.modules.mercurial.HgException;
057: import org.netbeans.modules.mercurial.HgProgressSupport;
058: import org.netbeans.modules.mercurial.Mercurial;
059: import org.netbeans.modules.mercurial.util.HgUtils;
060: import org.netbeans.modules.mercurial.util.HgCommand;
061: import org.openide.util.NbBundle;
062: import org.openide.util.RequestProcessor;
063: import org.openide.DialogDisplayer;
064: import org.openide.NotifyDescriptor;
065: import org.netbeans.modules.mercurial.ui.clone.CloneAction;
066:
067: // An example action demonstrating how the wizard could be called from within
068: // your code. You can copy-paste the code below wherever you need.
069: public final class CloneWizardAction extends CallableSystemAction
070: implements ChangeListener {
071:
072: private WizardDescriptor.Panel<WizardDescriptor>[] panels;
073:
074: private static CloneWizardAction instance;
075: private WizardDescriptor wizardDescriptor;
076: private CloneRepositoryWizardPanel cloneRepositoryWizardPanel;
077: private CloneDestinationDirectoryWizardPanel cloneDestinationDirectoryWizardPanel;
078: private ClonePathsWizardPanel clonePathsWizardPanel;
079: private PanelsIterator wizardIterator;
080: private String errorMessage;
081:
082: public static synchronized CloneWizardAction getInstance() {
083: if (instance == null) {
084: instance = new CloneWizardAction();
085: }
086: return instance;
087: }
088:
089: @SuppressWarnings("unchecked")
090: public void performAction() {
091: wizardIterator = new PanelsIterator();
092: wizardDescriptor = new WizardDescriptor(wizardIterator);
093:
094: // {0} will be replaced by WizardDesriptor.Panel.getComponent().getName()
095: wizardDescriptor.setTitleFormat(new MessageFormat("{0}")); // NOI18N
096: wizardDescriptor.setTitle(org.openide.util.NbBundle.getMessage(
097: CloneWizardAction.class, "CTL_Clone")); // NOI18N
098: Dialog dialog = DialogDisplayer.getDefault().createDialog(
099: wizardDescriptor);
100: dialog.setVisible(true);
101: dialog.toFront();
102: boolean cancelled = wizardDescriptor.getValue() != WizardDescriptor.FINISH_OPTION;
103: if (!cancelled) {
104: final String repository = (String) wizardDescriptor
105: .getProperty("repository"); // NOI18N
106: final String username = (String) wizardDescriptor
107: .getProperty("username"); // NOI18N
108: final String password = (String) wizardDescriptor
109: .getProperty("password"); // NOI18N
110: final String directory = (String) wizardDescriptor
111: .getProperty("directory"); // NOI18N
112: final String cloneName = (String) wizardDescriptor
113: .getProperty("cloneName"); // NOI18N
114: final String pullPath = (String) wizardDescriptor
115: .getProperty("defaultPullPath"); // NOI18N
116: final String pushPath = (String) wizardDescriptor
117: .getProperty("defaultPushPath"); // NOI18N
118: File cloneFile = new File(directory, cloneName);
119: CloneAction.performClone(repository, cloneFile
120: .getAbsolutePath(), true, null, pullPath, pushPath);
121: }
122: }
123:
124: public void stateChanged(ChangeEvent e) {
125: if (wizardIterator == null) {
126: return;
127: }
128: WizardDescriptor.Panel step = wizardIterator.current();
129: if (step == null) {
130: return;
131: }
132: if (step == cloneRepositoryWizardPanel) {
133: errorMessage = cloneRepositoryWizardPanel.getErrorMessage();
134: } else if (step == clonePathsWizardPanel) {
135: errorMessage = clonePathsWizardPanel.getErrorMessage();
136: } else if (step == cloneDestinationDirectoryWizardPanel) {
137: errorMessage = cloneDestinationDirectoryWizardPanel
138: .getErrorMessage();
139: }
140: if (wizardDescriptor != null) {
141: wizardDescriptor.putProperty("WizardPanel_errorMessage",
142: errorMessage); // NOI18N
143: }
144: }
145:
146: public String getName() {
147: return "Start Sample Wizard"; // NOI18N
148: }
149:
150: public String iconResource() {
151: return null;
152: }
153:
154: public HelpCtx getHelpCtx() {
155: return new HelpCtx(CloneWizardAction.class);
156: }
157:
158: protected boolean asynchronous() {
159: return false;
160: }
161:
162: /**
163: * Initialize panels representing individual wizard's steps and sets
164: * various properties for them influencing wizard appearance.
165: */
166: @SuppressWarnings("unchecked")
167: private class PanelsIterator extends WizardDescriptor.ArrayIterator {
168:
169: PanelsIterator() {
170: }
171:
172: protected WizardDescriptor.Panel[] initializePanels() {
173: WizardDescriptor.Panel[] panels = new WizardDescriptor.Panel[2];
174: cloneRepositoryWizardPanel = new CloneRepositoryWizardPanel();
175: clonePathsWizardPanel = new ClonePathsWizardPanel();
176: cloneDestinationDirectoryWizardPanel = new CloneDestinationDirectoryWizardPanel();
177: panels = new WizardDescriptor.Panel[] {
178: cloneRepositoryWizardPanel, clonePathsWizardPanel,
179: cloneDestinationDirectoryWizardPanel };
180: panels[0].addChangeListener(CloneWizardAction.this );
181: panels[1].addChangeListener(CloneWizardAction.this );
182: String[] steps = new String[panels.length];
183: for (int i = 0; i < panels.length; i++) {
184: Component c = panels[i].getComponent();
185: // Default step name to component name of panel. Mainly useful
186: // for getting the name of the target chooser to appear in the
187: // list of steps.
188: steps[i] = c.getName();
189: if (c instanceof JComponent) { // assume Swing components
190: JComponent jc = (JComponent) c;
191: // Sets step number of a component
192: jc.putClientProperty(
193: "WizardPanel_contentSelectedIndex",
194: new Integer(i)); // NOI18N
195: // Sets steps names for a panel
196: jc.putClientProperty("WizardPanel_contentData",
197: steps); // NOI18N
198: // Turn on subtitle creation on each step
199: jc.putClientProperty("WizardPanel_autoWizardStyle",
200: Boolean.TRUE); // NOI18N
201: // Show steps on the left side with the image on the background
202: jc.putClientProperty(
203: "WizardPanel_contentDisplayed",
204: Boolean.TRUE); // NOI18N
205: // Turn on numbering of all steps
206: jc.putClientProperty("WizardPanel_contentNumbered",
207: Boolean.TRUE); // NOI18N
208: }
209: }
210: return panels;
211: }
212: }
213: }
|