Search in sources :

Example 11 with DisposeListener

use of org.eclipse.swt.events.DisposeListener in project translationstudio8 by heartsome.

the class TableHierarchicalExample method initDND.

/**
     * Init a simple drag and drop operation for moving rows in the table.
     * 
     * @param table
     * @param parent
     */
private void initDND(final JaretTable table, Composite parent) {
    // support move only
    int operations = DND.DROP_MOVE;
    final DragSource source = new DragSource(table, operations);
    // Provide data in Text format
    Transfer[] types = new Transfer[] { TextTransfer.getInstance() };
    source.setTransfer(types);
    source.addDragListener(new DragSourceListener() {

        public void dragStart(DragSourceEvent event) {
            // check whether drag occured on the hierarchy column
            IColumn column = table.colForX(event.x);
            if (column != null && table.isHierarchyColumn(column)) {
                // TODO check whether a resize may have
                // higher priority
                // possible row drag
                IRow row = table.rowForY(event.y);
                if (row != null) {
                    // row hit, start row drag
                    _draggedRow = row;
                    // capture the data for internal use
                    // row drag: use row at starting position
                    _parentTableNode = getParent(table.getHierarchicalModel().getRootNode(), (ITableNode) row);
                } else {
                    event.doit = false;
                }
            }
        }

        public void dragSetData(DragSourceEvent event) {
            // Provide the data of the requested type.
            if (TextTransfer.getInstance().isSupportedType(event.dataType)) {
                if (_draggedRow != null) {
                    event.data = "row: " + _draggedRow.getId();
                }
            }
        }

        public void dragFinished(DragSourceEvent event) {
            // for this simple case we do all the manipulations in the drop
            // target
            // this is more of a hack ...
            _draggedRow = null;
        }
    });
    // ////////////////////
    // Drop target
    // moved to the drop target
    operations = DND.DROP_MOVE;
    final DropTarget target = new DropTarget(table, operations);
    // Receive data in Text
    final TextTransfer textTransfer = TextTransfer.getInstance();
    types = new Transfer[] { textTransfer };
    target.setTransfer(types);
    target.addDropListener(new DropTargetListener() {

        public void dragEnter(DropTargetEvent event) {
        }

        public void dragOver(DropTargetEvent event) {
            if (_draggedRow != null) {
            // no drag over effect right now
            }
        }

        public void dragOperationChanged(DropTargetEvent event) {
        }

        public void dragLeave(DropTargetEvent event) {
        }

        public void dropAccept(DropTargetEvent event) {
        }

        public void drop(DropTargetEvent event) {
            // this is kind of a hack ...
            if (textTransfer.isSupportedType(event.currentDataType)) {
                String text = (String) event.data;
                System.out.println("DROP: " + text);
                if (_draggedRow != null) {
                    int destY = Display.getCurrent().map(null, table, event.x, event.y).y;
                    int destX = Display.getCurrent().map(null, table, event.x, event.y).x;
                    IRow overRow = table.rowForY(destY);
                    if (overRow != null) {
                        System.out.println("over row " + overRow.getId());
                        // this is an action from the drag source listener
                        // ...
                        // this has to be done right here because otherwise
                        // the node would be at two places
                        // at the same time causing some redraw trouble ...
                        _parentTableNode.remNode((ITableNode) _draggedRow);
                        ITableNode node = (ITableNode) overRow;
                        node.addNode((ITableNode) _draggedRow);
                    }
                }
            }
        }
    });
    // Dispose listener on parent of timebar viewer to dispose the
    // dragsource and dragtarget BEFORE the timebar
    // viewer
    // this prevents an exception beeing thrown by SWT
    parent.addDisposeListener(new DisposeListener() {

        public void widgetDisposed(DisposeEvent e) {
            source.dispose();
            target.dispose();
        }
    });
}
Also used : DisposeListener(org.eclipse.swt.events.DisposeListener) DropTargetEvent(org.eclipse.swt.dnd.DropTargetEvent) DragSource(org.eclipse.swt.dnd.DragSource) DragSourceListener(org.eclipse.swt.dnd.DragSourceListener) DisposeEvent(org.eclipse.swt.events.DisposeEvent) DragSourceEvent(org.eclipse.swt.dnd.DragSourceEvent) IRow(de.jaret.util.ui.table.model.IRow) DropTargetListener(org.eclipse.swt.dnd.DropTargetListener) IColumn(de.jaret.util.ui.table.model.IColumn) TextTransfer(org.eclipse.swt.dnd.TextTransfer) Transfer(org.eclipse.swt.dnd.Transfer) DropTarget(org.eclipse.swt.dnd.DropTarget) ITableNode(de.jaret.util.ui.table.model.ITableNode) TextTransfer(org.eclipse.swt.dnd.TextTransfer)

Example 12 with DisposeListener

use of org.eclipse.swt.events.DisposeListener in project translationstudio8 by heartsome.

the class StyleTextCellRenderer method draw.

public void draw(GC gc, JaretTable jaretTable, ICellStyle cellStyle, Rectangle drawingArea, IRow row, IColumn column, boolean drawFocus, boolean selected, boolean printing) {
    super.draw(gc, jaretTable, cellStyle, drawingArea, row, column, drawFocus, selected, printing);
    Rectangle drect = drawBorder(gc, cellStyle, drawingArea, printing);
    Rectangle rect = applyInsets(drect);
    String s = convertValue(row, column);
    if (s != null && strStyleText != null) {
        int index = -1;
        if (blnIsCaseSensitive) {
            index = s.toUpperCase().indexOf(strStyleText.toUpperCase());
        } else {
            index = s.indexOf(strStyleText);
        }
        if (index != -1) {
            if (textLayout == null) {
                textLayout = new TextLayout(gc.getDevice());
                jaretTable.getParent().addDisposeListener(new DisposeListener() {

                    public void widgetDisposed(DisposeEvent e) {
                        textLayout.dispose();
                    }
                });
            }
            textLayout.setText(s);
            textLayout.setFont(gc.getFont());
            textLayout.setWidth(rect.width);
            if (style == null) {
                final Color color = new Color(gc.getDevice(), 150, 100, 100);
                final Font font = new Font(gc.getDevice(), gc.getFont().getFontData()[0].getName(), gc.getFont().getFontData()[0].getHeight(), SWT.ITALIC);
                style = new TextStyle(font, color, null);
                jaretTable.getParent().addDisposeListener(new DisposeListener() {

                    public void widgetDisposed(DisposeEvent e) {
                        color.dispose();
                        font.dispose();
                    }
                });
            }
            for (int i = 1; i < strStyleText.length(); i++) {
                int j = indexOf(s, strStyleText, i, blnIsCaseSensitive);
                if (j != -1) {
                    textLayout.setStyle(style, j, j + strStyleText.length() - 1);
                } else {
                    break;
                }
            }
            gc.fillRectangle(rect);
            textLayout.draw(gc, rect.x, rect.y);
            gc.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_LIST_FOREGROUND));
        }
    }
}
Also used : DisposeListener(org.eclipse.swt.events.DisposeListener) TextStyle(org.eclipse.swt.graphics.TextStyle) Color(org.eclipse.swt.graphics.Color) Rectangle(org.eclipse.swt.graphics.Rectangle) DisposeEvent(org.eclipse.swt.events.DisposeEvent) Font(org.eclipse.swt.graphics.Font) TextLayout(org.eclipse.swt.graphics.TextLayout)

Example 13 with DisposeListener

use of org.eclipse.swt.events.DisposeListener in project translationstudio8 by heartsome.

the class HsImageLabel method createControl.

public Composite createControl(Composite parent) {
    parent.addDisposeListener(new DisposeListener() {

        public void widgetDisposed(DisposeEvent e) {
            dispose();
        }
    });
    Composite content = new Composite(parent, SWT.NONE);
    GridLayout contentLayout = new GridLayout(2, false);
    contentLayout.marginWidth = 0;
    contentLayout.marginHeight = 0;
    content.setLayout(contentLayout);
    content.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    this.control = content;
    imageLabel = createImageLabel(content);
    if (imageLabel != null) {
        imageLabel.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, false, false, 1, 1));
    }
    Composite composite = new Composite(content, SWT.NONE);
    GridLayout comLayout = new GridLayout();
    comLayout.marginWidth = 0;
    comLayout.marginHeight = 0;
    composite.setLayout(comLayout);
    composite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    descriptionLabel = createDescriptionLabel(composite);
    if (descriptionLabel != null) {
        descriptionLabel.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    }
    body = new Composite(composite, SWT.NONE);
    GridLayout gd = new GridLayout();
    gd.marginRight = 0;
    gd.marginHeight = 0;
    body.setLayout(gd);
    if (body != null) {
        body.setLayoutData(new GridData(GridData.FILL_BOTH));
    }
    return body;
}
Also used : DisposeListener(org.eclipse.swt.events.DisposeListener) GridLayout(org.eclipse.swt.layout.GridLayout) Composite(org.eclipse.swt.widgets.Composite) GridData(org.eclipse.swt.layout.GridData) DisposeEvent(org.eclipse.swt.events.DisposeEvent)

Example 14 with DisposeListener

use of org.eclipse.swt.events.DisposeListener in project translationstudio8 by heartsome.

the class DefaultCellRenderer method paint.

/**
     * {@inheritDoc}
     */
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;
    }
    int width = getBounds().width - x - rightMargin;
    if (drawAsSelected) {
        gc.setForeground(getDisplay().getSystemColor(SWT.COLOR_LIST_SELECTION_TEXT));
    } else {
        gc.setForeground(item.getForeground(getColumn()));
    }
    if (!isWordWrap()) {
        String text = TextUtils.getShortString(gc, item.getText(getColumn()), width);
        if (getAlignment() == SWT.RIGHT) {
            int len = gc.stringExtent(text).x;
            if (len < width) {
                x += width - len;
            }
        } else if (getAlignment() == SWT.CENTER) {
            int len = gc.stringExtent(text).x;
            if (len < width) {
                x += (width - len) / 2;
            }
        }
        gc.drawString(text, getBounds().x + x, getBounds().y + textTopMargin + topMargin, true);
    } else {
        if (textLayout == null) {
            textLayout = new TextLayout(gc.getDevice());
            item.getParent().addDisposeListener(new DisposeListener() {

                public void widgetDisposed(DisposeEvent e) {
                    textLayout.dispose();
                }
            });
        }
        textLayout.setFont(gc.getFont());
        textLayout.setText(item.getText(getColumn()));
        textLayout.setAlignment(getAlignment());
        textLayout.setWidth(width < 1 ? 1 : width);
        if (item.getParent().isAutoHeight()) {
            // Look through all columns (except this one) to get the max height needed for this item
            int columnCount = item.getParent().getColumnCount();
            int maxHeight = textLayout.getBounds().height + textTopMargin + textBottomMargin;
            for (int i = 0; i < columnCount; i++) {
                GridColumn column = item.getParent().getColumn(i);
                if (i != getColumn() && column.getWordWrap()) {
                    int height = column.getCellRenderer().computeSize(gc, column.getWidth(), SWT.DEFAULT, item).y;
                    maxHeight = Math.max(maxHeight, height);
                }
            }
            // Also look at the row header if necessary
            if (item.getParent().isWordWrapHeader()) {
                int height = item.getParent().getRowHeaderRenderer().computeSize(gc, SWT.DEFAULT, SWT.DEFAULT, item).y;
                maxHeight = Math.max(maxHeight, height);
            }
            if (maxHeight != item.getHeight()) {
                item.setHeight(maxHeight);
            }
        }
        textLayout.draw(gc, getBounds().x + x, getBounds().y + textTopMargin + topMargin);
    }
    if (item.getParent().getLinesVisible()) {
        if (isCellSelected()) {
            //XXX: should be user definable?
            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) GridItem(org.eclipse.nebula.widgets.grid.GridItem) Color(org.eclipse.swt.graphics.Color) Rectangle(org.eclipse.swt.graphics.Rectangle) GridColumn(org.eclipse.nebula.widgets.grid.GridColumn) Image(org.eclipse.swt.graphics.Image) DisposeEvent(org.eclipse.swt.events.DisposeEvent) Point(org.eclipse.swt.graphics.Point) TextLayout(org.eclipse.swt.graphics.TextLayout)

Example 15 with DisposeListener

use of org.eclipse.swt.events.DisposeListener in project translationstudio8 by heartsome.

the class AttributeTextCellRenderer method draw.

public void draw(GC gc, JaretTable jaretTable, ICellStyle cellStyle, Rectangle drawingArea, IRow row, IColumn column, boolean drawFocus, boolean selected, boolean printing) {
    super.draw(gc, jaretTable, cellStyle, drawingArea, row, column, drawFocus, selected, printing);
    // Color bg = gc.getBackground();
    // Color fg = gc.getForeground();
    Rectangle drect = drawBorder(gc, cellStyle, drawingArea, printing);
    Rectangle rect = applyInsets(drect);
    String s = convertValue(row, column);
    if (s != null && mapStyle != null && mapStyle.containsKey(row)) {
        if (selected && !printing) {
            Color color = ColorManager.getColorManager(Display.getCurrent()).getColor(new RGB(218, 218, 218));
            gc.setBackground(color);
        } else {
            gc.setBackground(getBackgroundColor(cellStyle, printing));
        }
        if (textLayout == null) {
            textLayout = new TextLayout(gc.getDevice());
            jaretTable.getParent().addDisposeListener(new DisposeListener() {

                public void widgetDisposed(DisposeEvent e) {
                    textLayout.dispose();
                }
            });
        }
        textLayout.setOrientation(jaretTable.getOrientation());
        textLayout.setText(s);
        textLayout.setFont(gc.getFont());
        textLayout.setWidth(rect.width);
        ArrayList<int[]> lstIndex = mapStyle.get(row);
        if (lstIndex != null) {
            for (int[] arrIndex : lstIndex) {
                if (arrIndex != null && arrIndex.length == 2) {
                    textLayout.setStyle(style, arrIndex[0], arrIndex[1] - 1);
                }
            }
            gc.fillRectangle(rect);
            textLayout.draw(gc, rect.x, rect.y);
            gc.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_LIST_FOREGROUND));
        }
    }
}
Also used : DisposeListener(org.eclipse.swt.events.DisposeListener) Color(org.eclipse.swt.graphics.Color) Rectangle(org.eclipse.swt.graphics.Rectangle) RGB(org.eclipse.swt.graphics.RGB) DisposeEvent(org.eclipse.swt.events.DisposeEvent) TextLayout(org.eclipse.swt.graphics.TextLayout)

Aggregations

DisposeListener (org.eclipse.swt.events.DisposeListener)118 DisposeEvent (org.eclipse.swt.events.DisposeEvent)117 SelectionEvent (org.eclipse.swt.events.SelectionEvent)41 GridData (org.eclipse.swt.layout.GridData)38 SelectionAdapter (org.eclipse.swt.events.SelectionAdapter)33 Image (org.eclipse.swt.graphics.Image)29 Cursor (org.eclipse.swt.graphics.Cursor)26 ToolBar (org.eclipse.swt.widgets.ToolBar)25 ToolItem (org.eclipse.swt.widgets.ToolItem)23 Composite (org.eclipse.swt.widgets.Composite)19 Color (org.eclipse.swt.graphics.Color)17 GridLayout (org.eclipse.swt.layout.GridLayout)17 Point (org.eclipse.swt.graphics.Point)16 ControlEvent (org.eclipse.swt.events.ControlEvent)11 SelectionListener (org.eclipse.swt.events.SelectionListener)10 Control (org.eclipse.swt.widgets.Control)10 TextLayout (org.eclipse.swt.graphics.TextLayout)9 Listener (org.eclipse.swt.widgets.Listener)9 ControlListener (org.eclipse.swt.events.ControlListener)8 MouseEvent (org.eclipse.swt.events.MouseEvent)8