PageRenderTime 34ms CodeModel.GetById 2ms app.highlight 27ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-5-pre1/org/gjt/sp/jedit/gui/FontSelectorDialog.java

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