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.facet.impl;
19 import com.intellij.facet.*;
20 import com.intellij.openapi.application.ApplicationManager;
21 import com.intellij.openapi.application.Result;
22 import com.intellij.openapi.application.WriteAction;
23 import com.intellij.openapi.components.PersistentStateComponent;
24 import com.intellij.openapi.module.Module;
25 import com.intellij.openapi.util.InvalidDataException;
26 import com.intellij.openapi.util.WriteExternalException;
27 import com.intellij.util.ReflectionUtil;
28 import com.intellij.util.xmlb.SkipDefaultValuesSerializationFilters;
29 import com.intellij.util.xmlb.XmlSerializer;
30 import org.jdom.Element;
31 import org.jetbrains.annotations.NotNull;
32 import org.jetbrains.annotations.Nullable;
34 import java.lang.reflect.TypeVariable;
35 import java.util.Arrays;
40 public class FacetUtil {
42 public static <F extends Facet> F addFacet(Module module, FacetType<F, ?> type) {
43 final ModifiableFacetModel model = FacetManager.getInstance(module).createModifiableModel();
44 final F facet = createFacet(module, type);
45 ApplicationManager.getApplication().runWriteAction(new Runnable() {
47 model.addFacet(facet);
54 private static <F extends Facet, C extends FacetConfiguration> F createFacet(final Module module, final FacetType<F, C> type) {
55 return FacetManager.getInstance(module).createFacet(type, type.getPresentableName(), type.createDefaultConfiguration(), null);
58 public static void deleteFacet(final Facet facet) {
60 protected void run(final Result result) {
61 if (!isRegistered(facet)) {
65 ModifiableFacetModel model = FacetManager.getInstance(facet.getModule()).createModifiableModel();
66 model.removeFacet(facet);
72 public static boolean isRegistered(Facet facet) {
73 return Arrays.asList(FacetManager.getInstance(facet.getModule()).getAllFacets()).contains(facet);
76 public static void loadFacetConfiguration(final @NotNull FacetConfiguration configuration, final @Nullable Element config)
77 throws InvalidDataException {
79 if (configuration instanceof PersistentStateComponent) {
80 TypeVariable<Class<PersistentStateComponent>> variable = PersistentStateComponent.class.getTypeParameters()[0];
81 Class<?> stateClass = ReflectionUtil.getRawType(ReflectionUtil.resolveVariableInHierarchy(variable, configuration.getClass()));
82 ((PersistentStateComponent)configuration).loadState(XmlSerializer.deserialize(config, stateClass));
85 configuration.readExternal(config);
90 public static Element saveFacetConfiguration(final FacetConfiguration configuration) throws WriteExternalException {
91 if (configuration instanceof PersistentStateComponent) {
92 Object state = ((PersistentStateComponent)configuration).getState();
93 if (state instanceof Element) return ((Element)state);
94 return XmlSerializer.serialize(state, new SkipDefaultValuesSerializationFilters());
97 final Element config = new Element(FacetManagerImpl.CONFIGURATION_ELEMENT);
98 configuration.writeExternal(config);