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
.options
.ex
;
18 import com
.intellij
.CommonBundle
;
19 import com
.intellij
.openapi
.application
.ApplicationManager
;
20 import com
.intellij
.openapi
.application
.ModalityState
;
21 import com
.intellij
.openapi
.diagnostic
.Logger
;
22 import com
.intellij
.openapi
.help
.HelpManager
;
23 import com
.intellij
.openapi
.options
.BaseConfigurable
;
24 import com
.intellij
.openapi
.options
.Configurable
;
25 import com
.intellij
.openapi
.options
.ConfigurationException
;
26 import com
.intellij
.openapi
.project
.Project
;
27 import com
.intellij
.openapi
.ui
.DialogWrapper
;
28 import com
.intellij
.openapi
.ui
.Messages
;
29 import com
.intellij
.openapi
.wm
.ex
.IdeFocusTraversalPolicy
;
30 import com
.intellij
.util
.Alarm
;
31 import org
.jetbrains
.annotations
.NonNls
;
35 import java
.awt
.event
.ActionEvent
;
37 public class SingleConfigurableEditor
extends DialogWrapper
{
38 private static final Logger LOG
= Logger
.getInstance("#com.intellij.openapi.options.ex.SingleConfigurableEditor");
39 private Project myProject
;
40 private Component myParentComponent
;
41 private Configurable myConfigurable
;
42 private JComponent myCenterPanel
;
43 private String myDimensionKey
;
44 private boolean myChangesWereApplied
;
46 public SingleConfigurableEditor(Project project
, Configurable configurable
, @NonNls String dimensionKey
) {
48 myDimensionKey
= dimensionKey
;
49 setTitle(createTitleString(configurable
));
52 myConfigurable
= configurable
;
54 myConfigurable
.reset();
57 public SingleConfigurableEditor(Component parent
, Configurable configurable
, String dimensionServiceKey
) {
59 myDimensionKey
= dimensionServiceKey
;
60 setTitle(createTitleString(configurable
));
62 myParentComponent
= parent
;
63 myConfigurable
= configurable
;
65 myConfigurable
.reset();
68 public Configurable
getConfigurable() {
69 return myConfigurable
;
72 public Project
getProject() {
76 public SingleConfigurableEditor(Project project
, Configurable configurable
) {
77 this(project
, configurable
, null);
80 public SingleConfigurableEditor(Component parent
, Configurable configurable
) {
81 this(parent
, configurable
, null);
84 private static String
createTitleString(Configurable configurable
) {
85 String displayName
= configurable
.getDisplayName();
86 LOG
.assertTrue(displayName
!= null, configurable
.getClass().getName());
87 return displayName
.replaceAll("\n", " ");
90 protected String
getDimensionServiceKey() {
91 if (myDimensionKey
== null) {
92 return super.getDimensionServiceKey();
95 return myDimensionKey
;
99 protected Action
[] createActions() {
100 if (myConfigurable
.getHelpTopic() != null) {
101 return new Action
[]{getOKAction(), getCancelAction(), new ApplyAction(), getHelpAction()};
104 return new Action
[]{getOKAction(), getCancelAction(), new ApplyAction()};
108 protected void doHelpAction() {
109 HelpManager
.getInstance().invokeHelp(myConfigurable
.getHelpTopic());
113 public void doCancelAction() {
114 if (myChangesWereApplied
) {
115 ApplicationManager
.getApplication().saveAll();
117 super.doCancelAction();
120 protected void doOKAction() {
122 if (myConfigurable
.isModified()) myConfigurable
.apply();
124 ApplicationManager
.getApplication().saveAll();
126 catch (ConfigurationException e
) {
127 if (e
.getMessage() != null) {
128 if (myProject
!= null) {
129 Messages
.showMessageDialog(myProject
, e
.getMessage(), e
.getTitle(), Messages
.getErrorIcon());
132 Messages
.showMessageDialog(myParentComponent
, e
.getMessage(), e
.getTitle(), Messages
.getErrorIcon());
140 protected static String
createDimensionKey(Configurable configurable
) {
141 String displayName
= configurable
.getDisplayName();
142 displayName
= displayName
.replaceAll("\n", "_").replaceAll(" ", "_");
143 return "#" + displayName
;
146 protected class ApplyAction
extends AbstractAction
{
147 private final Alarm myUpdateAlarm
= new Alarm(Alarm
.ThreadToUse
.SWING_THREAD
);
149 public ApplyAction() {
150 super(CommonBundle
.getApplyButtonText());
151 final Runnable updateRequest
= new Runnable() {
153 if (!SingleConfigurableEditor
.this.isShowing()) return;
154 ApplyAction
.this.setEnabled(myConfigurable
!= null && myConfigurable
.isModified());
155 addUpdateRequest(this);
159 // invokeLater necessary to make sure dialog is already shown so we calculate modality state correctly.
160 SwingUtilities
.invokeLater(new Runnable() {
162 addUpdateRequest(updateRequest
);
167 private void addUpdateRequest(final Runnable updateRequest
) {
168 myUpdateAlarm
.addRequest(updateRequest
, 500, ModalityState
.stateForComponent(getWindow()));
171 public void actionPerformed(ActionEvent event
) {
172 if (myPerformAction
) return;
174 myPerformAction
= true;
175 if (myConfigurable
.isModified()) {
176 myConfigurable
.apply();
177 myChangesWereApplied
= true;
178 setCancelButtonText(CommonBundle
.getCloseButtonText());
181 catch (ConfigurationException e
) {
182 if (myProject
!= null) {
183 Messages
.showMessageDialog(myProject
, e
.getMessage(), e
.getTitle(), Messages
.getErrorIcon());
186 Messages
.showMessageDialog(myParentComponent
, e
.getMessage(), e
.getTitle(),
187 Messages
.getErrorIcon());
190 myPerformAction
= false;
195 protected JComponent
createCenterPanel() {
196 myCenterPanel
= myConfigurable
.createComponent();
197 return myCenterPanel
;
200 public JComponent
getPreferredFocusedComponent() {
201 if (myConfigurable
instanceof BaseConfigurable
) {
202 JComponent preferred
= ((BaseConfigurable
)myConfigurable
).getPreferredFocusedComponent();
203 if (preferred
!= null) return preferred
;
205 return IdeFocusTraversalPolicy
.getPreferredFocusedComponent(myCenterPanel
);
208 public void dispose() {
210 myConfigurable
.disposeUIResources();
211 myConfigurable
= null;