001: /*
002: * Copyright (C) 2004 Giuseppe MANNA
003: *
004: * This file is part of FreeReportBuilder
005: *
006: * FreeReportBuilder is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: */
021:
022: package it.frb;
023:
024: import java.awt.Event;
025: import java.awt.Font;
026: import java.awt.Label;
027: import java.awt.Panel;
028:
029: public class FontChooser extends java.awt.Dialog {
030: static final int fieldWidth = 10;
031:
032: private java.awt.Choice fontChoice = new java.awt.Choice();
033: private java.awt.Checkbox bold = new java.awt.Checkbox("Bold");
034: private java.awt.Checkbox italic = new java.awt.Checkbox("Italic");
035: private java.awt.TextField sizeText;
036: private java.awt.TextField sampleText = new java.awt.TextField(
037: "ABCabc");
038: private java.awt.Button okButton = new java.awt.Button("OK");
039: private java.awt.Button cancelButton = new java.awt.Button("Cancel");
040:
041: public boolean ok;
042: public Font font;
043:
044: public FontChooser(java.awt.Frame parent, Font font) {
045: super (parent, "Font Chooser", true);
046:
047: Panel mainPanel = new Panel();
048: Panel labelPanel = new Panel();
049: Panel samplePanel = new Panel();
050: Panel buttonPanel = new Panel();
051:
052: setLayout(new java.awt.BorderLayout());
053: setResizable(true);
054:
055: mainPanel.setLayout(new java.awt.GridLayout(5, 1));
056: labelPanel.setLayout(new java.awt.GridLayout(5, 1));
057:
058: labelPanel.add(new Label("Font:", Label.RIGHT));
059: fontChoice = new java.awt.Choice();
060:
061: java.awt.GraphicsEnvironment gps = java.awt.GraphicsEnvironment
062: .getLocalGraphicsEnvironment();
063: String[] fonts = gps.getAvailableFontFamilyNames();
064:
065: for (int i = 0; i < fonts.length; i++) {
066: fontChoice.addItem(fonts[i].toLowerCase());
067: }
068:
069: mainPanel.add(fontChoice);
070: labelPanel.add(new Label(""));
071: mainPanel.add(bold);
072: labelPanel.add(new Label(""));
073: mainPanel.add(italic);
074: labelPanel.add(new Label("Size:", Label.RIGHT));
075: sizeText = new java.awt.TextField(Integer.toString(font
076: .getSize()));
077: mainPanel.add(sizeText);
078:
079: labelPanel.add(new Label("Sample:", Label.RIGHT));
080: sampleText.setEditable(false);
081: mainPanel.add(sampleText);
082:
083: buttonPanel.add(okButton);
084: buttonPanel.add(cancelButton);
085:
086: add("Center", mainPanel);
087: add("West", labelPanel);
088: add("South", buttonPanel);
089:
090: sampleText.setFont(font);
091: fontChoice.select(font.getName().toLowerCase());
092: bold.setState(font.isBold());
093: italic.setState(font.isItalic());
094:
095: pack();
096: }
097:
098: private void refreshFont() {
099: Font newFont = new Font(fontChoice.getSelectedItem(), (bold
100: .getState() ? Font.BOLD : 0)
101: | (italic.getState() ? Font.ITALIC : 0), Integer
102: .valueOf(sizeText.getText()).intValue());
103:
104: if (newFont != null) {
105: font = newFont;
106: sampleText.setFont(font);
107: }
108: }
109:
110: public boolean handleEvent(Event evt)
111: //public boolean processEvent(Event evt)
112: {
113: if (evt.target == okButton && evt.id == Event.ACTION_EVENT) {
114: ok = true;
115: hide();
116: return true;
117: } else if (evt.target == cancelButton
118: && evt.id == Event.ACTION_EVENT) {
119: ok = false;
120: hide();
121: return true;
122: } else if (evt.target == fontChoice
123: && evt.id == Event.ACTION_EVENT || evt.target == bold
124: && evt.id == Event.ACTION_EVENT || evt.target == italic
125: && evt.id == Event.ACTION_EVENT
126: || evt.target == sizeText && evt.id == Event.LOST_FOCUS) {
127: refreshFont();
128: return true;
129: } else {
130: return false;
131: }
132: }
133:
134: boolean doModal() {
135: show();
136: return ok;
137: }
138: }
|