View Javadoc

1   /*
2    * Created on 2004/06/03
3    */
4   package org.asyrinx.joey.gui.swing;
5   
6   import java.awt.LayoutManager;
7   import java.awt.event.ActionEvent;
8   import java.awt.event.ActionListener;
9   import java.io.Serializable;
10  
11  import javax.swing.JPanel;
12  
13  import org.asyrinx.brownie.core.util.Captionable;
14  import org.asyrinx.joey.gui.EntityGuiRuntimeException;
15  import org.asyrinx.joey.gui.EntityPointer;
16  import org.asyrinx.joey.gui.EntityViewManager;
17  import org.asyrinx.joey.gui.EntityViewManagerUser;
18  import org.asyrinx.joey.om.Entity;
19  import org.asyrinx.joey.om.SearchCondition;
20  
21  /***
22   * @author akima
23   */
24  public class EntityField extends JPanel implements EntityPointer,
25  		EntityViewManagerUser {
26  	private javax.swing.JTextField captionField = null;
27  
28  	private javax.swing.JButton selectButton = null;
29  
30  	private javax.swing.JButton cancelButton = null;
31  
32  	private javax.swing.JButton detailButton = null;
33  
34  	/***
35  	 *  
36  	 */
37  	public EntityField() {
38  		super();
39  		initialize();
40  	}
41  
42  	/***
43  	 * @param isDoubleBuffered
44  	 */
45  	public EntityField(boolean isDoubleBuffered) {
46  		super(isDoubleBuffered);
47  		initialize();
48  	}
49  
50  	/***
51  	 * @param layout
52  	 */
53  	public EntityField(LayoutManager layout) {
54  		super(layout);
55  		initialize();
56  	}
57  
58  	/***
59  	 * @param layout
60  	 * @param isDoubleBuffered
61  	 */
62  	public EntityField(LayoutManager layout, boolean isDoubleBuffered) {
63  		super(layout, isDoubleBuffered);
64  		initialize();
65  	}
66  
67  	/***
68  	 * This method initializes this
69  	 * 
70  	 * @return void
71  	 */
72  	private void initialize() {
73  		this.setLayout(new javax.swing.BoxLayout(this,
74  				javax.swing.BoxLayout.X_AXIS));
75  		this.add(getCaptionField(), null);
76  		this.add(getSelectButton(), null);
77  		this.add(getCancelButton(), null);
78  		this.add(getDetailButton(), null);
79  		this.setSize(314, 28);
80  		this.addFocusListener(new java.awt.event.FocusAdapter() {
81  			public void focusGained(java.awt.event.FocusEvent e) {
82  				updateButtonEnabled();
83  			}
84  		});
85  		updateButtonEnabled();
86  	}
87  
88  	/***
89  	 *  
90  	 */
91  	protected void updateButtonEnabled() {
92  		getSelectButton()
93  				.setEnabled(isEditable() && (getEntityClass() != null));
94  		getCancelButton().setEnabled(entityKey != null);
95  		getDetailButton().setEnabled(entityKey != null);
96  	}
97  
98  	/***
99  	 * 
100 	 * This method initializes jTextField
101 	 * 
102 	 * @return javax.swing.JTextField
103 	 *  
104 	 */
105 	public javax.swing.JTextField getCaptionField() {
106 		if (captionField == null) {
107 			captionField = new javax.swing.JTextField();
108 			captionField.setEditable(false);
109 			captionField.addFocusListener(new java.awt.event.FocusAdapter() {
110 				public void focusGained(java.awt.event.FocusEvent e) {
111 					updateButtonEnabled();
112 				}
113 			});
114 		}
115 		return captionField;
116 	}
117 
118 	/***
119 	 * 
120 	 * This method initializes jButton
121 	 * 
122 	 * @return javax.swing.JButton
123 	 *  
124 	 */
125 	public javax.swing.JButton getSelectButton() {
126 		if (selectButton == null) {
127 			selectButton = new javax.swing.JButton();
128 			selectButton.setPreferredSize(new java.awt.Dimension(20, 20));
129 			selectButton.setText("");
130 			selectButton
131 					.setIcon(new javax.swing.ImageIcon(getClass().getResource(
132 							"/org/asyrinx/joey/gui/swing/btn_list_enabled.png")));
133 			selectButton
134 					.setDisabledIcon(new javax.swing.ImageIcon(
135 							getClass()
136 									.getResource(
137 											"/org/asyrinx/joey/gui/swing/btn_list_disabled.png")));
138 			selectButton.addActionListener(new ActionListener() {
139 				public void actionPerformed(ActionEvent e) {
140 					showSelection();
141 				}
142 			});
143 		}
144 		return selectButton;
145 	}
146 
147 	/***
148 	 * 
149 	 * This method initializes jButton
150 	 * 
151 	 * @return javax.swing.JButton
152 	 *  
153 	 */
154 	public javax.swing.JButton getCancelButton() {
155 		if (cancelButton == null) {
156 			cancelButton = new javax.swing.JButton();
157 			cancelButton.setPreferredSize(new java.awt.Dimension(20, 20));
158 			cancelButton.setText("");
159 			cancelButton
160 					.setIcon(new javax.swing.ImageIcon(
161 							getClass()
162 									.getResource(
163 											"/org/asyrinx/joey/gui/swing/btn_cancel_enabled.png")));
164 			cancelButton
165 					.setDisabledIcon(new javax.swing.ImageIcon(
166 							getClass()
167 									.getResource(
168 											"/org/asyrinx/joey/gui/swing/btn_cancel_disabled.png")));
169 			cancelButton.addActionListener(new ActionListener() {
170 				public void actionPerformed(ActionEvent e) {
171 					cancelSelection();
172 				}
173 			});
174 		}
175 		return cancelButton;
176 	}
177 
178 	/***
179 	 * 
180 	 * This method initializes jButton1
181 	 * 
182 	 * @return javax.swing.JButton
183 	 *  
184 	 */
185 	public javax.swing.JButton getDetailButton() {
186 		if (detailButton == null) {
187 			detailButton = new javax.swing.JButton();
188 			detailButton.setText("");
189 			detailButton.setPreferredSize(new java.awt.Dimension(20, 25));
190 			detailButton.setActionCommand("");
191 			detailButton
192 					.setIcon(new javax.swing.ImageIcon(
193 							getClass()
194 									.getResource(
195 											"/org/asyrinx/joey/gui/swing/btn_detail_enabled.png")));
196 			detailButton
197 					.setDisabledIcon(new javax.swing.ImageIcon(
198 							getClass()
199 									.getResource(
200 											"/org/asyrinx/joey/gui/swing/btn_detail_disabled.png")));
201 			detailButton.addActionListener(new ActionListener() {
202 				public void actionPerformed(ActionEvent e) {
203 					showDetail();
204 				}
205 			});
206 		}
207 		return detailButton;
208 	}
209 
210 	private EntityViewManager entityViewManager = null;
211 
212 	/***
213 	 *  
214 	 */
215 	protected void showSelection() {
216 		entityViewManager.showSelectionListView(this, getEntityClass(),
217 				(SearchCondition) null, getEntityKey());
218 	}
219 
220 	/***
221 	 */
222 	protected void cancelSelection() {
223 		this.showEntity(null);
224 	}
225 
226 	/***
227 	 *  
228 	 */
229 	protected void showDetail() {
230 		entityViewManager.showDetailEditView(this, getEntityClass(),
231 				getEntityKey());
232 	}
233 
234 	private boolean editable = true;
235 
236 	/***
237 	 * @return
238 	 */
239 	public boolean isEditable() {
240 		return this.editable;
241 	}
242 
243 	/***
244 	 * @param b
245 	 */
246 	public void setEditable(boolean b) {
247 		this.editable = b;
248 		updateButtonEnabled();
249 	}
250 
251 	private Serializable entityKey = null;
252 
253 	/***
254 	 * @return Returns the entityKey.
255 	 */
256 	public Serializable getEntityKey() {
257 		return entityKey;
258 	}
259 
260 	/***
261 	 * @param entityKey
262 	 *               The entityKey to set.
263 	 */
264 	public void setEntityKey(Serializable entityKey) {
265 		this.entityKey = entityKey;
266 		updateButtonEnabled();
267 	}
268 
269 	/*
270 	 * (non-Javadoc)
271 	 * 
272 	 * @see org.asyrinx.joey.gui.EntityPointer#getPointedKey()
273 	 */
274 	public Serializable getPointedKey() {
275 		return getEntityKey();
276 	}
277 
278 	/*
279 	 * (non-Javadoc)
280 	 * 
281 	 * @see org.asyrinx.joey.gui.EntityPointer#setPointedEntity(org.asyrinx.joey.om.Entity)
282 	 */
283 	public void setPointedEntity(Entity entity) {
284 		showEntity(entity);
285 	}
286 
287 	/*
288 	 * (non-Javadoc)
289 	 * 
290 	 * @see org.asyrinx.joey.gui.EntityPointer#setPointedKey(java.io.Serializable)
291 	 */
292 	public void setPointedKey(Serializable key) {
293 		if (getEntityViewManager() != null) {
294 			if (getEntityViewManager().getEntityServiceManager() != null) {
295 				Entity entity = getEntityViewManager()
296 						.getEntityServiceManager().loadEntity(
297 								this.getEntityClass(), key);
298 				showEntity(entity);
299 				return;
300 			}
301 		}
302 		setEntityKey(key);
303 	}
304 
305 	public void showEntity(Entity entity) {
306 		if (entity != null) {
307 			setEntityKey(entity.getPrimaryKey());
308 			if (entity instanceof Captionable) {
309 				getCaptionField().setText(((Captionable) entity).getCaption());
310 			} else {
311 				getCaptionField().setText(
312 						String.valueOf(entity.getPrimaryKey()));
313 			}
314 		} else {
315 			setEntityKey(null);
316 			getCaptionField().setText("");
317 		}
318 	}
319 
320 	private Class entityClass = null;
321 
322 	/***
323 	 * @return Returns the entityClass.
324 	 */
325 	public Class getEntityClass() {
326 		return entityClass;
327 	}
328 
329 	/***
330 	 * @param entityClass
331 	 *               The entityClass to set.
332 	 */
333 	public void setEntityClass(Class entityClass) {
334 		if (!Entity.class.isAssignableFrom(entityClass))
335 			throw new EntityGuiRuntimeException(
336 					"entityClass must be a sub-class of Entity");
337 		this.entityClass = entityClass;
338 		updateButtonEnabled();
339 	}
340 
341 	/***
342 	 * @return Returns the viewManager.
343 	 */
344 	public EntityViewManager getEntityViewManager() {
345 		return entityViewManager;
346 	}
347 
348 	/***
349 	 * @param viewManager
350 	 *               The viewManager to set.
351 	 */
352 	public void setEntityViewManager(EntityViewManager viewManager) {
353 		this.entityViewManager = viewManager;
354 	}
355 
356 } //  @jve:visual-info decl-index=0 visual-constraint="10,10"