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 org.vectomatic.svg.edit.client.gxt.form.ResetButtonField; 21 import org.vectomatic.svg.edit.client.model.CssMetadata; 22 import org.vectomatic.svg.edit.client.model.svg.SVGStyledElementModel; 23 24 import com.extjs.gxt.ui.client.binding.FieldBinding; 25 26 /** 27 * Binds a reset CSS property button with a 28 * CSS property of an SVGStyleElementModel. 29 * @author laaglu 30 */ 31 public class ResetButtonBinding extends FieldBinding { 32 33 public ResetButtonBinding(ResetButtonField field, String property) { 34 super(field, property); 35 } 36 37 private static boolean equals(Object v1, Object v2) { 38 if (v1 != null) { 39 return v1.equals(v2); 40 } 41 return v2 == null; 42 } 43 44 @Override 45 protected Object onConvertModelValue(Object value) { 46 // Enable the reset button only if the field does not 47 // have the default value. 48 CssMetadata<?> metadata = (CssMetadata<?>) ((SVGStyledElementModel)model).getMetaModel().getMetadata(property); 49 field.setEnabled(!equals(metadata.getDefaultValue(), value)); 50 return value; 51 } 52 53 @Override 54 public void updateModel() { 55 // create a command to record the update to the default value 56 super.updateModel(); 57 58 // since the model value is now the default value, the CSS 59 // property can be removed 60 model.remove(ResetButtonBinding.this.property); 61 } 62 }