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
.maddyhome
.idea
.copyright
;
19 import com
.intellij
.openapi
.application
.ApplicationManager
;
20 import com
.intellij
.openapi
.components
.*;
21 import com
.intellij
.openapi
.diagnostic
.Logger
;
22 import com
.intellij
.openapi
.fileEditor
.FileEditorManager
;
23 import com
.intellij
.openapi
.fileEditor
.FileEditorManagerAdapter
;
24 import com
.intellij
.openapi
.fileEditor
.FileEditorManagerListener
;
25 import com
.intellij
.openapi
.module
.Module
;
26 import com
.intellij
.openapi
.project
.Project
;
27 import com
.intellij
.openapi
.roots
.ProjectRootManager
;
28 import com
.intellij
.openapi
.util
.*;
29 import com
.intellij
.openapi
.util
.io
.FileUtil
;
30 import com
.intellij
.openapi
.vfs
.VirtualFile
;
31 import com
.intellij
.packageDependencies
.DependencyValidationManager
;
32 import com
.intellij
.psi
.PsiFile
;
33 import com
.intellij
.psi
.PsiManager
;
34 import com
.intellij
.psi
.search
.scope
.packageSet
.NamedScope
;
35 import com
.intellij
.psi
.search
.scope
.packageSet
.PackageSet
;
36 import com
.intellij
.util
.containers
.HashMap
;
37 import com
.intellij
.util
.text
.UniqueNameGenerator
;
38 import com
.maddyhome
.idea
.copyright
.actions
.UpdateCopyrightProcessor
;
39 import com
.maddyhome
.idea
.copyright
.options
.LanguageOptions
;
40 import com
.maddyhome
.idea
.copyright
.options
.Options
;
41 import com
.maddyhome
.idea
.copyright
.util
.FileTypeUtil
;
42 import com
.maddyhome
.idea
.copyright
.util
.NewFileTracker
;
43 import org
.jdom
.Attribute
;
44 import org
.jdom
.Element
;
45 import org
.jetbrains
.annotations
.NonNls
;
46 import org
.jetbrains
.annotations
.NotNull
;
47 import org
.jetbrains
.annotations
.Nullable
;
52 name
= "CopyrightManager",
55 file
= "$PROJECT_FILE$"), @Storage(id
= "dir", file
= "$PROJECT_CONFIG_DIR$/copyright/", scheme
= StorageScheme
.DIRECTORY_BASED
,
56 stateSplitter
= CopyrightManager
.CopyrightStateSplitter
.class)})
57 public class CopyrightManager
extends AbstractProjectComponent
implements JDOMExternalizable
, PersistentStateComponent
<Element
> {
58 private static final Logger LOG
= Logger
.getInstance("#" + CopyrightManager
.class.getName());
60 private CopyrightProfile myDefaultCopyright
= null;
62 private final LinkedHashMap
<String
, String
> myModule2Copyrights
= new LinkedHashMap
<String
, String
>();
64 private final Map
<String
, CopyrightProfile
> myCopyrights
= new HashMap
<String
, CopyrightProfile
>();
66 private final Options myOptions
= new Options();
68 public CopyrightManager(Project project
) {
73 private static final String COPYRIGHT
= "copyright";
75 private static final String MODULE2COPYRIGHT
= "module2copyright";
77 private static final String ELEMENT
= "element";
79 private static final String MODULE
= "module";
81 private static final String DEFAULT
= "default";
83 public static CopyrightManager
getInstance(Project project
) {
84 return project
.getComponent(CopyrightManager
.class);
88 public void projectOpened() {
89 if (myProject
!= null) {
90 FileEditorManagerListener listener
= new FileEditorManagerAdapter() {
91 public void fileOpened(FileEditorManager fileEditorManager
, VirtualFile virtualFile
) {
92 if (virtualFile
.isWritable() && NewFileTracker
.getInstance().contains(virtualFile
)) {
93 NewFileTracker
.getInstance().remove(virtualFile
);
94 if (FileTypeUtil
.getInstance().isSupportedFile(virtualFile
)) {
95 final Module module
= ProjectRootManager
.getInstance(myProject
).getFileIndex().getModuleForFile(virtualFile
);
97 final PsiFile file
= PsiManager
.getInstance(myProject
).findFile(virtualFile
);
99 ApplicationManager
.getApplication().invokeLater(new Runnable() {
101 if (file
.isValid() && file
.isWritable()) {
102 new UpdateCopyrightProcessor(myProject
, module
, file
).run();
113 FileEditorManager
.getInstance(myProject
).addFileEditorManagerListener(listener
, myProject
);
119 public String
getComponentName() {
120 return "CopyrightManager";
123 public void readExternal(Element element
) throws InvalidDataException
{
125 final Element module2copyright
= element
.getChild(MODULE2COPYRIGHT
);
126 if (module2copyright
!= null) {
127 for (Object o
: module2copyright
.getChildren(ELEMENT
)) {
128 final Element el
= (Element
)o
;
129 final String moduleName
= el
.getAttributeValue(MODULE
);
130 final String copyrightName
= el
.getAttributeValue(COPYRIGHT
);
131 myModule2Copyrights
.put(moduleName
, copyrightName
);
134 for (Object o
: element
.getChildren(COPYRIGHT
)) {
135 final CopyrightProfile copyrightProfile
= new CopyrightProfile();
136 copyrightProfile
.readExternal((Element
)o
);
137 myCopyrights
.put(copyrightProfile
.getName(), copyrightProfile
);
139 myDefaultCopyright
= myCopyrights
.get(element
.getAttributeValue(DEFAULT
));
140 myOptions
.readExternal(element
);
143 public void writeExternal(Element element
) throws WriteExternalException
{
144 for (CopyrightProfile copyright
: myCopyrights
.values()) {
145 final Element copyrightElement
= new Element(COPYRIGHT
);
146 copyright
.writeExternal(copyrightElement
);
147 element
.addContent(copyrightElement
);
149 final Element map
= new Element(MODULE2COPYRIGHT
);
150 for (String moduleName
: myModule2Copyrights
.keySet()) {
151 final Element setting
= new Element(ELEMENT
);
152 setting
.setAttribute(MODULE
, moduleName
);
153 setting
.setAttribute(COPYRIGHT
, myModule2Copyrights
.get(moduleName
));
154 map
.addContent(setting
);
156 element
.addContent(map
);
157 element
.setAttribute(DEFAULT
, myDefaultCopyright
!= null ? myDefaultCopyright
.getName() : "");
158 myOptions
.writeExternal(element
);
162 public Element
getState() {
164 final Element e
= new Element("settings");
168 catch (WriteExternalException e1
) {
174 public void loadState(Element state
) {
178 catch (InvalidDataException e
) {
183 public Map
<String
, String
> getCopyrightsMapping() {
184 return myModule2Copyrights
;
187 public void setDefaultCopyright(@Nullable CopyrightProfile copyright
) {
188 myDefaultCopyright
= copyright
;
192 public CopyrightProfile
getDefaultCopyright() {
193 return myDefaultCopyright
;
196 public void addCopyright(CopyrightProfile copyrightProfile
) {
197 myCopyrights
.put(copyrightProfile
.getName(), copyrightProfile
);
200 public void removeCopyright(CopyrightProfile copyrightProfile
) {
201 myCopyrights
.values().remove(copyrightProfile
);
202 for (Iterator
<String
> it
= myModule2Copyrights
.keySet().iterator(); it
.hasNext();) {
203 final String profileName
= myModule2Copyrights
.get(it
.next());
204 if (profileName
.equals(copyrightProfile
.getName())) {
210 public void clearCopyrights() {
211 myDefaultCopyright
= null;
212 myCopyrights
.clear();
213 myModule2Copyrights
.clear();
216 public void mapCopyright(String scopeName
, String copyrightProfileName
) {
217 myModule2Copyrights
.put(scopeName
, copyrightProfileName
);
220 public void unmapCopyright(String scopeName
) {
221 myModule2Copyrights
.remove(scopeName
);
224 public Collection
<CopyrightProfile
> getCopyrights() {
225 return myCopyrights
.values();
229 public CopyrightProfile
getCopyrightOptions(@NotNull PsiFile file
) {
230 if (myOptions
.getOptions(file
.getFileType().getName()).getFileTypeOverride() == LanguageOptions
.NO_COPYRIGHT
) return null;
231 final DependencyValidationManager validationManager
= DependencyValidationManager
.getInstance(myProject
);
232 for (String scopeName
: myModule2Copyrights
.keySet()) {
233 final NamedScope namedScope
= validationManager
.getScope(scopeName
);
234 if (namedScope
!= null) {
235 final PackageSet packageSet
= namedScope
.getValue();
236 if (packageSet
!= null) {
237 if (packageSet
.contains(file
, validationManager
)) {
238 final CopyrightProfile profile
= myCopyrights
.get(myModule2Copyrights
.get(scopeName
));
239 if (profile
!= null) {
246 return myDefaultCopyright
!= null ? myDefaultCopyright
: null;
249 public Options
getOptions() {
253 public void replaceCopyright(String displayName
, CopyrightProfile copyrightProfile
) {
254 if (myDefaultCopyright
!= null && Comparing
.strEqual(myDefaultCopyright
.getName(), displayName
)) {
255 myDefaultCopyright
= copyrightProfile
;
257 myCopyrights
.remove(displayName
);
258 addCopyright(copyrightProfile
);
261 public static class CopyrightStateSplitter
implements StateSplitter
{
262 public List
<Pair
<Element
, String
>> splitState(Element e
) {
263 final UniqueNameGenerator generator
= new UniqueNameGenerator();
264 final List
<Pair
<Element
, String
>> result
= new ArrayList
<Pair
<Element
, String
>>();
266 final Element
[] elements
= JDOMUtil
.getElements(e
);
267 for (Element element
: elements
) {
268 if (element
.getName().equals("copyright")) {
271 String profileName
= null;
272 final Element
[] options
= JDOMUtil
.getElements(element
);
273 for (Element option
: options
) {
274 if (option
.getName().equals("option") && option
.getAttributeValue("name").equals("myName")) {
275 profileName
= option
.getAttributeValue("value");
279 assert profileName
!= null;
281 final String name
= generator
.generateUniqueName(FileUtil
.sanitizeFileName(profileName
)) + ".xml";
282 result
.add(new Pair
<Element
, String
>(element
, name
));
285 result
.add(new Pair
<Element
, String
>(e
, generator
.generateUniqueName("profiles_settings") + ".xml"));
289 public void mergeStatesInto(Element target
, Element
[] elements
) {
290 for (Element element
: elements
) {
291 if (element
.getName().equals("copyright")) {
293 target
.addContent(element
);
296 final Element
[] states
= JDOMUtil
.getElements(element
);
297 for (Element state
: states
) {
299 target
.addContent(state
);
301 for (Object attr
: element
.getAttributes()) {
302 target
.setAttribute((Attribute
)((Attribute
)attr
).clone());