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.awt.Container;
24  import java.beans.PropertyChangeEvent;
25  import java.beans.PropertyChangeListener;
26  import java.beans.PropertyEditor;
27  import java.beans.PropertyEditorSupport;
28  
29  import com.cosylab.gui.components.util.RunnerHelper;
30  
31  /**
32   * <code>ValueIconPropertyEditor</code> is an implementation of the 
33   * <code>java.beans.PropertyEditor</code> used for creating ValueIconPairs
34   * and setting the value as a bean property.
35   *  
36   * @author Jaka Bobnar, Cosylab
37   *
38   */
39  public class ValueIconPropertyEditor extends PropertyEditorSupport implements PropertyEditor {
40  
41  	private IconCustomizer editor;
42  	 
43  	/**
44  	 * Creates new instance of SelectorPropertyEditor.
45  	 */
46  	public ValueIconPropertyEditor() {
47  		super();
48  	}
49  	
50  	/*
51  	 * (non-Javadoc)
52  	 * @see java.beans.PropertyEditorSupport#getCustomEditor()
53  	 */
54  	public Component getCustomEditor() {
55  		if (editor == null) {
56  			editor = new IconCustomizer();
57  			editor.setIcons((ValueIconPair[]) getValue());
58  			editor.addPropertyChangeListener(IconDisplayer.ICONS_PROPERTY,new PropertyChangeListener() {
59  				public void propertyChange(PropertyChangeEvent evt) {
60  					setValueSilently(editor.getIcons());
61  				}
62  			});
63  			editor.setValueIconMode(true);
64  			editor.setDefaultIconMode(false);
65  			editor.setSize(400,400);
66  		}
67  
68  		return editor;
69      }
70  
71  	/*
72  	 * (non-Javadoc)
73  	 * @see java.beans.PropertyEditorSupport#getJavaInitializationString()
74  	 */
75  	public String getJavaInitializationString() {
76  		StringBuffer str = new StringBuffer("new com.cosylab.gui.ValueIconPair[]{");
77  		if (getValue() == null) return str.toString()+"}";
78  		ValueIconPair[] pairs = (ValueIconPair[]) getValue();
79  		String name;
80  		int index;
81  		for (ValueIconPair pair : pairs) {
82  			name = pair.getIconName(); 
83  			name = name.replace('\\','/');
84  			index = name.indexOf("!/");
85  			if (index > 0) {
86  				name = name.substring(index+2);
87  			}
88  			str.append("new com.cosylab.gui.ValueIconPair(new Long(" +pair.getValue() + "),\"" + name+ "\"),");
89  		}
90  		str.append("}");
91  		
92  		return str.substring(0);
93      }
94  
95  	/*
96  	 * (non-Javadoc)
97  	 * @see java.beans.PropertyEditorSupport#supportsCustomEditor()
98  	 */
99  	public boolean supportsCustomEditor() {
100 	    return true;
101     }
102 	
103 //	/*
104 //	 * (non-Javadoc)
105 //	 * @see java.beans.PropertyEditorSupport#setAsText(java.lang.String)
106 //	 */
107 //	public void setAsText(String text) {
108 //		String[] values = text.split(";");
109 //		ValueIconPair[] pairs;
110 //		Icon icon;
111 //		Long value;
112 //		if (values != null && values.length > 0) {
113 //			String[] d;
114 //			pairs = new ValueIconPair[values.length];
115 //			for (int i = 0; i < values.length; i++) {
116 //				d = values[i].split("=");
117 //				value = Long.parseLong(d[0].trim());
118 //				icon = IconHelper.createIcon(d[1].trim());
119 //				pairs[i] = new ValueIconPair(value,icon);
120 //			}
121 //		} else {
122 //			values = text.split("=");
123 //			value = Long.parseLong(values[0].trim());
124 //			icon = IconHelper.createIcon(values[1].trim());
125 //			pairs = new ValueIconPair[]{new ValueIconPair(value,icon)};
126 //		}
127 //		setValue(pairs);
128 //    }
129 	
130 	public String getAsText() {
131 		ValueIconPair[] pairs = (ValueIconPair[]) getValue();	
132 	    if (pairs == null || pairs.length == 0) return "";
133 	    StringBuffer buffer = new StringBuffer();
134 	    for (ValueIconPair cp : pairs) {
135 	    	buffer.append(cp.toString() + ";");
136 	    }
137 	    return buffer.toString();
138 	}
139 	
140 	private void setValueSilently(Object value) {
141 		super.setValue(value);
142 	}
143 	
144 	/* (non-Javadoc)
145 	 * @see java.beans.PropertyEditorSupport#setValue(java.lang.Object)
146 	 */
147 	public void setValue(Object value) {
148 		super.setValue(value);
149 		if (editor!=null) {
150 			editor.setIcons((ValueIconPair[]) value);
151 		}
152 	}
153 	
154 	public static void main(String[] args) {
155 		ValueIconPropertyEditor ed = new ValueIconPropertyEditor();
156 		ed.addPropertyChangeListener(new PropertyChangeListener() {
157 			public void propertyChange(PropertyChangeEvent evt) {
158 				System.out.println(evt);
159 				
160 			}
161 		});
162 		RunnerHelper.runComponent((Container) ed.getCustomEditor(), 500,500);
163 	}
164 }
165 
166 
167 /* __oOo__ */