PageRenderTime 38ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

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

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