Search in sources :

Example 26 with Rectangle

use of org.eclipse.swt.graphics.Rectangle in project translationstudio8 by heartsome.

the class Grid method updateScrollbars.

/**
     * Manages the state of the scrollbars when new items are added or the
     * bounds are changed.
     */
private void updateScrollbars() {
    Point preferredSize = getTableSize();
    Rectangle clientArea = getClientArea();
    // area may have been added/removed.
    for (int doublePass = 1; doublePass <= 2; doublePass++) {
        if (preferredSize.y > clientArea.height) {
            vScroll.setVisible(true);
        } else {
            vScroll.setVisible(false);
            vScroll.setValues(0, 0, 1, 1, 1, 1);
        }
        if (preferredSize.x > clientArea.width) {
            hScroll.setVisible(true);
        } else {
            hScroll.setVisible(false);
            hScroll.setValues(0, 0, 1, 1, 1, 1);
        }
        // get the clientArea again with the now visible/invisible
        // scrollbars
        clientArea = getClientArea();
    }
    // if the scrollbar is visible set its values
    if (vScroll.getVisible()) {
        int max = currentVisibleItems;
        int thumb = 1;
        if (!hasDifferingHeights) {
            // in this case, the number of visible rows on screen is constant,
            // so use this as thumb
            thumb = (getVisibleGridHeight() + 1) / (getItemHeight() + 1);
        } else {
            // rows on the last page
            if (getVisibleGridHeight() >= 1) {
                RowRange range = getRowRange(-1, getVisibleGridHeight(), true, true);
                max -= range.rows - 1;
            }
        }
        // if possible, remember selection, if selection is too large, just
        // make it the max you can
        int selection = Math.min(vScroll.getSelection(), max);
        vScroll.setValues(selection, 0, max, thumb, 1, thumb);
    }
    // if the scrollbar is visible set its values
    if (hScroll.getVisible()) {
        if (!columnScrolling) {
            // horizontal scrolling works pixel by pixel
            int hiddenArea = preferredSize.x - clientArea.width + 1;
            // if possible, remember selection, if selection is too large,
            // just
            // make it the max you can
            int selection = Math.min(hScroll.getSelection(), hiddenArea - 1);
            hScroll.setValues(selection, 0, hiddenArea + clientArea.width - 1, clientArea.width, HORZ_SCROLL_INCREMENT, clientArea.width);
        } else {
            // horizontal scrolling is column by column
            int hiddenArea = preferredSize.x - clientArea.width + 1;
            int max = 0;
            int i = 0;
            while (hiddenArea > 0 && i < getColumnCount()) {
                GridColumn col = (GridColumn) displayOrderedColumns.get(i);
                i++;
                if (col.isVisible()) {
                    hiddenArea -= col.getWidth();
                    max++;
                }
            }
            max++;
            // max should never be greater than the number of visible cols
            int visCols = 0;
            for (Iterator iter = columns.iterator(); iter.hasNext(); ) {
                GridColumn element = (GridColumn) iter.next();
                if (element.isVisible()) {
                    visCols++;
                }
            }
            max = Math.min(visCols, max);
            // if possible, remember selection, if selection is too large,
            // just
            // make it the max you can
            int selection = Math.min(hScroll.getSelection(), max);
            hScroll.setValues(selection, 0, max, 1, 1, 1);
        }
    }
}
Also used : Rectangle(org.eclipse.swt.graphics.Rectangle) Iterator(java.util.Iterator) Point(org.eclipse.swt.graphics.Point) Point(org.eclipse.swt.graphics.Point)

Example 27 with Rectangle

use of org.eclipse.swt.graphics.Rectangle in project translationstudio8 by heartsome.

the class TBSearchCellRenderer method overCheck.

private boolean overCheck(GridItem item, Point point) {
    if (isCenteredCheckBoxOnly(item)) {
        point = new Point(point.x, point.y);
        point.x -= getBounds().x;
        point.y -= getBounds().y;
        Rectangle checkBounds = new Rectangle(0, 0, 0, 0);
        checkBounds.x = (getBounds().width - checkRenderer.getBounds().width) / 2;
        checkBounds.y = ((getBounds().height - checkRenderer.getBounds().height) / 2);
        checkBounds.width = checkRenderer.getBounds().width;
        checkBounds.height = checkRenderer.getBounds().height;
        return checkBounds.contains(point);
    } else {
        point = new Point(point.x, point.y);
        point.x -= getBounds().x;
        point.y -= getBounds().y;
        int x = leftMargin;
        if (isTree()) {
            x += getToggleIndent(item);
            x += toggleRenderer.getSize().x + insideMargin;
        }
        if (point.x >= x && point.x < (x + checkRenderer.getSize().x)) {
            int yStart = ((getBounds().height - checkRenderer.getBounds().height) / 2);
            if (point.y >= yStart && point.y < yStart + checkRenderer.getSize().y) {
                return true;
            }
        }
        return false;
    }
}
Also used : Rectangle(org.eclipse.swt.graphics.Rectangle) Point(org.eclipse.swt.graphics.Point) Point(org.eclipse.swt.graphics.Point)

Example 28 with Rectangle

use of org.eclipse.swt.graphics.Rectangle in project translationstudio8 by heartsome.

the class TBSearchCellRenderer method paint.

/**
	 * (non-Javadoc)
	 * @see org.eclipse.nebula.widgets.grid.IRenderer#paint(org.eclipse.swt.graphics.GC, java.lang.Object)
	 */
public void paint(GC gc, Object value) {
    GridItem item = (GridItem) value;
    gc.setFont(item.getFont(getColumn()));
    boolean drawAsSelected = isSelected();
    boolean drawBackground = true;
    if (isCellSelected()) {
        // (!isCellFocus());
        drawAsSelected = true;
    }
    if (drawAsSelected) {
        gc.setBackground(getDisplay().getSystemColor(SWT.COLOR_LIST_SELECTION));
        gc.setForeground(getDisplay().getSystemColor(SWT.COLOR_LIST_SELECTION_TEXT));
    } else {
        if (item.getParent().isEnabled()) {
            Color back = item.getBackground(getColumn());
            if (back != null) {
                gc.setBackground(back);
            } else {
                drawBackground = false;
            }
        } else {
            gc.setBackground(getDisplay().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
        }
        gc.setForeground(item.getForeground(getColumn()));
    }
    if (drawBackground)
        gc.fillRectangle(getBounds().x, getBounds().y, getBounds().width, getBounds().height);
    int x = leftMargin;
    if (isTree()) {
        boolean renderBranches = item.getParent().getTreeLinesVisible();
        if (renderBranches) {
            branchRenderer.setBranches(getBranches(item));
            branchRenderer.setIndent(treeIndent);
            branchRenderer.setBounds(getBounds().x + x, getBounds().y, getToggleIndent(item), // Take into account border
            getBounds().height + 1);
        }
        x += getToggleIndent(item);
        toggleRenderer.setExpanded(item.isExpanded());
        toggleRenderer.setHover(getHoverDetail().equals("toggle"));
        toggleRenderer.setLocation(getBounds().x + x, (getBounds().height - toggleRenderer.getBounds().height) / 2 + getBounds().y);
        if (item.hasChildren())
            toggleRenderer.paint(gc, null);
        if (renderBranches) {
            branchRenderer.setToggleBounds(toggleRenderer.getBounds());
            branchRenderer.paint(gc, null);
        }
        x += toggleRenderer.getBounds().width + insideMargin;
    }
    if (isCheck()) {
        checkRenderer.setChecked(item.getChecked(getColumn()));
        checkRenderer.setGrayed(item.getGrayed(getColumn()));
        if (!item.getParent().isEnabled()) {
            checkRenderer.setGrayed(true);
        }
        checkRenderer.setHover(getHoverDetail().equals("check"));
        if (isCenteredCheckBoxOnly(item)) {
            // Special logic if this column only has a checkbox and is centered
            checkRenderer.setBounds(getBounds().x + ((getBounds().width - checkRenderer.getBounds().width) / 2), (getBounds().height - checkRenderer.getBounds().height) / 2 + getBounds().y, checkRenderer.getBounds().width, checkRenderer.getBounds().height);
        } else {
            checkRenderer.setBounds(getBounds().x + x, (getBounds().height - checkRenderer.getBounds().height) / 2 + getBounds().y, checkRenderer.getBounds().width, checkRenderer.getBounds().height);
            x += checkRenderer.getBounds().width + insideMargin;
        }
        checkRenderer.paint(gc, null);
    }
    Image image = item.getImage(getColumn());
    if (image != null) {
        int y = getBounds().y;
        y += (getBounds().height - image.getBounds().height) / 2;
        gc.drawImage(image, getBounds().x + x, y);
        x += image.getBounds().width + insideMargin;
    }
    if (drawAsSelected) {
        gc.setForeground(getDisplay().getSystemColor(SWT.COLOR_LIST_SELECTION_TEXT));
    } else {
        gc.setForeground(item.getForeground(getColumn()));
    }
    int width = getBounds().width - x - rightMargin;
    int height = getBounds().height - bottomMargin;
    int y = getBounds().y + textTopMargin + topMargin;
    if (textLayout == null) {
        textLayout = new TextLayout(gc.getDevice());
        item.getParent().addDisposeListener(new DisposeListener() {

            public void widgetDisposed(DisposeEvent e) {
                textLayout.dispose();
            }
        });
    }
    textLayout.setOrientation(item.getParent().getOrientation());
    textLayout.setFont(gc.getFont());
    String itemText = item.getText(getColumn());
    textLayout.setText(item.getText(getColumn()));
    textLayout.setAlignment(getAlignment());
    textLayout.setWidth(width < 1 ? 1 : width);
    if (styleColumn != -1 && getColumn() == styleColumn) {
        if (style == null) {
            final Font font = new Font(gc.getDevice(), gc.getFont().getFontData()[0].getName(), gc.getFont().getFontData()[0].getHeight(), SWT.BOLD);
            style = new TextStyle(font, null, null);
            item.getParent().addDisposeListener(new DisposeListener() {

                public void widgetDisposed(DisposeEvent e) {
                    font.dispose();
                }
            });
        }
        if (strText != null) {
            if (blnIsApplyRegular) {
                Pattern pattern = null;
                if (blnIsCaseSensitive) {
                    pattern = Pattern.compile(strText);
                } else {
                    pattern = Pattern.compile(strText, Pattern.CASE_INSENSITIVE);
                }
                Matcher matcher = pattern.matcher(itemText);
                while (matcher.find()) {
                    textLayout.setStyle(style, matcher.start(), matcher.end() - 1);
                }
            } else {
                int index;
                if (blnIsCaseSensitive) {
                    index = itemText.indexOf(strText);
                } else {
                    index = itemText.toUpperCase().indexOf(strText.toUpperCase());
                }
                if (index != -1) {
                    textLayout.setStyle(null, 0, itemText.length() - 1);
                    for (int i = 1; i < strText.length(); i++) {
                        int j = TextUtil.indexOf(itemText, strText, i, blnIsCaseSensitive);
                        if (j != -1) {
                            textLayout.setStyle(style, j, j + strText.length() - 1);
                        } else {
                            break;
                        }
                    }
                }
            }
        }
    }
    if (item.getParent().isAutoHeight()) {
        int textHeight = topMargin + textTopMargin;
        for (int cnt = 0; cnt < textLayout.getLineCount(); cnt++) textHeight += textLayout.getLineBounds(cnt).height;
        textHeight += textBottomMargin + bottomMargin;
        Object obj = item.getData("itemHeight");
        if (getColumn() != item.getParent().getColumnCount() - 1) {
            if (obj != null) {
                int heigth = (Integer) obj;
                textHeight = Math.max(textHeight, heigth);
            }
            item.setData("itemHeight", textHeight);
        } else {
            int heigth = (Integer) obj;
            textHeight = Math.max(textHeight, heigth);
            if (textHeight != item.getHeight()) {
                item.setHeight(textHeight);
            }
            item.setData("itemHeight", null);
        }
    }
    y += getVerticalAlignmentAdjustment(textLayout.getBounds().height, height);
    textLayout.draw(gc, getBounds().x + x, y);
    if (item.getParent().getLinesVisible()) {
        if (isCellSelected()) {
            gc.setForeground(getDisplay().getSystemColor(SWT.COLOR_WIDGET_DARK_SHADOW));
        } else {
            gc.setForeground(item.getParent().getLineColor());
        }
        gc.drawLine(getBounds().x, getBounds().y + getBounds().height, getBounds().x + getBounds().width - 1, getBounds().y + getBounds().height);
        gc.drawLine(getBounds().x + getBounds().width - 1, getBounds().y, getBounds().x + getBounds().width - 1, getBounds().y + getBounds().height);
    }
    if (isCellFocus()) {
        Rectangle focusRect = new Rectangle(getBounds().x, getBounds().y, getBounds().width - 1, getBounds().height);
        gc.setForeground(getDisplay().getSystemColor(SWT.COLOR_LIST_FOREGROUND));
        gc.drawRectangle(focusRect);
        if (isFocus()) {
            focusRect.x++;
            focusRect.width -= 2;
            focusRect.y++;
            focusRect.height -= 2;
            gc.drawRectangle(focusRect);
        }
    }
}
Also used : DisposeListener(org.eclipse.swt.events.DisposeListener) Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) Color(org.eclipse.swt.graphics.Color) Rectangle(org.eclipse.swt.graphics.Rectangle) Image(org.eclipse.swt.graphics.Image) DisposeEvent(org.eclipse.swt.events.DisposeEvent) Point(org.eclipse.swt.graphics.Point) Font(org.eclipse.swt.graphics.Font) TextLayout(org.eclipse.swt.graphics.TextLayout) GridItem(org.eclipse.nebula.widgets.grid.GridItem) TextStyle(org.eclipse.swt.graphics.TextStyle)

Example 29 with Rectangle

use of org.eclipse.swt.graphics.Rectangle in project translationstudio8 by heartsome.

the class TBSearchCellRenderer method getTextBounds.

/**
	 * {@inheritDoc}
	 */
public Rectangle getTextBounds(GridItem item, boolean preferred) {
    int x = leftMargin;
    if (isTree()) {
        x += getToggleIndent(item);
        x += toggleRenderer.getBounds().width + insideMargin;
    }
    if (isCheck()) {
        x += checkRenderer.getBounds().width + insideMargin;
    }
    Image image = item.getImage(getColumn());
    if (image != null) {
        x += image.getBounds().width + insideMargin;
    }
    Rectangle bounds = new Rectangle(x, topMargin + textTopMargin, 0, 0);
    GC gc = new GC(item.getParent());
    gc.setFont(item.getFont(getColumn()));
    Point size = gc.stringExtent(item.getText(getColumn()));
    bounds.height = size.y;
    if (preferred) {
        bounds.width = size.x - 1;
    } else {
        bounds.width = getBounds().width - x - rightMargin;
    }
    gc.dispose();
    return bounds;
}
Also used : Rectangle(org.eclipse.swt.graphics.Rectangle) Point(org.eclipse.swt.graphics.Point) Image(org.eclipse.swt.graphics.Image) GC(org.eclipse.swt.graphics.GC) Point(org.eclipse.swt.graphics.Point)

Example 30 with Rectangle

use of org.eclipse.swt.graphics.Rectangle in project translationstudio8 by heartsome.

the class TermDbManagerDialog method setShellSize.

/**
	 * Changes the shell size to the given size, ensuring that it is no larger than the display bounds.
	 * @param width
	 *            the shell width
	 * @param height
	 *            the shell height
	 */
private void setShellSize(int width, int height) {
    Rectangle preferred = getShell().getBounds();
    preferred.width = width;
    preferred.height = height;
    getShell().setBounds(getConstrainedShellBounds(preferred));
}
Also used : Rectangle(org.eclipse.swt.graphics.Rectangle)

Aggregations

Rectangle (org.eclipse.swt.graphics.Rectangle)315 Point (org.eclipse.swt.graphics.Point)173 Image (org.eclipse.swt.graphics.Image)53 SelectionEvent (org.eclipse.swt.events.SelectionEvent)41 Color (org.eclipse.swt.graphics.Color)31 GC (org.eclipse.swt.graphics.GC)29 SelectionAdapter (org.eclipse.swt.events.SelectionAdapter)24 TableItem (org.eclipse.swt.widgets.TableItem)23 Event (org.eclipse.swt.widgets.Event)20 Menu (org.eclipse.swt.widgets.Menu)20 ArrayList (java.util.ArrayList)18 MenuItem (org.eclipse.swt.widgets.MenuItem)18 Listener (org.eclipse.swt.widgets.Listener)17 SelectionListener (org.eclipse.swt.events.SelectionListener)15 FocusEvent (org.eclipse.swt.events.FocusEvent)14 Composite (org.eclipse.swt.widgets.Composite)14 GridData (org.eclipse.swt.layout.GridData)13 Shell (org.eclipse.swt.widgets.Shell)13 MouseEvent (org.eclipse.swt.events.MouseEvent)12 HashMap (java.util.HashMap)10