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 com.google.common.base.Joiner; 14 15import org.eclipse.jface.window.Window; 16import org.eclipse.wb.internal.core.DesignerPlugin; 17import org.eclipse.wb.internal.core.model.ModelMessages; 18import org.eclipse.wb.internal.core.model.property.Property; 19import org.eclipse.wb.internal.core.utils.ui.dialogs.StringsDialog; 20 21/** 22 * {@link PropertyEditor} for array of {@link String}'s. 23 * 24 * @author scheglov_ke 25 * @coverage core.model.property.editor 26 */ 27public final class StringArrayPropertyEditor extends TextDialogPropertyEditor { 28 //////////////////////////////////////////////////////////////////////////// 29 // 30 // Instance 31 // 32 //////////////////////////////////////////////////////////////////////////// 33 public static final PropertyEditor INSTANCE = new StringArrayPropertyEditor(); 34 35 private StringArrayPropertyEditor() { 36 } 37 38 //////////////////////////////////////////////////////////////////////////// 39 // 40 // Presentation 41 // 42 //////////////////////////////////////////////////////////////////////////// 43 @Override 44 protected String getText(Property property) throws Exception { 45 String[] items = getItems(property); 46 return "[" + Joiner.on(", ").join(items) + "]"; 47 } 48 49 /** 50 * @return the items specified in value of given {@link Property}. 51 */ 52 private static String[] getItems(Property property) throws Exception { 53 Object value = property.getValue(); 54 if (value instanceof String[]) { 55 return (String[]) value; 56 } 57 // no items 58 return new String[0]; 59 } 60 61 //////////////////////////////////////////////////////////////////////////// 62 // 63 // Editing 64 // 65 //////////////////////////////////////////////////////////////////////////// 66 @Override 67 protected void openDialog(Property property) throws Exception { 68 StringsDialog itemsDialog = 69 new StringsDialog(DesignerPlugin.getShell(), 70 DesignerPlugin.getDefault(), 71 property.getTitle(), 72 ModelMessages.StringArrayPropertyEditor_itemsLabel, 73 ModelMessages.StringArrayPropertyEditor_hint); 74 itemsDialog.setItems(getItems(property)); 75 // open dialog 76 if (itemsDialog.open() == Window.OK) { 77 property.setValue(itemsDialog.getItems()); 78 } 79 } 80} 81