Пример #1
0
  private void onRestoreDefaults() {
    IPreferenceScope defaultScope = Owl.getPreferenceService().getDefaultScope();
    int[] defaultItemsState = defaultScope.getIntegers(DefaultPreferences.TOOLBAR_ITEMS);
    int defaultMode = defaultScope.getInteger(DefaultPreferences.TOOLBAR_MODE);

    fPreferences.putIntegers(DefaultPreferences.TOOLBAR_ITEMS, defaultItemsState);
    fPreferences.putInteger(DefaultPreferences.TOOLBAR_MODE, defaultMode);
    fItemViewer.refresh();
    fModeViewer.setSelection(new StructuredSelection(CoolBarMode.values()[defaultMode]));
    updateButtonEnablement();
  }
Пример #2
0
  /*
   * @see org.eclipse.jface.dialogs.TitleAreaDialog#createDialogArea(org.eclipse.swt.widgets.Composite)
   */
  @Override
  protected Control createDialogArea(Composite parent) {
    Composite container = createContainer(parent);

    Label infoLabel = new Label(container, SWT.None);
    infoLabel.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false, 2, 1));
    infoLabel.setText(Messages.CustomizeToolbarDialog_DIALOG_INFO);

    /* Table showing Tool Items */
    Composite tableContainer = new Composite(container, SWT.NONE);
    tableContainer.setLayout(LayoutUtils.createGridLayout(1, 0, 0));
    tableContainer.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

    CTable cTable = new CTable(tableContainer, SWT.BORDER | SWT.MULTI | SWT.FULL_SELECTION);

    fItemViewer = new TableViewer(cTable.getControl());
    fItemViewer.getTable().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
    fItemViewer.getTable().setHeaderVisible(false);
    ((GridData) fItemViewer.getTable().getLayoutData()).heightHint =
        fItemViewer.getTable().getItemHeight() * 24;
    fItemViewer.getTable().setFocus();
    fItemViewer
        .getTable()
        .setData(ApplicationWorkbenchWindowAdvisor.FOCUSLESS_SCROLL_HOOK, new Object());

    /* Custom Owner Drawn Category */
    if (!OwlUI.isHighContrast()) {
      fItemViewer
          .getControl()
          .addListener(
              SWT.EraseItem,
              new Listener() {
                public void handleEvent(Event event) {
                  ToolBarItem item = (ToolBarItem) event.item.getData();
                  if (item.item == CoolBarItem.SEPARATOR) {
                    Scrollable scrollable = (Scrollable) event.widget;
                    GC gc = event.gc;

                    Rectangle area = scrollable.getClientArea();
                    Rectangle rect = event.getBounds();

                    /* Paint the selection beyond the end of last column */
                    OwlUI.codExpandRegion(event, scrollable, gc, area);

                    /* Draw Gradient Rectangle */
                    Color oldForeground = gc.getForeground();
                    Color oldBackground = gc.getBackground();

                    /* Gradient */
                    gc.setBackground(fSeparatorBg);
                    gc.fillRectangle(0, rect.y, area.width, rect.height);

                    /* Top / Bottom Line */
                    gc.setForeground(fSeparatorBorderFg);
                    gc.drawLine(0, rect.y + rect.height - 1, area.width, rect.y + rect.height - 1);
                    gc.drawLine(0, rect.y, area.width, rect.y);

                    gc.setForeground(oldForeground);
                    gc.setBackground(oldBackground);

                    /* Mark as Background being handled */
                    event.detail &= ~SWT.BACKGROUND;
                  }
                }
              });
    }

    TableColumn nameCol = new TableColumn(fItemViewer.getTable(), SWT.NONE);

    CColumnLayoutData data = new CColumnLayoutData(Size.FILL, 100);
    cTable.manageColumn(
        nameCol, data, Messages.CustomizeToolbarDialog_VISIBLE_ITEMS, null, null, false, false);

    /* ContentProvider returns all selected Items */
    fItemViewer.setContentProvider(
        new IStructuredContentProvider() {
          public Object[] getElements(Object inputElement) {
            int[] itemIds = fPreferences.getIntegers(DefaultPreferences.TOOLBAR_ITEMS);
            ToolBarItem[] items = new ToolBarItem[itemIds.length];
            for (int i = 0; i < itemIds.length; i++) {
              items[i] = new ToolBarItem(CoolBarItem.values()[itemIds[i]]);
            }
            return items;
          }

          public void dispose() {}

          public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {}
        });

    /* Label Provider */
    fItemViewer.setLabelProvider(
        new CellLabelProvider() {
          @Override
          public void update(ViewerCell cell) {
            CoolBarItem item = ((ToolBarItem) cell.getElement()).item;
            cell.setText(item.getName());

            if (item.getImg() != null) cell.setImage(fResources.createImage(item.getImg()));

            if (!OwlUI.isHighContrast() && item == CoolBarItem.SPACER)
              cell.setForeground(getShell().getDisplay().getSystemColor(SWT.COLOR_DARK_GRAY));
          }
        });

    /* Selection */
    fItemViewer.addSelectionChangedListener(
        new ISelectionChangedListener() {
          public void selectionChanged(SelectionChangedEvent event) {
            updateButtonEnablement();
          }
        });

    /* Support Keyboard Remove */
    fItemViewer
        .getTable()
        .addKeyListener(
            new KeyAdapter() {
              @Override
              public void keyPressed(KeyEvent e) {
                if (e.keyCode == SWT.DEL || (Application.IS_MAC && e.keyCode == SWT.BS)) onRemove();
              }
            });

    /* Drag Support */
    fItemViewer.addDragSupport(
        DND.DROP_MOVE,
        new Transfer[] {LocalSelectionTransfer.getTransfer()},
        new DragSourceAdapter() {
          @Override
          public void dragStart(final DragSourceEvent event) {
            SafeRunnable.run(
                new LoggingSafeRunnable() {
                  public void run() throws Exception {
                    IStructuredSelection selection =
                        (IStructuredSelection) fItemViewer.getSelection();
                    event.doit = (selection.size() < fItemViewer.getTable().getItemCount());

                    if (event.doit) {
                      LocalSelectionTransfer.getTransfer().setSelection(selection);
                      LocalSelectionTransfer.getTransfer()
                          .setSelectionSetTime(event.time & 0xFFFFFFFFL);
                    }
                    ;
                  }
                });
          }

          @Override
          public void dragSetData(final DragSourceEvent event) {
            SafeRunnable.run(
                new LoggingSafeRunnable() {
                  public void run() throws Exception {
                    if (LocalSelectionTransfer.getTransfer().isSupportedType(event.dataType))
                      event.data = LocalSelectionTransfer.getTransfer().getSelection();
                  }
                });
          }

          @Override
          public void dragFinished(DragSourceEvent event) {
            SafeRunnable.run(
                new LoggingSafeRunnable() {
                  public void run() throws Exception {
                    LocalSelectionTransfer.getTransfer().setSelection(null);
                    LocalSelectionTransfer.getTransfer().setSelectionSetTime(0);
                  }
                });
          }
        });

    /* Drop Support */
    ViewerDropAdapter dropSupport =
        new ViewerDropAdapter(fItemViewer) {

          @Override
          public boolean validateDrop(Object target, int operation, TransferData transferType) {
            return true;
          }

          @Override
          public boolean performDrop(Object data) {
            ToolBarItem target = (ToolBarItem) getCurrentTarget();
            if (target != null) {
              onMove((StructuredSelection) data, target, getCurrentLocation());
              return true;
            }

            return false;
          }
        };
    dropSupport.setFeedbackEnabled(true);
    dropSupport.setScrollEnabled(true);
    dropSupport.setSelectionFeedbackEnabled(true);
    fItemViewer.addDropSupport(
        DND.DROP_MOVE, new Transfer[] {LocalSelectionTransfer.getTransfer()}, dropSupport);

    /* Set Dummy Input */
    fItemViewer.setInput(this);

    /* Container for the Buttons to Manage Providers */
    Composite buttonContainer = new Composite(container, SWT.None);
    buttonContainer.setLayout(LayoutUtils.createGridLayout(1, 0, 0));
    buttonContainer.setLayoutData(new GridData(SWT.BEGINNING, SWT.FILL, false, false));

    /* Add */
    fAddMenu = new Menu(getShell(), SWT.POP_UP);
    fAddMenu.addMenuListener(
        new MenuListener() {
          public void menuShown(MenuEvent e) {
            MenuItem[] items = fAddMenu.getItems();
            for (MenuItem item : items) {
              item.dispose();
            }

            /* Fill not yet visible Items */
            int[] toolbarItemIds = fPreferences.getIntegers(DefaultPreferences.TOOLBAR_ITEMS);
            List<CoolBarItem> visibleItems = new ArrayList<CoolBarItem>();
            for (int toolbarItemId : toolbarItemIds) {
              visibleItems.add(CoolBarItem.values()[toolbarItemId]);
            }

            CoolBarItem[] toolItems = getSortedItems();
            int currentGroup = -1;
            for (final CoolBarItem toolItem : toolItems) {
              if (!visibleItems.contains(toolItem)
                  || toolItem == CoolBarItem.SEPARATOR
                  || toolItem == CoolBarItem.SPACER) {

                /* Divide Groups by Separators */
                if (currentGroup >= 0 && currentGroup != toolItem.getGroup())
                  new MenuItem(fAddMenu, SWT.SEPARATOR);

                /* Create Menu Item */
                MenuItem item = new MenuItem(fAddMenu, SWT.PUSH);
                if (StringUtils.isSet(toolItem.getTooltip())) item.setText(toolItem.getTooltip());
                else item.setText(toolItem.getName());
                if (toolItem.getImg() != null)
                  item.setImage(fResources.createImage(toolItem.getImg()));

                item.addSelectionListener(
                    new SelectionAdapter() {
                      @Override
                      public void widgetSelected(SelectionEvent e) {

                        /* Add Item */
                        onAdd(toolItem);

                        /* Re-Open Menu for More */
                        JobRunner.runInUIThread(
                            fAddMenu,
                            new Runnable() {
                              public void run() {
                                fAddMenu.setVisible(true);
                              };
                            });
                      }
                    });

                currentGroup = toolItem.getGroup();
              }
            }
          }

          public void menuHidden(MenuEvent e) {}
        });

    fAddButton = new Button(buttonContainer, SWT.DOWN);
    fAddButton.setText(Messages.CustomizeToolbarDialog_ADD);
    applyDialogFont(fAddButton);
    setButtonLayoutData(fAddButton);
    fAddButton.addSelectionListener(
        new SelectionAdapter() {
          @Override
          public void widgetSelected(SelectionEvent e) {
            Rectangle rect = fAddButton.getBounds();
            Point pt = new Point(rect.x, rect.y + rect.height);
            pt = fAddButton.toDisplay(pt);
            fAddMenu.setLocation(pt.x, pt.y);
            fAddMenu.setVisible(true);
          }
        });

    /* Remove */
    fRemoveButton = new Button(buttonContainer, SWT.PUSH);
    fRemoveButton.setText(Messages.CustomizeToolbarDialog_REMOVE);
    fRemoveButton.setEnabled(false);
    applyDialogFont(fRemoveButton);
    setButtonLayoutData(fRemoveButton);
    fRemoveButton.addSelectionListener(
        new SelectionAdapter() {
          @Override
          public void widgetSelected(SelectionEvent e) {
            onRemove();
          }
        });

    /* Move Provider Up */
    fMoveUpButton = new Button(buttonContainer, SWT.PUSH);
    fMoveUpButton.setText(Messages.CustomizeToolbarDialog_MOVE_UP);
    fMoveUpButton.setEnabled(false);
    applyDialogFont(fMoveUpButton);
    setButtonLayoutData(fMoveUpButton);
    ((GridData) fMoveUpButton.getLayoutData()).verticalIndent = 10;
    fMoveUpButton.addSelectionListener(
        new SelectionAdapter() {
          @Override
          public void widgetSelected(SelectionEvent e) {
            onMove(true);
          }
        });

    /* Move Provider Down */
    fMoveDownButton = new Button(buttonContainer, SWT.PUSH);
    fMoveDownButton.setText(Messages.CustomizeToolbarDialog_MOVE_DOWN);
    fMoveDownButton.setEnabled(false);
    applyDialogFont(fMoveDownButton);
    setButtonLayoutData(fMoveDownButton);
    fMoveDownButton.addSelectionListener(
        new SelectionAdapter() {
          @Override
          public void widgetSelected(SelectionEvent e) {
            onMove(false);
          }
        });

    /* Restore Defaults */
    fRestoreDefaults = new Button(buttonContainer, SWT.PUSH);
    fRestoreDefaults.setText(Messages.CustomizeToolbarDialog_RESTORE_DEFAULTS);
    applyDialogFont(fRestoreDefaults);
    setButtonLayoutData(fRestoreDefaults);
    ((GridData) fRestoreDefaults.getLayoutData()).grabExcessVerticalSpace = true;
    ((GridData) fRestoreDefaults.getLayoutData()).verticalAlignment = SWT.END;
    fRestoreDefaults.addSelectionListener(
        new SelectionAdapter() {
          @Override
          public void widgetSelected(SelectionEvent e) {
            onRestoreDefaults();
          }
        });

    /* Toolbar Mode */
    Composite modeContainer = new Composite(container, SWT.None);
    modeContainer.setLayout(LayoutUtils.createGridLayout(2, 5, 0));
    modeContainer.setLayoutData(new GridData(SWT.BEGINNING, SWT.FILL, false, false, 2, 1));

    Label showLabel = new Label(modeContainer, SWT.NONE);
    showLabel.setText(Messages.CustomizeToolbarDialog_SHOW);

    fModeViewer = new ComboViewer(modeContainer, SWT.READ_ONLY | SWT.BORDER);
    fModeViewer.setContentProvider(new ArrayContentProvider());
    fModeViewer.setLabelProvider(
        new LabelProvider() {
          @Override
          public String getText(Object element) {
            if (element instanceof CoolBarMode) {
              switch ((CoolBarMode) element) {
                case IMAGE:
                  return Messages.CustomizeToolbarDialog_ICONS;
                case TEXT:
                  return Messages.CustomizeToolbarDialog_TEXT;
                case IMAGE_TEXT_VERTICAL:
                  return Messages.CustomizeToolbarDialog_ICONS_AND_TEXT;
                case IMAGE_TEXT_HORIZONTAL:
                  return Messages.CustomizeToolbarDialog_ICONS_AND_TEXT_SMALL;
              }
            }

            return super.getText(element);
          }
        });

    fModeViewer.addSelectionChangedListener(
        new ISelectionChangedListener() {
          public void selectionChanged(SelectionChangedEvent event) {
            Object selection = ((IStructuredSelection) event.getSelection()).getFirstElement();
            CoolBarMode mode = (CoolBarMode) selection;
            fPreferences.putInteger(DefaultPreferences.TOOLBAR_MODE, mode.ordinal());
          }
        });

    fModeViewer.setInput(CoolBarAdvisor.CoolBarMode.values());
    fModeViewer.setSelection(
        new StructuredSelection(
            CoolBarMode.values()[fPreferences.getInteger(DefaultPreferences.TOOLBAR_MODE)]));

    /* Separator */
    new Label(parent, SWT.SEPARATOR | SWT.HORIZONTAL)
        .setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false));

    applyDialogFont(container);

    return container;
  }