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
.actionSystem
.ex
;
18 import com
.intellij
.ide
.IdeBundle
;
19 import com
.intellij
.ide
.actions
.QuickSwitchSchemeAction
;
20 import com
.intellij
.openapi
.actionSystem
.ActionManager
;
21 import com
.intellij
.openapi
.actionSystem
.AnAction
;
22 import com
.intellij
.openapi
.actionSystem
.DataContext
;
23 import com
.intellij
.openapi
.actionSystem
.DefaultActionGroup
;
24 import com
.intellij
.openapi
.actionSystem
.impl
.BundledQuickListsProvider
;
25 import com
.intellij
.openapi
.application
.ApplicationManager
;
26 import com
.intellij
.openapi
.application
.PathManager
;
27 import com
.intellij
.openapi
.application
.ex
.DecodeDefaultsUtil
;
28 import com
.intellij
.openapi
.components
.ExportableApplicationComponent
;
29 import com
.intellij
.openapi
.components
.RoamingType
;
30 import com
.intellij
.openapi
.diagnostic
.Logger
;
31 import com
.intellij
.openapi
.options
.SchemeProcessor
;
32 import com
.intellij
.openapi
.options
.SchemesManager
;
33 import com
.intellij
.openapi
.options
.SchemesManagerFactory
;
34 import com
.intellij
.openapi
.options
.Scheme
;
35 import com
.intellij
.openapi
.project
.Project
;
36 import com
.intellij
.openapi
.util
.InvalidDataException
;
37 import com
.intellij
.openapi
.util
.JDOMUtil
;
38 import com
.intellij
.openapi
.util
.NamedJDOMExternalizable
;
39 import com
.intellij
.openapi
.util
.WriteExternalException
;
40 import org
.jdom
.Document
;
41 import org
.jdom
.Element
;
42 import org
.jdom
.JDOMException
;
43 import org
.jetbrains
.annotations
.NonNls
;
44 import org
.jetbrains
.annotations
.NotNull
;
47 import java
.io
.IOException
;
48 import java
.io
.InputStream
;
49 import java
.util
.Collection
;
50 import java
.util
.HashSet
;
56 public class QuickListsManager
implements ExportableApplicationComponent
, NamedJDOMExternalizable
{
57 @NonNls private static final String LIST_TAG
= "list";
58 private final ActionManager myActionManager
;
59 private final SchemesManager
<QuickList
, QuickList
> mySchemesManager
;
61 private final static Logger LOG
= Logger
.getInstance("#" + QuickListsManager
.class.getName());
63 public static QuickListsManager
getInstance() {
64 return ApplicationManager
.getApplication().getComponent(QuickListsManager
.class);
67 public QuickListsManager(ActionManagerEx actionManagerEx
, SchemesManagerFactory schemesManagerFactory
) {
68 myActionManager
= actionManagerEx
;
69 mySchemesManager
= schemesManagerFactory
.createSchemesManager(
70 "$ROOT_CONFIG$/quicklists",
71 new SchemeProcessor
<QuickList
>(){
72 public QuickList
readScheme(final Document schemeContent
) throws InvalidDataException
, IOException
, JDOMException
{
73 return loadListFromDocument(schemeContent
);
76 public Document
writeScheme(final QuickList scheme
) throws WriteExternalException
{
77 Element element
= new Element(LIST_TAG
);
78 scheme
.writeExternal(element
);
79 return new Document(element
);
82 public boolean shouldBeSaved(final QuickList scheme
) {
86 public void initScheme(final QuickList scheme
) {
90 public void onSchemeAdded(final QuickList scheme
) {
93 public void onSchemeDeleted(final QuickList scheme
) {
96 public void onCurrentSchemeChanged(final Scheme newCurrentScheme
) {
100 RoamingType
.PER_USER
);
102 loadAdditionalDefaultSchemes();
107 private QuickList
loadListFromDocument(Document schemeContent
) {
108 QuickList list
= new QuickList();
109 list
.readExternal(schemeContent
.getRootElement());
114 public String
getComponentName() {
115 return "QuickListsManager";
119 public File
[] getExportFiles() {
120 return new File
[]{PathManager
.getOptionsFile(this)};
124 public String
getPresentableName() {
125 return IdeBundle
.message("quick.lists.presentable.name");
128 public void initComponent() {
129 mySchemesManager
.loadSchemes();
133 public void disposeComponent() {
136 public String
getExternalFileName() {
140 public void readExternal(Element element
) throws InvalidDataException
{
141 for (Object group
: element
.getChildren(LIST_TAG
)) {
142 Element groupElement
= (Element
)group
;
143 QuickList list
= new QuickList();
144 list
.readExternal(groupElement
);
145 mySchemesManager
.addNewScheme(list
, true);
147 mySchemesManager
.loadSchemes();
151 public void writeExternal(Element element
) throws WriteExternalException
{
155 public QuickList
[] getAllQuickLists() {
156 Collection
<QuickList
> lists
= mySchemesManager
.getAllSchemes();
157 return lists
.toArray(new QuickList
[lists
.size()]);
160 public void removeAllQuickLists() {
161 mySchemesManager
.clearAllSchemes();
164 public void registerActions() {
166 HashSet
<String
> registeredIds
= new HashSet
<String
>(); // to prevent exception if 2 or more targets have the same name
168 ActionManager actionManager
= myActionManager
;
169 for (QuickList list
: mySchemesManager
.getAllSchemes()) {
170 String actionId
= list
.getActionId();
172 if (!registeredIds
.contains(actionId
)) {
173 registeredIds
.add(actionId
);
174 actionManager
.registerAction(actionId
, new InvokeQuickListAction(list
));
179 private void unregisterActions() {
180 ActionManagerEx actionManager
= (ActionManagerEx
)myActionManager
;
182 for (String oldId
: actionManager
.getActionIds(QuickList
.QUICK_LIST_PREFIX
)) {
183 actionManager
.unregisterAction(oldId
);
187 public void registerQuickList(final QuickList quickList
) {
188 mySchemesManager
.addNewScheme(quickList
, true);
191 public SchemesManager
<QuickList
, QuickList
> getSchemesManager() {
192 return mySchemesManager
;
196 private void loadAdditionalDefaultSchemes() {
197 //Get color schemes from EPs
198 for (BundledQuickListsProvider provider
: BundledQuickListsProvider
.EP_NAME
.getExtensions()) {
199 final String
[] paths
= provider
.getBundledListsRelativePaths();
201 for (final String path
: paths
) {
203 final InputStream inputStream
= DecodeDefaultsUtil
.getDefaultsInputStream(provider
, path
);
204 if (inputStream
== null) {
205 // Error shouldn't occur during this operation
206 // thus we report error instead of info
207 LOG
.error("Cannot read quick list from " + path
);
211 final Document document
;
213 document
= JDOMUtil
.loadDocument(inputStream
);
215 catch (JDOMException e
) {
216 LOG
.info("Error reading quick list from " + path
+ ": " + e
.getLocalizedMessage());
219 final QuickList scheme
= loadListFromDocument(document
);
220 mySchemesManager
.addNewScheme(scheme
, false);
222 catch (final Exception e
) {
223 ApplicationManager
.getApplication().invokeLater(
226 // Error shouldn't occur during this operation
227 // thus we report error instead of info
228 LOG
.error("Cannot read quick list from " + path
+ ": " + e
.getLocalizedMessage(), e
);
237 private static class InvokeQuickListAction
extends QuickSwitchSchemeAction
{
238 private final QuickList myQuickList
;
240 public InvokeQuickListAction(QuickList quickList
) {
241 myQuickList
= quickList
;
242 getTemplatePresentation().setDescription(myQuickList
.getDescription());
243 getTemplatePresentation().setText(myQuickList
.getDisplayName(), false);
246 protected void fillActions(Project project
, DefaultActionGroup group
, DataContext dataContext
) {
247 ActionManager actionManager
= ActionManagerEx
.getInstance();
248 for (String actionId
: myQuickList
.getActionIds()) {
249 if (QuickList
.SEPARATOR_ID
.equals(actionId
)) {
250 group
.addSeparator();
253 AnAction action
= actionManager
.getAction(actionId
);
254 if (action
!= null) {
261 protected boolean isEnabled() {