svn-gvsig-desktop / branches / v10 / extensions / extAnnotations / src / com / iver / cit / gvsig / project / documents / view / tool / gui / FontChooser.java @ 11743
History | View | Annotate | Download (8.06 KB)
1 |
package com.iver.cit.gvsig.project.documents.view.tool.gui; |
---|---|
2 |
|
3 |
/**
|
4 |
* Copyright (C) 2005 the Lexi Project.
|
5 |
*
|
6 |
* This file is part of the Lexi document editor.
|
7 |
*
|
8 |
* Lexi is free software; you can redistribute it and/or modify
|
9 |
* it under the terms of the GNU General Public License as published by
|
10 |
* the Free Software Foundation; either version 2, or (at your option)
|
11 |
* any later version.
|
12 |
*
|
13 |
* Lexi is distributed in the hope that it will be useful, but
|
14 |
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
16 |
* General Public License for more details.
|
17 |
*
|
18 |
* You should have received a copy of the GNU General Public License
|
19 |
* along with GNU Classpath; see the file COPYING. If not, write to the
|
20 |
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
21 |
* 02110-1301 USA.
|
22 |
*
|
23 |
* Linking this library statically or dynamically with other modules is
|
24 |
* making a combined work based on this library. Thus, the terms and
|
25 |
* conditions of the GNU General Public License cover the whole combination.
|
26 |
*/
|
27 |
|
28 |
import java.awt.BorderLayout; |
29 |
import java.awt.Component; |
30 |
import java.awt.FlowLayout; |
31 |
import java.awt.Font; |
32 |
import java.awt.GridLayout; |
33 |
import java.awt.event.ActionEvent; |
34 |
import java.awt.event.ActionListener; |
35 |
import java.awt.event.ItemEvent; |
36 |
import java.awt.event.ItemListener; |
37 |
import java.util.Enumeration; |
38 |
import java.util.Vector; |
39 |
|
40 |
import javax.swing.JButton; |
41 |
import javax.swing.JCheckBox; |
42 |
import javax.swing.JComboBox; |
43 |
import javax.swing.JFrame; |
44 |
import javax.swing.JList; |
45 |
import javax.swing.JPanel; |
46 |
import javax.swing.JScrollPane; |
47 |
import javax.swing.JSeparator; |
48 |
import javax.swing.JTextArea; |
49 |
import javax.swing.text.AttributeSet; |
50 |
import javax.swing.text.StyleConstants; |
51 |
import javax.swing.event.ListSelectionEvent; |
52 |
import javax.swing.event.ListSelectionListener; |
53 |
|
54 |
/**
|
55 |
* This class implements a Dialog for selecting a font, consisting of
|
56 |
* a font family, a style and a font size.
|
57 |
*/
|
58 |
public final class FontChooser extends JPanel |
59 |
implements ActionListener, |
60 |
ListSelectionListener, ItemListener { |
61 |
// private Lexi m_app;
|
62 |
private JList m_fontList; |
63 |
private JTextArea m_prevArea; |
64 |
private JComboBox m_sizeBox; |
65 |
private JCheckBox m_boldBox, m_italicBox, m_plainBox; |
66 |
|
67 |
private boolean m_outcome; |
68 |
|
69 |
/**
|
70 |
* @param owner this Dialog's parent window.
|
71 |
* @param initial the initial font attributes
|
72 |
*/
|
73 |
public FontChooser(JFrame owner, AttributeSet initial) { |
74 |
// super(owner, "Font Chooser");
|
75 |
setSize(700, 500); |
76 |
init(owner, initial); |
77 |
} |
78 |
|
79 |
/**
|
80 |
* Handle the actions associated with the 'ok' and the 'cancel' buttons
|
81 |
*/
|
82 |
public void actionPerformed(ActionEvent e) { |
83 |
String command = e.getActionCommand();
|
84 |
if (command.equals("can-button")) { |
85 |
m_outcome = false;
|
86 |
// dispose();
|
87 |
} |
88 |
else if (command.equals("ok-button")) { |
89 |
m_outcome = true;
|
90 |
// dispose();
|
91 |
} |
92 |
} |
93 |
|
94 |
public boolean getOutcome() { |
95 |
return m_outcome;
|
96 |
} |
97 |
|
98 |
public int getFontSize() { |
99 |
return Integer.parseInt(m_sizeBox.getSelectedItem().toString()); |
100 |
} |
101 |
|
102 |
public int getFontStyle() { |
103 |
return m_plainBox.isSelected() ? Font.PLAIN : |
104 |
((m_boldBox.isSelected() ? Font.BOLD : 0) + |
105 |
(m_italicBox.isSelected() ? Font.ITALIC : 0)); |
106 |
} |
107 |
|
108 |
public String getFontFamily() { |
109 |
return m_fontList.getSelectedValue().toString();
|
110 |
} |
111 |
|
112 |
/**
|
113 |
* Sets us up with the panels needed to create this font chooser
|
114 |
*/
|
115 |
private void init(Component c, AttributeSet initial) { |
116 |
// m_app = (Lexi) c;
|
117 |
|
118 |
JPanel main = new JPanel(); |
119 |
JPanel buttonPanel = new JPanel(); |
120 |
JPanel listPanel = new JPanel(); |
121 |
JPanel fontPanel = new JPanel(); |
122 |
JPanel optionPanel = new JPanel(); |
123 |
JPanel previewPanel = new JPanel(); |
124 |
main.setLayout(new BorderLayout()); |
125 |
buttonPanel.setLayout(new FlowLayout()); |
126 |
fontPanel.setLayout(new BorderLayout()); |
127 |
optionPanel.setLayout(new GridLayout(3, 2)); |
128 |
|
129 |
// Registry reg = Lexi.getRegistry();
|
130 |
// Enumeration fontEnum = reg.getKeys("FONTS");
|
131 |
Vector fonts = new Vector(); |
132 |
// while (fontEnum.hasMoreElements()) {
|
133 |
// String fontKey = (String) fontEnum.nextElement();
|
134 |
// if (reg.getBoolean("FONTS", fontKey)) {
|
135 |
// fonts.add(fontKey);
|
136 |
// }
|
137 |
// }
|
138 |
|
139 |
m_fontList = new JList(fonts); |
140 |
m_fontList.addListSelectionListener(this);
|
141 |
|
142 |
JScrollPane scroller = new JScrollPane(m_fontList); |
143 |
listPanel.add(scroller); |
144 |
|
145 |
fontPanel.add(listPanel, BorderLayout.WEST);
|
146 |
fontPanel.add(optionPanel, BorderLayout.EAST);
|
147 |
|
148 |
// int[] sizes = m_app.getFontSizes();
|
149 |
m_sizeBox = new JComboBox(); |
150 |
// for (int k = 0; k < sizes.length; k++) {
|
151 |
// m_sizeBox.addItem(Integer.toString(sizes[k]));
|
152 |
// }
|
153 |
m_sizeBox.setEditable(false);
|
154 |
m_sizeBox.addItemListener(this);
|
155 |
|
156 |
optionPanel.add(m_sizeBox); |
157 |
optionPanel.add(new JSeparator()); |
158 |
m_boldBox = new JCheckBox("Bold", false); |
159 |
m_boldBox.addItemListener(this);
|
160 |
m_italicBox = new JCheckBox("Italic", false); |
161 |
m_italicBox.addItemListener(this);
|
162 |
m_plainBox = new JCheckBox("Regular", false); |
163 |
m_plainBox.addItemListener(this);
|
164 |
|
165 |
// Select the initial font style, size and family
|
166 |
boolean bold = StyleConstants.isBold(initial); |
167 |
boolean italic = StyleConstants.isItalic(initial); |
168 |
if (!(bold || italic)) {
|
169 |
m_plainBox.setSelected(true);
|
170 |
m_plainBox.setEnabled(true);
|
171 |
m_boldBox.setEnabled(false);
|
172 |
m_italicBox.setEnabled(false);
|
173 |
} |
174 |
else {
|
175 |
if (bold) {
|
176 |
m_boldBox.setSelected(true);
|
177 |
} |
178 |
if (italic) {
|
179 |
m_italicBox.setSelected(true);
|
180 |
} |
181 |
m_plainBox.setEnabled(false);
|
182 |
m_boldBox.setEnabled(true);
|
183 |
m_italicBox.setEnabled(true);
|
184 |
} |
185 |
String fontSize = Integer.toString(StyleConstants.getFontSize(initial)); |
186 |
m_sizeBox.setSelectedItem(fontSize); |
187 |
String fontFamily = StyleConstants.getFontFamily(initial); |
188 |
m_fontList.setSelectedValue(fontFamily, true);
|
189 |
|
190 |
optionPanel.add(m_plainBox); |
191 |
optionPanel.add(m_italicBox); |
192 |
optionPanel.add(m_boldBox); |
193 |
|
194 |
// Button Pane
|
195 |
JButton okButton = new JButton("OK"); |
196 |
okButton.setActionCommand("ok-button");
|
197 |
okButton.addActionListener(this);
|
198 |
JButton cancelButton = new JButton("Cancel"); |
199 |
cancelButton.setActionCommand("can-button");
|
200 |
cancelButton.addActionListener(this);
|
201 |
buttonPanel.add(okButton); |
202 |
buttonPanel.add(cancelButton); |
203 |
|
204 |
// Preview Pane
|
205 |
m_prevArea = new JTextArea("The Quick Brown Fox..."); |
206 |
m_prevArea.setSize(200, 200); |
207 |
m_prevArea.setEditable(false);
|
208 |
previewPanel.add(m_prevArea); |
209 |
paintPreviewArea(true);
|
210 |
|
211 |
// Add Stuff to Main
|
212 |
main.add(fontPanel, BorderLayout.NORTH);
|
213 |
main.add(previewPanel, BorderLayout.CENTER);
|
214 |
main.add(buttonPanel, BorderLayout.SOUTH);
|
215 |
|
216 |
// getContentPane().add(main);
|
217 |
// pack();
|
218 |
// set the position of the dialog.
|
219 |
// setLocationRelativeTo(c);
|
220 |
} |
221 |
|
222 |
/**
|
223 |
* Handles the list of fonts and the changes
|
224 |
*/
|
225 |
public void itemStateChanged(ItemEvent iEv) { |
226 |
if (iEv.getItem() == m_plainBox && iEv.getStateChange() == ItemEvent.SELECTED) { |
227 |
m_boldBox.setEnabled(false);
|
228 |
m_italicBox.setEnabled(false);
|
229 |
} |
230 |
if (iEv.getItem() == m_plainBox &&
|
231 |
iEv.getStateChange() == ItemEvent.DESELECTED) {
|
232 |
m_boldBox.setEnabled(true);
|
233 |
m_italicBox.setEnabled(true);
|
234 |
} |
235 |
paintPreviewArea(true);
|
236 |
} |
237 |
|
238 |
/**
|
239 |
* Handles the changes in the font size ComboBox
|
240 |
*/
|
241 |
public void valueChanged(ListSelectionEvent listEv) { |
242 |
paintPreviewArea(true);
|
243 |
} |
244 |
|
245 |
private void paintPreviewArea(boolean repaint) { |
246 |
if (m_prevArea != null) { |
247 |
Object sizeSelection = m_sizeBox.getSelectedItem();
|
248 |
Object familySelection = m_fontList.getSelectedValue();
|
249 |
|
250 |
int size = Integer.parseInt(sizeSelection.toString()); |
251 |
String family = familySelection.toString();
|
252 |
int style = m_plainBox.isSelected() ? Font.PLAIN : |
253 |
((m_boldBox.isSelected() ? Font.BOLD : 0) + |
254 |
(m_italicBox.isSelected() ? Font.ITALIC : 0)); |
255 |
Font newFont = new Font(family, style, size); |
256 |
m_prevArea.setFont(newFont); |
257 |
|
258 |
// Changing the font of the displayed text may cause the
|
259 |
// preview area's preferred size to change.
|
260 |
if (repaint) {
|
261 |
// pack();
|
262 |
} |
263 |
} |
264 |
} |
265 |
} |