KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > plaf > FontChooser


1 /******************************************************************************
2  * The contents of this file are subject to the Compiere License Version 1.1
3  * ("License"); You may not use this file except in compliance with the License
4  * You may obtain a copy of the License at http://www.compiere.org/license.html
5  * Software distributed under the License is distributed on an "AS IS" basis,
6  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
7  * the specific language governing rights and limitations under the License.
8  * The Original Code is Compiere ERP & CRM Business Solution
9  * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
10  * Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke, parts
11  * created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
12  * Contributor(s): ______________________________________.
13  *****************************************************************************/

14 package org.compiere.plaf;
15
16 import java.awt.BorderLayout JavaDoc;
17 import java.awt.Dialog JavaDoc;
18 import java.awt.Dimension JavaDoc;
19 import java.awt.FlowLayout JavaDoc;
20 import java.awt.Font JavaDoc;
21 import java.awt.GraphicsEnvironment JavaDoc;
22 import java.awt.GridBagConstraints JavaDoc;
23 import java.awt.GridBagLayout JavaDoc;
24 import java.awt.Insets JavaDoc;
25 import java.awt.event.ActionEvent JavaDoc;
26 import java.awt.event.ActionListener JavaDoc;
27 import java.util.Arrays JavaDoc;
28 import java.util.ResourceBundle JavaDoc;
29
30 import javax.swing.BorderFactory JavaDoc;
31 import javax.swing.JDialog JavaDoc;
32 import javax.swing.JTextArea JavaDoc;
33
34 import org.compiere.swing.CButton;
35 import org.compiere.swing.CComboBox;
36 import org.compiere.swing.CLabel;
37 import org.compiere.swing.CPanel;
38
39 /**
40  * Font Chooser Dialog
41  *
42  * @author Jorg Janke
43  * @version $Id: FontChooser.java,v 1.5 2003/09/27 11:08:53 jjanke Exp $
44  */

45 public class FontChooser extends JDialog JavaDoc implements ActionListener JavaDoc
46 {
47     /**
48      * Show Dialog with initial font and return selected font
49      * @param owner Base window
50      * @param title Chooser Title
51      * @param initFont initial font
52      * @return selected font
53      */

54     public static Font JavaDoc showDialog (Dialog JavaDoc owner, String JavaDoc title, Font JavaDoc initFont)
55     {
56         Font JavaDoc retValue = initFont;
57         FontChooser fc = new FontChooser(owner, title, initFont);
58         retValue = fc.getFont();
59         fc = null;
60         return retValue;
61     } // showDialog
62

63     /*************************************************************************/
64
65     /**
66      * Constructor
67      *
68      * @param owner Base window
69      * @param title Chooser Title
70      * @param initFont Initial Font
71      */

72     public FontChooser(Dialog JavaDoc owner, String JavaDoc title, Font JavaDoc initFont)
73     {
74         super(owner, title, true);
75         try
76         {
77             jbInit();
78             dynInit();
79             setFont(initFont);
80             CompierePLAF.showCenterScreen(this);
81         }
82         catch(Exception JavaDoc ex)
83         {
84             System.err.println ("FontChooser");
85             ex.printStackTrace();
86         }
87     } // FontChooser
88

89     /**
90      * IDE Constructor
91      */

92     public FontChooser()
93     {
94         this (null, s_res.getString("FontChooser"), null);
95     } // FontChooser
96

97     static ResourceBundle JavaDoc s_res = ResourceBundle.getBundle("org.compiere.plaf.PlafRes");
98
99     /** Static list of Styles */
100     public static FontStyle[] s_list = {
101         new FontStyle(s_res.getString("Plain"), Font.PLAIN),
102         new FontStyle(s_res.getString("Italic"), Font.ITALIC),
103         new FontStyle(s_res.getString("Bold"), Font.BOLD),
104         new FontStyle(s_res.getString("BoldItalic"), Font.BOLD|Font.ITALIC)};
105
106     private Font JavaDoc m_font = super.getFont();
107     private Font JavaDoc m_retFont = null;
108
109     private boolean m_setting = false;
110
111     private CPanel mainPanel = new CPanel();
112     private BorderLayout JavaDoc mainLayout = new BorderLayout JavaDoc();
113     private CPanel selectPanel = new CPanel();
114     private CLabel nameLabel = new CLabel();
115     private CComboBox fontName = new CComboBox();
116     private CLabel sizeLabel = new CLabel();
117     private CLabel styleLabel = new CLabel();
118     private CComboBox fontStyle = new CComboBox();
119     private CComboBox fontSize = new CComboBox();
120     private JTextArea JavaDoc fontTest = new JTextArea JavaDoc();
121     private JTextArea JavaDoc fontInfo = new JTextArea JavaDoc();
122     private GridBagLayout JavaDoc selectLayout = new GridBagLayout JavaDoc();
123     private CPanel confirmPanel = new CPanel();
124     private CButton bCancel = CompierePLAF.getCancelButton();
125     private CButton bOK = CompierePLAF.getOKButton();
126     private FlowLayout JavaDoc confirmLayout = new FlowLayout JavaDoc();
127
128     /**
129      * Static Layout
130      * @throws Exception
131      */

132     private void jbInit() throws Exception JavaDoc
133     {
134         CompiereColor.setBackground(this);
135         mainPanel.setLayout(mainLayout);
136         nameLabel.setText(s_res.getString("Name"));
137         selectPanel.setLayout(selectLayout);
138         sizeLabel.setText(s_res.getString("Size"));
139         styleLabel.setText(s_res.getString("Style"));
140         fontTest.setText(s_res.getString("TestString"));
141         fontTest.setLineWrap(true);
142         fontTest.setWrapStyleWord(true);
143         fontTest.setBackground(CompierePLAF.getFieldBackground_Inactive());
144         fontTest.setBorder(BorderFactory.createLoweredBevelBorder());
145         fontTest.setPreferredSize(new Dimension JavaDoc(220, 100));
146         fontInfo.setText(s_res.getString("FontString"));
147         fontInfo.setLineWrap(true);
148         fontInfo.setWrapStyleWord(true);
149         fontInfo.setBackground(CompierePLAF.getFieldBackground_Inactive());
150         fontInfo.setOpaque(false);
151         fontInfo.setEditable(false);
152         confirmPanel.setLayout(confirmLayout);
153         confirmLayout.setAlignment(FlowLayout.RIGHT);
154         confirmPanel.setOpaque(false);
155         selectPanel.setOpaque(false);
156         getContentPane().add(mainPanel);
157         mainPanel.add(selectPanel, BorderLayout.CENTER);
158         selectPanel.add(nameLabel, new GridBagConstraints JavaDoc(0, 0, 1, 1, 0.0, 0.0
159             ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets JavaDoc(5, 5, 5, 5), 0, 0));
160         selectPanel.add(fontName, new GridBagConstraints JavaDoc(1, 0, 1, 1, 0.0, 0.0
161             ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets JavaDoc(5, 5, 5, 5), 0, 0));
162         selectPanel.add(sizeLabel, new GridBagConstraints JavaDoc(0, 1, 1, 1, 0.0, 0.0
163             ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets JavaDoc(5, 5, 5, 5), 0, 0));
164         selectPanel.add(styleLabel, new GridBagConstraints JavaDoc(0, 2, 1, 1, 0.0, 0.0
165             ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets JavaDoc(5, 5, 5, 5), 0, 0));
166         selectPanel.add(fontStyle, new GridBagConstraints JavaDoc(1, 2, 2, 1, 0.0, 0.0
167             ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets JavaDoc(5, 5, 5, 5), 0, 0));
168         selectPanel.add(fontSize, new GridBagConstraints JavaDoc(1, 1, 2, 1, 0.0, 0.0
169             ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets JavaDoc(5, 5, 5, 5), 0, 0));
170         selectPanel.add(fontTest, new GridBagConstraints JavaDoc(0, 3, 2, 1, 0.0, 0.0
171             ,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets JavaDoc(20, 5, 5, 5), 0, 0));
172         selectPanel.add(fontInfo, new GridBagConstraints JavaDoc(0, 4, 2, 1, 0.0, 0.0
173             ,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets JavaDoc(10, 5, 10, 5), 0, 0));
174         //
175
mainPanel.add(confirmPanel, BorderLayout.SOUTH);
176         confirmPanel.add(bCancel, null);
177         confirmPanel.add(bOK, null);
178         bCancel.addActionListener(this);
179         bOK.addActionListener(this);
180     } // jbInit
181

182     /**
183      * Dynamic Init
184      */

185     private void dynInit()
186     {
187         String JavaDoc[] names = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
188         Arrays.sort(names);
189         for (int i = 0; i < names.length; i++)
190             fontName.addItem(names[i]);
191         fontName.addActionListener(this);
192         //
193
for (int i = 6; i < 32; i++)
194             fontSize.addItem(String.valueOf(i));
195         fontSize.addActionListener(this);
196         //
197
for (int i = 0; i < s_list.length; i++)
198             fontStyle.addItem(s_list[i]);
199         fontStyle.addActionListener(this);
200     } // dynInit
201

202     /**
203      * Set Font - sets font for chooser - not the component font
204      * @param font
205      */

206     public void setFont(Font JavaDoc font)
207     {
208         if (font == null)
209             return;
210     // Log.trace("FontChooser.setFont - " + font.toString());
211
if (m_retFont == null)
212             m_retFont = font;
213         //
214
fontTest.setFont(font);
215         fontInfo.setFont(font);
216         fontInfo.setText(font.toString());
217         //
218
m_setting = true;
219         fontName.setSelectedItem(font.getName());
220         if (!fontName.getSelectedItem().equals(font.getName()))
221             System.err.println("FontChooser.setFont" + fontName.getSelectedItem().toString() + " <> " + font.getName());
222         //
223
fontSize.setSelectedItem(String.valueOf(font.getSize()));
224         if (!fontSize.getSelectedItem().equals(String.valueOf(font.getSize())))
225             System.err.println("FontChooser.setFont" + fontSize.getSelectedItem() + " <> " + font.getSize());
226         // find style
227
for (int i = 0; i < s_list.length; i++)
228             if (s_list[i].getID() == font.getStyle())
229                 fontStyle.setSelectedItem(s_list[i]);
230         if (((FontStyle)fontStyle.getSelectedItem()).getID() != font.getStyle())
231             System.err.println("FontChooser.setFont" + ((FontStyle)fontStyle.getSelectedItem()).getID() + " <> " + font.getStyle());
232         //
233
m_font = font;
234         this.pack();
235         m_setting = false;
236     } // setFont
237

238     /**
239      * Return selected font
240      * @return font
241      */

242     public Font JavaDoc getFont()
243     {
244         return m_retFont;
245     } // getFont
246

247     /**
248      * ActionListener
249      * @param e
250      */

251     public void actionPerformed(ActionEvent JavaDoc e)
252     {
253         if (m_setting)
254             return;
255
256         if (e.getSource() == bOK)
257         {
258             m_retFont = m_font;
259             dispose();
260         }
261
262         else if (e.getSource() == bCancel)
263             dispose();
264
265         else if (e.getSource() == fontName)
266         {
267             String JavaDoc s = fontName.getSelectedItem().toString();
268             m_font = new Font JavaDoc(s, m_font.getStyle(), m_font.getSize());
269         }
270         else if (e.getSource() == fontSize)
271         {
272             String JavaDoc s = fontSize.getSelectedItem().toString();
273             m_font = new Font JavaDoc(m_font.getName(), m_font.getStyle(), Integer.parseInt(s));
274         }
275         else if (e.getSource() == fontStyle)
276         {
277             FontStyle fs = (FontStyle)fontStyle.getSelectedItem();
278             m_font = new Font JavaDoc(m_font.getName(), fs.getID(), m_font.getSize());
279         }
280     // System.out.println("NewFont - " + m_font.toString());
281
setFont(m_font);
282     } // actionPerformed
283
} // FontChooser
284

285 /**
286  * Font Style Value Object
287  */

288 class FontStyle
289 {
290     /**
291      * Create FontStyle
292      * @param name
293      * @param id
294      */

295     public FontStyle(String JavaDoc name, int id)
296     {
297         m_name = name;
298         m_id = id;
299     } // FontStyle
300

301     private String JavaDoc m_name;
302     private int m_id;
303
304     /**
305      * Get Name
306      * @return name
307      */

308     public String JavaDoc toString()
309     {
310         return m_name;
311     } // getName
312

313     /**
314      * Get int value of Font Style
315      * @return id
316      */

317     public int getID()
318     {
319         return m_id;
320     } // getID
321
} // FontStyle
322
Popular Tags