Search code examples
eclipse-pluginhashmapeclipse-rcpsavestatememento

How to store all the information of view part using IMemento?


I have eclipse plugin that uses a view which extends ViewPart. ViewPart has a saveState Method which requires an IMemento. I added my code to saveState and the corresponding init-Method and it works.

I have created 3 hashmap.

1) hmTextONLY: its contains only text value.(Column Name(ColumnIndex):Threarname(1),categoryname(2),description(5),justification(6))

2) hmCOMBO1ONLY: its contains only combobox1 value.(Column Name(Column Index): status(3))

3) hmCOMBO2ONLY: its contains only combobox2 value.(Column Name(Column Index):Priority(4))

Method description here, which is used in the code.

init: Intialize the view.

createPartControl: create table in the view part.

fillRows: data in the table, comes from this method. row by row. saveState: this method is useful to save data.saveState is only called if the entire workspace is shutting down.

saveState: This is used for the saving a workspace when close the application.

Questions: 1) How to save whole table data sequential(Mixing of text ,combobox) using save state method? I have created one child form Id1 in saveState method.

Code:

public class Theartview extends ViewPart implements Serializable {

Table table;
private TableViewer tableViewer;
TableViewerColumn tableViewerColumn;
TableColumnLayout tableColumnLayout;
private HashMap<Integer, String> hmTextONLY = new HashMap<>();
private HashMap<Integer, String> hmCOMBO1ONLY = new HashMap<>();
private HashMap<Integer, String> hmCOMBO2ONLY = new HashMap<>();
private static Integer hmT = 0, hmC1 = 0, hmC2 = 0;
private IMemento memento;
private Text text_1, text_2, text_3, text_4;

public void createPartControl(Composite parent) {

    Composite tableComposite = new Composite(parent, SWT.NONE);
    tableColumnLayout = new TableColumnLayout();
    tableComposite.setLayout(tableColumnLayout);
    tableComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
            true));

    tableViewer = new TableViewer(tableComposite, SWT.MULTI | SWT.H_SCROLL
            | SWT.V_SCROLL);
    tableViewer.setContentProvider(ArrayContentProvider.getInstance());
    // TODO viewer.setLabelProvider(new ViewLabelProvider());
    table = tableViewer.getTable();
    table.setHeaderVisible(true);
    table.setLinesVisible(true);

    String[] titles = { "Threat Name", "Category Name", "Status",
            "Priority", "Description", "Justification" };

    for (int loopIndex = 0; loopIndex < titles.length; loopIndex++) {
        tableViewerColumn = new TableViewerColumn(tableViewer, SWT.NONE);
        TableColumn tblclmn = tableViewerColumn.getColumn();
        tableColumnLayout.setColumnData(tblclmn, new ColumnPixelData(200,
                true, true));
        tblclmn.setText(titles[loopIndex]);
    }

    if (memento != null) {
        System.out.println("Entering Restore State");
        restoreState(memento);
    }

    memento = null;

  }

   private void fillRows(String shortdesc, String categ, String descp) {
       TableItem ramtableitem = new TableItem(table, SWT.NONE);

       // for Threat_Name
       TableEditor editorTN = new TableEditor(table);
       text_1 = new Text(table, SWT.NONE);
       editorTN.grabHorizontal = true;
       editorTN.setEditor(text_1, ramtableitem, 0);
       text_1.setText(shortdesc);
       Theart_Name = text_1.getText();
       hmTextONLY.put(hmT++, Theart_Name);

       // For Category_Name
        TableEditor editorCN = new TableEditor(table);
        text_2 = new Text(table, SWT.NONE);
        editorCN.grabHorizontal = true;
        editorCN.setEditor(text_2, ramtableitem, 1);
        text_2.setText(categ);
        Category_Name = text_2.getText();
        hmTextONLY.put(hmT++, Category_Name);

         String items[] = { "Mitigated", "Not Applicable", "Not Started",
            "Needs Investigation" };
        Arrays.sort(items);

        final CCombo Status_Combo = new CCombo(table, SWT.NONE);
        Status_Combo.setItems(items);
        TableEditor editor = new TableEditor(table);
        editor.grabHorizontal = true;
        editor.setEditor(Status_Combo, ramtableitem, 2);

        Status_Combo.addSelectionListener(new SelectionListener() {
          public void widgetSelected(SelectionEvent e) {
             Status_Name = Status_Combo.getText();
            hmCOMBO1ONLY.put(hmC1, Status_Name);
         }

        public void widgetDefaultSelected(SelectionEvent e) {               
            Status_Name = Status_Combo.getText();               
            hmCOMBO1ONLY.put(hmC1, Status_Name);
            }
        });


       // For Priority_Name
    String itemss[] = { "High", "Medium", "Low" };
    Arrays.sort(itemss);
    final CCombo Priority_Combo = new CCombo(table, SWT.NONE);
    Priority_Combo.setItems(itemss);
    TableEditor editorP = new TableEditor(table);
    editorP.grabHorizontal = true;
    editorP.setEditor(Priority_Combo, ramtableitem, 3);

    Priority_Combo.addSelectionListener(new SelectionListener() {
        public void widgetSelected(SelectionEvent e) {
            System.out.println(Priority_Combo.getText());
            Priority_Name = Priority_Combo.getText();
            hmCOMBO2ONLY.put(hmC2, Priority_Name);
        }

        public void widgetDefaultSelected(SelectionEvent e) {
            System.out.println(Priority_Combo.getText());
            Priority_Name = Priority_Combo.getText();
            hmCOMBO2ONLY.put(hmC2, Priority_Name);
        }
    });
      // For Descrption_Name
        TableEditor editorDN = new TableEditor(table);
        text_3 = new Text(table, SWT.NONE);
        editorDN.grabHorizontal = true;
        editorDN.setEditor(text_3, ramtableitem, 4);
        text_3.setText(descp);
        Descrption_Name = text_3.getText();
        hmTextONLY.put(hmT++, Descrption_Name);

        // For justification
        TableEditor editorJ = new TableEditor(table);
        text_4 = new Text(table, SWT.MULTI | SWT.BORDER | SWT.WRAP
            | SWT.V_SCROLL);
         editorJ.grabHorizontal = true;
        editorJ.setEditor(text_4, ramtableitem, 5);
        Justification_Name = text_4.getText().toString().trim();
        hmTextONLY.put(hmT++, Justification_Name);
   }

public void saveState(IMemento memento) {
     super.saveState(memento);
    for (int s = 0; s <= hmTextONLY.size(); s++) {
    for (int su = 0; su <= hmCOMBO1ONLY.size(); su++) {
     for (int sum = 0; sum <= hmCOMBO2ONLY.size(); sum++) {
                IMemento mem = memento.createChild(ID1 + "s");
                mem.putString(ID1 + "s", hmTextONLY.get(s));
             }
        }
     }


public void init(IViewSite site, IMemento memento) throws PartInitException{
     super.init(site, memento);
    this.memento = memento;
    System.out.println("Intialize the view");

 }


 }

}

Current OutPut of above code:


Solution

  • IMemento is able to hold arbitrary state as long as it can be serialized into strings and primitives. You need to write the code that translates view state (e.g. column widths, selection, etc) to and from a given IMemento instance.

    In order to structure the data to be stored, mementos can also be nested. Use createChild(), to create a sub-memento.

    A view has lifecycle methods, namely init() and saveState() that are called when

    • a view is initialized and state from a memento should be applied to the view
    • a view is about to be closed and its state should be stored in a memento.

    This wiki page provides further details and also lists an alternative to using mementos: https://wiki.eclipse.org/FAQ_How_does_a_view_persist_its_state_between_sessions%3F