2012-06-12 51 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