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