2016-08-04 11 views
0

私はviewPartのsaveStateメソッドを使用して、ビューの状態を保存しています.Belowは、ビューポートにデータを保存するためのコードです。RCPアプリケーションで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を発生します。しかし、データを変更せずにviewPartを再度開くと、エラーが発生します。

2)私はすべての値をハッシュマップに保存しました。データを復元すると、最初の3つの値しか得られず、残りの値は表示されません。私がすべてのデータよりも数字を使うと、適切にデータが得られます。それに対する解決策はありますか?

3)私のfillTableRoWData()メソッドでは、2つのif条件が実行されたときに、すべてのリスナーが2番目の条件にのみ当てはまります。条件の2つの間でリスナーを移動する方法。

+0

viewpartを実装します。まさにあなたは何を保存したいですか? –

+0

@ greg-449私は編集を行いました。私は、getTableメソッドが呼び出されたときに表示されているデータを保存します。 savestateメソッドを使用してどのように保存できますか?私は私のviewpartに保存されているデータを取得していません。 –

+0

あなたのコードは 'saveState'メソッドで保存していますか?このコードを書くのはあなた次第です。 –

答えて

0

org.eclipse.ui.elementFactories拡張ポイントを使用し、IMementoオブジェクトを取るIElementFactoryインターフェイスのcreateElementメソッドを実装します。

ビューSaveableを作りたい場合は、あまりにも多くのコードがここにありますISaveablePart

関連する問題