NotNull, dispose
[fedora-idea.git] / platform / lang-impl / src / com / intellij / execution / impl / DefaultConfigurationSettingsEditor.java
blob028982320183f2e5fdba298b2a5a112ed4cc1718
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.intellij.execution.impl;
19 import com.intellij.execution.ExecutionBundle;
20 import com.intellij.execution.configurations.ConfigurationType;
21 import com.intellij.openapi.options.Configurable;
22 import com.intellij.openapi.options.ConfigurationException;
23 import com.intellij.openapi.project.Project;
24 import com.intellij.openapi.util.Comparing;
25 import com.intellij.ui.ScrollPaneFactory;
26 import com.intellij.ui.TreeToolTipHandler;
27 import com.intellij.util.ui.tree.TreeUtil;
28 import org.jetbrains.annotations.NonNls;
29 import org.jetbrains.annotations.Nullable;
31 import javax.swing.*;
32 import javax.swing.event.TreeSelectionEvent;
33 import javax.swing.event.TreeSelectionListener;
34 import javax.swing.tree.DefaultMutableTreeNode;
35 import javax.swing.tree.DefaultTreeCellRenderer;
36 import javax.swing.tree.DefaultTreeModel;
37 import javax.swing.tree.TreePath;
38 import java.awt.*;
39 import java.util.HashMap;
40 import java.util.Map;
42 /**
43 * User: anna
44 * Date: 29-Mar-2006
46 public class DefaultConfigurationSettingsEditor implements Configurable {
48 @NonNls private final DefaultMutableTreeNode myRoot = new DefaultMutableTreeNode("Root");
49 private final JTree myTree = new JTree(myRoot);
50 private final Project myProject;
51 private final Map<ConfigurationType, Configurable> myStoredComponents = new HashMap<ConfigurationType, Configurable>();
52 private final ConfigurationType mySelection;
54 public DefaultConfigurationSettingsEditor(Project project, ConfigurationType selection) {
55 myProject = project;
56 mySelection = selection;
59 public JComponent createComponent() {
60 final JPanel wholePanel = new JPanel(new BorderLayout());
61 final JScrollPane pane = ScrollPaneFactory.createScrollPane(myTree);
62 pane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
63 wholePanel.add(pane, BorderLayout.WEST);
64 final JPanel rightPanel = new JPanel(new BorderLayout());
65 wholePanel.add(rightPanel, BorderLayout.CENTER);
66 final ConfigurationType[] configurationTypes = RunManagerImpl.getInstanceImpl(myProject).getConfigurationFactories();
67 for (ConfigurationType type : configurationTypes) {
68 myRoot.add(new DefaultMutableTreeNode(type));
70 myTree.setRootVisible(false);
71 TreeToolTipHandler.install(myTree);
72 TreeUtil.installActions(myTree);
73 myTree.setCellRenderer(new DefaultTreeCellRenderer() {
74 public Component getTreeCellRendererComponent(JTree tree,
75 Object value,
76 boolean sel,
77 boolean expanded,
78 boolean leaf,
79 int row,
80 boolean hasFocus) {
81 final Component rendererComponent = super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
82 if (value instanceof DefaultMutableTreeNode) {
83 final Object userObject = ((DefaultMutableTreeNode)value).getUserObject();
84 if (userObject instanceof ConfigurationType) {
85 final ConfigurationType type = (ConfigurationType)userObject;
86 setText(type.getDisplayName());
87 setIcon(type.getIcon());
90 return rendererComponent;
92 });
93 myTree.getSelectionModel().addTreeSelectionListener(new TreeSelectionListener() {
94 public void valueChanged(TreeSelectionEvent e) {
95 final TreePath selectionPath = myTree.getSelectionPath();
96 if (selectionPath != null) {
97 final DefaultMutableTreeNode node = (DefaultMutableTreeNode)selectionPath.getLastPathComponent();
98 final ConfigurationType type = (ConfigurationType)node.getUserObject();
99 rightPanel.removeAll();
100 Configurable configurable = myStoredComponents.get(type);
101 if (configurable == null){
102 configurable = TypeTemplatesConfigurable.createConfigurable(type, myProject);
103 myStoredComponents.put(type, configurable);
104 rightPanel.add(configurable.createComponent());
105 configurable.reset();
106 } else {
107 rightPanel.add(configurable.createComponent());
109 rightPanel.revalidate();
110 rightPanel.repaint();
111 final Window window = SwingUtilities.windowForComponent(wholePanel);
112 if (window != null &&
113 (window.getSize().height < window.getMinimumSize().height ||
114 window.getSize().width < window.getMinimumSize().width)) {
115 window.pack();
120 RunConfigurable.sortTree(myRoot);
121 ((DefaultTreeModel)myTree.getModel()).reload();
122 TreeUtil.selectFirstNode(myTree);
123 TreeUtil.traverse(myRoot, new TreeUtil.Traverse() {
124 public boolean accept(Object node) {
125 if (node instanceof DefaultMutableTreeNode){
126 final DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode)node;
127 final Object o = treeNode.getUserObject();
128 if (Comparing.equal(o, mySelection)){
129 TreeUtil.selectInTree(treeNode, true, myTree);
130 return false;
133 return true;
136 return wholePanel;
139 public boolean isModified() {
140 for (Configurable configurable : myStoredComponents.values()) {
141 if (configurable.isModified()) return true;
143 return false;
146 public void apply() throws ConfigurationException {
147 for (Configurable configurable : myStoredComponents.values()) {
148 if (configurable.isModified()){
149 configurable.apply();
154 public void reset() {
155 //do nothing
158 public void disposeUIResources() {
159 for (Configurable configurable : myStoredComponents.values()) {
160 configurable.disposeUIResources();
164 public String getDisplayName() {
165 return ExecutionBundle.message("default.settings.editor.dialog.title");
168 public Icon getIcon() {
169 return null;
172 @Nullable
173 @NonNls
174 public String getHelpTopic() {
175 return null;