2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org
.jetbrains
.idea
.svn
.dialogs
;
18 import com
.intellij
.openapi
.diagnostic
.Logger
;
19 import com
.intellij
.openapi
.fileChooser
.FileChooserDescriptor
;
20 import com
.intellij
.openapi
.help
.HelpManager
;
21 import com
.intellij
.openapi
.options
.ConfigurationException
;
22 import com
.intellij
.openapi
.project
.Project
;
23 import com
.intellij
.openapi
.ui
.DialogWrapper
;
24 import com
.intellij
.openapi
.ui
.TextFieldWithBrowseButton
;
25 import com
.intellij
.openapi
.ui
.Messages
;
26 import com
.intellij
.openapi
.util
.IconLoader
;
27 import com
.intellij
.openapi
.vcs
.ProjectLevelVcsManager
;
28 import com
.intellij
.openapi
.vcs
.VcsException
;
29 import com
.intellij
.openapi
.vfs
.LocalFileSystem
;
30 import com
.intellij
.openapi
.vfs
.VirtualFile
;
31 import com
.intellij
.ui
.ComboboxWithBrowseButton
;
32 import com
.intellij
.ui
.DocumentAdapter
;
33 import com
.intellij
.util
.ArrayUtil
;
34 import org
.jetbrains
.annotations
.NonNls
;
35 import org
.jetbrains
.idea
.svn
.SvnBranchConfiguration
;
36 import org
.jetbrains
.idea
.svn
.SvnBranchConfigurationManager
;
37 import org
.jetbrains
.idea
.svn
.SvnBundle
;
38 import org
.jetbrains
.idea
.svn
.SvnVcs
;
39 import org
.jetbrains
.idea
.svn
.branchConfig
.SvnBranchConfigurationNew
;
40 import org
.jetbrains
.idea
.svn
.update
.SvnRevisionPanel
;
41 import org
.tmatesoft
.svn
.core
.SVNException
;
42 import org
.tmatesoft
.svn
.core
.internal
.util
.SVNEncodingUtil
;
43 import org
.tmatesoft
.svn
.core
.internal
.util
.SVNPathUtil
;
44 import org
.tmatesoft
.svn
.core
.wc
.SVNInfo
;
45 import org
.tmatesoft
.svn
.core
.wc
.SVNRevision
;
46 import org
.tmatesoft
.svn
.core
.wc
.SVNWCClient
;
49 import javax
.swing
.event
.ChangeEvent
;
50 import javax
.swing
.event
.ChangeListener
;
51 import javax
.swing
.event
.DocumentEvent
;
53 import java
.awt
.event
.ActionEvent
;
54 import java
.awt
.event
.ActionListener
;
58 * Created by IntelliJ IDEA.
63 public class CopyDialog
extends DialogWrapper
{
64 private static final Logger LOG
= Logger
.getInstance("org.jetbrains.idea.svn.dialogs.CopyDialog");
66 private final File mySrcFile
;
67 private String mySrcURL
;
68 private final Project myProject
;
71 private TextFieldWithBrowseButton myToURLText
;
73 private JTextArea myCommentText
;
74 private JPanel myTopPanel
;
75 private JRadioButton myWorkingCopyRadioButton
;
76 private JRadioButton myRepositoryRadioButton
;
77 private TextFieldWithBrowseButton myWorkingCopyField
;
78 private TextFieldWithBrowseButton myRepositoryField
;
79 private SvnRevisionPanel myRevisionPanel
;
80 private ComboboxWithBrowseButton myBranchTagBaseComboBox
;
81 private JTextField myBranchTextField
;
82 private JRadioButton myBranchOrTagRadioButton
;
83 private JRadioButton myAnyLocationRadioButton
;
84 private JButton myProjectButton
;
85 private JLabel myErrorLabel
;
87 @NonNls private static final String HELP_ID
= "vcs.subversion.branch";
88 private SvnBranchConfigurationNew myBranchConfiguration
;
89 private final VirtualFile mySrcVirtualFile
;
91 public CopyDialog(final Project project
, boolean canBeParent
, File file
) {
92 super(project
, canBeParent
);
96 setTitle(SvnBundle
.message("dialog.title.branch"));
97 getHelpAction().setEnabled(true);
98 myProjectButton
.setIcon(IconLoader
.getIcon("/nodes/ideaProject.png"));
99 myBranchTagBaseComboBox
.setPreferredSize(new Dimension(myBranchTagBaseComboBox
.getPreferredSize().width
,
100 myWorkingCopyField
.getPreferredSize().height
));
102 myWorkingCopyField
.addBrowseFolderListener("Select Working Copy Location", "Select Location to Copy From:",
103 project
, new FileChooserDescriptor(false, true, false, false, false, false));
104 myWorkingCopyField
.getTextField().getDocument().addDocumentListener(new DocumentAdapter() {
105 protected void textChanged(final DocumentEvent e
) {
109 myRepositoryField
.addActionListener(new ActionListener() {
110 public void actionPerformed(final ActionEvent e
) {
111 String url
= SelectLocationDialog
.selectLocation(project
, myRepositoryField
.getText());
113 myRepositoryField
.setText(url
);
117 myRepositoryField
.getTextField().getDocument().addDocumentListener(new DocumentAdapter() {
118 protected void textChanged(final DocumentEvent e
) {
122 myToURLText
.addActionListener(new ActionListener() {
123 public void actionPerformed(final ActionEvent e
) {
124 String url
= myToURLText
.getText();
125 String dstName
= SVNPathUtil
.tail(mySrcURL
);
126 dstName
= SVNEncodingUtil
.uriDecode(dstName
);
128 SelectLocationDialog dialog
= new SelectLocationDialog(myProject
, SVNPathUtil
.removeTail(url
), SvnBundle
.message("label.copy.select.location.dialog.copy.as"), dstName
, false);
131 url
= dialog
.getSelectedURL();
132 String name
= dialog
.getDestinationName();
133 url
= SVNPathUtil
.append(url
, name
);
134 myToURLText
.setText(url
);
137 catch (SVNException e1
) {
138 Messages
.showErrorDialog(project
, SvnBundle
.message("select.location.invalid.url.message", url
),
139 SvnBundle
.message("dialog.title.select.repository.location"));
140 //typed text can not be parsed - no information on what repository to use
145 VirtualFile srcVirtualFile
= LocalFileSystem
.getInstance().findFileByIoFile(file
);
146 srcVirtualFile
= ProjectLevelVcsManager
.getInstance(project
).getVcsRootFor(srcVirtualFile
);
147 this.mySrcVirtualFile
= srcVirtualFile
;
149 myRevisionPanel
.setRoot(mySrcVirtualFile
);
150 myRevisionPanel
.setProject(myProject
);
151 myRevisionPanel
.setUrlProvider(new SvnRevisionPanel
.UrlProvider() {
152 public String
getUrl() {
156 updateBranchTagBases();
158 myRevisionPanel
.addChangeListener(new ChangeListener() {
159 public void stateChanged(final ChangeEvent e
) {
160 getOKAction().setEnabled(isOKActionEnabled());
165 ActionListener listener
= new ActionListener() {
166 public void actionPerformed(ActionEvent e
) {
170 myWorkingCopyRadioButton
.addActionListener(listener
);
171 myRepositoryRadioButton
.addActionListener(listener
);
172 myBranchOrTagRadioButton
.addActionListener(listener
);
173 myAnyLocationRadioButton
.addActionListener(listener
);
175 myBranchTextField
.getDocument().addDocumentListener(new DocumentAdapter() {
176 protected void textChanged(final DocumentEvent e
) {
181 myProjectButton
.addActionListener(new ActionListener() {
182 public void actionPerformed(ActionEvent e
) {
183 myRepositoryField
.setText(myBranchConfiguration
.getBaseUrl(mySrcURL
));
186 myBranchTagBaseComboBox
.addActionListener(new ActionListener() {
187 public void actionPerformed(ActionEvent e
) {
188 BranchConfigurationDialog
.configureBranches(project
, mySrcVirtualFile
);
189 updateBranchTagBases();
193 myBranchTagBaseComboBox
.getComboBox().addActionListener(new ActionListener() {
194 public void actionPerformed(ActionEvent e
) {
201 private void updateBranchTagBases() {
203 myBranchConfiguration
= SvnBranchConfigurationManager
.getInstance(myProject
).get(mySrcVirtualFile
);
204 final String
[] strings
= ArrayUtil
.toStringArray(myBranchConfiguration
.getBranchUrls());
205 myBranchTagBaseComboBox
.getComboBox().setModel(new DefaultComboBoxModel(strings
));
207 catch (VcsException e
) {
209 myBranchTagBaseComboBox
.setEnabled(false);
213 private void updateToURL() {
214 if (myBranchConfiguration
== null) {
218 if (myWorkingCopyRadioButton
.isSelected()) {
219 relativeUrl
= myBranchConfiguration
.getRelativeUrl(mySrcURL
);
222 relativeUrl
= myBranchConfiguration
.getRelativeUrl(myRepositoryField
.getText());
225 final Object selectedBranch
= myBranchTagBaseComboBox
.getComboBox().getSelectedItem();
226 if (relativeUrl
!= null && selectedBranch
!= null) {
227 myToURLText
.setText(selectedBranch
.toString() + "/" + myBranchTextField
.getText() + relativeUrl
);
231 private void updateControls() {
232 myWorkingCopyField
.setEnabled(myWorkingCopyRadioButton
.isSelected());
233 myRepositoryField
.setEnabled(myRepositoryRadioButton
.isSelected());
234 myRevisionPanel
.setEnabled(myRepositoryRadioButton
.isSelected());
235 myProjectButton
.setEnabled(myRepositoryRadioButton
.isSelected());
237 myBranchTagBaseComboBox
.setEnabled(myBranchOrTagRadioButton
.isSelected());
238 myBranchTextField
.setEnabled(myBranchOrTagRadioButton
.isSelected());
239 myToURLText
.setEnabled(myAnyLocationRadioButton
.isSelected());
241 getOKAction().setEnabled(isOKActionEnabled());
244 protected Action
[] createActions() {
245 return new Action
[]{getOKAction(), getCancelAction(), getHelpAction()};
248 protected void doHelpAction() {
249 HelpManager
.getInstance().invokeHelp(HELP_ID
);
252 protected void init() {
254 SvnVcs vcs
= SvnVcs
.getInstance(myProject
);
257 SVNWCClient client
= vcs
.createWCClient();
258 SVNInfo info
= client
.doInfo(mySrcFile
, SVNRevision
.WORKING
);
260 mySrcURL
= info
.getURL() == null ?
null : info
.getURL().toString();
261 revStr
= String
.valueOf(info
.getRevision());
265 catch (SVNException e
) {
271 myWorkingCopyField
.setText(mySrcFile
.toString());
272 myRepositoryField
.setText(mySrcURL
);
273 myToURLText
.setText(myURL
);
274 myRevisionPanel
.setRevisionText(revStr
);
277 myWorkingCopyRadioButton
.setSelected(true);
280 public String
getComment() {
281 return myCommentText
.getText();
284 public SVNRevision
getRevision() {
285 if (myWorkingCopyRadioButton
.isSelected()) {
286 return SVNRevision
.WORKING
;
290 return myRevisionPanel
.getRevision();
292 catch (ConfigurationException e
) {
293 return SVNRevision
.UNDEFINED
;
298 public String
getToURL() {
299 return myToURLText
.getText();
302 protected JComponent
createCenterPanel() {
306 public JComponent
getPreferredFocusedComponent() {
310 public boolean shouldCloseOnCross() {
314 protected String
getDimensionServiceKey() {
315 return "svn.copyDialog";
318 public boolean isOKActionEnabled() {
319 myErrorLabel
.setText(" ");
323 if (myBranchOrTagRadioButton
.isSelected() && myBranchTagBaseComboBox
.getComboBox().getSelectedItem() == null) {
324 myErrorLabel
.setText(SvnBundle
.message("create.branch.no.base.location.error"));
327 String url
= myToURLText
.getText();
328 if (url
!= null && url
.trim().length() > 0) {
329 if (myRepositoryRadioButton
.isSelected()) {
330 SVNRevision revision
;
332 revision
= myRevisionPanel
.getRevision();
334 catch (ConfigurationException e
) {
335 revision
= SVNRevision
.UNDEFINED
;
337 if (!revision
.isValid() || revision
.isLocal()) {
338 myErrorLabel
.setText(SvnBundle
.message("create.branch.invalid.revision.error", myRevisionPanel
.getRevisionText()));
343 else if (myWorkingCopyRadioButton
.isSelected()) {
345 SVNWCClient client
= SvnVcs
.getInstance(myProject
).createWCClient();
346 SVNInfo info
= client
.doInfo(mySrcFile
, SVNRevision
.WORKING
);
347 mySrcURL
= info
!= null && info
.getURL() != null ? info
.getURL().toString() : null;
349 catch (SVNException e
) {
352 if (mySrcURL
== null) {
353 myErrorLabel
.setText(SvnBundle
.message("create.branch.no.working.copy.error", myWorkingCopyField
.getText()));
362 public boolean isCopyFromWorkingCopy() {
363 return myWorkingCopyRadioButton
.isSelected();
366 public String
getCopyFromPath() {
367 return myWorkingCopyField
.getText();
370 public String
getCopyFromUrl() {
371 return myRepositoryField
.getText();