|
|
Re: Store EMF Objects in PreferenceStore [message #1015235 is a reply to message #1010113] |
Wed, 27 February 2013 20:46 |
Andres Alvarez Mattos Messages: 12 Registered: December 2011 Location: Madrid |
Junior Member |
|
|
Hi Ed, thank you for your reply. Bug 353370 refers to the creation of a Field editor to be used on the preferences pages to show a list o models in a preference page. (At least that´s what I understand reading the bug)
What I needed, was a way to read and edit an EMF model using a preference page.
This way models on my workspace could read and refer those preferences and benefit from all the EMF features.
Finally I came with this solution:
I created a class MyModelPreferenceStore to read/write my preferences model, from my preferences plugin .metadata folder.
Then I created MyPreferencePage class extending PreferencePage and overwriting the performOk/performCancel methods, to call the save/load method of the MyModelPreferenceStore class.
The fields of my preference page class will need to create en execute the appropriate EMF commands in order to modify the preference model instance.
I attached these to files if someone want to take a look,
Any improvement/suggestions are more than welcomed.
Thanks again.
public class MyPreferencesStore {
//Instance of my preferences model
private EMyModelPreferences fMyInstancePreferences;
//My Editing Domain
private TransactionalEditingDomain _editingDomain = MyUtils.getEditingDomain();
//URI where I will store my preferences
URI dbPreferencesURI = URI.createFileURI(MyModelPreferencesPlugin.getDefault().getStateLocation().append("/MyPreferences.xml").toOSString());
/**
* Reload preferences
* @throws IOException
*/
public void reload() throws IOException {
if(fMyInstancePreferences.eResource()!=null)
fMyInstancePreferences.eResource().unload();
load();
}
/**
* Loads the preferences.
*
* @throws IOException if loading fails.
*/
public void load() throws IOException {
ResourceSet resourceSet = _editingDomain.getResourceSet();
try
{
Resource resource = resourceSet.getResource(dbPreferencesURI, true);
if(resource!=null)
fDMPreferences = (EMyModelPreferences)EcoreUtil.getObjectByType(resource.getContents(),
EPreferencesPackage.Literals.MY_MODEL_PREFERENCES);
else
//Problems ? Load default preferences
loadDefaultPreferences();
}catch(Exception exc)
{
loadDefaultPreferences();
}
}
/**
* Create and initialize the preferences for the first time
* @throws IOException
*/
private void loadDefaultPreferences() throws IOException {
fMyInstancePreferences = EPreferencesFactory.eINSTANCE.createMyModelPreferences();
//INITIALIZE YOU PREFERENCES HERE
ResourceSet resourceSet = _editingDomain.getResourceSet();
//Create a new resource
final Resource newResource = resourceSet.createResource(dbPreferencesURI);
AbstractTransactionalCommand createNewPreferencesCommand = new AbstractTransactionalCommand(_editingDomain, "Create New ModelPreferences Command", Collections.EMPTY_LIST) //$NON-NLS-1$
{
@Override
protected CommandResult doExecuteWithResult(
IProgressMonitor monitor, IAdaptable info)
throws ExecutionException
{
newResource.getContents().add(fMyInstancePreferences);
try {
Map<Object, Object> options = new HashMap<Object, Object>();
options.put(XMLResource.OPTION_ENCODING, "UTF-8"); //$NON-NLS-1$
newResource.save(options);
} catch (IOException e) {
ModelPreferencesPlugin.log(e); //$NON-NLS-1$
}
return CommandResult.newOKCommandResult();
}
};
try {
OperationHistoryFactory.getOperationHistory().execute(createNewPreferencesCommand,
new NullProgressMonitor(), null);
} catch (ExecutionException e) {
ModelPreferencesPlugin.log(e); //$NON-NLS-1$
}
}
/**
* Handles an {@link IOException} thrown during reloading the preferences due to a preference
* store update. The default is to write to stderr.
*
* @param x the exception
* @since 3.2
*/
protected void handleException(IOException x) {
x.printStackTrace();
}
/**
* Saves the preferences.
*
* @throws IOException if the Templates cannot be written
*/
public void save() throws IOException {
Map<Object, Object> options = new HashMap<Object, Object>();
options.put(XMLResource.OPTION_ENCODING, "UTF-8"); //$NON-NLS-1$
fMyInstancePreferences.eResource().save(options);
}
/**
*
*
* @param doSave <code>true</code> if the store should be saved after restoring
* @since 3.5
*/
public void restoreDefaults(boolean doSave) {
//TODO reload default preferences here
}
/**
* Deletes all user-added Templates and reverts all contributed Templates.
* <p>
* <strong>Note:</strong> the store will be saved after restoring.
* </p>
*/
public void restoreDefaults() {
restoreDefaults(true);
}
/**
* Returns all enabled Templates.
*
* @return all enabled Templates
*/
public EMyModelPreferences getPreferences() {
return fMyInstancePreferences;
}
}
public class MyPreferencesPage extends PreferencePage implements IWorkbenchPreferencePage{
/**
* Store for My Model Preferences
*/
MyPreferencesStore myPreferencesStore;
public MyPreferencesPage() {
super();
myPreferencesStore = new MyPreferencesStore(); //MyPreferencesStore should be a Singleton
}
@Override
protected Control createContents(Composite ancestor) {
//Create Your page fields here
}
@Override
public boolean performOk() {
try {
myPreferencesStore.save();
} catch (IOException e) {
openWriteErrorDialog(e);
}
return super.performOk();
}
/*
* @see PreferencePage#performCancel()
*/
public boolean performCancel() {
try {
myPreferencesStore.reload();
} catch (IOException e) {
openReadErrorDialog(e);
return false;
}
return super.performCancel();
}
//This is the object, this Page will edit:
= protected EMyModelPreferences getPreferences()
{
return myPreferencesStore.getPreferences();
}
}
[Updated on: Wed, 27 February 2013 21:06] Report message to a moderator
|
|
|
Powered by
FUDForum. Page generated in 0.03199 seconds