update copyright
[fedora-idea.git] / xml / impl / src / com / intellij / javaee / EditLocationDialog.java
blob18150a7bca853ee33c8871f18807be332497d561
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 com.intellij.javaee;
18 import com.intellij.openapi.fileChooser.FileChooser;
19 import com.intellij.openapi.fileChooser.FileChooserDescriptor;
20 import com.intellij.openapi.project.Project;
21 import com.intellij.openapi.ui.DialogWrapper;
22 import com.intellij.openapi.ui.FixedSizeButton;
23 import com.intellij.openapi.ui.TextFieldWithBrowseButton;
24 import com.intellij.openapi.vfs.VirtualFile;
25 import com.intellij.xml.XmlBundle;
27 import javax.swing.*;
28 import java.awt.*;
29 import java.awt.event.ActionEvent;
30 import java.awt.event.ActionListener;
31 import java.io.File;
33 public class EditLocationDialog extends DialogWrapper {
35 private JTextField myTfUrl;
36 private JTextField myTfPath;
37 private FixedSizeButton myBtnBrowseLocalPath;
38 private final Project myProject;
39 private final boolean myShowPath;
41 private final String myTitle;
42 private final String myName;
43 private final String myLocation;
44 private boolean myTfShared = true;
46 public EditLocationDialog(Project project, boolean showPath) {
47 super(project, true);
48 myProject = project;
49 myShowPath = showPath;
50 myTitle = XmlBundle.message("dialog.title.external.resource");
51 myName = XmlBundle.message("label.edit.external.resource.uri");
52 myLocation = XmlBundle.message("label.edit.external.resource.path");
53 init();
56 public EditLocationDialog(Project project, boolean showPath, String title, String name, String location) {
57 super(project, true);
58 myProject = project;
59 myShowPath = showPath;
60 myTitle = title;
61 myName = name;
62 myLocation = location;
63 init();
66 protected JComponent createCenterPanel() {
67 JPanel panel = new JPanel(new GridBagLayout());
69 panel.add(
70 new JLabel(myName),
71 new GridBagConstraints(0, 0, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 5, 3, 5), 0, 0)
73 panel.add(
74 myTfUrl,
75 new GridBagConstraints(0, 1, 2, 1, 1, 0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 5, 5, 5), 0, 0)
78 myTfUrl.setPreferredSize(new Dimension(350, myTfUrl.getPreferredSize().height));
80 if (myShowPath) {
81 panel.add(
82 new JLabel(myLocation),
83 new GridBagConstraints(0, 2, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 5, 3, 5), 0, 0)
85 panel.add(
86 myTfPath,
87 new GridBagConstraints(0, 3, 1, 1, 1, 0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 5, 10, 0), 0, 0)
89 panel.add(
90 myBtnBrowseLocalPath,
91 new GridBagConstraints(1, 3, 1, 1, 0, 0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 10, 5), 0, 0)
95 TextFieldWithBrowseButton.MyDoClickAction.addTo(myBtnBrowseLocalPath, myTfPath);
96 myBtnBrowseLocalPath.addActionListener(
97 new ActionListener() {
98 public void actionPerformed(ActionEvent ignored) {
99 FileChooserDescriptor descriptor = getChooserDescriptor();
100 VirtualFile[] files = FileChooser.chooseFiles(myProject, descriptor);
101 if (files.length != 0) {
102 myTfPath.setText(files[0].getPath().replace('/', File.separatorChar));
111 return panel;
114 public JComponent getPreferredFocusedComponent() {
115 return myTfUrl;
118 public NameLocationPair getPair() {
119 String path = myTfPath.getText().trim();
120 String url = myTfUrl.getText().trim();
121 return new NameLocationPair(url, path, myTfShared);
124 protected FileChooserDescriptor getChooserDescriptor(){
125 return new FileChooserDescriptor(true, false, false, false, true, false);
128 protected void init() {
129 setTitle(myTitle);
130 myTfUrl = new JTextField();
131 myTfPath = new JTextField();
132 myBtnBrowseLocalPath = new FixedSizeButton(myTfPath);
133 super.init();
137 * Initializes editor with the passed data.
139 public void init(NameLocationPair origin) {
140 myTfUrl.setText(origin.myName);
141 myTfPath.setText(origin.myLocation);
142 myTfShared = origin.myShared;
145 public static class NameLocationPair implements Comparable {
146 String myName;
147 String myLocation;
148 boolean myShared;
150 public NameLocationPair(String name, String location, boolean shared) {
151 myName = name;
152 myLocation = location;
153 myShared = shared;
156 public int compareTo(Object o) {
157 return myName.compareTo(((NameLocationPair)o).myName);
160 public boolean equals(Object obj) {
161 if (! (obj instanceof NameLocationPair)) return false;
162 return compareTo(obj) == 0;
165 public int hashCode() {
166 return myName.hashCode();
169 public String getName() {
170 return myName;
173 public String getLocation(){
174 return myLocation;