Package research.tool

Source Code of research.tool.TextAreaTool$CustomizeDocument

package research.tool;

import research.FloatingTextArea;
import research.Figure;
import research.TextAreaHolder;

import javax.swing.event.DocumentListener;
import javax.swing.event.DocumentEvent;
import javax.swing.text.BadLocationException;
import javax.swing.text.AttributeSet;
import javax.swing.text.PlainDocument;
import java.awt.event.MouseEvent;
import java.awt.*;
import java.awt.geom.AffineTransform;

/**
* author: zhangwei
* Date: 2003-6-8
* Time: 15:05:58
*/
public class TextAreaTool extends CreationTool {
    protected DocumentListener documentListener = new FloatingTextAreaDocumentListener();

    protected FloatingTextArea fTextField;
    protected TextAreaHolder fTypingTarget;

    private class CustomizeDocument extends PlainDocument {

        public void insertString(int offset, String s, AttributeSet as)
                throws BadLocationException {
            s = s.replaceAll("\t", "    ");
            super.insertString(offset, s, as);
        }
    }

    /**
     * If the pressed figure is a TextHolder it can be edited otherwise
     * a new text figure is created.
     */
    public void mouseDown(MouseEvent e, int x, int y) {
        TextAreaHolder textHolder = null;

        double scale = drawingView.getScale();
        int realX = (int) (e.getX() / scale + 0.5);
        int realY = (int) (e.getY() / scale + 0.5);

        Figure pressedFigure = drawingView.getDrawing().findFigureInside(realX, realY);

        if (pressedFigure instanceof TextAreaHolder) {
            textHolder = (TextAreaHolder) pressedFigure;
            if (!textHolder.acceptsTyping())
                textHolder = null;
        }
        if (textHolder != null) {
            beginEdit(textHolder);
            return;
        }
        if (getTypingTarget() != null) {
            endEdit();
        } else {
            super.mouseDown(e, x, y);
            drawingView.checkDamage();
            textHolder = (TextAreaHolder) createdFigure;
            beginEdit(textHolder);
        }
    }

    public void mouseDrag(MouseEvent e, int x, int y) {
    }

    public void mouseUp(MouseEvent e, int x, int y) {
    }

    public void end() {
        endEdit();
    }

    /**
     * VTextIconTest whether the text tool is currently activated and is displaying
     * a overlay TextFigure for accepting input.
     *
     * @return true, if the text tool has a accepting target TextFigure for its input, false otherwise
     */
    public boolean isActivated() {
        return getTypingTarget() != null;
    }

    protected void beginEdit(TextAreaHolder figure) {
        if (fTextField == null) {
            fTextField = new FloatingTextArea(new CustomizeDocument());
            fTextField.addDocumentListener(documentListener);
        }

        if (figure != getTypingTarget() && getTypingTarget() != null) {
            endEdit();
        }

        Font displayFont = null;

        if (drawingView.getScale() != 1) {
            AffineTransform trans = new AffineTransform();
            trans.scale(drawingView.getScale(), drawingView.getScale());
            displayFont = figure.getFont().deriveFont(trans);
        } else {
            displayFont = figure.getFont();
        }

        fTextField.createOverlay((Container) drawingView, displayFont);
        fTextField.setColumns(figure.getColumns());
        fTextField.setBounds(fieldBounds(figure), figure.getText());

        /**
        fTextField.createOverlay((Container) drawingView, figure.getFont());
        fTextField.setColumns(figure.getColumns());
        fTextField.setBounds(fieldBounds(figure), figure.getText());
        **/
        setTypingTarget(figure);
    }

    protected void endEdit() {
        if (getTypingTarget() != null) {
            if (fTextField.getText().length() > 0) {
                getTypingTarget().setText(fTextField.getText());
            } else {
                drawingView.getDrawing().remove(createdFigure);
            }

            setTypingTarget(null);
            fTextField.endOverlay();

            getPropertyChangeSupport().firePropertyChange(Tool.TOOL_DONE, false, true);
        }
    }
   
    protected Rectangle fieldBounds(TextAreaHolder figure) {
        Rectangle box = figure.textDisplayBox();

        double scale = drawingView.getScale();

        int realX = (int) (box.x * scale);
        int realY = (int) (box.y * scale);
        int realW = (int) (box.width * scale);
        int realH = (int) (box.height * scale);

        box.setRect(realX, realY, realW, realH);

        return box;
    }


    /**
    protected Rectangle fieldBounds(TextAreaHolder figure) {
        Rectangle box = figure.textDisplayBox();

        double scale = drawingView.getScale();

        int realX = (int) (box.x * scale);
        int realY = (int) (box.y * scale);
        int realW = (int) (box.width);
        int realH = (int) (box.height);

        box.setRect(realX, realY, realW, realH);

        return box;
    }
    **/

    protected void setTypingTarget(TextAreaHolder newTypingTarget) {
        fTypingTarget = newTypingTarget;
    }

    protected TextAreaHolder getTypingTarget() {
        return fTypingTarget;
    }

    protected class FloatingTextAreaDocumentListener implements DocumentListener {
        public void changedUpdate(DocumentEvent evt) {
            updateBounds();
        }

        public void insertUpdate(DocumentEvent evt) {
            updateBounds();
        }

        public void removeUpdate(DocumentEvent evt) {
            updateBounds();
        }
    }

    protected void updateBounds() {
        if ((getTypingTarget() != null) && (fTextField != null)) {
            getTypingTarget().setText(fTextField.getText());
            fTextField.updateBounds(fieldBounds(getTypingTarget()));
            drawingView.checkDamage();
        }
    }
}
TOP

Related Classes of research.tool.TextAreaTool$CustomizeDocument

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.