2016-09-27 10 views
0

ViewPart를 확장 한 뷰를 사용하는 eclipse 플러그인이 있습니다. ViewPart에는 IMemento가 필요한 saveState 메소드가 있습니다. saveState 및 해당 init-Method에 코드를 추가하면 작동합니다.IMemento를 사용하여 뷰 파트의 모든 정보를 저장하는 방법은 무엇입니까?

나는 3 개의 해시 맵을 만들었습니다.

1) hmTextONLY는 : 그것은 텍스트 값을 포함한다 (열 이름 (columnIndex에). Threarname (1), 범주 (2), 설명 (5), 정당성 (6))

2) hmCOMBO1ONLY : 그에만 콤보 상자의 값을 포함 (열 이름 (열 인덱스) : 상태 (3)).

3) hmCOMBO2ONLY는 :의 만 combobox2 값을 포함 (열 이름 (열 인덱스). 우선 순위 (4))

방법 설명 이 코드는 코드에서 사용됩니다.

초기화 :보기를 초기화하십시오.

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() 불리는주기 방법을 가지고 그것의 상태는 기념물에 저장되어야한다.

    이 위키 페이지에서 자세한 내용을 제공하며 사용 기념품에 대한 대안이 나열되어 https://wiki.eclipse.org/FAQ_How_does_a_view_persist_its_state_between_sessions%3F

  • +0

    @ Rüdiger Herrmann 정말 감사합니다. –

    +0

    @ Rüdiger Herrmann : 코드를 수정 했으므로 현재 직면하고있는 실제 문제를보다 구체적으로 설명합니다. 위의 코드를주의 깊게 살펴보십시오. –

    +0

    당신은 직면 한 정확한 문제가 무엇인지 밝히지 않았습니다. –