View Javadoc

1   /*
2    * Copyright (c) 2003-2008 by Cosylab d. d.
3    *
4    * This file is part of CosyBeans-Common.
5    *
6    * CosyBeans-Common 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-Common 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-Common.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  
20  package com.cosylab.gui.components;
21  
22  import java.awt.Component;
23  import java.beans.PropertyChangeEvent;
24  import java.beans.PropertyChangeListener;
25  import java.beans.PropertyEditorSupport;
26  
27  import com.cosylab.gui.components.range2.RangedValuePolicy;
28  
29  /**
30   * RangedValuePolicy property editor for gui builders.
31   * 
32   * @author <a href="mailto:jaka.bobnar@cosylab.com">Jaka Bobnar</a>
33   * @version $Id: ValuePolicyPropertyEditor.java,v 1.7 2008-04-22 12:28:40 jbobnar Exp $
34   *
35   */
36  public class ValuePolicyPropertyEditor extends PropertyEditorSupport {
37  	
38  	private ValuePolicyCustomizer editor;
39  	 
40  	/*
41  	 * (non-Javadoc)
42  	 * @see java.beans.PropertyEditorSupport#getCustomEditor()
43  	 */
44  	public Component getCustomEditor() {
45  		if (editor == null) {
46  			editor = new ValuePolicyCustomizer();
47  			editor.setValuePolicy((RangedValuePolicy)getValue());
48  			editor.addPropertyChangeListener("valuePolicy", new PropertyChangeListener() {
49  				public void propertyChange(PropertyChangeEvent evt) {
50  					setValueSilently(editor.getValuePolicy());
51  				}
52  			});
53  		}
54  
55  		return editor;
56      }
57  
58  	/*
59  	 * (non-Javadoc)
60  	 * @see java.beans.PropertyEditorSupport#getJavaInitializationString()
61  	 */
62  	public String getJavaInitializationString() {
63  		return "new " + getValue().getClass().getName()+"()";
64      }
65  
66  	/*
67  	 * (non-Javadoc)
68  	 * @see java.beans.PropertyEditorSupport#supportsCustomEditor()
69  	 */
70  	public boolean supportsCustomEditor() {
71  	    return true;
72      }
73  	
74  	private void setValueSilently(Object value) {
75  		super.setValue(value);
76  	}
77  	
78  	/* (non-Javadoc)
79  	 * @see java.beans.PropertyEditorSupport#setValue(java.lang.Object)
80  	 */
81  	@Override
82  	public void setValue(Object value) {
83  		super.setValue(value);
84  		if (editor!=null) {
85  			editor.setValuePolicy((RangedValuePolicy)value);
86  		}
87  	}
88  }