Back to index

moin  1.9.0~rc2
TextTool.java
Go to the documentation of this file.
00001 /*
00002  * @(#)TextTool.java 5.1
00003  *
00004  */
00005 
00006 package CH.ifa.draw.figures;
00007 
00008 import java.awt.*;
00009 import java.awt.event.*;
00010 import CH.ifa.draw.framework.*;
00011 import CH.ifa.draw.standard.*;
00012 import CH.ifa.draw.util.FloatingTextField;
00013 
00024 public class TextTool extends CreationTool {
00025 
00026     private FloatingTextField   fTextField;
00027     private TextHolder  fTypingTarget;
00028 
00029     public TextTool(DrawingView view, Figure prototype) {
00030         super(view, prototype);
00031     }
00032 
00037     public void mouseDown(MouseEvent e, int x, int y)
00038     {
00039            Figure pressedFigure;
00040            TextHolder textHolder = null;
00041 
00042            pressedFigure = drawing().findFigureInside(x, y);
00043            if (pressedFigure instanceof TextHolder) {
00044                textHolder = (TextHolder) pressedFigure;
00045                if (!textHolder.acceptsTyping())
00046                    textHolder = null;
00047         }
00048            if (textHolder != null) {
00049                beginEdit(textHolder);
00050                return;
00051            }
00052            if (fTypingTarget != null) {
00053                editor().toolDone();
00054                endEdit();
00055            } else {
00056            super.mouseDown(e, x, y);
00057            textHolder = (TextHolder)createdFigure();
00058            beginEdit(textHolder);
00059         }
00060     }
00061 
00062     public void mouseDrag(MouseEvent e, int x, int y) {
00063     }
00064 
00065     public void mouseUp(MouseEvent e, int x, int y) {
00066     }
00067 
00071     public void deactivate() {
00072         super.deactivate();
00073         endEdit();
00074     }
00075 
00079     public void activate() {
00080         super.activate();
00081         view().clearSelection();
00082         // JDK1.1 TEXT_CURSOR has an incorrect hot spot
00083         //view.setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR));
00084     }
00085 
00086     protected void beginEdit(TextHolder figure) {
00087         if (fTextField == null)
00088             fTextField = new FloatingTextField();
00089 
00090        if (figure != fTypingTarget && fTypingTarget != null)
00091            endEdit();
00092 
00093         fTextField.createOverlay((Container)view(), figure.getFont());
00094        fTextField.setBounds(fieldBounds(figure), figure.getText());
00095        fTypingTarget = figure;
00096     }
00097 
00098     protected void endEdit() {
00099        if (fTypingTarget != null) {
00100            if (fTextField.getText().length() > 0)
00101               fTypingTarget.setText(fTextField.getText());
00102            else
00103               drawing().remove((Figure)fTypingTarget);
00104            fTypingTarget = null;
00105            fTextField.endOverlay();
00106            view().checkDamage();
00107        }
00108     }
00109 
00110     private Rectangle fieldBounds(TextHolder figure) {
00111        Rectangle box = figure.textDisplayBox();
00112        int nRows = figure.overlayRows();
00113        int nCols = figure.overlayColumns();
00114         Dimension d = fTextField.getPreferredSize(nRows, nCols);
00115         return new Rectangle(box.x, box.y, d.width, d.height);
00116     }
00117 }
00118