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 String}.
22 *
23 * @author scheglov_ke
24 * @coverage core.model.property.editor
25 */
26public final class CharacterPropertyEditor extends AbstractTextPropertyEditor {
27  ////////////////////////////////////////////////////////////////////////////
28  //
29  // Instance
30  //
31  ////////////////////////////////////////////////////////////////////////////
32  public static final PropertyEditor INSTANCE = new CharacterPropertyEditor();
33
34  private CharacterPropertyEditor() {
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 Character) {
46      return String.valueOf(((Character) value).charValue());
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    // check for delete
64    if (text.length() == 0) {
65      property.setValue(Property.UNKNOWN_VALUE);
66      return true;
67    }
68    // only one character
69    if (text.length() > 1) {
70      UiUtils.openWarning(
71          DesignerPlugin.getShell(),
72          property.getTitle(),
73          MessageFormat.format(ModelMessages.CharacterPropertyEditor_notValid, text));
74      return false;
75    }
76    // modify property
77    property.setValue(new Character(text.charAt(0)));
78    return true;
79  }
80}
81