View Javadoc

1   /*
2    * This file is a part of CAST project.
3    * (c) Copyright 2007, AGH University of Science & Technology
4    * https://caribou.iisg.agh.edu.pl/trac/cast
5    *
6    * Licensed under the Eclipse Public License, Version 1.0 (the "License").
7    * You may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    * http://www.eclipse.org/legal/epl-v10.html
10   */
11  /*
12   * File: NewDialogCellModifier.java
13   * Created: 2009-08-04
14   * Author: kpietak
15   * $Id$
16   */
17  
18  package pl.edu.agh.cast.ui.dialogs.property;
19  
20  import org.apache.log4j.Logger;
21  import org.eclipse.jface.viewers.ICellModifier;
22  import org.eclipse.swt.SWT;
23  import org.eclipse.swt.widgets.MessageBox;
24  import org.eclipse.swt.widgets.TreeItem;
25  
26  import pl.edu.agh.cast.Activator;
27  import pl.edu.agh.cast.data.model.property.MetaProperty;
28  import pl.edu.agh.cast.data.model.property.Property;
29  import pl.edu.agh.cast.ui.dialogs.property.PropertyTreeEntry.PropertyEditType;
30  import pl.edu.agh.cast.util.Messages;
31  
32  /**
33   * A cell modifier implementation for {@link PropertyEditorDialog}.
34   *
35   * @author AGH CAST Team
36   */
37  final class PropertyCellModifier implements ICellModifier {
38  
39  	@SuppressWarnings("unused")
40  	private static Logger log = Activator.getLogger();
41  
42  	private final PropertiesEditorDialog dialog;
43  
44  	/**
45  	 * Creates new {@link ICellModifier} for {@link PropertyEditorDialog}.
46  	 *
47  	 * @param propertyEditorDialog
48  	 *            dialog to create {@link ICellModifier} for
49  	 */
50  	PropertyCellModifier(PropertiesEditorDialog propertyEditorDialog) {
51  		dialog = propertyEditorDialog;
52  	}
53  
54  	/**
55  	 *
56  	 * {@inheritDoc}
57  	 *
58  	 * @see org.eclipse.jface.viewers.ICellModifier#canModify(java.lang.Object, java.lang.String)
59  	 */
60  	@Override
61  	public boolean canModify(Object element, String property) {
62  		if (PropertiesEditorDialog.VALUE_COLUMN.equals(property)) {
63  			PropertyTreeEntry entry = (PropertyTreeEntry)element;
64  			if (entry.getProperty() != null) {
65  				return entry.getProperty().getMetaProperty().isWritable();
66  			}
67  			return false;
68  		} else {
69  			return PropertiesEditorDialog.ISLABEL_COLUMN.equals(property);
70  		}
71  	}
72  
73  	/**
74  	 *
75  	 * {@inheritDoc}
76  	 *
77  	 * @see org.eclipse.jface.viewers.ICellModifier#getValue(java.lang.Object, java.lang.String)
78  	 */
79  	@Override
80  	public Object getValue(Object element, String attribute) {
81  		PropertyTreeEntry entry = (PropertyTreeEntry)element;
82  		if (PropertiesEditorDialog.NAME_COLUMN.equals(attribute)) {
83  			return entry.getProperty() != null ? entry.getProperty().getMetaProperty().getName() : entry.getLabel();
84  		} else if (PropertiesEditorDialog.ISLABEL_COLUMN.equals(attribute)) {
85  			return entry.isMarkAsLabel();
86  		} else {
87  			return entry.getProperty() != null ? entry.getProperty().getValue() : null;
88  		}
89  	}
90  
91  	/**
92  	 *
93  	 * {@inheritDoc}
94  	 *
95  	 * @see org.eclipse.jface.viewers.ICellModifier#modify(java.lang.Object, java.lang.String, java.lang.Object)
96  	 */
97  	@Override
98  	public void modify(Object element, String attribute, Object value) {
99  
100 		String[] props = new String[] { PropertiesEditorDialog.ISLABEL_COLUMN, PropertiesEditorDialog.VALUE_COLUMN };
101 
102 		TreeItem treeItem = (TreeItem)element;
103 		PropertyTreeEntry entry = (PropertyTreeEntry)treeItem.getData();
104 
105 		// check if new value and old value are empty
106 		// if yes ignore edit operation
107 		if (entry.getProperty() == null && value.toString().isEmpty()) {
108 			return;
109 		}
110 
111 		if (PropertiesEditorDialog.NAME_COLUMN.equals(attribute)) {
112 			throw new IllegalArgumentException("Cannot modify column " + attribute); //$NON-NLS-1$
113 		} else if (PropertiesEditorDialog.ISLABEL_COLUMN.equals(attribute)) {
114 			modifyIsLabel(entry, value);
115 
116 		} else {
117 			PropertyTreeEntry newEntry = modifyPropertyValue(value, treeItem, entry);
118 			if (newEntry == null) {
119 				return;
120 			}
121 		}
122 		dialog.treeViewer.update(entry, props);
123 	}
124 
125 	private PropertyTreeEntry modifyPropertyValue(Object value, TreeItem treeItem, PropertyTreeEntry entry) {
126 
127 		Property<? extends MetaProperty> property = entry.getProperty();
128 
129 		if (property == null) {
130 			throw new IllegalArgumentException("Cannot modify value of entry with label"); //$NON-NLS-1$
131 		}
132 
133 		MetaProperty metaProperty = entry.getProperty().getMetaProperty();
134 		boolean isValidFormat = metaProperty.getType().getValidator().isValidStringFormat(value.toString());
135 
136 		if (isValidFormat) {
137 
138 			Object newValue = metaProperty.getType().getValidator().convertFromStringFormat(value.toString());
139 			property.setValue(newValue);
140 
141 			return entry;
142 		} else {
143 			MessageBox msgBox = new MessageBox(dialog.dialogShell, SWT.OK | SWT.ICON_ERROR);
144 			msgBox.setText(Messages.PropertiesEditorDialog_10);
145 
146 			msgBox.setMessage(Messages.ShowAttributes_InvalidFormat);
147 			msgBox.open();
148 			return null;
149 		}
150 	}
151 
152 	private void modifyIsLabel(PropertyTreeEntry entry, Object newValue) {
153 		if (newValue instanceof Boolean) {
154 			if (!entry.getType().equals(PropertyEditType.CATEGORY)) {
155 				entry.setIsMarkAsLabel((Boolean)newValue);
156 				// log.debug(String
157 				// .format("Property %s marked as label with value %s", entry.getId(),
158 				// newValue.toString())); //$NON-NLS-1$
159 			}
160 		}
161 	}
162 
163 }