41bc0cb83dd9f938cbe818d6e0ea5e74ac5c49f8
[debian/jabref.git] / src / java / net / sf / jabref / FontSelectorDialog.java
1 package net.sf.jabref;
2 /*
3   Taken from JpicEdt
4 modified slightly by nizar batada for JabRef
5
6  EepicViewFactory.java - February 11, 2002 - jPicEdt, a picture editor for LaTeX.
7  copyright (C) 1999-2002 Sylvain Reynal
8  Portions copyright (C) 2000, 2001 Slava Pestov
9  Portions copyright (C) 1999 Jason Ginchereau
10
11  D\uFFFDpartement de Physique
12  Ecole Nationale Sup\uFFFDrieure de l'Electronique et de ses Applications (ENSEA)
13  6, avenue du Ponceau
14  F-95014 CERGY CEDEX
15
16  Tel : +33 130 736 245
17  Fax : +33 130 736 667
18  e-mail : reynal@ensea.fr
19  jPicEdt web page : http://trashx.ensea.fr/jpicedt/
20
21  This program is free software; you can redistribute it and/or
22  modify it under the terms of the GNU General Public License
23  as published by the Free Software Foundation; either version 2
24  of the License, or any later version.
25
26  This program is distributed in the hope that it will be useful,
27  but WITHOUT ANY WARRANTY; without even the implied warranty of
28  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29  GNU General Public License for more details.
30
31  You should have received a copy of the GNU General Public License
32  along with this program; if not, write to the Free Software
33  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
34  */
35
36 import java.awt.event.*;
37 import java.awt.*;
38 import java.util.Vector;
39 import javax.swing.border.*;
40 import javax.swing.event.*;
41 import javax.swing.*;
42
43 /**
44  * A font chooser widget.
45  * @author Slava Pestov (jEdit), Sylvain Reynal
46  * @since jpicedt 1.3.2.beta-9
47  * @version $Id: FontSelectorDialog.java,v 1.6 2004/02/27 23:28:41 mortenalver Exp $
48  * <p>
49  * $Log: FontSelectorDialog.java,v $
50  * Revision 1.6  2004/02/27 23:28:41  mortenalver
51  * Some code tidying, no effect on behaviour (hopefully)
52  *
53  * Revision 1.5  2004/02/24 23:30:18  mortenalver
54  * Added more translations, and started work on a Replace string feature
55  *
56  * Revision 1.4  2004/02/17 09:14:02  mortenalver
57  * Similar update in FontSelector preview.
58  *
59  * Revision 1.3  2004/02/17 07:35:22  mortenalver
60  * Experimenting with antialiasing in table.
61  *
62  * Revision 1.2  2003/12/14 23:48:02  mortenalver
63  * .
64  *
65  * Revision 1.1  2003/11/07 22:18:07  nbatada
66  * modified it slightly from initial version
67  *
68  * Revision 1.1  2003/11/07 22:14:34  nbatada
69  * modified it from initial version
70  *
71  * Revision 1.4  2003/11/02 01:51:06  reynal
72  * Cleaned-up i18n labels
73  *
74  * Revision 1.3  2003/08/31 22:05:40  reynal
75  *
76  * Enhanced class interface for some widgets.
77  *
78
79  */
80
81 class FontSelector extends JButton {
82
83         static final String PLAIN="plain";
84         static final String BOLD="bold";
85         static final String BOLD_ITALIC="bold-italic";
86         static final String ITALIC="italic";
87
88         /** init with a default font */
89         public FontSelector(){
90                 this(new Font("SansSerif", Font.PLAIN, 10));
91         }
92
93         /** init with the given font */
94         public FontSelector(Font font){
95                 setFont(font);
96                 setRequestFocusEnabled(false);
97                 addActionListener(new ActionHandler());
98         }
99
100         public void setFont(Font font){
101                 super.setFont(font);
102                 updateText();
103         }
104
105         /**
106          * update button's text content from the current button's font.
107          */
108         private void updateText(){
109                 Font font = getFont();
110                 String styleString;
111                 switch(font.getStyle()){
112                 case Font.PLAIN:
113                         styleString = PLAIN;
114                         break;
115                 case Font.BOLD:
116                         styleString = BOLD;
117                         break;
118                 case Font.ITALIC:
119                         styleString = ITALIC;
120                         break;
121                 case Font.BOLD | Font.ITALIC:
122                         styleString = BOLD_ITALIC;
123                         break;
124                 default:
125                         styleString = "UNKNOWN!!!???";
126                         break;
127                 }
128
129                 setText(font.getFamily() + " " + font.getSize() + " " + styleString);
130         }
131
132         /**
133          * button's action-listener ; open a FontSelectorDialog
134          */
135         class ActionHandler implements ActionListener {
136                 public void actionPerformed(ActionEvent evt) {
137                         Font font = new FontSelectorDialog(FontSelector.this,getFont()).getSelectedFont();
138                         if(font != null){
139                                 setFont(font);
140                         }
141                 }
142         }
143
144 }
145
146
147
148 ///////////////////////////////////////////////////////////////////////////////
149
150 /**
151  *
152  */
153 public class FontSelectorDialog extends JDialog {
154
155         /**
156          *
157          */
158         static final String PLAIN="plain";
159         static final String BOLD="bold";
160         static final String BOLD_ITALIC="bold-italic";
161         static final String ITALIC="italic";
162
163         public FontSelectorDialog(Component comp, Font font) {
164
165             //super(JOptionPane.getFrameForComponent(comp),jpicedt.Localizer.currentLocalizer().get("widget.FontSelector"),true); //
166             super(JOptionPane.getFrameForComponent(comp),Globals.lang("FontSelector"),true); //
167                 JPanel content = new JPanel(new BorderLayout());
168                 content.setBorder(new EmptyBorder(12,12,12,12));
169                 setContentPane(content);
170
171                 JPanel listPanel = new JPanel(new GridLayout(1,3,6,6));
172
173                 JPanel familyPanel = createTextFieldAndListPanel(
174                                                                  Globals.lang("Font Family"),
175                                                                  familyField = new JTextField(),
176                                                                  familyList = new JList(getFontList()));
177                 listPanel.add(familyPanel);
178
179                 String[] sizes = { "9", "10", "12", "14", "16", "18", "24" };
180                 JPanel sizePanel = createTextFieldAndListPanel(
181                                                                Globals.lang("Font Size"),
182                                        sizeField = new JTextField(),
183                                        sizeList = new JList(sizes));
184                 listPanel.add(sizePanel);
185
186                 String[] styles = {PLAIN,BOLD,ITALIC,BOLD_ITALIC};
187
188                 JPanel stylePanel = createTextFieldAndListPanel(
189                                                                 Globals.lang("Font Style"),
190                                         styleField = new JTextField(),
191                                         styleList = new JList(styles));
192                 styleField.setEditable(false);
193                 listPanel.add(stylePanel);
194
195                 familyList.setSelectedValue(font.getFamily(),true);
196                 familyField.setText(font.getFamily());
197                 sizeList.setSelectedValue(String.valueOf(font.getSize()),true);
198                 sizeField.setText(String.valueOf(font.getSize()));
199                 styleList.setSelectedIndex(font.getStyle());
200                 styleField.setText((String)styleList.getSelectedValue());
201
202                 ListHandler listHandler = new ListHandler();
203                 familyList.addListSelectionListener(listHandler);
204                 sizeList.addListSelectionListener(listHandler);
205                 styleList.addListSelectionListener(listHandler);
206
207                 content.add(BorderLayout.NORTH,listPanel);
208
209                 //preview = new JLabel("Font Preview");
210                 
211                 /* --------------------------------------------------------
212                    |  Experimental addition by Morten Alver. I want to    |
213                    |  enable antialiasing in the preview field, since I'm |
214                    |  working on introducing this in the table view.      |
215                    -------------------------------------------------------- */
216                 preview = new JLabel(Globals.lang("Font Preview")) {
217                         public void paint(Graphics g) {
218                             Graphics2D g2 = (Graphics2D)g;
219                             g2.setRenderingHint
220                                 (RenderingHints.KEY_ANTIALIASING,
221                                  RenderingHints.VALUE_ANTIALIAS_ON);
222                             super.paint(g2);
223                         }
224
225                     };
226
227
228
229                 preview.setBorder(new TitledBorder(Globals.lang("Font Preview")));
230
231                 updatePreview();
232
233                 Dimension prefSize = preview.getPreferredSize();
234                 prefSize.height = 50;
235                 preview.setPreferredSize(prefSize);
236
237                 content.add(BorderLayout.CENTER,preview);
238
239                 JPanel buttons = new JPanel();
240                 buttons.setLayout(new BoxLayout(buttons,BoxLayout.X_AXIS));
241                 buttons.setBorder(new EmptyBorder(12,0,0,0));
242                 buttons.add(Box.createGlue());
243
244                 ok = new JButton(Globals.lang("OK"));
245                 ok.addActionListener(new ActionHandler());
246                 getRootPane().setDefaultButton(ok);
247                 buttons.add(ok);
248
249                 buttons.add(Box.createHorizontalStrut(6));
250
251                 cancel = new JButton(Globals.lang("Cancel"));
252                 cancel.addActionListener(new ActionHandler());
253                 buttons.add(cancel);
254
255                 buttons.add(Box.createGlue());
256
257                 content.add(BorderLayout.SOUTH,buttons);
258
259                 pack();
260                 setLocationRelativeTo(JOptionPane.getFrameForComponent(comp));
261                 show();
262         }
263
264         public void ok(){
265                 isOK = true;
266                 dispose();
267         }
268
269         public void cancel(){
270                 dispose();
271         }
272
273         public Font getSelectedFont(){
274                 if(!isOK)
275                         return null;
276
277                 int size;
278                 try{
279                         size = Integer.parseInt(sizeField.getText());
280                 }
281                 catch(Exception e){
282                         size = 14;
283                 }
284
285                 return new Font(familyField.getText(),styleList.getSelectedIndex(),size);
286         }
287
288         // private members
289         private boolean isOK;
290         private JTextField familyField;
291         private JList familyList;
292         private JTextField sizeField;
293         private JList sizeList;
294         private JTextField styleField;
295         private JList styleList;
296         private JLabel preview;
297         private JButton ok;
298         private JButton cancel;
299
300         /**
301          * For some reason the default Java fonts show up in the
302          * list with .bold, .bolditalic, and .italic extensions.
303          */
304         private static final String[] HIDEFONTS = {".bold",".italic"};
305
306         // [pending] from GeneralCustomizer :
307         // GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames()
308         private String[] getFontList(){
309                 try{
310                         Class GEClass = Class.forName("java.awt.GraphicsEnvironment");
311                         Object GEInstance = GEClass.getMethod("getLocalGraphicsEnvironment", null).invoke(null, null);
312
313                         String[] nameArray = (String[])GEClass.getMethod("getAvailableFontFamilyNames", null).invoke(GEInstance, null);
314                         Vector nameVector = new Vector(nameArray.length);
315
316                         for(int i = 0, j; i < nameArray.length; i++){
317                                 for(j = 0; j < HIDEFONTS.length; j++){
318                                         if(nameArray[i].indexOf(HIDEFONTS[j]) >= 0) break;
319                                 }
320
321                                 if(j == HIDEFONTS.length) nameVector.addElement(nameArray[i]);
322                         }
323
324                         String[] _array = new String[nameVector.size()];
325                         nameVector.copyInto(_array);
326                         return _array;
327                 }
328                 catch(Exception ex){
329                     return null;//return Toolkit.getDefaultToolkit().getFontList();
330                 }
331         }
332
333         private JPanel createTextFieldAndListPanel(String label,JTextField textField, JList list){
334                 GridBagLayout layout = new GridBagLayout();
335                 JPanel panel = new JPanel(layout);
336
337                 GridBagConstraints cons = new GridBagConstraints();
338                 cons.gridx = cons.gridy = 0;
339                 cons.gridwidth = cons.gridheight = 1;
340                 cons.fill = GridBagConstraints.BOTH;
341                 cons.weightx = 1.0f;
342
343                 JLabel _label = new JLabel(label);
344                 layout.setConstraints(_label,cons);
345                 panel.add(_label);
346
347                 cons.gridy = 1;
348                 Component vs = Box.createVerticalStrut(6);
349                 layout.setConstraints(vs,cons);
350                 panel.add(vs);
351
352                 cons.gridy = 2;
353                 layout.setConstraints(textField,cons);
354                 panel.add(textField);
355
356                 cons.gridy = 3;
357                 vs = Box.createVerticalStrut(6);
358                 layout.setConstraints(vs,cons);
359                 panel.add(vs);
360
361                 cons.gridy = 4;
362                 cons.gridheight = GridBagConstraints.REMAINDER;
363                 cons.weighty = 1.0f;
364                 JScrollPane scroller = new JScrollPane(list);
365                 layout.setConstraints(scroller,cons);
366                 panel.add(scroller);
367
368                 return panel;
369         }
370
371         private void updatePreview(){
372                 String family = familyField.getText();
373                 int size;
374                 try{
375                         size = Integer.parseInt(sizeField.getText());
376                 }
377                 catch(Exception e){
378                         size = 14;
379                 }
380                 int style = styleList.getSelectedIndex();
381                 preview.setFont(new Font(family,style,size));
382         }
383
384         class ActionHandler implements ActionListener {
385                 public void actionPerformed(ActionEvent evt){
386                         if(evt.getSource() == ok)ok();
387                         else if(evt.getSource() == cancel)cancel();
388                 }
389         }
390
391         class ListHandler implements ListSelectionListener {
392                 public void valueChanged(ListSelectionEvent evt)
393                 {
394                         Object source = evt.getSource();
395                         if(source == familyList) {
396                                 String family = (String)familyList.getSelectedValue();
397                                 if(family != null)
398                                         familyField.setText(family);
399                         }
400                         else if(source == sizeList) {
401                                 String size = (String)sizeList.getSelectedValue();
402                                 if(size != null)
403                                         sizeField.setText(size);
404                         }
405                         else if(source == styleList) {
406                                 String style = (String)styleList.getSelectedValue();
407                                 if(style != null)
408                                         styleField.setText(style);
409                         }
410                         updatePreview();
411                 }
412         }
413     /*public static void main(String args[])
414         {
415             Font font = new FontSelectorDialog(null,new Font("Times",Font.PLAIN,12)).getSelectedFont();
416
417         }
418     */
419 }