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