我正在使用viewPart的saveState方法保存我的视图状态。以下是我在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)在我的fillTableRoWData()方法中,当两个条件都执行时,我所有的侦听器仅处于第二个条件。如何在两个if条件之间移动侦听器。

最佳答案

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

如果要创建视图Saveable,则使viewpart实现ISaveablePart

10-06 01:53