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