NotNull, dispose
[fedora-idea.git] / platform / lang-impl / src / com / intellij / execution / impl / ProjectRunConfigurationManager.java
blobaa4cc2c27a9824ebbe95df44c6dd861f8422211a
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.openapi.components.*;
20 import com.intellij.openapi.diagnostic.Logger;
21 import com.intellij.openapi.util.Comparing;
22 import com.intellij.openapi.util.InvalidDataException;
23 import com.intellij.openapi.util.Pair;
24 import com.intellij.openapi.util.WriteExternalException;
25 import com.intellij.openapi.util.io.FileUtil;
26 import com.intellij.util.containers.HashSet;
27 import com.intellij.util.text.UniqueNameGenerator;
28 import org.jdom.Element;
29 import org.jetbrains.annotations.NonNls;
30 import org.jetbrains.annotations.NotNull;
32 import java.util.ArrayList;
33 import java.util.Collection;
34 import java.util.List;
35 import java.util.Set;
37 /**
38 * User: anna
39 * Date: 28-Mar-2006
41 @State(
42 name = "ProjectRunConfigurationManager",
43 storages = {
44 @Storage(id = "default", file = "$PROJECT_FILE$")
45 ,@Storage(id = "dir", file = "$PROJECT_CONFIG_DIR$/runConfigurations/", scheme = StorageScheme.DIRECTORY_BASED, stateSplitter = ProjectRunConfigurationManager.RunConfigurationStateSplitter.class)
48 public class ProjectRunConfigurationManager implements ProjectComponent, PersistentStateComponent<Element> {
49 private static final Logger LOG = Logger.getInstance("#com.intellij.execution.impl.ProjectRunConfigurationManager");
51 private final RunManagerImpl myManager;
52 private List<Element> myUnloadedElements = null;
54 public ProjectRunConfigurationManager(final RunManagerImpl manager) {
55 myManager = manager;
58 public void projectOpened() {
61 public void projectClosed() {
64 @NotNull
65 @NonNls
66 public String getComponentName() {
67 return "ProjectRunConfigurationManager";
70 public void initComponent() {
74 public void disposeComponent() {
78 public Element getState() {
79 try {
80 final Element e = new Element("state");
81 writeExternal(e);
82 return e;
84 catch (WriteExternalException e1) {
85 LOG.error(e1);
86 return null;
90 public void loadState(Element state) {
91 try {
92 readExternal(state);
94 catch (InvalidDataException e) {
95 LOG.error(e);
99 public void readExternal(Element element) throws InvalidDataException {
100 myUnloadedElements = null;
101 final Set<String> existing = new HashSet<String>();
103 final List children = element.getChildren();
104 for (final Object child : children) {
105 final RunnerAndConfigurationSettingsImpl configuration = myManager.loadConfiguration((Element)child, true);
106 if (configuration == null && Comparing.strEqual(element.getName(), RunManagerImpl.CONFIGURATION)) {
107 if (myUnloadedElements == null) myUnloadedElements = new ArrayList<Element>(2);
108 myUnloadedElements.add(element);
111 if (configuration != null) {
112 existing.add(RunManagerImpl.getUniqueName(configuration.getConfiguration()));
116 myManager.removeNotExistingSharedConfigurations(existing);
119 public void writeExternal(Element element) throws WriteExternalException {
120 final Collection<RunnerAndConfigurationSettingsImpl> configurations = myManager.getStableConfigurations().values();
121 for (RunnerAndConfigurationSettingsImpl configuration : configurations) {
122 if (myManager.isConfigurationShared(configuration)){
123 myManager.addConfigurationElement(element, configuration);
126 if (myUnloadedElements != null) {
127 for (Element unloadedElement : myUnloadedElements) {
128 element.addContent((Element)unloadedElement.clone());
133 public static class RunConfigurationStateSplitter implements StateSplitter {
134 public List<Pair<Element, String>> splitState(Element e) {
135 final UniqueNameGenerator generator = new UniqueNameGenerator();
137 List<Pair<Element, String>> result = new ArrayList<Pair<Element, String>>();
139 final List list = e.getChildren();
140 for (final Object o : list) {
141 Element library = (Element)o;
142 final String name = generator.generateUniqueName(FileUtil.sanitizeFileName(library.getAttributeValue(RunManagerImpl.NAME_ATTR))) + ".xml";
143 result.add(new Pair<Element, String>(library, name));
146 return result;
149 public void mergeStatesInto(Element target, Element[] elements) {
150 for (Element e : elements) {
151 target.addContent(e);