Example #1
0
  void createPopup(String[] items, int selectionIndex) {
    // create shell and list
    popup = new AlphaDialog(getShell(), SWT.NO_TRIM | SWT.ON_TOP | SWT.BORDER);
    popup.setBackgroundMode(SWT.INHERIT_DEFAULT);
    Color borderColor = ColorCache.getInstance().getColor(200, 200, 200);
    popup.setBackground(borderColor);

    GridLayout gridLayout = new GridLayout(2, false);
    gridLayout.marginHeight = 0;
    gridLayout.marginBottom = 1;
    gridLayout.marginTop = 0;
    gridLayout.marginLeft = 0;
    gridLayout.marginRight = 0;

    gridLayout.horizontalSpacing = 0;
    gridLayout.verticalSpacing = 0;
    gridLayout.marginWidth = 0;
    popup.getPopup().setLayout(gridLayout);

    int style = getStyle();
    int listStyle = SWT.SINGLE | SWT.V_SCROLL | SWT.H_SCROLL | SWT.BORDER;
    if ((style & SWT.FLAT) != 0) listStyle |= SWT.FLAT;
    if ((style & SWT.RIGHT_TO_LEFT) != 0) listStyle |= SWT.RIGHT_TO_LEFT;
    if ((style & SWT.LEFT_TO_RIGHT) != 0) listStyle |= SWT.LEFT_TO_RIGHT;
    list = new List(popup.getPopup(), listStyle);
    GridData gridData = new GridData(GridData.FILL_BOTH);
    gridData.horizontalSpan = 2;
    list.setLayoutData(gridData);
    list.addMouseListener(
        new MouseAdapter() {
          public void mouseDown(MouseEvent e) {
            if (e.button == 3) {
              text.getMenu().setVisible(true);
            }
            if (list.isFocusControl()) return;
            text.forceFocus();
            list.setFocus();
          }
        });

    if (font != null) list.setFont(font);
    if (foreground != null) list.setForeground(foreground);
    if (background != null) list.setBackground(background);

    int[] popupEvents = {SWT.Close, SWT.Paint, SWT.Deactivate};
    for (int i = 0; i < popupEvents.length; i++)
      popup.getPopup().addListener(popupEvents[i], listener);
    int[] listEvents = {
      SWT.MouseUp, SWT.Selection, SWT.Traverse, SWT.KeyDown, SWT.KeyUp, SWT.FocusIn, SWT.Dispose
    };
    for (int i = 0; i < listEvents.length; i++) list.addListener(listEvents[i], listener);

    if (items != null) list.setItems(items);
    if (selectionIndex != -1) list.setSelection(selectionIndex);
  }
Example #2
0
 public void redraw() {
   super.redraw();
   text.redraw();
   arrow.redraw();
   if (icon != null) icon.redraw();
   if (popup.isVisible()) list.redraw();
 }
Example #3
0
 public void setEnabled(boolean enabled) {
   super.setEnabled(enabled);
   if (popup != null) popup.setVisible(false);
   if (text != null) text.setEnabled(enabled);
   if (icon != null) icon.setEnabled(enabled);
   if (arrow != null) arrow.setEnabled(enabled);
 }
Example #4
0
 public boolean isFocusControl() {
   checkWidget();
   if (text.isFocusControl()
       || arrow.isFocusControl()
       || list.isFocusControl()
       || icon.isFocusControl()
       || popup.isFocusControl()) {
     return true;
   }
   return super.isFocusControl();
 }
Example #5
0
  private void dropDown(boolean drop) {
    if (drop == isDropped()) return;
    if (!drop) {
      popup.setVisible(false);
      if (!isDisposed() && arrow.isFocusControl()) {
        text.setFocus();
      }
      return;
    }

    if (getShell() != popup.getParent()) {
      String[] items = list.getItems();
      int selectionIndex = list.getSelectionIndex();
      list.removeListener(SWT.Dispose, listener);
      popup.dispose();
      popup = null;
      list = null;
      createPopup(items, selectionIndex);
    }

    computeShowArea();

    popup.setAlpha(popup.getFinalAlpha());
    popup.setVisible(true);
  }
Example #6
0
 void comboEvent(Event event) {
   switch (event.type) {
     case SWT.Dispose:
       if (popup != null && !popup.isDisposed()) {
         list.removeListener(SWT.Dispose, listener);
         popup.dispose();
       }
       Shell shell = getShell();
       shell.removeListener(SWT.Deactivate, listener);
       Display display = getDisplay();
       display.removeFilter(SWT.FocusIn, filter);
       popup = null;
       text = null;
       list = null;
       arrow = null;
       break;
     case SWT.Move:
       dropDown(false);
       break;
     case SWT.Resize:
       internalLayout(false);
       break;
   }
 }
Example #7
0
  private void computeShowArea() {
    //    Point size = getSize ();
    int itemCount = list.getItemCount();
    itemCount = (itemCount == 0) ? visibleItemCount : Math.min(visibleItemCount, itemCount);
    int itemHeight = list.getItemHeight() * itemCount;
    Point listSize = list.computeSize(SWT.DEFAULT, itemHeight, false);
    //  list.setBounds(1, 1, Math.min (size.x - 2, listSize.x), listSize.y);

    int index = list.getSelectionIndex();
    if (index != -1) list.setTopIndex(index);

    Display display = getDisplay();
    Rectangle listRect = list.getBounds();
    Rectangle parentRect = display.map(getParent(), null, getBounds());
    Point comboSize = getSize();
    Rectangle displayRect = getMonitor().getClientArea();
    int width = Math.max(comboSize.x, listRect.width + 2);
    int height = listRect.height + 2;
    int x = parentRect.x;
    int y = parentRect.y + comboSize.y;
    if (y + height > displayRect.y + displayRect.height) y = parentRect.y - height;
    popup.setBounds(x, y, width - 15, listSize.y + 15);
  }
Example #8
0
  void listEvent(Event event) {
    switch (event.type) {
      case SWT.Dispose:
        if (getShell() != popup.getParent()) {
          String[] items = list.getItems();
          int selectionIndex = list.getSelectionIndex();
          popup = null;
          list = null;
          createPopup(items, selectionIndex);
        }
        break;
      case SWT.FocusIn:
        {
          handleFocus(SWT.FocusIn);
          break;
        }
      case SWT.MouseUp:
        {
          if (event.button != 1) return;
          dropDown(false);
          break;
        }
      case SWT.Selection:
        {
          int index = list.getSelectionIndex();
          if (index == -1) return;
          text.setText(list.getItem(index));
          //    text.selectAll ();
          list.setSelection(index);
          //    list.forceFocus();

          Event e = new Event();
          e.time = event.time;
          e.stateMask = event.stateMask;
          e.doit = event.doit;
          notifyListeners(SWT.Selection, e);
          event.doit = e.doit;
          break;
        }
      case SWT.Traverse:
        {
          switch (event.detail) {
            case SWT.TRAVERSE_RETURN:
            case SWT.TRAVERSE_ESCAPE:
            case SWT.TRAVERSE_ARROW_PREVIOUS:
            case SWT.TRAVERSE_ARROW_NEXT:
              event.doit = false;
              break;
          }
          Event e = new Event();
          e.time = event.time;
          e.detail = event.detail;
          e.doit = event.doit;
          e.character = event.character;
          e.keyCode = event.keyCode;
          notifyListeners(SWT.Traverse, e);
          event.doit = e.doit;
          event.detail = e.detail;
          break;
        }
      case SWT.KeyUp:
        {
          Event e = new Event();
          e.time = event.time;
          e.character = event.character;
          e.keyCode = event.keyCode;
          e.stateMask = event.stateMask;
          notifyListeners(SWT.KeyUp, e);
          break;
        }

      case SWT.KeyDown:
        {
          if (event.character == SWT.ESC) {
            // Escape key cancels popup list
            dropDown(false);
          }
          if ((event.stateMask & SWT.ALT) != 0
              && (event.keyCode == SWT.ARROW_UP || event.keyCode == SWT.ARROW_DOWN)) {
            dropDown(false);
          }
          if (event.character == SWT.CR) {
            // Enter causes default selection
            dropDown(false);
            Event e = new Event();
            e.time = event.time;
            e.stateMask = event.stateMask;
            notifyListeners(SWT.DefaultSelection, e);
          }
          // At this point the widget may have been disposed.
          // If so, do not continue.
          if (isDisposed()) break;
          Event e = new Event();
          e.time = event.time;
          e.character = event.character;
          e.keyCode = event.keyCode;
          e.stateMask = event.stateMask;
          notifyListeners(SWT.KeyDown, e);
          break;
        }
    }
  }
Example #9
0
 boolean isDropped() {
   if (popup == null) return false;
   return popup.getVisible();
 }
Example #10
0
 private void showFadeIn() {
   computeShowArea();
   popup.showFadeIn();
 }
Example #11
0
  void textEvent(Event event) {
    switch (event.type) {
      case SWT.FocusIn:
        {
          handleFocus(SWT.FocusIn);
          break;
        }
      case SWT.KeyDown:
        {
          if (event.character == SWT.CR) {
            dropDown(false);
            Event e = new Event();
            e.time = event.time;
            e.stateMask = event.stateMask;
            notifyListeners(SWT.DefaultSelection, e);
          }
          // At this point the widget may have been disposed.
          // If so, do not continue.
          if (isDisposed()) break;

          if (event.keyCode == SWT.ARROW_UP || event.keyCode == SWT.ARROW_DOWN) {
            event.doit = false;
            if ((event.stateMask & SWT.ALT) != 0) {
              boolean dropped = isDropped();
              text.selectAll();
              if (!dropped) setFocus();
              dropDown(!dropped);
              break;
            }

            int oldIndex = getSelectionIndex();
            if (event.keyCode == SWT.ARROW_UP && popup.isVisible()) {
              select(Math.max(oldIndex - 1, 0));
            } else if (popup.isVisible()) {
              select(Math.min(oldIndex + 1, getItemCount() - 1));
            }
            if (oldIndex != getSelectionIndex()) {
              Event e = new Event();
              e.time = event.time;
              e.stateMask = event.stateMask;
              notifyListeners(SWT.Selection, e);
            }
            // At this point the widget may have been disposed.
            // If so, do not continue.
            if (isDisposed()) break;
          }

          // Further work : Need to add support for incremental search in
          // pop up list as characters typed in text widget

          Event e = new Event();
          e.time = event.time;
          e.character = event.character;
          e.keyCode = event.keyCode;
          e.stateMask = event.stateMask;
          notifyListeners(SWT.KeyDown, e);
          break;
        }
      case SWT.KeyUp:
        {
          Event e = new Event();
          e.time = event.time;
          e.character = event.character;
          e.keyCode = event.keyCode;
          e.stateMask = event.stateMask;
          notifyListeners(SWT.KeyUp, e);
          break;
        }
      case SWT.Modify:
        {
          list.deselectAll();
          Event e = new Event();
          e.time = event.time;
          notifyListeners(SWT.Modify, e);
          break;
        }

      case SWT.MouseDown:
        {
          if (event.button != 1) return;
          if (text.getEditable()) return;
          boolean dropped = isDropped();
          text.selectAll();
          if (!dropped) setFocus();
          dropDown(!dropped);
          break;
        }

      case SWT.MouseUp:
        {
          if (event.button != 1) return;
          if (text.getEditable()) return;
          text.selectAll();
          break;
        }

      case SWT.Traverse:
        {
          switch (event.detail) {
            case SWT.TRAVERSE_RETURN:
            case SWT.TRAVERSE_ARROW_PREVIOUS:
            case SWT.TRAVERSE_ARROW_NEXT:
              // The enter causes default selection and
              // the arrow keys are used to manipulate the list contents so
              // do not use them for traversal.
              event.doit = false;
              break;
          }

          Event e = new Event();
          e.time = event.time;
          e.detail = event.detail;
          e.doit = event.doit;
          e.character = event.character;
          e.keyCode = event.keyCode;
          notifyListeners(SWT.Traverse, e);
          event.doit = e.doit;
          event.detail = e.detail;
          break;
        }
    }
  }
Example #12
0
 public void setVisible(boolean visible) {
   super.setVisible(visible);
   if (!visible) popup.setVisible(false);
 }