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.diff;
042:
043: import org.netbeans.modules.versioning.spi.VCSContext;
044:
045: import javax.swing.*;
046: import java.awt.event.ActionEvent;
047: import java.io.File;
048: import java.util.List;
049:
050: import org.netbeans.modules.mercurial.HgException;
051: import org.netbeans.modules.mercurial.HgProgressSupport;
052: import org.netbeans.modules.mercurial.Mercurial;
053: import org.netbeans.modules.mercurial.OutputLogger;
054: import org.netbeans.modules.mercurial.HgModuleConfig;
055: import org.netbeans.modules.mercurial.util.HgUtils;
056: import org.netbeans.modules.mercurial.util.HgRepositoryContextCache;
057: import org.netbeans.modules.mercurial.util.HgCommand;
058: import org.netbeans.modules.mercurial.ui.actions.ContextAction;
059: import org.openide.util.NbBundle;
060: import org.openide.util.RequestProcessor;
061: import org.openide.DialogDisplayer;
062: import org.openide.DialogDescriptor;
063: import org.openide.NotifyDescriptor;
064: import java.awt.event.ActionListener;
065: import java.awt.Dialog;
066: import javax.swing.JFileChooser;
067: import javax.swing.filechooser.FileFilter;
068: import org.netbeans.modules.versioning.util.AccessibleJFileChooser;
069:
070: /**
071: * ImportDiff action for mercurial:
072: * hg export
073: *
074: * @author Padraig O'Briain
075: */
076: public class ImportDiffAction extends ContextAction {
077:
078: private final VCSContext context;
079:
080: public ImportDiffAction(String name, VCSContext context) {
081: this .context = context;
082: putValue(Action.NAME, name);
083: }
084:
085: public void performAction(ActionEvent e) {
086: importDiff(context);
087: }
088:
089: public boolean isEnabled() {
090: return HgUtils.getRootFile(context) != null;
091: }
092:
093: private static void importDiff(VCSContext ctx) {
094: final File root = HgUtils.getRootFile(ctx);
095: final JFileChooser fileChooser = new AccessibleJFileChooser(
096: NbBundle.getMessage(ImportDiffAction.class,
097: "ACSD_ImportBrowseFolder"), null); // NO I18N
098: fileChooser.setDialogTitle(NbBundle.getMessage(
099: ImportDiffAction.class, "ImportBrowse_title")); // NO I18N
100: fileChooser.setMultiSelectionEnabled(false);
101: FileFilter[] old = fileChooser.getChoosableFileFilters();
102: for (int i = 0; i < old.length; i++) {
103: FileFilter fileFilter = old[i];
104: fileChooser.removeChoosableFileFilter(fileFilter);
105:
106: }
107: fileChooser.setApproveButtonMnemonic(NbBundle.getMessage(
108: ImportDiffAction.class, "OK_Button").charAt(0)); // NO I18N
109: fileChooser.setApproveButtonText(NbBundle.getMessage(
110: ImportDiffAction.class, "OK_Button")); // NO I18N
111: fileChooser.setCurrentDirectory(new File(HgModuleConfig
112: .getDefault().getImportFolder()));
113:
114: DialogDescriptor dd = new DialogDescriptor(fileChooser,
115: NbBundle.getMessage(ImportDiffAction.class,
116: "Browse_title")); // NO I18N
117: dd.setOptions(new Object[0]);
118: final Dialog dialog = DialogDisplayer.getDefault()
119: .createDialog(dd);
120: fileChooser.addActionListener(new ActionListener() {
121: public void actionPerformed(ActionEvent e) {
122: String state = e.getActionCommand();
123: if (state.equals(JFileChooser.APPROVE_SELECTION)) {
124: final File patchFile = fileChooser
125: .getSelectedFile();
126:
127: HgModuleConfig.getDefault().setImportFolder(
128: patchFile.getParent());
129: if (patchFile != null) {
130: RequestProcessor rp = Mercurial.getInstance()
131: .getRequestProcessor(
132: root.getAbsolutePath());
133: HgProgressSupport support = new HgProgressSupport() {
134: public void perform() {
135: OutputLogger logger = getLogger();
136: performImport(root, patchFile, logger);
137: }
138: };
139: support.start(rp, root.getAbsolutePath(),
140: org.openide.util.NbBundle.getMessage(
141: ImportDiffAction.class,
142: "LBL_ImportDiff_Progress")); // NOI18N
143: }
144: }
145: dialog.dispose();
146: }
147: });
148: dialog.setVisible(true);
149: }
150:
151: private static void performImport(File repository, File patchFile,
152: OutputLogger logger) {
153: try {
154: logger.outputInRed(NbBundle.getMessage(
155: ImportDiffAction.class, "MSG_IMPORT_TITLE")); // NOI18N
156: logger.outputInRed(NbBundle.getMessage(
157: ImportDiffAction.class, "MSG_IMPORT_TITLE_SEP")); // NOI18N
158:
159: List<String> list = HgCommand.doImport(repository,
160: patchFile, logger);
161: Mercurial.getInstance().changesetChanged(repository);
162: logger.output(list); // NOI18N
163:
164: } catch (HgException ex) {
165: NotifyDescriptor.Exception e = new NotifyDescriptor.Exception(
166: ex);
167: DialogDisplayer.getDefault().notifyLater(e);
168: } finally {
169: logger.outputInRed(NbBundle.getMessage(
170: ImportDiffAction.class, "MSG_IMPORT_DONE")); // NOI18N
171: logger.output(""); // NOI18N
172: }
173: }
174: }
|