1 17 package org.eclipse.emf.importer.rose.ui; 18 19 import org.eclipse.core.runtime.IStatus; 20 import org.eclipse.core.runtime.NullProgressMonitor; 21 import org.eclipse.core.runtime.Status; 22 import org.eclipse.jface.viewers.ColumnWeightData; 23 import org.eclipse.jface.viewers.TableLayout; 24 import org.eclipse.jface.viewers.TableViewer; 25 import org.eclipse.swt.SWT; 26 import org.eclipse.swt.events.FocusAdapter; 27 import org.eclipse.swt.events.FocusEvent; 28 import org.eclipse.swt.events.KeyAdapter; 29 import org.eclipse.swt.events.KeyEvent; 30 import org.eclipse.swt.graphics.Image; 31 import org.eclipse.swt.layout.GridData; 32 import org.eclipse.swt.layout.GridLayout; 33 import org.eclipse.swt.widgets.Button; 34 import org.eclipse.swt.widgets.Composite; 35 import org.eclipse.swt.widgets.DirectoryDialog; 36 import org.eclipse.swt.widgets.Event; 37 import org.eclipse.swt.widgets.Group; 38 import org.eclipse.swt.widgets.Table; 39 import org.eclipse.swt.widgets.TableColumn; 40 import org.eclipse.swt.widgets.TableItem; 41 import org.eclipse.swt.widgets.Text; 42 43 import org.eclipse.emf.common.notify.AdapterFactory; 44 import org.eclipse.emf.common.notify.impl.AdapterFactoryImpl; 45 import org.eclipse.emf.common.ui.celleditor.ExtendedTableEditor; 46 import org.eclipse.emf.edit.provider.ItemProvider; 47 import org.eclipse.emf.edit.ui.provider.AdapterFactoryContentProvider; 48 import org.eclipse.emf.edit.ui.provider.AdapterFactoryLabelProvider; 49 import org.eclipse.emf.importer.ImporterPlugin; 50 import org.eclipse.emf.importer.ModelImporter; 51 import org.eclipse.emf.importer.rose.RoseImporter; 52 import org.eclipse.emf.importer.rose.RoseImporterPlugin; 53 import org.eclipse.emf.importer.ui.contribution.base.ModelDetailPage; 54 import org.eclipse.emf.importer.util.ImporterUtil; 55 56 57 60 public class RoseDetailPage extends ModelDetailPage 61 { 62 protected Button loadPathMapSymbolsButton; 63 protected Button browsePathMapLocationButton; 64 protected Table pathMapTable; 65 protected TableViewer pathMapTableViewer; 66 67 protected boolean isCellEditing = false; 68 69 public RoseDetailPage(ModelImporter modelImporter, String pageName) 70 { 71 super(modelImporter, pageName); 72 73 setTitle(RoseImporterPlugin.INSTANCE.getString("_UI_RoseModelImport_title")); 74 setDescription(RoseImporterPlugin.INSTANCE.getString(showGenModel() ? 75 "_UI_RoseModelImportNewProject_description" : "_UI_RoseModelImportFile_description")); 76 } 77 78 public void dispose() 79 { 80 if (loadPathMapSymbolsButton != null) 81 { 82 loadPathMapSymbolsButton.removeListener(SWT.Selection, this); 83 loadPathMapSymbolsButton = null; 84 } 85 if (browsePathMapLocationButton != null) 86 { 87 browsePathMapLocationButton.removeListener(SWT.Selection, this); 88 browsePathMapLocationButton = null; 89 } 90 if (pathMapTable != null) 91 { 92 pathMapTable.removeListener(SWT.Selection, this); 93 pathMapTable = null; 94 } 95 if (pathMapTableViewer != null) 96 { 97 pathMapTableViewer = null; 98 } 99 100 super.dispose(); 101 } 102 103 protected boolean supportMultipleModelLocation() 104 { 105 return false; 106 } 107 108 public RoseImporter getRoseImporter() 109 { 110 return (RoseImporter)getModelImporter(); 111 } 112 113 protected void addControl(Composite parent) 114 { 115 Group pathMapGroup = new Group(parent, SWT.SHADOW_ETCHED_IN); 116 { 117 GridLayout layout = new GridLayout(); 118 layout.numColumns = 2; 119 layout.verticalSpacing = 12; 120 pathMapGroup.setLayout(layout); 121 } 122 pathMapGroup.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1)); 123 pathMapGroup.setText(RoseImporterPlugin.INSTANCE.getString("_UI_PathMap_label")); 124 125 Composite buttonComposite = new Composite(pathMapGroup, SWT.NONE); 126 { 127 GridData data = new GridData(GridData.HORIZONTAL_ALIGN_END); 128 data.horizontalSpan = 2; 129 buttonComposite.setLayoutData(data); 130 131 GridLayout layout = new GridLayout(2, true); 132 layout.marginLeft = -5; 133 layout.marginRight = -5; 134 layout.marginTop = -5; 135 layout.marginBottom = -5; 136 buttonComposite.setLayout(layout); 137 } 138 139 loadPathMapSymbolsButton = new Button(buttonComposite, SWT.PUSH); 140 loadPathMapSymbolsButton.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 141 loadPathMapSymbolsButton.setText(RoseImporterPlugin.INSTANCE.getString("_UI_LoadSymbols_label")); 142 loadPathMapSymbolsButton.addListener(SWT.Selection, this); 143 144 browsePathMapLocationButton = new Button(buttonComposite, SWT.PUSH); 145 browsePathMapLocationButton.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 146 browsePathMapLocationButton.setText(RoseImporterPlugin.INSTANCE.getString("_UI_Browse_label")); 147 browsePathMapLocationButton.setEnabled(false); 148 browsePathMapLocationButton.addListener(SWT.Selection, this); 149 150 pathMapTable = new Table(pathMapGroup, SWT.BORDER); 151 pathMapTable.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1)); 152 pathMapTableViewer = new TableViewer(pathMapTable); 153 154 pathMapTable.setHeaderVisible(true); 155 pathMapTable.setLinesVisible(true); 156 { 157 TableLayout layout = new TableLayout(); 158 159 TableColumn variableNameColumn = new TableColumn(pathMapTable, SWT.NONE); 160 layout.addColumnData(new ColumnWeightData(2, true)); 161 variableNameColumn.setText(RoseImporterPlugin.INSTANCE.getString("_UI_SymbolName_label")); 162 variableNameColumn.setResizable(true); 163 164 TableColumn locationColumn = new TableColumn(pathMapTable, SWT.NONE); 165 layout.addColumnData(new ColumnWeightData(3, true)); 166 locationColumn.setText(RoseImporterPlugin.INSTANCE.getString("_UI_ActualLocation_label")); 167 locationColumn.setResizable(true); 168 169 pathMapTable.setLayout(layout); 170 } 171 pathMapTable.addListener(SWT.Selection, this); 172 173 new ExtendedTableEditor(pathMapTable) 174 { 175 protected void editItem(final TableItem tableItem, final int column) 176 { 177 switch (column) 178 { 179 case 1: { 180 final String string = tableItem.getText(column); 181 horizontalAlignment = SWT.LEFT; 182 minimumWidth = Math.max(50, tableItem.getBounds(column).width); 183 184 final Text text = new Text(table, SWT.NONE); 185 setEditor(text, tableItem, column); 186 text.setFocus(); 187 text.setText(string); 188 text.setSelection(0, string.length()); 189 190 text.addFocusListener(new FocusAdapter() 191 { 192 public void focusLost(FocusEvent event) 193 { 194 modify(tableItem, column, text); 195 } 196 }); 197 198 text.addKeyListener(new KeyAdapter() 199 { 200 public void keyPressed(KeyEvent event) 201 { 202 if (event.character == '\r' || event.character == '\n') 203 { 204 modify(tableItem, column, text); 205 setEditor(null); 206 text.dispose(); 207 } 208 else if (event.character == '\033') 209 { 210 setEditor(null); 211 text.dispose(); 212 } 213 } 214 }); 215 216 isCellEditing = true; 217 setPageComplete(false); 218 break; 219 } 220 } 221 } 222 223 protected void modify(TableItem tableItem, int column, Text text) 224 { 225 tableItem.setText(column, text.getText()); 226 String key = tableItem.getText(); 227 String value = tableItem.getText(column); 228 text.setVisible(false); 229 if ("".equals(value)) 230 { 231 value = null; 232 } 233 getRoseImporter().getPathMap().put(key, value); 234 235 isCellEditing = false; 236 setErrorMessage(null); 237 setMessage(null); 238 setPageComplete(isPageComplete()); 239 } 240 }; 241 242 AdapterFactory adapterFactory = new AdapterFactoryImpl(); 243 pathMapTableViewer.setColumnProperties(new String []{ "a", "b" }); 244 pathMapTableViewer.setContentProvider(new AdapterFactoryContentProvider(adapterFactory)); 245 pathMapTableViewer.setLabelProvider(new AdapterFactoryLabelProvider(adapterFactory) 246 { 247 public Image getColumnImage(Object o, int columnIndex) 248 { 249 switch (columnIndex) 250 { 251 default: { 252 return null; 253 } 254 } 255 } 256 257 public String getColumnText(Object o, int columnIndex) 258 { 259 switch (columnIndex) 260 { 261 case 0: { 262 return (String )o; 263 } 264 default: 265 case 1: { 266 String result = (String )getRoseImporter().getPathMap().get(o); 267 return result == null ? "" : result; 268 } 269 } 270 } 271 }); 272 } 273 274 protected void doHandleEvent(Event event) 275 { 276 if (event.type == SWT.Selection && event.widget == loadPathMapSymbolsButton) 277 { 278 if (modelLocationText.getText().trim().length() > 0) 279 { 280 getControl().getDisplay().syncExec(new Runnable () 281 { 282 public void run() 283 { 284 loadPathMapSymbols();; 285 } 286 }); 287 } 288 } 289 else if (event.type == SWT.Selection && event.widget == pathMapTable) 290 { 291 browsePathMapLocationButton.setEnabled(pathMapTable.getSelectionIndex() != -1); 292 } 293 else if (event.type == SWT.Selection && event.widget == browsePathMapLocationButton) 294 { 295 int index = pathMapTable.getSelectionIndex(); 296 String symbol = pathMapTable.getItem(index).getText(); 297 DirectoryDialog directoryDialog = new DirectoryDialog(getShell()); 298 directoryDialog.setMessage(RoseImporterPlugin.INSTANCE.getString("_UI_PathMapDirectoryDialog_message", new Object [] { symbol })); 299 String path = directoryDialog.open(); 300 301 if (path != null && path.length() > 0) 302 { 303 getRoseImporter().getPathMap().put(symbol, path); 304 setPageComplete(false); 305 pathMapTableViewer.refresh(); 306 if (++index < pathMapTable.getItemCount()) 307 { 308 pathMapTable.select(index); 309 } 310 } 311 } 312 else 313 { 314 super.doHandleEvent(event); 315 } 316 getContainer().updateButtons(); 317 } 318 319 public boolean isPageComplete() 320 { 321 return !isCellEditing && super.isPageComplete(); 322 } 323 324 protected void adjustLoadButton() 325 { 326 super.adjustLoadButton(); 327 loadPathMapSymbolsButton.setEnabled(loadButton.isEnabled()); 328 } 329 330 protected void refreshModel() 331 { 332 super.refreshModel(); 333 pathMapTableViewer.setInput(new ItemProvider(getRoseImporter().getPathMap().keySet())); 334 } 335 336 protected void loadPathMapSymbols() 337 { 338 IStatus status = null; 339 try 340 { 341 status = getRoseImporter().loadPathMap(new NullProgressMonitor()); 342 } 343 catch (Exception exception) 344 { 345 status = ImporterUtil.createErrorStatus(exception, false); 346 } 347 348 internalSetGenModelFileName(status.isOK() ? 349 getRoseImporter().getGenModelFileName() : 350 getRoseImporter().computeDefaultGenModelFileName()); 351 352 IStatus nameStatus = getRoseImporter().checkGenModelFileName(); 353 if (!nameStatus.isOK()) 354 { 355 if (status.isOK()) 356 { 357 status = nameStatus; 358 } 359 else 360 { 361 status = ImporterUtil.mergeStatus(status, nameStatus); 362 } 363 } 364 365 if (status.isOK() && getRoseImporter().getPathMap().isEmpty()) 366 { 367 status = new Status(IStatus.INFO, ImporterPlugin.ID, 368 ImporterUtil.ACTION_DEFAULT, RoseImporterPlugin.INSTANCE.getString("_UI_NoPathMap_message"), null); 369 } 370 371 pathMapTableViewer.setInput(new ItemProvider(getRoseImporter().getPathMap().keySet())); 372 handleStatus(status, null, RoseImporterPlugin.INSTANCE.getString("_UI_LoadProblem_title"), RoseImporterPlugin.INSTANCE.getString("_UI_RoseLoadFailed_message")); 373 } 374 } | Popular Tags |