在 RCP 应用程序中保存存储在 viewpart 中的数据

Saving Data Stored In viewpart in RCP application

我正在使用 viewPart 的 saveState 方法来保存我的视图 state.Below 是我在 viewPart 中保存数据的代码。

package view;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.TableEditor;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IMemento;
import org.eclipse.ui.IViewSite;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.part.ViewPart;

import tutogef.model.Connection;
import tutogef.model.Node;
import tutogef.model.Service;
import tutogef.xml.ThreatTypeXMLToObject;

public class Theartview extends ViewPart implements Serializable {

    private static final long serialVersionUID = -3033215443267698138L;
    public static String ID = "TutoGEF.theartview_id";
    public static String ID1 = "TutoGEF.theartview_id1";
    private Table table;
    private String Theart_Name;
    private String Category_Name;
    private String Status_Name;
    private String Priority_Name;
    private String Descrption_Name;
    private String Justification_Name;
    private static Node sourceNode;
    private static Node targetNode;
    private static String connectionType;
    private String shortDescription;
    private String category;
    private String description;
    private ThreatTypeXMLToObject threattypexmltoobject;
    private Connection conn;
    private Text text_1, text_2, text_3, text_4;
    private ArrayList<String> list1 = new ArrayList<>();
    private HashMap<Integer, String> list2 = new HashMap<>();
    // HashMap<Integer, String> list3 = new HashMap<>();
    private static Integer a = 0;
    private IMemento memento;
//  String Key = "Key";
//  String Key1 = "Key1";
//  String Key2 = "Key2";
//  String Key3 = "Key3";
//  String Key4 = "Key4";
//  String Key5 = "Key5";
//  String Key6 = "Key6";
    String[] keyValues = { "KeyValue1", "KeyValue2", "KeyValue3", "KeyValue4",
            "KeyValue5", "KeyValue6" };

    @SuppressWarnings("static-access")
    public void getDataOfConnection(Node sourceNode, Node targetNode,
            String connectionType1) {
        this.sourceNode = sourceNode;
        this.targetNode = targetNode;
        this.connectionType = connectionType1;

    }

    public Theartview() {
    }

    @Override
    public void createPartControl(Composite parent) {

        parent.setLayout(new GridLayout(10, false));

        table = new Table(parent, SWT.BORDER | SWT.FULL_SELECTION);
        table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 10, 1));
        table.setHeaderVisible(true);
        table.setLinesVisible(true);

        String[] titles = { "Theart Name", "Category", "Satus", "Priority",
                "Description", "Justification" };
        for (int i = 0; i < titles.length; i++) {
            TableColumn column = new TableColumn(table, SWT.NONE);
            column.setWidth(150);
            column.setText(titles[i]);
        }

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

    void restoreState(IMemento memento) {
        System.out.println("Restore State Entered");

        IMemento[] mems = memento.getChildren(ID1);
        System.out.println(mems);
        for (int q = 0; q < mems.length; q+=3) {
            System.out.println("Enter: -----------------------------");
             IMemento mem = mems[q];

             System.out.println(mems.length);
             System.out.println(q);
             System.out.println(mem);
            TableItem item = new TableItem(table, SWT.NONE);

            // for Theart_Name
            TableEditor editor = new TableEditor(table);

            Text text = new Text(table, SWT.NONE);
            editor.grabHorizontal = true;
            editor.setEditor(text, item, 0);
            text.setText(mem.getString(keyValues[q]));
            Theart_Name = text.getText().toString().trim();

            // For Category_Name
            editor = new TableEditor(table);
            text = new Text(table, SWT.NONE);
            editor.grabHorizontal = true;
            editor.setEditor(text, item, 1);
            text.setText(mem.getString(keyValues[q + 1]));
            Category_Name = text.getText().toString().trim();

            // For Status_Name
            editor = new TableEditor(table);
            final Combo Status_Combo = new Combo(table, SWT.READ_ONLY);
            Status_Combo.add("Mitigated");
            Status_Combo.add("Not Applicable");
            Status_Combo.add("Not Started");
            Status_Combo.add("Needs Investigation");
            editor.grabHorizontal = true;
            editor.setEditor(Status_Combo, item, 2);

            Status_Combo.addSelectionListener(new SelectionListener() {
                public void widgetSelected(SelectionEvent e) {
                    System.out.println(Status_Combo.getText());
                    Status_Name = Status_Combo.getText();
                }

                public void widgetDefaultSelected(SelectionEvent e) {
                    System.out.println(Status_Combo.getText());
                    Status_Name = Status_Combo.getText();
                }
            });

            // For Priority_Name
            editor = new TableEditor(table);
            final Combo priority_Combo = new Combo(table, SWT.NONE);
            priority_Combo.add("High");
            priority_Combo.add("Medium");
            priority_Combo.add("Low");
            editor.grabHorizontal = true;
            editor.setEditor(priority_Combo, item, 3);

            priority_Combo.addSelectionListener(new SelectionListener() {
                public void widgetSelected(SelectionEvent e) {
                    System.out.println(priority_Combo.getText());
                    Priority_Name = priority_Combo.getText();
                }

                public void widgetDefaultSelected(SelectionEvent e) {
                    System.out.println(priority_Combo.getText());
                    Priority_Name = priority_Combo.getText();
                }
            });

            // For Descrption_Name
            editor = new TableEditor(table);
            text = new Text(table, SWT.NONE);
            editor.grabHorizontal = true;
            editor.setEditor(text, item, 4);
            text.setText(mem.getString(keyValues[q + 2]));
            Descrption_Name = text.getText().toString().trim();

            // For justification
            editor = new TableEditor(table);
            text = new Text(table, SWT.MULTI | SWT.BORDER | SWT.WRAP
                    | SWT.V_SCROLL);
            editor.grabHorizontal = true;
            editor.setEditor(text, item, 5);
            Justification_Name = text.getText().toString().trim();
        }
    }

    @SuppressWarnings("static-access")
    public void fillTableRoWData() {
        if (Connection.Number_Of_Connection != 0) {
            // item = new TableItem(table, SWT.NONE);
            if (Service.class.isInstance(sourceNode)) {
                String id = "S1";
                shortDescription = threattypexmltoobject.shortdescription(id,
                        sourceNode.getName(), targetNode.getName(), null);

                category = "Spoofing";
                description = threattypexmltoobject.longdescription(id,
                        sourceNode.getName(), targetNode.getName(), null);

                fillRows(shortDescription, category, description);
            }

            if (Service.class.isInstance(sourceNode)
                    && (connectionType == Connection.CONNECTION_DESIGN)) {
                String id = "T1";

                System.out.println(conn.getConnectionDesign());

                shortDescription = threattypexmltoobject.shortdescription(id,
                        sourceNode.getName(), targetNode.getName(),
                        conn.getConnectionDesign());
                category = "Tampering";
                description = threattypexmltoobject.longdescription(id,
                        sourceNode.getName(), targetNode.getName(),
                        conn.getConnectionDesign());
                fillRows(shortDescription, category, description);
            }
        }
        System.out.println("Hash Map" + list2);
        System.out.println("List : []" + list1);
    }

    @Override
    public void setFocus() {

    }

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

        // for Threat_Name
        TableEditor editor = new TableEditor(table);

        text_1 = new Text(table, SWT.NONE);
        editor.grabHorizontal = true;
        editor.setEditor(text_1, item, 0);
        text_1.setText(shortdesc);

        text_1.addModifyListener(new ModifyListener() {

            @Override
            public void modifyText(ModifyEvent e) {
                System.out.println("Modify Text");
                Text text = (Text) e.widget;
                System.out.println(text.getText());
            }
        });
        Theart_Name = text_1.getText().toString().trim();
        list1.add(Theart_Name);
        list2.put(a, Theart_Name);

        // For Category_Name
        editor = new TableEditor(table);
        text_2 = new Text(table, SWT.NONE);
        editor.grabHorizontal = true;
        editor.setEditor(text_2, item, 1);
        text_2.setText(categ);
        Category_Name = text_2.getText().toString().trim();
        list1.add(Category_Name);
        list2.put(++a, Category_Name);

        // For Status_Name
        editor = new TableEditor(table);
        final Combo Status_Combo = new Combo(table, SWT.READ_ONLY);
        Status_Combo.add("Mitigated");
        Status_Combo.add("Not Applicable");
        Status_Combo.add("Not Started");
        Status_Combo.add("Needs Investigation");
        editor.grabHorizontal = true;
        editor.setEditor(Status_Combo, item, 2);

        Status_Combo.addSelectionListener(new SelectionListener() {
            public void widgetSelected(SelectionEvent e) {
                System.out.println(Status_Combo.getText());
                Status_Name = Status_Combo.getText();
                list1.add(Status_Name);
                list2.put(++a, Status_Name);
            }

            public void widgetDefaultSelected(SelectionEvent e) {
                System.out.println(Status_Combo.getText());
                Status_Name = Status_Combo.getText();
            }
        });

        // For Priority_Name
        editor = new TableEditor(table);
        final Combo priority_Combo = new Combo(table, SWT.NONE);
        priority_Combo.add("High");
        priority_Combo.add("Medium");
        priority_Combo.add("Low");
        editor.grabHorizontal = true;
        editor.setEditor(priority_Combo, item, 3);

        priority_Combo.addSelectionListener(new SelectionListener() {
            public void widgetSelected(SelectionEvent e) {
                System.out.println(priority_Combo.getText());
                Priority_Name = priority_Combo.getText();
                list1.add(Priority_Name);
                list2.put(++a, Priority_Name);
            }

            public void widgetDefaultSelected(SelectionEvent e) {
                System.out.println(priority_Combo.getText());
                Priority_Name = priority_Combo.getText();
            }
        });

        // For Descrption_Name
        editor = new TableEditor(table);
        text_3 = new Text(table, SWT.NONE);
        editor.grabHorizontal = true;
        editor.setEditor(text_3, item, 4);
        text_3.setText(descp);
        Descrption_Name = text_3.getText().toString().trim();
        list1.add(Descrption_Name);
        list2.put(++a, Descrption_Name);

        // For justification
        editor = new TableEditor(table);
        text_4 = new Text(table, SWT.MULTI | SWT.BORDER | SWT.WRAP
                | SWT.V_SCROLL);
        editor.grabHorizontal = true;
        editor.setEditor(text_4, item, 5);
        Justification_Name = text_4.getText().toString().trim();
        list1.add(Justification_Name);
        list2.put(++a, Justification_Name);

    }

    @Override
    public void saveState(IMemento memento) {
        super.saveState(memento);
        System.out.println("Save State Called");

        IMemento mem = memento.createChild(ID1);
        for (int i = 0; i < 6; i++) {
            mem.putString(keyValues[i], list2.get(i));
            System.out.println("Hash Map Values: [" + i + "] " + list2.get(i));
            System.out.println(mem);
        }
    }

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

    }
}

出现以下问题

1) 当第一次初始化 memento 时,代码可以正常工作。但是,当我在不更改任何数据的情况下再次打开我的 viewPart 时,会发生错误。

2) 我已将所有值存储在哈希图中。当我恢复数据时,我只得到前 3 个值,其余值不会出现。如果我使用数字,那么所有数据都会正确显示。有什么解决方案吗?

3) 在我的 fillTableRoWData() 方法中,当执行两个 if 条件时,我的所有听众仅站在第二个条件下。如何在两个 if 条件之间移动侦听器。

使用 org.eclipse.ui.elementFactories 扩展点并实现 IElementFactory 接口的 createElement 方法,它接受一个 IMemento 对象。

如果你想制作视图 Saveable 然后制作视图部分 ISaveablePart