View Javadoc

1   /*
2    *  Copyright (C) 2003-2005 SINTEF
3    *  Author:  Fredrik Vraalsen (fredrik dot vraalsen at sintef dot no)
4    *  Webpage: http://coras.sourceforge.net/
5    *
6    *  This program is free software; you can redistribute it and/or
7    *  modify it under the terms of the GNU Lesser General Public License
8    *  as published by the Free Software Foundation; either version 2.1
9    *  of the License, or (at your option) any later version.
10   *
11   *  This program is distributed in the hope that it will be useful,
12   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   *  Lesser General Public License for more details.
15   *
16   *  You should have received a copy of the GNU Lesser General Public
17   *  License along with this program; if not, write to the Free
18   *  Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19   *  02111-1307 USA
20   */
21  package coras.client.ui;
22  
23  import java.awt.GridBagConstraints;
24  import java.awt.GridBagLayout;
25  import java.beans.PropertyChangeEvent;
26  import java.beans.PropertyChangeListener;
27  import java.util.Observable;
28  import java.util.Observer;
29  
30  import javax.swing.DefaultComboBoxModel;
31  import javax.swing.JLabel;
32  import javax.swing.JPanel;
33  import javax.swing.JScrollPane;
34  import javax.swing.JTextField;
35  import javax.swing.event.DocumentEvent;
36  import javax.swing.event.DocumentListener;
37  import javax.swing.text.Document;
38  
39  import coras.client.SwingWorker;
40  import coras.common.CorasPackage;
41  import coras.reuse.CategoryEnum;
42  import coras.reuse.ExperiencePackage;
43  /***
44   * @author fvr
45   *
46   * TODO To change the template for this generated type comment go to
47   * Window - Preferences - Java - Code Style - Code Templates
48   */
49  public class PackageDetailsPanel extends JPanel {
50  
51  	public static final String PACKAGE_NAME = "packageName";
52  	public static final String PACKAGE_SHORT_DESCRIPTION = "packageShortDescription";
53  	public static final String PACKAGE_FULL_DESCRIPTION = "packageFullDescription";
54  	public static final String PACKAGE_DOMAIN = "packageDomain";
55  	public static final String PACKAGE_CATEGORY = "packageCategory";
56  	
57  	private CorasPackage _package = null;
58      private boolean editable = false;
59  
60  	private JLabel nameLabel = null;
61  	private JLabel shortDescriptionLabel = null;
62  	private JLabel fullDescriptionLabel = null;
63  	private JTextField nameField = null;
64  	private JTextField shortDescriptionField = null;
65  	// private JTextArea fullDescriptionArea = null;
66  	private CorasTextArea fullDescriptionArea = null;
67  	
68  	private JScrollPane descriptionScrollPane = null;
69  	private JLabel domainLabel = null;
70  	private CorasComboBox domainBox = null;
71  
72  	private JLabel categoryLabel = null;
73  	private CorasComboBox categoryBox = null;
74      /***
75  	 * This is the default constructor
76  	 */
77  	public PackageDetailsPanel() {
78  		super();
79  		initialize();
80  	}
81  	
82  	public synchronized void setPackage(CorasPackage newPackage) {
83  	    if (_package == newPackage) {
84  	        return;
85  	    }
86  	    if (_package != null) {
87  	        _package.deleteObserver(observer);
88  	    }
89  	    _package = newPackage;
90  	    if (_package != null) {
91  	        _package.addObserver(observer);
92  	    }
93  	    observer.update(_package, null);
94  	}
95  
96  	public boolean isEditable() {
97  	    return editable;
98  	}
99  	
100 	public boolean isDirty() {
101 		boolean clean = 
102 			_package != null &&
103 			getPackageName().equals(_package.getName()) &&
104 			getShortDescription().equals(_package.getShortDescription()) &&
105 			getFullDescription().equals(_package.getFullDescription());
106 		if (_package instanceof ExperiencePackage) {
107 			ExperiencePackage xp = (ExperiencePackage) _package;
108 			clean &= 
109 				getDomain().equals(xp.getDomain()) &&
110 				getCategory().equals(xp.getCategory());
111 		}
112 		return !clean;
113 	}
114 	
115 	public synchronized void setEditable(boolean editable) {
116 	    if (this.editable == editable) {
117 	        return;
118 	    }
119 	    this.editable = editable;
120 	    getNameField().setEditable(editable);
121 	    getShortDescriptionField().setEditable(editable);
122 	    getDomainBox().setEditable(editable);
123 	    getFullDescriptionArea().setEditable(editable);
124 	}
125 	
126 	public boolean isCategoryEnabled() {
127 	    return getCategoryBox().isValid();
128 	}
129 	
130 	public void setCategoryEnabled(boolean enabled) {
131 	    categoryLabel.setVisible(enabled);
132 	    getCategoryBox().setVisible(enabled);
133 	}
134 	
135 	public boolean isCategoryEditable() {
136 	    return getCategoryBox().isEditable();
137 	}
138 	
139 	public void setCategoryEditable(boolean editable) {
140 	    getCategoryBox().setEditable(editable);
141 	}
142 
143 	public boolean isDomainEnabled() {
144 	    return getDomainBox().isVisible();
145 	}
146 	
147 	public void setDomainEnabled(boolean enabled) {
148 	    domainLabel.setVisible(enabled);
149 	    getDomainBox().setVisible(enabled);
150 	}
151 
152     public boolean isDomainEditable() {
153         return getDomainBox().isComboBoxEditable();
154     }
155     
156     public void setDomainEditable(boolean editable) {
157         getDomainBox().setComboBoxEditable(editable);
158     }
159 	
160     /***
161      * @return
162      */
163     public String getPackageName() {
164         return getNameField().getText();
165     }
166 
167     /***
168      * @return
169      */
170     public String getShortDescription() {
171         return getShortDescriptionField().getText();
172     }
173     
174     public CategoryEnum getCategory() {
175         return (CategoryEnum) getCategoryBox().getSelectedItem();
176     }
177 
178     public String getDomain() {
179         Object item = getDomainBox().getSelectedItem();
180         if (item == null || item instanceof String) {
181             return (String) item;
182         } else {
183             // FIXME error
184             return null;
185         }
186     }
187     
188     /***
189      * @return
190      */
191     public String getFullDescription() {
192         return getFullDescriptionArea().getText();
193     }
194     
195     private Observer observer = new Observer() {
196     	public void update(Observable o, Object arg) {
197     		if ((o != null) && !(o instanceof CorasPackage)) {
198     			System.err.println("ERROR: PackageDetailsPanel should only observe CorasPackages");
199     			return;
200     		}
201     		CorasPackage _package = (CorasPackage) o;
202     		getNameField().setText(_package != null ? _package.getName() : "");
203     		getShortDescriptionField().setText(_package != null ? _package.getShortDescription() : "");
204     		if (_package instanceof ExperiencePackage) {
205     			String domain = ((ExperiencePackage)_package).getDomain();
206     			getDomainBox().setSelectedItem(domain);
207     		}
208     		getFullDescriptionArea().setText(_package != null ? _package.getFullDescription() : "");
209     	}
210     };
211 	
212 	private PropertyChangeListener propListener = new PropertyChangeListener() {
213 		public void propertyChange(final PropertyChangeEvent e) {
214 			new SwingWorker() {
215 				public Object construct() {
216 					String property = e.getPropertyName();
217 					Object source = e.getSource();
218 					if (source == getCategoryBox() && property == CorasComboBox.SELECTED_ITEM) {
219 						firePropertyChange(PACKAGE_CATEGORY, null, getDomain());
220 					} else if (source == getDomainBox() && property == CorasComboBox.SELECTED_ITEM) {
221 						firePropertyChange(PACKAGE_DOMAIN, null, getDomain());
222 					} else {
223 						System.out.println("Unknown property or source: " + property + " / " + source);
224 					}
225 					return null;
226 				}
227 			}.start();
228 		}
229 	};
230 	
231 	private DocumentListener docListener = new DocumentListener() {
232 		public void changedUpdate(DocumentEvent e) {
233 			// Do nothing
234 			System.out.println("This should never happen with non-styled documents");
235 		}
236 		public void insertUpdate(DocumentEvent e) {
237 			firePropertyChange(e);
238 		}
239 		public void removeUpdate(DocumentEvent e) {
240 			firePropertyChange(e);
241 		}
242 	};
243 	
244 	private void firePropertyChange(final DocumentEvent e) {
245 		new SwingWorker() {
246 			public Object construct() {
247 				Document doc = e.getDocument();
248 				if (doc == getNameField().getDocument()) {
249 					firePropertyChange(PACKAGE_NAME, null, getPackageName());
250 				} else if (doc == getShortDescriptionField().getDocument()) {
251 					firePropertyChange(PACKAGE_SHORT_DESCRIPTION, null, getShortDescription());
252 				} else if (doc == getFullDescriptionArea().getDocument()) {
253 					firePropertyChange(PACKAGE_FULL_DESCRIPTION, null, getFullDescription());
254 				} else {
255 					System.out.println("Unknown document: " + doc);
256 				}
257 				return null;
258 			}
259 		}.start();
260 	}
261 
262 	/***
263 	 * This method initializes this
264 	 * 
265 	 * @return void
266 	 */
267 	private  void initialize() {
268 		GridBagConstraints gridBagConstraints51 = new GridBagConstraints();
269 		categoryLabel = new JLabel();
270 		domainLabel = new JLabel();
271 		GridBagConstraints gridBagConstraints24 = new GridBagConstraints();
272 		fullDescriptionLabel = new JLabel();
273 		shortDescriptionLabel = new JLabel();
274 		nameLabel = new JLabel();
275 		GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
276 		GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
277 		GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
278 		GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
279 		GridBagConstraints gridBagConstraints5 = new GridBagConstraints();
280 		GridBagConstraints gridBagConstraints6 = new GridBagConstraints();
281 		GridBagConstraints gridBagConstraints32 = new GridBagConstraints();
282 		GridBagConstraints gridBagConstraints21 = new GridBagConstraints();
283 		GridBagConstraints gridBagConstraints31 = new GridBagConstraints();
284 		this.setLayout(new GridBagLayout());
285 		this.setSize(300,200);
286 		gridBagConstraints1.gridx = 0;
287 		gridBagConstraints1.gridy = 0;
288 		gridBagConstraints1.anchor = java.awt.GridBagConstraints.WEST;
289 		gridBagConstraints1.insets = new java.awt.Insets(0,0,0,0);
290 		nameLabel.setText("Name:");
291 		gridBagConstraints2.gridx = 0;
292 		gridBagConstraints2.gridy = 1;
293 		gridBagConstraints2.anchor = java.awt.GridBagConstraints.WEST;
294 		gridBagConstraints2.insets = new java.awt.Insets(0,0,0,0);
295 		shortDescriptionLabel.setText("Description:");
296 		gridBagConstraints3.gridx = 0;
297 		gridBagConstraints3.gridy = 4;
298 		gridBagConstraints3.anchor = java.awt.GridBagConstraints.WEST;
299 		fullDescriptionLabel.setText("Full description:");
300 		gridBagConstraints4.gridx = 1;
301 		gridBagConstraints4.gridy = 0;
302 		gridBagConstraints4.weightx = 1.0;
303 		gridBagConstraints4.fill = java.awt.GridBagConstraints.HORIZONTAL;
304 		gridBagConstraints5.gridx = 1;
305 		gridBagConstraints5.gridy = 1;
306 		gridBagConstraints5.weightx = 1.0;
307 		gridBagConstraints5.fill = java.awt.GridBagConstraints.HORIZONTAL;
308 		gridBagConstraints6.gridx = 0;
309 		gridBagConstraints6.gridy = 4;
310 		gridBagConstraints6.weightx = 1.0;
311 		gridBagConstraints6.weighty = 1.0;
312 		gridBagConstraints6.fill = java.awt.GridBagConstraints.BOTH;
313 		gridBagConstraints6.gridwidth = 2;
314 		gridBagConstraints24.gridx = 0;
315 		gridBagConstraints24.gridy = 5;
316 		gridBagConstraints24.weightx = 1.0;
317 		gridBagConstraints24.weighty = 1.0;
318 		gridBagConstraints24.fill = java.awt.GridBagConstraints.BOTH;
319 		gridBagConstraints24.gridwidth = 2;
320 		gridBagConstraints21.gridx = 0;
321 		gridBagConstraints21.gridy = 3;
322 		gridBagConstraints21.anchor = java.awt.GridBagConstraints.WEST;
323 		domainLabel.setText("Domain:");
324 		gridBagConstraints31.gridx = 1;
325 		gridBagConstraints31.gridy = 3;
326 		gridBagConstraints31.fill = java.awt.GridBagConstraints.HORIZONTAL;
327 		gridBagConstraints32.gridx = 0;
328 		gridBagConstraints32.gridy = 2;
329 		gridBagConstraints32.anchor = java.awt.GridBagConstraints.WEST;
330 		categoryLabel.setText("Category:");
331 		gridBagConstraints51.gridx = 1;
332 		gridBagConstraints51.gridy = 2;
333 		gridBagConstraints51.fill = java.awt.GridBagConstraints.HORIZONTAL;
334 		this.add(nameLabel, gridBagConstraints1);
335 		this.add(getNameField(), gridBagConstraints4);
336 		this.add(shortDescriptionLabel, gridBagConstraints2);
337 		this.add(getShortDescriptionField(), gridBagConstraints5);
338 		this.add(domainLabel, gridBagConstraints21);
339 		this.add(getDomainBox(), gridBagConstraints31);
340 		this.add(fullDescriptionLabel, gridBagConstraints3);
341 		this.add(getDescriptionScrollPane(), gridBagConstraints24);
342 		this.add(categoryLabel, gridBagConstraints32);
343 		this.add(getCategoryBox(), gridBagConstraints51);
344 	}
345 	/***
346 	 * This method initializes jTextField	
347 	 * 	
348 	 * @return javax.swing.JTextField	
349 	 */    
350 	private JTextField getNameField() {
351 		if (nameField == null) {
352 			nameField = new JTextField();
353 			nameField.setEditable(editable);
354 			// nameField.setBackground(java.awt.Color.white);
355 			nameField.getDocument().addDocumentListener(docListener);
356 		}
357 		return nameField;
358 	}
359 	/***
360 	 * This method initializes jTextField1	
361 	 * 	
362 	 * @return javax.swing.JTextField	
363 	 */    
364 	private JTextField getShortDescriptionField() {
365 		if (shortDescriptionField == null) {
366 			shortDescriptionField = new JTextField();
367 			shortDescriptionField.setEditable(editable);
368 			shortDescriptionField.getDocument().addDocumentListener(docListener);
369 			// shortDescriptionField.setBackground(java.awt.Color.white);
370 		}
371 		return shortDescriptionField;
372 	}
373 	/***
374 	 * This method initializes jTextArea	
375 	 * 	
376 	 * @return javax.swing.JTextArea	
377 	 */    
378 	private CorasTextArea getFullDescriptionArea() {
379 		if (fullDescriptionArea == null) {
380 		    // fullDescriptionArea = new JTextArea();
381 			fullDescriptionArea = new CorasTextArea();
382 			fullDescriptionArea.setEditable(editable);
383 			// fullDescriptionArea.setBackground(java.awt.Color.white);
384 			fullDescriptionArea.setLineWrap(true);
385 			fullDescriptionArea.setWrapStyleWord(true);
386 			fullDescriptionArea.getDocument().addDocumentListener(docListener);
387 		}
388 		return fullDescriptionArea;
389 	}
390 	
391 	/***
392 	 * This method initializes descriptionScrollPane	
393 	 * 	
394 	 * @return javax.swing.JScrollPane	
395 	 */    
396 	private JScrollPane getDescriptionScrollPane() {
397 		if (descriptionScrollPane == null) {
398 			descriptionScrollPane = new JScrollPane();
399 			descriptionScrollPane.setViewportView(getFullDescriptionArea());
400 			descriptionScrollPane.setHorizontalScrollBarPolicy(javax.swing.JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
401 			descriptionScrollPane.setViewportView(getFullDescriptionArea());
402 		}
403 		return descriptionScrollPane;
404 	}
405 
406 	/***
407 	 * This method initializes corasChoiceBox	
408 	 * 	
409 	 * @return coras.client.ui.CorasChoiceBox	
410 	 */    
411 	private CorasComboBox getDomainBox() {
412 		if (domainBox == null) {
413 			domainBox = new CorasComboBox();
414 			domainBox.setEditable(editable);
415 			domainBox.setModel(new DefaultComboBoxModel(new String[] {"General", "eCommerce", "Telemedicine"} ));
416 			domainBox.addPropertyChangeListener(CorasComboBox.SELECTED_ITEM, propListener);
417 		}
418 		return domainBox;
419 	}
420 	/***
421 	 * This method initializes corasComboBox	
422 	 * 	
423 	 * @return coras.client.ui.CorasComboBox	
424 	 */    
425 	private CorasComboBox getCategoryBox() {
426 		if (categoryBox == null) {
427 			categoryBox = new CorasComboBox();
428 			categoryBox.setEditable(editable);
429 			categoryBox.setComboBoxEditable(false);
430 			categoryBox.setModel(new DefaultComboBoxModel(CategoryEnum.VALUES.toArray()));
431 			domainBox.addPropertyChangeListener(CorasComboBox.SELECTED_ITEM, propListener);
432 		}
433 		return categoryBox;
434 	}
435   }