Package de.FeatureModellingTool.GraphicalEditor

Source Code of de.FeatureModellingTool.GraphicalEditor.ResourceComponentCreationTool$ResourceComponentDocument

package de.FeatureModellingTool.GraphicalEditor;

import research.FloatingTextField;
import research.TextHolder;
import research.Figure;
import research.tool.Tool;
import research.tool.CreationTool;

import javax.swing.event.DocumentListener;
import javax.swing.event.DocumentEvent;
import javax.swing.text.PlainDocument;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;

import de.FeatureModellingTool.FeatureModel.*;
import de.reuse.GroupMap;

import java.beans.PropertyChangeEvent;
import java.awt.event.MouseEvent;
import java.awt.geom.AffineTransform;
import java.awt.*;

/**
* Created by IntelliJ IDEA.
* User: saturn
* Date: 2004-4-25
* Time: 18:08:54
* To change this template use File | Settings | File Templates.
*/
class ResourceComponentCreationTool extends CreationTool implements ConstantDefinition {
    protected DocumentListener documentListener = new FloatingTextFieldDocumentListener();

    protected FloatingTextField fTextField;
    protected TextHolder fTypingTarget;

    private InteractionModel interactionModel = null;
    private ResourceComponentEditor resourceComponentEditor = null;
    private InteractionModelEditor interactionModelEditor = null;

    private GroupMap idToFigureMap = null;

    private class ResourceComponentDocument extends PlainDocument {

        public void insertString(int offset, String s, AttributeSet as)
                throws BadLocationException {
            s = s.replace(' ', '-');
            super.insertString(offset, s, as);
        }
    }

    protected void contextChange(PropertyChangeEvent e) {
        super.contextChange(e);
        String propertyName = e.getPropertyName();
        if (INTERACTION_MODEL.equals(propertyName)) {
            end();
            interactionModel = (InteractionModel) e.getNewValue();
        } else if (RESOURCE_COMPONENT_EDITOR.equals(propertyName)) {
            end();
            resourceComponentEditor = (ResourceComponentEditor) e.getNewValue();
        } else if (INTERACTION_MODEL_EDITOR.equals(propertyName)) {
            end();
            interactionModelEditor = (InteractionModelEditor) e.getNewValue();
        } else if (ID_TO_FIGURE_MAP.equals(propertyName)) {
            idToFigureMap = (GroupMap) e.getNewValue();
        }
    }

    public boolean isEnabled() {
        if (!super.isEnabled()) return false;
        if (interactionModel == null) return false;
        if (resourceComponentEditor == null) return false;
        if (interactionModelEditor == null) return false;

        return true;
    }

    /**
     * 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) {
        //if(!isEnabled()) return;

        if ((!isActivated()) && (!isEnabled()))
            return;

        boolean flag = false;

        if (getTypingTarget() != null) {//������ǰ�ı༭״̬
            endEdit(false);
            flag = true;
        }

        TextHolder 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 ResourceComponentFigure) {
            textHolder = (TextHolder) pressedFigure;
            if (!textHolder.acceptsTyping())
                textHolder = null;
        }

        if (textHolder != null) {//��ʼ�༭��갴�´���textHolder����������
            beginEdit(textHolder);
            return;
        }

        //if (getTypingTarget() != null) {//������ǰ�ı༭״̬
        //    endEdit();
        if (flag) {
            getPropertyChangeSupport().firePropertyChange(Tool.TOOL_DONE, false, true);
            flag = false;
        } else {//�����µ��ı�ͼԪ�����༭�����������
            super.mouseDown(e, x, y);

            //�� interaction model �������Դ����
            ResourceComponent currentRC = interactionModelEditor.addResourceComponent("");
            createdFigure.setAttribute("id", currentRC.getID());

            //����id��figure��ӳ���
            if (idToFigureMap != null) {
                idToFigureMap.add(currentRC.getID(), createdFigure);
            }

            drawingView.checkDamage();
            textHolder = (TextHolder) 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(TextHolder figure) {
        if (fTextField == null) {
            fTextField = new FloatingTextField(new ResourceComponentDocument());
            fTextField.addDocumentListener(documentListener);
        }

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


        AffineTransform trans = new AffineTransform();
        trans.scale(drawingView.getScale(), drawingView.getScale());
        Font displayFont = figure.getFont().deriveFont(trans);


        fTextField.createOverlay((Container) drawingView, displayFont);
        fTextField.setBackground((Color) ((Figure) figure).getAttribute("fillColor"));
        fTextField.setBounds(fieldBounds(figure), figure.getText());

        setTypingTarget(figure);
    }

    public void endEdit() {
        endEdit(true);
    }

    protected void endEdit(boolean release) {
        if (getTypingTarget() != null) {

            getTypingTarget().setText(fTextField.getText());

            setTypingTarget(null);
            fTextField.endOverlay();

            if (release)
                getPropertyChangeSupport().firePropertyChange(Tool.TOOL_DONE, false, true);
        }
    }


    protected Rectangle fieldBounds(TextHolder 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 + 0.5);
        int realH = (int) (box.height * scale + 0.5);

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

        return box;
    }

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

    protected TextHolder getTypingTarget() {
        return fTypingTarget;
    }

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

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

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


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

    /**
     * ���µ�ǰͼԪ���������������
     */
    protected void updateName() {
        if ((getTypingTarget() != null) && (fTextField != null)) {
            Figure figure = (Figure) getTypingTarget();
            String id = (String) figure.getAttribute("id");
            ResourceComponent rc = interactionModel.getResourceComponent(id);
            resourceComponentEditor.setName(rc, getTypingTarget().getText());
        }
    }
}
TOP

Related Classes of de.FeatureModellingTool.GraphicalEditor.ResourceComponentCreationTool$ResourceComponentDocument

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.