1 package org.eclipse.emf.examples.jet.article2.ui; 2 3 4 import java.util.ArrayList ; 5 import java.util.Iterator ; 6 import java.util.List ; 7 8 import org.eclipse.jface.dialogs.Dialog; 9 import org.eclipse.jface.viewers.CheckStateChangedEvent; 10 import org.eclipse.jface.viewers.CheckboxTableViewer; 11 import org.eclipse.jface.viewers.ColumnWeightData; 12 import org.eclipse.jface.viewers.DoubleClickEvent; 13 import org.eclipse.jface.viewers.ICheckStateListener; 14 import org.eclipse.jface.viewers.IDoubleClickListener; 15 import org.eclipse.jface.viewers.ISelectionChangedListener; 16 import org.eclipse.jface.viewers.IStructuredContentProvider; 17 import org.eclipse.jface.viewers.IStructuredSelection; 18 import org.eclipse.jface.viewers.ITableLabelProvider; 19 import org.eclipse.jface.viewers.LabelProvider; 20 import org.eclipse.jface.viewers.SelectionChangedEvent; 21 import org.eclipse.jface.viewers.StructuredSelection; 22 import org.eclipse.jface.viewers.TableLayout; 23 import org.eclipse.jface.viewers.Viewer; 24 import org.eclipse.jface.wizard.WizardPage; 25 import org.eclipse.swt.SWT; 26 import org.eclipse.swt.events.SelectionAdapter; 27 import org.eclipse.swt.events.SelectionEvent; 28 import org.eclipse.swt.graphics.Image; 29 import org.eclipse.swt.graphics.Rectangle; 30 import org.eclipse.swt.layout.GridData; 31 import org.eclipse.swt.layout.GridLayout; 32 import org.eclipse.swt.widgets.Button; 33 import org.eclipse.swt.widgets.Composite; 34 import org.eclipse.swt.widgets.Table; 35 import org.eclipse.swt.widgets.TableColumn; 36 import org.eclipse.swt.widgets.TableItem; 37 38 import org.eclipse.emf.examples.jet.article2.model.Attribute; 39 import org.eclipse.emf.examples.jet.article2.model.NameUtil; 40 import org.eclipse.emf.examples.jet.article2.model.TypesafeEnum; 41 42 43 44 51 public class NewTypesafeEnumCreationWizardPageAttributes extends WizardPage 52 { 53 54 private final static String PAGE_NAME = "AttributesPage"; 56 privatefinal String [] COLUMN_NAMES = new String []{ WizardMessages.getString("NewEnumWizPageAttr.col.Key"), WizardMessages.getString("NewEnumWizPageAttr.col.Name"), WizardMessages.getString("NewEnumWizPageAttr.col.Type"), }; 60 61 private static final int COL_KEY = 0; 62 63 private static final int COL_NAME = 1; 64 65 private static final int COL_TYPE = 2; 66 67 private static final int[] COLUMN_WIDTHS = new int []{ 40, 200, 200, }; 68 69 private CheckboxTableViewer mTableViewer = null; 70 71 private Table mTableWidget = null; 72 73 private Button mButtonAdd = null; 74 75 private Button mButtonEdit = null; 76 77 private Button mButtonRemove = null; 78 79 82 protected class AttributeContentProvider implements IStructuredContentProvider 83 { 84 public Object [] getElements(Object inputElement) 85 { 86 List result = new ArrayList (); 87 for (Iterator i = ((TypesafeEnum)inputElement).attributes(); i.hasNext();) 88 { 89 result.add(i.next()); 90 } 91 return result.toArray(); 92 } 93 94 public void dispose() 95 { 96 } 97 98 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) 99 { 100 } 101 } 102 103 106 protected class AttributeLabelProvider extends LabelProvider implements ITableLabelProvider 107 { 108 109 public Image getColumnImage(Object element, int columnIndex) 110 { 111 return null; 112 } 113 114 public String getColumnText(Object element, int columnIndex) 115 { 116 if (element instanceof Attribute) 117 { 118 Attribute attribute = (Attribute)element; 119 120 switch (columnIndex) 121 { 122 case COL_NAME: 123 return attribute.getName() == null ? "" : attribute.getName(); 124 125 case COL_TYPE: 126 return attribute.getType(); 127 128 case COL_KEY: 129 mTableViewer.setChecked(attribute, attribute.isKey()); 130 return ""; 131 } 132 } 133 return null; 134 } 135 } 136 137 140 public NewTypesafeEnumCreationWizardPageAttributes() 141 { 142 super(PAGE_NAME); 143 setTitle(WizardMessages.getString("NewEnumWizPageAttr.title")); setDescription(WizardMessages.getString("NewEnumWizPageAttr.description")); } 146 147 152 public void createControl(Composite parent) 153 { 154 initializeDialogUnits(parent); 155 Composite container = new Composite(parent, SWT.NONE); 156 GridLayout layout = new GridLayout(); 157 layout.numColumns = 2; 158 container.setLayout(layout); 159 160 container.setLayoutData(new GridData(GridData.FILL_BOTH)); 161 162 createTableWidget(container); 163 createButtonsPanel(container); 164 165 hookListeners(); 166 updateEnabledState(); 167 mButtonAdd.forceFocus(); 168 169 setControl(container); 170 Dialog.applyDialogFont(container); 171 setPageComplete(false); 172 } 173 174 177 private void hookListeners() 178 { 179 mTableViewer.addCheckStateListener(new ICheckStateListener() 180 { 181 public void checkStateChanged(CheckStateChangedEvent event) 182 { 183 Object element = event.getElement(); 184 if (element instanceof TableItem) 185 { element = ((TableItem)element).getData(); 187 } 188 if (element instanceof Attribute) 189 { 190 Attribute attribute = (Attribute)element; 191 attribute.setKey(event.getChecked()); 192 validatePage(); 193 } 194 } 195 }); 196 mTableViewer.addSelectionChangedListener(new ISelectionChangedListener() 197 { 198 public void selectionChanged(SelectionChangedEvent event) 199 { 200 updateEnabledState(); 201 } 202 }); 203 mTableViewer.addDoubleClickListener(new IDoubleClickListener() 204 { 205 public void doubleClick(DoubleClickEvent event) 206 { 207 handleEditPressed(); 208 209 } 210 }); 211 212 mButtonAdd.addSelectionListener(new SelectionAdapter() 213 { 214 public void widgetSelected(SelectionEvent e) 215 { 216 handleAddPressed(); 217 } 218 }); 219 mButtonEdit.addSelectionListener(new SelectionAdapter() 220 { 221 public void widgetSelected(SelectionEvent e) 222 { 223 handleEditPressed(); 224 } 225 }); 226 mButtonRemove.addSelectionListener(new SelectionAdapter() 227 { 228 public void widgetSelected(SelectionEvent e) 229 { 230 handleRemovePressed(); 231 } 232 }); 233 } 234 235 private void updateEnabledState() 236 { 237 238 mTableWidget.setEnabled(mTableWidget.getItemCount() > 0); 239 240 boolean enabled = (mTableWidget.getSelectionCount() > 0); 241 mButtonEdit.setEnabled(enabled); 242 mButtonRemove.setEnabled(enabled); 243 } 244 245 private void handleAddPressed() 246 { 247 EditAttributeDialog dialog = new EditAttributeDialog(getShell()); 248 dialog.setAttribute(new Attribute()); 249 dialog.setTitle(WizardMessages.getString("NewEnumWizPageAttr.Add_Attribute")); 250 int reply = dialog.open(); 251 if (reply == EditAttributeDialog.OK) 252 { 253 getTypesafeEnumModel().addAttribute(dialog.getAttribute()); 254 mTableViewer.refresh(); 255 mTableViewer.setSelection(new StructuredSelection(dialog.getAttribute())); 256 mTableViewer.setChecked(dialog.getAttribute(), dialog.getAttribute().isKey()); 257 } 258 updateEnabledState(); 259 ((NewTypesafeEnumCreationWizardPageInstances)getNextPage()).setPageComplete(false); 260 validatePage(); 261 mButtonAdd.forceFocus(); 262 } 263 264 private void handleEditPressed() 265 { 266 Attribute attribute = getSelectedAttribute(); 267 if (attribute == null) 268 { 269 return; 270 } 271 272 EditAttributeDialog dialog = new EditAttributeDialog(getShell()); 273 dialog.setAttribute(attribute); 274 dialog.setTitle(WizardMessages.getString("NewEnumWizPageAttr.Edit_Attribute")); 275 int reply = dialog.open(); 276 mTableViewer.refresh(); 277 mTableViewer.setSelection(new StructuredSelection(dialog.getAttribute())); 278 mTableViewer.setChecked(dialog.getAttribute(), dialog.getAttribute().isKey()); 279 updateEnabledState(); 280 validatePage(); 281 } 282 283 private void handleRemovePressed() 284 { 285 Attribute attribute = getSelectedAttribute(); 286 if (attribute == null) 287 { 288 return; 289 } 290 getTypesafeEnumModel().removeAttribute(attribute); 291 mTableViewer.refresh(); 292 updateEnabledState(); 293 validatePage(); 294 } 295 296 private Attribute getSelectedAttribute() 297 { 298 IStructuredSelection selection = (IStructuredSelection)mTableViewer.getSelection(); 299 if (selection.isEmpty()) 300 { 301 return null; 302 } 303 304 Object element = selection.getFirstElement(); 305 if (element instanceof TableItem) 306 { element = ((TableItem)element).getData(); 308 } 309 Attribute attribute = (Attribute)element; 310 return attribute; 311 } 312 313 316 private void createButtonsPanel(Composite parent) 317 { 318 Composite panel = new Composite(parent, SWT.NONE); 319 GridLayout layout = new GridLayout(); 320 layout.numColumns = 3; 321 panel.setLayout(layout); 322 323 mButtonAdd = new Button(panel, SWT.PUSH); 324 mButtonEdit = new Button(panel, SWT.PUSH); 325 mButtonRemove = new Button(panel, SWT.PUSH); 326 327 mButtonAdd.setText(WizardMessages.getString("NewEnumWizPageAttr.button.Add")); 328 mButtonEdit.setText(WizardMessages.getString("NewEnumWizPageAttr.button.Edit")); 329 mButtonRemove.setText(WizardMessages.getString("NewEnumWizPageAttr.button.Remove")); 330 } 331 332 private void createTableWidget(Composite container) 333 { 334 mTableWidget = new Table(container, SWT.CHECK | SWT.MULTI | SWT.V_SCROLL | SWT.H_SCROLL | SWT.FULL_SELECTION | SWT.BORDER); 335 mTableWidget.setHeaderVisible(true); 336 mTableWidget.setLinesVisible(true); 337 338 int listHeight = mTableWidget.getItemHeight() * 10; Rectangle trim = mTableWidget.computeTrim(0, 0, 0, listHeight); 340 GridData tblGD = new GridData(GridData.FILL_BOTH); 341 tblGD.heightHint = trim.height; 342 tblGD.horizontalSpan = 2; mTableWidget.setLayoutData(tblGD); 344 345 TableLayout tableLayout = new TableLayout(); 346 for (int i = 0; i < COLUMN_WIDTHS.length; i++) 347 { 348 tableLayout.addColumnData(new ColumnWeightData(1, COLUMN_WIDTHS[i], true)); 349 } 350 mTableWidget.setLayout(tableLayout); 351 352 for (int i = 0; i < COLUMN_NAMES.length; i++) 353 { 354 new TableColumn(mTableWidget, SWT.NONE).setText(COLUMN_NAMES[i]); 355 } 356 mTableWidget.pack(); 357 358 mTableViewer = new CheckboxTableViewer(mTableWidget); 359 mTableViewer.setColumnProperties(COLUMN_NAMES); 362 mTableViewer.setLabelProvider(new AttributeLabelProvider()); 363 mTableViewer.setContentProvider(new AttributeContentProvider()); 364 mTableViewer.setInput(getTypesafeEnumModel()); 365 } 366 367 370 private void validatePage() 371 { 372 TypesafeEnum type = getTypesafeEnumModel(); 373 if (type.attributeCount() == 0) 374 { 375 setErrorMessage(WizardMessages.getString("NewEnumWizPageAttr.Must_have_at_least_one_attribute")); 376 setPageComplete(false); 377 return; 378 } 379 for (Iterator i = type.attributes(); i.hasNext();) 380 { 381 Attribute attribute = (Attribute)i.next(); 382 if (attribute.getName().length() == 0) 383 { 384 setErrorMessage(WizardMessages.getString("NewEnumWizPageAttr.attribute_must_have_a_name")); 385 setPageComplete(false); 386 return; 387 } 388 if (!NameUtil.isValidIdentifier(attribute.getName())) 389 { 390 setErrorMessage(WizardMessages.getFormattedString("NewEnumWizPageAttr.invalid_attribute_name", attribute.getName())); 391 setPageComplete(false); 392 return; 393 } 394 if (attribute.getType().length() == 0) 395 { 396 setErrorMessage(WizardMessages.getFormattedString("NewEnumWizPageAttr.attribute_must_have_a_type", attribute.getName())); 397 setPageComplete(false); 398 return; 399 } 400 if (!NameUtil.isValidIdentifier(attribute.getType())) 401 { 402 setErrorMessage(WizardMessages.getFormattedString("NewEnumWizPageAttr.invalid_attribute_type", attribute.getType())); 403 setPageComplete(false); 404 return; 405 } 406 } 407 if (type.keyAttributes().hasNext() == false) 408 { 409 setErrorMessage(WizardMessages.getString("NewEnumWizPageAttr.missing_key_attribute")); 410 setPageComplete(false); 411 return; 412 } 413 setErrorMessage(null); 414 setPageComplete(true); 415 } 416 417 422 public TypesafeEnum getTypesafeEnumModel() 423 { 424 return ((NewTypesafeEnumCreationWizardPage)getPreviousPage()).getTypesafeEnumModel(); 425 } 426 } | Popular Tags |