1 package com.explosion.utilities.preferences.editandrender.form;
2
3 import java.awt.Component;
4 import java.awt.Dialog;
5 import java.awt.Font;
6 import java.awt.Frame;
7 import java.awt.GridBagConstraints;
8 import java.awt.GridBagLayout;
9 import java.awt.Insets;
10 import java.awt.event.ActionEvent;
11 import java.awt.event.ActionListener;
12
13 import javax.swing.JButton;
14 import javax.swing.JLabel;
15
16 import org.apache.log4j.LogManager;
17 import org.apache.log4j.Logger;
18
19 import com.explosion.utilities.GeneralUtils;
20 import com.explosion.utilities.dialog.FontSelector;
21 import com.explosion.utilities.exception.ExceptionManagerFactory;
22 import com.explosion.utilities.preferences.Preference;
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45 /***
46 * This class provides a way to edit and rebder text based preferences
47 * @author Stephen Cowx
48 * @version
49 */
50 public class FontPreferenceEditorAndRenderer extends FormBasedPreferenceRendererAndEditor {
51
52 private JLabel fontLabel = new JLabel();
53 private JButton button = new JButton("...");
54 private JLabel label = new JLabel();
55 private Component owner;
56 private static Logger log = LogManager.getLogger(FontPreferenceEditorAndRenderer.class);
57
58 /***
59 * Constructs an instance of this class
60 * @param preference
61 */
62 public FontPreferenceEditorAndRenderer(Preference preference, Component owner)
63 {
64 super(preference);
65 this.owner = owner;
66 init();
67 }
68
69 /***
70 * This method initialises this renderer editor combination
71 */
72 public void init()
73 {
74 this.setLayout(new GridBagLayout());
75
76
77 label.setText(getPreference().getLongName());
78 fontLabel.setText(getPreference().toString());
79 fontLabel.setFont((Font)getPreference().getValue());
80
81
82 this.add(label , new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0 ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(2, 2, 2, 2), 0, 0));
83 this.add(fontLabel , new GridBagConstraints(1, 0, 1, 1, 1.0, 1.0 ,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(2, 2, 2, 2), 0, 0));
84 this.add(button , new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(2, 2, 2, 2), 0, 0));
85
86
87 button.addActionListener(new FontButtonActionListener(this, owner));
88 }
89
90 /***
91 * @throws Exception
92 */
93 public void setTheFont(Font font) {
94 log.debug("setFont called");
95 try {
96 getPreference().setValue(font);
97 fontLabel.setText(getPreference().toString());
98 fontLabel.setFont((Font)getPreference().getValue());
99 } catch (Exception e) {
100 ExceptionManagerFactory.getExceptionManager().manageException(e, "Exception caught while updating color preference");
101 }
102 }
103
104 /***
105 * Returns the Font for the preference
106 * @return
107 */
108 public Font getTheFont()
109 {
110 return (Font) getPreference().getValue();
111 }
112 }
113
114
115 /***
116 * Opens up a FontSelectorDialog
117 */
118
119 class FontButtonActionListener implements ActionListener {
120
121 private FontPreferenceEditorAndRenderer fontEditor = null;
122 private static Logger log = LogManager
123 .getLogger(FontButtonActionListener.class);
124 private FontSelector selector = null;
125
126 private Component owner;
127
128 public FontButtonActionListener(FontPreferenceEditorAndRenderer fontEditor,
129 Component owner) {
130 log.debug("FontButtonActionListener " + owner);
131 this.fontEditor = fontEditor;
132 this.owner = owner;
133 }
134
135 public void actionPerformed(ActionEvent e) {
136 try {
137 System.out.println("actionPerformed " + owner + fontEditor);
138 if (owner instanceof Dialog)
139 selector = new FontSelector("Choose a font", (Dialog) owner,
140 (Font) fontEditor.getFont());
141 else if (owner instanceof Frame)
142 selector = new FontSelector("Choose a font", (Frame) owner,
143 (Font) fontEditor.getTheFont());
144
145 GeneralUtils.centreWindowInParent(selector, owner);
146 selector.setVisible(true);
147
148 if (selector != null
149 && selector.getResult() == FontSelector.RESULT_OK)
150 fontEditor.setTheFont(selector.getSelectedFont());
151
152 } catch (Exception e1) {
153 com.explosion.utilities.exception.ExceptionManagerFactory.getExceptionManager()
154 .manageException(e1, null);
155 }
156 }
157
158 }