1 package org.hibernate.eclipse.mapper.editors; 2 3 4 import java.io.IOException ; 5 import java.io.InputStream ; 6 7 import org.eclipse.core.resources.IResource; 8 import org.eclipse.core.resources.IResourceStatus; 9 import org.eclipse.core.runtime.CoreException; 10 import org.eclipse.core.runtime.IProgressMonitor; 11 import org.eclipse.core.runtime.IStatus; 12 import org.eclipse.core.runtime.NullProgressMonitor; 13 import org.eclipse.core.runtime.Status; 14 import org.eclipse.jface.preference.IPreferenceStore; 15 import org.eclipse.jface.text.IDocument; 16 import org.eclipse.swt.events.ShellAdapter; 17 import org.eclipse.swt.events.ShellEvent; 18 import org.eclipse.swt.widgets.Display; 19 import org.eclipse.swt.widgets.Shell; 20 import org.eclipse.ui.IEditorInput; 21 import org.eclipse.ui.IEditorPart; 22 import org.eclipse.ui.IFileEditorInput; 23 import org.eclipse.ui.IPartListener; 24 import org.eclipse.ui.IStorageEditorInput; 25 import org.eclipse.ui.IWorkbench; 26 import org.eclipse.ui.IWorkbenchPart; 27 import org.eclipse.ui.IWorkbenchWindow; 28 import org.eclipse.ui.PartInitException; 29 import org.eclipse.ui.PlatformUI; 30 import org.eclipse.ui.forms.editor.FormEditor; 31 import org.eclipse.ui.forms.editor.IFormPage; 32 import org.eclipse.wst.sse.core.internal.provisional.IStructuredModel; 33 import org.eclipse.wst.sse.core.internal.provisional.exceptions.SourceEditingRuntimeException; 34 import org.eclipse.wst.sse.ui.internal.StructuredTextEditor; 35 import org.eclipse.wst.xml.core.internal.provisional.IXMLPreferenceNames; 36 import org.eclipse.wst.xml.ui.internal.provisional.StructuredTextEditorXML; 37 import org.hibernate.eclipse.mapper.MapperPlugin; 38 import org.hibernate.eclipse.mapper.editors.reveng.OverrideFormPage; 39 import org.hibernate.eclipse.mapper.editors.reveng.OverviewFormPage; 40 import org.hibernate.eclipse.mapper.editors.reveng.PreviewMappingFormPage; 41 import org.hibernate.eclipse.mapper.editors.reveng.SQLTypeMappingFormPage; 42 import org.w3c.dom.Document ; 43 import org.w3c.dom.Node ; 44 45 54 public class ReverseEngineeringMultiPageEditor extends FormEditor { 55 56 59 class PartListener extends ShellAdapter implements IPartListener { 60 private IWorkbenchPart fActivePart; 61 private boolean fIsHandlingActivation = false; 62 63 private void handleActivation() { 64 65 if (fIsHandlingActivation) 66 return; 67 68 if (fActivePart == ReverseEngineeringMultiPageEditor.this) { 69 fIsHandlingActivation = true; 70 try { 71 safelySanityCheckState(); 72 } finally { 73 fIsHandlingActivation = false; 74 } 75 } 76 } 77 78 81 public void partActivated(IWorkbenchPart part) { 82 fActivePart = part; 83 handleActivation(); 84 } 85 86 89 public void partBroughtToTop(IWorkbenchPart part) { 90 } 91 92 95 public void partClosed(IWorkbenchPart part) { 96 } 97 98 101 public void partDeactivated(IWorkbenchPart part) { 102 fActivePart = null; 103 } 104 105 108 public void partOpened(IWorkbenchPart part) { 109 } 110 111 114 public void shellActivated(ShellEvent e) { 115 handleActivation(); 116 } 117 } 118 119 120 private int fSourcePageIndex; 121 122 private StructuredTextEditor fTextEditor; 123 124 private IFormPage overviewPage; 125 private IFormPage typeMappingPage; 126 private IFormPage previewMappingPage; 127 private IFormPage overridePage; 128 129 130 133 public ReverseEngineeringMultiPageEditor() { 134 super(); 135 } 136 137 protected int getDefaultPageIndex() { 138 return overviewPage.getIndex(); 139 } 140 141 145 protected void _firePropertyChange(int property) { 146 } 148 149 152 protected void addSourcePage() { 153 try { 154 fSourcePageIndex = addPage(fTextEditor, getEditorInput()); 155 setPageText(fSourcePageIndex, "source"); 156 } catch (PartInitException exception) { 157 dispose(); 159 MapperPlugin.getDefault().logException(exception); 160 throw new SourceEditingRuntimeException(exception, "An error has occurred when {1}"); 161 } 162 } 163 164 165 protected void addPages() { 166 try { 167 createSourcePage(); 169 createAndAddFormPages(); 170 addSourcePage(); 171 172 setActivePage(); 173 174 } catch (PartInitException e) { 178 MapperPlugin.getDefault().logException(e); 179 throw new RuntimeException (e); 180 } 181 } 182 183 private void createAndAddFormPages() throws PartInitException { 184 overviewPage = new OverviewFormPage(this); 185 addPage(overviewPage); 186 187 typeMappingPage = new SQLTypeMappingFormPage(this); 188 addPage(typeMappingPage); 189 190 overridePage = new OverrideFormPage(this); 191 addPage(overridePage); 192 193 previewMappingPage = new PreviewMappingFormPage(this); 194 addPage(previewMappingPage); 195 196 } 197 198 201 protected void createSourcePage() { 202 fTextEditor = createTextEditor(); 203 fTextEditor.setEditorPart(this); 204 } 206 207 212 protected StructuredTextEditor createTextEditor() { 213 return new StructuredTextEditorXML(); 214 } 215 216 223 public void doSave(IProgressMonitor monitor) { 224 fTextEditor.doSave(monitor); 225 } 226 227 235 public void doSaveAs() { 236 fTextEditor.doSaveAs(); 237 } 243 244 private void editorInputIsAcceptable(IEditorInput input) throws PartInitException { 245 if (input instanceof IFileEditorInput) { 246 CoreException[] coreExceptionArray = new CoreException[1]; 248 if (fileDoesNotExist((IFileEditorInput) input, coreExceptionArray)) { 249 CoreException coreException = coreExceptionArray[0]; 250 if (coreException.getStatus().getCode() == IResourceStatus.FAILED_READ_LOCAL) { 251 try { 257 ((IFileEditorInput) input).getFile().refreshLocal(IResource.DEPTH_ZERO, new NullProgressMonitor()); 258 } catch (CoreException ce) { 259 MapperPlugin.getDefault().logException(ce); 261 } 262 throw new PartInitException("Resource does not exist" + input.getName()); 263 } else { 264 throw new PartInitException("Editor could not be open" + input.getName()); 265 } 266 } 267 } else if (input instanceof IStorageEditorInput) { 268 InputStream contents = null; 269 try { 270 contents = ((IStorageEditorInput) input).getStorage().getContents(); 271 } catch (CoreException noStorageExc) { 272 } 273 if (contents == null) { 274 throw new PartInitException("Editor could not be open on " + input.getName()); 275 } else { 276 try { 277 contents.close(); 278 } catch (IOException e) { 279 } 280 } 281 } 282 } 283 284 285 294 protected boolean fileDoesNotExist(IFileEditorInput input, Throwable [] coreException) { 295 boolean result = false; 296 InputStream inStream = null; 297 if ((!(input.exists())) || (!(input.getFile().exists()))) { 298 result = true; 299 } else { 300 try { 301 inStream = input.getFile().getContents(true); 302 } catch (CoreException e) { 303 result = true; 305 coreException[0] = e; 306 } finally { 307 if (input != null) { 308 try { 309 if (inStream != null) { 310 inStream.close(); 311 } 312 } catch (IOException e) { 313 MapperPlugin.getDefault().logException(e); 314 } 315 } 316 } 317 } 318 return result; 319 } 320 321 public Object getAdapter(Class key) { 322 Object result = null; 323 324 if (fTextEditor != null) { 329 result = fTextEditor.getAdapter(key); 330 } 331 332 return result; 333 } 334 335 338 public Node getCaretNode() { 339 if (getTextEditor() == null) 340 return null; 341 342 return getTextEditor().getCaretNode(); 343 } 344 345 348 public int getCaretPosition() { 349 if (getTextEditor() == null) 350 return -1; 351 352 return getTextEditor().getCaretPosition(); 353 } 354 355 358 public IDocument getDocument() { 359 if (getTextEditor() == null) 360 return null; 361 362 return getTextEditor().getDocument(); 363 } 364 365 368 public Document getDOMDocument() { 369 if (getTextEditor() == null) 370 return null; 371 372 return getTextEditor().getDOMDocument(); 373 } 374 375 378 public IEditorPart getEditorPart() { 379 return this; 380 } 381 382 protected IStructuredModel getModel() { 383 IStructuredModel model = null; 384 if (fTextEditor != null) 385 model = fTextEditor.getModel(); 386 return model; 387 } 388 389 protected IPreferenceStore getPreferenceStore() { 390 return MapperPlugin.getDefault().getPreferenceStore(); 391 } 392 393 public StructuredTextEditor getTextEditor() { 394 return fTextEditor; 395 } 396 397 398 421 422 430 public boolean isSaveAsAllowed() { 431 return fTextEditor != null && fTextEditor.isSaveAsAllowed(); 432 } 433 434 440 public boolean isSaveOnCloseNeeded() { 441 if (fTextEditor != null) 443 return fTextEditor.isSaveOnCloseNeeded(); 444 return isDirty(); 445 } 446 447 455 protected void pageChange(int newPageIndex) { 456 super.pageChange(newPageIndex); 457 saveLastActivePageIndex(newPageIndex); 458 } 459 460 463 protected void postOnDisplayQue(Runnable runnable) { 464 IWorkbench workbench = PlatformUI.getWorkbench(); 465 IWorkbenchWindow[] windows = workbench.getWorkbenchWindows(); 466 if (windows != null && windows.length > 0) { 467 Display display = windows[0].getShell().getDisplay(); 468 display.asyncExec(runnable); 469 } else 470 runnable.run(); 471 } 472 473 474 protected void safelySanityCheckState() { 475 if (getTextEditor() == null) 478 return; 479 480 getTextEditor().safelySanityCheckState(getEditorInput()); 481 482 } 483 484 protected void saveLastActivePageIndex(int newPageIndex) { 485 getPreferenceStore().setValue(IXMLPreferenceNames.LAST_ACTIVE_PAGE, newPageIndex); 487 } 488 489 492 protected void setActivePage() { 493 int activePageIndex = getPreferenceStore().getInt(IXMLPreferenceNames.LAST_ACTIVE_PAGE); 495 496 if (activePageIndex < 0 || activePageIndex >= getPageCount()) { 502 activePageIndex = getDefaultPageIndex(); 503 } 504 setActivePage(activePageIndex); 505 } 506 507 512 protected void setInput(IEditorInput input) { 513 super.setInput(input); 517 519 setPartName(input.getName()); 520 } 521 522 525 public IStatus validateEdit(Shell context) { 526 if (getTextEditor() == null) 527 return new Status(IStatus.ERROR, MapperPlugin.ID, IStatus.INFO, "", null); 529 return getTextEditor().validateEdit(context); 530 } 531 } 532 | Popular Tags |