cbridgha | 5d7510f | 2005-03-24 19:48:27 +0000 | [diff] [blame] | 1 | /******************************************************************************* |
cbridgha | 68a7415 | 2007-06-07 19:31:19 +0000 | [diff] [blame] | 2 | * Copyright (c) 2003, 2006 IBM Corporation and others. |
cbridgha | 5d7510f | 2005-03-24 19:48:27 +0000 | [diff] [blame] | 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 |
jlanuti | fed82ab | 2005-12-01 22:03:10 +0000 | [diff] [blame] | 7 | * |
cbridgha | 5d7510f | 2005-03-24 19:48:27 +0000 | [diff] [blame] | 8 | * Contributors: |
| 9 | * IBM Corporation - initial API and implementation |
| 10 | *******************************************************************************/ |
cbridgha | 43b5448 | 2005-04-18 20:25:03 +0000 | [diff] [blame] | 11 | package org.eclipse.wst.common.frameworks.internal.datamodel.ui; |
cbridgha | 5d7510f | 2005-03-24 19:48:27 +0000 | [diff] [blame] | 12 | |
| 13 | import java.awt.event.ActionEvent; |
| 14 | import java.awt.event.ActionListener; |
| 15 | import java.util.Arrays; |
| 16 | import java.util.Collection; |
| 17 | import java.util.HashMap; |
| 18 | import java.util.Iterator; |
| 19 | import java.util.Map; |
| 20 | |
| 21 | import org.eclipse.jface.viewers.CheckStateChangedEvent; |
| 22 | import org.eclipse.jface.viewers.CheckboxTableViewer; |
| 23 | import org.eclipse.jface.viewers.CheckboxTreeViewer; |
| 24 | import org.eclipse.jface.viewers.ICheckStateListener; |
| 25 | import org.eclipse.jface.viewers.StructuredViewer; |
| 26 | import org.eclipse.swt.SWT; |
| 27 | import org.eclipse.swt.events.ModifyEvent; |
| 28 | import org.eclipse.swt.events.ModifyListener; |
| 29 | import org.eclipse.swt.events.SelectionEvent; |
| 30 | import org.eclipse.swt.events.SelectionListener; |
| 31 | import org.eclipse.swt.widgets.Button; |
| 32 | import org.eclipse.swt.widgets.Combo; |
| 33 | import org.eclipse.swt.widgets.Control; |
cbridgha | f7c53c9 | 2005-11-11 20:47:41 +0000 | [diff] [blame] | 34 | import org.eclipse.swt.widgets.Display; |
cbridgha | 5d7510f | 2005-03-24 19:48:27 +0000 | [diff] [blame] | 35 | import org.eclipse.swt.widgets.Label; |
| 36 | import org.eclipse.swt.widgets.List; |
| 37 | import org.eclipse.swt.widgets.Table; |
| 38 | import org.eclipse.swt.widgets.TableItem; |
| 39 | import org.eclipse.swt.widgets.Text; |
| 40 | import org.eclipse.swt.widgets.Tree; |
| 41 | import org.eclipse.swt.widgets.TreeItem; |
| 42 | import org.eclipse.swt.widgets.Widget; |
cbridgha | b5cd811 | 2005-03-31 18:35:10 +0000 | [diff] [blame] | 43 | import org.eclipse.wst.common.frameworks.datamodel.DataModelEvent; |
| 44 | import org.eclipse.wst.common.frameworks.datamodel.DataModelPropertyDescriptor; |
| 45 | import org.eclipse.wst.common.frameworks.datamodel.IDataModel; |
| 46 | import org.eclipse.wst.common.frameworks.datamodel.IDataModelListener; |
cbridgha | 5d7510f | 2005-03-24 19:48:27 +0000 | [diff] [blame] | 47 | import org.eclipse.wst.common.frameworks.internal.ui.TimedModifyListener; |
cbridgha | 5d7510f | 2005-03-24 19:48:27 +0000 | [diff] [blame] | 48 | |
| 49 | /** |
| 50 | * This class is EXPERIMENTAL and is subject to substantial changes. |
| 51 | */ |
| 52 | public class DataModelSynchHelper implements IDataModelListener { |
cbridgha | f7c53c9 | 2005-11-11 20:47:41 +0000 | [diff] [blame] | 53 | protected static final boolean isLinux = System.getProperty("os.name").equals("Linux"); //$NON-NLS-1$ //$NON-NLS-2$ |
| 54 | |
cbridgha | 62e1d16 | 2005-03-25 19:31:07 +0000 | [diff] [blame] | 55 | protected IDataModel dataModel; |
cbridgha | 5d7510f | 2005-03-24 19:48:27 +0000 | [diff] [blame] | 56 | protected Map widgetToPropertyHash; |
| 57 | protected Map propertyToWidgetHash; |
| 58 | protected Map widgetToDepControls; |
| 59 | |
| 60 | protected String currentProperty; |
| 61 | protected Widget currentWidget; |
| 62 | protected Widget currentWidgetFromEvent; // TODO M4 see if this should be set with for |
| 63 | // listeners |
| 64 | protected boolean ignoreModifyEvent = false; |
| 65 | |
| 66 | private class ModifyTextListener implements ModifyListener { |
| 67 | public void modifyText(ModifyEvent e) { |
| 68 | if (ignoreModifyEvent) |
| 69 | return; |
| 70 | Text text = (Text) e.getSource(); |
| 71 | if (currentWidget == text) |
| 72 | return; |
| 73 | String propertyName = (String) widgetToPropertyHash.get(text); |
| 74 | setProperty(propertyName, text.getText()); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | private ModifyTextListener modifyTextListener; |
| 79 | private TimedModifyListener timedModifyListener; |
| 80 | |
| 81 | private class ComboListener implements SelectionListener, ModifyListener { |
cbridgha | f7c53c9 | 2005-11-11 20:47:41 +0000 | [diff] [blame] | 82 | |
cbridgha | 5d7510f | 2005-03-24 19:48:27 +0000 | [diff] [blame] | 83 | public void modifyText(ModifyEvent e) { |
| 84 | if (ignoreModifyEvent) |
| 85 | return; |
| 86 | Combo combo = (Combo) e.getSource(); |
| 87 | if (currentWidget == combo) |
| 88 | return; |
cbridgha | f7c53c9 | 2005-11-11 20:47:41 +0000 | [diff] [blame] | 89 | |
cbridgha | 5d7510f | 2005-03-24 19:48:27 +0000 | [diff] [blame] | 90 | try { |
| 91 | currentWidgetFromEvent = combo; |
| 92 | String propertyName = (String) widgetToPropertyHash.get(combo); |
cbridgha | 62e1d16 | 2005-03-25 19:31:07 +0000 | [diff] [blame] | 93 | DataModelPropertyDescriptor[] descriptors = dataModel.getValidPropertyDescriptors(propertyName); |
cbridgha | 5d7510f | 2005-03-24 19:48:27 +0000 | [diff] [blame] | 94 | String description = combo.getText(); |
cbridgha | f7c53c9 | 2005-11-11 20:47:41 +0000 | [diff] [blame] | 95 | // On a combo selection linux fires 2 events; |
| 96 | // the first clears the value which needs to be ignored when the type is not String |
| 97 | // the second sets the new value |
| 98 | if (isLinux && description.length() == 0 && descriptors.length != 0 && !(descriptors[0].getPropertyValue() instanceof String)) { |
cbridgha | c33c9a6 | 2005-07-08 14:07:07 +0000 | [diff] [blame] | 99 | return; |
| 100 | } |
cbridgha | 5d7510f | 2005-03-24 19:48:27 +0000 | [diff] [blame] | 101 | for (int i = 0; i < descriptors.length; i++) { |
| 102 | if (description.equals(descriptors[i].getPropertyDescription())) { |
| 103 | setProperty(propertyName, descriptors[i].getPropertyValue()); |
| 104 | return; |
| 105 | } |
| 106 | } |
| 107 | setProperty(propertyName, combo.getText()); |
| 108 | } finally { |
| 109 | currentWidgetFromEvent = null; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | public void widgetSelected(SelectionEvent e) { |
| 114 | Combo combo = (Combo) e.getSource(); |
| 115 | if (currentWidget == combo) |
| 116 | return; |
| 117 | String propertyName = (String) widgetToPropertyHash.get(combo); |
| 118 | if (combo.getSelectionIndex() >= 0) { |
cbridgha | 62e1d16 | 2005-03-25 19:31:07 +0000 | [diff] [blame] | 119 | DataModelPropertyDescriptor[] descriptors = dataModel.getValidPropertyDescriptors(propertyName); |
cbridgha | 5d7510f | 2005-03-24 19:48:27 +0000 | [diff] [blame] | 120 | String description = combo.getItem(combo.getSelectionIndex()); |
| 121 | for (int i = 0; i < descriptors.length; i++) { |
| 122 | if (description.equals(descriptors[i].getPropertyDescription())) { |
| 123 | setProperty(propertyName, descriptors[i].getPropertyValue()); |
| 124 | return; |
| 125 | } |
| 126 | } |
| 127 | setProperty(propertyName, combo.getItem(combo.getSelectionIndex())); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | public void widgetDefaultSelected(SelectionEvent e) { |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | private ComboListener comboListener; |
| 136 | |
| 137 | private class CheckboxSelectionListener implements SelectionListener { |
| 138 | public void widgetSelected(SelectionEvent e) { |
| 139 | Button button = (Button) e.getSource(); |
| 140 | if (currentWidget == button) |
| 141 | return; |
| 142 | String propertyName = (String) widgetToPropertyHash.get(button); |
| 143 | setProperty(propertyName, new Boolean(button.getSelection())); |
| 144 | } |
| 145 | |
| 146 | public void widgetDefaultSelected(SelectionEvent e) { |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | private CheckboxSelectionListener checkboxSelectionListener; |
| 151 | |
| 152 | public DataModelSynchHelper(IDataModel model) { |
cbridgha | 62e1d16 | 2005-03-25 19:31:07 +0000 | [diff] [blame] | 153 | this.dataModel = model; |
cbridgha | 5d7510f | 2005-03-24 19:48:27 +0000 | [diff] [blame] | 154 | dataModel.addListener(this); |
| 155 | } |
| 156 | |
| 157 | private CheckBoxViewerListener checkBoxViewerStateListener; |
| 158 | |
| 159 | private class CheckBoxViewerListener implements ICheckStateListener { |
| 160 | public void checkStateChanged(CheckStateChangedEvent event) { |
| 161 | StructuredViewer viewer = (StructuredViewer) event.getSource(); |
| 162 | CheckboxTableViewer checkBoxTableViewer; |
| 163 | CheckboxTreeViewer checkBoxTreeViewer; |
| 164 | Object[] items = null; |
| 165 | if (viewer instanceof CheckboxTableViewer) { |
| 166 | checkBoxTableViewer = (CheckboxTableViewer) viewer; |
| 167 | items = checkBoxTableViewer.getCheckedElements(); |
| 168 | } else if (viewer instanceof CheckboxTreeViewer) { |
| 169 | checkBoxTreeViewer = (CheckboxTreeViewer) viewer; |
| 170 | items = checkBoxTreeViewer.getCheckedElements(); |
| 171 | } |
| 172 | String propertyName = (String) widgetToPropertyHash.get(viewer.getControl()); |
| 173 | setPropertyItems(propertyName, items); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | public void propertyChanged(DataModelEvent event) { |
| 178 | String propertyName = event.getPropertyName(); |
| 179 | int flag = event.getFlag(); |
| 180 | if (flag == DataModelEvent.ENABLE_CHG) |
| 181 | setEnablement(propertyName, event.isPropertyEnabled()); |
| 182 | else |
| 183 | synchUIWithModel(propertyName, flag); |
| 184 | } |
| 185 | |
| 186 | protected void setProperty(String propertyName, Object value) { |
| 187 | currentProperty = propertyName; |
| 188 | try { |
| 189 | dataModel.setProperty(propertyName, value); |
| 190 | } finally { |
| 191 | currentProperty = null; |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | protected void setPropertyItems(String propertyName, Object[] items) { |
| 196 | currentProperty = propertyName; |
| 197 | try { |
| 198 | dataModel.setProperty(propertyName, items); |
| 199 | } finally { |
| 200 | currentProperty = null; |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * Sets the UI to have the property value defined in the model |
| 206 | * |
| 207 | * @param propertyName |
| 208 | * @link IDataModelListener for the flag values. |
| 209 | */ |
cbridgha | f7c53c9 | 2005-11-11 20:47:41 +0000 | [diff] [blame] | 210 | public void synchUIWithModel(final String propertyName, final int flag) { |
cbridgha | 5d7510f | 2005-03-24 19:48:27 +0000 | [diff] [blame] | 211 | if (null != propertyToWidgetHash && propertyToWidgetHash.containsKey(propertyName)) { |
vbhadrir | a7f3c53 | 2005-11-11 21:58:46 +0000 | [diff] [blame] | 212 | Display.getDefault().syncExec(new Runnable() { |
cbridgha | f7c53c9 | 2005-11-11 20:47:41 +0000 | [diff] [blame] | 213 | public void run() { |
cbridgha | 5d7510f | 2005-03-24 19:48:27 +0000 | [diff] [blame] | 214 | try { |
cbridgha | f7c53c9 | 2005-11-11 20:47:41 +0000 | [diff] [blame] | 215 | currentWidget = (Widget) propertyToWidgetHash.get(propertyName); |
| 216 | if (currentWidget != null && currentWidget != currentWidgetFromEvent) { |
cbridgha | 0c5dcb2 | 2006-04-20 15:06:53 +0000 | [diff] [blame] | 217 | //We must hold a copy in a temp variable because setting the widget value |
| 218 | //may trigger an event that will cause this method to be called again. |
| 219 | Widget widget = currentWidget; |
cbridgha | f7c53c9 | 2005-11-11 20:47:41 +0000 | [diff] [blame] | 220 | try { |
cbridgha | b2dfe0b | 2006-05-11 20:57:03 +0000 | [diff] [blame] | 221 | ignoreModifyEvent = true; |
cbridgha | f7c53c9 | 2005-11-11 20:47:41 +0000 | [diff] [blame] | 222 | if (currentWidget instanceof Text) |
| 223 | setWidgetValue(propertyName, flag, (Text) currentWidget); |
| 224 | else if (currentWidget instanceof Combo) { |
| 225 | setWidgetValue(propertyName, flag, (Combo) currentWidget); |
| 226 | } else if (currentWidget instanceof Button) |
| 227 | setWidgetValue(propertyName, flag, (Button) currentWidget); |
| 228 | else if (currentWidget instanceof Label) |
| 229 | setWidgetValue(propertyName, flag, (Label) currentWidget); |
| 230 | else if (currentWidget instanceof List) |
| 231 | setWidgetValue(propertyName, flag, (List) currentWidget); |
| 232 | else if (currentWidget instanceof Table) |
| 233 | setWidgetValue(propertyName, flag, (Table) currentWidget); |
| 234 | else if (currentWidget instanceof Tree) |
| 235 | setWidgetValue(propertyName, flag, (Tree) currentWidget); |
| 236 | } finally { |
| 237 | ignoreModifyEvent = false; |
| 238 | } |
cbridgha | 0c5dcb2 | 2006-04-20 15:06:53 +0000 | [diff] [blame] | 239 | //Pass the copy of the currentWidget |
| 240 | setEnablement((Control) widget, dataModel.isPropertyEnabled(propertyName)); |
cbridgha | f7c53c9 | 2005-11-11 20:47:41 +0000 | [diff] [blame] | 241 | } |
cbridgha | 5d7510f | 2005-03-24 19:48:27 +0000 | [diff] [blame] | 242 | } finally { |
cbridgha | f7c53c9 | 2005-11-11 20:47:41 +0000 | [diff] [blame] | 243 | currentWidget = null; |
cbridgha | 5d7510f | 2005-03-24 19:48:27 +0000 | [diff] [blame] | 244 | } |
cbridgha | 5d7510f | 2005-03-24 19:48:27 +0000 | [diff] [blame] | 245 | } |
cbridgha | f7c53c9 | 2005-11-11 20:47:41 +0000 | [diff] [blame] | 246 | }); |
cbridgha | 5d7510f | 2005-03-24 19:48:27 +0000 | [diff] [blame] | 247 | } |
| 248 | } |
| 249 | |
| 250 | /** |
| 251 | * @param control |
cbridgha | 5d7510f | 2005-03-24 19:48:27 +0000 | [diff] [blame] | 252 | * @param enabled |
| 253 | */ |
cbridgha | f7c53c9 | 2005-11-11 20:47:41 +0000 | [diff] [blame] | 254 | protected void setEnablement(Control control, boolean enabled) { |
cbridgha | 5d7510f | 2005-03-24 19:48:27 +0000 | [diff] [blame] | 255 | if (control.isEnabled() != enabled) |
| 256 | control.setEnabled(enabled); |
| 257 | setDependentControlEnablement(control, enabled); |
| 258 | } |
| 259 | |
cbridgha | f7c53c9 | 2005-11-11 20:47:41 +0000 | [diff] [blame] | 260 | private void setEnablement(final String propertyName, final boolean enabled) { |
cbridgha | 5d7510f | 2005-03-24 19:48:27 +0000 | [diff] [blame] | 261 | if (propertyToWidgetHash != null) { |
vbhadrir | a7f3c53 | 2005-11-11 21:58:46 +0000 | [diff] [blame] | 262 | Display.getDefault().syncExec(new Runnable() { |
cbridgha | f7c53c9 | 2005-11-11 20:47:41 +0000 | [diff] [blame] | 263 | public void run() { |
| 264 | Control control = (Control) propertyToWidgetHash.get(propertyName); |
| 265 | if (control != null) { |
| 266 | setEnablement(control, enabled); |
| 267 | } |
| 268 | } |
| 269 | }); |
cbridgha | 5d7510f | 2005-03-24 19:48:27 +0000 | [diff] [blame] | 270 | } |
| 271 | } |
| 272 | |
| 273 | /** |
| 274 | * @param control |
| 275 | * @param enabled |
| 276 | */ |
| 277 | private void setDependentControlEnablement(Control control, boolean enabled) { |
| 278 | if (widgetToDepControls != null) { |
| 279 | Control[] dependents = (Control[]) widgetToDepControls.get(control); |
| 280 | if (dependents != null) { |
| 281 | Control dep = null; |
| 282 | for (int i = 0; i < dependents.length; i++) { |
| 283 | dep = dependents[i]; |
| 284 | if (dep.isEnabled() != enabled) |
| 285 | dep.setEnabled(enabled); |
| 286 | } |
| 287 | } |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | protected void setWidgetValue(String propertyName, int flag, Button button) { |
| 292 | if ((button.getStyle() & SWT.CHECK) == SWT.CHECK || (button.getStyle() & SWT.RADIO) == SWT.RADIO) { |
| 293 | boolean checked = dataModel.getBooleanProperty(propertyName); |
| 294 | if (button.getSelection() != checked) { |
| 295 | button.setSelection(checked); |
| 296 | } |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | protected void setWidgetValue(String propertyName, int flag, Combo combo) { |
| 301 | if (flag == DataModelEvent.VALID_VALUES_CHG || combo.getItemCount() == 0) { |
| 302 | // Display properties should only fire if the contents change. |
cbridgha | 62e1d16 | 2005-03-25 19:31:07 +0000 | [diff] [blame] | 303 | DataModelPropertyDescriptor[] descriptors = dataModel.getValidPropertyDescriptors(propertyName); |
cbridgha | 5d7510f | 2005-03-24 19:48:27 +0000 | [diff] [blame] | 304 | String[] items = new String[descriptors.length]; |
| 305 | for (int i = 0; i < descriptors.length; i++) { |
| 306 | items[i] = descriptors[i].getPropertyDescription(); |
| 307 | } |
| 308 | combo.setItems(items); |
| 309 | } |
| 310 | String newText = dataModel.getPropertyDescriptor(propertyName).getPropertyDescription(); |
| 311 | int selIndex = combo.getSelectionIndex(); |
| 312 | if (selIndex < 0 || !newText.equals(combo.getItem(selIndex))) { |
| 313 | String[] items = combo.getItems(); |
| 314 | for (int i = 0; i < items.length; i++) { |
| 315 | if (items[i].equals(newText)) { |
| 316 | combo.select(i); |
| 317 | return; |
| 318 | } |
| 319 | } |
| 320 | } |
| 321 | combo.setText(newText); |
| 322 | } |
| 323 | |
| 324 | protected void setWidgetValue(String propertyName, int flag, Text text) { |
| 325 | String newText = dataModel.getStringProperty(propertyName); |
| 326 | if (!newText.equals(text.getText())) { |
| 327 | text.setText(newText); |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | protected void setWidgetValue(String propertyName, int flag, Table table) { |
| 332 | Object[] elements = (Object[]) dataModel.getProperty(propertyName); |
| 333 | if (elements == null || elements.length == 0) { |
| 334 | setTableItemsChecked(table.getItems(), false); |
| 335 | return; |
| 336 | } |
| 337 | java.util.List elementList = Arrays.asList(elements); |
| 338 | TableItem[] tableItems = table.getItems(); |
| 339 | for (int i = 0; i < tableItems.length; i++) { |
| 340 | TableItem item = tableItems[i]; |
| 341 | if (elementList.contains(item.getData())) |
| 342 | item.setChecked(true); |
| 343 | else |
| 344 | item.setChecked(false); |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | protected void setTableItemsChecked(TableItem[] tableItems, boolean b) { |
| 349 | for (int i = 0; i < tableItems.length; i++) |
| 350 | tableItems[i].setChecked(b); |
| 351 | } |
| 352 | |
| 353 | protected void setWidgetValue(String propertyName, int flag, Tree tree) { |
| 354 | Object[] elements = (Object[]) dataModel.getProperty(propertyName); |
| 355 | if (elements == null) |
| 356 | return; |
| 357 | java.util.List elementList = Arrays.asList(elements); |
| 358 | TreeItem[] treeItems = tree.getItems(); |
| 359 | if (elementList.size() == 0) { |
| 360 | setTreeItemsGrey(treeItems, false); |
| 361 | return; |
| 362 | } |
| 363 | if (treeItems.length > 0) |
| 364 | setTreeItemChecked(treeItems, elementList); |
| 365 | } |
| 366 | |
| 367 | public void setTreeItemsGrey(TreeItem[] treeItems, boolean b) { |
| 368 | for (int i = 0; i < treeItems.length; i++) { |
| 369 | TreeItem item = treeItems[i]; |
| 370 | item.setGrayed(b); |
| 371 | setTreeItemsGrey(treeItems[i].getItems(), b); |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | protected void setTreeItemChecked(TreeItem[] treeItems, java.util.List items) { |
| 376 | for (int i = 0; i < treeItems.length; i++) { |
cbridgha | 1aea551 | 2006-04-05 19:39:27 +0000 | [diff] [blame] | 377 | if (items.contains(treeItems[i].getData())){ |
cbridgha | 5d7510f | 2005-03-24 19:48:27 +0000 | [diff] [blame] | 378 | treeItems[i].setChecked(true); |
cbridgha | 1aea551 | 2006-04-05 19:39:27 +0000 | [diff] [blame] | 379 | treeItems[i].setGrayed(false); |
| 380 | }else{ |
cbridgha | 5d7510f | 2005-03-24 19:48:27 +0000 | [diff] [blame] | 381 | treeItems[i].setGrayed(true); |
cbridgha | 1aea551 | 2006-04-05 19:39:27 +0000 | [diff] [blame] | 382 | } |
cbridgha | 5d7510f | 2005-03-24 19:48:27 +0000 | [diff] [blame] | 383 | TreeItem[] childernItems = treeItems[i].getItems(); |
| 384 | if (childernItems.length > 0) { |
| 385 | treeItems[i].setExpanded(true); |
| 386 | setTreeItemChecked(childernItems, items); |
| 387 | } |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | protected void setWidgetValue(String propertyName, int flag, Label label) { |
| 392 | String newText = dataModel.getStringProperty(propertyName); |
| 393 | if (!newText.equals(label.getText())) { |
| 394 | label.setText(newText); |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | protected void setWidgetValue(String propertyName, int flag, List list) { |
| 399 | Object newContents = dataModel.getProperty(propertyName); |
| 400 | if (newContents == null) { |
| 401 | list.setItems(new String[0]); |
| 402 | return; |
| 403 | } |
| 404 | if (newContents instanceof java.util.List) { |
| 405 | java.util.List modelContents = (java.util.List) newContents; |
| 406 | String[] items = new String[modelContents.size()]; |
| 407 | for (int i = 0; i < modelContents.size(); i++) { |
| 408 | items[i] = modelContents.get(i).toString(); |
| 409 | } |
| 410 | list.setItems(items); |
| 411 | } else if (newContents instanceof String[]) { |
| 412 | list.setItems((String[]) newContents); |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | public void synchAllUIWithModel() { |
| 417 | if (null != propertyToWidgetHash) { |
| 418 | Collection keys = propertyToWidgetHash.keySet(); |
| 419 | if (!keys.isEmpty()) { |
| 420 | Iterator propertyNames = keys.iterator(); |
| 421 | String propertyName = null; |
| 422 | while (propertyNames.hasNext()) { |
| 423 | propertyName = (String) propertyNames.next(); |
cbridgha | 62e1d16 | 2005-03-25 19:31:07 +0000 | [diff] [blame] | 424 | synchUIWithModel(propertyName, DataModelEvent.VALUE_CHG); |
cbridgha | 5d7510f | 2005-03-24 19:48:27 +0000 | [diff] [blame] | 425 | } |
| 426 | } |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | protected void synchComposite(Widget widget, String propertyName, Control[] depControls) { |
| 431 | if (null == widgetToPropertyHash) |
| 432 | widgetToPropertyHash = new HashMap(); |
| 433 | if (propertyToWidgetHash == null) |
| 434 | propertyToWidgetHash = new HashMap(); |
| 435 | widgetToPropertyHash.put(widget, propertyName); |
| 436 | propertyToWidgetHash.put(propertyName, widget); |
| 437 | if (depControls != null) { |
| 438 | if (widgetToDepControls == null) |
| 439 | widgetToDepControls = new HashMap(); |
| 440 | widgetToDepControls.put(widget, depControls); |
| 441 | } |
cbridgha | 62e1d16 | 2005-03-25 19:31:07 +0000 | [diff] [blame] | 442 | synchUIWithModel(propertyName, DataModelEvent.VALUE_CHG); |
cbridgha | 5d7510f | 2005-03-24 19:48:27 +0000 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | public void synchText(Text text, String propertyName, Control[] dependentControls) { |
| 446 | synchText(text, propertyName, false, dependentControls); |
| 447 | } |
| 448 | |
| 449 | public void synchText(Text text, String propertyName, boolean isTimeModified, Control[] dependentControls) { |
| 450 | synchComposite(text, propertyName, dependentControls); |
| 451 | if (isTimeModified) |
| 452 | text.addModifyListener(getTimedListener()); |
| 453 | else |
| 454 | text.addModifyListener(getModifyTextListener()); |
| 455 | } |
| 456 | |
| 457 | public void synchLabel(Label label, String propertyName, Control[] dependentControls) { |
| 458 | synchComposite(label, propertyName, dependentControls); |
| 459 | } |
| 460 | |
| 461 | /** |
| 462 | * Use this to synch the contents of the <code>list</code> to the List elements returned from |
| 463 | * the <code>propertyName</code>. |
| 464 | */ |
| 465 | public void synchList(List list, String propertyName, Control[] dependentControls) { |
| 466 | synchComposite(list, propertyName, dependentControls); |
| 467 | } |
| 468 | |
| 469 | /** |
| 470 | * Use this to synch the value of the specified Combo with the specified propertyName. The |
| 471 | * possible values displayed to the user are determined by return of |
| 472 | * IDataModel.getValidPropertyDescriptors(String). |
| 473 | * |
| 474 | * @param combo |
| 475 | * @param propertyName |
| 476 | * @param dependentControls |
| 477 | */ |
| 478 | public void synchCombo(Combo combo, String propertyName, Control[] dependentControls) { |
| 479 | synchComposite(combo, propertyName, dependentControls); |
| 480 | if (null == comboListener) { |
| 481 | comboListener = new ComboListener(); |
| 482 | } |
| 483 | combo.addSelectionListener(comboListener); |
| 484 | combo.addModifyListener(comboListener); |
| 485 | } |
| 486 | |
| 487 | /** |
| 488 | * Use this to sync the state of the specified checkbox with the value of the specified |
| 489 | * propertyName. The specified propertyName must contain a java.lang.Boolean typed Object. |
| 490 | * |
| 491 | * @param checkbox |
| 492 | * @param propertyName |
| 493 | * @param dependentControls |
| 494 | */ |
| 495 | public void synchCheckbox(Button checkbox, String propertyName, Control[] dependentControls) { |
| 496 | synchComposite(checkbox, propertyName, dependentControls); |
| 497 | if (null == checkboxSelectionListener) { |
| 498 | checkboxSelectionListener = new CheckboxSelectionListener(); |
| 499 | } |
| 500 | checkbox.addSelectionListener(checkboxSelectionListener); |
| 501 | } |
| 502 | |
| 503 | public void synchCheckBoxTableViewer(CheckboxTableViewer tableViewer, String propertyName, Control[] dependentControls) { |
| 504 | synchComposite(tableViewer.getControl(), propertyName, dependentControls); |
| 505 | if (null == checkBoxViewerStateListener) { |
| 506 | checkBoxViewerStateListener = new CheckBoxViewerListener(); |
| 507 | } |
| 508 | tableViewer.addCheckStateListener(checkBoxViewerStateListener); |
| 509 | } |
| 510 | |
| 511 | public void synchCheckBoxTreeViewer(CheckboxTreeViewer treeViewer, String propertyName, Control[] dependentControls) { |
| 512 | treeViewer.expandAll(); |
| 513 | synchComposite(treeViewer.getControl(), propertyName, dependentControls); |
| 514 | if (null == checkBoxViewerStateListener) { |
| 515 | checkBoxViewerStateListener = new CheckBoxViewerListener(); |
| 516 | } |
| 517 | treeViewer.addCheckStateListener(checkBoxViewerStateListener); |
| 518 | } |
| 519 | |
| 520 | public void synchRadio(Button radio, String propertyName, Control[] dependentControls) { |
| 521 | // Uses checkbox syncher |
| 522 | synchCheckbox(radio, propertyName, dependentControls); |
| 523 | } |
| 524 | |
| 525 | private TimedModifyListener getTimedListener() { |
| 526 | if (timedModifyListener == null) |
| 527 | timedModifyListener = new TimedModifyListener(new ActionListener() { |
| 528 | public void actionPerformed(ActionEvent e) { |
| 529 | Widget w = (Widget) e.getSource(); |
| 530 | if (currentWidget == w || w.isDisposed()) |
| 531 | return; |
| 532 | String propertyName = (String) widgetToPropertyHash.get(w); |
| 533 | setTextProperty(propertyName, (Text) w); |
| 534 | } |
| 535 | }) { |
| 536 | public void modifyText(ModifyEvent e) { |
| 537 | if (ignoreModifyEvent) |
| 538 | return; |
| 539 | super.modifyText(e); |
| 540 | } |
| 541 | }; |
| 542 | return timedModifyListener; |
| 543 | } |
| 544 | |
| 545 | protected void setTextProperty(String propertyName, Text text) { |
| 546 | setProperty(propertyName, text.getText()); |
| 547 | } |
| 548 | |
| 549 | private ModifyTextListener getModifyTextListener() { |
| 550 | if (null == modifyTextListener) |
| 551 | modifyTextListener = new ModifyTextListener(); |
| 552 | return modifyTextListener; |
| 553 | } |
| 554 | |
| 555 | public IDataModel getDataModel() { |
| 556 | return dataModel; |
| 557 | } |
| 558 | |
| 559 | public void dispose() { |
| 560 | dataModel.removeListener(this); |
| 561 | checkboxSelectionListener = null; |
| 562 | currentWidget = null; |
| 563 | modifyTextListener = null; |
| 564 | propertyToWidgetHash = null; |
| 565 | timedModifyListener = null; |
| 566 | widgetToDepControls = null; |
| 567 | widgetToPropertyHash = null; |
| 568 | } |
jlanuti | fed82ab | 2005-12-01 22:03:10 +0000 | [diff] [blame] | 569 | } |