public void initGui()

in ruta-ep-addons/src/main/java/org/apache/uima/ruta/cde/ui/ConstraintSelectComposite.java [114:412]


  public void initGui() {
    constraintList = new ArrayList<ConstraintData>();
    this.setLayout(new FormLayout());

    // Simple Constraint Button
    constraintButtonRuta = new Button(this, SWT.PUSH | SWT.CENTER);
    FormData ConstraintButtonTMData = new FormData();
    ConstraintButtonTMData.width = 25;
    ConstraintButtonTMData.height = 25;
    ConstraintButtonTMData.top = new FormAttachment(0, 10);
    ConstraintButtonTMData.right = new FormAttachment(100, -5);
    constraintButtonRuta.setLayoutData(ConstraintButtonTMData);
    Image folderIcon = getImage("add");
    constraintButtonRuta.setImage(folderIcon);
    constraintButtonRuta.addSelectionListener(new SelectionAdapter() {
      @Override
      public void widgetSelected(SelectionEvent event) {

        ConstraintDialog cdialog = new ConstraintDialog(getShell());
        cdialog.create();
        if (cdialog.open() == Window.OK) {
          SimpleRutaRuleConstraint newConstraint = new SimpleRutaRuleConstraint(cdialog.getRule(),
                  cdialog.getDescription());
          ConstraintData newConstraintData = new ConstraintData(newConstraint);
          constraintList.add(newConstraintData);
          tableViewer.setInput(constraintList);
          tableViewer.refresh();
        }
      }
    });

    // ListConstraint Button
    constraintButtonRutaList = new Button(this, SWT.PUSH | SWT.CENTER);
    FormData ConstraintButtonTMListData = new FormData();
    ConstraintButtonTMListData.width = 25;
    ConstraintButtonTMListData.height = 25;
    ConstraintButtonTMListData.top = new FormAttachment(constraintButtonRuta, 10);
    ConstraintButtonTMListData.right = new FormAttachment(100, -5);
    constraintButtonRutaList.setLayoutData(ConstraintButtonTMListData);
    Image addListIcon = getImage("scriptAdd");
    constraintButtonRutaList.setImage(addListIcon);
    constraintButtonRutaList.setToolTipText("Add a list of SimpleRutaConstraints");
    constraintButtonRutaList.addSelectionListener(new SelectionAdapter() {
      @Override
      public void widgetSelected(SelectionEvent event) {

        ConstraintDialog cdialog = new ConstraintDialog(getShell());
        cdialog.create();
        if (cdialog.open() == Window.OK) {
          RutaRuleListConstraint newConstraint = new RutaRuleListConstraint(cdialog.getRule(),
                  cdialog.getDescription());
          ConstraintData newConstraintData = new ConstraintData(newConstraint);
          constraintList.add(newConstraintData);
          tableViewer.setInput(constraintList);
          tableViewer.refresh();
        }
      }
    });

    // Complex Constraint Button now used for GEConstraints

    constraintButtonTMComplex = new Button(this, SWT.PUSH | SWT.CENTER);
    FormData ConstraintButtonTMComplexData = new FormData();
    ConstraintButtonTMComplexData.width = 25;
    ConstraintButtonTMComplexData.height = 25;
    ConstraintButtonTMComplexData.top = new FormAttachment(constraintButtonRutaList, 10);
    ConstraintButtonTMComplexData.right = new FormAttachment(100, -5);
    constraintButtonTMComplex.setLayoutData(ConstraintButtonTMComplexData);
    Image addTableIcon = getImage("tmAdd");
    constraintButtonTMComplex.setImage(addTableIcon);
    constraintButtonTMComplex.setToolTipText("Add an Annotation-Distribution Constraint");
    constraintButtonTMComplex.addSelectionListener(new SelectionAdapter() {
      @Override
      public void widgetSelected(SelectionEvent event) {
        ConstraintDialog cdialog = new ConstraintDialog(getShell());
        cdialog.create();
        if (cdialog.open() == Window.OK) {
          RutaGEConstraint newConstraint = new RutaGEConstraint(cdialog.getRule(), cdialog
                  .getDescription());
          ConstraintData newConstraintData = new ConstraintData(newConstraint);
          constraintList.add(newConstraintData);
          tableViewer.setInput(constraintList);
          tableViewer.refresh();
        }
      }
    });

    // Constraint Edit Button

    constraintButconJava = new Button(this, SWT.PUSH | SWT.CENTER);
    FormData ConstraintButtonJavaData = new FormData();
    ConstraintButtonJavaData.width = 25;
    ConstraintButtonJavaData.height = 25;
    ConstraintButtonJavaData.top = new FormAttachment(constraintButtonTMComplex, 10);
    ConstraintButtonJavaData.right = new FormAttachment(100, -5);
    constraintButconJava.setLayoutData(ConstraintButtonJavaData);
    Image addJavaIcon = getImage("cupAdd");
    constraintButconJava.setImage(addJavaIcon);
    constraintButconJava.setToolTipText("Add Java Constraint");

    constraintRemoveButton = new Button(this, SWT.PUSH | SWT.CENTER);
    FormData ConstraintRemoveData = new FormData();
    ConstraintRemoveData.width = 25;
    ConstraintRemoveData.height = 25;
    ConstraintRemoveData.top = new FormAttachment(constraintButconJava, 10);
    ConstraintRemoveData.right = new FormAttachment(100, -5);
    constraintRemoveButton.setLayoutData(ConstraintRemoveData);
    Image addRemoveIcon = getImage("delete");
    constraintRemoveButton.setImage(addRemoveIcon);
    constraintRemoveButton.setToolTipText("Remove Selected Constraints");
    constraintRemoveButton.addSelectionListener(new SelectionAdapter() {
      @Override
      public void widgetSelected(SelectionEvent event) {
        int[] indices = table.getSelectionIndices();
        for (int i = indices.length - 1; i >= 0; i--) {
          constraintList.remove(indices[i]);
        }
        tableViewer.setSelection(new StructuredSelection());
        editor.getEditor().dispose();
        tableViewer.setInput(constraintList);
        tableViewer.refresh();
      }
    });

    tableViewer = new TableViewer(this, SWT.MULTI | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL
            | SWT.FULL_SELECTION);
    contentProvider = new ConstraintSelectTableContentProvider();
    labelProvider = new ConstraintSelectTableLabelProvider();

    tableViewer.setContentProvider(contentProvider);
    tableViewer.setLabelProvider(labelProvider);
    table = tableViewer.getTable();
    FormData tableFormData = new FormData();
    tableFormData.top = new FormAttachment(0, 10);
    tableFormData.left = new FormAttachment(0, 10);
    tableFormData.right = new FormAttachment(constraintButtonRuta, -10);
    tableFormData.bottom = new FormAttachment(100, 0);
    table.setLayoutData(tableFormData);

    table.addKeyListener(new KeyListener() {

      public void keyPressed(KeyEvent e) {
        // if (((e.stateMask & SWT.CTRL) == SWT.CTRL) && (e.keyCode == 'c')) {
        // String output = "";
        // TableItem[] items = table.getSelection();
        // for (TableItem item : items) {
        // ConstraintData data = (ConstraintData) item.getData();
        // output = output + data.getConstraint().getData() + "\n";
        // }
        // clipboard.setContents(new Object[] { output },
        // new Transfer[] { TextTransfer.getInstance() });
        // }
      }

      public void keyReleased(KeyEvent arg0) {
      }
    });

    tableViewer.addDoubleClickListener(new IDoubleClickListener() {
      public void doubleClick(DoubleClickEvent event) {
        IStructuredSelection selection = (IStructuredSelection) tableViewer.getSelection();
        if (selection.isEmpty()) {
          return;
        }
        Object element = selection.getFirstElement();
        if (element instanceof ConstraintData) {
          ConstraintData data = (ConstraintData) element;
          IRutaConstraint constraint = data.getConstraint();
          if (constraint instanceof SimpleRutaRuleConstraint) {
            ConstraintDialog cdialog = new ConstraintDialog(getShell());
            cdialog.create((SimpleRutaRuleConstraint) constraint);
            if (cdialog.open() == Window.OK) {
              SimpleRutaRuleConstraint newConstraint = new SimpleRutaRuleConstraint(cdialog
                      .getRule(), cdialog.getDescription());
              data.setConstraint(newConstraint);
              tableViewer.setInput(constraintList);
              tableViewer.refresh();
            }
          }

          if (constraint instanceof RutaRuleListConstraint) {
            ConstraintDialog cdialog = new ConstraintDialog(getShell());
            cdialog.create((RutaRuleListConstraint) constraint);
            if (cdialog.open() == Window.OK) {
              RutaRuleListConstraint newConstraint = new RutaRuleListConstraint(cdialog.getRule(),
                      cdialog.getDescription());
              data.setConstraint(newConstraint);
              tableViewer.setInput(constraintList);
              tableViewer.refresh();
            }
          }

          if (constraint instanceof RutaGEConstraint) {
            ConstraintDialog cdialog = new ConstraintDialog(getShell());
            cdialog.create((RutaGEConstraint) constraint);
            if (cdialog.open() == Window.OK) {

              RutaGEConstraint newConstraint = new RutaGEConstraint(cdialog.getRule(), cdialog
                      .getDescription());
              data.setConstraint(newConstraint);
              tableViewer.setInput(constraintList);
              tableViewer.refresh();
            }
          }
        }

        // /...
      }

    });

    tc1 = new TableColumn(table, SWT.LEFT);
    tc1.setText("Constraint");
    tc1.setWidth(300);
    tc1.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent event) {
        Comparator comparator = comparatorFactory.getComparator(tc1);
        Collections.sort(constraintList, comparator);
        if (editor.getEditor() != null) {
          editor.getEditor().dispose();
        }
        tableViewer.setInput(constraintList);
        tableViewer.refresh();
      }
    });

    tc2 = new TableColumn(table, SWT.LEFT);
    tc2.setText("Weight");
    tc2.setWidth(50);
    tc2.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent event) {
        Comparator comparator = comparatorFactory.getComparator(tc2);
        Collections.sort(constraintList, comparator);
        if (editor.getEditor() != null) {
          editor.getEditor().dispose();
        }
        tableViewer.setInput(constraintList);
        tableViewer.refresh();
      }
    });

    editor = new TableEditor(table);
    // The editor must have the same size as the cell and must
    // not be any smaller than 50 pixels.
    editor.horizontalAlignment = SWT.LEFT;
    editor.grabHorizontal = true;
    editor.minimumWidth = 50;
    table.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent e) {
        // Clean up any previous editor control
        Control oldEditor = editor.getEditor();

        if (oldEditor != null)
          oldEditor.dispose();

        // Identify the selected row
        TableItem item = (TableItem) e.item;
        if (item == null)
          return;

        // The control that will be the editor must be a child of the
        // Table
        Spinner newEditor = new Spinner(table, SWT.NONE);

        if (item.getData() instanceof ConstraintData) {
          ConstraintData data = (ConstraintData) item.getData();
          newEditor.setSelection(data.getWeight());
        }

        newEditor.addModifyListener(new ModifyListener() {
          public void modifyText(ModifyEvent e) {
            if (editor.getItem().isDisposed()) {
              return;
            }
            Spinner spinner = (Spinner) editor.getEditor();
            int newWeight = spinner.getSelection();
            TableItem item = editor.getItem();
            if (item.getData() instanceof ConstraintData) {
              ConstraintData data = (ConstraintData) item.getData();
              data.setWeight(newWeight);
            }
            tableViewer.refresh();
          }
        });
        newEditor.setFocus();
        editor.setEditor(newEditor, item, EDITABLECOLUMN);
      }
    });
    // Resize columns to match input
    // for (int i = 0, n = table.getColumnCount(); i < n; i++) {
    // table.getColumn(i).pack();
    // }

    table.setHeaderVisible(true);
    table.setLinesVisible(true);
    tableViewer.refresh();
    this.layout();

  }