summaryrefslogtreecommitdiff
path: root/propertysheet/src/org/eclipse/wb/internal/core/model/property/editor/StringArrayPropertyEditor.java
blob: fd78e01306f5b2c29eec338707a9596e7f47b1bb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*******************************************************************************
 * Copyright (c) 2011 Google, Inc.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Google, Inc. - initial API and implementation
 *******************************************************************************/
package org.eclipse.wb.internal.core.model.property.editor;

import com.google.common.base.Joiner;

import org.eclipse.jface.window.Window;
import org.eclipse.wb.internal.core.DesignerPlugin;
import org.eclipse.wb.internal.core.model.ModelMessages;
import org.eclipse.wb.internal.core.model.property.Property;
import org.eclipse.wb.internal.core.utils.ui.dialogs.StringsDialog;

/**
 * {@link PropertyEditor} for array of {@link String}'s.
 *
 * @author scheglov_ke
 * @coverage core.model.property.editor
 */
public final class StringArrayPropertyEditor extends TextDialogPropertyEditor {
  ////////////////////////////////////////////////////////////////////////////
  //
  // Instance
  //
  ////////////////////////////////////////////////////////////////////////////
  public static final PropertyEditor INSTANCE = new StringArrayPropertyEditor();

  private StringArrayPropertyEditor() {
  }

  ////////////////////////////////////////////////////////////////////////////
  //
  // Presentation
  //
  ////////////////////////////////////////////////////////////////////////////
  @Override
  protected String getText(Property property) throws Exception {
    String[] items = getItems(property);
    return "[" + Joiner.on(", ").join(items) + "]";
  }

  /**
   * @return the items specified in value of given {@link Property}.
   */
  private static String[] getItems(Property property) throws Exception {
    Object value = property.getValue();
    if (value instanceof String[]) {
      return (String[]) value;
    }
    // no items
    return new String[0];
  }

  ////////////////////////////////////////////////////////////////////////////
  //
  // Editing
  //
  ////////////////////////////////////////////////////////////////////////////
  @Override
  protected void openDialog(Property property) throws Exception {
    StringsDialog itemsDialog =
        new StringsDialog(DesignerPlugin.getShell(),
            DesignerPlugin.getDefault(),
            property.getTitle(),
            ModelMessages.StringArrayPropertyEditor_itemsLabel,
            ModelMessages.StringArrayPropertyEditor_hint);
    itemsDialog.setItems(getItems(property));
    // open dialog
    if (itemsDialog.open() == Window.OK) {
      property.setValue(itemsDialog.getItems());
    }
  }
}