1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
34
35
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
46
47
48
49
50 PropertyCellModifier(PropertiesEditorDialog propertyEditorDialog) {
51 dialog = propertyEditorDialog;
52 }
53
54
55
56
57
58
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
76
77
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
94
95
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
106
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);
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");
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
157
158
159 }
160 }
161 }
162
163 }