SWT JFACE 关于Tree上加celleditor的代码摘录

这里是摘录一些 SWT/JFACE RCP项目中 比较有用的小代码。


右键复杂菜单
final MenuManager menuMgr = new MenuManager("#popupMenu");
        menuMgr.setRemoveAllWhenShown(true);
        reqTreeViewer.getControl().setMenu(menuMgr.createContextMenu(reqTreeViewer.getControl()));
        menuMgr.addMenuListener(new IMenuListener(){

            public void menuAboutToShow(IMenuManager manager) {
                manager.add(new IAction());
                   
            }
            
 });


TREE加Tex EDITOR
final TreeItem[]lastItem = new TreeItem[1];
        final TreeEditor editor = new TreeEditor(reqTree);
        reqTree.addListener(SWT.Selection, new Listener() {
            public void handleEvent(Event event) {
              final TreeItem item = (TreeItem) event.item;
              if (item != null && item == lastItem[0]) {
                boolean isCarbon = SWT.getPlatform().equals("carbon");
                final Composite composite = new Composite(reqTree, SWT.NONE);
                if (!isCarbon)
                  composite.setBackground(new Color(null,0,0,0));
                final Text text = new Text(composite, SWT.NONE);
                final int inset = isCarbon ? 0 : 1;
                    composite.addListener(SWT.Resize, new Listener() {
                        public void handleEvent(Event e) {
                            Rectangle rect = composite.getClientArea();
                            text.setBounds(rect.x + inset, rect.y + inset, rect.width - inset * 2, rect.height - inset * 2);
                        }
                    }
                
                );
                Listener textListener = new Listener() {
                  public void handleEvent(final Event e) {
                    switch (e.type) {
                    case SWT.FocusOut:
                      item.setText(1,text.getText());
                      composite.dispose();
                      break;
                    case SWT.Verify:
                      String newText = text.getText();
                      String leftText = newText.substring(0, e.start);
                      String rightText = newText.substring(e.end, newText.length());
                      GC gc = new GC(text);
                      Point size = gc.textExtent(leftText + e.text + rightText);
                      gc.dispose();
                      size = text.computeSize(size.x, SWT.DEFAULT);
                      editor.horizontalAlignment = SWT.LEFT;
                      Rectangle itemRect = item.getBounds(),
                      rect = reqTree.getClientArea();
                      editor.minimumWidth = Math.max(size.x, itemRect.width) + inset * 2;
                      int left = itemRect.x,
                      right = rect.x + rect.width;
                      editor.setColumn(1);
                      editor.minimumWidth = Math.min(editor.minimumWidth, right - left);
                      editor.minimumHeight = size.y + inset * 2;
                      editor.layout();
                      break;
                    case SWT.Traverse:
                      switch (e.detail) {
                      case SWT.TRAVERSE_RETURN:
                        item.setText(1,text.getText());
                      // FALL THROUGH
                      case SWT.TRAVERSE_ESCAPE:
                        composite.dispose();
                        e.doit = false;
                      }
                      break;
                    }
                  }
                };
                text.addListener(SWT.FocusOut, textListener);
                text.addListener(SWT.Traverse, textListener);
                text.addListener(SWT.Verify, textListener);
                
               
                
                editor.setEditor(composite, item);
                if(item.getData() instanceof IRequestParameter){
                    IRequestParameter param= (IRequestParameter)item.getData();
                    text.setText(getFormatStringValue(param));
                }
                
                text.selectAll();
                text.setFocus();
              }
              lastItem[0] = item;
            }
          });
     



TREE加BUTTON
final Tree tree = new Tree(shell, SWT.SINGLE);
    for (int i = 0; i < 3; i++) {
      TreeItem iItem = new TreeItem(tree, SWT.NONE);
      iItem.setText("Item " + (i + 1));
      for (int j = 0; j < 3; j++) {
        TreeItem jItem = new TreeItem(iItem, SWT.NONE);
        jItem.setText("Sub Item " + (j + 1));
        for (int k = 0; k < 3; k++) {
          new TreeItem(jItem, SWT.NONE).setText("Sub Sub Item " + (k + 1));
        }
        jItem.setExpanded(true);
      }
      iItem.setExpanded(true);
    }

    final TreeEditor editor = new TreeEditor(tree);
    editor.horizontalAlignment = SWT.LEFT;
    editor.grabHorizontal = true;

    tree.addMouseListener(new MouseAdapter() {
      public void mouseDown(MouseEvent event) {
        final TreeItem item = tree.getSelection()[0];

        final Button bn = new Button(tree, SWT.NONE);
        bn.setText("click");
        bn.setFocus();

        bn.addSelectionListener(new SelectionListener() {

          public void widgetSelected(SelectionEvent arg0) {
            int style = SWT.ICON_QUESTION | SWT.YES | SWT.NO;

            MessageBox messageBox = new MessageBox(shell, style);
            messageBox.setMessage("Message");
            int rc = messageBox.open();

            item.setText(rc + "");
            bn.dispose();
          }

          public void widgetDefaultSelected(SelectionEvent arg0) {
          }
        });

        editor.setEditor(bn, item);
      }
    });



TREE加LABEL
        reqTree.addMouseListener(new MouseAdapter(){
            public void mouseDown(MouseEvent event){
                clearTmpSelections();
                final TreeItem item = reqTree.getSelection()[0];
                final Label bn=new Label(reqTree,SWT.None);
                bn.setFocus();
                bn.setVisible(false);
                if(item.getData() instanceof IRequestParameter){
                    IRequestParameter param=(IRequestParameter)item.getData();
                    bn.setText(getFormatStringValue(param));
                }
               
                bn.addListener(SWT.MouseDoubleClick, new Listener(){
                   
                    public void handleEvent(Event event) {
                        System.out.println("Dasdasd");
                    }
                    
                });
                tmpLabels.add(bn);
           
                bn.addListener(SWT.MouseDoubleClick, new Listener() {

                    public void handleEvent(Event event) {
                        int style = SWT.ICON_QUESTION | SWT.YES | SWT.NO;

                        MessageBox messageBox = new MessageBox(new Shell(reqTree.getDisplay()));
                        messageBox.setMessage("Message");
                        int rc = messageBox.open();
                        item.setText(rc + "");
                        bn.dispose();
                    }
                });
                editor.setColumn(0);
                editor.setEditor(bn, item);
            }
        });


你可能感兴趣的:(J#)