update copyrights
[idea/community.git] / platform / lang-impl / src / com / intellij / application / options / editor / CodeFoldingConfigurable.java
1 /*
2  * Copyright 2000-2009 JetBrains s.r.o.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package com.intellij.application.options.editor;
18
19 import com.intellij.codeInsight.folding.CodeFoldingManager;
20 import com.intellij.openapi.application.ApplicationBundle;
21 import com.intellij.openapi.application.ApplicationManager;
22 import com.intellij.openapi.application.ModalityState;
23 import com.intellij.openapi.editor.Editor;
24 import com.intellij.openapi.editor.EditorFactory;
25 import com.intellij.openapi.editor.ex.EditorSettingsExternalizable;
26 import com.intellij.openapi.extensions.Extensions;
27 import com.intellij.openapi.options.CompositeConfigurable;
28 import com.intellij.openapi.options.ConfigurationException;
29 import com.intellij.openapi.project.Project;
30 import org.jetbrains.annotations.Nls;
31
32 import javax.swing.*;
33 import java.awt.*;
34 import java.util.Arrays;
35 import java.util.List;
36
37 /**
38  * @author yole
39  */
40 public class CodeFoldingConfigurable extends CompositeConfigurable<CodeFoldingOptionsProvider> implements EditorOptionsProvider {
41   private JCheckBox myCbFolding;
42   private JPanel myRootPanel;
43   private JPanel myFoldingPanel;
44
45   @Nls
46   public String getDisplayName() {
47     return ApplicationBundle.message("group.code.folding");
48   }
49
50   public Icon getIcon() {
51     return null;
52   }
53
54   public String getHelpTopic() {
55     return "reference.settingsdialog.IDE.editor.code.folding";
56   }
57
58   public JComponent createComponent() {
59     for (CodeFoldingOptionsProvider provider : getConfigurables()) {
60       myFoldingPanel.add(provider.createComponent(), new GridBagConstraints(0, GridBagConstraints.RELATIVE, 1, 1, 0, 0, GridBagConstraints.NORTHWEST,
61                                                                             GridBagConstraints.NONE, new Insets(0,0,0,0), 0,0));
62     }
63     return myRootPanel;
64   }
65
66   public boolean isModified() {
67     return myCbFolding.isSelected() != EditorSettingsExternalizable.getInstance().isFoldingOutlineShown() ||
68            super.isModified();
69   }
70
71   public void apply() throws ConfigurationException {
72     EditorSettingsExternalizable.getInstance().setFoldingOutlineShown(myCbFolding.isSelected());
73     super.apply();
74
75     for (final Editor editor : EditorFactory.getInstance().getAllEditors()) {
76       final Project project = editor.getProject();
77       if (project != null && !project.isDefault()) {
78         final CodeFoldingManager foldingManager = CodeFoldingManager.getInstance(project);
79         if (foldingManager != null) {
80           ApplicationManager.getApplication().invokeLater(new Runnable() {
81             public void run() {
82               foldingManager.forceDefaultState(editor);
83             }
84           }, ModalityState.NON_MODAL);
85         }
86       }
87     }
88   }
89
90   public void reset() {
91     myCbFolding.setSelected(EditorSettingsExternalizable.getInstance().isFoldingOutlineShown());
92     super.reset();
93   }
94
95   protected List<CodeFoldingOptionsProvider> createConfigurables() {
96     return Arrays.asList(Extensions.getExtensions(CodeFoldingOptionsProvider.EP_NAME));
97   }
98
99   public String getId() {
100     return "editor.preferences.folding";
101   }
102
103   public Runnable enableSearch(final String option) {
104     return null;
105   }
106 }