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
.actionSystem
.AnAction
;
19 import com
.intellij
.openapi
.actionSystem
.AnActionEvent
;
20 import com
.intellij
.openapi
.application
.Result
;
21 import com
.intellij
.openapi
.application
.WriteAction
;
22 import com
.intellij
.openapi
.components
.State
;
23 import com
.intellij
.openapi
.components
.Storage
;
24 import com
.intellij
.openapi
.options
.ConfigurationException
;
25 import com
.intellij
.openapi
.project
.DumbAwareAction
;
26 import com
.intellij
.openapi
.project
.Project
;
27 import com
.intellij
.openapi
.project
.ProjectBundle
;
28 import com
.intellij
.openapi
.roots
.ui
.configuration
.projectRoot
.BaseStructureConfigurable
;
29 import com
.intellij
.openapi
.roots
.ui
.configuration
.projectRoot
.StructureConfigurableContext
;
30 import com
.intellij
.openapi
.roots
.ui
.configuration
.projectRoot
.daemon
.ProjectStructureElement
;
31 import com
.intellij
.packaging
.artifacts
.*;
32 import org
.jetbrains
.annotations
.Nls
;
33 import org
.jetbrains
.annotations
.NonNls
;
34 import org
.jetbrains
.annotations
.NotNull
;
35 import org
.jetbrains
.annotations
.Nullable
;
38 import java
.util
.ArrayList
;
39 import java
.util
.Collection
;
40 import java
.util
.List
;
46 name
= "ArtifactsStructureConfigurable.UI",
47 storages
= {@Storage(id
= "other", file
= "$WORKSPACE_FILE$")}
49 public class ArtifactsStructureConfigurable
extends BaseStructureConfigurable
{
50 private ArtifactsStructureConfigurableContextImpl myPackagingEditorContext
;
51 @NonNls private static final String DEFAULT_ARTIFACT_NAME
= "unnamed";
53 public ArtifactsStructureConfigurable(@NotNull Project project
) {
58 public void init(StructureConfigurableContext context
) {
60 myPackagingEditorContext
= new ArtifactsStructureConfigurableContextImpl(myContext
, myProject
, new ArtifactAdapter() {
62 public void artifactAdded(@NotNull Artifact artifact
) {
63 final MyNode node
= addArtifactNode(artifact
);
64 selectNodeInTree(node
);
65 myContext
.getDaemonAnalyzer().queueUpdate(new ArtifactProjectStructureElement(myContext
, myPackagingEditorContext
, artifact
));
71 public String
getDisplayName() {
72 return ProjectBundle
.message("display.name.artifacts");
75 protected void loadTree() {
76 myTree
.setRootVisible(false);
77 myTree
.setShowsRootHandles(false);
78 for (Artifact artifact
: myPackagingEditorContext
.getArtifactModel().getArtifacts()) {
79 addArtifactNode(artifact
);
85 protected Collection
<?
extends ProjectStructureElement
> getProjectStructureElements() {
86 final List
<ProjectStructureElement
> elements
= new ArrayList
<ProjectStructureElement
>();
87 for (Artifact artifact
: myPackagingEditorContext
.getArtifactModel().getArtifacts()) {
88 elements
.add(new ArtifactProjectStructureElement(myContext
, myPackagingEditorContext
, artifact
));
93 private MyNode
addArtifactNode(final Artifact artifact
) {
94 final MyNode node
= new MyNode(new ArtifactConfigurable(artifact
, myPackagingEditorContext
, TREE_UPDATER
, myContext
));
95 addNode(node
, myRoot
);
100 public void reset() {
101 myPackagingEditorContext
.resetModifiableModel();
106 public boolean isModified() {
107 final ModifiableArtifactModel modifiableModel
= myPackagingEditorContext
.getActualModifiableModel();
108 if (modifiableModel
!= null && modifiableModel
.isModified()) {
111 return myPackagingEditorContext
.getManifestFilesInfo().isManifestFilesModified() || super.isModified();
114 public ArtifactsStructureConfigurableContext
getArtifactsStructureContext() {
115 return myPackagingEditorContext
;
118 public ModifiableArtifactModel
getModifiableArtifactModel() {
119 return myPackagingEditorContext
.getModifiableArtifactModel();
122 protected AbstractAddGroup
createAddAction() {
123 return new AbstractAddGroup(ProjectBundle
.message("add.new.header.text")) {
126 public AnAction
[] getChildren(@Nullable AnActionEvent e
) {
127 final ArtifactType
[] types
= ArtifactType
.getAllTypes();
128 final AnAction
[] actions
= new AnAction
[types
.length
];
129 for (int i
= 0; i
< types
.length
; i
++) {
130 actions
[i
] = new AddArtifactAction(types
[i
]);
137 private void addArtifact(@NotNull ArtifactType type
) {
138 String name
= DEFAULT_ARTIFACT_NAME
;
140 while (myPackagingEditorContext
.getArtifactModel().findArtifact(name
) != null) {
141 name
= DEFAULT_ARTIFACT_NAME
+ i
;
144 final ModifiableArtifact artifact
= myPackagingEditorContext
.getModifiableArtifactModel().addArtifact(name
, type
);
145 selectNodeInTree(findNodeByObject(myRoot
, artifact
));
149 public void apply() throws ConfigurationException
{
151 myPackagingEditorContext
.getManifestFilesInfo().saveManifestFiles();
152 final ModifiableArtifactModel modifiableModel
= myPackagingEditorContext
.getActualModifiableModel();
153 if (modifiableModel
!= null) {
155 protected void run(final Result result
) {
156 modifiableModel
.commit();
159 myPackagingEditorContext
.resetModifiableModel();
164 public void disposeUIResources() {
165 super.disposeUIResources();
166 myPackagingEditorContext
.disposeUIResources();
170 public String
getHelpTopic() {
171 final String topic
= super.getHelpTopic();
172 return topic
!= null ? topic
: "reference.settingsdialog.project.structure.artifacts";
176 protected void removeArtifact(Artifact artifact
) {
177 myPackagingEditorContext
.getModifiableArtifactModel().removeArtifact(artifact
);
178 myContext
.getDaemonAnalyzer().removeElement(new ArtifactProjectStructureElement(myContext
, myPackagingEditorContext
, artifact
));
181 protected void processRemovedItems() {
184 protected boolean wasObjectStored(Object editableObject
) {
188 public String
getId() {
189 return "project.artifacts";
192 public Runnable
enableSearch(String option
) {
196 public void dispose() {
199 public Icon
getIcon() {
204 private class AddArtifactAction
extends DumbAwareAction
{
205 private final ArtifactType myType
;
207 public AddArtifactAction(ArtifactType type
) {
208 super(ProjectBundle
.message("action.text.add.artifact", type
.getPresentableName()), null, type
.getIcon());
213 public void actionPerformed(AnActionEvent e
) {