Mega Code Archive
Font Dialog from claribole
/* FILE: FontDialog.java
* DATE OF CREATION: Wed Jan 15 17:07:07 2002
* AUTHOR : Emmanuel Pietriga (emmanuel@w3.org)
* MODIF: Emmanuel Pietriga (emmanuel.pietriga@inria.fr)
* Copyright (c) Emmanuel Pietriga, 2002. All Rights Reserved
* Copyright (c) INRIA, 2008. All Rights Reserved
* Licensed under the GNU LGPL. For full terms see the file COPYING.
*
* $Id: FontDialog.java 1207 2008-08-13 16:03:07Z epietrig $
*/
import java.awt.Container;
import java.awt.Dialog;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.StringTokenizer;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
public class FontDialog extends JDialog implements ActionListener {
static final String ZVTM_FONT_CHOOSER = "ZVTM Font Chooser";
static String DEFAULT_FAMILY="Dialog";
static int DEFAULT_STYLE=Font.PLAIN;
static int DEFAULT_SIZE=10;
FontTracker ft;
JButton okBt,cancelBt;
JList familyList,styleList,sizeList;
String[] allFontFamilies=java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
String[] allFontStyles={"Plain","Bold","Italic","BoldItalic"};
String[] allFontSizes={"4","6","8","10","12","14","16","18","20","24"};
/** Call a FontDialog window that will return the font selected in the window
*@param owner application frame that owns this Modal component
*/
public static Font getFontDialog(Frame owner){
FontTracker res=new FontTracker();
FontDialog fd=new FontDialog(res,owner);
fd.addWindowListener(new FontDialog.Closer());
fd.addComponentListener(new FontDialog.DisposeOnClose());
fd.setLocationRelativeTo(owner);
fd.setVisible(true); //blocks until the dialog is closed
return res.getFont();
}
/** Call a FontDialog window that will return the font selected in the window
*@param owner application dialog that owns this Modal component
*/
public static Font getFontDialog(Dialog owner){
FontTracker res=new FontTracker();
FontDialog fd=new FontDialog(res,owner);
fd.addWindowListener(new FontDialog.Closer());
fd.addComponentListener(new FontDialog.DisposeOnClose());
fd.setLocationRelativeTo(owner);
fd.setVisible(true); //blocks until the dialog is closed
return res.getFont();
}
/** Call a FontDialog window that will return the font selected in the window
*@param owner application frame that owns this Modal component
*@param f a font giving the default parameters with which the dialog should be initialized (family, size, style)
*/
public static Font getFontDialog(Frame owner,Font f){
if (f!=null){
DEFAULT_FAMILY=f.getFamily();
DEFAULT_STYLE=f.getStyle();
DEFAULT_SIZE=f.getSize();
}
FontTracker res=new FontTracker();
FontDialog fd=new FontDialog(res,owner);
fd.addWindowListener(new FontDialog.Closer());
fd.addComponentListener(new FontDialog.DisposeOnClose());
fd.setLocationRelativeTo(owner);
fd.setVisible(true); //blocks until the dialog is closed
return res.getFont();
}
/** Call a FontDialog window that will return the font selected in the window
*@param owner application dialog that owns this Modal component
*@param f a font giving the default parameters with which the dialog should be initialized (family, size, style)
*/
public static Font getFontDialog(Dialog owner,Font f){
if (f!=null){
DEFAULT_FAMILY=f.getFamily();
DEFAULT_STYLE=f.getStyle();
DEFAULT_SIZE=f.getSize();
}
FontTracker res=new FontTracker();
FontDialog fd=new FontDialog(res,owner);
fd.addWindowListener(new FontDialog.Closer());
fd.addComponentListener(new FontDialog.DisposeOnClose());
fd.setLocationRelativeTo(owner);
fd.setVisible(true); //blocks until the dialog is closed
return res.getFont();
}
/**
* Font.decode does not seem to work that well, or I don;t understand how it is supposed to work
* Anyway, this one does what I want (decodes family+" "+style+" "+size)
*/
public static Font decode(String fontSpec){
if (fontSpec!=null && fontSpec.length()>0){
StringTokenizer st=new StringTokenizer(fontSpec," ",false);
String[] tokens=new String[st.countTokens()];
if (tokens.length>=3){
int i=0;
while (st.hasMoreTokens()) {
tokens[i++]=st.nextToken();
}
String tokenizedFamily="";
for (int j=0;j