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.binding;
19  
20  import java.util.HashMap;
21  import java.util.Map;
22  
23  import org.vectomatic.dom.svg.OMSVGLength;
24  import org.vectomatic.dom.svg.OMSVGSVGElement;
25  import org.vectomatic.dom.svg.utils.SVGConstants;
26  import org.vectomatic.svg.edit.client.command.GenericEditCommandFactory;
27  import org.vectomatic.svg.edit.client.engine.SVGModel;
28  import org.vectomatic.svg.edit.client.gxt.form.ImageHrefField;
29  import org.vectomatic.svg.edit.client.model.svg.SVGImageElementModel;
30  import org.vectomatic.svg.edit.client.model.svg.SVGLength;
31  
32  import com.extjs.gxt.ui.client.binding.FieldBinding;
33  import com.extjs.gxt.ui.client.data.ModelData;
34  import com.extjs.gxt.ui.client.util.Size;
35  import com.google.gwt.event.logical.shared.ValueChangeEvent;
36  import com.google.gwt.event.logical.shared.ValueChangeHandler;
37  import com.google.gwt.event.shared.HandlerRegistration;
38  
39  /**
40   * Binding class for ImageHrefField
41   * @author laaglu
42   */
43  public class ImageHrefFieldBinding extends FieldBinding implements ValueChangeHandler<Size> {
44  	private HandlerRegistration registration;
45  	
46  	public ImageHrefFieldBinding(ImageHrefField field, String property) {
47  		super(field, property);
48  	}
49  	public void updateModel() {
50  		ImageHrefField field = (ImageHrefField)this.field;
51  		SVGImageElementModel model = (SVGImageElementModel)this.model;
52  		model.setResourceName(field.getResourceName());
53  		super.updateModel();
54  	}
55  	@Override
56  	public void onValueChange(ValueChangeEvent<Size> event) {
57  		GenericEditCommandFactory factory = GenericEditCommandFactory.INSTANTIATOR.create();
58  		factory.start(this);
59  		Size size = event.getValue();
60  		SVGImageElementModel imageModel = (SVGImageElementModel) model;
61  		SVGModel owner = imageModel.getOwner();
62  		OMSVGSVGElement svg = owner.getSvgElement();
63  		Map<String, Object> oldValues = new HashMap<String, Object>();
64  		oldValues.put(SVGConstants.SVG_WIDTH_ATTRIBUTE, imageModel.get(SVGConstants.SVG_WIDTH_ATTRIBUTE));
65  		oldValues.put(SVGConstants.SVG_HEIGHT_ATTRIBUTE, imageModel.get(SVGConstants.SVG_HEIGHT_ATTRIBUTE));
66  		imageModel.setSilent(true);
67  		imageModel.set(SVGConstants.SVG_WIDTH_ATTRIBUTE, new SVGLength(svg.createSVGLength(OMSVGLength.SVG_LENGTHTYPE_PX, size.width)));
68  		imageModel.set(SVGConstants.SVG_HEIGHT_ATTRIBUTE, new SVGLength(svg.createSVGLength(OMSVGLength.SVG_LENGTHTYPE_PX, size.height)));
69  		imageModel.setSilent(false);
70  		owner.getCommandStore().addCommand(factory.createCommand(imageModel, oldValues));
71  	}
72  	
73  	@Override
74  	public void bind(ModelData model) {
75  		super.bind(model);
76  		ImageHrefField imageField = (ImageHrefField)field;
77  		registration = imageField.addValueChangeHandler(this);
78  	}
79  	
80  	@Override
81  	public void unbind() {
82  		super.unbind();
83  		registration.removeHandler();
84  	}
85  }