Difference between revisions of "FAQ How does a view persist its state between sessions?"

From Eclipsepedia

Jump to: navigation, search
 
m
Line 9: Line 9:
 
examples, where a view is persisting the current selection of a list viewer
 
examples, where a view is persisting the current selection of a list viewer
 
in a memento:
 
in a memento:
 +
 
<pre>
 
<pre>
   private static final String STORE_SELECTION =  
+
   private static final String STORE_SELECTION = "ShoppingList.SELECTION";
      "ShoppingList.SELECTION";
+
 
   public void saveState(IMemento memento) {
 
   public void saveState(IMemento memento) {
 
       super.saveState(memento);
 
       super.saveState(memento);
Line 24: Line 24:
 
   }
 
   }
 
</pre>
 
</pre>
 
  
 
When the workbench is reopened, the method  
 
When the workbench is reopened, the method  
Line 36: Line 35:
 
this example, here is the code for restoring the viewer selection
 
this example, here is the code for restoring the viewer selection
 
when the view is reopened:
 
when the view is reopened:
 +
 
<pre>
 
<pre>
 
   private IMemento memento;
 
   private IMemento memento;
Line 54: Line 54:
 
   }
 
   }
 
</pre>
 
</pre>
 +
 
Note that the <tt>IMemento</tt> instance can be <tt>null</tt> if the view state
 
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
 
was not saved from a previous session: for example, when the view
 
is first created.
 
is first created.
 
  
 
Another mechanism for persisting view state is the JFace
 
Another mechanism for persisting view state is the JFace
Line 67: Line 67:
 
the user.  Dialog settings, on the other hand, can be changed  
 
the user.  Dialog settings, on the other hand, can be changed  
 
and persisted whenever you want.
 
and persisted whenever you want.
 
  
 
Views commonly use a combination of both dialog settings and
 
Views commonly use a combination of both dialog settings and
Line 75: Line 74:
 
and expansion state, will be stored in the memento only when
 
and expansion state, will be stored in the memento only when
 
the workbench is being shut down.
 
the workbench is being shut down.
 
  
 
== See Also: ==
 
== See Also: ==
 
+
*[[FAQ_How_do_I_save_settings_for_a_dialog_or_a_wizard%3F]]
[[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>
 
<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 22:43, 29 May 2006

Storing view state is done in two commons ways, depending on whether you want to store settings between workbench sessions or across invocations of your view. The first of these facilities is found directly on IViewPart. When the workbench is shut down, the method saveState is called on all open views. The parameter to this method is an IMemento, 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:

   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());
   }

When the workbench is reopened, the method init(IViewSite, IMemento) is called the first time each view becomes visible. The IMemento will contain all the information that was added to it when the workbench was shut down. Note that init is called before the createPartControl method, so you will not be able to restore widget state directly from the init method. You can store the IMemento 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:

   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);
   }

Note that the IMemento instance can be null if the view state was not saved from a previous session: for example, when the view is first created.

Another mechanism for persisting view state is the JFace IDialogSettings facility. The advantage of dialog settings over the view save/init mechanism is that you can control when settings are persisted. The saveState method is called only if your view is open when the workbench shuts down, so 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:


This FAQ was originally published in Official Eclipse 3.0 FAQs. Copyright 2004, Pearson Education, Inc. All rights reserved. This text is made available here under the terms of the Eclipse Public License v1.0.