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
.project
.Project
;
19 import com
.intellij
.openapi
.ui
.DialogWrapper
;
20 import com
.intellij
.ui
.ColoredListCellRenderer
;
21 import com
.intellij
.ui
.SimpleTextAttributes
;
22 import com
.intellij
.uiDesigner
.UIDesignerBundle
;
23 import com
.intellij
.uiDesigner
.lw
.FontDescriptor
;
24 import com
.intellij
.uiDesigner
.propertyInspector
.properties
.IntroFontProperty
;
25 import com
.intellij
.util
.ui
.UIUtil
;
26 import org
.jetbrains
.annotations
.NotNull
;
29 import javax
.swing
.event
.ChangeEvent
;
30 import javax
.swing
.event
.ChangeListener
;
31 import javax
.swing
.event
.ListSelectionEvent
;
32 import javax
.swing
.event
.ListSelectionListener
;
34 import java
.awt
.event
.ItemListener
;
35 import java
.awt
.event
.ItemEvent
;
36 import java
.util
.ArrayList
;
37 import java
.util
.Collections
;
38 import java
.util
.Comparator
;
39 import java
.util
.Enumeration
;
44 public class FontEditorDialog
extends DialogWrapper
{
45 private JList myFontNameList
;
46 private JList myFontStyleList
;
47 private JList myFontSizeList
;
48 private JPanel myRootPane
;
49 private JLabel myPreviewTextLabel
;
50 private JTextField myFontNameEdit
;
51 private JTextField myFontStyleEdit
;
52 private JSpinner myFontSizeEdit
;
53 private JList mySwingFontList
;
54 private JTabbedPane myTabbedPane
;
55 private JCheckBox myFontNameCheckbox
;
56 private JCheckBox myFontStyleCheckbox
;
57 private JCheckBox myFontSizeCheckbox
;
58 private FontDescriptor myValue
;
60 protected FontEditorDialog(final Project project
, String propertyName
) {
61 super(project
, false);
63 setTitle(UIDesignerBundle
.message("font.chooser.title", propertyName
));
64 myFontNameList
.setListData(UIUtil
.getValidFontNames(true));
65 myFontNameList
.addListSelectionListener(new MyListSelectionListener(myFontNameEdit
));
66 myFontStyleList
.setListData(new String
[] {
67 UIDesignerBundle
.message("font.chooser.regular"),
68 UIDesignerBundle
.message("font.chooser.bold"),
69 UIDesignerBundle
.message("font.chooser.italic"),
70 UIDesignerBundle
.message("font.chooser.bold.italic")
72 myFontStyleList
.addListSelectionListener(new MyListSelectionListener(myFontStyleEdit
));
73 myFontSizeList
.setListData(UIUtil
.getStandardFontSizes());
74 myFontSizeList
.addListSelectionListener(new ListSelectionListener() {
75 public void valueChanged(ListSelectionEvent e
) {
76 final Integer selValue
= Integer
.valueOf(myFontSizeList
.getSelectedValue().toString());
77 myFontSizeEdit
.setValue(selValue
);
81 myFontSizeEdit
.setModel(new SpinnerNumberModel(3, 3, 96, 1));
82 myFontSizeEdit
.addChangeListener(new ChangeListener() {
83 public void stateChanged(ChangeEvent e
) {
84 myFontSizeList
.setSelectedValue(myFontSizeEdit
.getValue().toString(), true);
88 mySwingFontList
.setListData(collectSwingFontDescriptors());
89 mySwingFontList
.setCellRenderer(new ColoredListCellRenderer() {
90 protected void customizeCellRenderer(JList list
, Object value
, int index
, boolean selected
, boolean hasFocus
) {
91 FontDescriptor descriptor
= (FontDescriptor
) value
;
93 append(descriptor
.getSwingFont(),
94 selected ? SimpleTextAttributes
.SELECTED_SIMPLE_CELL_ATTRIBUTES
: SimpleTextAttributes
.SIMPLE_CELL_ATTRIBUTES
);
95 append(" (" + fontToString(UIManager
.getFont(descriptor
.getSwingFont())) + ")",
96 selected ? SimpleTextAttributes
.SELECTED_SIMPLE_CELL_ATTRIBUTES
: SimpleTextAttributes
.GRAYED_ATTRIBUTES
);
99 mySwingFontList
.addListSelectionListener(new ListSelectionListener() {
100 public void valueChanged(ListSelectionEvent e
) {
101 myValue
= (FontDescriptor
)mySwingFontList
.getSelectedValue();
103 //showFont(myValue.getResolvedFont());
107 myFontNameCheckbox
.addChangeListener(new ChangeListener() {
108 public void stateChanged(ChangeEvent e
) {
109 myFontNameList
.setEnabled(myFontNameCheckbox
.isSelected());
113 myFontStyleCheckbox
.addItemListener(new ItemListener() {
114 public void itemStateChanged(ItemEvent e
) {
115 myFontStyleList
.setEnabled(myFontStyleCheckbox
.isSelected());
119 myFontSizeCheckbox
.addChangeListener(new ChangeListener() {
120 public void stateChanged(ChangeEvent e
) {
121 myFontSizeList
.setEnabled(myFontSizeCheckbox
.isSelected());
122 myFontSizeEdit
.setEnabled(myFontSizeCheckbox
.isSelected());
128 private static String
fontToString(final Font font
) {
129 StringBuilder result
= new StringBuilder(font
.getFamily());
130 result
.append(" ").append(font
.getSize());
131 if ((font
.getStyle() & Font
.BOLD
) != 0) {
132 result
.append(" ").append(UIDesignerBundle
.message("font.chooser.bold"));
134 if ((font
.getStyle() & Font
.ITALIC
) != 0) {
135 result
.append(" ").append(UIDesignerBundle
.message("font.chooser.bold"));
137 return result
.toString();
140 private static FontDescriptor
[] collectSwingFontDescriptors() {
141 ArrayList
<FontDescriptor
> result
= new ArrayList
<FontDescriptor
>();
142 UIDefaults defaults
= UIManager
.getDefaults();
143 Enumeration e
= defaults
.keys ();
144 while(e
.hasMoreElements()) {
145 Object key
= e
.nextElement();
146 Object value
= defaults
.get(key
);
147 if (key
instanceof String
&& value
instanceof Font
) {
148 result
.add(FontDescriptor
.fromSwingFont((String
) key
));
151 Collections
.sort(result
, new Comparator
<FontDescriptor
>() {
152 public int compare(final FontDescriptor o1
, final FontDescriptor o2
) {
153 return o1
.getSwingFont().compareTo(o2
.getSwingFont());
156 return result
.toArray(new FontDescriptor
[result
.size()]);
159 public FontDescriptor
getValue() {
163 public void setValue(@NotNull final FontDescriptor value
) {
165 if (value
.getSwingFont() != null) {
166 myTabbedPane
.setSelectedIndex(1);
167 mySwingFontList
.setSelectedValue(myValue
, true);
170 myFontNameCheckbox
.setSelected(value
.getFontName() != null);
171 myFontSizeCheckbox
.setSelected(value
.getFontSize() >= 0);
172 myFontStyleCheckbox
.setSelected(value
.getFontStyle() >= 0);
173 myFontNameList
.setSelectedValue(value
.getFontName(), true);
174 myFontStyleList
.setSelectedIndex(value
.getFontStyle());
175 if (value
.getFontSize() >= 0) {
176 myFontSizeList
.setSelectedValue(Integer
.toString(value
.getFontSize()), true);
177 if (myFontSizeList
.getSelectedIndex() < 0) {
178 myFontSizeEdit
.setValue(new Integer(value
.getFontSize()));
182 myFontSizeList
.setSelectedIndex(-1);
183 myFontSizeEdit
.setValue(0);
188 private void updateValue() {
189 final int fontSize
= ((Integer
)myFontSizeEdit
.getValue()).intValue();
190 myValue
= new FontDescriptor(myFontNameCheckbox
.isSelected() ?
(String
) myFontNameList
.getSelectedValue() : null,
191 myFontStyleCheckbox
.isSelected() ? myFontStyleList
.getSelectedIndex() : -1,
192 myFontSizeCheckbox
.isSelected() ? fontSize
: -1);
196 private void updatePreview() {
197 myPreviewTextLabel
.setText(IntroFontProperty
.descriptorToString(myValue
));
198 myPreviewTextLabel
.setFont(myValue
.getResolvedFont(myRootPane
.getFont()));
201 protected JComponent
createCenterPanel() {
205 private class MyListSelectionListener
implements ListSelectionListener
{
206 private final JTextField myTextField
;
208 public MyListSelectionListener(final JTextField textField
) {
209 myTextField
= textField
;
212 public void valueChanged(ListSelectionEvent e
) {
213 JList sourceList
= (JList
) e
.getSource();
214 final Object selValue
= sourceList
.getSelectedValue();
215 if (selValue
!= null) {
216 myTextField
.setText(selValue
.toString());
219 myTextField
.setText("");