update copyright
[fedora-idea.git] / java / idea-ui / src / com / intellij / openapi / roots / ui / configuration / AnnotationsEditor.java
blob10e076e8f0979264c10ca668c9db2445ae1f53ad
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.Project;
21 import com.intellij.openapi.project.ProjectBundle;
22 import com.intellij.openapi.roots.ModifiableRootModel;
23 import com.intellij.openapi.roots.AnnotationOrderRootType;
24 import com.intellij.openapi.roots.ui.util.CellAppearance;
25 import com.intellij.openapi.roots.ui.util.CellAppearanceUtils;
26 import com.intellij.openapi.roots.ui.util.SimpleTextCellAppearance;
27 import com.intellij.openapi.ui.PanelWithText;
28 import com.intellij.openapi.util.IconLoader;
29 import com.intellij.openapi.vfs.VirtualFile;
30 import com.intellij.openapi.vfs.VirtualFileManager;
31 import com.intellij.ui.ColoredTableCellRenderer;
32 import com.intellij.ui.ScrollPaneFactory;
33 import com.intellij.ui.TableUtil;
34 import com.intellij.util.ui.ItemRemovable;
35 import com.intellij.util.ui.Table;
36 import com.intellij.util.ArrayUtil;
38 import javax.swing.*;
39 import javax.swing.border.Border;
40 import javax.swing.event.ListSelectionEvent;
41 import javax.swing.event.ListSelectionListener;
42 import javax.swing.table.DefaultTableModel;
43 import java.awt.*;
44 import java.awt.event.ActionEvent;
45 import java.awt.event.ActionListener;
46 import java.util.List;
48 /**
49 * @author Eugene Zhuravlev
50 * Date: Oct 4, 2003
51 * Time: 6:54:57 PM
53 public class AnnotationsEditor extends ModuleElementsEditor {
54 private JTable myTable;
55 private JButton myAddPathButton;
56 private JButton myRemoveButton;
58 public static final String NAME = ProjectBundle.message("project.roots.external.annotations.tab.title");
59 public static final Icon ICON = IconLoader.getIcon("/modules/annotation.png");
61 public AnnotationsEditor(Project project, ModifiableRootModel model) {
62 super(project, model);
65 public String getHelpTopic() {
66 return "project.paths.annotations";//todo
69 public String getDisplayName() {
70 return NAME;
73 public Icon getIcon() {
74 return ICON;
77 public void saveData() {
78 TableUtil.stopEditing(myTable);
79 final int count = myTable.getRowCount();
80 String[] urls = ArrayUtil.newStringArray(count);
81 for (int row = 0; row < count; row++) {
82 final TableItem item = ((MyTableModel)myTable.getModel()).getTableItemAt(row);
83 urls[row] = item.getUrl();
85 myModel.setRootUrls(AnnotationOrderRootType.getInstance(), urls);
88 public JComponent createComponentImpl() {
89 final JPanel mainPanel = new JPanel(new BorderLayout());
90 mainPanel.setBorder(BorderFactory.createEmptyBorder(6, 6, 6, 6));
91 mainPanel.setPreferredSize(new Dimension(-1, 200));
92 final DefaultTableModel tableModel = createModel();
93 myTable = new Table(tableModel);
94 myTable.setIntercellSpacing(new Dimension(0, 0));
95 myTable.setDefaultRenderer(TableItem.class, new MyRenderer());
96 myTable.setShowGrid(false);
97 myTable.setDragEnabled(false);
98 myTable.setShowHorizontalLines(false);
99 myTable.setShowVerticalLines(false);
100 myTable.getSelectionModel().setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
102 myAddPathButton = new JButton(ProjectBundle.message("module.javadoc.add.path.button"));
103 myAddPathButton.addActionListener(new AddPathActionListener());
106 myRemoveButton = new JButton(ProjectBundle.message("module.javadoc.remove.button"));
107 myRemoveButton.addActionListener(new ActionListener() {
108 public void actionPerformed(ActionEvent e) {
109 final List removedItems = TableUtil.removeSelectedItems(myTable);
110 if (removedItems.size() > 0) {
111 saveData();
116 final JPanel panel = new JPanel(new GridBagLayout());
117 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));
118 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));
120 mainPanel.add(ScrollPaneFactory.createScrollPane(myTable), BorderLayout.CENTER);
121 mainPanel.add(panel, BorderLayout.EAST);
122 final PanelWithText panelWithText = new PanelWithText(ProjectBundle.message("project.roots.external.annotations.description"));
123 panelWithText.setBorder(null);
124 mainPanel.add(panelWithText, BorderLayout.NORTH);
126 myTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
127 public void valueChanged(ListSelectionEvent e) {
128 if (e.getValueIsAdjusting()) {
129 return;
131 final int selectedIndex = myTable.getSelectedRow();
132 myRemoveButton.setEnabled(selectedIndex >= 0);
135 if (tableModel.getRowCount() > 0) {
136 TableUtil.selectRows(myTable, new int[] {0});
138 else {
139 myRemoveButton.setEnabled(false);
141 return mainPanel;
144 protected DefaultTableModel createModel() {
145 final MyTableModel tableModel = new MyTableModel();
146 final String[] urls = myModel.getRootUrls(AnnotationOrderRootType.getInstance());
147 for (String javadocUrl : urls) {
148 tableModel.addTableItem(new TableItem(javadocUrl));
150 return tableModel;
153 public void moduleStateChanged() {
154 if (myTable != null) {
155 final DefaultTableModel tableModel = createModel();
156 myTable.setModel(tableModel);
157 myRemoveButton.setEnabled(tableModel.getRowCount() > 0);
161 private static class MyRenderer extends ColoredTableCellRenderer {
162 private static final Border NO_FOCUS_BORDER = BorderFactory.createEmptyBorder(1, 1, 1, 1);
164 protected void customizeCellRenderer(JTable table, Object value, boolean selected, boolean hasFocus, int row, int column) {
165 setPaintFocusBorder(false);
166 setFocusBorderAroundIcon(true);
167 setBorder(NO_FOCUS_BORDER);
169 final TableItem tableItem = ((TableItem)value);
170 tableItem.getCellAppearance().customize(this);
174 private static class TableItem {
175 private final String myUrl;
176 private final CellAppearance myCellAppearance;
177 public TableItem(VirtualFile file) {
178 myUrl = file.getUrl();
179 myCellAppearance = CellAppearanceUtils.forVirtualFile(file);
182 public TableItem(String url) {
183 myUrl = url;
184 final VirtualFile file = VirtualFileManager.getInstance().findFileByUrl(url);
185 if (file != null) {
186 myCellAppearance = CellAppearanceUtils.forVirtualFile(file);
188 else {
189 myCellAppearance = SimpleTextCellAppearance.invalid(url, CellAppearanceUtils.INVALID_ICON);
193 public String getUrl() {
194 return myUrl;
197 public CellAppearance getCellAppearance() {
198 return myCellAppearance;
202 private static class MyTableModel extends DefaultTableModel implements ItemRemovable{
203 public String getColumnName(int column) {
204 return null;
207 public Class getColumnClass(int columnIndex) {
208 return TableItem.class;
211 public int getColumnCount() {
212 return 1;
215 public boolean isCellEditable(int row, int column) {
216 return false;
219 public TableItem getTableItemAt(int row) {
220 return (TableItem)getValueAt(row, 0);
223 public void addTableItem(TableItem item) {
224 addRow(new Object[] {item});
228 private abstract class MyAddAction implements ActionListener {
229 protected abstract VirtualFile[] getFiles();
231 public void actionPerformed(ActionEvent e) {
232 VirtualFile[] files = getFiles();
233 final MyTableModel tableModel = (MyTableModel)myTable.getModel();
234 boolean changes = false;
235 for (final VirtualFile file : files) {
236 if (file != null) {
237 tableModel.addTableItem(new TableItem(file));
238 changes = true;
241 if (changes) {
242 saveData();
243 TableUtil.selectRows(myTable, new int[] {tableModel.getRowCount() - 1});
248 private class AddPathActionListener extends MyAddAction{
249 private final FileChooserDescriptor myDescriptor;
251 public AddPathActionListener() {
252 myDescriptor = new FileChooserDescriptor(false, true, true, false, true, true);
253 myDescriptor.setTitle(ProjectBundle.message("add.external.annotations.path.title"));
254 myDescriptor.setDescription(ProjectBundle.message("add.external.annotations.path.description"));
257 protected VirtualFile[] getFiles() {
258 return FileChooser.chooseFiles(myTable, myDescriptor);