0

In a Tree Viewer, I have five Columns and in this columns except first I am using CCombo Box with the help of tree editor. Now in CCombo box I need to select all items. How is it possible? Please find the below code implementation of CCombo box in my view:

final CCombo comboForRunOn = new CCombo(remoteTable, SWT.READ_ONLY);
comboForRunOn.setItems(remoteComputerIPs);

/*Exe Type - WINDOWS, ANDROID*/
final CCombo comboForExeType = new CCombo(remoteTable, SWT.READ_ONLY);
comboForExeType.setItems(exeTypes.toArray(new String[exeTypes.size()]));
comboForExeType.select(exeTypes.indexOf(tsTCGson.tcParams.get(1).tcparamValue));
/*Exe Type - Firefox, IE, Chrome*/
final CCombo comboForExePlatform = new CCombo(remoteTable, SWT.READ_ONLY);
comboForExePlatform.setItems(exePlatform.toArray(new String[exePlatform.size()]));
comboForExePlatform.select(exePlatform.indexOf(tsTCGson.tcParams.get(0).tcparamValue));

TreeEditor editorForRunOn = new TreeEditor(remoteTable);
TreeEditor editorForExeType = new TreeEditor(remoteTable);
TreeEditor editorForExePlatform = new TreeEditor(remoteTable);
editorForRunOn.setEditor(comboForRunOn, trtmTestcases, 3);
editorForExeType.setEditor(comboForExeType, trtmTestcases, 2);
editorForExePlatform.setEditor(comboForExePlatform, trtmTestcases, 1);
editorForRunOn.horizontalAlignment = SWT.LEFT;
editorForRunOn.grabHorizontal = true;
editorForExeType.horizontalAlignment = SWT.LEFT; 
editorForExeType.grabHorizontal = true;
editorForExePlatform.horizontalAlignment = SWT.LEFT; 
editorForExePlatform.grabHorizontal = true;

4

1 回答 1

0

为了克服这个问题,我创建了 MultiSectionCombo.java 类,找到下面的代码,

import java.util.Arrays;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.ShellAdapter;
import org.eclipse.swt.events.ShellEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.TreeItem;
import org.eclipse.wb.swt.SWTResourceManager;

public class MultiSelectionCombo extends Composite {

   Shell shell = null;
   List list = null;

   Text txtCurrentSelection = null;

   String[] textItems = null;
   int[] currentSelection = null;
   TreeItem treeItem = null;

   public MultiSelectionCombo(Composite parent,TreeItem tItem, String[] items, int[] selection, int style) {
      super(parent, style);
      currentSelection = selection;
      textItems = items;
      treeItem = tItem;
      init();
   }

   private void init() {
      GridLayout layout = new GridLayout();
      layout.marginBottom = 0;
      layout.marginTop = 0;
      layout.marginLeft = 0;
      layout.marginRight = 0;
      layout.marginWidth = 0;
      layout.marginHeight = 0;
      GridLayout gridLayout = new GridLayout();
      gridLayout.marginWidth = 0;
      gridLayout.marginHeight = 0;
      setLayout(gridLayout);
      txtCurrentSelection = new Text(this, SWT.READ_ONLY);
      txtCurrentSelection.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
      txtCurrentSelection.setLayoutData(new GridData(GridData.FILL_BOTH));

      displayText();

      txtCurrentSelection.addMouseListener(new MouseAdapter() {

         @Override
         public void mouseDown(MouseEvent event) {
            super.mouseDown(event);
            initFloatShell();
         }

      });
   }

   private void initFloatShell() {
      Point p = txtCurrentSelection.getParent().toDisplay(txtCurrentSelection.getLocation());
      Point size = txtCurrentSelection.getSize();
      Rectangle shellRect = new Rectangle(p.x, p.y + size.y, size.x, 0);
      shell = new Shell(MultiSelectionCombo.this.getShell(), SWT.NO_TRIM);

      GridLayout gl = new GridLayout();
      gl.marginBottom = 0;
      gl.marginTop = 0;
      gl.marginRight = 0;
      gl.marginLeft = 0;
      gl.marginWidth = 0;
      gl.marginHeight = 0;
      shell.setLayout(gl);

      list = new List(shell, SWT.BORDER | SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
      for (String value: textItems) {
         list.add(value);
      }

      list.setSelection(currentSelection);

      GridData gd = new GridData(GridData.FILL_BOTH);
      list.setLayoutData(gd);

      shell.setSize(shellRect.width, 100);
      shell.setLocation(shellRect.x, shellRect.y);

      list.addMouseListener(new MouseAdapter() {

         @Override
         public void mouseUp(MouseEvent event) {
            super.mouseUp(event);
            currentSelection = list.getSelectionIndices();
            if ((event.stateMask & SWT.CTRL) == 0) {
               shell.dispose();
               displayText();
            }
         }
      });

      shell.addShellListener(new ShellAdapter() {

         public void shellDeactivated(ShellEvent arg0) {
            if (shell != null && !shell.isDisposed()) {
               currentSelection = list.getSelectionIndices();
               displayText();
               shell.dispose();
            }
         }
      });
      shell.open();
   }

   private void displayText() {
      if (currentSelection != null && currentSelection.length > 0) {
         StringBuffer sb = new StringBuffer();
         for (int i = 0; i < currentSelection.length; i++) {
            if (i > 0)
               sb.append(", ");
            sb.append(textItems[currentSelection[i]]);
         }
         txtCurrentSelection.setText(sb.toString());
         treeItem.setText(1, sb.toString());
      }
      else {
         txtCurrentSelection.setText("");
         treeItem.setText(1,"");
      }
   }

   public int[] getSelections() {
      return this.currentSelection;
   }

}

并且它在代码中工作正常。

于 2017-04-19T12:07:01.513 回答