PageRenderTime 44ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

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

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