001: /*
002: * uDig - User Friendly Desktop Internet GIS client
003: * http://udig.refractions.net
004: * (C) 2004, Refractions Research Inc.
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation;
009: * version 2.1 of the License.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: */
017: package net.refractions.udig.printing.ui.internal.editor.parts;
018:
019: import net.refractions.udig.printing.model.Box;
020: import net.refractions.udig.printing.model.impl.LabelBoxPrinter;
021: import net.refractions.udig.printing.ui.internal.editor.figures.BoxFigure;
022:
023: import org.eclipse.draw2d.IFigure;
024: import org.eclipse.draw2d.geometry.Dimension;
025: import org.eclipse.gef.GraphicalEditPart;
026: import org.eclipse.gef.tools.CellEditorLocator;
027: import org.eclipse.gef.tools.DirectEditManager;
028: import org.eclipse.swt.SWT;
029: import org.eclipse.swt.events.VerifyEvent;
030: import org.eclipse.swt.events.VerifyListener;
031: import org.eclipse.swt.graphics.Font;
032: import org.eclipse.swt.graphics.FontData;
033: import org.eclipse.swt.graphics.GC;
034: import org.eclipse.swt.graphics.Point;
035: import org.eclipse.swt.widgets.Text;
036:
037: /**
038: * Provides ...TODO summary sentence
039: * <p>
040: * TODO Description
041: * </p><p>
042: * Responsibilities:
043: * <ul>
044: * <li>
045: * <li>
046: * </ul>
047: * </p><p>
048: * Example Use:<pre><code>
049: * NodeDirectEditManager x = new NodeDirectEditManager( ... );
050: * TODO code example
051: * </code></pre>
052: * </p>
053: * @author Richard Gould
054: * @since 0.3
055: */
056: public class LabelDirectEditManager extends DirectEditManager {
057:
058: Font scaledFont;
059: protected VerifyListener verifyListener;
060: protected BoxFigure nodeFigure;
061:
062: public LabelDirectEditManager(GraphicalEditPart source,
063: Class editorType, CellEditorLocator locator,
064: BoxFigure nodeFigure) {
065: super (source, editorType, locator);
066: this .nodeFigure = nodeFigure;
067: }
068:
069: protected void bringDown() {
070: Font disposeFont = this .scaledFont;
071: this .scaledFont = null;
072: super .bringDown();
073: if (disposeFont != null) {
074: disposeFont.dispose();
075: }
076: }
077:
078: protected void unhookListeners() {
079: super .unhookListeners();
080: Text text = (Text) getCellEditor().getControl();
081: text.removeVerifyListener(verifyListener);
082: verifyListener = null;
083: }
084:
085: protected void initCellEditor() {
086: verifyListener = new VerifyListener() {
087: public void verifyText(VerifyEvent event) {
088:
089: Text text = (Text) getCellEditor().getControl();
090: String oldText = text.getText();
091: String leftText = oldText.substring(0, event.start);
092: String rightText = oldText.substring(event.end, oldText
093: .length());
094:
095: GC gc = new GC(text);
096: Point size = gc.textExtent(leftText + event.text
097: + rightText);
098: gc.dispose();
099:
100: if (size.x != 0) {
101: size = text.computeSize(size.x, SWT.DEFAULT);
102: }
103: getCellEditor().getControl().setSize(size.x, size.y);
104: }
105: };
106:
107: Text text = (Text) getCellEditor().getControl();
108: text.addVerifyListener(verifyListener);
109: text
110: .setText(((LabelBoxPrinter) ((Box) getEditPart()
111: .getModel()).getBoxPrinter()).getText());
112: getCellEditor().setValue(this .nodeFigure.getText());
113: IFigure figure = ((GraphicalEditPart) getEditPart())
114: .getFigure();
115: scaledFont = figure.getFont();
116: FontData data = scaledFont.getFontData()[0];
117: Dimension fontSize = new Dimension(0, data.getHeight());
118: data.setHeight(fontSize.height);
119: scaledFont = new Font(null, data);
120:
121: text.setFont(scaledFont);
122: text.selectAll();
123: }
124:
125: }
|