KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > subversion > ui > relocate > RelocateAction


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.subversion.ui.relocate;
20
21 import java.awt.Dialog JavaDoc;
22 import java.awt.event.ActionEvent JavaDoc;
23 import java.awt.event.ActionListener JavaDoc;
24 import java.io.File JavaDoc;
25 import java.net.MalformedURLException JavaDoc;
26 import java.util.ResourceBundle JavaDoc;
27 import java.util.Set JavaDoc;
28 import javax.swing.AbstractAction JavaDoc;
29 import javax.swing.JButton JavaDoc;
30 import javax.swing.event.DocumentEvent JavaDoc;
31 import javax.swing.event.DocumentListener JavaDoc;
32 import org.netbeans.modules.subversion.Subversion;
33 import org.netbeans.modules.subversion.client.SvnClient;
34 import org.netbeans.modules.subversion.client.SvnProgressSupport;
35 import org.netbeans.modules.subversion.util.SvnUtils;
36 import org.netbeans.modules.versioning.spi.VCSContext;
37 import org.openide.DialogDescriptor;
38 import org.openide.DialogDisplayer;
39 import org.openide.ErrorManager;
40 import org.openide.util.HelpCtx;
41 import org.openide.util.NbBundle;
42 import org.openide.util.RequestProcessor;
43 import org.tigris.subversion.svnclientadapter.SVNClientException;
44 import org.tigris.subversion.svnclientadapter.SVNUrl;
45
46 /**
47  *
48  * @author Peter Pis
49  */

50 public class RelocateAction extends AbstractAction JavaDoc {
51     
52     VCSContext ctx;
53     SvnProgressSupport support;
54     
55     /** Creates a new instance of RelocateAction */
56     public RelocateAction(String JavaDoc name, VCSContext ctx) {
57         super(name);
58         this.ctx = ctx;
59     }
60     
61     public boolean isEnabled() {
62         Set JavaDoc<File JavaDoc> roots = ctx.getRootFiles();
63         if(roots == null || roots.size() != 1) {
64             return false;
65         }
66         File JavaDoc file = roots.iterator().next();
67         return file != null && file.isDirectory();
68     }
69     
70     public void actionPerformed(ActionEvent JavaDoc event) {
71         ResourceBundle JavaDoc loc = NbBundle.getBundle(RelocateAction.class);
72         
73         final RelocatePanel panel = new RelocatePanel();
74         panel.getCurrentURL().setText(getCurrentURL());
75         panel.getWorkingCopy().setText(getWorkingCopy());
76         
77         
78         final JButton JavaDoc btnRelocate = new JButton JavaDoc(loc.getString("CTL_Relocate_Action_Name"));
79         btnRelocate.setEnabled(false);
80         btnRelocate.setToolTipText(loc.getString("TT_Relocate_Action"));
81         
82         panel.getNewURL().getDocument().addDocumentListener(new DocumentListener JavaDoc() {
83             public void insertUpdate(DocumentEvent JavaDoc event) {
84                 validate(panel, btnRelocate);
85             }
86
87             public void removeUpdate(DocumentEvent JavaDoc event) {
88                 validate(panel, btnRelocate);
89             }
90
91             public void changedUpdate(DocumentEvent JavaDoc event) {
92                 validate(panel, btnRelocate);
93             }
94         });
95         JButton JavaDoc btnCancel = new JButton JavaDoc(loc.getString("CTL_Relocate_Action_Cancel"));
96         btnCancel.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(RelocateAction.class, "ACSD_Relocate_Action_Cancel")); // NOI18N
97
DialogDescriptor descriptor = new DialogDescriptor(panel, loc.getString("CTL_Relocate_Title"), true, new Object JavaDoc [] {btnRelocate, btnCancel}, btnRelocate, DialogDescriptor.BOTTOM_ALIGN, null, null);
98         descriptor.setClosingOptions(null);
99         descriptor.setHelpCtx(new HelpCtx(RelocateAction.class));
100         Dialog JavaDoc dialog = DialogDisplayer.getDefault().createDialog(descriptor);
101         dialog.getAccessibleContext().setAccessibleDescription(loc.getString("ACSD_Relocate"));
102         
103         dialog.setVisible(true);
104         if (descriptor.getValue() != btnRelocate)
105             return;
106         
107         panel.getNewURL().addActionListener(new ActionListener JavaDoc() {
108             public void actionPerformed(ActionEvent JavaDoc event) {
109                 validate(panel, btnRelocate);
110             }
111         });
112         
113         final String JavaDoc newUrl = panel.getNewURL().getText();
114         
115         File JavaDoc root = ctx.getRootFiles().iterator().next();
116         final SVNUrl repositoryUrl = SvnUtils.getRepositoryRootUrl(root);
117         final String JavaDoc wc = root.getAbsolutePath();
118         RequestProcessor rp = Subversion.getInstance().getRequestProcessor(repositoryUrl);
119         try {
120             support = new SvnProgressSupport() {
121                 SvnClient client = null;
122                 protected void perform() {
123                     try {
124                         client = Subversion.getInstance().getClient(repositoryUrl);
125                     } catch (SVNClientException ex) {
126                         ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
127                         return;
128                     }
129                     
130                     try {
131                         client.relocate(repositoryUrl.toString(), newUrl, wc, true);
132                     } catch (SVNClientException ex) {
133                         ErrorManager.getDefault().notify(ErrorManager.ERROR, ex);
134                     } catch (Exception JavaDoc e) {
135                         ErrorManager.getDefault().notify(ErrorManager.ERROR, e);
136                     }
137                 }
138             };
139             support.start(rp, repositoryUrl, loc.getString("LBL_Relocate_Progress"));
140         } finally {
141             support = null;
142         }
143     }
144     
145     private String JavaDoc getCurrentURL() {
146         File JavaDoc root = ctx.getRootFiles().iterator().next();
147         final SVNUrl repositoryUrl = SvnUtils.getRepositoryRootUrl(root);
148         return repositoryUrl.toString();
149     }
150     
151     private String JavaDoc getWorkingCopy() {
152         File JavaDoc root = ctx.getRootFiles().iterator().next();
153         final String JavaDoc working = root.getAbsolutePath();
154         return working;
155     }
156     
157     public void validate(RelocatePanel panel, JButton JavaDoc btnOk) {
158         try {
159             new SVNUrl(panel.getNewURL().getText());
160             btnOk.setEnabled(true);
161         } catch (MalformedURLException JavaDoc e) {
162             btnOk.setEnabled(false);
163         }
164         
165     }
166 }
Popular Tags