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
.application
.options
.codeStyle
;
19 import com
.intellij
.openapi
.application
.ApplicationBundle
;
20 import com
.intellij
.openapi
.ui
.DetailsComponent
;
21 import com
.intellij
.psi
.codeStyle
.CodeStyleScheme
;
22 import com
.intellij
.util
.Alarm
;
23 import org
.jetbrains
.annotations
.NonNls
;
27 import java
.util
.Collection
;
28 import java
.util
.HashMap
;
31 public class CodeStyleMainPanel
extends JPanel
{
32 private final CardLayout myLayout
= new CardLayout();
33 private final JPanel mySettingsPanel
= new JPanel(myLayout
);
35 private final Map
<String
, NewCodeStyleSettingsPanel
> mySettingsPanels
= new HashMap
<String
, NewCodeStyleSettingsPanel
>();
37 private final Alarm myAlarm
= new Alarm(Alarm
.ThreadToUse
.SHARED_THREAD
);
38 private final CodeStyleSchemesModel myModel
;
39 private final CodeStyleSettingsPanelFactory myFactory
;
42 private static final String WAIT_CARD
= "CodeStyleSchemesConfigurable.$$$.Wait.placeholder.$$$";
45 public CodeStyleMainPanel(CodeStyleSchemesModel model
, CodeStyleSettingsPanelFactory factory
) {
46 super(new BorderLayout());
50 final CodeStyleSchemesPanel schemesPanel
= new CodeStyleSchemesPanel(model
);
52 model
.addListener(new CodeStyleSettingsListener(){
53 public void currentSchemeChanged(final Object source
) {
54 if (source
!= schemesPanel
) {
55 schemesPanel
.onSelectedSchemeChanged();
57 onCurrentSchemeChanged();
60 public void schemeListChanged() {
61 schemesPanel
.resetSchemesCombo();
64 public void currentSettingsChanged() {
65 ensureCurrentPanel().onSomethingChanged();
68 public void usePerProjectSettingsOptionChanged() {
69 schemesPanel
.usePerProjectSettingsOptionChanged();
72 public void schemeChanged(final CodeStyleScheme scheme
) {
73 ensurePanel(scheme
).resetFromClone();
79 add(schemesPanel
.getPanel(), BorderLayout
.NORTH
);
81 DetailsComponent detComp
= new DetailsComponent();
82 detComp
.setPaintBorder(false);
83 detComp
.setContent(mySettingsPanel
);
84 detComp
.setText(getDisplayName());
86 add(detComp
.getComponent(), BorderLayout
.CENTER
);
88 schemesPanel
.resetSchemesCombo();
89 schemesPanel
.onSelectedSchemeChanged();
90 onCurrentSchemeChanged();
94 private void addWaitCard() {
95 JPanel waitPanel
= new JPanel(new BorderLayout());
96 JLabel label
= new JLabel(ApplicationBundle
.message("label.loading.page.please.wait"));
97 label
.setHorizontalAlignment(SwingConstants
.CENTER
);
98 waitPanel
.add(label
, BorderLayout
.CENTER
);
99 label
.setCursor(Cursor
.getPredefinedCursor(Cursor
.WAIT_CURSOR
));
100 waitPanel
.setCursor(Cursor
.getPredefinedCursor(Cursor
.WAIT_CURSOR
));
101 mySettingsPanel
.add(WAIT_CARD
, waitPanel
);
104 public void onCurrentSchemeChanged() {
105 myLayout
.show(mySettingsPanel
, WAIT_CARD
);
107 myAlarm
.cancelAllRequests();
108 final Runnable request
= new Runnable() {
110 SwingUtilities
.invokeLater(new Runnable() {
112 ensureCurrentPanel().onSomethingChanged();
113 myLayout
.show(mySettingsPanel
, myModel
.getSelectedScheme().getName());
118 myAlarm
.addRequest(request
, 200);
121 public NewCodeStyleSettingsPanel
[] getPanels() {
122 final Collection
<NewCodeStyleSettingsPanel
> panels
= mySettingsPanels
.values();
123 return panels
.toArray(new NewCodeStyleSettingsPanel
[panels
.size()]);
126 public boolean isModified() {
127 final NewCodeStyleSettingsPanel
[] panels
= getPanels();
128 for (NewCodeStyleSettingsPanel panel
: panels
) {
129 if (panel
.isModified()) return true;
134 public void reset() {
136 onCurrentSchemeChanged();
139 private void clearPanels() {
140 for (NewCodeStyleSettingsPanel panel
: mySettingsPanels
.values()) {
143 mySettingsPanels
.clear();
146 public void apply() {
147 final NewCodeStyleSettingsPanel
[] panels
= getPanels();
148 for (NewCodeStyleSettingsPanel panel
: panels
) {
149 if (panel
.isModified()) panel
.apply();
154 public String
getHelpTopic() {
155 NewCodeStyleSettingsPanel selectedPanel
= ensureCurrentPanel();
156 if (selectedPanel
== null) {
157 return "reference.settingsdialog.IDE.globalcodestyle";
159 return selectedPanel
.getHelpTopic();
162 private NewCodeStyleSettingsPanel
ensureCurrentPanel() {
163 return ensurePanel(myModel
.getSelectedScheme());
166 private NewCodeStyleSettingsPanel
ensurePanel(final CodeStyleScheme scheme
) {
167 String name
= scheme
.getName();
168 if (!mySettingsPanels
.containsKey(name
)) {
169 NewCodeStyleSettingsPanel panel
= myFactory
.createPanel(scheme
);
171 panel
.setModel(myModel
);
172 mySettingsPanels
.put(name
, panel
);
173 mySettingsPanel
.add(scheme
.getName(), panel
);
176 return mySettingsPanels
.get(name
);
179 public String
getDisplayName() {
180 return ensureCurrentPanel().getDisplayName();
183 public void disposeUIResources() {
184 myAlarm
.cancelAllRequests();
188 public boolean isModified(final CodeStyleScheme scheme
) {
189 if (!mySettingsPanels
.containsKey(scheme
.getName())) {
193 return mySettingsPanels
.get(scheme
.getName()).isModified();