001: /*
002: * Copyright (C) 2005 Jeff Tassin
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018:
019: package com.jeta.swingbuilder.gui.font;
020:
021: import java.awt.BorderLayout;
022: import java.awt.Font;
023:
024: import javax.swing.DefaultListModel;
025: import javax.swing.JList;
026:
027: import com.jeta.forms.components.panel.FormPanel;
028: import com.jeta.open.gui.framework.JETAPanel;
029: import com.jeta.swingbuilder.gui.components.IntegerDocument;
030: import com.jgoodies.forms.layout.CellConstraints;
031:
032: /**
033: * Panel that allows a user to select properties for a Font. This includes the
034: * Font family, style (bold, italic), and point size.
035: *
036: * @author Jeff Tassin
037: */
038: public class FontView extends JETAPanel {
039: /**
040: * The actual view
041: */
042: private FormPanel m_view;
043:
044: /**
045: * The font preview component
046: */
047: private FontSampleComponent m_font_comp;
048:
049: public static final String STYLE_BOLD_ITALIC = "Bold Italic";
050: public static final String STYLE_BOLD = "Bold";
051: public static final String STYLE_ITALIC = "Italic";
052: public static final String STYLE_NORMAL = "Regular";
053:
054: /**
055: * ctor
056: */
057: public FontView(Font f) {
058: setLayout(new BorderLayout());
059: m_view = new FormPanel(
060: "com/jeta/swingbuilder/gui/font/fontSelector.frm");
061: add(m_view, BorderLayout.CENTER);
062: setBorder(javax.swing.BorderFactory.createEmptyBorder(10, 10,
063: 10, 10));
064: m_view.getTextField(FontViewNames.ID_SIZE_FIELD).setDocument(
065: new IntegerDocument());
066:
067: CellConstraints cc = new CellConstraints();
068: m_font_comp = new FontSampleComponent(f);
069:
070: m_view.getFormAccessor(FontViewNames.ID_SAMPLE_VIEW).addBean(
071: m_font_comp, cc.xy(1, 1));
072:
073: initialize(f);
074: setController(new FontViewController(this ));
075: }
076:
077: /**
078: * @return a Font object based on the settings in the view
079: */
080: public Font createFont() {
081: return new Font(getFamilyName(), getStyleValue(),
082: getPointSize());
083: }
084:
085: public String getFamilyName() {
086: return m_view.getText(FontViewNames.ID_FAMILY_FIELD);
087: }
088:
089: public int getStyleValue() {
090: String style = m_view.getText(FontViewNames.ID_STYLE_FIELD);
091: if (STYLE_BOLD_ITALIC.equals(style))
092: return (Font.BOLD | Font.ITALIC);
093: else if (STYLE_BOLD.equals(style))
094: return Font.BOLD;
095: else if (STYLE_ITALIC.equals(style))
096: return Font.ITALIC;
097: else
098: return Font.PLAIN;
099: }
100:
101: public int getPointSize() {
102: try {
103: int point_size = Integer.parseInt(m_view
104: .getText(FontViewNames.ID_SIZE_FIELD));
105: if (point_size < 1)
106: point_size = 10;
107: return point_size;
108: } catch (Exception e) {
109: e.printStackTrace();
110: return 10;
111: }
112: }
113:
114: /**
115: * Initializes the view with the current font set
116: */
117: private void initialize(Font f) {
118: String[] fonts = java.awt.GraphicsEnvironment
119: .getLocalGraphicsEnvironment()
120: .getAvailableFontFamilyNames();
121:
122: JList list = m_view.getList(FontViewNames.ID_FAMILY_LIST);
123: DefaultListModel lmodel = new DefaultListModel();
124: list.setModel(lmodel);
125:
126: for (int index = 0; index < fonts.length; index++) {
127: lmodel.addElement(fonts[index]);
128: }
129:
130: if (f != null) {
131: setFontValue(f);
132: }
133: }
134:
135: /**
136: * Sets the family name in the view
137: */
138: private void setFamilyName(String name) {
139: m_view.setText(FontViewNames.ID_FAMILY_FIELD, name);
140: m_view.getList(FontViewNames.ID_FAMILY_LIST).setSelectedValue(
141: name, true);
142: }
143:
144: /**
145: * Sets the point size in the view
146: */
147: public void setPointSize(int size) {
148: m_view.getList(FontViewNames.ID_SIZE_LIST).setSelectedValue(
149: String.valueOf(size), true);
150: m_view.setText(FontViewNames.ID_SIZE_FIELD, String
151: .valueOf(size));
152: }
153:
154: /**
155: * Sets the style in the view
156: */
157: public void setStyle(String style) {
158: m_view.setText(FontViewNames.ID_STYLE_FIELD, style);
159: m_view.getList(FontViewNames.ID_STYLE_LIST).setSelectedValue(
160: style, true);
161: }
162:
163: /**
164: * Sets the font to be displayed in this view
165: */
166: public void setFontValue(Font f) {
167: setFamilyName(f.getName());
168: if (f.isBold() && f.isItalic()) {
169: setStyle(STYLE_BOLD_ITALIC);
170: } else if (f.isBold()) {
171: setStyle(STYLE_BOLD);
172: } else if (f.isItalic()) {
173: setStyle(STYLE_ITALIC);
174: } else {
175: setStyle(STYLE_NORMAL);
176: }
177: setPointSize(f.getSize());
178: m_font_comp.setFont(f);
179: }
180:
181: }
|