Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between pages "FAQ How does a view persist its state between sessions?" and "Eclipse Project"

(Difference between pages)
 
 
Line 1: Line 1:
Storing view state is done in two commons ways, depending
+
The unfortunately named "Eclipse Project" is the project dedicated to producing the Eclipse SDK. This name made sense back when there were only two or three projects at Eclipse, but now it is frequently referred to as the "Eclipse SDK Project" to reduce confusion (or just "The Platform" when we're feeling grandiose)This project in turn is composed of four sub-projects: [http://eclipse.org/equinox Equinox], [http://eclipse.org/platform Platform], [http://eclipse.org/jdt Java development tools] (JDT), and [http://eclipse.org/pde Plug-in Development Environment] (PDE).
on whether you want to store settings between
+
workbench sessions or across invocations of your viewThe first of these
+
facilities is found directly on <tt>IViewPart</tt>. When the workbench
+
is shut down, the method <tt>saveState</tt> is called on all open views.
+
The parameter to this method is an <tt>IMemento</tt>, a simple
+
data structure that stores hierarchies of nodes containing numbers
+
and strings.  Here is an example from the recipe application in the FAQ
+
examples, where a view is persisting the current selection of a list viewer
+
in a memento:
+
<pre>
+
  private static final String STORE_SELECTION =
+
      "ShoppingList.SELECTION";
+
  public void saveState(IMemento memento) {
+
      super.saveState(memento);
+
      ISelection sel = viewer.getSelection();
+
      IStructuredSelection ss = (IStructuredSelection) sel;
+
      StringBuffer buf = new StringBuffer();
+
      for (Iterator it = ss.iterator(); it.hasNext();) {
+
        buf.append(it.next());
+
        buf.append(',');
+
      }
+
      memento.putString(STORE_SELECTION, buf.toString());
+
  }
+
</pre>
+
  
 +
== Sub-project pages ==
  
When the workbench is reopened, the method
+
* [[Platform]]
<tt>init(IViewSite, IMemento)</tt> is called the first time each view
+
* [[Equinox]]
becomes visible.  The <tt>IMemento</tt> will contain all the information
+
* [[JDT|JDT]]
that was added to it when the workbench was shut down.  Note that
+
<tt>init</tt> is called before the <tt>createPartControl</tt> method,
+
so you will not be able to restore widget state directly from the
+
<tt>init</tt> method.  You can store the <tt>IMemento</tt> instance in a field
+
and restore state later on when your widgets have been created.  Continuing
+
this example, here is the code for restoring the viewer selection
+
when the view is reopened:
+
<pre>
+
  private IMemento memento;
+
  ...
+
  public void init(IViewSite site, IMemento memento)
+
      throws PartInitException {
+
      super.init(site, memento);
+
      this.memento = memento;
+
  }
+
  public void createPartControl(Composite parent) {
+
      //create widgets ...
+
      if (memento == null) return;
+
      String value = memento.getString(STORE_SELECTION);
+
      if (value == null) return;
+
      IStructuredSelection ss = new StructuredSelection(
+
        value.split(","));
+
      viewer.setSelection(ss);
+
  }
+
</pre>
+
Note that the <tt>IMemento</tt> instance can be <tt>null</tt> if the view state
+
was not saved from a previous session: for example, when the view
+
is first created.
+
  
 +
== Topic hubs ==
  
Another mechanism for persisting view state is the JFace
+
* [[Automated Testing]]
<tt>IDialogSettings</tt> facility.  The advantage of dialog settings
+
* [[Eclipse Documentation | Documentation]]
over the view <tt>save</tt>/<tt>init</tt> mechanism is that you can control when
+
* [[Internationalization]]
settings are persisted.  The <tt>saveState</tt> method is called only
+
* [[API Central]]
if your view is open when the workbench shuts down, so
+
* [[Polish3.2|Polish]] item list for 3.2
it is not useful for storing view state when the view is closed by
+
the user.  Dialog settings, on the other hand, can be changed
+
and persisted whenever you want.
+
 
+
 
+
Views commonly use a combination of both dialog settings and
+
a memento for persisting view state.  Important settings, such as
+
filters, sorters, and other view preferences, will be stored as
+
dialog settings; more transient attributes, such as selection
+
and expansion state, will be stored in the memento only when
+
the workbench is being shut down.
+
 
+
 
+
== See Also: ==
+
 
+
[[FAQ_How_do_I_save_settings_for_a_dialog_or_a_wizard%3F]]
+
 
+
<hr><font size=-2>This FAQ was originally published in [http://www.eclipsefaq.org Official Eclipse 3.0 FAQs]. Copyright 2004, Pearson Education, Inc. All rights reserved. This text is made available here under the terms of the [http://www.eclipse.org/legal/epl-v10.html Eclipse Public License v1.0].</font>
+

Revision as of 10:51, 26 April 2006

The unfortunately named "Eclipse Project" is the project dedicated to producing the Eclipse SDK. This name made sense back when there were only two or three projects at Eclipse, but now it is frequently referred to as the "Eclipse SDK Project" to reduce confusion (or just "The Platform" when we're feeling grandiose). This project in turn is composed of four sub-projects: Equinox, Platform, Java development tools (JDT), and Plug-in Development Environment (PDE).

Sub-project pages

Topic hubs

Back to the top