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>int</code>.
22 *
23 * @author scheglov_ke
24 * @coverage core.model.property.editor
25 */
26public final class IntegerPropertyEditor extends AbstractTextPropertyEditor {
27  ////////////////////////////////////////////////////////////////////////////
28  //
29  // Instance
30  //
31  ////////////////////////////////////////////////////////////////////////////
32  public static final IntegerPropertyEditor INSTANCE = new IntegerPropertyEditor();
33
34  private IntegerPropertyEditor() {
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 Integer) {
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      return true;
68    }
69    // prepare value
70    Integer value;
71    try {
72      value = Integer.valueOf(text);
73    } catch (Throwable e) {
74      UiUtils.openWarning(
75          DesignerPlugin.getShell(),
76          property.getTitle(),
77          MessageFormat.format(ModelMessages.IntegerPropertyEditor_notValidInt, text));
78      return false;
79    }
80    // modify property
81    property.setValue(value);
82    return true;
83  }
84}
85