/**
   * Initialize the composite with pre-existing values.
   *
   * @param ruleWrapper the {@link RuleWrapper}.
   */
  public void init(RuleWrapper ruleWrapper) {
    PointSymbolizerWrapper pointSymbolizerWrapper =
        ruleWrapper.getGeometrySymbolizersWrapper().adapt(PointSymbolizerWrapper.class);

    mainComposite = new Composite(parent, SWT.RESIZE);
    mainComposite.setLayout(new GridLayout(2, false));
    mainComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

    Label fontLabel = new Label(mainComposite, SWT.NONE);
    fontLabel.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false));
    fontLabel.setText("Font:"); // $NON-NLS-1$

    fontCombo = new Combo(mainComposite, SWT.DROP_DOWN | SWT.READ_ONLY);
    fontCombo.setItems(getScalableFonts());
    fontCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));

    String characterPath = pointSymbolizerWrapper.getMarkName();
    if (characterPath != null && characterPath.matches("ttf://.+#.+")) { // $NON-NLS-1$
      String[] fontElements = characterPath.substring(6).split("#"); // $NON-NLS-1$
      int index = fontCombo.indexOf(fontElements[0]);
      if (index != -1) {
        fontCombo.select(index);
        characterCode = fontElements[1].substring(2);
      } else {
        fontCombo.select(0);
        characterCode = Integer.toHexString(PLUS_SIGN);
      }
    } else {
      fontCombo.select(0);
      characterCode = Integer.toHexString(PLUS_SIGN);
    }

    fontCombo.addSelectionListener(this);
    fontName = fontCombo.getItem(fontCombo.getSelectionIndex());

    table = new Table(mainComposite, SWT.VIRTUAL | SWT.BORDER | SWT.V_SCROLL | SWT.SIMPLE);
    GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, true);
    gridData.horizontalSpan = 2;
    gridData.heightHint = 300;
    table.setLayoutData(gridData);
    table.setLinesVisible(true);
    table.setRedraw(false);
    Font font = new Font(Display.getCurrent(), fontName, FONT_SIZE, SWT.NORMAL);
    table.setFont(font);

    table.addListener(
        SWT.SetData,
        new Listener() {
          java.awt.Font awtFont = (new java.awt.Font(fontName, java.awt.Font.PLAIN, FONT_SIZE));

          @Override
          public void handleEvent(Event event) {

            final TableItem item = (TableItem) event.item;
            final int index = table.indexOf(item);
            final int ch = +FIRST_CHAR;

            // set a loading message
            item.setText(0, "..."); // $NON-NLS-1$

            Job load =
                new Job("Loading font icon") {

                  @Override
                  protected IStatus run(IProgressMonitor monitor) {

                    Display.getDefault()
                        .syncExec(
                            new Runnable() {

                              public void run() {

                                int character = ch + (index * COLUMNS);

                                for (int cel = 0; cel < COLUMNS; cel++) {

                                  if (character < CHARACTERS) {
                                    if (character > 0xFF && !awtFont.canDisplay(character)) {
                                      item.setText(cel, ""); // $NON-NLS-1$
                                    } else {
                                      item.setText(cel, characterString[character]);
                                    }
                                  }

                                  character++;
                                }
                              }
                            });

                    return Status.OK_STATUS;
                  }
                };

            load.schedule();
          }
        });

    for (int i = 0; i < COLUMNS; i++) {
      new TableColumn(table, SWT.NONE);
    }

    initializeCharacterStringArray();
    table.setItemCount(CHARACTERS / COLUMNS);

    table.getColumn(0).pack();
    int width = table.getColumn(0).getWidth();
    for (int i = 1; i < COLUMNS; i++) {
      table.getColumn(i).setWidth(width);
    }

    // Set redraw back to true so that the table
    // will paint appropriately
    table.setRedraw(true);
    //
    tableCursor = new TableCursor(table, SWT.NONE);
    tableCursor.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WIDGET_LIGHT_SHADOW));

    int index = Integer.parseInt(characterCode, 16);
    int row = index / COLUMNS - 4;
    int col = index % COLUMNS;
    tableCursor.setSelection(row, col);
    tableCursor.setFont(font);
    tableCursor.addSelectionListener(this);

    characterLabel = new Label(mainComposite, SWT.NONE);
    characterLabel.setText(LABEL_PREFIX + characterCode + "      "); // $NON-NLS-1$
  }
Exemplo n.º 2
0
  /**
   * Constructs a new instance of this class given its parent table and a style value describing its
   * behavior and appearance.
   *
   * <p>The style value is either one of the style constants defined in class <code>SWT</code> which
   * is applicable to instances of this class, or must be built by <em>bitwise OR</em>'ing together
   * (that is, using the <code>int</code> "|" operator) two or more of those <code>SWT</code> style
   * constants. The class description lists the style constants that are applicable to the class.
   * Style bits are also inherited from superclasses.
   *
   * @param parent a Table control which will be the parent of the new instance (cannot be null)
   * @param style the style of control to construct
   * @exception IllegalArgumentException
   *     <ul>
   *       <li>ERROR_NULL_ARGUMENT - if the parent is null
   *     </ul>
   *
   * @exception SWTException
   *     <ul>
   *       <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent
   *       <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass
   *     </ul>
   *
   * @see SWT#BORDER
   * @see Widget#checkSubclass()
   * @see Widget#getStyle()
   */
  public TableCursor(Table parent, int style) {
    super(parent, style);
    table = parent;
    setBackground(null);
    setForeground(null);

    listener =
        event -> {
          switch (event.type) {
            case SWT.Dispose:
              onDispose(event);
              break;
            case SWT.FocusIn:
            case SWT.FocusOut:
              redraw();
              break;
            case SWT.KeyDown:
              keyDown(event);
              break;
            case SWT.Paint:
              paint(event);
              break;
            case SWT.Traverse:
              {
                event.doit = true;
                switch (event.detail) {
                  case SWT.TRAVERSE_ARROW_NEXT:
                  case SWT.TRAVERSE_ARROW_PREVIOUS:
                  case SWT.TRAVERSE_RETURN:
                    event.doit = false;
                    break;
                }
                break;
              }
          }
        };
    int[] events =
        new int[] {SWT.Dispose, SWT.FocusIn, SWT.FocusOut, SWT.KeyDown, SWT.Paint, SWT.Traverse};
    for (int i = 0; i < events.length; i++) {
      addListener(events[i], listener);
    }

    tableListener =
        event -> {
          switch (event.type) {
            case SWT.MouseDown:
              tableMouseDown(event);
              break;
            case SWT.FocusIn:
              tableFocusIn(event);
              break;
          }
        };
    table.addListener(SWT.FocusIn, tableListener);
    table.addListener(SWT.MouseDown, tableListener);

    disposeItemListener =
        event -> {
          unhookRowColumnListeners();
          row = null;
          column = null;
          _resize();
        };
    disposeColumnListener =
        event -> {
          unhookRowColumnListeners();
          row = null;
          column = null;
          _resize();
        };
    resizeListener = event -> _resize();
    ScrollBar hBar = table.getHorizontalBar();
    if (hBar != null) {
      hBar.addListener(SWT.Selection, resizeListener);
    }
    ScrollBar vBar = table.getVerticalBar();
    if (vBar != null) {
      vBar.addListener(SWT.Selection, resizeListener);
    }

    getAccessible()
        .addAccessibleControlListener(
            new AccessibleControlAdapter() {
              @Override
              public void getRole(AccessibleControlEvent e) {
                e.detail = ACC.ROLE_TABLECELL;
              }
            });
    getAccessible()
        .addAccessibleListener(
            new AccessibleAdapter() {
              @Override
              public void getName(AccessibleEvent e) {
                if (row == null) return;
                int columnIndex = column == null ? 0 : table.indexOf(column);
                e.result = row.getText(columnIndex);
              }
            });
  }