exception #198317
[fedora-idea.git] / plugins / copyright / src / com / maddyhome / idea / copyright / ui / ProjectSettingsPanel.java
blob68cc209a188fe39c15fea0c767f6559a754f7545
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.
17 package com.maddyhome.idea.copyright.ui;
19 import com.intellij.ide.DataManager;
20 import com.intellij.ide.util.scopeChooser.PackageSetChooserCombo;
21 import com.intellij.ide.util.scopeChooser.ScopeChooserConfigurable;
22 import com.intellij.openapi.options.newEditor.OptionsEditor;
23 import com.intellij.openapi.project.Project;
24 import com.intellij.openapi.ui.ComboBox;
25 import com.intellij.openapi.ui.LabeledComponent;
26 import com.intellij.openapi.util.Comparing;
27 import com.intellij.packageDependencies.DefaultScopesProvider;
28 import com.intellij.packageDependencies.DependencyValidationManager;
29 import com.intellij.psi.search.scope.packageSet.NamedScope;
30 import com.intellij.ui.HyperlinkLabel;
31 import com.intellij.ui.PanelWithButtons;
32 import com.intellij.ui.TableUtil;
33 import com.intellij.ui.table.TableView;
34 import com.intellij.util.ui.*;
35 import com.maddyhome.idea.copyright.CopyrightManager;
36 import com.maddyhome.idea.copyright.CopyrightProfile;
37 import org.jetbrains.annotations.NotNull;
38 import org.jetbrains.annotations.Nullable;
40 import javax.swing.*;
41 import javax.swing.event.HyperlinkEvent;
42 import javax.swing.event.HyperlinkListener;
43 import javax.swing.event.ListSelectionEvent;
44 import javax.swing.event.ListSelectionListener;
45 import javax.swing.table.DefaultTableCellRenderer;
46 import javax.swing.table.TableCellEditor;
47 import javax.swing.table.TableCellRenderer;
48 import java.awt.*;
49 import java.awt.event.ActionEvent;
50 import java.awt.event.ActionListener;
51 import java.awt.event.ItemEvent;
52 import java.awt.event.ItemListener;
53 import java.util.*;
54 import java.util.List;
56 public class ProjectSettingsPanel extends PanelWithButtons {
57 private final Project myProject;
58 private final CopyrightProfilesPanel myProfilesModel;
59 private final CopyrightManager myManager;
61 private final TableView<ScopeSetting> myScopeMappingTable;
62 private final ListTableModel<ScopeSetting> myScopeMappingModel;
63 private final JComboBox myProfilesComboBox = new JComboBox();
65 private JButton myAddButton;
66 private JButton myRemoveButton;
67 private JButton myMoveUpButton;
68 private JButton myMoveDownButton;
70 private final HyperlinkLabel myScopesLink = new HyperlinkLabel();
72 public ProjectSettingsPanel(Project project, CopyrightProfilesPanel profilesModel) {
73 myProject = project;
74 myProfilesModel = profilesModel;
75 myProfilesModel.addItemsChangeListener(new Runnable(){
76 public void run() {
77 final Object selectedItem = myProfilesComboBox.getSelectedItem();
78 fillCopyrightProfiles();
79 myProfilesComboBox.setSelectedItem(selectedItem);
80 final ArrayList<ScopeSetting> toRemove = new ArrayList<ScopeSetting>();
81 for (ScopeSetting setting : myScopeMappingModel.getItems()) {
82 if (setting.getProfile() == null) {
83 toRemove.add(setting);
86 for (ScopeSetting setting : toRemove) {
87 myScopeMappingModel.removeRow(myScopeMappingModel.indexOf(setting));
89 updateButtons();
91 });
92 myManager = CopyrightManager.getInstance(project);
94 myScopeMappingModel = new ListTableModel<ScopeSetting>(new ColumnInfo[]{SCOPE, SETTING}, new ArrayList<ScopeSetting>(), 0);
95 myScopeMappingTable = new TableView<ScopeSetting>(myScopeMappingModel);
97 fillCopyrightProfiles();
98 myProfilesComboBox.setRenderer(new DefaultListCellRenderer() {
99 public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
100 final Component rendererComponent = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
101 if (value == null) {
102 setText("No copyright");
103 } else {
104 setText(((CopyrightProfile) value).getName());
106 return rendererComponent;
110 myScopeMappingTable.setRowHeight(myProfilesComboBox.getPreferredSize().height);
111 myScopeMappingTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
112 public void valueChanged(final ListSelectionEvent e) {
113 updateButtons();
116 initPanel();
118 myScopesLink.setVisible(!myProject.isDefault());
119 myScopesLink.setHyperlinkText("Select Scopes to add new scopes or modify existing ones");
120 myScopesLink.addHyperlinkListener(new HyperlinkListener() {
121 public void hyperlinkUpdate(final HyperlinkEvent e) {
122 if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
123 final OptionsEditor optionsEditor = OptionsEditor.KEY.getData(DataManager.getInstance().getDataContext());
124 if (optionsEditor != null) {
125 optionsEditor.select(ScopeChooserConfigurable.getInstance(myProject));
132 private void updateButtons() {
133 myAddButton.setEnabled(!myProfilesModel.getAllProfiles().isEmpty());
134 int index = myScopeMappingTable.getSelectedRow();
135 if (0 <= index && index < myScopeMappingModel.getRowCount()) {
136 myRemoveButton.setEnabled(true);
137 myMoveUpButton.setEnabled(index > 0);
138 myMoveDownButton.setEnabled(index < myScopeMappingModel.getRowCount() - 1);
140 else {
141 myRemoveButton.setEnabled(false);
142 myMoveUpButton.setEnabled(false);
143 myMoveDownButton.setEnabled(false);
147 @Nullable
148 protected String getLabelText() {
149 return null;
152 protected JButton[] createButtons() {
153 myAddButton = new JButton("Add");
154 myAddButton.setMnemonic('d');
155 myAddButton.addActionListener(new ActionListener() {
156 public void actionPerformed(final ActionEvent e) {
157 TableUtil.stopEditing(myScopeMappingTable);
158 List<ScopeSetting> newList = new ArrayList<ScopeSetting>(myScopeMappingModel.getItems());
159 newList.add(new ScopeSetting(DefaultScopesProvider.getAllScope(),
160 myProfilesModel.getAllProfiles().values().iterator().next()));
161 myScopeMappingModel.setItems(newList);
162 TableUtil.editCellAt(myScopeMappingTable, myScopeMappingModel.getRowCount() - 1, 0);
165 myRemoveButton = new JButton("Remove");
166 myRemoveButton.setMnemonic('R');
167 myRemoveButton.addActionListener(new ActionListener() {
168 public void actionPerformed(final ActionEvent e) {
169 TableUtil.stopEditing(myScopeMappingTable);
170 int index = myScopeMappingTable.getSelectedRow();
171 if (0 <= index && index < myScopeMappingModel.getRowCount()) {
172 myScopeMappingModel.removeRow(index);
173 if (index < myScopeMappingModel.getRowCount()) {
174 myScopeMappingTable.setRowSelectionInterval(index, index);
175 } else {
176 if (index > 0) {
177 myScopeMappingTable.setRowSelectionInterval(index - 1, index - 1);
180 updateButtons();
182 myScopeMappingTable.requestFocus();
186 myMoveUpButton = new JButton("Move Up");
187 myMoveUpButton.setMnemonic('U');
188 myMoveUpButton.addActionListener(
189 new ActionListener() {
190 public void actionPerformed(ActionEvent e) {
191 TableUtil.moveSelectedItemsUp(myScopeMappingTable);
196 myMoveDownButton = new JButton("Move Down");
197 myMoveDownButton.setMnemonic('D');
198 myMoveDownButton.addActionListener(
199 new ActionListener() {
200 public void actionPerformed(ActionEvent e) {
201 TableUtil.moveSelectedItemsDown(myScopeMappingTable);
206 return new JButton[]{myAddButton, myRemoveButton, myMoveUpButton, myMoveDownButton};
209 private void fillCopyrightProfiles() {
210 final DefaultComboBoxModel boxModel = (DefaultComboBoxModel) myProfilesComboBox.getModel();
211 boxModel.removeAllElements();
212 boxModel.addElement(null);
213 for (CopyrightProfile profile : myProfilesModel.getAllProfiles().values()) {
214 boxModel.addElement(profile);
219 protected JComponent createMainComponent() {
220 return new JScrollPane(myScopeMappingTable);
223 public JComponent getMainComponent() {
224 final JPanel panel = new JPanel(new BorderLayout(0, 10));
225 final LabeledComponent<JComboBox> component = new LabeledComponent<JComboBox>();
226 component.setText("Default &project copyright:");
227 component.setLabelLocation(BorderLayout.WEST);
228 component.setComponent(myProfilesComboBox);
229 panel.add(component, BorderLayout.NORTH);
230 panel.add(this, BorderLayout.CENTER);
231 panel.add(myScopesLink, BorderLayout.SOUTH);
232 return panel;
235 public boolean isModified() {
236 final CopyrightProfile defaultCopyright = myManager.getDefaultCopyright();
237 final Object selected = myProfilesComboBox.getSelectedItem();
238 if (defaultCopyright != selected) {
239 if (selected == null) return true;
240 if (defaultCopyright == null) return true;
241 if (!defaultCopyright.equals(selected)) return true;
243 final Map<String, String> map = myManager.getCopyrightsMapping();
244 if (map.size() != myScopeMappingModel.getItems().size()) return true;
245 final Iterator<String> iterator = map.keySet().iterator();
246 for (ScopeSetting setting : myScopeMappingModel.getItems()) {
247 final NamedScope scope = setting.getScope();
248 if (!iterator.hasNext()) return true;
249 final String scopeName = iterator.next();
250 if (!Comparing.strEqual(scopeName, scope.getName())) return true;
251 final String profileName = map.get(scope.getName());
252 if (profileName == null) return true;
253 if (!profileName.equals(setting.getProfileName())) return true;
255 return false;
258 public void apply() {
259 Collection<CopyrightProfile> profiles = new ArrayList<CopyrightProfile>(myManager.getCopyrights());
260 myManager.clearCopyrights();
261 for (CopyrightProfile profile : profiles) {
262 myManager.addCopyright(profile);
264 final List<ScopeSetting> settingList = myScopeMappingModel.getItems();
265 for (ScopeSetting scopeSetting : settingList) {
266 myManager.mapCopyright(scopeSetting.getScope().getName(), scopeSetting.getProfileName());
268 myManager.setDefaultCopyright((CopyrightProfile) myProfilesComboBox.getSelectedItem());
271 public void reset() {
272 myProfilesComboBox.setSelectedItem(myManager.getDefaultCopyright());
273 final List<ScopeSetting> mappings = new ArrayList<ScopeSetting>();
274 final Map<String, String> copyrights = myManager.getCopyrightsMapping();
275 final DependencyValidationManager manager = DependencyValidationManager.getInstance(myProject);
276 for (final String scopeName : copyrights.keySet()) {
277 final NamedScope scope = manager.getScope(scopeName);
278 if (scope != null) {
279 mappings.add(new ScopeSetting(scope, copyrights.get(scopeName)));
280 } else {
281 myManager.unmapCopyright(scopeName);
284 myScopeMappingModel.setItems(mappings);
285 updateButtons();
289 private class ScopeSetting {
290 private NamedScope myScope;
291 private CopyrightProfile myProfile;
292 private String myProfileName;
294 private ScopeSetting(NamedScope scope, CopyrightProfile profile) {
295 myScope = scope;
296 myProfile = profile;
297 if (myProfile != null) {
298 myProfileName = myProfile.getName();
302 public ScopeSetting(NamedScope scope, String profile) {
303 myScope = scope;
304 myProfileName = profile;
307 public CopyrightProfile getProfile() {
308 if (myProfileName != null) {
309 myProfile = myProfilesModel.getAllProfiles().get(getProfileName());
311 return myProfile;
314 public void setProfile(@NotNull CopyrightProfile profile) {
315 myProfile = profile;
316 myProfileName = profile.getName();
319 public NamedScope getScope() {
320 return myScope;
323 public void setScope(NamedScope scope) {
324 myScope = scope;
327 public String getProfileName() {
328 return myProfile != null ? myProfile.getName() : myProfileName;
333 private final ColumnInfo<ScopeSetting, CopyrightProfile> SETTING = new ColumnInfo<ScopeSetting, CopyrightProfile>("Copyright") {
334 public CopyrightProfile valueOf(final ScopeSetting object) {
335 return object.getProfile();
338 public boolean isCellEditable(final ScopeSetting o) {
339 return true;
343 public TableCellRenderer getRenderer(final ScopeSetting scopeSetting) {
344 return new DefaultTableCellRenderer() {
345 // implements javax.swing.table.TableCellRenderer
346 public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
347 final Component rendererComponent = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
348 if (!isSelected)
349 setForeground(myProfilesModel.getAllProfiles().get(scopeSetting.getProfileName()) == null ? Color.red : UIUtil.getTableForeground());
350 setText(scopeSetting.getProfileName());
351 return rendererComponent;
356 public TableCellEditor getEditor(ScopeSetting scopeSetting) {
357 return new AbstractTableCellEditor() {
358 private ComboBox myProfilesCombo;
360 public Object getCellEditorValue() {
361 return myProfilesCombo.getSelectedItem();
364 public Component getTableCellEditorComponent(final JTable table, Object value, boolean isSelected, int row, int column) {
365 final Collection<CopyrightProfile> copyrights = myProfilesModel.getAllProfiles().values();
366 myProfilesCombo = new ComboBox(copyrights.toArray(new CopyrightProfile[copyrights.size()]), 60);
367 myProfilesCombo.addItemListener(new ItemListener() {
368 public void itemStateChanged(final ItemEvent e) {
369 if (table.isEditing()) {
370 stopCellEditing();
374 myProfilesCombo.setRenderer(new DefaultListCellRenderer() {
375 public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
376 Component rendererComponent = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
377 setText(((CopyrightProfile) value).getName());
378 return rendererComponent;
381 return myProfilesCombo;
386 public void setValue(ScopeSetting scopeSetting, CopyrightProfile copyrightProfile) {
387 if (copyrightProfile != null) {
388 scopeSetting.setProfile(copyrightProfile);
393 private final ColumnInfo<ScopeSetting, NamedScope> SCOPE = new ColumnInfo<ScopeSetting, NamedScope>("Scope") {
395 public boolean isCellEditable(ScopeSetting mapping) {
396 return true;
399 public TableCellRenderer getRenderer(ScopeSetting mapping) {
400 return new DefaultTableCellRenderer() {
401 public Component getTableCellRendererComponent(JTable table,
402 Object value,
403 boolean isSelected,
404 boolean hasFocus,
405 int row,
406 int column) {
407 super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
408 setText(value == null ? "" : ((NamedScope) value).getName());
409 return this;
414 public TableCellEditor getEditor(ScopeSetting mapping) {
415 return new AbstractTableCellEditor() {
416 private PackageSetChooserCombo myCombo;
418 public Object getCellEditorValue() {
419 return myCombo.getSelectedScope();
422 public Component getTableCellEditorComponent(final JTable table, Object value, boolean isSelected, int row, int column) {
423 myCombo = new PackageSetChooserCombo(myProject, value == null ? null : ((NamedScope) value).getName(), false);
424 myCombo.getComboBox().addItemListener(new ItemListener() {
425 public void itemStateChanged(final ItemEvent e) {
426 if (table.isEditing()) {
427 stopCellEditing();
431 return new CellEditorComponentWithBrowseButton(myCombo, this);
437 public NamedScope valueOf(ScopeSetting mapping) {
438 return mapping.getScope();
441 public void setValue(ScopeSetting mapping, NamedScope set) {
442 mapping.setScope(set);