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>short</code>.
22 *
23 * @author scheglov_ke
24 * @coverage core.model.property.editor
25 */
26public class ShortPropertyEditor extends AbstractTextPropertyEditor {
27  ////////////////////////////////////////////////////////////////////////////
28  //
29  // Instance
30  //
31  ////////////////////////////////////////////////////////////////////////////
32  public static final PropertyEditor INSTANCE = new ShortPropertyEditor();
33
34  private ShortPropertyEditor() {
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 Short) {
46      return value.toString();
47    }
48    return null;
49  }
50
51  ////////////////////////////////////////////////////////////////////////////
52  //
53  // Editing
54  //
55  ////////////////////////////////////////////////////////////////////////////
56  @Override
57  protected String getEditorText(Property property) throws Exception {
58    return getText(property);
59  }
60
61  @Override
62  protected boolean setEditorText(Property property, String text) throws Exception {
63    text = text.trim();
64    // check for delete
65    if (text.length() == 0) {
66      property.setValue(Property.UNKNOWN_VALUE);
67    }
68    // prepare value
69    Short value;
70    try {
71      value = Short.valueOf(text);
72    } catch (Throwable e) {
73      UiUtils.openWarning(
74          DesignerPlugin.getShell(),
75          property.getTitle(),
76          MessageFormat.format(ModelMessages.ShortPropertyEditor_notValidShort, text));
77      return false;
78    }
79    // modify property
80    property.setValue(value);
81    return true;
82  }
83}