update copyright
[fedora-idea.git] / plugins / ui-designer / src / com / intellij / uiDesigner / propertyInspector / properties / IntroListModelProperty.java
blobaba673924fbbdff1a64a8d3b053de4cdca274ad2
1 /*
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.uiDesigner.propertyInspector.properties;
19 import com.intellij.uiDesigner.UIFormXmlConstants;
20 import com.intellij.uiDesigner.XmlWriter;
21 import com.intellij.uiDesigner.propertyInspector.IntrospectedProperty;
22 import com.intellij.uiDesigner.propertyInspector.PropertyEditor;
23 import com.intellij.uiDesigner.propertyInspector.PropertyRenderer;
24 import com.intellij.uiDesigner.propertyInspector.editors.ListModelEditor;
25 import com.intellij.uiDesigner.propertyInspector.renderers.LabelPropertyRenderer;
26 import com.intellij.uiDesigner.radComponents.RadComponent;
27 import com.intellij.uiDesigner.snapShooter.SnapshotContext;
28 import org.jetbrains.annotations.NonNls;
29 import org.jetbrains.annotations.NotNull;
31 import javax.swing.*;
32 import java.lang.reflect.Method;
34 /**
35 * @author yole
37 public class IntroListModelProperty extends IntrospectedProperty<String[]> {
38 private LabelPropertyRenderer<String[]> myRenderer;
39 private ListModelEditor myEditor;
40 @NonNls private static final String CLIENT_PROPERTY_KEY_PREFIX = "IntroListModelProperty_";
42 public IntroListModelProperty(final String name, final Method readMethod, final Method writeMethod, final boolean storeAsClient) {
43 super(name, readMethod, writeMethod, storeAsClient);
46 public void write(final String[] value, final XmlWriter writer) {
47 for(String s: value) {
48 writer.startElement(UIFormXmlConstants.ELEMENT_ITEM);
49 writer.addAttribute(UIFormXmlConstants.ATTRIBUTE_VALUE, s);
50 writer.endElement();
54 @NotNull
55 public PropertyRenderer<String[]> getRenderer() {
56 if (myRenderer == null) {
57 myRenderer = new MyRenderer();
59 return myRenderer;
62 public PropertyEditor<String[]> getEditor() {
63 if (myEditor == null) {
64 myEditor = new ListModelEditor(getName());
66 return myEditor;
69 @Override public String[] getValue(final RadComponent component) {
70 final String[] strings = (String[])component.getDelegee().getClientProperty(CLIENT_PROPERTY_KEY_PREFIX + getName());
71 if (strings == null) {
72 return new String[0];
74 return strings;
77 @Override protected void setValueImpl(final RadComponent component, final String[] value) throws Exception {
78 component.getDelegee().putClientProperty(CLIENT_PROPERTY_KEY_PREFIX + getName(), value);
79 DefaultComboBoxModel model = new DefaultComboBoxModel();
80 for(String s: value) {
81 model.addElement(s);
83 invokeSetter(component, model);
86 @Override
87 public void importSnapshotValue(final SnapshotContext context, final JComponent component, final RadComponent radComponent) {
88 ListModel listModel;
89 try {
90 listModel = (ListModel)myReadMethod.invoke(component, EMPTY_OBJECT_ARRAY);
92 catch (Exception e) {
93 return;
95 if (listModel == null || listModel.getSize() == 0) return;
96 String[] values = new String [listModel.getSize()];
97 for(int i=0; i<listModel.getSize(); i++) {
98 final Object value = listModel.getElementAt(i);
99 if (!(value instanceof String)) {
100 return;
102 values [i] = (String) value;
104 try {
105 setValue(radComponent, values);
107 catch (Exception e) {
108 // ignore
112 private static class MyRenderer extends LabelPropertyRenderer<String[]> {
113 @Override protected void customize(final String[] value) {
114 setText(ListModelEditor.listValueToString(value));