1 11 package org.eclipse.ui.forms.editor; 12 13 import java.util.Vector ; 14 15 import org.eclipse.core.runtime.ListenerList; 16 import org.eclipse.jface.dialogs.IPageChangeProvider; 17 import org.eclipse.jface.dialogs.IPageChangedListener; 18 import org.eclipse.jface.dialogs.PageChangedEvent; 19 import org.eclipse.jface.util.SafeRunnable; 20 import org.eclipse.jface.viewers.ISelection; 21 import org.eclipse.jface.viewers.ISelectionProvider; 22 import org.eclipse.jface.viewers.SelectionChangedEvent; 23 import org.eclipse.jface.viewers.StructuredSelection; 24 import org.eclipse.swt.widgets.Composite; 25 import org.eclipse.swt.widgets.Control; 26 import org.eclipse.swt.widgets.Display; 27 import org.eclipse.ui.IEditorActionBarContributor; 28 import org.eclipse.ui.IEditorInput; 29 import org.eclipse.ui.IEditorPart; 30 import org.eclipse.ui.IEditorSite; 31 import org.eclipse.ui.PartInitException; 32 import org.eclipse.ui.forms.IManagedForm; 33 import org.eclipse.ui.forms.widgets.FormToolkit; 34 import org.eclipse.ui.part.MultiPageEditorActionBarContributor; 35 import org.eclipse.ui.part.MultiPageEditorPart; 36 import org.eclipse.ui.part.MultiPageSelectionProvider; 37 38 61 public abstract class FormEditor extends MultiPageEditorPart implements 62 IPageChangeProvider { 63 64 71 protected Vector pages = new Vector (); 72 73 private FormToolkit toolkit; 74 75 private int currentPage = -1; 76 77 private ListenerList pageListeners = new ListenerList(); 78 79 private static class FormEditorSelectionProvider extends 80 MultiPageSelectionProvider { 81 private ISelection globalSelection; 82 83 86 public FormEditorSelectionProvider(FormEditor formEditor) { 87 super(formEditor); 88 } 89 90 public ISelection getSelection() { 91 IEditorPart activeEditor = ((FormEditor) getMultiPageEditor()) 92 .getActiveEditor(); 93 if (activeEditor != null) { 94 ISelectionProvider selectionProvider = activeEditor.getSite() 95 .getSelectionProvider(); 96 if (selectionProvider != null) 97 return selectionProvider.getSelection(); 98 } 99 if (globalSelection != null) { 100 return globalSelection; 101 } 102 return StructuredSelection.EMPTY; 103 } 104 105 108 public void setSelection(ISelection selection) { 109 IEditorPart activeEditor = ((FormEditor) getMultiPageEditor()) 110 .getActiveEditor(); 111 if (activeEditor != null) { 112 ISelectionProvider selectionProvider = activeEditor.getSite() 113 .getSelectionProvider(); 114 if (selectionProvider != null) 115 selectionProvider.setSelection(selection); 116 } else { 117 this.globalSelection = selection; 118 fireSelectionChanged(new SelectionChangedEvent(this, 119 globalSelection)); 120 } 121 } 122 } 123 124 127 public FormEditor() { 128 } 129 130 133 public void init(IEditorSite site, IEditorInput input) 134 throws PartInitException { 135 setSite(site); 136 setInput(input); 137 site.setSelectionProvider(new FormEditorSelectionProvider(this)); 138 } 139 140 145 protected void createPages() { 146 addPages(); 147 } 148 149 152 protected Composite createPageContainer(Composite parent) { 153 parent = super.createPageContainer(parent); 154 toolkit = createToolkit(parent.getDisplay()); 155 return parent; 156 } 157 158 169 protected FormToolkit createToolkit(Display display) { 170 return new FormToolkit(display); 171 } 172 173 177 protected abstract void addPages(); 178 179 184 public void addPageChangedListener(IPageChangedListener listener) { 185 pageListeners.add(listener); 186 } 187 188 193 public void removePageChangedListener(IPageChangedListener listener) { 194 pageListeners.remove(listener); 195 } 196 197 202 public Object getSelectedPage() { 203 return getActivePageInstance(); 204 } 205 206 214 public int addPage(IFormPage page) throws PartInitException { 215 int i = super.addPage(page.getPartControl()); 216 configurePage(i, page); 217 return i; 218 } 219 220 231 public void addPage(int index, IFormPage page) throws PartInitException { 232 super.addPage(index, page.getPartControl()); 233 configurePage(index, page); 234 } 235 236 244 public int addPage(Control control) { 245 int i = super.addPage(control); 246 try { 247 registerPage(-1, control); 248 } catch (PartInitException e) { 249 } 251 return i; 252 } 253 254 264 public void addPage(int index, Control control) { 265 super.addPage(index, control); 266 try { 267 registerPage(index, control); 268 } catch (PartInitException e) { 269 } 271 } 272 273 282 283 public boolean isDirty() { 284 if (pages != null) { 285 for (int i = 0; i < pages.size(); i++) { 286 Object page = pages.get(i); 287 if (page instanceof IFormPage) { 288 IFormPage fpage = (IFormPage) page; 289 if (fpage.isDirty()) 290 return true; 291 } 292 } 293 } 294 return super.isDirty(); 295 } 296 297 304 305 protected void commitPages(boolean onSave) { 306 if (pages != null) { 307 for (int i = 0; i < pages.size(); i++) { 308 Object page = (IFormPage)pages.get(i); 309 if (page instanceof IFormPage) { 310 IFormPage fpage = (IFormPage)page; 311 IManagedForm mform = fpage.getManagedForm(); 312 if (mform != null && mform.isDirty()) 313 mform.commit(onSave); 314 } 315 } 316 } 317 } 318 319 324 public int addPage(IEditorPart editor, IEditorInput input) 325 throws PartInitException { 326 int index = super.addPage(editor, input); 327 if (editor instanceof IFormPage) 328 configurePage(index, (IFormPage) editor); 329 else 330 registerPage(-1, editor); 331 return index; 332 } 333 334 341 public void addPage(int index, IEditorPart editor, IEditorInput input) 342 throws PartInitException { 343 super.addPage(index, editor, input); 344 if (editor instanceof IFormPage) 345 configurePage(index, (IFormPage) editor); 346 else 347 registerPage(index, editor); 348 } 349 350 360 protected void configurePage(int index, IFormPage page) 361 throws PartInitException { 362 setPageText(index, page.getTitle()); 363 page.setIndex(index); 365 registerPage(index, page); 366 } 367 368 374 public void removePage(int pageIndex) { 375 if (pageIndex >= 0 && pageIndex < pages.size()) { 376 Object page = pages.get(pageIndex); 377 pages.remove(page); 378 if (page instanceof IFormPage) { 379 IFormPage fpage = (IFormPage) page; 380 if (!fpage.isEditor()) 381 fpage.dispose(); 382 updatePageIndices(); 383 } 384 } 385 super.removePage(pageIndex); 386 } 387 388 private void updatePageIndices() { 390 for (int i = 0; i < pages.size(); i++) { 391 Object page = pages.get(i); 392 if (page instanceof IFormPage) { 393 IFormPage fpage = (IFormPage) page; 394 fpage.setIndex(i); 395 } 396 } 397 } 398 399 403 public void editorDirtyStateChanged() { 404 firePropertyChange(PROP_DIRTY); 405 } 406 407 411 public void dispose() { 412 super.dispose(); 413 for (int i = 0; i < pages.size(); i++) { 414 Object page = pages.get(i); 415 if (page instanceof IFormPage) { 416 IFormPage fpage = (IFormPage) page; 417 if (!fpage.isEditor()) 420 fpage.dispose(); 421 } 422 } 423 pages = null; 424 if (toolkit != null) { 427 toolkit.dispose(); 428 toolkit = null; 429 } 430 } 431 432 437 public FormToolkit getToolkit() { 438 return toolkit; 439 } 440 441 446 public IEditorPart getActiveEditor() { 447 return super.getActiveEditor(); 448 } 449 450 463 protected int getCurrentPage() { 464 return currentPage; 465 } 466 467 470 protected void pageChange(int newPageIndex) { 471 int oldPageIndex = getCurrentPage(); 473 if (oldPageIndex != -1 && pages.size() > oldPageIndex 474 && pages.get(oldPageIndex) instanceof IFormPage 475 && oldPageIndex != newPageIndex) { 476 IFormPage oldFormPage = (IFormPage) pages.get(oldPageIndex); 478 if (oldFormPage.canLeaveThePage() == false) { 479 setActivePage(oldPageIndex); 480 return; 481 } 482 } 483 Object page = pages.get(newPageIndex); 485 if (page instanceof IFormPage) { 486 IFormPage fpage = (IFormPage) page; 487 if (fpage.getPartControl() == null) { 488 fpage.createPartControl(getContainer()); 489 setControl(newPageIndex, fpage.getPartControl()); 490 fpage.getPartControl().setMenu(getContainer().getMenu()); 491 } 492 } 493 if (oldPageIndex != -1 && pages.size() > oldPageIndex 494 && pages.get(oldPageIndex) instanceof IFormPage) { 495 IFormPage oldFormPage = (IFormPage) pages.get(oldPageIndex); 497 IManagedForm mform = oldFormPage.getManagedForm(); 498 if (mform != null) 499 mform.commit(false); 500 } 501 if (pages.size() > newPageIndex 502 && pages.get(newPageIndex) instanceof IFormPage) 503 ((IFormPage) pages.get(newPageIndex)).setActive(true); 504 if (oldPageIndex != -1 && pages.size() > oldPageIndex 505 && newPageIndex != oldPageIndex && 506 pages.get(oldPageIndex) instanceof IFormPage) 507 ((IFormPage) pages.get(oldPageIndex)).setActive(false); 508 super.pageChange(newPageIndex); 510 this.currentPage = newPageIndex; 511 IFormPage newPage = getActivePageInstance(); 512 if (newPage != null) 513 firePageChanged(new PageChangedEvent(this, newPage)); 514 } 515 516 523 public IFormPage setActivePage(String pageId) { 524 for (int i = 0; i < pages.size(); i++) { 525 Object page = pages.get(i); 526 if (page instanceof IFormPage) { 527 IFormPage fpage = (IFormPage) page; 528 if (fpage.getId().equals(pageId)) { 529 setActivePage(i); 530 return fpage; 531 } 532 } 533 } 534 return null; 535 } 536 537 544 public IFormPage findPage(String pageId) { 545 for (int i = 0; i < pages.size(); i++) { 546 Object page = pages.get(i); 547 if (page instanceof IFormPage) { 548 IFormPage fpage = (IFormPage) pages.get(i); 549 if (fpage.getId().equals(pageId)) 550 return fpage; 551 } 552 } 553 return null; 554 } 555 556 566 public IFormPage setActivePage(String pageId, Object pageInput) { 567 IFormPage page = setActivePage(pageId); 568 if (page != null) { 569 IManagedForm mform = page.getManagedForm(); 570 if (mform != null) 571 mform.setInput(pageInput); 572 } 573 return page; 574 } 575 576 586 public IFormPage selectReveal(Object pageInput) { 587 for (int i = 0; i < pages.size(); i++) { 588 Object page = pages.get(i); 589 if (page instanceof IFormPage) { 590 IFormPage fpage = (IFormPage) page; 591 if (fpage.selectReveal(pageInput)) 592 return fpage; 593 } 594 } 595 return null; 596 } 597 598 605 public IFormPage getActivePageInstance() { 606 int index = getActivePage(); 607 if (index != -1) { 608 Object page = pages.get(index); 609 if (page instanceof IFormPage) 610 return (IFormPage) page; 611 } 612 return null; 613 } 614 615 618 protected void setActivePage(int pageIndex) { 619 if (pages.size() > pageIndex 622 && pages.get(pageIndex) instanceof IFormPage) { 623 pageChange(pageIndex); 624 IFormPage activePage = (IFormPage) pages.get(pageIndex); 625 activePage.setActive(true); 626 super.setActivePage(pageIndex); 627 } else 628 super.setActivePage(pageIndex); 629 updateActionBarContributor(pageIndex); 630 } 631 632 638 protected void updateActionBarContributor(int pageIndex) { 639 IEditorActionBarContributor contributor = getEditorSite() 642 .getActionBarContributor(); 643 if (contributor != null 644 && contributor instanceof MultiPageEditorActionBarContributor) { 645 ((MultiPageEditorActionBarContributor) contributor) 646 .setActivePage(getEditor(pageIndex)); 647 } 648 } 649 650 657 public void close(final boolean save) { 658 Display display = getSite().getShell().getDisplay(); 659 display.asyncExec(new Runnable () { 660 public void run() { 661 if (toolkit != null) { 662 getSite().getPage().closeEditor(FormEditor.this, save); 663 } 664 } 665 }); 666 } 667 668 private void registerPage(int index, Object page) throws PartInitException { 669 if (!pages.contains(page)) { 670 if (index == -1) 671 pages.add(page); 672 else 673 pages.add(index, page); 674 } 675 if (page instanceof IFormPage) { 676 IFormPage fpage = (IFormPage) page; 677 if (fpage.isEditor() == false) 678 fpage.init(getEditorSite(), getEditorInput()); 679 } 680 } 681 682 private void firePageChanged(final PageChangedEvent event) { 683 Object [] listeners = pageListeners.getListeners(); 684 for (int i = 0; i < listeners.length; ++i) { 685 final IPageChangedListener l = (IPageChangedListener) listeners[i]; 686 SafeRunnable.run(new SafeRunnable() { 687 public void run() { 688 l.pageChanged(event); 689 } 690 }); 691 } 692 } 693 } 694 | Popular Tags |