/* ========================
* JSynoptic : a free Synoptic editor
* ========================
*
* Project Info: http://jsynoptic.sourceforge.net/index.html
*
* This program is free software; you can redistribute it and/or modify it under the terms
* of the GNU Lesser General Public License as published by the Free Software Foundation;
* either version 2.1 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with this
* program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*
* (C) Copyright 2001-2006, by :
* Corporate:
* EADS Astrium SAS
* EADS CRC
* Individual:
* Claude Cazenave
*
* $$Id$$
*
* Changes
* -------
* 21 avr. 2006 : Initial public release (CC);
*
*/
package simtools.ui;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.geom.AffineTransform;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import simtools.data.DataSource;
import simtools.diagram.DiagramParameters;
/**
* A panel to select font parameters and to display a preview of the selected
* font
*
*/
public class FontChooserPanel extends GridBagPanel implements ActionListener {
/** The default font sizes that can be selected. */
public static final String[] SIZES = {
"6",
"7",
"8",
"9",
"10",
"11",
"12",
"14",
"16",
"18",
"20",
"22",
"24",
"28",
"36",
"48",
"72" };
static MenuResourceBundle resources = ResourceFinder.getMenu(FontChooserPanel.class);
protected JComboBox cbName, cbSize;
protected JCheckBox cBold, cItalic;
protected JTextField tfpreview;
protected Font currentFont;
protected JButton btextcolor;
protected JLabel ltextColor;
protected Color textColor;
protected Color noColor;
protected ColorMapper textMapper;
protected DataSource textMapperSource;
private boolean editing;
private JPropertiesPanel owner;
/**
* Create a font chooser panel
*
* @param names
* the list of available font names if null GraphicsEnvironment
* list is used
* @param withLock
* set to true to use of a lock check box
* @param preview
* an optional text field preview to be updated according to font
* choices
*/
public FontChooserPanel(JPropertiesPanel owner, String[] names, JTextField preview) {
super(resources.getStringValue("Font"));
editing = false;
this.owner = owner;
tfpreview = preview;
currentFont = null;
String[] fonts = names != null ? names : GraphicsEnvironment.getLocalGraphicsEnvironment()
.getAvailableFontFamilyNames();
cbName = new JComboBox(fonts);
cbName.addActionListener(this);
cbSize = new JComboBox(SIZES);
cbSize.addActionListener(this);
cbSize.setEditable(true);
cBold = new JCheckBox(resources.getString("Bold"));
cBold.addActionListener(this);
cItalic = new JCheckBox(resources.getString("Italic"));
cItalic.addActionListener(this);
// text color
textMapper = null;
textMapperSource = null;
textColor = DiagramParameters.DEFAULT_COLOR;
btextcolor = new JButton(" ");
noColor = btextcolor.getBackground();
btextcolor.setFocusPainted(false);
btextcolor.setBackground(textColor);
btextcolor.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
DynamicColorChooser dialog = new DynamicColorChooser(FontChooserPanel.this.getOwner(), resources
.getString("ChooseATextColor"), null, textColor, textMapperSource, textMapper);
dialog.pack();
dialog.setVisible(true);
if (dialog.isOk()){
textColor = dialog.getColor();
btextcolor.setBackground(textColor);
textMapperSource = dialog.getSource();
textMapper = dialog.getMapper();
}
}
});
ltextColor = new JLabel(resources.getString("TextColor"));
addOnCurrentRow(cbName, 2);
carriageReturn();
addOnCurrentRow(new JLabel(resources.getString("Size")));
addOnCurrentRow(cbSize);
carriageReturn();
addOnCurrentRow(cBold);
addOnCurrentRow(cItalic);
carriageReturn();
addOnCurrentRow(ltextColor);
addOnCurrentRow(btextcolor);
carriageReturn();
}
public JDialog getOwner() {
return owner.getOwner();
}
/*
* (non-Javadoc)
*
* @see java.awt.Component#setEnabled(boolean)
*/
public void setEnabled(boolean enabled) {
cbName.setEnabled(enabled);
cbSize.setEnabled(enabled);
cBold.setEnabled(enabled);
cItalic.setEnabled(enabled);
}
/*
* (non-Javadoc)
*
* @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
*/
public void actionPerformed(ActionEvent e) {
if (!editing) {
apply();
}
}
/**
* Apply current font selection
*/
public void apply() {
Font f = getSelectedFont();
if (f != null) {
currentFont = f;
updatePreview();
} else { // invalid font, display previous one
displayCurrentFont();
}
}
/**
* Update the preview field
*/
protected void updatePreview() {
if ((tfpreview != null) && (currentFont != null)) {
Font noScaleFont = currentFont.deriveFont(new AffineTransform());
if (!noScaleFont.equals(tfpreview.getFont())) {
tfpreview.setFont(noScaleFont);
updateLayout();
}
}
}
/**
* Redo the layout of the preview field to take into account size changes
* This default implementation packs the window, if any, owner of this panel
*/
protected void updateLayout() {
Window w = SwingUtilities.getWindowAncestor(tfpreview);
if (w != null) {
w.pack();
}
}
/**
* @return Returns the currentFont.
*/
public Font getCurrentFont() {
return currentFont;
}
/**
* @param currentFont
* The currentFont to set.
*/
public void setCurrentFont(Font currentFont) {
this.currentFont = currentFont;
displayCurrentFont();
updatePreview();
}
/**
* Get the selected font
*
* @return a font or null if invalid selection
*/
protected Font getSelectedFont() {
int size;
try {
size = Integer.parseInt((String) cbSize.getSelectedItem());
} catch (NumberFormatException nfe) {
return null;
}
if (size <= 0) {
return null;
}
Font f = new Font((String) cbName.getSelectedItem(), (cBold.isSelected() ? Font.BOLD : 0)
+ (cItalic.isSelected() ? Font.ITALIC : 0), size);
return f;
}
/**
* Display current font paramters
*/
protected void displayCurrentFont() {
if (currentFont != null) {
editing = true;
cbName.setSelectedItem(currentFont.getName());
cItalic.setSelected(currentFont.isItalic());
cBold.setSelected(currentFont.isBold());
cbSize.setSelectedItem("" + currentFont.getSize());
editing = false;
}
}
/**
* A test/demo
*
* @param args
*/
public static void main(String[] args) {
JFrame jf = new JFrame();
jf.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
JTextField preview = new JTextField("Test");
jf.getContentPane().add(preview, BorderLayout.NORTH);
FontChooserPanel fcp = new FontChooserPanel(null, null, preview);
fcp.setCurrentFont(preview.getFont());
jf.getContentPane().add(fcp, BorderLayout.SOUTH);
jf.pack();
jf.setVisible(true);
}
public Color getTextColor() {
return textColor;
}
public ColorMapper getTextMapper() {
return textMapper;
}
public DataSource getTextMapperSource() {
return textMapperSource;
}
public void setTextColor(Color textColor) {
this.textColor = textColor;
}
public void setTextMapper(ColorMapper textMapper) {
this.textMapper = textMapper;
}
public void setTextMapperSource(DataSource textMapperSource) {
this.textMapperSource = textMapperSource;
}
public JButton getTextColorButton() {
return btextcolor;
}
public JLabel getTextColorLabel() {
return ltextColor;
}
}