1 package org.wcb.common.component;
2
3 import org.wcb.util.TableSorter;
4 import org.wcb.common.*;
5
6 import javax.swing.*;
7 import javax.swing.table.TableColumn;
8 import java.awt.*;
9 import java.awt.event.ActionListener;
10 import java.awt.event.ActionEvent;
11 import java.util.Vector;
12 import java.util.Enumeration;
13
14 /***
15 * Copyright (C) 1999 Walter Bogaardt
16 *
17 * This library is free software; you can redistribute it and/or
18 * modify it under the terms of the GNU Lesser General Public
19 * License as published by the Free Software Foundation; either
20 * version 2 of the License, or (at your option) any later version.
21 *
22 * This library is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 * Lesser General Public License for more details.
26 *
27 * You should have received a copy of the GNU Lesser General Public
28 * License along with this library; if not, write to the Free Software
29 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
30 *
31 * Project: Alice X10 Home Automation
32 * Filename: $Id: FontEditorPanel.java,v 1.3 2004/07/22 03:06:50 wbogaardt Exp $<BR>
33 * Abstract: Used to display the list of editable swing fonts
34 *
35 * $Log: FontEditorPanel.java,v $
36 * Revision 1.3 2004/07/22 03:06:50 wbogaardt
37 * removed deprecated method calls.
38 *
39 * Revision 1.2 2004/01/31 07:40:26 wbogaardt
40 * modified layout managers for the panel so that it uses the java standard layoutsrather than the FormLayout api. The purpose is to reduce dependency on non-standard layout api.
41 *
42 */
43 public class FontEditorPanel extends JPanel {
44
45 private FontTableModel fontModel;
46 private Object uiFonts[][];
47 private JTable fontTable;
48
49 public FontEditorPanel(){
50 this.setup();
51 }
52
53 public void saveUIFonts(UIDefaultsLoader uiProperties){
54 for (int i = 0; i < uiFonts.length; i++) {
55 uiProperties.setProperty(fontModel.getValueAt(i, 0).toString(),
56 this.getFontString((Font)fontModel.getValueAt(i, 1) ));
57 UIManager.put(fontModel.getValueAt(i, 0), fontModel.getValueAt(i, 1));
58 }
59 }
60
61 private String getFontString(Font f) {
62 String family;
63 int style,size;
64 family = f.getFamily();
65 style = f.getStyle();
66 size = f.getSize();
67 return "Font."+family+","+style+","+size;
68 }
69
70 private void setup(){
71 setLayout(new BorderLayout());
72 Vector systemFonts = buildSystemFonts();
73 uiFonts = convertToObjectArray(systemFonts);
74 fontModel = new FontTableModel(uiFonts);
75 TableSorter sort = new TableSorter(fontModel);
76 fontTable = new JTable(sort);
77 sort.addMouseListenerToHeaderInTable(fontTable);
78 JScrollPane fontScroll = new JScrollPane(fontTable);
79 setUpFontColumn(fontTable.getColumnModel().getColumn(1));
80 add(new JLabel("Click on font name to change."), BorderLayout.NORTH);
81 add(fontScroll, BorderLayout.CENTER);
82 }
83
84 private void setUpFontColumn(TableColumn fontColumn) {
85 setUpFontEditor(fontTable);
86 FontRender render= new FontRender();
87 render.setToolTipText("Click here to edit the font");
88 fontColumn.setCellRenderer(render);
89 }
90
91 private void setUpFontEditor(JTable table) {
92 final JButton button = new JButton("") {
93 public void setText(String s) {
94 }
95 };
96 button.setBackground(Color.white);
97 button.setBorderPainted(false);
98 button.setMargin(new Insets(0,0,0,0));
99
100 final FontCellEditor fontEditor = new FontCellEditor(button);
101 table.setDefaultEditor(Font.class, fontEditor);
102
103 final FontChoiceDialog fontChooser = new FontChoiceDialog(button);
104 ActionListener okListener = new ActionListener() {
105 public void actionPerformed(ActionEvent e) {
106 fontEditor.currentFont = fontChooser.getFont();
107 }
108 };
109
110 final JDialog dialog = FontChoiceDialog.createDialog(button,
111 "Pick a Font",
112 true,
113 fontChooser,
114 okListener,
115 null);
116
117 button.addActionListener(new ActionListener() {
118 public void actionPerformed(ActionEvent e) {
119 button.setFont(fontEditor.currentFont);
120 fontChooser.setFont(fontEditor.currentFont);
121 dialog.setFont(fontEditor.currentFont);
122 dialog.setLocationRelativeTo(button);
123 dialog.setSize(200,175);
124 dialog.setVisible(true);
125 }
126 });
127 }
128
129 private Vector buildSystemFonts() {
130 UIDefaults uidef = UIManager.getDefaults();
131 Enumeration keys = uidef.keys();
132 Vector returnValue = new Vector();
133 Vector nextRow;
134 String nextKey;
135 Font systemValue;
136 while (keys.hasMoreElements()) {
137 nextKey = keys.nextElement().toString();
138 systemValue = uidef.getFont(nextKey);
139 if (systemValue != null) {
140 nextRow = new Vector(2);
141 nextRow.addElement(nextKey);
142 nextRow.addElement(systemValue);
143 returnValue.addElement(nextRow);
144 }
145 }
146 return returnValue;
147 }
148
149 private Object[][] convertToObjectArray(Vector systemValues){
150 Object[] sortArray = new Object[systemValues.size()];
151 systemValues.copyInto(sortArray);
152 Comparator asComparable = new Comparator() {
153 public boolean less(Object lhs, Object rhs)
154 {
155 Vector left = (Vector)lhs;
156 Vector right = (Vector)rhs;
157 int compValue = (left.elementAt(0).toString()).compareTo(right.elementAt(0).toString());
158 return
159 (compValue < 0);
160 }
161 };
162 ArraySorter.sort(sortArray, asComparable);
163 int colorsLength = sortArray.length/2;
164 Object[][] returnValue = new Object[colorsLength][2];
165 Vector nextRow;
166 for (int i=0;i<colorsLength;i++) {
167 nextRow = (Vector)sortArray[i*2];
168 returnValue[i][0] = nextRow.elementAt(0);
169 returnValue[i][1] = nextRow.elementAt(1);
170 }
171 return returnValue;
172 }
173 }