Search in sources :

Example 1 with ILabelDelegate

use of org.eclipse.gmf.runtime.diagram.ui.label.ILabelDelegate in project statecharts by Yakindu.

the class XtextDirectEditManager method getTextCellEditorLocator.

/**
 * @param source
 *            the <code>ITextAwareEditPart</code> to determine the cell
 *            editor for
 * @return the <code>CellEditorLocator</code> that is appropriate for the
 *         source <code>EditPart</code>
 */
public static CellEditorLocator getTextCellEditorLocator(final IXtextAwareEditPart source) {
    final ILabelDelegate label = (ILabelDelegate) source.getAdapter(ILabelDelegate.class);
    if (label != null) {
        return new CellEditorLocator() {

            public void relocate(CellEditor celleditor) {
                StyledText text = (StyledText) celleditor.getControl();
                Rectangle rect = label.getTextBounds().getCopy();
                if (label.getText().length() <= 0) {
                    // if there is no text, let's assume a default size
                    // of one character because it looks silly when the cell
                    // editor is tiny.
                    // $NON-NLS-1$
                    rect.setSize(TextUtilities.INSTANCE.getTextExtents("a", text.getFont()));
                    if (label.isTextWrapOn()) {
                        // justification (i.e. where the cursor will be
                        if (label.getTextJustification() == PositionConstants.RIGHT) {
                            rect.translate(-rect.width, 0);
                        } else if (label.getTextJustification() == PositionConstants.CENTER) {
                            rect.translate(-rect.width / 2, 0);
                        }
                    }
                }
                if (!text.getFont().isDisposed()) {
                    // exception.
                    if (label.isTextWrapOn()) {
                        // When zoomed in, the height of this rectangle is
                        // not
                        // sufficient because the text is shifted downwards
                        // a
                        // little bit. Add some to the height to compensate
                        // for
                        // this. I'm not sure why this is happening, but I
                        // can
                        // see the text shifting down even in a label on a
                        // GEF
                        // logic diagram when zoomed into 400%.
                        int charHeight = FigureUtilities.getFontMetrics(text.getFont()).getHeight();
                        rect.resize(0, charHeight / 2);
                    } else {
                        rect.setSize(new Dimension(text.computeSize(SWT.DEFAULT, SWT.DEFAULT)));
                        // If SWT.WRAP is not passed in as a style of the
                        // TextCellEditor, then for some reason the first
                        // character disappears upon entering the second
                        // character. This should be investigated and an
                        // SWT bug logged.
                        int avr = FigureUtilities.getFontMetrics(text.getFont()).getAverageCharWidth();
                        rect.setSize(new Dimension(text.computeSize(SWT.DEFAULT, SWT.DEFAULT)).expand(avr * 2, 0));
                    }
                }
                org.eclipse.swt.graphics.Rectangle newRect = text.computeTrim(rect.x, rect.y, rect.width, rect.height);
                if (!newRect.equals(text.getBounds())) {
                    text.setBounds(newRect.x, newRect.y, newRect.width, newRect.height);
                }
            }
        };
    }
    // return a default figure locator
    return new CellEditorLocator() {

        public void relocate(CellEditor celleditor) {
            StyledText text = (StyledText) celleditor.getControl();
            Rectangle rect = source.getFigure().getBounds().getCopy();
            // Added min width because it looks silly if the label has a
            // width of 0
            rect.width = Math.max(rect.width, LABEL_MIN_WIDTH);
            if (!text.isDisposed() && text.getFont() != null && !text.getFont().isDisposed()) {
                Dimension fontMetrics = TextUtilities.INSTANCE.getTextExtents("a", text.getFont()).getCopy();
                source.getFigure().translateToRelative(fontMetrics);
                rect.height = Math.max(rect.height, fontMetrics.height);
            }
            source.getFigure().translateToAbsolute(rect);
            if (!rect.equals(new Rectangle(text.getBounds()))) {
                text.setBounds(rect.x, rect.y, rect.width, rect.height);
            }
        }
    };
}
Also used : CellEditorLocator(org.eclipse.gef.tools.CellEditorLocator) StyledText(org.eclipse.swt.custom.StyledText) CellEditor(org.eclipse.jface.viewers.CellEditor) Rectangle(org.eclipse.draw2d.geometry.Rectangle) ILabelDelegate(org.eclipse.gmf.runtime.diagram.ui.label.ILabelDelegate) Dimension(org.eclipse.draw2d.geometry.Dimension) Point(org.eclipse.swt.graphics.Point)

Example 2 with ILabelDelegate

use of org.eclipse.gmf.runtime.diagram.ui.label.ILabelDelegate in project statecharts by Yakindu.

the class XtextDirectEditManager method unhookListeners.

/*
	 * Overrides super unhookListeners to set listeners attached flag This
	 * method prevents unhooking listeners twice if the UI thread is blocked.
	 * For example, a validation dialog may block the thread
	 */
protected void unhookListeners() {
    if (listenersAttached) {
        listenersAttached = false;
        super.unhookListeners();
        ILabelDelegate label = (ILabelDelegate) getEditPart().getAdapter(ILabelDelegate.class);
        if (label != null && textFigureListener != null) {
            ((IFigure) ((WrappingLabel) getEditPart().getFigure()).getTextFigure().getChildren().get(0)).removeAncestorListener(textFigureListener);
            textFigureListener = null;
        }
    }
}
Also used : ILabelDelegate(org.eclipse.gmf.runtime.diagram.ui.label.ILabelDelegate) IFigure(org.eclipse.draw2d.IFigure)

Example 3 with ILabelDelegate

use of org.eclipse.gmf.runtime.diagram.ui.label.ILabelDelegate in project statecharts by Yakindu.

the class XtextDirectEditManager method hookListeners.

protected void hookListeners() {
    super.hookListeners();
    // TODO: This gets around the problem of the cell editor not growing big
    // enough when in autosize mode because it doesn't listen to textflow
    // size changes. The superclass should be modified to not assume we want
    // to listen to the editpart's figure.
    ILabelDelegate label = (ILabelDelegate) getEditPart().getAdapter(ILabelDelegate.class);
    if (label != null && getEditPart().getFigure() instanceof WrappingLabel) {
        textFigureListener = new AncestorListener.Stub() {

            public void ancestorMoved(IFigure ancestor) {
                getLocator().relocate(getCellEditor());
            }
        };
        ((IFigure) ((WrappingLabel) getEditPart().getFigure()).getTextFigure().getChildren().get(0)).addAncestorListener(textFigureListener);
    }
}
Also used : ILabelDelegate(org.eclipse.gmf.runtime.diagram.ui.label.ILabelDelegate) AncestorListener(org.eclipse.draw2d.AncestorListener) WrappingLabel(org.eclipse.gmf.runtime.draw2d.ui.figures.WrappingLabel) IFigure(org.eclipse.draw2d.IFigure)

Aggregations

ILabelDelegate (org.eclipse.gmf.runtime.diagram.ui.label.ILabelDelegate)3 IFigure (org.eclipse.draw2d.IFigure)2 AncestorListener (org.eclipse.draw2d.AncestorListener)1 Dimension (org.eclipse.draw2d.geometry.Dimension)1 Rectangle (org.eclipse.draw2d.geometry.Rectangle)1 CellEditorLocator (org.eclipse.gef.tools.CellEditorLocator)1 WrappingLabel (org.eclipse.gmf.runtime.draw2d.ui.figures.WrappingLabel)1 CellEditor (org.eclipse.jface.viewers.CellEditor)1 StyledText (org.eclipse.swt.custom.StyledText)1 Point (org.eclipse.swt.graphics.Point)1