L0036 - Creating Views and Editors


Published on

A detailed description of how to create and manage views, editors and perspectives.

Published in: Education, Technology
  • Be the first to comment

L0036 - Creating Views and Editors

  1. 1. Creating Views and EditorsA detailed description of how to create and manage views, editors andperspectives.Redistribution and other use of this material requires written permission from The RCP Company.L0001 - 2010-11-27
  2. 2. Views and Editors in Eclipse ApplicationsIn any Eclipse Application (RCP or IDE) theprimary user interface is via views and editorsorganized in one or more perspectives2 L0001 - 2010-11-27
  3. 3. Basic Eclipse UI Information Model Workspace Window Top-level Menu Page Perspective Top-level Toolbar Status Line Editors Views Switcher Local Toolbar Drop-Down Menu3 L0001 - 2010-11-27
  4. 4. Editors and ViewsThe main functionality of an Eclipse application is handled via editors andviews and the actions provided by theseThe use of editors and views is different  Editors express the state of one or more editor inputs – typically file resources  Editors have an associated life-cycle  Views express additional navigational or context-oriented information – e.g. problems, outline or search – and is usually stateless  Views have no state, only configuration – e.g. colors and column directionsEditors are explicitly saved whereas views are implicitly savedEditors and views are grouped together via perspectives4 L0001 - 2010-11-27
  5. 5. Visual Differences between Editors and ViewsThese are some visual differences between editors and views:  Editors provide additional actions to the top-level menu and the tool bar; views do not  Views have an independent menu bar and tool bar; editors do not  There are at most one active editor in a perspective; there can be any number of active views  The are at most one view of each “type”; there can be any number of editors  Editors are all placed in the “editor area”; views are placed in a number of “view stacks”Using the Presentation API, the visual difference can be ironed out5 L0001 - 2010-11-27
  6. 6. Sites, Parts and ReferencesViews and Editors are represented in threedifferent ways in the workbench page:  The site object (IViewSite and IEditorSite) Site represents the workbench state that is specific for a part  Access to the workbench, the workbench window, and the workbench page  Access to the ID and name of the part Reference (from the view or editor extension)  Registration of context menus  The part object (IViewPart and IEditorPart) represents the actual part of a site Part  This comes from the developed plug-in  The reference object (IViewReference and IEditorReference) represents a reference to a specific part object  Used to prevent premature activation of plug-ins6 L0001 - 2010-11-27
  7. 7. ViewsThe Editor IDE contains views with all sorts of content. Some relevantexamples are:  A table with items: Problems  A styled text: Javadoc  A big (virtual) tree: Package Explorer  Using the Forms UI: Properties (depends on the current editor)  A graph: Metrics  A view with content supplied by the editor: Outline  Tables and trees with sorting and filtering: Problems and OutlineIf in doubt about how to implement a specify feature, always have a look atthe views from the IDE. Although these sometimes can seem overlycomplicated, they can provide good inspiration.7 L0001 - 2010-11-27
  8. 8. Adding a ViewViews are defined via the extension point org.eclipse.ui.views/view  Has id, name, class, category, icon, fastViewWidthRatio, allowMultiple, restorable and description (a sub-element)  Most attributes are optional <extension point="org.eclipse.ui.views"> <view category="com.rcpcompany.cexx.viewCategories.Main" class="com.rcpcompany.cexx.views.ContactTableView" id="com.rcpcompany.cexx.views.ContactTableView" name="Contact Table" restorable="true"> <description> This view contains a simple table with all defined contacts along with a filter. </description> </view> </extension>8 L0001 - 2010-11-27
  9. 9. View Life CycleBefore the view is shown  init(IViewSite) or init(IViewSite, IMemento): marks the start of the view. The second version is called for views that have saved a memento  createPartControl(Composite): Creates the visual content of the view  setFocus(): Sets the initial focus of the viewWhen the view is closed  saveState(IMemento): Saves the state of the view in a memento  dispose(): Disposes all resources – especially images and fonts9 L0001 - 2010-11-27
  10. 10. Standard Views (RCP only) The platform contains a number of standard views that can be added to an RCP application via plugin.xml For example, to add the Progress view, use: <extension point="org.eclipse.ui.views"> <view class="org.eclipse.ui.ExtensionFactory:progressView" id="com.rcpcompany.gir.application.views.progress" name="Progress"/> </extension>10 L0001 - 2010-11-27
  11. 11. Editors Content  Text – e.g. a programming language  Graphical – e.g. a model or an XML file  Form – e.g. an XML file or a configuration file  A combination – e.g. the plugin.xml editor11 L0001 - 2010-11-27
  12. 12. Editors Interface more complicated than for views, with many more mandatory methods  Saving editor content (ISaveablePart)  Introduces IEditorInput12 L0001 - 2010-11-27
  13. 13. Editor Input The subject of an editor is specified via an IEditorInput  Simple interface: public interface IEditorInput extends IAdaptable { public boolean exists(); public ImageDescriptor getImageDescriptor(); public String getName(); public IPersistableElement getPersistable(); public String getToolTipText(); }  Typically a file based on the resource API  But can be anything – specialized as  IPathEditorInput: Local file system resource  IFileEditorInput: Resource API-based resource  IStorageEditorInput: Storage-based resource – any sequence of bytes Subclass EditorInput for your own new editor inputs13 L0001 - 2010-11-27
  14. 14. Editor Life Cycle Before the view is shown  init(IEditorSite, IEditorInput): marks the start of the editor  createPartControl(Composite): Creates the visual content of the editor  setFocus(): Sets the initial focus of the editor When the editor is closed  isDirty() and isSaveAsAllowed(): Returns whether saving is needed or indeed allowed  Used by workbench to determine whether exit is allowed  doSave(IProgressMonitor) or doSaveAs(): Saves the state of the editor  dispose(): Disposes all resources – especially images and fonts14 L0001 - 2010-11-27
  15. 15. Lab Exercise Create a number of views using the UI designer  If you use the Visual Designer, then start Eclipse 3.2 with its own workspace and import the existing plug-ins!15 L0001 - 2010-11-27
  16. 16. Saving State (IMemento) When the workbench closes, all state is saved – including all workbench-related state When the workbench is reopened, all state is re-read State saved in an IMemento  Hierarchical structure  Normally XML based  Data can be  Strings, integers or floats  Text blocks The view life cycle explicitly handles state  init(IViewSite, IMemento)  saveState(IMemento) Saved as file in the local configuration directory  Can be saved anywhere  Workbench reads file workbench.xml16 L0001 - 2010-11-27
  17. 17. Saving State (IMemento) The IMemento interface has three groups of operations:  Operations to get or create a child context  Operations to get or set the values of a context  A few operations to handle the IDs public interface IMemento { public IMemento createChild(String type); public IMemento createChild(String type, String id); public IMemento getChild(String type); public IMemento[] getChildren(String type); public Float getFloat(String key); public String getID(); public Integer getInteger(String key); public String getString(String key); public String getTextData(); public void putFloat(String key, float value); public void putInteger(String key, int value); public void putMemento(IMemento memento); public void putString(String key, String value); public void putTextData(String data); }17 L0001 - 2010-11-27
  18. 18. Saving State in Views and Editors The view life cycle explicitly handles state  init(IViewSite, IMemento)  Will be null the first time!  saveState(IMemento)  Because views are shared between perspectives, init and saveState will only be called once per WorkbenchWindow! In Eclipse 3.3 an editor can also save state  Must implement IPersistableEditor  restoreState(IMemento)  saveState(IMemento)18 L0001 - 2010-11-27
  19. 19. Lab Exercise Save and restore the content of the first text field of the first view using IMemento:  In WorkbenchAdvisor.preStartup() use getWorkbenchConfigurer().setSaveAndRestore(true) to enable saved state (RCP only)19 L0001 - 2010-11-27
  20. 20. Perspectives A perspective is a description of a number of user interface elements that is used to perform a specific task  Whether or not an editor is used  A specific type of resources that will be shown in the navigator (if used)  A specific set of views that is used in the perspective  A specific set of shortcuts to be added to certain menus  Wizards to be shown at the top of the “File” → “New…” menu  Views to be shown at the top of the “Window” → “Show View” menu  Perspectives shown at the top of the perspective switcher  A specific set of action sets to be added to the top-level menu and tool bar A perspective also specifies the layout of the editors and the associated views using a “slice” system Additions can be made to a perspective using the perspectiveExtensions extension point20 L0001 - 2010-11-27
  21. 21. Standard Perspectives The Eclipse IDE includes a large number of perspectives that correspond to the many tasks that must be performed in a development environment. For example:  Java  Debug  Team  CVS Repository21 L0001 - 2010-11-27
  22. 22. Perspective Implementation Perspective is a specific layout and views and editors within a Workbench window Workspace Window Top-level Menu Page Perspective Top-level Toolbar Status Line Editors Views Switcher Local Toolbar Drop-Down Menu22 L0001 - 2010-11-27
  23. 23. Adding a New Perspective A new perspective can easily be added using the extensions page for the plugin.xml file  Add a new extension with the name “org.eclipse.ui.perspectives”  Select the new line and add a “New”→”perspective” using the context menu  Specify ID, name and factory class  Click on the “class” link to create the new View class automatically23 L0001 - 2010-11-27
  24. 24. Designing Perspectives Nearly any layout with an editor (optional) and zero, one or more views can be designed using addView(…) The method is rather simple:  You start with the editor area  You then add one new view at a time  Each time, you divide an already placed view according to an edge (TOP, RIGHT, BOTTOM, or LEFT) and a percentage (in the range [0.05; 0.95]) Thus addView("view2", IPageLayout.LEFT, 0.4f, "view1") means  Find the already placed view view1  Place view2 to the LEFT of view1  Assign 40% of the original area of view1 to view224 L0001 - 2010-11-27
  25. 25. Designing Perspectives 1 2 4 Some layouts cannot be created this way – for example, this “ wheel” layout 325 L0001 - 2010-11-27
  26. 26. Designing Perspectives An Example The parts of layout (the editor and the four views) are represented by the rectangles The numbers refers to the next start of the line to be executed Editor 3 Area 4 5 View1 View2 View4 View2 6 View2 View1 7 View1 8 View1 View3 View3 public void createInitialLayout(IPageLayout layout) { final String area = layout.getEditorArea(); layout.setEditorAreaVisible(false); layout.addView("com.rcpcompany.cex.view1", IPageLayout.LEFT, 1.0f, area); layout.addView("com.rcpcompany.cex.view2", IPageLayout.LEFT, 0.666f, "com.rcpcompany.cex.view1"); layout.addView("com.rcpcompany.cex.view3", IPageLayout.BOTTOM, 0.75f, "com.rcpcompany.cex.view2"); layout.addView("com.rcpcompany.cex.view4", IPageLayout.LEFT, 0.5f, "com.rcpcompany.cex.view2"); }26 L0001 - 2010-11-27
  27. 27. Designing Perspectives Views can be added to a designated spot in a number of ways  As a normal view with tab using addView(…)  As a view with no tab using addStandaloneView(…)  As a stack or folder using createFolder(…)  As a fast view using addFastView(…) Views can also be added as a placeholder for a future view using addPlaceholder(…) or createFolderPlaceholder(…) The editor area has ID org.eclipse.ui.editorss (with ss!) Views can be set un-closeable or un-moveable via getViewLayout(id) See IPageLayout for more information27 L0001 - 2010-11-27
  28. 28. Extending an Existing Perspective Used when extending an existing perspective with information from a new (usually independent) plug-in  E.g. adding a new profile view to the Java perspective Using extension point org.eclipse.ui.perspectiveExtensions  Specify the target perspective ID  Can add fast views, action set, shortcuts  Cannot hide or show the editor28 L0001 - 2010-11-27
  29. 29. Lab Exercise Implement the following perspective using four views via a Java class  You make the perspective switcher visible in the WorkbenchWindowAdvisor.preWindowOpen() (RCP Only): configurer.setShowPerspectiveBar(true) Then re-implement the layout in a new perspective using the perspectiveExtensions extension point  Or just add a new view to the old existing perspective Later possibly move view3 to a fast view Make view1 not moveable in both perspectives Add the new perspective as a shortcut in the old perspective – using perspectiveExtensions Stack with View2 and View 4 View1 View329 L0001 - 2010-11-27
  30. 30. Lab Exercise Implement the following perspective using four views Stack with View2 and View 4 Editor View1 Area View3 Add one of the above views to the existing Resource perspective using the perspectiveExtensions extension point  The ID of the Resource perspective is org.eclipse.ui.resourcePerspective  The ID of the Task view is org.eclipse.ui.views.TaskList30 L0001 - 2010-11-27
  31. 31. How to open editors, views and perspectives Perspectives  IWorkbenchWindow.openPage(String perspectiveId, IAdaptable input) Editors  IDE.openEditor(IWorkbenchPage page, IFile file)  One of a large number of methods  IWorkbenchPage.findEditor(IFile file) Views  IWorkbenchPage.showView(String id)  Will open the view in the site according to the perspective definition31 L0001 - 2010-11-27
  32. 32. More Information “Using Perspectives in the Eclipse UI”  http://www.eclipse.org/resources/resource.php?id=232 Visual Editor Project  http://www.eclipse.org/vep/WebContent/main.php Advanced workbench concepts from Eclipse Help  http://help.eclipse.org/help32/index.jsp?topic=/ org.eclipse.platform.doc.isv/guide/wrkAdv.htm “Code Access Guide to Eclipse 3.4”  http://www.eclipse.org/resources/resource.php?id=428  Among other things an introduction to the PDE Spy.32 L0001 - 2010-11-27