001: /*
002: * WbFontPicker.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.io.Serializable;
017: import javax.swing.JDialog;
018: import javax.swing.JOptionPane;
019: import javax.swing.JPanel;
020: import javax.swing.SwingUtilities;
021: import javax.swing.UIManager;
022: import workbench.gui.WbSwingUtilities;
023: import workbench.resource.ResourceMgr;
024:
025: /**
026: *
027: * @author support@sql-workbench.net
028: */
029: public class WbFontPicker extends JPanel implements Serializable {
030: private Font selectedFont;
031: private boolean monospacedOnly = false;
032: private boolean allowFontReset = false;
033:
034: /** Creates new form WbFontPicker */
035: public WbFontPicker() {
036: initComponents();
037: this .setAllowFontReset(false);
038: }
039:
040: public void setAllowFontReset(boolean flag) {
041: this .allowFontReset = flag;
042: this .resetButton.setVisible(flag);
043: this .resetButton.setEnabled(flag);
044: if (flag) {
045: this .resetButton.setIcon(ResourceMgr.getImage("Delete"));
046: }
047: }
048:
049: public void setListMonospacedOnly(boolean flag) {
050: this .monospacedOnly = flag;
051: }
052:
053: /** This method is called from within the constructor to
054: * initialize the form.
055: * WARNING: Do NOT modify this code. The content of this method is
056: * always regenerated by the Form Editor.
057: */
058: // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
059: private void initComponents() {
060: java.awt.GridBagConstraints gridBagConstraints;
061:
062: fontName = new javax.swing.JLabel();
063: selectFontButton = new FlatButton();
064: resetButton = new FlatButton();
065:
066: setLayout(new java.awt.GridBagLayout());
067:
068: fontName.setText("jLabel1");
069: fontName.setBorder(javax.swing.BorderFactory
070: .createCompoundBorder(javax.swing.BorderFactory
071: .createEtchedBorder(),
072: javax.swing.BorderFactory.createEmptyBorder(1,
073: 5, 1, 5)));
074: fontName.setMaximumSize(new java.awt.Dimension(45, 22));
075: fontName.setMinimumSize(new java.awt.Dimension(45, 22));
076: fontName.setPreferredSize(new java.awt.Dimension(45, 22));
077: gridBagConstraints = new java.awt.GridBagConstraints();
078: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
079: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
080: gridBagConstraints.weightx = 1.0;
081: gridBagConstraints.weighty = 1.0;
082: gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 5);
083: add(fontName, gridBagConstraints);
084:
085: selectFontButton.setText("...");
086: selectFontButton.setMaximumSize(new java.awt.Dimension(22, 22));
087: selectFontButton.setMinimumSize(new java.awt.Dimension(22, 22));
088: selectFontButton
089: .setPreferredSize(new java.awt.Dimension(22, 22));
090: selectFontButton
091: .addMouseListener(new java.awt.event.MouseAdapter() {
092: public void mouseClicked(
093: java.awt.event.MouseEvent evt) {
094: selectFontButtonMouseClicked(evt);
095: }
096: });
097: gridBagConstraints = new java.awt.GridBagConstraints();
098: gridBagConstraints.gridx = 2;
099: gridBagConstraints.gridy = 0;
100: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
101: add(selectFontButton, gridBagConstraints);
102:
103: resetButton.setToolTipText(ResourceMgr
104: .getDescription("LblResetFont"));
105: resetButton.setMaximumSize(new java.awt.Dimension(22, 22));
106: resetButton.setMinimumSize(new java.awt.Dimension(22, 22));
107: resetButton.setPreferredSize(new java.awt.Dimension(22, 22));
108: resetButton
109: .addActionListener(new java.awt.event.ActionListener() {
110: public void actionPerformed(
111: java.awt.event.ActionEvent evt) {
112: resetButtonActionPerformed(evt);
113: }
114: });
115: gridBagConstraints = new java.awt.GridBagConstraints();
116: gridBagConstraints.gridx = 1;
117: gridBagConstraints.gridy = 0;
118: gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 3);
119: add(resetButton, gridBagConstraints);
120: }// </editor-fold>//GEN-END:initComponents
121:
122: private void resetButtonActionPerformed(
123: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_resetButtonActionPerformed
124: this .setSelectedFont(null);
125: }//GEN-LAST:event_resetButtonActionPerformed
126:
127: private void selectFontButtonMouseClicked(
128: java.awt.event.MouseEvent evt)//GEN-FIRST:event_selectFontButtonMouseClicked
129: {//GEN-HEADEREND:event_selectFontButtonMouseClicked
130: WbFontChooser chooser = new WbFontChooser(monospacedOnly,
131: allowFontReset);
132: chooser.setSelectedFont(getSelectedFont());
133: Dimension d = new Dimension(320, 240);
134: chooser.setSize(d);
135: chooser.setPreferredSize(d);
136:
137: JOptionPane option = new JOptionPane(chooser,
138: JOptionPane.PLAIN_MESSAGE, JOptionPane.OK_CANCEL_OPTION);
139: JDialog dialog = option.createDialog(this , ResourceMgr
140: .getString("TxtWindowTitleChooseFont"));
141: dialog.pack();
142: WbSwingUtilities.center(dialog, SwingUtilities
143: .getWindowAncestor(this ));
144: dialog.setVisible(true);
145: dialog.dispose();
146:
147: if (chooser.isFontReset()) {
148: this .setSelectedFont(null);
149: return;
150: }
151:
152: Object value = option.getValue();
153: if (value == null)
154: return;
155: Font result = null;
156: if (value instanceof Integer) {
157: int answer = ((Integer) value).intValue();
158: if (answer == JOptionPane.OK_OPTION) {
159: result = chooser.getSelectedFont();
160: }
161: }
162: if (result != null) {
163: this .setSelectedFont(result);
164: }
165:
166: }//GEN-LAST:event_selectFontButtonMouseClicked
167:
168: public Font getSelectedFont() {
169: return this .selectedFont;
170: }
171:
172: public void setSelectedFont(Font f) {
173: this .selectedFont = f;
174: if (f == null) {
175: Font df = UIManager.getDefaults().getFont("Label.font");
176: this .fontName.setFont(df);
177: this .fontName.setText(ResourceMgr
178: .getString("LblDefaultIndicator"));
179: } else {
180: this .fontName.setFont(f);
181: this .fontName.setText(f.getFontName() + ", " + f.getSize());
182: }
183: }
184:
185: // Variables declaration - do not modify//GEN-BEGIN:variables
186: private javax.swing.JLabel fontName;
187: private javax.swing.JButton resetButton;
188: private javax.swing.JButton selectFontButton;
189: // End of variables declaration//GEN-END:variables
190:
191: }
|