1 package com.explosion.utilities.preferences.editandrender.table;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 import java.awt.Color;
24 import java.awt.Component;
25 import java.awt.Dialog;
26 import java.awt.Font;
27 import java.awt.Frame;
28 import java.awt.event.ActionEvent;
29 import java.awt.event.ActionListener;
30
31 import javax.swing.DefaultCellEditor;
32 import javax.swing.JButton;
33 import javax.swing.JCheckBox;
34 import javax.swing.JTable;
35
36 import org.apache.log4j.LogManager;
37 import org.apache.log4j.Logger;
38
39 import com.explosion.utilities.GeneralUtils;
40 import com.explosion.utilities.dialog.FontSelector;
41 import com.explosion.utilities.preferences.Preference;
42
43 /***
44 * Author: Stephen Cowx Date: Nov 9, 2002 Time: 7:50:42 PM
45 * This class double as a class that can throw up an editor for plain Font objects
46 * as well as thrown up and editor for Preference Objects of type FONT
47 */
48 public class FontPreferenceEditor extends DefaultCellEditor {
49
50 private static Logger log = LogManager
51 .getLogger(FontPreferenceEditor.class);
52 private Object value;
53
54 public FontPreferenceEditor(JButton button, Component owner) {
55 super(new JCheckBox());
56
57 log.debug("FontPreferenceEditor " + owner);
58 this.editorComponent = button;
59 ((JButton) editorComponent).setBackground(Color.WHITE);
60
61 setClickCountToStart(1);
62
63 ((JButton) editorComponent)
64 .addActionListener(new FontButtonActionListener(this, owner));
65
66 ((JButton) editorComponent).addActionListener(new ActionListener() {
67
68 public void actionPerformed(ActionEvent e) {
69 fireEditingStopped();
70 }
71 });
72 }
73
74 protected void fireEditingStopped() {
75 super.fireEditingStopped();
76 }
77
78 public Object getCellEditorValue() {
79 return value;
80 }
81
82 public Component getTableCellEditorComponent(JTable table, Object value,
83 boolean isSelected, int row, int column) {
84 this.value = value;
85 ((JButton) editorComponent).setText("..");
86
87 return ((JButton) editorComponent);
88 }
89
90 /***
91 * Returns the Font value for this editor
92 *
93 * @return
94 */
95 public Font getFont() {
96 if (value instanceof Preference) {
97 return (Font) ((Preference) value).getValue();
98 } else if (value instanceof Font) {
99 return (Font) value;
100 }
101
102 return null;
103 }
104
105 /***
106 * Sets the Font value for this editor
107 *
108 * @param font
109 * @throws Exception
110 */
111 public void setFont(Font font) throws Exception {
112 if (value instanceof Preference) {
113 ((Preference) value).setValue(font);
114 } else if (value instanceof Font) {
115 value = font;
116 }
117 }
118 }
119
120 /***
121 * Opens up a FontSelectorDialog
122 */
123
124 class FontButtonActionListener implements ActionListener {
125
126 private FontPreferenceEditor fontEditor = null;
127 private static Logger log = LogManager
128 .getLogger(FontButtonActionListener.class);
129 private FontSelector selector = null;
130
131 private Component owner;
132
133 public FontButtonActionListener(FontPreferenceEditor fontEditor,
134 Component owner) {
135 log.debug("FontButtonActionListener " + owner);
136 this.fontEditor = fontEditor;
137 this.owner = owner;
138 }
139
140 public void actionPerformed(ActionEvent e) {
141 try {
142 System.out.println("actionPerformed " + owner + fontEditor);
143 if (owner instanceof Dialog)
144 selector = new FontSelector("Choose a font", (Dialog) owner,
145 (Font) fontEditor.getFont());
146 else if (owner instanceof Frame)
147 selector = new FontSelector("Choose a font", (Frame) owner,
148 (Font) fontEditor.getFont());
149
150 GeneralUtils.centreWindowInParent(selector, owner);
151 selector.setVisible(true);
152
153 if (selector != null
154 && selector.getResult() == FontSelector.RESULT_OK)
155 fontEditor.setFont(selector.getSelectedFont());
156
157 } catch (Exception e1) {
158 com.explosion.utilities.exception.ExceptionManagerFactory.getExceptionManager()
159 .manageException(e1, null);
160 }
161 }
162
163 }