IDEA-26360 (Performance and inconsistency issues with svn:externals and "Detect neste...
[fedora-idea.git] / plugins / svn4idea / src / org / jetbrains / idea / svn / update / SvnUpdateRootOptionsPanel.java
blob7be228a22e8fb6e6087bf01d4a415098d7bbf904
1 /*
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.update;
18 import com.intellij.openapi.diagnostic.Logger;
19 import com.intellij.openapi.options.ConfigurationException;
20 import com.intellij.openapi.project.Project;
21 import com.intellij.openapi.ui.TextFieldWithBrowseButton;
22 import com.intellij.openapi.vcs.FilePath;
23 import org.jetbrains.annotations.Nullable;
24 import org.jetbrains.idea.svn.*;
25 import org.jetbrains.idea.svn.actions.SelectBranchPopup;
26 import org.jetbrains.idea.svn.branchConfig.SvnBranchConfigurationNew;
27 import org.jetbrains.idea.svn.dialogs.SelectLocationDialog;
28 import org.jetbrains.idea.svn.history.SvnChangeList;
29 import org.jetbrains.idea.svn.history.SvnRepositoryLocation;
30 import org.tmatesoft.svn.core.SVNException;
31 import org.tmatesoft.svn.core.SVNURL;
32 import org.tmatesoft.svn.core.internal.util.SVNPathUtil;
33 import org.tmatesoft.svn.core.wc.SVNRevision;
35 import javax.swing.*;
36 import java.awt.*;
37 import java.awt.event.ActionEvent;
38 import java.awt.event.ActionListener;
40 public class SvnUpdateRootOptionsPanel implements SvnPanel{
41 private final static Logger LOG = Logger.getInstance("#org.jetbrains.idea.svn.update.SvnUpdateRootOptionsPanel.SvnUpdateRootOptionsPanel");
42 private TextFieldWithBrowseButton myURLText;
43 private JCheckBox myRevisionBox;
44 private TextFieldWithBrowseButton myRevisionText;
46 private final SvnVcs myVcs;
47 private JPanel myPanel;
48 private final FilePath myRoot;
49 private JCheckBox myUpdateToSpecificUrl;
50 private TextFieldWithBrowseButton myBranchField;
51 private JLabel myBranchLabel;
52 private JLabel myUrlLabel;
53 private JLabel myCopyType;
54 private String mySourceUrl;
55 private SVNURL myBranchUrl;
57 public SvnUpdateRootOptionsPanel(FilePath root, final SvnVcs vcs) {
58 myRoot = root;
59 myVcs = vcs;
61 myURLText.setEditable(true);
63 myURLText.addActionListener(new ActionListener() {
64 public void actionPerformed(ActionEvent e) {
65 chooseUrl();
67 });
69 myBranchField.setEditable(false);
70 myBranchField.addActionListener(new ActionListener() {
71 public void actionPerformed(final ActionEvent e) {
72 chooseBranch();
74 });
75 myBranchLabel.setLabelFor(myBranchField);
76 myUrlLabel.setLabelFor(myURLText);
78 myUpdateToSpecificUrl.addActionListener(new ActionListener() {
79 public void actionPerformed(ActionEvent e) {
80 if (myUpdateToSpecificUrl.isSelected()) {
81 myURLText.setEnabled(true);
82 myBranchField.setEnabled((myBranchUrl != null) && (mySourceUrl != null));
83 //chooseBranch();
84 } else {
85 myURLText.setEnabled(false);
86 myBranchField.setEnabled(false);
89 });
91 myRevisionBox.addActionListener(new ActionListener() {
92 public void actionPerformed(ActionEvent e) {
93 if (e.getSource() == myRevisionBox) {
94 myRevisionText.setEnabled(myRevisionBox.isSelected());
95 if (myRevisionBox.isSelected()) {
96 if ("".equals(myRevisionText.getText().trim())) {
97 myRevisionText.setText("HEAD");
99 myRevisionText.getTextField().selectAll();
100 myRevisionText.requestFocus();
106 myRevisionText.addActionListener(new ActionListener() {
107 public void actionPerformed(ActionEvent e) {
108 final Project project = vcs.getProject();
109 // todo check whether ok; rather shoudl be used if checkbox is turned on
110 final SvnRepositoryLocation location = new SvnRepositoryLocation(myURLText.getText());
111 final SvnChangeList repositoryVersion = SvnSelectRevisionUtil.chooseCommittedChangeList(project, location, myRoot.getVirtualFile());
112 if (repositoryVersion != null) {
113 myRevisionText.setText(String.valueOf(repositoryVersion.getNumber()));
118 myRevisionText.setText(SVNRevision.HEAD.toString());
119 myRevisionText.getTextField().selectAll();
120 myRevisionText.setEnabled(myRevisionBox.isSelected());
121 myURLText.setEnabled(myUpdateToSpecificUrl.isSelected());
122 myBranchField.setEnabled(myUpdateToSpecificUrl.isSelected() && (myBranchUrl != null) && (mySourceUrl != null));
124 final boolean revisionCanBeSpecifiedForRoot = isRevisionCanBeSpecifiedForRoot();
125 myRevisionBox.setEnabled(revisionCanBeSpecifiedForRoot);
126 myRevisionText.setEnabled(revisionCanBeSpecifiedForRoot);
127 myCopyType.setVisible(! revisionCanBeSpecifiedForRoot);
128 myCopyType.setFont(myCopyType.getFont().deriveFont(Font.ITALIC));
131 private boolean isRevisionCanBeSpecifiedForRoot() {
132 final RootUrlInfo info = myVcs.getSvnFileUrlMapping().getWcRootForFilePath(myRoot.getIOFile());
133 if (info != null) {
134 final boolean result = (!NestedCopyType.external.equals(info.getType())) && (!NestedCopyType.switched.equals(info.getType()));
135 if (! result) {
136 myCopyType.setText(info.getType().getName() + " copy");
138 return result;
140 return true;
143 private void chooseBranch() {
144 if ((myBranchUrl == null) || (mySourceUrl == null)) {
145 myBranchField.setEnabled(false);
146 return;
148 SelectBranchPopup.show(myVcs.getProject(), myRoot.getVirtualFile(), new SelectBranchPopup.BranchSelectedCallback() {
149 public void branchSelected(final Project project, final SvnBranchConfigurationNew configuration, final String url, final long revision) {
150 recalculateUrl(url);
151 myBranchField.setText(SVNPathUtil.tail(url));
153 }, SvnBundle.message("select.branch.popup.general.title"));
156 private void recalculateUrl(final String url) {
157 // recalculate fields
158 try {
159 final String newText = SVNURL.parseURIEncoded(url).appendPath(mySourceUrl.substring(myBranchUrl.toString().length()), true).toString();
160 myURLText.setText(newText);
162 catch (SVNException e) {
163 LOG.error(e);
167 private void chooseUrl() {
168 String selected = SelectLocationDialog.selectLocation(myVcs.getProject(), myURLText.getText());
169 if (selected != null) {
170 myURLText.setText(selected);
174 public JPanel getPanel() {
175 return myPanel;
178 @Nullable
179 private SVNURL getBranchForUrl(final String url) {
180 final SvnFileUrlMapping urlMapping = myVcs.getSvnFileUrlMapping();
181 final RootUrlInfo rootForFilePath = urlMapping.getWcRootForFilePath(myRoot.getIOFile());
182 if (rootForFilePath == null) {
183 return null;
185 return SvnUtil.getBranchForUrl(myVcs, rootForFilePath.getVirtualFile(), url);
188 public void reset(final SvnConfiguration configuration) {
189 final UpdateRootInfo rootInfo = configuration.getUpdateRootInfo(myRoot.getIOFile(), myVcs);
191 mySourceUrl = rootInfo.getUrlAsString();
192 myBranchUrl = getBranchForUrl(mySourceUrl);
193 if (myBranchUrl != null) {
194 myBranchField.setText(SVNPathUtil.tail(myBranchUrl.toString()));
197 myURLText.setText(mySourceUrl);
198 myRevisionBox.setSelected(rootInfo.isUpdateToRevision());
199 myRevisionText.setText(rootInfo.getRevision().toString());
200 myUpdateToSpecificUrl.setSelected(false);
201 myRevisionText.setEnabled(myRevisionBox.isSelected());
202 myURLText.setEnabled(myUpdateToSpecificUrl.isSelected());
203 myBranchField.setEnabled(myUpdateToSpecificUrl.isSelected() && (myBranchUrl != null) && (mySourceUrl != null));
206 public void apply(final SvnConfiguration configuration) throws ConfigurationException {
207 final UpdateRootInfo rootInfo = configuration.getUpdateRootInfo(myRoot.getIOFile(), myVcs);
208 if (myUpdateToSpecificUrl.isSelected()) {
209 rootInfo.setUrl(myURLText.getText());
211 rootInfo.setUpdateToRevision(myRevisionBox.isSelected());
212 final SVNRevision revision = SVNRevision.parse(myRevisionText.getText());
213 if (!revision.isValid()) {
214 throw new ConfigurationException(SvnBundle.message("invalid.svn.revision.error.message", myRevisionText.getText()));
216 rootInfo.setRevision(revision);
219 public boolean canApply() {
220 return true;
223 /* private class MyBranchFieldFocusListener implements FocusListener {
224 private SvnBranchConfiguration mySvnBranchConfiguration;
226 private MyBranchFieldFocusListener() {
227 final VirtualFile root = ProjectLevelVcsManager.getInstance(myVcs.getProject()).getVcsRootFor(myRoot);
228 if (root != null) {
229 try {
230 mySvnBranchConfiguration = SvnBranchConfigurationManager.getInstance(myVcs.getProject()).get(root);
232 catch (VcsException e) {
233 LOG.error(e);
238 public void focusGained(final FocusEvent e) {
241 public void focusLost(final FocusEvent e) {
242 if (mySvnBranchConfiguration != null) {
243 String text = myBranchField.getText();
244 text = (text == null) ? "" : text.trim();
245 if ((myBranchUrl != null) && (mySourceUrl != null) && (text.length() > 0) && (! text.contains("/"))) {
246 try {
247 final String branch = mySvnBranchConfiguration.getBranchByName(myVcs.getProject(), text);
248 if (branch != null) {
249 recalculateUrl(branch);
252 catch (SVNException exc) {
253 LOG.error(exc);