2012-06-12 77 views
0

如何將text.getText()傳遞給selectionChanged?鑄造似乎不起作用。我正在按按鈕使用它。這是滿級,但StackOverflow上是不是讓我把這裏更多文字,不用逐行解釋吧..事件上的Eclipse RPC SelectionChanged偵聽器

Listener listener = new Listener() { 
    public void handleEvent(Event event) { 
     if (event.widget == button3) { 
      viewer.setSelection(text.getText()); 
     } 
    } 


public class OpisView extends ViewPart implements ActionListener,ISelectionListener { 

    public final static String VIEW_ID="DetailsView"; 
    private String path; 
    public Composite x ; 
    private TableViewer viewer; 
      //public static final String VIEW_ID = "com.example.rcpmvc.calculator"; 

      @Override 
      public void createPartControl(final Composite parent) { 
       final Text text = new Text(parent, SWT.NONE); 

       getViewSite().getPage().addSelectionListener(this); 
       viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL); 
       PlatformUI.getWorkbench().getHelpSystem().setHelp(viewer.getControl(), "Widoki.OpisView"); 
       text.setText(""); 
       x = parent; 

       parent.setLayout(new GridLayout(2, false)); 
       GridData gridData = new GridData(); 
       gridData.widthHint = 50; 
       gridData.heightHint = 30; 
       getSite().setSelectionProvider(viewer); 
       getViewSite().getPage().addSelectionListener(this); 


       final Button button1 = new Button(parent, SWT.PUSH); 
       final Button button2 = new Button(parent, SWT.PUSH); 
       final Button button3 = new Button(parent, SWT.PUSH); 

       Listener listener = new Listener() { 
         public void handleEvent(Event event) { 
         if (event.widget == button1) { 

          FileRead x = new FileRead(); 
          try { 
           x.Add(text.getText(),path); 
           showMessage("Pomyslnie otagowano " + path + ", tagiem " + text.getText()); 
          } catch (IOException e) { 
           // TODO Auto-generated catch block 
           e.printStackTrace(); 
          } 

         } 
         if (event.widget == button2) { 


          FileRead x = new FileRead(); 
          try { 
           x.Remove(text.getText(),path); 
           showMessage("Pomyslnie usunieto tag " + text.getText() + " z pliku " + path); 
          } catch (IOException e) { 
           // TODO Auto-generated catch block 
           e.printStackTrace(); 
          } 

         } 
         if (event.widget == button3) { 

           text.getText(); 


         } 
         } 
        }; 

       text.addListener(SWT.KeyDown, listener); 
       button1.setLayoutData(gridData); 
       button1.addListener(SWT.Selection, listener); 
       button1.setText("Dodaj"); 


       button2.setLayoutData(gridData); 
       button2.addListener(SWT.Selection, listener); 
       button2.setText("Usuń"); 

       button3.setLayoutData(gridData); 
       button3.addListener(SWT.Selection, listener); 
       button3.setText("Wyszukaj"); 

       // Set the sorter for the table 

       //sGridLayoutFactory.fillDefaults().numColumns(3).spacing(3, 0).margins(0, 0).applyTo(parent); 

       GridLayoutFactory.swtDefaults().numColumns(3).spacing(0, 0).margins(0, 0).applyTo(parent); 

      } 

      private void showMessage(String message) { 
       MessageDialog.openInformation(
        x.getShell(), 
        "Opis", 
        message); 
      } 

      public void selectionChanged(IWorkbenchPart part, ISelection selection) { 
       if (selection instanceof IStructuredSelection) { 
        Object obj = ((IStructuredSelection) selection).getFirstElement(); 

        if (obj instanceof String) { 

         path = (String) obj; 
        } 
       } 
      } 

} 

能否請你幫我這個?我真的不能在一段時間內整理出來...

+0

這是一個Java問題,不是一個Eclipse之一。 –

+0

什麼類型是文本?您應該在一段代碼中定義您使用的所有內容以獲得更好的幫助。 – cklab

+0

'viewer'和'text'的類型是什麼? – SimplyPanda

回答

0

這真的是一個的問題!

但解決辦法是在最後加上關鍵字....

final Text text = new Text(parent, SWT.NONE); 
Listener listener = new Listener() { 
    public void handleEvent(Event event) { 
     if (event.button == SWT.BUTTON3) { 
     viewer.setSelection(text.getText()); 
     } 
    } 
}; 
text.addListener(SWT.KeyDown, listener); 
+0

仍然出現錯誤,當我運行你的代碼時,'org.eclipse.swt.widgets.Text不能轉換爲org.eclipse.jface.viewers.ISelection'。 – Seb

+2

你必須包括更多的代碼和確切的錯誤(加上位置)... –

+0

我粘貼在我的課堂上,你可以看看嗎? – Seb