4 modified slightly by nizar batada for JabRef
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
11 D\uFFFDpartement de Physique
12 Ecole Nationale Sup\uFFFDrieure de l'Electronique et de ses Applications (ENSEA)
18 e-mail : reynal@ensea.fr
19 jPicEdt web page : http://trashx.ensea.fr/jpicedt/
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.
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.
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.
36 import java.awt.event.*;
38 import java.util.Vector;
39 import javax.swing.border.*;
40 import javax.swing.event.*;
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 $
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)
53 * Revision 1.5 2004/02/24 23:30:18 mortenalver
54 * Added more translations, and started work on a Replace string feature
56 * Revision 1.4 2004/02/17 09:14:02 mortenalver
57 * Similar update in FontSelector preview.
59 * Revision 1.3 2004/02/17 07:35:22 mortenalver
60 * Experimenting with antialiasing in table.
62 * Revision 1.2 2003/12/14 23:48:02 mortenalver
65 * Revision 1.1 2003/11/07 22:18:07 nbatada
66 * modified it slightly from initial version
68 * Revision 1.1 2003/11/07 22:14:34 nbatada
69 * modified it from initial version
71 * Revision 1.4 2003/11/02 01:51:06 reynal
72 * Cleaned-up i18n labels
74 * Revision 1.3 2003/08/31 22:05:40 reynal
76 * Enhanced class interface for some widgets.
81 class FontSelector extends JButton {
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";
88 /** init with a default font */
89 public FontSelector(){
90 this(new Font("SansSerif", Font.PLAIN, 10));
93 /** init with the given font */
94 public FontSelector(Font font){
96 setRequestFocusEnabled(false);
97 addActionListener(new ActionHandler());
100 public void setFont(Font font){
106 * update button's text content from the current button's font.
108 private void updateText(){
109 Font font = getFont();
111 switch(font.getStyle()){
119 styleString = ITALIC;
121 case Font.BOLD | Font.ITALIC:
122 styleString = BOLD_ITALIC;
125 styleString = "UNKNOWN!!!???";
129 setText(font.getFamily() + " " + font.getSize() + " " + styleString);
133 * button's action-listener ; open a FontSelectorDialog
135 class ActionHandler implements ActionListener {
136 public void actionPerformed(ActionEvent evt) {
137 Font font = new FontSelectorDialog(FontSelector.this,getFont()).getSelectedFont();
148 ///////////////////////////////////////////////////////////////////////////////
153 public class FontSelectorDialog extends JDialog {
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";
163 public FontSelectorDialog(Component comp, Font font) {
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);
171 JPanel listPanel = new JPanel(new GridLayout(1,3,6,6));
173 JPanel familyPanel = createTextFieldAndListPanel(
174 Globals.lang("Font Family"),
175 familyField = new JTextField(),
176 familyList = new JList(getFontList()));
177 listPanel.add(familyPanel);
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);
186 String[] styles = {PLAIN,BOLD,ITALIC,BOLD_ITALIC};
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);
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());
202 ListHandler listHandler = new ListHandler();
203 familyList.addListSelectionListener(listHandler);
204 sizeList.addListSelectionListener(listHandler);
205 styleList.addListSelectionListener(listHandler);
207 content.add(BorderLayout.NORTH,listPanel);
209 //preview = new JLabel("Font Preview");
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;
220 (RenderingHints.KEY_ANTIALIASING,
221 RenderingHints.VALUE_ANTIALIAS_ON);
229 preview.setBorder(new TitledBorder(Globals.lang("Font Preview")));
233 Dimension prefSize = preview.getPreferredSize();
234 prefSize.height = 50;
235 preview.setPreferredSize(prefSize);
237 content.add(BorderLayout.CENTER,preview);
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());
244 ok = new JButton(Globals.lang("OK"));
245 ok.addActionListener(new ActionHandler());
246 getRootPane().setDefaultButton(ok);
249 buttons.add(Box.createHorizontalStrut(6));
251 cancel = new JButton(Globals.lang("Cancel"));
252 cancel.addActionListener(new ActionHandler());
255 buttons.add(Box.createGlue());
257 content.add(BorderLayout.SOUTH,buttons);
260 setLocationRelativeTo(JOptionPane.getFrameForComponent(comp));
269 public void cancel(){
273 public Font getSelectedFont(){
279 size = Integer.parseInt(sizeField.getText());
285 return new Font(familyField.getText(),styleList.getSelectedIndex(),size);
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;
298 private JButton cancel;
301 * For some reason the default Java fonts show up in the
302 * list with .bold, .bolditalic, and .italic extensions.
304 private static final String[] HIDEFONTS = {".bold",".italic"};
306 // [pending] from GeneralCustomizer :
307 // GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames()
308 private String[] getFontList(){
310 Class GEClass = Class.forName("java.awt.GraphicsEnvironment");
311 Object GEInstance = GEClass.getMethod("getLocalGraphicsEnvironment", null).invoke(null, null);
313 String[] nameArray = (String[])GEClass.getMethod("getAvailableFontFamilyNames", null).invoke(GEInstance, null);
314 Vector nameVector = new Vector(nameArray.length);
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;
321 if(j == HIDEFONTS.length) nameVector.addElement(nameArray[i]);
324 String[] _array = new String[nameVector.size()];
325 nameVector.copyInto(_array);
329 return null;//return Toolkit.getDefaultToolkit().getFontList();
333 private JPanel createTextFieldAndListPanel(String label,JTextField textField, JList list){
334 GridBagLayout layout = new GridBagLayout();
335 JPanel panel = new JPanel(layout);
337 GridBagConstraints cons = new GridBagConstraints();
338 cons.gridx = cons.gridy = 0;
339 cons.gridwidth = cons.gridheight = 1;
340 cons.fill = GridBagConstraints.BOTH;
343 JLabel _label = new JLabel(label);
344 layout.setConstraints(_label,cons);
348 Component vs = Box.createVerticalStrut(6);
349 layout.setConstraints(vs,cons);
353 layout.setConstraints(textField,cons);
354 panel.add(textField);
357 vs = Box.createVerticalStrut(6);
358 layout.setConstraints(vs,cons);
362 cons.gridheight = GridBagConstraints.REMAINDER;
364 JScrollPane scroller = new JScrollPane(list);
365 layout.setConstraints(scroller,cons);
371 private void updatePreview(){
372 String family = familyField.getText();
375 size = Integer.parseInt(sizeField.getText());
380 int style = styleList.getSelectedIndex();
381 preview.setFont(new Font(family,style,size));
384 class ActionHandler implements ActionListener {
385 public void actionPerformed(ActionEvent evt){
386 if(evt.getSource() == ok)ok();
387 else if(evt.getSource() == cancel)cancel();
391 class ListHandler implements ListSelectionListener {
392 public void valueChanged(ListSelectionEvent evt)
394 Object source = evt.getSource();
395 if(source == familyList) {
396 String family = (String)familyList.getSelectedValue();
398 familyField.setText(family);
400 else if(source == sizeList) {
401 String size = (String)sizeList.getSelectedValue();
403 sizeField.setText(size);
405 else if(source == styleList) {
406 String style = (String)styleList.getSelectedValue();
408 styleField.setText(style);
413 /*public static void main(String args[])
415 Font font = new FontSelectorDialog(null,new Font("Times",Font.PLAIN,12)).getSelectedFont();