update copyright
[fedora-idea.git] / plugins / ui-designer / src / com / intellij / uiDesigner / propertyInspector / editors / ComponentEditor.java
blob41cb6b8e2cbdcfc40d100332d01a3fea479a2e87
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.
16 package com.intellij.uiDesigner.propertyInspector.editors;
18 import com.intellij.openapi.util.Condition;
19 import com.intellij.uiDesigner.FormEditingUtil;
20 import com.intellij.uiDesigner.lw.IComponent;
21 import com.intellij.uiDesigner.propertyInspector.renderers.ComponentRenderer;
22 import com.intellij.uiDesigner.propertyInspector.InplaceContext;
23 import com.intellij.uiDesigner.radComponents.RadComponent;
24 import com.intellij.uiDesigner.radComponents.RadContainer;
26 import javax.swing.*;
27 import java.util.ArrayList;
29 /**
30 * @author yole
32 public class ComponentEditor extends ComboBoxPropertyEditor<String> {
33 private final Class myPropertyType;
34 private final Condition<RadComponent> myFilter;
35 private String myOldValue;
37 public ComponentEditor(final Class propertyType, final Condition<RadComponent> filter) {
38 myPropertyType = propertyType;
39 myFilter = filter;
40 myCbx.setRenderer(new ComponentRenderer());
43 public JComponent getComponent(RadComponent component, String value, InplaceContext inplaceContext) {
44 RadComponent[] components = collectFilteredComponents(component);
45 // components [0] = null (<none>)
46 myCbx.setModel(new DefaultComboBoxModel(components));
47 myOldValue = value;
48 if (value == null || myOldValue.length() == 0) {
49 myCbx.setSelectedIndex(0);
51 else {
52 for(int i=1; i<components.length; i++) {
53 if (components [i].getId().equals(value)) {
54 myCbx.setSelectedIndex(i);
55 break;
59 return myCbx;
62 private RadComponent[] collectFilteredComponents(final RadComponent component) {
63 final ArrayList<RadComponent> result = new ArrayList<RadComponent>();
64 result.add(null);
66 RadContainer container = component.getParent();
67 while(container.getParent() != null) {
68 container = container.getParent();
71 FormEditingUtil.iterate(container, new FormEditingUtil.ComponentVisitor() {
72 public boolean visit(final IComponent component) {
73 RadComponent radComponent = (RadComponent) component;
74 final JComponent delegee = radComponent.getDelegee();
75 if (!myPropertyType.isInstance(delegee)) {
76 return true;
78 if (myFilter == null || myFilter.value(radComponent)) {
79 result.add(radComponent);
81 return true;
83 });
85 return result.toArray(new RadComponent[result.size()]);
88 @Override
89 public String getValue() throws Exception {
90 final RadComponent selection = (RadComponent)myCbx.getSelectedItem();
91 if (selection == null) {
92 return myOldValue == null ? null : "";
94 return selection.getId();