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
;
18 import com
.intellij
.openapi
.Disposable
;
19 import com
.intellij
.openapi
.util
.Disposer
;
20 import com
.intellij
.openapi
.util
.Factory
;
21 import com
.intellij
.ui
.UserActivityListener
;
22 import com
.intellij
.ui
.UserActivityWatcher
;
23 import org
.jetbrains
.annotations
.NotNull
;
26 import java
.util
.ArrayList
;
27 import java
.util
.List
;
30 * This class presents an abstraction of user interface transactional editor provider of some abstract data type.
31 * {@link #getComponent()} should be called before {@link #resetFrom(Object)}
33 public abstract class SettingsEditor
<Settings
> implements Disposable
{
34 private List
<SettingsEditorListener
<Settings
>> myListeners
;
35 private UserActivityWatcher myWatcher
;
36 private boolean myIsInUpdate
= false;
37 private Factory
<Settings
> mySettingsFactory
;
38 private CompositeSettingsEditor
<Settings
> myOwner
;
39 private JComponent myEditorComponent
;
41 protected abstract void resetEditorFrom(Settings s
);
42 protected abstract void applyEditorTo(Settings s
) throws ConfigurationException
;
45 protected abstract JComponent
createEditor();
46 protected abstract void disposeEditor();
48 public SettingsEditor() {
52 public SettingsEditor(Factory
<Settings
> settingsFactory
) {
53 mySettingsFactory
= settingsFactory
;
54 Disposer
.register(this, new Disposable() {
55 public void dispose() {
62 public Settings
getSnapshot() throws ConfigurationException
{
63 if (myOwner
!= null) return myOwner
.getSnapshot();
65 Settings settings
= mySettingsFactory
.create();
70 final void setOwner(CompositeSettingsEditor
<Settings
> owner
) {
74 public final CompositeSettingsEditor
<Settings
> getOwner() {
78 public Factory
<Settings
> getFactory() {
79 return mySettingsFactory
;
82 public final void resetFrom(Settings s
) {
92 public final void applyTo(Settings s
) throws ConfigurationException
{
96 public final JComponent
getComponent() {
97 if (myEditorComponent
== null) {
98 myEditorComponent
= createEditor();
99 installWatcher(myEditorComponent
);
101 return myEditorComponent
;
104 public final void dispose() {
107 protected void uninstallWatcher() {
111 protected void installWatcher(JComponent c
) {
112 myWatcher
= new UserActivityWatcher();
113 myWatcher
.register(c
);
114 UserActivityListener userActivityListener
= new UserActivityListener() {
115 public void stateChanged() {
116 fireEditorStateChanged();
119 myWatcher
.addUserActivityListener(userActivityListener
, this);
122 public final void addSettingsEditorListener(SettingsEditorListener
<Settings
> listener
) {
123 if (myListeners
== null) myListeners
= new ArrayList
<SettingsEditorListener
<Settings
>>();
124 myListeners
.add(listener
);
127 public final void removeSettingsEditorListener(SettingsEditorListener
<Settings
> listener
) {
128 if (myListeners
== null) return;
129 myListeners
.remove(listener
);
130 if (myListeners
.size() == 0) myListeners
= null;
133 protected final void fireEditorStateChanged() {
134 if (myIsInUpdate
|| myListeners
== null) return;
135 SettingsEditorListener
[] listeners
= myListeners
.toArray(new SettingsEditorListener
[myListeners
.size()]);
136 for (SettingsEditorListener listener
: listeners
) {
137 listener
.stateChanged(this);