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
blob0781e03b50ba474451f321b6ffb9e6eb2721e681
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));
129 myUpdateToSpecificUrl.setEnabled(revisionCanBeSpecifiedForRoot);
132 private boolean isRevisionCanBeSpecifiedForRoot() {
133 final RootUrlInfo info = myVcs.getSvnFileUrlMapping().getWcRootForFilePath(myRoot.getIOFile());
134 if (info != null) {
135 final boolean result = (!NestedCopyType.external.equals(info.getType())) && (!NestedCopyType.switched.equals(info.getType()));
136 if (! result) {
137 myCopyType.setText(info.getType().getName() + " copy");
139 return result;
141 return true;
144 private void chooseBranch() {
145 if ((myBranchUrl == null) || (mySourceUrl == null)) {
146 myBranchField.setEnabled(false);
147 return;
149 SelectBranchPopup.show(myVcs.getProject(), myRoot.getVirtualFile(), new SelectBranchPopup.BranchSelectedCallback() {
150 public void branchSelected(final Project project, final SvnBranchConfigurationNew configuration, final String url, final long revision) {
151 recalculateUrl(url);
152 myBranchField.setText(SVNPathUtil.tail(url));
154 }, SvnBundle.message("select.branch.popup.general.title"));
157 private void recalculateUrl(final String url) {
158 // recalculate fields
159 try {
160 final String newText = SVNURL.parseURIEncoded(url).appendPath(mySourceUrl.substring(myBranchUrl.toString().length()), true).toString();
161 myURLText.setText(newText);
163 catch (SVNException e) {
164 LOG.error(e);
168 private void chooseUrl() {
169 String selected = SelectLocationDialog.selectLocation(myVcs.getProject(), myURLText.getText());
170 if (selected != null) {
171 myURLText.setText(selected);
175 public JPanel getPanel() {
176 return myPanel;
179 @Nullable
180 private SVNURL getBranchForUrl(final String url) {
181 final SvnFileUrlMapping urlMapping = myVcs.getSvnFileUrlMapping();
182 final RootUrlInfo rootForFilePath = urlMapping.getWcRootForFilePath(myRoot.getIOFile());
183 if (rootForFilePath == null) {
184 return null;
186 return SvnUtil.getBranchForUrl(myVcs, rootForFilePath.getVirtualFile(), url);
189 public void reset(final SvnConfiguration configuration) {
190 final UpdateRootInfo rootInfo = configuration.getUpdateRootInfo(myRoot.getIOFile(), myVcs);
192 mySourceUrl = rootInfo.getUrlAsString();
193 myBranchUrl = getBranchForUrl(mySourceUrl);
194 if (myBranchUrl != null) {
195 myBranchField.setText(SVNPathUtil.tail(myBranchUrl.toString()));
198 myURLText.setText(mySourceUrl);
199 myRevisionBox.setSelected(rootInfo.isUpdateToRevision());
200 myRevisionText.setText(rootInfo.getRevision().toString());
201 myUpdateToSpecificUrl.setSelected(false);
202 myRevisionText.setEnabled(myRevisionBox.isSelected());
203 myURLText.setEnabled(myUpdateToSpecificUrl.isSelected());
204 myBranchField.setEnabled(myUpdateToSpecificUrl.isSelected() && (myBranchUrl != null) && (mySourceUrl != null));
207 public void apply(final SvnConfiguration configuration) throws ConfigurationException {
208 final UpdateRootInfo rootInfo = configuration.getUpdateRootInfo(myRoot.getIOFile(), myVcs);
209 if (myUpdateToSpecificUrl.isSelected()) {
210 rootInfo.setUrl(myURLText.getText());
212 rootInfo.setUpdateToRevision(myRevisionBox.isSelected());
213 final SVNRevision revision = SVNRevision.parse(myRevisionText.getText());
214 if (!revision.isValid()) {
215 throw new ConfigurationException(SvnBundle.message("invalid.svn.revision.error.message", myRevisionText.getText()));
217 rootInfo.setRevision(revision);
220 public boolean canApply() {
221 return true;
224 /* private class MyBranchFieldFocusListener implements FocusListener {
225 private SvnBranchConfiguration mySvnBranchConfiguration;
227 private MyBranchFieldFocusListener() {
228 final VirtualFile root = ProjectLevelVcsManager.getInstance(myVcs.getProject()).getVcsRootFor(myRoot);
229 if (root != null) {
230 try {
231 mySvnBranchConfiguration = SvnBranchConfigurationManager.getInstance(myVcs.getProject()).get(root);
233 catch (VcsException e) {
234 LOG.error(e);
239 public void focusGained(final FocusEvent e) {
242 public void focusLost(final FocusEvent e) {
243 if (mySvnBranchConfiguration != null) {
244 String text = myBranchField.getText();
245 text = (text == null) ? "" : text.trim();
246 if ((myBranchUrl != null) && (mySourceUrl != null) && (text.length() > 0) && (! text.contains("/"))) {
247 try {
248 final String branch = mySvnBranchConfiguration.getBranchByName(myVcs.getProject(), text);
249 if (branch != null) {
250 recalculateUrl(branch);
253 catch (SVNException exc) {
254 LOG.error(exc);