PageRenderTime 223ms CodeModel.GetById 206ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-2-pre4/org/gjt/sp/jedit/gui/FontSelector.java

#
Java | 416 lines | 300 code | 61 blank | 55 comment | 29 complexity | 8ae4d3ca10c325ae93331916409fb872 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.*;
 34import org.gjt.sp.util.Log;
 35//}}}
 36
 37//{{{ FontSelector class
 38/**
 39 * A font chooser widget.
 40 * @author Slava Pestov
 41 * @version $Id: FontSelector.java 4775 2003-06-11 20:33:37Z spestov $
 42 */
 43public class FontSelector extends JButton
 44{
 45	//{{{ FontSelector constructor
 46	public FontSelector(Font font)
 47	{
 48		setFont(font);
 49
 50		updateText();
 51
 52		setRequestFocusEnabled(false);
 53
 54		addActionListener(new ActionHandler());
 55	} //}}}
 56
 57	//{{{ updateText() method
 58	private void updateText()
 59	{
 60		Font font = getFont();
 61		String styleString;
 62		switch(font.getStyle())
 63		{
 64		case Font.PLAIN:
 65			styleString = jEdit.getProperty("font-selector.plain");
 66			break;
 67		case Font.BOLD:
 68			styleString = jEdit.getProperty("font-selector.bold");
 69			break;
 70		case Font.ITALIC:
 71			styleString = jEdit.getProperty("font-selector.italic");
 72			break;
 73		case Font.BOLD | Font.ITALIC:
 74			styleString = jEdit.getProperty("font-selector.bolditalic");
 75			break;
 76		default:
 77			styleString = "UNKNOWN!!!???";
 78			break;
 79		}
 80
 81		setText(font.getName() + " " + font.getSize() + " " + styleString);
 82	} //}}}
 83
 84	//{{{ ActionHandler class
 85	class ActionHandler implements ActionListener
 86	{
 87		public void actionPerformed(ActionEvent evt)
 88		{
 89			Font font;
 90
 91			JDialog dialog = GUIUtilities.getParentDialog(FontSelector.this);
 92			if(dialog == null)
 93			{
 94				font = new FontSelectorDialog(
 95					JOptionPane.getFrameForComponent(
 96					FontSelector.this),getFont())
 97					.getSelectedFont();
 98			}
 99			else
100			{
101				font = new FontSelectorDialog(dialog,getFont())
102					.getSelectedFont();
103			}
104
105			if(font != null)
106			{
107				setFont(font);
108				updateText();
109			}
110		}
111	} //}}}
112} //}}}
113
114//{{{ FontSelectorDialog class
115class FontSelectorDialog extends EnhancedDialog
116{
117	//{{{ FontSelectorDialog constructor
118	public FontSelectorDialog(Frame parent, Font font)
119	{
120		super(parent,jEdit.getProperty("font-selector.title"),true);
121		init(font);
122	} //}}}
123
124	//{{{ FontSelectorDialog constructor
125	public FontSelectorDialog(Dialog parent, Font font)
126	{
127		super(parent,jEdit.getProperty("font-selector.title"),true);
128		init(font);
129	} //}}}
130
131	//{{{ ok() method
132	public void ok()
133	{
134		isOK = true;
135		dispose();
136	} //}}}
137
138	//{{{ cancel() method
139	public void cancel()
140	{
141		dispose();
142	} //}}}
143
144	//{{{ getSelectedFont() method
145	public Font getSelectedFont()
146	{
147		if(!isOK)
148			return null;
149
150		int size;
151		try
152		{
153			size = Integer.parseInt(sizeField.getText());
154		}
155		catch(Exception e)
156		{
157			size = 12;
158		}
159
160		return new Font(familyField.getText(),styleList
161			.getSelectedIndex(),size);
162	} //}}}
163
164	//{{{ Private members
165
166	//{{{ Instance variables
167	private boolean isOK;
168	private JTextField familyField;
169	private JList familyList;
170	private JTextField sizeField;
171	private JList sizeList;
172	private JTextField styleField;
173	private JList styleList;
174	private JLabel preview;
175	private JButton ok;
176	private JButton cancel;
177	//}}}
178
179	/**
180	 * For some reason the default Java fonts show up in the
181	 * list with .bold, .bolditalic, and .italic extensions.
182	 */
183	private static final String[] HIDEFONTS = {
184		".bold",
185		".italic"
186	};
187
188	//{{{ init() method
189	private void init(Font font)
190	{
191		JPanel content = new JPanel(new BorderLayout());
192		content.setBorder(new EmptyBorder(12,12,12,12));
193		setContentPane(content);
194
195		JPanel listPanel = new JPanel(new GridLayout(1,3,6,6));
196
197		String[] fonts;
198		try
199		{
200			fonts = getFontList();
201		}
202		catch(Exception e)
203		{
204			Log.log(Log.ERROR,this,"Broken Java implementation!");
205			/* Log.log(Log.ERROR,this,"Using deprecated Toolkit.getFontList()"); */
206			Log.log(Log.ERROR,this,e);
207
208			/* fonts = getToolkit().getFontList(); */
209			fonts = new String[] { "Broken Java implementation!" };
210		}
211
212		JPanel familyPanel = createTextFieldAndListPanel(
213			"font-selector.family",
214			familyField = new JTextField(),
215			familyList = new JList(fonts));
216		listPanel.add(familyPanel);
217
218		String[] sizes = { "9", "10", "12", "14", "16", "18", "24" };
219		JPanel sizePanel = createTextFieldAndListPanel(
220			"font-selector.size",
221			sizeField = new JTextField(),
222			sizeList = new JList(sizes));
223		listPanel.add(sizePanel);
224
225		String[] styles = {
226			jEdit.getProperty("font-selector.plain"),
227			jEdit.getProperty("font-selector.bold"),
228			jEdit.getProperty("font-selector.italic"),
229			jEdit.getProperty("font-selector.bolditalic")
230		};
231
232		JPanel stylePanel = createTextFieldAndListPanel(
233			"font-selector.style",
234			styleField = new JTextField(),
235			styleList = new JList(styles));
236		styleField.setEditable(false);
237		listPanel.add(stylePanel);
238
239		familyList.setSelectedValue(font.getFamily(),true);
240		familyField.setText(font.getFamily());
241		sizeList.setSelectedValue(String.valueOf(font.getSize()),true);
242		sizeField.setText(String.valueOf(font.getSize()));
243		styleList.setSelectedIndex(font.getStyle());
244		styleField.setText((String)styleList.getSelectedValue());
245
246		ListHandler listHandler = new ListHandler();
247		familyList.addListSelectionListener(listHandler);
248		sizeList.addListSelectionListener(listHandler);
249		styleList.addListSelectionListener(listHandler);
250
251		content.add(BorderLayout.NORTH,listPanel);
252
253		preview = new JLabel(jEdit.getProperty("font-selector.long-text"));
254		preview.setBorder(new TitledBorder(jEdit.getProperty(
255			"font-selector.preview")));
256
257		updatePreview();
258
259		Dimension prefSize = preview.getPreferredSize();
260		prefSize.height = 50;
261		preview.setPreferredSize(prefSize);
262
263		content.add(BorderLayout.CENTER,preview);
264
265		JPanel buttons = new JPanel();
266		buttons.setLayout(new BoxLayout(buttons,BoxLayout.X_AXIS));
267		buttons.setBorder(new EmptyBorder(12,0,0,0));
268		buttons.add(Box.createGlue());
269
270		ok = new JButton(jEdit.getProperty("common.ok"));
271		ok.addActionListener(new ActionHandler());
272		getRootPane().setDefaultButton(ok);
273		buttons.add(ok);
274
275		buttons.add(Box.createHorizontalStrut(6));
276
277		cancel = new JButton(jEdit.getProperty("common.cancel"));
278		cancel.addActionListener(new ActionHandler());
279		buttons.add(cancel);
280
281		buttons.add(Box.createGlue());
282
283		content.add(BorderLayout.SOUTH,buttons);
284
285		pack();
286		setLocationRelativeTo(getParent());
287		show();
288	} //}}}
289
290	//{{{ getFontList() method
291	private String[] getFontList()
292	{
293		String[] nameArray = GraphicsEnvironment
294			.getLocalGraphicsEnvironment()
295			.getAvailableFontFamilyNames();
296		Vector nameVector = new Vector(nameArray.length);
297
298		for(int i = 0, j; i < nameArray.length; i++)
299		{
300			for(j = 0; j < HIDEFONTS.length; j++)
301			{
302				if(nameArray[i].indexOf(HIDEFONTS[j]) >= 0)
303					break;
304			}
305
306			if(j == HIDEFONTS.length)
307				nameVector.addElement(nameArray[i]);
308		}
309
310		String[] _array = new String[nameVector.size()];
311		nameVector.copyInto(_array);
312		return _array;
313	} //}}}
314
315	//{{{ createTextFieldAndListPanel() method
316	private JPanel createTextFieldAndListPanel(String label,
317		JTextField textField, JList list)
318	{
319		GridBagLayout layout = new GridBagLayout();
320		JPanel panel = new JPanel(layout);
321
322		GridBagConstraints cons = new GridBagConstraints();
323		cons.gridx = cons.gridy = 0;
324		cons.gridwidth = cons.gridheight = 1;
325		cons.fill = GridBagConstraints.BOTH;
326		cons.weightx = 1.0f;
327
328		JLabel _label = new JLabel(jEdit.getProperty(label));
329		layout.setConstraints(_label,cons);
330		panel.add(_label);
331
332		cons.gridy = 1;
333		Component vs = Box.createVerticalStrut(6);
334		layout.setConstraints(vs,cons);
335		panel.add(vs);
336
337		cons.gridy = 2;
338		layout.setConstraints(textField,cons);
339		panel.add(textField);
340
341		cons.gridy = 3;
342		vs = Box.createVerticalStrut(6);
343		layout.setConstraints(vs,cons);
344		panel.add(vs);
345
346		cons.gridy = 4;
347		cons.gridheight = GridBagConstraints.REMAINDER;
348		cons.weighty = 1.0f;
349		JScrollPane scroller = new JScrollPane(list);
350		layout.setConstraints(scroller,cons);
351		panel.add(scroller);
352
353		return panel;
354	} //}}}
355
356	//{{{ updatePreview() method
357	private void updatePreview()
358	{
359		String family = familyField.getText();
360		int size;
361		try
362		{
363			size = Integer.parseInt(sizeField.getText());
364		}
365		catch(Exception e)
366		{
367			size = 12;
368		}
369		int style = styleList.getSelectedIndex();
370
371		preview.setFont(new Font(family,style,size));
372	} //}}}
373
374	//}}}
375
376	//{{{ ActionHandler class
377	class ActionHandler implements ActionListener
378	{
379		public void actionPerformed(ActionEvent evt)
380		{
381			if(evt.getSource() == ok)
382				ok();
383			else if(evt.getSource() == cancel)
384				cancel();
385		}
386	} //}}}
387
388	//{{{ ListHandler class
389	class ListHandler implements ListSelectionListener
390	{
391		public void valueChanged(ListSelectionEvent evt)
392		{
393			Object source = evt.getSource();
394			if(source == familyList)
395			{
396				String family = (String)familyList.getSelectedValue();
397				if(family != null)
398					familyField.setText(family);
399			}
400			else if(source == sizeList)
401			{
402				String size = (String)sizeList.getSelectedValue();
403				if(size != null)
404					sizeField.setText(size);
405			}
406			else if(source == styleList)
407			{
408				String style = (String)styleList.getSelectedValue();
409				if(style != null)
410					styleField.setText(style);
411			}
412
413			updatePreview();
414		}
415	} //}}}
416} //}}}