2 * Copyright (c) 2005 Your Corporation. All Rights Reserved.
4 package com
.intellij
.util
.xml
.ui
;
6 import com
.intellij
.openapi
.application
.Result
;
7 import com
.intellij
.openapi
.command
.WriteCommandAction
;
8 import com
.intellij
.openapi
.project
.Project
;
9 import com
.intellij
.openapi
.util
.Comparing
;
10 import com
.intellij
.util
.EventDispatcher
;
11 import com
.intellij
.util
.xml
.DomElement
;
12 import com
.intellij
.ui
.SimpleTextAttributes
;
13 import org
.jetbrains
.annotations
.Nullable
;
17 import java
.awt
.event
.FocusEvent
;
18 import java
.awt
.event
.FocusListener
;
19 import java
.lang
.reflect
.InvocationTargetException
;
24 public abstract class BaseControl
<Bound
extends JComponent
, T
> implements DomUIControl
{
25 public static final Color ERROR_BACKGROUND
= new Color(255,204,204);
26 public static final Color ERROR_FOREGROUND
= SimpleTextAttributes
.ERROR_ATTRIBUTES
.getFgColor();
27 public static final Color WARNING_BACKGROUND
= new Color(255,255,204);
29 private final EventDispatcher
<CommitListener
> myDispatcher
= EventDispatcher
.create(CommitListener
.class);
31 private Bound myBoundComponent
;
32 private DomWrapper
<T
> myDomWrapper
;
33 private boolean myCommitting
;
35 private Color myDefaultForeground
;
36 private Color myDefaultBackground
;
38 protected BaseControl(final DomWrapper
<T
> domWrapper
) {
39 myDomWrapper
= domWrapper
;
42 private void checkInitialized() {
43 if (myBoundComponent
!= null) return;
48 protected JComponent
getHighlightedComponent(Bound component
) {
52 protected final Color
getDefaultBackground() {
53 return myDefaultBackground
;
56 protected final Color
getDefaultForeground() {
57 return myDefaultForeground
;
60 protected final Color
getErrorBackground() {
61 return ERROR_BACKGROUND
;
64 protected final Color
getWarningBackground() {
65 return WARNING_BACKGROUND
;
68 protected final Color
getErrorForeground() {
69 return ERROR_FOREGROUND
;
73 private void initialize(final Bound boundComponent
) {
74 myBoundComponent
= createMainComponent(boundComponent
);
75 final JComponent highlightedComponent
= getHighlightedComponent(myBoundComponent
);
76 myDefaultForeground
= highlightedComponent
.getForeground();
77 myDefaultBackground
= highlightedComponent
.getBackground();
78 final JComponent component
= getComponentToListenFocusLost(myBoundComponent
);
79 if (component
!= null) {
80 component
.addFocusListener(new FocusListener() {
81 public void focusGained(FocusEvent e
) {
84 public void focusLost(FocusEvent e
) {
85 if (!e
.isTemporary() && myDomWrapper
.isValid()) {
96 protected JComponent
getComponentToListenFocusLost(Bound component
) {
100 protected abstract Bound
createMainComponent(Bound boundedComponent
);
102 public void bind(JComponent component
) {
103 initialize((Bound
)component
);
106 public void addCommitListener(CommitListener listener
) {
107 myDispatcher
.addListener(listener
);
110 public void removeCommitListener(CommitListener listener
) {
111 myDispatcher
.removeListener(listener
);
114 public final DomElement
getDomElement() {
115 return myDomWrapper
.getDomElement();
118 public final DomWrapper
<T
> getDomWrapper() {
122 public final Bound
getComponent() {
124 return myBoundComponent
;
127 public void dispose() {
130 public final void commit() {
131 if (myDomWrapper
.isValid() && !isCommitted()) {
132 setValueToXml(getValue(getComponent()));
137 private static boolean valuesAreEqual(final Object valueInXml
, final Object valueInControl
) {
138 return "".equals(valueInControl
) && null == valueInXml
|| Comparing
.equal(valueInXml
, valueInControl
);
141 public final void reset() {
148 protected void updateComponent() {
151 protected void doReset() {
152 if (!isCommitted()) {
153 setValue(getComponent(), getValueFromXml());
157 protected final boolean isCommitted() {
158 return valuesAreEqual(getValueFromXml(), getValue(getComponent()));
161 private void setValueToXml(final T value
) {
162 if (myCommitting
) return;
165 new WriteCommandAction(getProject()) {
166 protected void run(Result result
) throws Throwable
{
167 final CommitListener multicaster
= myDispatcher
.getMulticaster();
168 multicaster
.beforeCommit(BaseControl
.this);
169 myDomWrapper
.setValue("".equals(value
) ?
null : value
);
170 multicaster
.afterCommit(BaseControl
.this);
175 myCommitting
= false;
179 protected final Project
getProject() {
180 return myDomWrapper
.getProject();
183 private T
getValueFromXml() {
185 return myDomWrapper
.getValue();
187 catch (IllegalAccessException e
) {
188 throw new RuntimeException(e
);
190 catch (InvocationTargetException e
) {
191 throw new RuntimeException(e
);
196 public boolean canNavigate(DomElement element
) {
200 public void navigate(DomElement element
) {
203 protected abstract T
getValue(Bound component
);
204 protected abstract void setValue(Bound component
, T value
);