Results 1 to 3 of 3
  1. #1
    el shorty is offline Member
    Join Date
    Feb 2012
    Posts
    3
    Rep Power
    0

    Default Use EMF to load data from XML

    Hello everybody,

    I wasn't sure where to post this, but since it is the Eclipse modelling framework, I decided to put it here.
    I'm doing my internship at an IT-company where I have to learn to work with EMF. So far so good, but now I have to use EMF to save data to an xml file or to extract data from an xml and use this data.

    For example,
    I have a person, this person has a firstname and a lastname. When I create this person this data has to be saved to an xml file. Then at a later point I might need this person again and need to get his first and lastname from this xml-file.

    Anyone has an idea how to do this using EMF?

  2. #2
    el shorty is offline Member
    Join Date
    Feb 2012
    Posts
    3
    Rep Power
    0

    Default Re: Use EMF to load data from XML

    I have managed to bind my view to my model trough databdinging. The code is here:

    package smartapps.smartsignature.ui.personview;

    import org.eclipse.swt.SWT;

    public class NewView extends ViewPart {

    private Text personFirstName;
    private Text personLastName;
    private Text personTitle;
    private Text personMobile;
    private final FormToolkit formToolkit = new FormToolkit(Display.getDefault());

    public NewView() {
    // TODO Auto-generated constructor stub
    }

    @Override
    public void createPartControl(Composite parent) {
    parent.setLayout(new GridLayout(2, false));

    //Initialize the model
    SmartsignaturePackage.eINSTANCE.eClass();
    // Retrieve the default factory singleton
    SmartsignatureFactory factory = SmartsignatureFactory.eINSTANCE;

    final Person person = factory.createPerson();

    DataBindingContext bindingContext = new DataBindingContext();

    Section sctnCreateANew = formToolkit.createSection(parent, Section.TWISTIE | Section.TITLE_BAR);
    GridData gd_sctnCreateANew = new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1);
    gd_sctnCreateANew.widthHint = 585;
    sctnCreateANew.setLayoutData(gd_sctnCreateANew);
    formToolkit.paintBordersFor(sctnCreateANew);
    sctnCreateANew.setText("Create a new person");
    new Label(parent, SWT.NONE);

    Label lblName = new Label(parent, SWT.NONE);
    lblName.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
    lblName.setText("Firstname:");

    personFirstName = new Text(parent, SWT.BORDER);
    personFirstName.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));

    Label lblLastname = new Label(parent, SWT.NONE);
    lblLastname.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
    lblLastname.setText("Lastname:");

    personLastName = new Text(parent, SWT.BORDER);
    personLastName.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));

    Label lblTitle = new Label(parent, SWT.NONE);
    lblTitle.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
    lblTitle.setText("Title:");

    personTitle = new Text(parent, SWT.BORDER);
    personTitle.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));

    Label lblMobile = new Label(parent, SWT.NONE);
    lblMobile.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
    lblMobile.setText("Mobile:");

    personMobile = new Text(parent, SWT.BORDER);
    personMobile.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
    new Label(parent, SWT.NONE);

    bindingContext.bindValue(
    WidgetProperties.text(SWT.Modify).observe(personFi rstName),
    EMFProperties.value(SmartsignaturePackage.Literals .PERSON__FIRST_NAME)
    .observe(person));

    bindingContext.bindValue(
    WidgetProperties.text(SWT.Modify).observe(personLa stName),
    EMFProperties.value(SmartsignaturePackage.Literals .PERSON__LAST_NAME)
    .observe(person));

    bindingContext.bindValue(
    WidgetProperties.text(SWT.Modify).observe(personTi tle),
    EMFProperties.value(SmartsignaturePackage.Literals .PERSON__TITLE)
    .observe(person));

    bindingContext.bindValue(
    WidgetProperties.text(SWT.Modify).observe(personMo bile),
    EMFProperties.value(SmartsignaturePackage.Literals .PERSON__MOBILE_NR)
    .observe(person));

    Button btnSend = new Button(parent, SWT.NONE);
    btnSend.setText("Send");

    btnSend.addMouseListener(new MouseAdapter() {
    @Override
    public void mouseDown(MouseEvent e) {
    System.out.println(person.getFirstName());
    System.out.println(person.getLastName());
    System.out.println(person.getTitle());
    System.out.println(person.getMobileNr());
    }
    });
    }

    @Override
    public void setFocus() {

    }

    }
    As you can see are my input fields binded to my person. But my button just prints out the results, what I need to do is save the data, so when the next time that I open my application I can manage this data.

  3. #3
    el shorty is offline Member
    Join Date
    Feb 2012
    Posts
    3
    Rep Power
    0

    Default Re: Use EMF to load data from XML

    I managed to figure out how to save the data, now I have another problem.

    This code:

    Java Code:
    btnAddPerson.addMouseListener(new MouseAdapter() {
    			@Override
    			public void mouseDown(MouseEvent e) {				
    				System.out.println(person.getFirstName());
    				ResourceSet resourceSet = new ResourceSetImpl();
    				URI fileURI = URI.createFileURI(SmartSignResourceUtil.getPath(DataPart.Persons));
    				Resource resource = resourceSet.createResource(fileURI);
    				resource.getContents().add(person);
    				
    				try {
    					final Map<Object, Object> saveOptions = new HashMap<Object, Object>();
    					saveOptions.put(Resource.OPTION_SAVE_ONLY_IF_CHANGED, Resource.OPTION_SAVE_ONLY_IF_CHANGED_MEMORY_BUFFER);
    					saveOptions.put(XMLResource.OPTION_KEEP_DEFAULT_CONTENT, true);
    					
    					resource.save(saveOptions);
    				} catch (IOException e1) {
    					// TODO Auto-generated catch block
    					e1.printStackTrace();
    				}
    				setTextTextbox();
    			}
    		});
    But it only saves one person. If I add another it gets overwritten by this code. I really need some help here, does anybody have an idea?

Similar Threads

  1. jTree load data from DB *PLS HELP*
    By michalss in forum AWT / Swing
    Replies: 1
    Last Post: 04-14-2011, 05:28 PM
  2. Load data from file to ArrayList
    By humbug in forum New To Java
    Replies: 3
    Last Post: 01-05-2011, 12:53 PM
  3. Load file to data structure
    By TinoJF in forum New To Java
    Replies: 4
    Last Post: 03-31-2009, 04:16 PM
  4. Replies: 0
    Last Post: 12-04-2008, 06:39 PM
  5. Add data to combobox in page load
    By hussainzim in forum JavaServer Pages (JSP) and JSTL
    Replies: 5
    Last Post: 05-15-2008, 01:57 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •