1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package net.sourceforge.jeuclid.app.mathviewer;
20
21 import java.awt.BorderLayout;
22 import java.awt.Dialog;
23 import java.awt.FlowLayout;
24 import java.awt.Font;
25 import java.awt.Frame;
26 import java.awt.event.ActionEvent;
27 import java.awt.event.ActionListener;
28 import java.util.ArrayList;
29 import java.util.Collections;
30 import java.util.List;
31 import java.util.Vector;
32
33 import javax.swing.JButton;
34 import javax.swing.JDialog;
35 import javax.swing.JLabel;
36 import javax.swing.JList;
37 import javax.swing.JPanel;
38 import javax.swing.JScrollPane;
39 import javax.swing.JTextArea;
40 import javax.swing.ScrollPaneConstants;
41 import javax.swing.WindowConstants;
42 import javax.swing.event.ListSelectionEvent;
43 import javax.swing.event.ListSelectionListener;
44
45 import net.sourceforge.jeuclid.font.FontFactory;
46
47
48
49
50
51
52
53 public class FontSelectionDialog extends JDialog {
54
55
56 private static final int SAMPLE_FONTSIZE = 14;
57
58
59
60
61 private static final long serialVersionUID = 1L;
62
63 private static final String PREVIEW_TEXT = "The quick brown fox jumps over the lazy dog. 123456790";
64
65 private final List<String> fontNames = new ArrayList<String>();
66
67 private JList list;
68
69
70
71
72
73
74
75
76
77 public FontSelectionDialog(final Dialog parent,
78 final List<String> currentFontNames) {
79 super(parent);
80 this.init(currentFontNames);
81 }
82
83
84
85
86
87
88
89
90
91 public FontSelectionDialog(final Frame parent,
92 final List<String> currentFontNames) {
93 super(parent);
94 this.init(currentFontNames);
95 }
96
97 private void init(final List<String> currentFontNames) {
98 this.setTitle(Messages
99 .getString("MathViewer.FontSelectionDialog.title"));
100 this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
101 this
102 .getContentPane()
103 .add(
104 new JLabel(
105 Messages
106 .getString("MathViewer.FontSelectionDialog.topLabel")),
107 BorderLayout.NORTH);
108 this.setupFontsList(currentFontNames);
109 this.setupButtons();
110 this.pack();
111 this.setLocationByPlatform(true);
112 this.setModal(true);
113 }
114
115
116
117
118 public List<String> getFontNames() {
119 return this.fontNames;
120 }
121
122 private void setupFontsList(final List<String> currentFontNames) {
123
124
125 final Vector<String> allFonts = new Vector<String>(FontFactory
126 .getInstance().listFontNames());
127
128 Collections.sort(allFonts);
129 this.list = new JList(allFonts);
130 final List<Integer> selectedIndicies = new ArrayList<Integer>();
131 if (currentFontNames != null && !currentFontNames.isEmpty()) {
132 for (final String value : currentFontNames) {
133 final int i = Collections.binarySearch(allFonts, value);
134 if (i > -1) {
135 selectedIndicies.add(i);
136 }
137 }
138 }
139 final int[] selectedIndiciesArr = new int[selectedIndicies.size()];
140 for (int i = 0; i < selectedIndiciesArr.length; i++) {
141 selectedIndiciesArr[i] = selectedIndicies.get(i);
142 }
143 this.list.setSelectedIndices(selectedIndiciesArr);
144 final JScrollPane scrollPane = new JScrollPane(this.list,
145 ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
146 ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
147 final JTextArea preview = new JTextArea(
148 FontSelectionDialog.PREVIEW_TEXT);
149 this.list.addListSelectionListener(new ListSelectionListener() {
150 public void valueChanged(final ListSelectionEvent e) {
151 preview.setFont(FontFactory.getInstance().getFont(
152 (String) FontSelectionDialog.this.list
153 .getSelectedValue(), Font.PLAIN,
154 FontSelectionDialog.SAMPLE_FONTSIZE));
155 preview.revalidate();
156 }
157 });
158 this.getContentPane().add(scrollPane, BorderLayout.WEST);
159 this.getContentPane().add(preview, BorderLayout.CENTER);
160 }
161
162 private void setupButtons() {
163 final JPanel buttonsPanel = new JPanel(new FlowLayout(
164 FlowLayout.CENTER));
165 final JButton btnOK = new JButton(Messages.getString("MathViewer.ok"));
166 btnOK.setMnemonic('O');
167 final ActionListener actionListener = new ActionListener() {
168 public void actionPerformed(final ActionEvent e) {
169 if (e.getSource() == btnOK) {
170 for (Object val : FontSelectionDialog.this.list
171 .getSelectedValues()) {
172 FontSelectionDialog.this.fontNames.add((String) val);
173 }
174 }
175 FontSelectionDialog.this.dispose();
176 }
177 };
178 btnOK.addActionListener(actionListener);
179 buttonsPanel.add(btnOK);
180 final JButton btnCancel = new JButton(Messages
181 .getString("MathViewer.cancel"));
182 btnCancel.setMnemonic('C');
183 btnCancel.addActionListener(actionListener);
184 buttonsPanel.add(btnCancel);
185 this.getContentPane().add(buttonsPanel, BorderLayout.SOUTH);
186 }
187
188 }