IDEADEV-41116: exploded war artefact: add warning for dependent module
[fedora-idea.git] / java / idea-ui / src / com / intellij / openapi / roots / ui / configuration / AnnotationsEditor.java
blob0223d7ed9fde5c9439c0881a18022443c735d9ba
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.openapi.roots.ui.configuration;
18 import com.intellij.openapi.fileChooser.FileChooser;
19 import com.intellij.openapi.fileChooser.FileChooserDescriptor;
20 import com.intellij.openapi.project.ProjectBundle;
21 import com.intellij.openapi.roots.AnnotationOrderRootType;
22 import com.intellij.openapi.roots.ui.util.CellAppearance;
23 import com.intellij.openapi.roots.ui.util.CellAppearanceUtils;
24 import com.intellij.openapi.roots.ui.util.SimpleTextCellAppearance;
25 import com.intellij.openapi.ui.PanelWithText;
26 import com.intellij.openapi.util.IconLoader;
27 import com.intellij.openapi.vfs.VirtualFile;
28 import com.intellij.openapi.vfs.VirtualFileManager;
29 import com.intellij.ui.ColoredTableCellRenderer;
30 import com.intellij.ui.ScrollPaneFactory;
31 import com.intellij.ui.TableUtil;
32 import com.intellij.util.ArrayUtil;
33 import com.intellij.util.ui.ItemRemovable;
34 import com.intellij.util.ui.Table;
36 import javax.swing.*;
37 import javax.swing.border.Border;
38 import javax.swing.event.ListSelectionEvent;
39 import javax.swing.event.ListSelectionListener;
40 import javax.swing.table.DefaultTableModel;
41 import java.awt.*;
42 import java.awt.event.ActionEvent;
43 import java.awt.event.ActionListener;
44 import java.util.List;
46 /**
47 * @author Eugene Zhuravlev
48 * Date: Oct 4, 2003
49 * Time: 6:54:57 PM
51 public class AnnotationsEditor extends ModuleElementsEditor {
52 private JTable myTable;
53 private JButton myAddPathButton;
54 private JButton myRemoveButton;
56 public static final String NAME = ProjectBundle.message("project.roots.external.annotations.tab.title");
57 public static final Icon ICON = IconLoader.getIcon("/modules/annotation.png");
59 public AnnotationsEditor(final ModuleConfigurationState state) {
60 super(state);
63 public String getHelpTopic() {
64 return "project.paths.annotations";//todo
67 public String getDisplayName() {
68 return NAME;
71 public Icon getIcon() {
72 return ICON;
75 public void saveData() {
76 TableUtil.stopEditing(myTable);
77 final int count = myTable.getRowCount();
78 String[] urls = ArrayUtil.newStringArray(count);
79 for (int row = 0; row < count; row++) {
80 final TableItem item = ((MyTableModel)myTable.getModel()).getTableItemAt(row);
81 urls[row] = item.getUrl();
83 getModel().setRootUrls(AnnotationOrderRootType.getInstance(), urls);
86 public JComponent createComponentImpl() {
87 final JPanel mainPanel = new JPanel(new BorderLayout());
88 mainPanel.setBorder(BorderFactory.createEmptyBorder(6, 6, 6, 6));
89 mainPanel.setPreferredSize(new Dimension(-1, 200));
90 final DefaultTableModel tableModel = createModel();
91 myTable = new Table(tableModel);
92 myTable.setIntercellSpacing(new Dimension(0, 0));
93 myTable.setDefaultRenderer(TableItem.class, new MyRenderer());
94 myTable.setShowGrid(false);
95 myTable.setDragEnabled(false);
96 myTable.setShowHorizontalLines(false);
97 myTable.setShowVerticalLines(false);
98 myTable.getSelectionModel().setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
100 myAddPathButton = new JButton(ProjectBundle.message("module.javadoc.add.path.button"));
101 myAddPathButton.addActionListener(new AddPathActionListener());
104 myRemoveButton = new JButton(ProjectBundle.message("module.javadoc.remove.button"));
105 myRemoveButton.addActionListener(new ActionListener() {
106 public void actionPerformed(ActionEvent e) {
107 final List removedItems = TableUtil.removeSelectedItems(myTable);
108 if (removedItems.size() > 0) {
109 saveData();
114 final JPanel panel = new JPanel(new GridBagLayout());
115 panel.add(myAddPathButton, new GridBagConstraints(0, GridBagConstraints.RELATIVE, 1, 1, 1.0, 0.0, GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, new Insets(0, 6, 0, 0), 0, 0));
116 panel.add(myRemoveButton, new GridBagConstraints(0, GridBagConstraints.RELATIVE, 1, 1, 1.0, 1.0, GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, new Insets(6, 6, 0, 0), 0, 0));
118 mainPanel.add(ScrollPaneFactory.createScrollPane(myTable), BorderLayout.CENTER);
119 mainPanel.add(panel, BorderLayout.EAST);
120 final PanelWithText panelWithText = new PanelWithText(ProjectBundle.message("project.roots.external.annotations.description"));
121 panelWithText.setBorder(null);
122 mainPanel.add(panelWithText, BorderLayout.NORTH);
124 myTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
125 public void valueChanged(ListSelectionEvent e) {
126 if (e.getValueIsAdjusting()) {
127 return;
129 final int selectedIndex = myTable.getSelectedRow();
130 myRemoveButton.setEnabled(selectedIndex >= 0);
133 if (tableModel.getRowCount() > 0) {
134 TableUtil.selectRows(myTable, new int[] {0});
136 else {
137 myRemoveButton.setEnabled(false);
139 return mainPanel;
142 protected DefaultTableModel createModel() {
143 final MyTableModel tableModel = new MyTableModel();
144 final String[] urls = getModel().getRootUrls(AnnotationOrderRootType.getInstance());
145 for (String javadocUrl : urls) {
146 tableModel.addTableItem(new TableItem(javadocUrl));
148 return tableModel;
151 public void moduleStateChanged() {
152 if (myTable != null) {
153 final DefaultTableModel tableModel = createModel();
154 myTable.setModel(tableModel);
155 myRemoveButton.setEnabled(tableModel.getRowCount() > 0);
159 private static class MyRenderer extends ColoredTableCellRenderer {
160 private static final Border NO_FOCUS_BORDER = BorderFactory.createEmptyBorder(1, 1, 1, 1);
162 protected void customizeCellRenderer(JTable table, Object value, boolean selected, boolean hasFocus, int row, int column) {
163 setPaintFocusBorder(false);
164 setFocusBorderAroundIcon(true);
165 setBorder(NO_FOCUS_BORDER);
167 final TableItem tableItem = ((TableItem)value);
168 tableItem.getCellAppearance().customize(this);
172 private static class TableItem {
173 private final String myUrl;
174 private final CellAppearance myCellAppearance;
175 public TableItem(VirtualFile file) {
176 myUrl = file.getUrl();
177 myCellAppearance = CellAppearanceUtils.forVirtualFile(file);
180 public TableItem(String url) {
181 myUrl = url;
182 final VirtualFile file = VirtualFileManager.getInstance().findFileByUrl(url);
183 if (file != null) {
184 myCellAppearance = CellAppearanceUtils.forVirtualFile(file);
186 else {
187 myCellAppearance = SimpleTextCellAppearance.invalid(url, CellAppearanceUtils.INVALID_ICON);
191 public String getUrl() {
192 return myUrl;
195 public CellAppearance getCellAppearance() {
196 return myCellAppearance;
200 private static class MyTableModel extends DefaultTableModel implements ItemRemovable{
201 public String getColumnName(int column) {
202 return null;
205 public Class getColumnClass(int columnIndex) {
206 return TableItem.class;
209 public int getColumnCount() {
210 return 1;
213 public boolean isCellEditable(int row, int column) {
214 return false;
217 public TableItem getTableItemAt(int row) {
218 return (TableItem)getValueAt(row, 0);
221 public void addTableItem(TableItem item) {
222 addRow(new Object[] {item});
226 private abstract class MyAddAction implements ActionListener {
227 protected abstract VirtualFile[] getFiles();
229 public void actionPerformed(ActionEvent e) {
230 VirtualFile[] files = getFiles();
231 final MyTableModel tableModel = (MyTableModel)myTable.getModel();
232 boolean changes = false;
233 for (final VirtualFile file : files) {
234 if (file != null) {
235 tableModel.addTableItem(new TableItem(file));
236 changes = true;
239 if (changes) {
240 saveData();
241 TableUtil.selectRows(myTable, new int[] {tableModel.getRowCount() - 1});
246 private class AddPathActionListener extends MyAddAction{
247 private final FileChooserDescriptor myDescriptor;
249 public AddPathActionListener() {
250 myDescriptor = new FileChooserDescriptor(false, true, true, false, true, true);
251 myDescriptor.setTitle(ProjectBundle.message("add.external.annotations.path.title"));
252 myDescriptor.setDescription(ProjectBundle.message("add.external.annotations.path.description"));
255 protected VirtualFile[] getFiles() {
256 return FileChooser.chooseFiles(myTable, myDescriptor);