1 | /* |
---|
2 | * $Id: FontDialog.java,v 1.7 2003/08/22 23:02:39 dwd Exp $ |
---|
3 | * |
---|
4 | * This software is provided by NOAA for full, free and open release. It is |
---|
5 | * understood by the recipient/user that NOAA assumes no liability for any |
---|
6 | * errors contained in the code. Although this software is released without |
---|
7 | * conditions or restrictions in its use, it is expected that appropriate |
---|
8 | * credit be given to its author and to the National Oceanic and Atmospheric |
---|
9 | * Administration should the software be included by the recipient as an |
---|
10 | * element in other product development. |
---|
11 | */ |
---|
12 | package gov.noaa.pmel.sgt.swing.prop; |
---|
13 | |
---|
14 | import javax.swing.*; |
---|
15 | import java.awt.*; |
---|
16 | |
---|
17 | /** |
---|
18 | * Edits a <code>Font</code> object. This dialog does not |
---|
19 | * make a copy of the object so changes "Applied" will cause |
---|
20 | * <code>sgt</code> to redraw the plot using the new <code>Font</code> |
---|
21 | * properties unless |
---|
22 | * {@link gov.noaa.pmel.sgt.JPane#setBatch(boolean) batching} |
---|
23 | * has been turned on. |
---|
24 | * |
---|
25 | * <p> Example of <code>FontDialog</code> use: |
---|
26 | * <pre> |
---|
27 | * public void editFont(Font font) { |
---|
28 | * FontDialog fd = new FontDialog(); |
---|
29 | * fd.showDialog(font); |
---|
30 | * } |
---|
31 | * </pre> |
---|
32 | * |
---|
33 | * @author Donald Denbo |
---|
34 | * @version $Revision: 1.7 $, $Date: 2003/08/22 23:02:39 $ |
---|
35 | * @since 2.0 |
---|
36 | */ |
---|
37 | public class FontDialog extends JDialog { |
---|
38 | private int result_; |
---|
39 | private Font font_; |
---|
40 | private String[] fontNames_; |
---|
41 | private int[] styles_ = {Font.PLAIN, Font.BOLD, Font.ITALIC, Font.BOLD|Font.ITALIC}; |
---|
42 | private String[] styleNames_ = {"plain", "bold", "italic", "bold-italic"}; |
---|
43 | /** OK button was selected */ |
---|
44 | public static int OK_RESPONSE = 1; |
---|
45 | /** Cancel button was selected */ |
---|
46 | public static int CANCEL_RESPONSE = 2; |
---|
47 | /** |
---|
48 | * Constructor. |
---|
49 | */ |
---|
50 | public FontDialog(Frame parent) { |
---|
51 | super(parent); |
---|
52 | try { |
---|
53 | jbInit(); |
---|
54 | } catch(Exception ex) { |
---|
55 | ex.printStackTrace(); |
---|
56 | } |
---|
57 | } |
---|
58 | void jbInit() throws Exception { |
---|
59 | //{{INIT_CONTROLS |
---|
60 | getContentPane().setLayout(new BorderLayout(0,0)); |
---|
61 | setSize(450,141); |
---|
62 | setVisible(false); |
---|
63 | buttonPanel.setBorder(etchedBorder1); |
---|
64 | buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER,5,5)); |
---|
65 | italicButton.setMnemonic('0'); |
---|
66 | okButton.setText("OK"); |
---|
67 | okButton.setActionCommand("OK"); |
---|
68 | cancelButton.setText("Cancel"); |
---|
69 | cancelButton.setActionCommand("Cancel"); |
---|
70 | buttonPanel.add(okButton); |
---|
71 | buttonPanel.add(cancelButton); |
---|
72 | //$$ etchedBorder1.move(0,228); |
---|
73 | mainPanel.setLayout(new GridBagLayout()); |
---|
74 | getContentPane().add(mainPanel, "Center"); |
---|
75 | mainPanel.add(fontComboBox, new GridBagConstraints(0,1,1,1,0.0,0.0, |
---|
76 | GridBagConstraints.CENTER,GridBagConstraints.NONE,new Insets(5,5,5,1),0,0)); |
---|
77 | boldButton.setText("Bold"); |
---|
78 | mainPanel.add(boldButton, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0 |
---|
79 | ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(5, 1, 5, 2), 0, 0)); |
---|
80 | italicButton.setText("Italic"); |
---|
81 | mainPanel.add(italicButton, new GridBagConstraints(2, 1, 1, 1, 0.0, 0.0 |
---|
82 | ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(5, 2, 5, 5), 0, 0)); |
---|
83 | italicButton.setFont(new Font("Dialog", Font.BOLD|Font.ITALIC, 12)); |
---|
84 | fontLabel.setText("Font Name in font."); |
---|
85 | mainPanel.add(fontLabel, new GridBagConstraints(0,0,3,1,0.0,0.0, |
---|
86 | GridBagConstraints.CENTER,GridBagConstraints.HORIZONTAL,new Insets(5,5,5,5),0,0)); |
---|
87 | this.getContentPane().add(buttonPanel, BorderLayout.SOUTH); |
---|
88 | setTitle("Select a Mark"); |
---|
89 | //}} |
---|
90 | |
---|
91 | //{{REGISTER_LISTENERS |
---|
92 | SymWindow aSymWindow = new SymWindow(); |
---|
93 | this.addWindowListener(aSymWindow); |
---|
94 | SymAction lSymAction = new SymAction(); |
---|
95 | cancelButton.addActionListener(lSymAction); |
---|
96 | okButton.addActionListener(lSymAction); |
---|
97 | fontComboBox.addActionListener(lSymAction); |
---|
98 | boldButton.addActionListener(lSymAction); |
---|
99 | italicButton.addActionListener(lSymAction); |
---|
100 | //}} |
---|
101 | } |
---|
102 | /** Used internally */ |
---|
103 | public void addNotify() { |
---|
104 | // Record the size of the window prior to calling parents addNotify. |
---|
105 | Dimension d = getSize(); |
---|
106 | |
---|
107 | super.addNotify(); |
---|
108 | |
---|
109 | if (fComponentsAdjusted) |
---|
110 | return; |
---|
111 | |
---|
112 | // Adjust components according to the insets |
---|
113 | Insets ins = getInsets(); |
---|
114 | setSize(ins.left + ins.right + d.width, ins.top + ins.bottom + d.height); |
---|
115 | Component components[] = getContentPane().getComponents(); |
---|
116 | for (int i = 0; i < components.length; i++) { |
---|
117 | Point p = components[i].getLocation(); |
---|
118 | p.translate(ins.left, ins.top); |
---|
119 | components[i].setLocation(p); |
---|
120 | } |
---|
121 | fComponentsAdjusted = true; |
---|
122 | } |
---|
123 | |
---|
124 | // Used for addNotify check. |
---|
125 | boolean fComponentsAdjusted = false; |
---|
126 | /** |
---|
127 | * Constructor. |
---|
128 | */ |
---|
129 | public FontDialog(String title) { |
---|
130 | this(); |
---|
131 | setTitle(title); |
---|
132 | } |
---|
133 | /** |
---|
134 | * Default constructor. |
---|
135 | */ |
---|
136 | public FontDialog() { |
---|
137 | this((Frame)null); |
---|
138 | } |
---|
139 | /** |
---|
140 | * Make the dialog visible |
---|
141 | */ |
---|
142 | public void setVisible(boolean b) { |
---|
143 | if(b) { |
---|
144 | setLocation(50, 50); |
---|
145 | } |
---|
146 | super.setVisible(b); |
---|
147 | } |
---|
148 | |
---|
149 | class SymWindow extends java.awt.event.WindowAdapter { |
---|
150 | public void windowClosing(java.awt.event.WindowEvent event) { |
---|
151 | Object object = event.getSource(); |
---|
152 | if (object == FontDialog.this) |
---|
153 | FontDialog_WindowClosing(event); |
---|
154 | } |
---|
155 | } |
---|
156 | |
---|
157 | void FontDialog_WindowClosing(java.awt.event.WindowEvent event) { |
---|
158 | dispose(); |
---|
159 | } |
---|
160 | |
---|
161 | //{{DECLARE_CONTROLS |
---|
162 | javax.swing.JPanel buttonPanel = new javax.swing.JPanel(); |
---|
163 | javax.swing.JButton okButton = new javax.swing.JButton(); |
---|
164 | javax.swing.JButton cancelButton = new javax.swing.JButton(); |
---|
165 | javax.swing.border.EtchedBorder etchedBorder1 = new javax.swing.border.EtchedBorder(); |
---|
166 | javax.swing.JPanel mainPanel = new javax.swing.JPanel(); |
---|
167 | javax.swing.JComboBox fontComboBox = new javax.swing.JComboBox(); |
---|
168 | javax.swing.JToggleButton boldButton = new javax.swing.JToggleButton(); |
---|
169 | javax.swing.JToggleButton italicButton = new javax.swing.JToggleButton(); |
---|
170 | javax.swing.JLabel fontLabel = new javax.swing.JLabel(); |
---|
171 | //}} |
---|
172 | |
---|
173 | |
---|
174 | class SymAction implements java.awt.event.ActionListener { |
---|
175 | public void actionPerformed(java.awt.event.ActionEvent event) { |
---|
176 | Object object = event.getSource(); |
---|
177 | if (object == cancelButton) |
---|
178 | cancelButton_actionPerformed(event); |
---|
179 | else if (object == okButton) |
---|
180 | okButton_actionPerformed(event); |
---|
181 | else if (object == fontComboBox) |
---|
182 | fontComboBox_actionPerformed(event); |
---|
183 | else if (object == boldButton) |
---|
184 | boldButton_actionPerformed(event); |
---|
185 | else if (object == italicButton) |
---|
186 | italicButton_actionPerformed(event); |
---|
187 | } |
---|
188 | } |
---|
189 | |
---|
190 | void cancelButton_actionPerformed(java.awt.event.ActionEvent event) { |
---|
191 | this.setVisible(false); |
---|
192 | result_ = CANCEL_RESPONSE; |
---|
193 | } |
---|
194 | |
---|
195 | void okButton_actionPerformed(java.awt.event.ActionEvent event) { |
---|
196 | result_ = OK_RESPONSE; |
---|
197 | this.setVisible(false); |
---|
198 | } |
---|
199 | /** |
---|
200 | * Test entry point |
---|
201 | */ |
---|
202 | public static void main(String[] args) { |
---|
203 | FontDialog la = new FontDialog(); |
---|
204 | la.setFont(null); |
---|
205 | la.setTitle("Test Font Dialog"); |
---|
206 | la.setVisible(true); |
---|
207 | } |
---|
208 | /** |
---|
209 | * Show the dialog and wait for a response |
---|
210 | * |
---|
211 | * @param font the font |
---|
212 | * @return result, either CANCEL_RESPONSE or OK_RESPONSE |
---|
213 | */ |
---|
214 | public int showDialog(Font font) { |
---|
215 | // fontNames_ = Toolkit.getDefaultToolkit().getFontList(); |
---|
216 | fontNames_ = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames(); |
---|
217 | for(int i=0; i < fontNames_.length; i++) { |
---|
218 | fontComboBox.addItem(fontNames_[i]); |
---|
219 | } |
---|
220 | setDefaultFont(font); |
---|
221 | result_ = CANCEL_RESPONSE; |
---|
222 | setModal(true); |
---|
223 | super.setVisible(true); |
---|
224 | return result_; |
---|
225 | } |
---|
226 | /** |
---|
227 | * Set the font to be edited |
---|
228 | */ |
---|
229 | public void setDefaultFont(Font font) { |
---|
230 | int index=0; |
---|
231 | font_ = font; |
---|
232 | if(font_ == null) { |
---|
233 | font_ = super.getFont(); |
---|
234 | } |
---|
235 | // |
---|
236 | // setup font combo box |
---|
237 | // |
---|
238 | for(int i=0; i < fontNames_.length; i++) { |
---|
239 | if(font_.getName().equals(fontNames_[i])) { |
---|
240 | index = i; |
---|
241 | break; |
---|
242 | } |
---|
243 | } |
---|
244 | boldButton.setSelected(font_.isBold()); |
---|
245 | italicButton.setSelected(font_.isItalic()); |
---|
246 | fontComboBox.setSelectedIndex(index); |
---|
247 | fontLabel.setText(fontString()); |
---|
248 | fontLabel.setFont(font_); |
---|
249 | } |
---|
250 | /** |
---|
251 | * Create a string representation of the <code>Font</code> |
---|
252 | */ |
---|
253 | public String fontString() { |
---|
254 | int style = (boldButton.isSelected()?1:0) + (italicButton.isSelected()?2:0); |
---|
255 | return font_.getName() + " " + styleNames_[style]; |
---|
256 | } |
---|
257 | /** |
---|
258 | * Get the edited font. |
---|
259 | */ |
---|
260 | public Font getFont() { |
---|
261 | return font_; |
---|
262 | } |
---|
263 | |
---|
264 | void fontComboBox_actionPerformed(java.awt.event.ActionEvent event) { |
---|
265 | updateFont(); |
---|
266 | } |
---|
267 | |
---|
268 | void boldButton_actionPerformed(java.awt.event.ActionEvent event) { |
---|
269 | updateFont(); |
---|
270 | } |
---|
271 | |
---|
272 | void italicButton_actionPerformed(java.awt.event.ActionEvent event) { |
---|
273 | updateFont(); |
---|
274 | } |
---|
275 | |
---|
276 | void updateFont() { |
---|
277 | int style = (boldButton.isSelected()?1:0) + (italicButton.isSelected()?2:0); |
---|
278 | Font font = new Font(fontNames_[fontComboBox.getSelectedIndex()], styles_[style], 12); |
---|
279 | font_ = font; |
---|
280 | fontLabel.setText(fontString()); |
---|
281 | fontLabel.setFont(font_); |
---|
282 | } |
---|
283 | } |
---|