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
.util
.xml
.ui
;
19 import com
.intellij
.openapi
.diagnostic
.Logger
;
20 import com
.intellij
.openapi
.module
.Module
;
21 import com
.intellij
.openapi
.project
.Project
;
22 import com
.intellij
.openapi
.util
.Factory
;
23 import com
.intellij
.openapi
.util
.text
.StringUtil
;
24 import com
.intellij
.openapi
.application
.ApplicationManager
;
25 import com
.intellij
.util
.xml
.DomElement
;
26 import com
.intellij
.util
.xml
.DomFileElement
;
27 import com
.intellij
.util
.xml
.DomUtil
;
28 import com
.intellij
.util
.xml
.GenericDomValue
;
29 import com
.intellij
.util
.xml
.highlighting
.DomElementAnnotationsManager
;
30 import com
.intellij
.util
.xml
.reflect
.DomChildrenDescription
;
31 import com
.intellij
.util
.xml
.reflect
.DomCollectionChildDescription
;
32 import com
.intellij
.util
.xml
.reflect
.DomFixedChildDescription
;
33 import com
.intellij
.util
.xml
.reflect
.AbstractDomChildrenDescription
;
34 import org
.jetbrains
.annotations
.NotNull
;
37 import java
.lang
.reflect
.Field
;
38 import java
.util
.HashMap
;
42 * User: Sergey.Vasiliev
45 public abstract class BasicDomElementComponent
<T
extends DomElement
> extends AbstractDomElementComponent
<T
> {
46 private static final Logger LOG
= Logger
.getInstance("#com.intellij.util.xml.ui.editors.BasicDomElementComponent");
47 private final Map
<JComponent
, DomUIControl
> myBoundComponents
= new HashMap
<JComponent
, DomUIControl
>();
49 public BasicDomElementComponent(T domElement
) {
53 protected final void bindProperties() {
54 bindProperties(getDomElement());
57 protected boolean commitOnEveryChange(GenericDomValue element
) {
61 protected final void bindProperties(final DomElement domElement
) {
62 if (domElement
== null) return;
64 DomElementAnnotationsManager
.getInstance(domElement
.getManager().getProject()).addHighlightingListener(new DomElementAnnotationsManager
.DomHighlightingListener() {
65 public void highlightingFinished(@NotNull final DomFileElement element
) {
66 ApplicationManager
.getApplication().invokeLater(new Runnable() {
68 if (getComponent().isShowing() && element
.isValid()) {
76 for (final AbstractDomChildrenDescription description
: domElement
.getGenericInfo().getChildrenDescriptions()) {
77 final JComponent boundComponent
= getBoundComponent(description
);
78 if (boundComponent
!= null) {
79 if (description
instanceof DomFixedChildDescription
&& DomUtil
.isGenericValueType(((DomFixedChildDescription
)description
).getType())) {
80 if ((description
.getValues(domElement
)).size() == 1) {
81 final GenericDomValue element
= domElement
.getManager().createStableValue(new Factory
<GenericDomValue
>() {
82 public GenericDomValue
create() {
83 return domElement
.isValid() ?
(GenericDomValue
)description
.getValues(domElement
).get(0) : null;
86 doBind(DomUIFactory
.createControl(element
, commitOnEveryChange(element
)), boundComponent
);
93 else if (description
instanceof DomCollectionChildDescription
) {
94 doBind(DomUIFactory
.getDomUIFactory().createCollectionControl(domElement
, (DomCollectionChildDescription
)description
), boundComponent
);
101 protected void doBind(final DomUIControl control
, final JComponent boundComponent
) {
102 myBoundComponents
.put(boundComponent
, control
);
103 control
.bind(boundComponent
);
104 addComponent(control
);
107 private JComponent
getBoundComponent(final AbstractDomChildrenDescription description
) {
108 for (Field field
: getClass().getDeclaredFields()) {
110 field
.setAccessible(true);
112 if (description
instanceof DomChildrenDescription
) {
113 final DomChildrenDescription childrenDescription
= (DomChildrenDescription
)description
;
114 if (convertFieldName(field
.getName(), childrenDescription
).equals(childrenDescription
.getXmlElementName()) && field
.get(this) instanceof JComponent
) {
115 return (JComponent
)field
.get(this);
119 catch (IllegalAccessException e
) {
127 private String
convertFieldName(String propertyName
, final DomChildrenDescription description
) {
128 if (propertyName
.startsWith("my")) propertyName
= propertyName
.substring(2);
130 String convertedName
= description
.getDomNameStrategy(getDomElement()).convertName(propertyName
);
132 if (description
instanceof DomCollectionChildDescription
) {
133 final String unpluralizedStr
= StringUtil
.unpluralize(convertedName
);
135 if (unpluralizedStr
!= null) return unpluralizedStr
;
137 return convertedName
;
140 public final Project
getProject() {
141 return getDomElement().getManager().getProject();
144 public final Module
getModule() {
145 return getDomElement().getModule();
148 protected final DomUIControl
getDomControl(JComponent component
) {
149 return myBoundComponents
.get(component
);