Search in sources :

Example 1 with CellEditorLocator

use of org.eclipse.gef.tools.CellEditorLocator 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 CellEditorLocator

use of org.eclipse.gef.tools.CellEditorLocator in project yamcs-studio by yamcs.

the class PVWidgetSelectionHandle method createDragTracker.

@Override
protected DragTracker createDragTracker() {
    DragEditPartsTracker tracker = new DragEditPartsTracker(getOwner()) {

        @Override
        protected boolean handleButtonDown(int button) {
            if ((button == 1 || button == 3) && widgetModel instanceof IPVWidgetModel) {
                DirectEditManager directEditManager = new PVNameDirectEditManager(getOwner(), new CellEditorLocator() {

                    @Override
                    public void relocate(CellEditor celleditor) {
                        Rectangle rect;
                        int width = 120;
                        if (!pvName.isEmpty() && getTextExtent().width > 120)
                            width = getTextExtent().width + 4;
                        rect = new Rectangle(PVWidgetSelectionHandle.this.getLocation(), new Dimension(width, getTextExtent().height));
                        translateToAbsolute(rect);
                        Text control = (Text) celleditor.getControl();
                        org.eclipse.swt.graphics.Rectangle trim = control.computeTrim(0, 0, 0, 0);
                        rect.translate(trim.x, trim.y);
                        rect.width += trim.width;
                        rect.height += trim.height;
                        control.setBounds(rect.x, rect.y, rect.width, rect.height);
                    }
                });
                directEditManager.show();
            }
            return true;
        }
    };
    tracker.setDefaultCursor(getCursor());
    return tracker;
}
Also used : CellEditorLocator(org.eclipse.gef.tools.CellEditorLocator) IPVWidgetModel(org.csstudio.opibuilder.model.IPVWidgetModel) CellEditor(org.eclipse.jface.viewers.CellEditor) PVNameTextCellEditor(org.csstudio.opibuilder.visualparts.PVNameTextCellEditor) Rectangle(org.eclipse.draw2d.geometry.Rectangle) DirectEditManager(org.eclipse.gef.tools.DirectEditManager) Text(org.eclipse.swt.widgets.Text) Dimension(org.eclipse.draw2d.geometry.Dimension) DragEditPartsTracker(org.eclipse.gef.tools.DragEditPartsTracker)

Aggregations

Dimension (org.eclipse.draw2d.geometry.Dimension)2 Rectangle (org.eclipse.draw2d.geometry.Rectangle)2 CellEditorLocator (org.eclipse.gef.tools.CellEditorLocator)2 CellEditor (org.eclipse.jface.viewers.CellEditor)2 IPVWidgetModel (org.csstudio.opibuilder.model.IPVWidgetModel)1 PVNameTextCellEditor (org.csstudio.opibuilder.visualparts.PVNameTextCellEditor)1 DirectEditManager (org.eclipse.gef.tools.DirectEditManager)1 DragEditPartsTracker (org.eclipse.gef.tools.DragEditPartsTracker)1 ILabelDelegate (org.eclipse.gmf.runtime.diagram.ui.label.ILabelDelegate)1 StyledText (org.eclipse.swt.custom.StyledText)1 Point (org.eclipse.swt.graphics.Point)1 Text (org.eclipse.swt.widgets.Text)1