View Javadoc

1   package com.explosion.utilities.preferences.editandrender.table;
2   
3   /*
4    * =============================================================================
5    * 
6    * Copyright 2004 Stephen Cowx
7    * 
8    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
9    * use this file except in compliance with the License. You may obtain a copy of
10   * the License at
11   * 
12   * http://www.apache.org/licenses/LICENSE-2.0
13   * 
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17   * License for the specific language governing permissions and limitations under
18   * the License.
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()); //Unfortunately, the constructor expects a
56  								// check box, combo box, or text field.
57  		log.debug("FontPreferenceEditor " + owner);
58  		this.editorComponent = button;
59  		((JButton) editorComponent).setBackground(Color.WHITE);
60  
61  		setClickCountToStart(1); //This is usually 1 or 2.
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 }