1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.utils.ui.workbench.dialogs;
14 import java.util.ArrayList;
15 import java.util.HashSet;
16 import java.util.List;
19 import org.eclipse.jface.viewers.CellEditor;
20 import org.eclipse.jface.viewers.ICellModifier;
21 import org.eclipse.jface.viewers.IStructuredSelection;
22 import org.eclipse.jface.viewers.StructuredSelection;
23 import org.eclipse.jface.viewers.TableViewer;
24 import org.eclipse.jface.viewers.TextCellEditor;
25 import org.eclipse.swt.SWT;
26 import org.eclipse.swt.events.VerifyEvent;
27 import org.eclipse.swt.events.VerifyListener;
28 import org.eclipse.swt.widgets.Composite;
29 import org.eclipse.swt.widgets.Control;
30 import org.eclipse.swt.widgets.Shell;
31 import org.eclipse.swt.widgets.Table;
32 import org.eclipse.swt.widgets.TableItem;
33 import org.eclipse.swt.widgets.Text;
37 * EditablePropertyDialog is a property dialog where dialog cells can be
41 public class PropertyEditorDialog extends PropertyDialog {
43 public static final int ADD_PROPERTY_ID = -5000;
45 public static final int REMOVE_PROPERTY_ID = -5000 + 1;
47 /** Fields that are read-only */
48 protected Set<String> readOnlyKeys = new HashSet<String>();
50 protected List<PropertyLineVerifyListener> verifyListeners = new ArrayList<PropertyLineVerifyListener>();
52 public PropertyEditorDialog(Shell parent) {
57 protected Control createDialogArea(Composite container) {
58 Control composite = super.createDialogArea(container);
59 TableViewer viewer = getTableViewer();
60 Table table = viewer.getTable();
62 viewer.setInput(getTableViewer().getInput());
64 CellEditor keyEditor = new TextCellEditor(table, SWT.NORMAL);
65 TextCellEditor valueEditor = new TextCellEditor(table, SWT.NORMAL);
66 ((Text)valueEditor.getControl()).addVerifyListener(new VerifyListener() {
67 public void verifyText(VerifyEvent e) {
68 String sel[] = getSelection();
69 if (sel==null) return;
70 Text w = (Text)e.widget;
71 String newText = w.getText();
73 if (e.text.length()==0)
74 newText = newText.substring(0,e.start) + newText.substring(e.end, newText.length());
77 newText = newText.substring(0,e.start) + e.text + newText.substring(e.end );
79 e.doit = verifyValueModification(sel[0], newText );
81 viewer.setCellEditors(new CellEditor[] { keyEditor, valueEditor });
83 viewer.setCellModifier(new ICellModifier() {
84 public boolean canModify(Object element, String property) {
85 int column = columnNameToColumn(property);
88 String line[] = (String[]) element;
90 boolean keyReadOnly = isKeyReadonly(key);
95 public Object getValue(Object element, String property) {
96 int column = columnNameToColumn(property);
97 String line[] = (String[]) element;
101 public void modify(Object element, String property, Object value) {
102 // Some kind of bug? This returns TableItem object sometimes
103 if (element instanceof TableItem)
104 element = ((TableItem) element).getData();
105 int column = columnNameToColumn(property);
106 String line[] = (String[]) element;
107 String key = line[0];
109 // Cannot have two values with same key
110 if (column==0 && containsKey(value.toString()))
112 if (column==0 && isKeyReadonly(key))
114 if (column==0 && !verifyValueModification(value.toString(), line[1]))
117 line[column] = value.toString();
118 labelProvider.elementChanged(element);
125 protected boolean containsKey(String key) {
126 for (String[] line : data)
127 if (line[0].equals(key))
133 protected void createButtonsForButtonBar(Composite parent) {
134 // Add property button
135 createButton(parent, ADD_PROPERTY_ID, "Add property", false);
136 // Remove property button
137 createButton(parent, REMOVE_PROPERTY_ID, "Remove property", false);
139 setAddCancelButton(true);
140 super.createButtonsForButtonBar(parent);
141 // Having this true enables double-click listener later
143 setAddCancelButton(false);
147 protected void buttonPressed(int buttonId) {
148 if (buttonId == ADD_PROPERTY_ID)
149 addPropertyPressed();
150 else if (buttonId == REMOVE_PROPERTY_ID)
151 removePropertyPressed();
153 super.buttonPressed(buttonId);
156 protected String[] getSelection() {
157 IStructuredSelection sel = (IStructuredSelection) this.getTableViewer().getSelection();
158 if (sel==null) return null;
159 Object o = sel.getFirstElement();
160 if (o==null) return null;
164 private void removePropertyPressed() {
165 String o[] = getSelection();
167 if (readOnlyKeys.contains(o[0]))
169 if (!data.remove(o)) return;
170 labelProvider.refreshAll();
173 private void addPropertyPressed() {
174 String newElement[] = new String[] {"<new property>", "<value>"};
175 data.add(newElement);
176 labelProvider.refreshAll();
177 getTableViewer().setSelection(new StructuredSelection(newElement));
181 * Set fields that are read-only (These are 1st column key names)
183 * @param readOnlyFields fields that are read only
185 public void setReadOnlyFields(String[] readOnlyFields) {
186 for (String key : readOnlyFields)
187 readOnlyKeys.add(key);
191 * Set a key read-only / writable
192 * @param key key name
193 * @param readOnly true = readonly, false = writable
195 public void setFieldReadOnlyStatus(String key, boolean readOnly) {
197 readOnlyKeys.add(key);
199 readOnlyKeys.remove(key);
202 protected boolean isKeyReadonly(String key) {
203 return readOnlyKeys.contains(key);
208 * @return result data
210 public List<String[]> getData() {
214 public void addVerifyListener(PropertyLineVerifyListener listener) {
215 verifyListeners.add(listener);
218 public void removeVerifyListener(PropertyLineVerifyListener listener) {
219 verifyListeners.remove(listener);
222 protected boolean verifyValueModification(String key, String value) {
223 boolean result = true;
224 for (PropertyLineVerifyListener l : verifyListeners)
225 result = result & l.verifyValue(key, value);