summaryrefslogtreecommitdiff
path: root/plugins/ui-designer/src/com/intellij/uiDesigner/propertyInspector/editors/ColorEditor.java
blob: 7e6b94dc47846f8a2e214bd0128cbf418886a184 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*
 * Copyright 2000-2009 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.intellij.uiDesigner.propertyInspector.editors;

import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.ui.TextFieldWithBrowseButton;
import com.intellij.ui.ListSpeedSearch;
import com.intellij.ui.ScrollPaneFactory;
import com.intellij.ui.components.JBList;
import com.intellij.uiDesigner.UIDesignerBundle;
import com.intellij.uiDesigner.lw.ColorDescriptor;
import com.intellij.uiDesigner.propertyInspector.InplaceContext;
import com.intellij.uiDesigner.propertyInspector.PropertyEditor;
import com.intellij.uiDesigner.propertyInspector.renderers.ColorRenderer;
import com.intellij.uiDesigner.radComponents.RadComponent;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import javax.swing.colorchooser.AbstractColorChooserPanel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.plaf.ColorChooserUI;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.*;
import java.util.List;

/**
 * @author yole
 */
public class ColorEditor extends PropertyEditor<ColorDescriptor> {
  private final String myPropertyName;
  private final TextFieldWithBrowseButton myTextField = new TextFieldWithBrowseButton();
  private ColorDescriptor myValue;
  private Project myProject;

  public ColorEditor(String propertyName) {
    myPropertyName = propertyName;
    myTextField.getTextField().setBorder(BorderFactory.createEmptyBorder(0, 20, 0, 0));
    myTextField.getTextField().setEditable(false);
    myTextField.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        MyColorChooserDialog dialog = new MyColorChooserDialog(myProject);
        dialog.setSelectedValue(myValue);
        dialog.show();
        if (dialog.getExitCode() == DialogWrapper.OK_EXIT_CODE) {
          myValue = dialog.getSelectedValue();
          updateTextField();
        }
      }
    });
  }

  public ColorDescriptor getValue() throws Exception {
    return myValue;
  }

  public JComponent getComponent(RadComponent component, ColorDescriptor value, InplaceContext inplaceContext) {
    myValue = value != null ? value : new ColorDescriptor(new Color(0));
    myProject = component.getProject();
    updateTextField();
    return myTextField;
  }

  private void updateTextField() {
    myTextField.setText(myValue == null ? "" : myValue.toString());
  }

  public void updateUI() {
    SwingUtilities.updateComponentTreeUI(myTextField);
  }

  private static class ColorDescriptorWrapper extends Color {
    private final ColorDescriptor myDescriptor;

    public ColorDescriptorWrapper(ColorDescriptor descriptor) {
      super(descriptor.getResolvedColor() == null ? 0 : descriptor.getResolvedColor().getRGB());
      myDescriptor = descriptor;
    }

    public ColorDescriptor getDescriptor() {
      return myDescriptor;
    }
  }

  private class MyColorChooserDialog extends DialogWrapper {
    private JColorChooser myColorChooser;
    private MyDescriptorChooserPanel mySwingChooserPanel;
    private MyDescriptorChooserPanel mySystemChooserPanel;
    private MyDescriptorChooserPanel myAWTChooserPanel;

    public MyColorChooserDialog(Project project) {
      super(project, false);
      setTitle(UIDesignerBundle.message("color.chooser.title", myPropertyName));
      init();
    }

    protected JComponent createCenterPanel() {
      myColorChooser = new JColorChooser();
      mySwingChooserPanel = new MyDescriptorChooserPanel(UIDesignerBundle.message("color.chooser.swing.palette"), collectSwingColorDescriptors());
      myColorChooser.addChooserPanel(mySwingChooserPanel);
      mySystemChooserPanel = new MyDescriptorChooserPanel(UIDesignerBundle.message("color.chooser.system.palette"),
                                                          collectColorFields(SystemColor.class, true));
      myColorChooser.addChooserPanel(mySystemChooserPanel);
      myAWTChooserPanel = new MyDescriptorChooserPanel(UIDesignerBundle.message("color.chooser.awt.palette"),
                                                       collectColorFields(Color.class, false));
      myColorChooser.addChooserPanel(myAWTChooserPanel);
      return myColorChooser;
    }

    private void selectTabForColor(@Nullable final ColorDescriptor value) {
      String tabName;

      if (value == null || value.getSwingColor() != null) {
        tabName = mySwingChooserPanel.getDisplayName();
      }
      else if (value.getSystemColor() != null) {
        tabName = mySystemChooserPanel.getDisplayName();
      }
      else if (value.getAWTColor() != null) {
        tabName = myAWTChooserPanel.getDisplayName();
      }
      else {
        return;
      }

      final ColorChooserUI ui = myColorChooser.getUI();
      try {
        final Field field = ui.getClass().getDeclaredField("tabbedPane");
        field.setAccessible(true);
        JTabbedPane tabbedPane = (JTabbedPane) field.get(ui);
        for(int i=0; i<tabbedPane.getTabCount(); i++) {
          if (tabbedPane.getTitleAt(i).equals(tabName)) {
            tabbedPane.setSelectedIndex(i);
            break;
          }
        }
      }
      catch (NoSuchFieldException e) {
        // ignore
      }
      catch (IllegalAccessException e) {
        // ignore
      }
    }

    private List<ColorDescriptor> collectSwingColorDescriptors() {
      ArrayList<ColorDescriptor> result = new ArrayList<ColorDescriptor>();
      UIDefaults defaults = UIManager.getDefaults();
      Enumeration e = defaults.keys ();
      while(e.hasMoreElements()) {
        Object key = e.nextElement();
        Object value = defaults.get(key);
        if (key instanceof String && value instanceof Color) {
          result.add(ColorDescriptor.fromSwingColor((String) key));
        }
      }
      return result;
    }

    private List<ColorDescriptor> collectColorFields(final Class aClass, final boolean isSystem) {
      ArrayList<ColorDescriptor> result = new ArrayList<ColorDescriptor>();
      Field[] colorFields = aClass.getDeclaredFields();
      for(Field field: colorFields) {
        if ((field.getModifiers() & Modifier.STATIC) != 0 &&
            Color.class.isAssignableFrom(field.getType()) &&
            Character.isLowerCase(field.getName().charAt(0))) {
          final ColorDescriptor color = isSystem
                                        ? ColorDescriptor.fromSystemColor(field.getName())
                                        : ColorDescriptor.fromAWTColor(field.getName());
          result.add(color);
        }
      }
      return result;
    }

    public void setSelectedValue(@NotNull final ColorDescriptor value) {
      myColorChooser.setColor(new ColorDescriptorWrapper(value));
      selectTabForColor(value);
    }

    public ColorDescriptor getSelectedValue() {
      final Color color = myColorChooser.getColor();
      if (color instanceof ColorDescriptorWrapper) {
        return ((ColorDescriptorWrapper) color).getDescriptor();
      }
      return new ColorDescriptor(color);
    }
  }

  private static class MyDescriptorChooserPanel extends AbstractColorChooserPanel {
    private final String myDisplayName;
    private final ColorDescriptor[] myColorDescriptors;
    private JList myDescriptorList;

    public MyDescriptorChooserPanel(final String displayName, List<ColorDescriptor> colorDescriptorList) {
      myDisplayName = displayName;

      Collections.sort(colorDescriptorList, new Comparator<ColorDescriptor>() {
        public int compare(final ColorDescriptor o1, final ColorDescriptor o2) {
          return o1.toString().compareTo(o2.toString());
        }
      });

      myColorDescriptors = colorDescriptorList.toArray(new ColorDescriptor[colorDescriptorList.size()]);
    }

    public void updateChooser() {
      myDescriptorList.setSelectedValue(getColorFromModel(), true);
    }

    protected void buildChooser() {
      setLayout(new BorderLayout());
      myDescriptorList = new JBList(myColorDescriptors);
      myDescriptorList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
      myDescriptorList.setVisibleRowCount(15);
      myDescriptorList.setCellRenderer(new ColorRenderer());
      myDescriptorList.addListSelectionListener(new ListSelectionListener() {
        public void valueChanged(ListSelectionEvent e) {
          ColorDescriptor descriptor = (ColorDescriptor)myDescriptorList.getSelectedValue();
          getColorSelectionModel().setSelectedColor(new ColorDescriptorWrapper(descriptor));
        }
      });
      new ListSpeedSearch(myDescriptorList);
      add(ScrollPaneFactory.createScrollPane(myDescriptorList), BorderLayout.CENTER);
    }

    public String getDisplayName() {
      return myDisplayName;
    }

    @Nullable
    public Icon getSmallDisplayIcon() {
      return null;
    }

    @Nullable
    public Icon getLargeDisplayIcon() {
      return null;
    }
  }
}