find usages & validation in project structure reworked
[fedora-idea.git] / java / idea-ui / src / com / intellij / openapi / roots / ui / configuration / artifacts / ArtifactConfigurable.java
blob2bf7cbb8db8c8fd309e91b9e5becbd8a06896f8a
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.artifacts;
18 import com.intellij.openapi.options.ConfigurationException;
19 import com.intellij.openapi.project.ProjectBundle;
20 import com.intellij.openapi.roots.ui.configuration.projectRoot.ProjectStructureElementConfigurable;
21 import com.intellij.openapi.roots.ui.configuration.projectRoot.StructureConfigurableContext;
22 import com.intellij.openapi.roots.ui.configuration.projectRoot.daemon.ProjectStructureElement;
23 import com.intellij.openapi.ui.ComboBox;
24 import com.intellij.openapi.util.Comparing;
25 import com.intellij.packaging.artifacts.Artifact;
26 import com.intellij.packaging.artifacts.ArtifactType;
27 import com.intellij.packaging.elements.CompositePackagingElement;
28 import com.intellij.packaging.impl.artifacts.ArtifactUtil;
29 import org.jetbrains.annotations.Nls;
31 import javax.swing.*;
32 import java.awt.*;
33 import java.awt.event.ActionEvent;
34 import java.awt.event.ActionListener;
36 /**
37 * @author nik
39 public class ArtifactConfigurable extends ProjectStructureElementConfigurable<Artifact> {
40 private final Artifact myOriginalArtifact;
41 private final ArtifactsStructureConfigurableContext myArtifactsStructureContext;
42 private final ArtifactEditorImpl myEditor;
43 private boolean myIsInUpdateName;
44 private ProjectStructureElement myProjectStructureElement;
46 public ArtifactConfigurable(Artifact originalArtifact, ArtifactsStructureConfigurableContextImpl artifactsStructureContext, final Runnable updateTree,
47 StructureConfigurableContext context) {
48 super(true, updateTree);
49 myOriginalArtifact = originalArtifact;
50 myArtifactsStructureContext = artifactsStructureContext;
51 myEditor = artifactsStructureContext.getOrCreateEditor(originalArtifact);
52 myProjectStructureElement = new ArtifactProjectStructureElement(context, myArtifactsStructureContext, myOriginalArtifact);
55 public void setDisplayName(String name) {
56 final String oldName = getArtifact().getName();
57 if (name != null && !name.equals(oldName) && !myIsInUpdateName) {
58 myArtifactsStructureContext.getModifiableArtifactModel().getOrCreateModifiableArtifact(myOriginalArtifact).setName(name);
59 myEditor.updateOutputPath(oldName, name);
63 @Override
64 public ProjectStructureElement getProjectStructureElement() {
65 return myProjectStructureElement;
68 @Override
69 public void updateName() {
70 myIsInUpdateName = true;
71 try {
72 super.updateName();
74 finally {
75 myIsInUpdateName = false;
79 private Artifact getArtifact() {
80 return myArtifactsStructureContext.getArtifactModel().getArtifactByOriginal(myOriginalArtifact);
83 public Artifact getEditableObject() {
84 return getArtifact();
87 public String getBannerSlogan() {
88 return ProjectBundle.message("banner.slogan.artifact.0", getDisplayName());
91 public JComponent createOptionsPanel() {
92 return myEditor.createMainComponent();
95 @Nls
96 public String getDisplayName() {
97 return getArtifact().getName();
100 public Icon getIcon() {
101 return getArtifact().getArtifactType().getIcon();
104 public String getHelpTopic() {
105 return "reference.settingsdialog.project.structure.artifacts";
108 @Override
109 protected JComponent createTopRightComponent() {
110 final ComboBox artifactTypeBox = new ComboBox();
111 for (ArtifactType type : ArtifactType.getAllTypes()) {
112 artifactTypeBox.addItem(type);
115 artifactTypeBox.setRenderer(new DefaultListCellRenderer() {
116 @Override
117 public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
118 final Component component = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
119 final ArtifactType type = (ArtifactType)value;
120 setIcon(type.getIcon());
121 setText(type.getPresentableName());
122 return component;
126 artifactTypeBox.setSelectedItem(getArtifact().getArtifactType());
127 artifactTypeBox.addActionListener(new ActionListener() {
128 public void actionPerformed(ActionEvent e) {
129 final ArtifactType selected = (ArtifactType)artifactTypeBox.getSelectedItem();
130 if (!Comparing.equal(selected, getArtifact().getArtifactType())) {
131 final CompositePackagingElement<?> element = myEditor.getRootElement();
132 final CompositePackagingElement<?> newRootElement = selected.createRootElement(getArtifact().getName());
133 myArtifactsStructureContext.getModifiableArtifactModel().getOrCreateModifiableArtifact(myOriginalArtifact).setArtifactType(selected);
134 if (!newRootElement.getType().equals(element.getType())) {
135 ArtifactUtil.copyChildren(element, newRootElement, myArtifactsStructureContext.getProject());
136 myEditor.getLayoutTreeComponent().setRootElement(newRootElement);
142 final JPanel panel = new JPanel(new FlowLayout());
143 panel.add(new JLabel("Type: "));
144 panel.add(artifactTypeBox);
145 return panel;
148 public boolean isModified() {
149 return myEditor.isModified();
152 public void apply() throws ConfigurationException {
153 myEditor.apply();
156 public void reset() {
159 public void disposeUIResources() {