001: /*
002: * WbFontChooser.java
003: *
004: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
005: *
006: * Copyright 2002-2008, Thomas Kellerer
007: * No part of this code maybe reused without the permission of the author
008: *
009: * To contact the author please send an email to: support@sql-workbench.net
010: *
011: */
012: package workbench.gui.components;
013:
014: import java.awt.Dimension;
015: import java.awt.Font;
016: import java.awt.FontMetrics;
017: import java.awt.GraphicsEnvironment;
018: import javax.swing.DefaultListModel;
019: import javax.swing.JComponent;
020: import javax.swing.JDialog;
021: import javax.swing.JOptionPane;
022: import javax.swing.SwingUtilities;
023: import workbench.gui.WbSwingUtilities;
024: import workbench.resource.ResourceMgr;
025: import workbench.util.StringUtil;
026:
027: /**
028: *
029: * @author support@sql-workbench.net
030: */
031: public class WbFontChooser extends javax.swing.JPanel {
032: private boolean updateing;
033: private boolean fontReset = false;
034:
035: public WbFontChooser(boolean monospacedOnly) {
036: this (monospacedOnly, false);
037: }
038:
039: public WbFontChooser(boolean monospacedOnly, boolean allowReset) {
040: initComponents();
041: resetButton.setVisible(allowReset);
042: resetButton.setEnabled(allowReset);
043: fillFontList(monospacedOnly);
044: }
045:
046: public void setSelectedFont(Font aFont) {
047: this .updateing = true;
048: try {
049: if (aFont != null) {
050: String name = aFont.getFamily();
051: String size = Integer.toString(aFont.getSize());
052: int style = aFont.getStyle();
053:
054: this .fontNameList.setSelectedValue(name, true);
055: this .fontSizeComboBox.setSelectedItem(size);
056: this .boldCheckBox
057: .setSelected((style & Font.BOLD) == Font.BOLD);
058: this .italicCheckBox
059: .setSelected((style & Font.ITALIC) == Font.ITALIC);
060: fontReset = false;
061: } else {
062: this .fontNameList.clearSelection();
063: this .boldCheckBox.setSelected(false);
064: this .italicCheckBox.setSelected(false);
065: }
066: } catch (Exception e) {
067: }
068: this .updateing = false;
069: this .updateFontDisplay();
070: }
071:
072: public boolean isFontReset() {
073: return this .fontReset;
074: }
075:
076: public Font getSelectedFont() {
077: String fontName = (String) this .fontNameList.getSelectedValue();
078: if (fontName == null)
079: return null;
080: int size = StringUtil
081: .getIntValue((String) this .fontSizeComboBox
082: .getSelectedItem());
083: int style = Font.PLAIN;
084: if (this .italicCheckBox.isSelected())
085: style = style | Font.ITALIC;
086: if (this .boldCheckBox.isSelected())
087: style = style | Font.BOLD;
088:
089: Font f = new Font(fontName, style, size);
090: return f;
091: }
092:
093: public static Font chooseFont(JComponent owner, Font defaultFont,
094: boolean monospacedOnly, boolean allowReset) {
095: WbFontChooser chooser = new WbFontChooser(monospacedOnly,
096: allowReset);
097: if (defaultFont != null)
098: chooser.setSelectedFont(defaultFont);
099: Dimension d = new Dimension(320, 240);
100: chooser.setSize(d);
101: chooser.setPreferredSize(d);
102:
103: JOptionPane option = new JOptionPane(chooser,
104: JOptionPane.PLAIN_MESSAGE, JOptionPane.OK_CANCEL_OPTION);
105: //int answer = JOptionPane.showConfirmDialog(owner, chooser, ResourceMgr.getString("TxtWindowTitleChooseFont"), JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
106: JDialog dialog = option.createDialog(owner, ResourceMgr
107: .getString("TxtWindowTitleChooseFont"));
108: dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
109:
110: dialog.pack();//setSize(320,240);
111: WbSwingUtilities.center(dialog, SwingUtilities
112: .getWindowAncestor(owner));
113: dialog.setVisible(true);
114: Object value = option.getValue();
115: if (value == null)
116: return null;
117: Font result = null;
118: if (value instanceof Integer) {
119: int answer = ((Integer) value).intValue();
120: if (answer == JOptionPane.OK_OPTION) {
121: result = chooser.getSelectedFont();
122: }
123: }
124: return result;
125: }
126:
127: private void fillFontList(boolean monospacedOnly) {
128: String[] fonts = GraphicsEnvironment
129: .getLocalGraphicsEnvironment()
130: .getAvailableFontFamilyNames();
131: DefaultListModel model = new DefaultListModel();
132:
133: for (int i = 0; i < fonts.length; i++) {
134: if (monospacedOnly) {
135: Font f = new Font(fonts[i], Font.PLAIN, 10);
136: FontMetrics fm = getFontMetrics(f);
137: int iWidth = fm.charWidth('i');
138: int mWidth = fm.charWidth('M');
139: if (iWidth != mWidth)
140: continue;
141: }
142: model.addElement(fonts[i]);
143: }
144: this .fontNameList.setModel(model);
145: }
146:
147: private void updateFontDisplay() {
148: if (!this .updateing) {
149: synchronized (this ) {
150: this .updateing = true;
151: try {
152: Font f = this .getSelectedFont();
153: if (f != null) {
154: this .sampleLabel.setFont(f);
155: this .sampleLabel
156: .setText((String) this .fontNameList
157: .getSelectedValue());
158: } else {
159: this .sampleLabel.setText("");
160: }
161: } finally {
162: this .updateing = false;
163: }
164: }
165: }
166: }
167:
168: /** This method is called from within the constructor to
169: * initialize the form.
170: * WARNING: Do NOT modify this code. The content of this method is
171: * always regenerated by the Form Editor.
172: */
173: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
174: private void initComponents() {
175: java.awt.GridBagConstraints gridBagConstraints;
176:
177: fontSizeComboBox = new javax.swing.JComboBox();
178: jScrollPane1 = new javax.swing.JScrollPane();
179: fontNameList = new javax.swing.JList();
180: boldCheckBox = new javax.swing.JCheckBox();
181: italicCheckBox = new javax.swing.JCheckBox();
182: sampleLabel = new javax.swing.JLabel();
183: resetButton = new javax.swing.JButton();
184:
185: setMinimumSize(new java.awt.Dimension(320, 240));
186: setPreferredSize(new java.awt.Dimension(320, 240));
187: setLayout(new java.awt.GridBagLayout());
188:
189: fontSizeComboBox.setModel(new javax.swing.DefaultComboBoxModel(
190: new String[] { "8", "9", "10", "11", "12", "13", "14",
191: "15", "16", "17", "18", "19", "20", "21", "22",
192: "23", "24" }));
193: fontSizeComboBox
194: .setMaximumSize(new java.awt.Dimension(200, 22));
195: fontSizeComboBox.setMinimumSize(new java.awt.Dimension(30, 22));
196: fontSizeComboBox
197: .setPreferredSize(new java.awt.Dimension(40, 22));
198: fontSizeComboBox
199: .addItemListener(new java.awt.event.ItemListener() {
200: public void itemStateChanged(
201: java.awt.event.ItemEvent evt) {
202: fontSizeComboBoxupdateFontDisplay(evt);
203: }
204: });
205: gridBagConstraints = new java.awt.GridBagConstraints();
206: gridBagConstraints.gridx = 1;
207: gridBagConstraints.gridy = 0;
208: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
209: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
210: gridBagConstraints.weightx = 0.5;
211: gridBagConstraints.insets = new java.awt.Insets(0, 8, 0, 1);
212: add(fontSizeComboBox, gridBagConstraints);
213:
214: fontNameList.setModel(new javax.swing.AbstractListModel() {
215: String[] strings = { "Item 1", "Item 2", "Item 3",
216: "Item 4", "Item 5" };
217:
218: public int getSize() {
219: return strings.length;
220: }
221:
222: public Object getElementAt(int i) {
223: return strings[i];
224: }
225: });
226: fontNameList
227: .setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
228: fontNameList
229: .addListSelectionListener(new javax.swing.event.ListSelectionListener() {
230: public void valueChanged(
231: javax.swing.event.ListSelectionEvent evt) {
232: fontNameListValueChanged(evt);
233: }
234: });
235: jScrollPane1.setViewportView(fontNameList);
236:
237: gridBagConstraints = new java.awt.GridBagConstraints();
238: gridBagConstraints.gridx = 0;
239: gridBagConstraints.gridy = 0;
240: gridBagConstraints.gridheight = 4;
241: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
242: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
243: gridBagConstraints.weightx = 0.5;
244: gridBagConstraints.weighty = 0.5;
245: add(jScrollPane1, gridBagConstraints);
246:
247: boldCheckBox.setText(ResourceMgr.getString("LblBold"));
248: boldCheckBox.addItemListener(new java.awt.event.ItemListener() {
249: public void itemStateChanged(java.awt.event.ItemEvent evt) {
250: boldCheckBoxupdateFontDisplay(evt);
251: }
252: });
253: gridBagConstraints = new java.awt.GridBagConstraints();
254: gridBagConstraints.gridx = 1;
255: gridBagConstraints.gridy = 2;
256: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
257: gridBagConstraints.insets = new java.awt.Insets(0, 5, 0, 0);
258: add(boldCheckBox, gridBagConstraints);
259:
260: italicCheckBox.setText(ResourceMgr.getString("LblItalic"));
261: italicCheckBox
262: .addItemListener(new java.awt.event.ItemListener() {
263: public void itemStateChanged(
264: java.awt.event.ItemEvent evt) {
265: italicCheckBoxupdateFontDisplay(evt);
266: }
267: });
268: gridBagConstraints = new java.awt.GridBagConstraints();
269: gridBagConstraints.gridx = 1;
270: gridBagConstraints.gridy = 1;
271: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
272: gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 0);
273: add(italicCheckBox, gridBagConstraints);
274:
275: sampleLabel.setBorder(javax.swing.BorderFactory
276: .createCompoundBorder(javax.swing.BorderFactory
277: .createTitledBorder("Preview"),
278: javax.swing.BorderFactory.createEmptyBorder(1,
279: 1, 5, 1)));
280: sampleLabel.setMaximumSize(new java.awt.Dimension(43, 100));
281: sampleLabel.setMinimumSize(new java.awt.Dimension(48, 60));
282: sampleLabel.setPreferredSize(new java.awt.Dimension(48, 60));
283: gridBagConstraints = new java.awt.GridBagConstraints();
284: gridBagConstraints.gridx = 0;
285: gridBagConstraints.gridy = 4;
286: gridBagConstraints.gridwidth = 2;
287: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
288: add(sampleLabel, gridBagConstraints);
289:
290: resetButton.setText(ResourceMgr.getString("LblResetFont"));
291: resetButton.setToolTipText(ResourceMgr
292: .getDescription("LblResetFont"));
293: resetButton
294: .addActionListener(new java.awt.event.ActionListener() {
295: public void actionPerformed(
296: java.awt.event.ActionEvent evt) {
297: resetButtonActionPerformed(evt);
298: }
299: });
300: gridBagConstraints = new java.awt.GridBagConstraints();
301: gridBagConstraints.gridx = 1;
302: gridBagConstraints.gridy = 3;
303: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
304: gridBagConstraints.insets = new java.awt.Insets(10, 7, 0, 0);
305: add(resetButton, gridBagConstraints);
306: }// </editor-fold>//GEN-END:initComponents
307:
308: private void resetButtonActionPerformed(
309: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_resetButtonActionPerformed
310: setSelectedFont(null);
311: fontReset = true;
312: }//GEN-LAST:event_resetButtonActionPerformed
313:
314: private void fontNameListValueChanged(
315: javax.swing.event.ListSelectionEvent evt)//GEN-FIRST:event_fontNameListValueChanged
316: {//GEN-HEADEREND:event_fontNameListValueChanged
317: updateFontDisplay();
318: }//GEN-LAST:event_fontNameListValueChanged
319:
320: private void italicCheckBoxupdateFontDisplay(
321: java.awt.event.ItemEvent evt)//GEN-FIRST:event_italicCheckBoxupdateFontDisplay
322: {//GEN-HEADEREND:event_italicCheckBoxupdateFontDisplay
323: updateFontDisplay();
324: }//GEN-LAST:event_italicCheckBoxupdateFontDisplay
325:
326: private void boldCheckBoxupdateFontDisplay(
327: java.awt.event.ItemEvent evt)//GEN-FIRST:event_boldCheckBoxupdateFontDisplay
328: {//GEN-HEADEREND:event_boldCheckBoxupdateFontDisplay
329: updateFontDisplay();
330: }//GEN-LAST:event_boldCheckBoxupdateFontDisplay
331:
332: private void fontSizeComboBoxupdateFontDisplay(
333: java.awt.event.ItemEvent evt)//GEN-FIRST:event_fontSizeComboBoxupdateFontDisplay
334: {//GEN-HEADEREND:event_fontSizeComboBoxupdateFontDisplay
335: updateFontDisplay();
336: }//GEN-LAST:event_fontSizeComboBoxupdateFontDisplay
337:
338: // Variables declaration - do not modify//GEN-BEGIN:variables
339: public javax.swing.JCheckBox boldCheckBox;
340: public javax.swing.JList fontNameList;
341: public javax.swing.JComboBox fontSizeComboBox;
342: public javax.swing.JCheckBox italicCheckBox;
343: public javax.swing.JScrollPane jScrollPane1;
344: public javax.swing.JButton resetButton;
345: public javax.swing.JLabel sampleLabel;
346: // End of variables declaration//GEN-END:variables
347:
348: }
|