View Javadoc

1   /*
2    * Copyright (c) 2003-2008 by Cosylab d. d.
3    *
4    * This file is part of CosyBeans.
5    *
6    * CosyBeans is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU General Public License as published by
8    * the Free Software Foundation, either version 3 of the License, or
9    * (at your option) any later version.
10   *
11   * CosyBeans 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
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU General Public License
17   * along with CosyBeans.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  
20  package com.cosylab.gui.adapters;
21  
22  import java.awt.Component;
23  import java.beans.PropertyChangeEvent;
24  import java.beans.PropertyChangeListener;
25  import java.beans.PropertyEditor;
26  import java.beans.PropertyEditorSupport;
27  import java.beans.PropertyVetoException;
28  
29  import com.cosylab.gui.displayers.ConvertibleDisplayer;
30  
31  /**
32   * 
33   * @author Jaka Bobnar, Cosylab
34   *
35   */
36  public class ConverterCustomizerPropertyEditor extends PropertyEditorSupport
37  		implements PropertyEditor {
38  	private static final long serialVersionUID = 1L;
39  
40  	ConverterCustomizer customizer = null;
41  
42  	public ConverterCustomizerPropertyEditor() {
43  		super();
44  	}
45  
46  	/*
47  	 * (non-Javadoc)
48  	 * @see java.beans.PropertyEditorSupport#getCustomEditor()
49  	 */
50  	public Component getCustomEditor() {
51  		if (customizer == null) {
52  			customizer = new ConverterCustomizer();
53  			customizer.addPropertyChangeListener(ConvertibleDisplayer.CONVERTER_PROPERTY,
54  					new PropertyChangeListener() {
55  						public void propertyChange(PropertyChangeEvent e) {
56  							setValueSilently(customizer.getConverter());
57  						}
58  					});
59  		}
60  		return customizer;
61  	}
62  
63  	private void setValueSilently(Converter value) {
64  		super.setValue(value);
65  	}
66  
67  	/*
68  	 * (non-Javadoc)
69  	 * 
70  	 * @see java.beans.PropertyEditorSupport#setValue(java.lang.Object)
71  	 */
72  	public void setValue(Object value) {
73  		super.setValue(value);
74  		if (customizer != null) {
75  			customizer.setConverter((Converter) value);
76  		}
77  	}
78  	
79  	/*
80  	 * (non-Javadoc)
81  	 * @see java.beans.PropertyEditorSupport#setAsText(java.lang.String)
82  	 */
83  	public void setAsText(String text) {
84  		String[] values = text.split(";");
85  		Converter[] converters = null;
86  		if (values != null && values.length > 0) {
87  			converters = new Converter[values.length];
88  			for (int i = 0; i < values.length; i++) {
89  				converters[i] = ConverterUtilities.getConverterFromString(values[i]);
90  			}
91  		} else {
92  			converters = new Converter[]{ConverterUtilities.getConverterFromString(text)};
93  		}
94  		try {
95  			setValue(new ConverterChain(converters));
96  		} catch (PropertyVetoException e) {
97  			e.printStackTrace();
98  		}
99      }
100 
101 	/*
102 	 * (non-Javadoc)
103 	 * @see java.beans.PropertyEditorSupport#getJavaInitializationString()
104 	 */
105 	public String getJavaInitializationString() {
106 		if (getValue() != null && getValue() instanceof Converter) {
107 			return ConverterUtilities.getInitializationString((Converter)getValue());
108 		} else {
109 			return "";
110 		}
111 	}
112 	
113 	/*
114 	 * (non-Javadoc)
115 	 * @see java.beans.PropertyEditorSupport#supportsCustomEditor()
116 	 */
117 	public boolean supportsCustomEditor() {
118 		return true;
119 	}
120 }