Integration of OCCT 6.5.0 from SVN
[occt.git] / samples / java / java / util / JFontChooser.java
1
2 //Title:        FontChooser dialog
3 //Version:
4 //Copyright:    Copyright (c) 1998
5 //Author:       LamaSoft
6 //Company:
7 //Description:
8
9 package util;
10
11 import javax.swing.*;
12 import java.awt.*;
13 import java.awt.event.*;
14 import java.util.*;
15 import javax.swing.event.*;
16 import javax.swing.border.*;
17 import java.awt.font.*;
18 import java.awt.geom.*;
19
20
21 /**
22  * A simple FontChooser dialog that implements similar functionality to
23  * the JFileChooser, JOptionPane and JColorChooser components provided
24  * with Swing.<p>
25  * Upon initialization, the JFontChooser polls the system for all available
26  * fonts, initializes the various JList components to the values of the
27  * default font and provides a preview of the font. As options are changed/selected
28  * the preview is updated to display the current font.<p>
29  * JFileChooser can either be created and added to a GUI as a typical
30  * JComponent or it can display a JDialog using the {@link #showDialog(Component, String) showDialog}
31  * method (just like the <b>JFileChooser</b> component). Objects
32  * that extend JFontChooser should use the {@link #acceptSelection() acceptSelection} and
33  * {@link #cancelSelection() cancelSelection} methods to determine either
34  * an accept or cancel selection.<p>
35  * <i>Example:</i>
36  * <blockquote>
37  * <samp>
38  * JFontChooser chooser = new JFontChooser(new Font("Arial", Font.BOLD, 12));
39  * if (chooser.showDialog(this, "Choose a font...") == JFontChooser.ACCEPT_OPTION) {
40  *      Font f = chooser.getSelectedFont();
41  *      // Process font here...
42  * }
43  * </samp></blockquote>
44  * <p>
45  */
46
47 public class JFontChooser extends JComponent implements ActionListener, ListSelectionListener {
48         
49         private Font font;
50         private JList fontNames, fontSizes, fontStyles;
51         private JTextField currentSize;
52         private JButton okay, cancel;
53         private Font[] availableFonts;
54         private JFontPreviewPanel preview;
55         private JDialog dialog;
56         private int returnValue;
57
58         /**
59          * Value returned by {@link #showDialog(Component, String) showDialog} upon an error.
60          */
61         public static final int ERROR_OPTION=0;
62
63         /**
64          * Value returned by {@link #showDialog(Component, String) showDialog} upon a selected font.
65          */
66         public static final int ACCEPT_OPTION=2;
67
68         /**
69          * Value returned by {@link #showDialog(Component, String) showDialog} upon a cancel.
70          */
71         public static final int CANCEL_OPTION=4;
72         
73         /**
74          * Constructs a new JFontChooser component initialized to the supplied font object.
75          * @see com.lamasoft.JFontChooser#showDialog(Component, String)
76          */
77         public JFontChooser(Font font) {
78                 super();
79                 this.font = font;
80                 setup();
81         }
82         
83         private void setup() {
84                 setLayout(new BorderLayout());
85                 
86                 Font[] fontList = GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts();
87                 Vector fonts = new Vector(1, 1);
88                 Vector names = new Vector(1, 1);
89                 for (int i = 0; i < fontList.length; i++) {
90                         String fontName = fontList[i].getFamily();
91                         if (! names.contains(fontName)) {
92                                 names.addElement(fontName);
93                                 fonts.addElement(fontList[i]);
94                         }
95                 }
96                 availableFonts = new Font[fonts.size()];
97                 for (int i = 0; i < fonts.size(); i++) 
98                         availableFonts[i] = (Font) fonts.elementAt(i);
99                 fontNames = new JList(names);
100                 JScrollPane fontNamesScroll = new JScrollPane(fontNames);
101                 fontNames.addListSelectionListener(this);
102                 
103                 Object[] styles = {"Regular", "Bold", "Italic", "BoldItalic"};
104                 fontStyles = new JList(styles);
105                 JScrollPane fontStylesScroll = new JScrollPane(fontStyles);
106                 fontStyles.setSelectedIndex(0);
107                 fontStyles.addListSelectionListener(this);
108                 
109                 String[] sizes = new String[69];
110                 for (int i = 3; i < 72; i++)
111                         sizes[i - 3] = (new Integer(i + 1)).toString();
112                 fontSizes = new JList(sizes);
113                 JScrollPane fontSizesScroll = new JScrollPane(fontSizes);
114                 fontSizes.addListSelectionListener(this);
115                 
116                 currentSize = new JTextField(5);
117                 currentSize.setText((new Integer(font.getSize())).toString());
118                 currentSize.addActionListener(this);
119                 
120                 GridBagLayout g2 = new GridBagLayout();
121                 GridBagConstraints c2 = new GridBagConstraints();
122                 JPanel sizePane = new JPanel(g2);
123                 c2.gridx = 0;
124                 c2.gridy = 0;
125                 c2.insets = new Insets(2, 5, 2, 5);
126                 c2.anchor = c2.WEST;
127                 sizePane.add(currentSize);
128                 g2.setConstraints(currentSize, c2);
129                 
130                 sizePane.add(fontSizesScroll);
131                 c2.gridy++;
132                 c2.fill = c2.HORIZONTAL;
133                 g2.setConstraints(fontSizesScroll, c2);
134                 
135                 preview = new JFontPreviewPanel(this.font);
136                 
137                 okay = new JButton("Ok");
138                 okay.addActionListener(this);
139                 cancel = new JButton("Cancel");
140                 cancel.addActionListener(this);
141                 
142                 GridBagLayout g = new GridBagLayout();
143                 GridBagConstraints c = new GridBagConstraints();
144                 JPanel top = new JPanel(g);
145                 c.anchor = c.WEST;
146                 c.fill = c.VERTICAL;
147                 c.insets = new Insets(2, 5, 2, 5);
148                 c.gridx = 0;
149                 c.gridy = 0;
150                 top.add(fontNamesScroll);
151                 g.setConstraints(fontNamesScroll, c);
152                 c.gridx++;
153                 top.add(fontStylesScroll);
154                 g.setConstraints(fontStylesScroll, c);
155                 c.gridx++;
156                 top.add(sizePane);
157                 g.setConstraints(sizePane, c);
158                 
159                 add("North", top);
160                 add("Center", preview);
161                 
162                 JPanel buttons = new JPanel(new FlowLayout(FlowLayout.RIGHT));
163                 
164                 buttons.add(okay);
165                 buttons.add(cancel);
166                 
167                 add("South", buttons);
168                 
169                 fontSizes.setSelectedValue((new Integer(font.getSize())).toString(), true);
170                 fontNames.setSelectedValue(font.getFamily(), true);
171                 if (font.getStyle() == Font.PLAIN)
172                         fontStyles.setSelectedValue("Regular", false);
173                 else if (font.getStyle() == Font.ITALIC)
174                         fontStyles.setSelectedValue("Italic", false);
175                 else if (font.getStyle() == Font.BOLD)
176                         fontStyles.setSelectedValue("Bold", false);
177                 else if (font.getStyle() == (Font.BOLD | Font.ITALIC))
178                         fontStyles.setSelectedValue("BoldItalic", false);
179         }
180         
181         private void updateFont(Font f) {
182                 this.font = f;
183                 preview.setFont(this.font);
184         }
185         
186         private void updateFontSize(int size) {
187                 updateFont(font.deriveFont((new Integer(size)).floatValue()));
188         }
189         
190         private void updateFontStyle(int style) {
191                 updateFont(font.deriveFont(style));
192         }
193         
194         /**
195          * Returns the currently selected font. Typically called after receipt
196          * of an ACCEPT_OPTION (using the {@link #showDialog(Component, String) showDialog} option) or from within the
197          * approveSelection method (using the component option).
198          * @return java.awt.Font A font class that represents the currently selected font.
199          */
200         public Font getSelectedFont() {
201                 return font;
202         }
203         
204         /**
205          * Processes action events from the okay and cancel buttons
206          * as well as the current size TextField. 
207          */
208         public void actionPerformed(ActionEvent e) {
209                 if (e.getSource() == okay) {
210                         returnValue = ACCEPT_OPTION;
211                         if (dialog != null)
212                                 dialog.setVisible(false);
213                         acceptSelection();
214                         
215                 }
216                 if (e.getSource() == cancel) {
217                         returnValue = CANCEL_OPTION;
218                         if (dialog != null)
219                                 dialog.setVisible(false);
220                         cancelSelection();
221                 }
222                 if (e.getSource() == currentSize) {
223                         fontSizes.setSelectedValue(currentSize.getText(), true);
224                 }
225         }
226         
227         /**
228          * Processes events received from the various JList objects.
229          */
230         public void valueChanged(ListSelectionEvent e) {
231                 if (e.getSource() == fontNames) {
232                         Font f = availableFonts[fontNames.getSelectedIndex()];
233                         f = new Font(f.getFontName(), font.getStyle(), font.getSize());
234                         updateFont(f);
235                 }
236                 if (e.getSource() == fontSizes) {
237                         currentSize.setText((String) fontSizes.getSelectedValue());
238                         updateFontSize((new Integer(currentSize.getText())).intValue());
239                 }
240                 if (e.getSource() == fontStyles) {
241                         int style = Font.PLAIN;
242                         String selection = (String) fontStyles.getSelectedValue();
243                         if (selection.equals("Regular"))
244                                 style = Font.PLAIN;
245                         if (selection.equals("Bold"))
246                                 style = Font.BOLD;
247                         if (selection.equals("Italic"))
248                                 style = Font.ITALIC;
249                         if (selection.equals("BoldItalic"))
250                                 style = (Font.BOLD | Font.ITALIC);
251                         updateFontStyle(style);
252                 }
253         }
254         
255         /**
256          * Pops up a Font chooser dialog with the supplied <i>title</i>, centered
257          * about the component <i>parent</i>.
258          * @return int An integer that equates to the static variables <i>ERROR_OPTION</i>, <i>ACCEPT_OPTION</i> or <i>CANCEL_OPTION</i>.
259          */
260         public int showDialog(Component parent, String title) {
261                 returnValue = ERROR_OPTION;
262                 Frame frame = parent instanceof Frame ? (Frame) parent : (Frame)SwingUtilities.getAncestorOfClass(Frame.class, parent);
263                 dialog = new JDialog(frame, title, true);
264                 dialog.getContentPane().add("Center", this);
265                 dialog.pack();
266                 dialog.setLocationRelativeTo(parent);
267                 dialog.show();
268                 return returnValue;
269         }
270         
271         /**
272          * This method is called when the user presses the okay button, selecting
273          * the currently displayed font. Children of JFontChooser should override this
274          * method to process this event.
275          */
276         public void acceptSelection() {
277         }
278          
279         /**
280          * This method is called when the user presses the cancel button. Children of
281          * JFontChooser should override this method to process this event.
282          */
283         public void cancelSelection() {
284         }
285 }
286
287
288                 
289 class JFontPreviewPanel extends JPanel {
290         
291         private Font font;
292         
293         public JFontPreviewPanel(Font f) {
294                 super();
295                 setFont(f);
296                 setBorder(new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED), "Preview"));
297         }
298         
299         public void setFont(Font f) {
300                 this.font = f;
301                 repaint();
302         }
303         
304         public void update(Graphics g) {
305                 paintComponent(g);
306                 paintBorder(g);
307         }
308         
309         public void paintComponent(Graphics g) {
310                 Image osi = createImage(getSize().width, getSize().height);
311                 Graphics osg = osi.getGraphics();
312                 osg.setFont(this.font);
313                 Rectangle2D bounds = font.getStringBounds(font.getFontName(), 0, font.getFontName().length(), new FontRenderContext(null, true, false));
314                 int width = (new Double(bounds.getWidth())).intValue();
315                 int height = (new Double(bounds.getHeight())).intValue();
316                 osg.drawString(font.getFontName(), 5, (((getSize().height - height) / 2) + height));
317                 
318                 g.drawImage(osi, 0, 0, this);
319         }
320         
321         public Dimension getPreferredSize() {
322                 return new Dimension(getSize().width, 75);
323         }
324         
325         public Dimension getMinimumSize() {
326                 return getPreferredSize();
327         }
328 }
329
330
331