Merge branch 'master' of git@git.labs.intellij.net:idea/community into tool-window
[fedora-idea.git] / platform / platform-impl / src / com / intellij / application / options / SelectFontDialog.java
blob7b9e28ee88fefdd3d7e770fc031ae1d953767e48
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.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;
24 import javax.swing.*;
25 import java.awt.*;
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) {
40 super(parent, true);
41 myNameToIsMonospaced = nameToIsMonospaced;
42 setTitle(ApplicationBundle.message("title.select.font"));
43 myFontNames = fontNames;
44 myInitialFontName = initialFontName;
45 init();
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(
57 new MouseAdapter() {
58 public void mouseClicked(MouseEvent e) {
59 if (e.getClickCount() == 2){
60 doOKAction();
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());
79 });
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() {
87 public void run() {
88 myShowMonospacedCheckbox.setSelected(useOnlyMonospacedFonts);
90 });
92 return panel;
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);
106 public void show() {
107 SwingUtilities.invokeLater(new Runnable() {
108 public void run() {
109 ListScrollingUtil.selectItem(myFontList, myInitialFontName);
112 super.show();
117 public JComponent getPreferredFocusedComponent() {
118 return myFontList;
121 public String getFontName() {
122 return (String)myFontList.getSelectedValue();
125 private static class MyListCellRenderer extends DefaultListCellRenderer {
126 public Component getListCellRendererComponent(
127 JList list,
128 Object value,
129 int index,
130 boolean isSelected,
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));
135 return c;