SVN auth
[fedora-idea.git] / plugins / svn4idea / src / org / jetbrains / idea / svn / dialogs / SelectLocationDialog.java
blob84862f65174ff5304e74683e7f98458d2c8d7d74
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.dialogs;
18 import com.intellij.openapi.help.HelpManager;
19 import com.intellij.openapi.project.Project;
20 import com.intellij.openapi.ui.DialogWrapper;
21 import com.intellij.openapi.ui.Messages;
22 import com.intellij.openapi.util.Disposer;
23 import org.jetbrains.annotations.NonNls;
24 import org.jetbrains.annotations.Nullable;
25 import org.jetbrains.idea.svn.SvnBundle;
26 import org.jetbrains.idea.svn.SvnVcs;
27 import org.jetbrains.idea.svn.dialogs.browser.UrlOpeningExpander;
28 import org.tmatesoft.svn.core.SVNException;
29 import org.tmatesoft.svn.core.SVNURL;
30 import org.tmatesoft.svn.core.internal.util.SVNEncodingUtil;
31 import org.tmatesoft.svn.core.io.SVNRepository;
33 import javax.swing.*;
34 import javax.swing.event.DocumentEvent;
35 import javax.swing.event.DocumentListener;
36 import javax.swing.event.TreeSelectionEvent;
37 import javax.swing.event.TreeSelectionListener;
38 import java.awt.*;
40 /**
41 * Created by IntelliJ IDEA.
42 * User: alex
43 * Date: 25.06.2005
44 * Time: 17:12:47
45 * To change this template use File | Settings | File Templates.
47 public class SelectLocationDialog extends DialogWrapper {
48 private Project myProject;
49 private RepositoryBrowserComponent myRepositoryBrowser;
50 private SVNURL myURL;
51 private String myDstName;
52 private String myDstLabel;
53 private JTextField myDstText;
54 private boolean myIsShowFiles;
56 @NonNls private static final String HELP_ID = "vcs.subversion.common";
58 @Nullable
59 public static String selectLocation(Project project, String url) {
60 try {
61 SVNURL.parseURIEncoded(url);
62 SelectLocationDialog dialog = new SelectLocationDialog(project, url);
63 dialog.show();
64 if (!dialog.isOK()) return null;
65 return dialog.getSelectedURL();
66 } catch (SVNException e) {
67 Messages.showErrorDialog(project, SvnBundle.message("select.location.invalid.url.message", url),
68 SvnBundle.message("dialog.title.select.repository.location"));
69 return null;
73 public SelectLocationDialog(Project project, String url) throws SVNException {
74 this(project, url, null, null, true);
77 public SelectLocationDialog(Project project, String url,
78 String dstLabel, String dstName, boolean showFiles) throws SVNException {
79 super(project, true);
80 myProject = project;
81 myDstLabel = dstLabel;
82 myDstName = dstName;
83 myURL = SVNURL.parseURIEncoded(url);
84 myIsShowFiles = showFiles;
85 setTitle(SvnBundle.message("dialog.title.select.repository.location"));
86 getHelpAction().setEnabled(true);
87 init();
90 protected void doHelpAction() {
91 HelpManager.getInstance().invokeHelp(HELP_ID);
94 protected Action[] createActions() {
95 return new Action[]{getOKAction(), getCancelAction(), getHelpAction()};
98 protected String getDimensionServiceKey() {
99 return "svn.repositoryBrowser";
102 protected void init() {
103 super.init();
104 String urlString = myURL.toString();
105 try {
106 SVNRepository repos = SvnVcs.getInstance(myProject).createRepository(urlString);
107 myURL = repos.getRepositoryRoot(true);
108 repos.closeSession();
109 } catch (SVNException e) {
110 // show error dialog.
112 myRepositoryBrowser.setRepositoryURL(myURL, myIsShowFiles, new UrlOpeningExpander.Factory(urlString, urlString));
113 myRepositoryBrowser.addChangeListener(new TreeSelectionListener() {
114 public void valueChanged(TreeSelectionEvent e) {
115 getOKAction().setEnabled(isOKActionEnabled());
120 @Override
121 protected void dispose() {
122 super.dispose();
123 Disposer.dispose(myRepositoryBrowser);
126 protected JComponent createCenterPanel() {
127 JPanel panel = new JPanel();
128 panel.setLayout(new GridBagLayout());
130 GridBagConstraints gc = new GridBagConstraints();
131 gc.insets = new Insets(2, 2, 2, 2);
132 gc.gridwidth = 2;
133 gc.gridheight = 1;
134 gc.gridx = 0;
135 gc.gridy = 0;
136 gc.anchor = GridBagConstraints.WEST;
137 gc.fill = GridBagConstraints.BOTH;
138 gc.weightx = 1;
139 gc.weighty = 1;
142 myRepositoryBrowser = new RepositoryBrowserComponent(SvnVcs.getInstance(myProject));
143 panel.add(myRepositoryBrowser, gc);
144 if (myDstName != null) {
145 gc.gridy += 1;
146 gc.gridwidth = 1;
147 gc.gridx = 0;
148 gc.fill = GridBagConstraints.NONE;
149 gc.weightx = 0;
150 gc.weighty = 0;
152 JLabel dstLabel = new JLabel(myDstLabel);
153 panel.add(dstLabel, gc);
155 gc.gridx += 1;
156 gc.weightx = 1;
157 gc.fill = GridBagConstraints.HORIZONTAL;
159 myDstText = new JTextField();
160 myDstText.setText(myDstName);
161 myDstText.selectAll();
162 panel.add(myDstText, gc);
164 myDstText.getDocument().addDocumentListener(new DocumentListener() {
165 public void insertUpdate(DocumentEvent e) {
166 getOKAction().setEnabled(isOKActionEnabled());
169 public void removeUpdate(DocumentEvent e) {
170 getOKAction().setEnabled(isOKActionEnabled());
173 public void changedUpdate(DocumentEvent e) {
174 getOKAction().setEnabled(isOKActionEnabled());
178 dstLabel.setLabelFor(myDstText);
179 gc.gridx = 0;
180 gc.gridy += 1;
181 gc.gridwidth = 2;
183 panel.add(new JSeparator(), gc);
186 return panel;
189 public JComponent getPreferredFocusedComponent() {
190 return (JComponent)myRepositoryBrowser.getPreferredFocusedComponent();
193 public boolean shouldCloseOnCross() {
194 return true;
197 public boolean isOKActionEnabled() {
198 boolean ok = myRepositoryBrowser.getSelectedURL() != null;
199 if (ok && myDstText != null) {
200 return myDstText.getText().trim().length() > 0;
202 return ok;
205 public String getDestinationName() {
206 return SVNEncodingUtil.uriEncode(myDstText.getText().trim());
209 public String getSelectedURL() {
210 return myRepositoryBrowser.getSelectedURL();
213 public SVNURL getSelectedSVNURL() {
214 return myRepositoryBrowser.getSelectedSVNURL();