View Javadoc

1   /**********************************************
2    * Copyright (C) 2011 Lukas Laag
3    * This file is part of svgreal.
4    * 
5    * svgreal is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    * 
10   * svgreal is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   * 
15   * You should have received a copy of the GNU General Public License
16   * along with svgreal.  If not, see http://www.gnu.org/licenses/
17   **********************************************/
18  package org.vectomatic.svg.edit.client.gxt.form;
19  
20  import java.util.Arrays;
21  
22  import org.vectomatic.svg.edit.client.gxt.widget.SpinnerFieldExt;
23  import org.vectomatic.svg.edit.client.model.svg.SVGLength;
24  
25  import com.extjs.gxt.ui.client.widget.Html;
26  import com.extjs.gxt.ui.client.widget.LayoutContainer;
27  import com.extjs.gxt.ui.client.widget.form.AdapterField;
28  import com.extjs.gxt.ui.client.widget.form.SimpleComboBox;
29  import com.extjs.gxt.ui.client.widget.layout.ColumnData;
30  import com.extjs.gxt.ui.client.widget.layout.ColumnLayout;
31  import com.google.gwt.dom.client.Style;
32  import com.google.gwt.i18n.client.NumberFormat;
33  
34  /**
35   * Field subclass to edit SVGLength values
36   * @author laaglu
37   */
38  public class SVGLengthField extends AdapterField {
39  	private class SVGLengthPanel extends LayoutContainer {
40  		private SpinnerFieldExt valueField;
41  		private SimpleComboBox<Style.Unit> unitField;
42  	
43  		/**
44  		 * Constructor
45  		 * @param metadata metadata used to label the field.
46  		 */
47  		public SVGLengthPanel(float min, float max, float increment) {
48  		    valueField = new SpinnerFieldExt();
49  		    valueField.setPropertyEditorType(Float.class);
50  		    valueField.setAllowDecimals(true);
51  		    valueField.setAllowNegative(true);
52  		    valueField.setFormat(NumberFormat.getFormat("#0.0"));
53  		    valueField.setMinValue(min);
54  		    valueField.setMaxValue(max);
55  		    valueField.setIncrement(increment);
56  		    
57  			unitField = new SimpleComboBox<Style.Unit>();
58  			unitField.setLazyRender(false);
59  			unitField.add(Arrays.asList(Style.Unit.values()));
60  
61  			setLayout(new ColumnLayout());
62  			add(valueField, new ColumnData(.775f));
63  			add(new Html("&nbsp;"), new ColumnData(.025f));
64  			add(unitField, new ColumnData(.2f));
65  			valueField.setFireChangeEventOnSetValue(true);
66  			unitField.setFireChangeEventOnSetValue(true);
67  
68  			valueField.setFireChangeEventOnSetValue(true);
69  			unitField.setFireChangeEventOnSetValue(true);
70  		}
71  		
72  		public void update(SVGLength length) {
73  			if (length != null) {
74  				if (valueField.getValue() == null || length.getValue() != valueField.getValue().floatValue()) {
75  					valueField.setFireChangeEventOnSetValue(false);
76  					valueField.setValue(length.getValue());
77  					valueField.setFireChangeEventOnSetValue(true);
78  				}
79  				if (unitField.getValue() == null || length.getUnit() != unitField.getValue().getValue()) {
80  					unitField.setFireChangeEventOnSetValue(false);
81  					unitField.setValue(unitField.findModel(length.getUnit()));
82  					unitField.setFireChangeEventOnSetValue(true);
83  				}
84  			}
85  		}
86  	}
87  
88  	public SVGLengthField(float min, float max, float increment) {
89  		super(null);
90  		widget = new SVGLengthPanel(min, max, increment);
91  		setResizeWidget(true);
92  		setFireChangeEventOnSetValue(true);
93  	}
94  
95  	@Override
96  	public void setValue(Object value) {
97  		((SVGLengthPanel)widget).update((SVGLength)value);
98  		super.setValue(value);
99  	}
100 	
101 	@Override
102 	public Object getValue() {
103 		return value;
104 	}
105 	
106 	public SpinnerFieldExt getValueField() {
107 		return ((SVGLengthPanel)widget).valueField;
108 	}
109 	
110 	public SimpleComboBox<Style.Unit> getUnitField() {
111 		return ((SVGLengthPanel)widget).unitField;
112 	}
113 
114 }