PageRenderTime 18ms CodeModel.GetById 2ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-1-pre5/org/gjt/sp/jedit/gui/FontSelector.java

#
Java | 375 lines | 268 code | 56 blank | 51 comment | 27 complexity | d3bd8ac10324cab56922cdd0a7920861 MD5 | raw file
  1/*
  2 * FontSelector.java - Font selector
  3 * :tabSize=8:indentSize=8:noTabs=false:
  4 * :folding=explicit:collapseFolds=1:
  5 *
  6 * Copyright (C) 2000, 2001 Slava Pestov
  7 * Portions copyright (C) 1999 Jason Ginchereau
  8 *
  9 * This program is free software; you can redistribute it and/or
 10 * modify it under the terms of the GNU General Public License
 11 * as published by the Free Software Foundation; either version 2
 12 * of the License, or any later version.
 13 *
 14 * This program is distributed in the hope that it will be useful,
 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 17 * GNU General Public License for more details.
 18 *
 19 * You should have received a copy of the GNU General Public License
 20 * along with this program; if not, write to the Free Software
 21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 22 */
 23
 24package org.gjt.sp.jedit.gui;
 25
 26//{{{ Imports
 27import java.awt.event.*;
 28import java.awt.*;
 29import java.util.Vector;
 30import javax.swing.border.*;
 31import javax.swing.event.*;
 32import javax.swing.*;
 33import org.gjt.sp.jedit.jEdit;
 34//}}}
 35
 36//{{{ FontSelector class
 37/**
 38 * A font chooser widget.
 39 * @author Slava Pestov
 40 * @version $Id: FontSelector.java 3935 2001-12-11 06:32:54Z spestov $
 41 */
 42public class FontSelector extends JButton
 43{
 44	//{{{ FontSelector constructor
 45	public FontSelector(Font font)
 46	{
 47		setFont(font);
 48
 49		updateText();
 50
 51		setRequestFocusEnabled(false);
 52
 53		addActionListener(new ActionHandler());
 54	} //}}}
 55
 56	//{{{ updateText() method
 57	private void updateText()
 58	{
 59		Font font = getFont();
 60		String styleString;
 61		switch(font.getStyle())
 62		{
 63		case Font.PLAIN:
 64			styleString = jEdit.getProperty("font-selector.plain");
 65			break;
 66		case Font.BOLD:
 67			styleString = jEdit.getProperty("font-selector.bold");
 68			break;
 69		case Font.ITALIC:
 70			styleString = jEdit.getProperty("font-selector.italic");
 71			break;
 72		case Font.BOLD | Font.ITALIC:
 73			styleString = jEdit.getProperty("font-selector.bolditalic");
 74			break;
 75		default:
 76			styleString = "UNKNOWN!!!???";
 77			break;
 78		}
 79
 80		setText(font.getName() + " " + font.getSize() + " " + styleString);
 81	} //}}}
 82
 83	//{{{ ActionHandler class
 84	class ActionHandler implements ActionListener
 85	{
 86		public void actionPerformed(ActionEvent evt)
 87		{
 88			Font font = new FontSelectorDialog(FontSelector.this,getFont())
 89				.getSelectedFont();
 90			if(font != null)
 91			{
 92				setFont(font);
 93				updateText();
 94			}
 95		}
 96	} //}}}
 97} //}}}
 98
 99//{{{ FontSelectorDialog class
100class FontSelectorDialog extends EnhancedDialog
101{
102	//{{{ FontSelectorDialog constructor
103	public FontSelectorDialog(Component comp, Font font)
104	{
105		super(JOptionPane.getFrameForComponent(comp),
106			jEdit.getProperty("font-selector.title"),true);
107
108		JPanel content = new JPanel(new BorderLayout());
109		content.setBorder(new EmptyBorder(12,12,12,12));
110		setContentPane(content);
111
112		JPanel listPanel = new JPanel(new GridLayout(1,3,6,6));
113
114		JPanel familyPanel = createTextFieldAndListPanel(
115			"font-selector.family",
116			familyField = new JTextField(),
117			familyList = new JList(getFontList()));
118		listPanel.add(familyPanel);
119
120		String[] sizes = { "9", "10", "12", "14", "16", "18", "24" };
121		JPanel sizePanel = createTextFieldAndListPanel(
122			"font-selector.size",
123			sizeField = new JTextField(),
124			sizeList = new JList(sizes));
125		listPanel.add(sizePanel);
126
127		String[] styles = {
128			jEdit.getProperty("font-selector.plain"),
129			jEdit.getProperty("font-selector.bold"),
130			jEdit.getProperty("font-selector.italic"),
131			jEdit.getProperty("font-selector.bolditalic")
132		};
133
134		JPanel stylePanel = createTextFieldAndListPanel(
135			"font-selector.style",
136			styleField = new JTextField(),
137			styleList = new JList(styles));
138		styleField.setEditable(false);
139		listPanel.add(stylePanel);
140
141		familyList.setSelectedValue(font.getFamily(),true);
142		familyField.setText(font.getFamily());
143		sizeList.setSelectedValue(String.valueOf(font.getSize()),true);
144		sizeField.setText(String.valueOf(font.getSize()));
145		styleList.setSelectedIndex(font.getStyle());
146		styleField.setText((String)styleList.getSelectedValue());
147
148		ListHandler listHandler = new ListHandler();
149		familyList.addListSelectionListener(listHandler);
150		sizeList.addListSelectionListener(listHandler);
151		styleList.addListSelectionListener(listHandler);
152
153		content.add(BorderLayout.NORTH,listPanel);
154
155		preview = new JLabel(jEdit.getProperty("font-selector.long-text"));
156		preview.setBorder(new TitledBorder(jEdit.getProperty(
157			"font-selector.preview")));
158
159		updatePreview();
160
161		Dimension prefSize = preview.getPreferredSize();
162		prefSize.height = 50;
163		preview.setPreferredSize(prefSize);
164
165		content.add(BorderLayout.CENTER,preview);
166
167		JPanel buttons = new JPanel();
168		buttons.setLayout(new BoxLayout(buttons,BoxLayout.X_AXIS));
169		buttons.setBorder(new EmptyBorder(12,0,0,0));
170		buttons.add(Box.createGlue());
171
172		ok = new JButton(jEdit.getProperty("common.ok"));
173		ok.addActionListener(new ActionHandler());
174		getRootPane().setDefaultButton(ok);
175		buttons.add(ok);
176
177		buttons.add(Box.createHorizontalStrut(6));
178
179		cancel = new JButton(jEdit.getProperty("common.cancel"));
180		cancel.addActionListener(new ActionHandler());
181		buttons.add(cancel);
182
183		buttons.add(Box.createGlue());
184
185		content.add(BorderLayout.SOUTH,buttons);
186
187		pack();
188		setLocationRelativeTo(JOptionPane.getFrameForComponent(comp));
189		show();
190	} //}}}
191
192	//{{{ ok() method
193	public void ok()
194	{
195		isOK = true;
196		dispose();
197	} //}}}
198
199	//{{{ cancel() method
200	public void cancel()
201	{
202		dispose();
203	} //}}}
204
205	//{{{ getSelectedFont() method
206	public Font getSelectedFont()
207	{
208		if(!isOK)
209			return null;
210
211		int size;
212		try
213		{
214			size = Integer.parseInt(sizeField.getText());
215		}
216		catch(Exception e)
217		{
218			size = 12;
219		}
220
221		return new Font(familyField.getText(),styleList
222			.getSelectedIndex(),size);
223	} //}}}
224
225	//{{{ Private members
226
227	//{{{ Instance variables
228	private boolean isOK;
229	private JTextField familyField;
230	private JList familyList;
231	private JTextField sizeField;
232	private JList sizeList;
233	private JTextField styleField;
234	private JList styleList;
235	private JLabel preview;
236	private JButton ok;
237	private JButton cancel;
238	//}}}
239
240	/**
241	 * For some reason the default Java fonts show up in the
242	 * list with .bold, .bolditalic, and .italic extensions.
243	 */
244	private static final String[] HIDEFONTS = {
245		".bold",
246		".italic"
247	};
248
249	//{{{ getFontList() method
250	private String[] getFontList()
251	{
252		String[] nameArray = GraphicsEnvironment
253			.getLocalGraphicsEnvironment()
254			.getAvailableFontFamilyNames();
255		Vector nameVector = new Vector(nameArray.length);
256
257		for(int i = 0, j; i < nameArray.length; i++)
258		{
259			for(j = 0; j < HIDEFONTS.length; j++)
260			{
261				if(nameArray[i].indexOf(HIDEFONTS[j]) >= 0)
262					break;
263			}
264
265			if(j == HIDEFONTS.length)
266				nameVector.addElement(nameArray[i]);
267		}
268
269		String[] _array = new String[nameVector.size()];
270		nameVector.copyInto(_array);
271		return _array;
272	} //}}}
273
274	//{{{ createTextFieldAndListPanel() method
275	private JPanel createTextFieldAndListPanel(String label,
276		JTextField textField, JList list)
277	{
278		GridBagLayout layout = new GridBagLayout();
279		JPanel panel = new JPanel(layout);
280
281		GridBagConstraints cons = new GridBagConstraints();
282		cons.gridx = cons.gridy = 0;
283		cons.gridwidth = cons.gridheight = 1;
284		cons.fill = GridBagConstraints.BOTH;
285		cons.weightx = 1.0f;
286
287		JLabel _label = new JLabel(jEdit.getProperty(label));
288		layout.setConstraints(_label,cons);
289		panel.add(_label);
290
291		cons.gridy = 1;
292		Component vs = Box.createVerticalStrut(6);
293		layout.setConstraints(vs,cons);
294		panel.add(vs);
295
296		cons.gridy = 2;
297		layout.setConstraints(textField,cons);
298		panel.add(textField);
299
300		cons.gridy = 3;
301		vs = Box.createVerticalStrut(6);
302		layout.setConstraints(vs,cons);
303		panel.add(vs);
304
305		cons.gridy = 4;
306		cons.gridheight = GridBagConstraints.REMAINDER;
307		cons.weighty = 1.0f;
308		JScrollPane scroller = new JScrollPane(list);
309		layout.setConstraints(scroller,cons);
310		panel.add(scroller);
311
312		return panel;
313	} //}}}
314
315	//{{{ updatePreview() method
316	private void updatePreview()
317	{
318		String family = familyField.getText();
319		int size;
320		try
321		{
322			size = Integer.parseInt(sizeField.getText());
323		}
324		catch(Exception e)
325		{
326			size = 12;
327		}
328		int style = styleList.getSelectedIndex();
329
330		preview.setFont(new Font(family,style,size));
331	} //}}}
332
333	//}}}
334
335	//{{{ ActionHandler class
336	class ActionHandler implements ActionListener
337	{
338		public void actionPerformed(ActionEvent evt)
339		{
340			if(evt.getSource() == ok)
341				ok();
342			else if(evt.getSource() == cancel)
343				cancel();
344		}
345	} //}}}
346
347	//{{{ ListHandler class
348	class ListHandler implements ListSelectionListener
349	{
350		public void valueChanged(ListSelectionEvent evt)
351		{
352			Object source = evt.getSource();
353			if(source == familyList)
354			{
355				String family = (String)familyList.getSelectedValue();
356				if(family != null)
357					familyField.setText(family);
358			}
359			else if(source == sizeList)
360			{
361				String size = (String)sizeList.getSelectedValue();
362				if(size != null)
363					sizeField.setText(size);
364			}
365			else if(source == styleList)
366			{
367				String style = (String)styleList.getSelectedValue();
368				if(style != null)
369					styleField.setText(style);
370			}
371
372			updatePreview();
373		}
374	} //}}}
375}