001: /**
002: * FontChooser.java
003: * Copyright 2005 Carlos Silva A.
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */package com.csa.lib.swing;
018:
019: import java.awt.BorderLayout;
020: import java.awt.Color;
021: import java.awt.FlowLayout;
022: import java.awt.Font;
023: import java.awt.Frame;
024: import java.awt.GraphicsEnvironment;
025: import java.awt.event.ActionEvent;
026: import java.awt.event.ActionListener;
027: import java.util.Vector;
028:
029: import javax.swing.BorderFactory;
030: import javax.swing.BoxLayout;
031: import javax.swing.JButton;
032: import javax.swing.JDialog;
033: import javax.swing.JLabel;
034: import javax.swing.JList;
035: import javax.swing.JPanel;
036: import javax.swing.JScrollPane;
037: import javax.swing.ListModel;
038: import javax.swing.event.ListSelectionEvent;
039: import javax.swing.event.ListSelectionListener;
040:
041: /**
042: * Ventana para escoger Fonts. Se puede usar facilmente.
043: *
044: * <pre>
045: * FontChooser fc = new FontChooser(ownerFrame);
046: * if (fc.execute())
047: * System.out.println(fc.getFont());
048: * </pre>
049: *
050: * @author csilva
051: */
052: public class FontChooserDialog implements ListSelectionListener,
053: ActionListener {
054: private JDialog dialog;
055:
056: private JList fontNames;
057:
058: private JList fontSizes;
059:
060: private JList fontStyles;
061:
062: private JLabel demoFont;
063:
064: private JButton botonOk;
065:
066: private JButton botonCancel;
067:
068: private Font font;
069:
070: private boolean ok = true;
071: private boolean ignoreValueChanges = false;
072:
073: public FontChooserDialog(Frame owner) {
074: buildUI(owner, "Seleccion de font");
075: }
076:
077: public FontChooserDialog(Frame owner, String title) {
078: buildUI(owner, title);
079: }
080:
081: public FontChooserDialog(Frame owner, String title, Font f) {
082: buildUI(owner, title);
083: setFont(f);
084: }
085:
086: public void setFont(Font f) {
087: font = f;
088: System.out.println("setfont:" + font);
089: System.out.println("setfont style:" + font.getStyle());
090: }
091:
092: /**
093: * Construye un dialogo (interno) para dar soporte a la eleccion del font
094: *
095: * @param owner
096: */
097: void buildUI(Frame owner, String title) {
098: dialog = new JDialog(owner, title);
099:
100: GraphicsEnvironment ge = GraphicsEnvironment
101: .getLocalGraphicsEnvironment();
102: String familyNames[] = ge.getAvailableFontFamilyNames();
103: Vector sizes = new Vector();
104: for (int sz = 8; sz < 24; sz++)
105: sizes.add("" + sz);
106: Vector styles = new Vector();
107: styles.add("plain");
108: styles.add("bold");
109: styles.add("italic");
110: styles.add("bold italic");
111: fontNames = new JList(familyNames);
112: fontNames.setBorder(BorderFactory.createLineBorder(Color.gray));
113: fontNames.addListSelectionListener(this );
114: fontSizes = new JList(sizes);
115: fontSizes.addListSelectionListener(this );
116: fontStyles = new JList(styles);
117: fontStyles.addListSelectionListener(this );
118: JPanel configPanel = new JPanel(null);
119: configPanel.setLayout(new BoxLayout(configPanel,
120: BoxLayout.X_AXIS));
121: configPanel.add(new JScrollPane(fontNames));
122: configPanel.add(new JScrollPane(fontStyles));
123: configPanel.add(new JScrollPane(fontSizes));
124:
125: demoFont = new JLabel(
126: "ABCDEFGHIJKLMNÑOPQRSTUVWXYZ abcdefghijklmnñopqrstuvwxyz");
127: demoFont.setBorder(BorderFactory.createTitledBorder(null,
128: "Ejemplo"));
129: if (font != null)
130: demoFont.setFont(font);
131: else
132: font = demoFont.getFont();
133:
134: botonOk = new JButton("Ok");
135: botonCancel = new JButton("Cancel");
136: botonOk.addActionListener(this );
137: botonCancel.addActionListener(this );
138: JPanel botonera = new JPanel(new FlowLayout());
139: botonera.add(botonOk);
140: botonera.add(botonCancel);
141:
142: JPanel mainPanel = new JPanel(new BorderLayout());
143: mainPanel.add(configPanel, BorderLayout.CENTER);
144: mainPanel.add(demoFont, BorderLayout.SOUTH);
145: dialog.getContentPane().setLayout(new BorderLayout());
146: dialog.getContentPane().add(mainPanel, BorderLayout.CENTER);
147: dialog.getContentPane().add(botonera, BorderLayout.SOUTH);
148: }
149:
150: public void valueChanged(ListSelectionEvent e) {
151: if (ignoreValueChanges)
152: return;
153: buildFont();
154: }
155:
156: public void buildFont() {
157: String fontName = (String) fontNames.getSelectedValue();
158: if (fontName == null)
159: return;
160:
161: String sizeStr = (String) fontSizes.getSelectedValue();
162: if (sizeStr == null)
163: return;
164: int fontSize = Integer.parseInt(sizeStr);
165: int fontStyle = Font.PLAIN;
166: switch (fontStyles.getSelectedIndex()) {
167: case 1:
168: fontStyle = Font.BOLD;
169: break;
170: case 2:
171: fontStyle = Font.ITALIC;
172: break;
173: case 3:
174: fontStyle = Font.BOLD | Font.ITALIC;
175: break;
176: default:
177: ;
178: }
179: font = new Font(fontName, fontStyle, fontSize);
180:
181: demoFont.setFont(font);
182: }
183:
184: public void actionPerformed(ActionEvent e) {
185: if (e.getSource() == botonOk) {
186: ok = true;
187: }
188: dialog.setVisible(false);
189: }
190:
191: /**
192: * Ejecuta el dialogo y retorna true si el usuario selecciono un Font.
193: *
194: * @return
195: */
196: public boolean execute() {
197: System.out.println("font:" + font);
198: System.out.println("style:" + font.getStyle());
199: ok = false;
200: ignoreValueChanges = true;
201: // Asignar los valores iniciales de cada lista de acuerdo
202: // al font seleccionado.
203: ListModel model = fontNames.getModel();
204: String value = (font != null ? font.getFamily() : null);
205: for (int i = 0; i < model.getSize(); i++) {
206: if (model.getElementAt(i).equals(value)) {
207: fontNames.setSelectedIndex(i);
208: fontNames.ensureIndexIsVisible(i);
209: break;
210: }
211: }
212: model = fontSizes.getModel();
213: value = (font != null ? String.valueOf(font.getSize()) : null);
214: for (int i = 0; i < model.getSize(); i++) {
215: if (model.getElementAt(i).equals(value)) {
216:
217: fontSizes.setSelectedIndex(i);
218: fontSizes.ensureIndexIsVisible(i);
219:
220: break;
221: }
222: }
223:
224: int style = (font != null ? font.getStyle() : 0);
225: System.out.println("font:" + font);
226: System.out.println("style:" + style);
227: boolean isBold = (style & Font.BOLD) != 0;
228: boolean isItalic = (style & Font.ITALIC) != 0;
229:
230: fontStyles.setSelectedIndex((isBold ? 1 : 0)
231: + (isItalic ? 2 : 0));
232:
233: ignoreValueChanges = false;
234: dialog.pack();
235: dialog.setModal(true);
236: dialog.setVisible(true);
237: dialog.dispose();
238:
239: return ok;
240: }
241:
242: public Font getFont() {
243: return demoFont.getFont();
244: }
245:
246: public String getFamilyName() {
247: return demoFont.getFont().getFamily();
248: }
249:
250: public int getFontStyle() {
251: return demoFont.getFont().getStyle();
252: }
253:
254: public int getFontSize() {
255: return demoFont.getFont().getSize();
256: }
257:
258: public static void main(String args[]) {
259: FontChooserDialog fc = new FontChooserDialog(null);
260: if (fc.execute())
261: System.out.println(fc.getFont());
262: System.exit(0);
263: }
264: }
|