Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Store EMF Objects in PreferenceStore(Store EMF Objects in PreferenceStore)
Store EMF Objects in PreferenceStore [message #1009805] Fri, 15 February 2013 18:59 Go to next message
Andres Alvarez Mattos is currently offline Andres Alvarez MattosFriend
Messages: 12
Registered: December 2011
Location: Madrid
Junior Member
Hello everyone,

I´m working on a EMF/GMF based editor. Models created with this editor need to access and refer some common EMF Objects stored on a workspace context. Users should be able to edit and modify those EMF Objects trough the preferences page.

Anyone knows if there is an extension to the PreferencePage class (something like EMFPreferencePage) to allow users to create their own EMF based preferences pages to store and access a common EMF Model ?

thanks in advance !!
Re: Store EMF Objects in PreferenceStore [message #1010113 is a reply to message #1009805] Sat, 16 February 2013 15:26 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 26215
Registered: July 2009
Senior Member
Andres,

I only know about https://bugs.eclipse.org/bugs/show_bug.cgi?id=353370.

On 15/02/2013 7:59 PM, Andres Alvarez Mattos wrote:
> Hello everyone,
>
> I´m working on a EMF/GMF based editor. Models created with this editor
> need to access and refer some common EMF Objects stored on a workspace
> context. Users should be able to edit and modify those EMF Objects
> trough the preferences page.
>
> Anyone knows if there is an extension to the PreferencePage class
> (something like EMFPreferencePage) to allow users to create their own
> EMF based preferences pages to store and access a common EMF Model ?
>
> thanks in advance !!
Re: Store EMF Objects in PreferenceStore [message #1015235 is a reply to message #1010113] Wed, 27 February 2013 20:46 Go to previous message
Andres Alvarez Mattos is currently offline Andres Alvarez MattosFriend
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

Previous Topic:ecore XMLHandler barfing on transient and derived member - why?
Next Topic:CacheAdapter performance issue
Goto Forum:
  


Current Time: Sun Nov 23 11:21:14 GMT 2014

Powered by FUDForum. Page generated in 0.01951 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software