2016-09-27 59 views
0

我有eclipse插件,它使用了一个扩展ViewPart的视图。 ViewPart有一个需要IMemento的saveState方法。我将我的代码添加到saveState和相应的init-Method,并且它可以工作。如何使用IMemento存储视图部分的所有信息?

我已经创建了3个hashmap。

1)hmTextONLY:其只包含文本值(列名(ColumnIndex):Threarname(1)中,类别名(2),描述(5),理由(6))

2) hmCOMBO1ONLY:其只含有combobox1值(列名(列指数):状态(3))。

3)hmCOMBO2ONLY:其只含有combobox2值(列名(列指数):优先级(4) )

方法描述这里,在代码中使用。

init:初始化视图。

createPartControl:在视图部分中创建表。

fillRows:表中的数据来自此方法。逐行。saveState:此方法对于保存data.saveState非常有用,只有在整个工作空间关闭时才会调用该方法。

saveState:这用于关闭应用程序时保存工作区。

问题: 1)如何保存整个表数据的顺序使用保存状态的方法(混合文本,组合框)?我在saveState方法中创建了一个子表单Id1。

代码:

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"); 

} 


} 

} 

电流上面的代码输出:

+0

这是过于广泛。你需要告诉我们你已经做了一些研究,使用'IMemento'和你卡住的地方。 –

+0

@ greg-449:是否可以使用IMemento保存所有零件? –

+0

如果你足够努力,那么你可以节省几乎所有的东西。 –

回答

0

IMemento可以,只要它可以被序列化到字符串和原语持有任意状态。您需要编写翻译视图状态的代码(例如列宽,选择等)到给定的IMemento实例。

为了构造要存储的数据,纪念品也可以嵌套。使用createChild(),创建子纪念品。

甲视图具有生命周期方法,即init()saveState()

  • 一个视图被初始化,并从纪念品状态应该被应用于视图
  • 的图将要被关闭,并且被称为其状态应该存储在纪念品中。

本wiki页面提供进一步的细节,还列出了使用纪念品替代:https://wiki.eclipse.org/FAQ_How_does_a_view_persist_its_state_between_sessions%3F

+0

@RüdigerHerrmann非常感谢你。 –

+0

@RüdigerHerrmann:我修改了代码,更具体地描述了我现在面临的实际问题。请参阅上面的代码。 –

+0

你没有说明你面对的确切问题是什么。 –