1/*******************************************************************************
2 * Copyright (c) 2011 Google, Inc.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 *    Google, Inc. - initial API and implementation
10 *******************************************************************************/
11package org.eclipse.wb.internal.core.model.property.editor;
12
13import org.eclipse.wb.internal.core.DesignerPlugin;
14import org.eclipse.wb.internal.core.model.ModelMessages;
15import org.eclipse.wb.internal.core.model.property.Property;
16import org.eclipse.wb.internal.core.utils.ui.UiUtils;
17
18import java.text.MessageFormat;
19
20/**
21 * The {@link PropertyEditor} for <code>double</code>.
22 *
23 * @author scheglov_ke
24 * @coverage core.model.property.editor
25 */
26public final class DoublePropertyEditor extends AbstractTextPropertyEditor {
27  ////////////////////////////////////////////////////////////////////////////
28  //
29  // Instance
30  //
31  ////////////////////////////////////////////////////////////////////////////
32  public static final PropertyEditor INSTANCE = new DoublePropertyEditor();
33
34  private DoublePropertyEditor() {
35  }
36
37  ////////////////////////////////////////////////////////////////////////////
38  //
39  // Presentation
40  //
41  ////////////////////////////////////////////////////////////////////////////
42  @Override
43  public String getText(Property property) throws Exception {
44    Object value = property.getValue();
45    if (value instanceof Number) {
46      double doubleValue = ((Number) value).doubleValue();
47      return Double.toString(doubleValue);
48    }
49    return null;
50  }
51
52  ////////////////////////////////////////////////////////////////////////////
53  //
54  // Editing
55  //
56  ////////////////////////////////////////////////////////////////////////////
57  @Override
58  protected String getEditorText(Property property) throws Exception {
59    return getText(property);
60  }
61
62  @Override
63  protected boolean setEditorText(Property property, String text) throws Exception {
64    text = text.trim();
65    // check for delete
66    if (text.length() == 0) {
67      property.setValue(Property.UNKNOWN_VALUE);
68    }
69    // prepare value
70    Double value;
71    try {
72      value = Double.valueOf(text);
73    } catch (Throwable e) {
74      UiUtils.openWarning(
75          DesignerPlugin.getShell(),
76          property.getTitle(),
77          MessageFormat.format(ModelMessages.DoublePropertyEditor_notValidDouble, text));
78      return false;
79    }
80    // modify property
81    property.setValue(value);
82    return true;
83  }
84}