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 revisions of "FAQ How do I save settings for a dialog or a wizard?"

m
m
 
Line 1: Line 1:
The <tt>IDialogSettings</tt> mechanism can be used to store any
+
#REDIRECT [[FAQ How do I save settings for a dialog or wizard?]]
hierarchical collection of strings and numbers.  It is used in Eclipse to
+
persist preferences and history for most views, dialogs, and wizards.
+
For example, dialogs will often store the last few values entered in
+
each entry field and allow the user to select from these previous values,
+
using a combo box. Views will use dialog settings to store filtering, sorting,
+
and layout preferences.  In all these cases, the basic steps involved
+
are the same.
+
 
+
First, you need to find or create an instance of <tt>IDialogSettings</tt>.  If your plug-in is a subclass of
+
<tt>AbstractUIPlugin</tt>, your plug-in already has a global setting object.  Simply call the method
+
<tt>getDialogSettings</tt> on your plug-in to obtain an instance.
+
If you&#146;re not using <tt>AbstractUIPlugin</tt>, you can
+
create an instance of <tt>DialogSettings</tt> yourself.
+
 
+
Once you have a settings instance, you simply store the data
+
values you want to keep on the settings object.  If you&#146;re using
+
a global settings object, you can add subsections, using
+
<tt>addNewSection</tt>.  Typically, each dialog, wizard page,
+
and view will have its own settings section within the global settings
+
instance.
+
 
+
Finally, the settings must be saved to disk at some point.  If you&#146;re
+
using the <tt>AbstractUIPlugin</tt> settings instance, it is saved automatically when the plug-in is shut down.  If you created
+
the settings yourself, you have to save the settings manually when you&#146;re
+
finished changing them by calling the <tt>save</tt> method.  Note
+
that each plug-in has a state location allotted to it, and you can
+
find out the location by calling <tt>getStateLocation</tt> on
+
your plug-in instance.  The following example loads a settings file from disk,
+
changes some values, and then saves it again:
+
<pre>
+
  IPath path = ExamplesPlugin.getDefault().getStateLocation();
+
  String filename = path.append("settings.txt").toOSString();
+
  DialogSettings settings = new DialogSettings("Top");
+
  settings.load(filename);
+
  settings.put("Name", "Joe");
+
  settings.put("Age", 35);
+
  settings.save(filename);
+
</pre>
+
 
+
== See Also: ==
+
[[FAQ_How_does_a_view_persist_its_state_between_sessions%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>
+

Latest revision as of 00:40, 9 June 2006

Back to the top