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
.application
.options
;
18 import com
.intellij
.openapi
.application
.ApplicationBundle
;
19 import com
.intellij
.openapi
.editor
.colors
.EditorColorsManager
;
20 import com
.intellij
.openapi
.ui
.DialogWrapper
;
21 import com
.intellij
.ui
.ListScrollingUtil
;
22 import com
.intellij
.util
.containers
.HashMap
;
26 import java
.awt
.event
.ActionEvent
;
27 import java
.awt
.event
.ActionListener
;
28 import java
.awt
.event
.MouseAdapter
;
29 import java
.awt
.event
.MouseEvent
;
30 import java
.util
.List
;
32 public class SelectFontDialog
extends DialogWrapper
{
33 private JList myFontList
;
34 private JCheckBox myShowMonospacedCheckbox
;
35 private final List
<String
> myFontNames
;
36 private final String myInitialFontName
;
37 private final HashMap myNameToIsMonospaced
;
39 public SelectFontDialog(Component parent
, List
<String
> fontNames
, String initialFontName
, HashMap nameToIsMonospaced
) {
41 myNameToIsMonospaced
= nameToIsMonospaced
;
42 setTitle(ApplicationBundle
.message("title.select.font"));
43 myFontNames
= fontNames
;
44 myInitialFontName
= initialFontName
;
48 protected JComponent
createCenterPanel() {
49 myShowMonospacedCheckbox
= new JCheckBox(ApplicationBundle
.message("checkbox.show.only.monospaced.fonts"));
50 final boolean useOnlyMonospacedFonts
= EditorColorsManager
.getInstance().isUseOnlyMonospacedFonts();
51 myShowMonospacedCheckbox
.setSelected(useOnlyMonospacedFonts
);
52 myFontList
= new JList();
53 myFontList
.setModel(new DefaultListModel());
54 fillList(useOnlyMonospacedFonts
);
56 myFontList
.addMouseListener(
58 public void mouseClicked(MouseEvent e
) {
59 if (e
.getClickCount() == 2){
66 myFontList
.setCellRenderer(new MyListCellRenderer());
68 myShowMonospacedCheckbox
.addActionListener(new ActionListener() {
69 public void actionPerformed(ActionEvent e
) {
70 boolean onlyMonospaced
= myShowMonospacedCheckbox
.isSelected();
71 EditorColorsManager
.getInstance().setUseOnlyMonospacedFonts(onlyMonospaced
);
72 String selection
= (String
) myFontList
.getSelectedValue();
73 fillList(onlyMonospaced
);
74 if (selection
!= null) {
75 myFontList
.setSelectedValue(selection
, true);
76 myFontList
.ensureIndexIsVisible(myFontList
.getSelectedIndex());
81 JPanel panel
= new JPanel(new BorderLayout());
83 panel
.add(myShowMonospacedCheckbox
, BorderLayout
.NORTH
);
84 panel
.add(new JScrollPane(myFontList
), BorderLayout
.CENTER
);
86 SwingUtilities
.invokeLater(new Runnable() {
88 myShowMonospacedCheckbox
.setSelected(useOnlyMonospacedFonts
);
95 private void fillList(boolean onlyMonospaced
) {
96 DefaultListModel model
= (DefaultListModel
) myFontList
.getModel();
97 model
.removeAllElements();
98 for (int i
= 0; i
< myFontNames
.size(); i
++) {
99 String fontName
= myFontNames
.get(i
);
100 if (!onlyMonospaced
|| Boolean
.TRUE
.equals(myNameToIsMonospaced
.get(fontName
))) {
101 model
.addElement(fontName
);
107 SwingUtilities
.invokeLater(new Runnable() {
109 ListScrollingUtil
.selectItem(myFontList
, myInitialFontName
);
117 public JComponent
getPreferredFocusedComponent() {
121 public String
getFontName() {
122 return (String
)myFontList
.getSelectedValue();
125 private static class MyListCellRenderer
extends DefaultListCellRenderer
{
126 public Component
getListCellRendererComponent(
131 boolean cellHasFocus
) {
132 Component c
= super.getListCellRendererComponent(list
, value
, index
, isSelected
, cellHasFocus
);
133 String fontName
= (String
) value
;
134 c
.setFont(new Font(fontName
, Font
.PLAIN
, 14));