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;
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  
28  /**
29   * <code>IconPropertyEditor</code> is an implementation of the 
30   * <code>java.beans.PropertyEditor</code> which enables setting of an icon.
31   * @author Jaka Bobnar, Cosylab
32   *
33   */
34  public class IconPropertyEditor extends PropertyEditorSupport implements PropertyEditor {
35  
36  	private IconEditor editor;
37  	 
38  	/**
39  	 * Creates new instance of SelectorPropertyEditor.
40  	 */
41  	public IconPropertyEditor() {
42  		super();
43  	}
44  	
45  	/*
46  	 * (non-Javadoc)
47  	 * @see java.beans.PropertyEditorSupport#getCustomEditor()
48  	 */
49  	public Component getCustomEditor() {
50  		if (editor == null) {
51  			editor = new IconEditor();
52  			editor.setIcon((String)getValue());
53  			editor.addPropertyChangeListener(IconEditor.ICON,new PropertyChangeListener() {
54  				public void propertyChange(PropertyChangeEvent evt) {
55  					setValueSilently(editor.getIconPath());
56  				}
57  			});
58  			editor.setSize(250,80);
59  		}
60  		
61  		return editor;
62      }
63  
64  	/*
65  	 * (non-Javadoc)
66  	 * @see java.beans.PropertyEditorSupport#getJavaInitializationString()
67  	 */
68  	public String getJavaInitializationString() {
69  		return "\"" + getAsText() + "\"";
70  //		if (getValue() == null) return "null";
71  //		String name = getValue().toString(); 
72  //		name = name.replace('\\','/');
73  //		int index = name.indexOf("!/");
74  //		if (index > 0) {
75  //			name = name.substring(index+2);
76  //		}
77  //		return "com.cosylab.gui.components.util.IconHelper.createIcon(\"" + name + "\")";
78      }
79  
80  	/*
81  	 * (non-Javadoc)
82  	 * @see java.beans.PropertyEditorSupport#supportsCustomEditor()
83  	 */
84  	public boolean supportsCustomEditor() {
85  	    return true;
86      }
87  	
88  	private void setValueSilently(Object value) {
89  		super.setValue(value);
90  	}
91  	
92  	/* (non-Javadoc)
93  	 * @see java.beans.PropertyEditorSupport#setValue(java.lang.Object)
94  	 */
95  	public void setValue(Object value) {
96  		super.setValue(value);
97  		if (editor!=null) {
98  			editor.setIcon((String)value);
99  		}
100 	}
101 	
102 }
103 
104 
105 /* __oOo__ */