update copyright
[fedora-idea.git] / xml / impl / src / com / intellij / javaee / ExternalResourceConfigurable.java
blob386f4308f75f87e5113d2d00781b43ed983e3847
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.options.BaseConfigurable;
19 import com.intellij.openapi.options.OptionalConfigurable;
20 import com.intellij.openapi.options.SearchableConfigurable;
21 import com.intellij.openapi.project.Project;
22 import com.intellij.openapi.util.IconLoader;
23 import com.intellij.openapi.vfs.JarFileSystem;
24 import com.intellij.openapi.vfs.LocalFileSystem;
25 import com.intellij.openapi.vfs.VirtualFile;
26 import com.intellij.ui.AddEditRemovePanel;
27 import com.intellij.util.ui.Table;
28 import com.intellij.util.ui.UIUtil;
29 import com.intellij.xml.XmlBundle;
30 import org.jetbrains.annotations.Nullable;
32 import javax.swing.*;
33 import javax.swing.event.TableModelEvent;
34 import javax.swing.event.TableModelListener;
35 import javax.swing.table.DefaultTableCellRenderer;
36 import javax.swing.table.TableColumn;
37 import java.awt.*;
38 import java.util.ArrayList;
39 import java.util.Arrays;
40 import java.util.Collections;
41 import java.util.List;
43 public class ExternalResourceConfigurable extends BaseConfigurable implements SearchableConfigurable, OptionalConfigurable {
45 private JPanel myPanel;
46 private List<EditLocationDialog.NameLocationPair> myPairs;
47 private List<String> myIgnoredUrls;
48 private AddEditRemovePanel<EditLocationDialog.NameLocationPair> myExtPanel;
49 private AddEditRemovePanel<String> myIgnorePanel;
50 private final Project myProject;
52 public ExternalResourceConfigurable(Project project) {
53 myProject = project;
56 public String getDisplayName() {
57 return XmlBundle.message("display.name.edit.external.resource");
60 public JComponent createComponent() {
61 myPanel = new JPanel(new GridBagLayout()) {
62 public Dimension getPreferredSize() {
63 return new Dimension(700, 400);
67 myExtPanel = new AddEditRemovePanel<EditLocationDialog.NameLocationPair>(new ExtUrlsTableModel(), myPairs, XmlBundle.message("label.edit.external.resource.configure.external.resources")) {
68 protected EditLocationDialog.NameLocationPair addItem() {
69 return addExtLocation();
72 protected boolean removeItem(EditLocationDialog.NameLocationPair o) {
73 setModified(true);
74 return true;
77 protected EditLocationDialog.NameLocationPair editItem(EditLocationDialog.NameLocationPair o) {
78 return editExtLocation(o);
82 myExtPanel.setRenderer(1, new PathRenderer());
84 JTable table = myExtPanel.getTable();
85 TableColumn column = table.getColumn(table.getColumnName(2));
86 column.setMaxWidth(50);
87 column.setCellEditor(Table.createBooleanEditor());
89 table.getModel().addTableModelListener(new TableModelListener() {
90 public void tableChanged(TableModelEvent e) {
91 setModified(true);
93 });
94 myIgnorePanel = new AddEditRemovePanel<String>(new IgnoredUrlsModel(), myIgnoredUrls, XmlBundle.message("label.edit.external.resource.configure.ignored.resources")) {
95 protected String addItem() {
96 return addIgnoreLocation();
99 protected boolean removeItem(String o) {
100 setModified(true);
101 return true;
104 protected String editItem(String o) {
105 return editIgnoreLocation(o);
109 myPanel.add(myExtPanel,
110 new GridBagConstraints(0, 0, 1, 1, 1, 1, GridBagConstraints.NORTH, GridBagConstraints.BOTH, new Insets(5, 2, 4, 2), 0, 0));
111 myPanel.add(myIgnorePanel,
112 new GridBagConstraints(0, 1, 1, 1, 1, 1, GridBagConstraints.NORTH, GridBagConstraints.BOTH, new Insets(5, 2, 4, 2), 0, 0));
115 return myPanel;
118 public Icon getIcon() {
119 return IconLoader.getIcon("/general/configurableExternalResources.png");
122 public void apply() {
123 ExternalResourceManagerEx manager = ExternalResourceManagerEx.getInstanceEx();
125 manager.clearAllResources(myProject);
126 for (Object myPair : myPairs) {
127 EditLocationDialog.NameLocationPair pair = (EditLocationDialog.NameLocationPair)myPair;
128 String s = pair.myLocation.replace('\\', '/');
129 if (pair.myShared) {
130 manager.addResource(pair.myName, s);
131 } else {
132 manager.addResource(pair.myName, s, myProject);
136 for (Object myIgnoredUrl : myIgnoredUrls) {
137 String url = (String)myIgnoredUrl;
138 manager.addIgnoredResource(url);
140 setModified(false);
143 public void reset() {
145 myPairs = new ArrayList<EditLocationDialog.NameLocationPair>();
146 ExternalResourceManagerEx manager = ExternalResourceManagerEx.getInstanceEx();
148 String[] urls = manager.getAvailableUrls();
149 for (String url : urls) {
150 String loc = manager.getResourceLocation(url, myProject);
151 myPairs.add(new EditLocationDialog.NameLocationPair(url, loc, true));
153 urls = manager.getAvailableUrls(myProject);
154 for (String url : urls) {
155 String loc = manager.getResourceLocation(url, myProject);
156 myPairs.add(new EditLocationDialog.NameLocationPair(url, loc, false));
159 Collections.sort(myPairs);
161 myIgnoredUrls = new ArrayList<String>();
162 final String[] ignoredResources = manager.getIgnoredResources();
163 myIgnoredUrls.addAll(Arrays.asList(ignoredResources));
165 Collections.sort(myIgnoredUrls);
167 myExtPanel.setData(myPairs);
168 myIgnorePanel.setData(myIgnoredUrls);
170 setModified(false);
173 public void disposeUIResources() {
174 myPanel = null;
175 myExtPanel = null;
176 myIgnorePanel = null;
179 public String getHelpTopic() {
180 return getId();
183 @Nullable
184 private EditLocationDialog.NameLocationPair addExtLocation() {
185 EditLocationDialog dialog = new EditLocationDialog(null, true);
186 dialog.show();
187 if (!dialog.isOK()) return null;
188 setModified(true);
189 return dialog.getPair();
192 @Nullable
193 private EditLocationDialog.NameLocationPair editExtLocation(Object o) {
194 EditLocationDialog dialog = new EditLocationDialog(null, true);
195 final EditLocationDialog.NameLocationPair pair = (EditLocationDialog.NameLocationPair)o;
196 dialog.init(pair);
197 dialog.show();
198 if (!dialog.isOK()) {
199 return null;
201 setModified(true);
202 return dialog.getPair();
205 @Nullable
206 private String addIgnoreLocation() {
207 EditLocationDialog dialog = new EditLocationDialog(null, false);
208 dialog.show();
209 if (!dialog.isOK()) return null;
210 setModified(true);
211 return dialog.getPair().myName;
214 @Nullable
215 private String editIgnoreLocation(Object o) {
216 EditLocationDialog dialog = new EditLocationDialog(null, false);
217 dialog.init(new EditLocationDialog.NameLocationPair(o.toString(), null, false));
218 dialog.show();
219 if (!dialog.isOK()) return null;
220 setModified(true);
221 return dialog.getPair().myName;
224 public void selectResource(final String uri) {
225 myExtPanel.setSelected(new EditLocationDialog.NameLocationPair(uri, null, false));
228 private static class PathRenderer extends DefaultTableCellRenderer {
229 public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
230 super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
231 String loc = value.toString().replace('\\', '/');
232 final int jarDelimIndex = loc.indexOf(JarFileSystem.JAR_SEPARATOR);
233 final VirtualFile path;
235 if (jarDelimIndex != -1) {
236 path = JarFileSystem.getInstance().findFileByPath(loc);
237 } else {
238 path = LocalFileSystem.getInstance().findFileByPath(loc);
241 setForeground(path != null ? isSelected ? UIUtil.getTableSelectionForeground() : Color.black : new Color(210, 0, 0));
242 return this;
246 private static class IgnoredUrlsModel extends AddEditRemovePanel.TableModel {
247 private final String[] myNames = {XmlBundle.message("column.name.edit.external.resource.uri")};
249 public int getColumnCount() {
250 return myNames.length;
253 public Object getField(Object o, int columnIndex) {
254 return o;
257 public Class getColumnClass(int columnIndex) {
258 return String.class;
261 public boolean isEditable(int column) {
262 return false;
265 public void setValue(Object aValue, Object data, int columnIndex) {
269 public String getColumnName(int column) {
270 return myNames[column];
274 private static class ExtUrlsTableModel extends AddEditRemovePanel.TableModel<EditLocationDialog.NameLocationPair> {
275 final String[] myNames =
276 {XmlBundle.message("column.name.edit.external.resource.uri"), XmlBundle.message("column.name.edit.external.resource.location"), "Project"};
278 public int getColumnCount() {
279 return myNames.length;
282 public Object getField(EditLocationDialog.NameLocationPair pair, int columnIndex) {
283 switch (columnIndex) {
284 case 0:
285 return pair.myName;
286 case 1:
287 return pair.myLocation;
288 case 2:
289 return !pair.myShared;
292 return "";
295 public Class getColumnClass(int columnIndex) {
296 return columnIndex == 2 ? Boolean.class : String.class;
299 public boolean isEditable(int column) {
300 return column == 2;
303 public void setValue(Object aValue, EditLocationDialog.NameLocationPair data, int columnIndex) {
304 data.myShared = !((Boolean)aValue).booleanValue();
307 public String getColumnName(int column) {
308 return myNames[column];
312 public String getId() {
313 return "preferences.externalResources";
316 @Nullable
317 public Runnable enableSearch(String option) {
318 return null;
321 public boolean needDisplay() {
322 return !"Ruby".equals(System.getProperty("idea.platform.prefix"));