Package de.FeatureModellingTool.Customize.ui

Source Code of de.FeatureModellingTool.Customize.ui.CustomizationExplorerImplementation$CutFeatureAction

package de.FeatureModellingTool.Customize.ui;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.HashMap;
import java.util.Iterator;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.tree.TreePath;

import org.jdesktop.swingx.JXTreeTable;

import research.DrawingView;
import research.Figure;
import research.FigureEnumeration;
import de.FeatureModellingTool.Customize.ConstantDefinition;
import de.FeatureModellingTool.Customize.CustomizationModel;
import de.FeatureModellingTool.Customize.CustomizationVersion;
import de.FeatureModellingTool.DupHelper.ConstraintTransformHelper;
import de.FeatureModellingTool.FeatureModel.CompositeConstraintEditor;
import de.FeatureModellingTool.FeatureModel.ConstraintModel;
import de.FeatureModellingTool.FeatureModel.ConstraintModelEditor;
import de.FeatureModellingTool.FeatureModel.FeatureEditor;
import de.FeatureModellingTool.FeatureModel.FeatureModel;
import de.FeatureModellingTool.FeatureModel.FeatureModelEditor;
import de.FeatureModellingTool.FeatureModel.GroupConstraintEditor;
import de.reuse.Context;
import de.reuse.ContextImplementation;

public class CustomizationExplorerImplementation implements CustomizationExplorer {

    private static final String INNER_SELECTEDNODE = "innerSelectedNode";
    private Component container = null;
    private CustomizationVersionInfoEditor cvie = null;    // ��������
    protected final ContextImplementation context = new ContextImplementation();    // ����_�汾��
    private CustomizationVersionTreeModel customizationVersionTreeModel = null;
    private JXTreeTable treeTable = null;    // ����_�����˵�
    protected JPopupMenu popupMenu = null;
    protected NewCustomizationVersionAction newCustomizationVersionAction = null;
    protected UpdateCustomizationVersionAction updateCustomizationVersionAction = null;
    protected RemoveCustomizationVersionAction removeCustomizationVersionAction = null;
    protected CutFeatureAction cutFeatureAction = null;
    /** ��װ�˶Զ���ģ�͵IJ��� **/
    protected CustomizationModel customizationModel = null;

    public CustomizationExplorerImplementation() {
        CustomizationVersionTreeNode root = new CustomizationVersionTreeNode(null);
        customizationVersionTreeModel = new CustomizationVersionTreeModel(root);
        treeTable = new JXTreeTable(customizationVersionTreeModel);
        treeTable.setRootVisible(true);
        treeTable.addMouseListener(new CustomizationVersionExplorerProbe());

        context.addContextChangeListener(new ContextChangeListener());


        newCustomizationVersionAction = new NewCustomizationVersionAction();
        updateCustomizationVersionAction = new UpdateCustomizationVersionAction();
        removeCustomizationVersionAction = new RemoveCustomizationVersionAction();
        cutFeatureAction = new CutFeatureAction();

        popupMenu = new JPopupMenu();
        popupMenu.add(newCustomizationVersionAction);
        popupMenu.add(updateCustomizationVersionAction);
        popupMenu.add(removeCustomizationVersionAction);
        popupMenu.add(cutFeatureAction);

        this.cvie = new CustomizationVersionInfoEditor();
    }

    public Context getContext() {
        return context;
    }

    protected class ContextChangeListener implements PropertyChangeListener {

        public void propertyChange(PropertyChangeEvent e) {
            String propertyName = e.getPropertyName();

            if (ConstantDefinition.CUSTOMIZATION_MODEL.equals(propertyName)) {
                customizationModel = (CustomizationModel) e.getNewValue();

                refreshTreeNodes(customizationModel);
            }

            if (ConstantDefinition.CONTAINER.equals(propertyName)) {
                container = (Component) e.getNewValue();
            }
        }
    }

    private void refreshTreeNodes(CustomizationModel customizationModel) {
        CustomizationVersionTreeNode root = (CustomizationVersionTreeNode) treeTable.getTreeTableModel().getRoot();

        for (Iterator<CustomizationVersion> itCV = this.customizationModel.getRootCustomizationVersions().iterator(); itCV.hasNext();) {
            this.refreshTreeNodes(root, itCV.next());
        }
    }

    private void refreshTreeNodes(CustomizationVersionTreeNode parentNode, CustomizationVersion customizationVersion) {
        CustomizationVersionTreeNode thisNode = new CustomizationVersionTreeNode(customizationVersion);
        customizationVersionTreeModel.insertNodeInto(thisNode, parentNode, parentNode.getChildCount());

        for (Iterator<String> itCVId = customizationVersion.getChildVersionIds().iterator(); itCVId.hasNext();) {
            CustomizationVersion cvChild = customizationVersion.getChildVersionById(itCVId.next());
            this.refreshTreeNodes(thisNode, cvChild);
        }
    }

    public JComponent getExplorerComponent() {
        return treeTable;
    }

    protected class NewCustomizationVersionAction extends AbstractAction {

        protected static final long serialVersionUID = 0L;

        public NewCustomizationVersionAction() {
            this.putValue(Action.NAME, "�½����ư汾");
        }

        public void actionPerformed(ActionEvent e) {
            if (!isEnabled()) {
                return;
            }
            CustomizationVersionTreeNode cvtn = (CustomizationVersionTreeNode) getContext().getValue(INNER_SELECTEDNODE);

            cvie.setId("");
            cvie.setEditName("");

            int result = JOptionPane.showConfirmDialog(container, cvie,
                    "�½����ư汾", JOptionPane.OK_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE);

            if (result == JOptionPane.OK_OPTION) {
                String parentId = null;
                if (cvtn != null && cvtn.getCustomizationVersion() != null) {
                    parentId = cvtn.getCustomizationVersion().getId();
                }

                //TODO: ����id�������򴴽���id
                String newId = (new java.text.SimpleDateFormat("yyyyMMddHHmmss")).format(new java.util.Date());

                String editName = cvie.getEditName();
                String bindingTime = cvie.getBindingTime();

                CustomizationVersion newCV = newCustomizationVersion(parentId, newId, editName, bindingTime);

                CustomizationVersionTreeNode newCVTN = new CustomizationVersionTreeNode(newCV);
                if (cvtn == null) {
                    CustomizationVersionTreeNode root = (CustomizationVersionTreeNode) customizationVersionTreeModel.getRoot();
                    customizationVersionTreeModel.insertNodeInto(newCVTN, root, root.getChildCount());
                } else {
                    customizationVersionTreeModel.insertNodeInto(newCVTN, cvtn, cvtn.getChildCount());
                }
               
                treeTable.expandRow(treeTable.getSelectedRow());
            }
        }

        @Override
        public void firePropertyChange(String propertyName, Object oldValue, Object newValue) {
            super.firePropertyChange(propertyName, oldValue, newValue);
        }
    }

    protected class UpdateCustomizationVersionAction extends AbstractAction {

        public UpdateCustomizationVersionAction() {
            this.putValue(Action.NAME, "�޸Ķ��ư汾");
        }

        @Override
        public boolean isEnabled() {
            if (!super.isEnabled()) {
                return false;
            }

            CustomizationVersionTreeNode selectedNode = (CustomizationVersionTreeNode) getContext().getValue(INNER_SELECTEDNODE);

            return selectedNode != null && selectedNode != customizationVersionTreeModel.getRoot();
        }

        public void actionPerformed(ActionEvent e) {
            if (!isEnabled()) {
                return;
            }
            CustomizationVersionTreeNode cvtn = (CustomizationVersionTreeNode) getContext().getValue(INNER_SELECTEDNODE);

            if (cvtn == null) {
                return;
            }

            cvie.setId(cvtn.getCustomizationVersion().getId());
            cvie.setEditName(cvtn.getCustomizationVersion().getName());
            cvie.setBindingTime(cvtn.getCustomizationVersion().getBindingTime().getName());

            int result = JOptionPane.showConfirmDialog(container, cvie,
                    "�޸Ķ��ư汾", JOptionPane.OK_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE);

            if (result == JOptionPane.OK_OPTION) {
                updateCustomizationVersion(cvie.getId(), cvie.getEditName(), cvie.getBindingTime());

                treeTable.updateUI();
            }
        }

        @Override
        public void firePropertyChange(String propertyName, Object oldValue, Object newValue) {
            super.firePropertyChange(propertyName, oldValue, newValue);
        }
    }

    protected class RemoveCustomizationVersionAction extends AbstractAction {

        public RemoveCustomizationVersionAction() {
            this.putValue(Action.NAME, "ɾ�����ư汾");
        }

        public boolean isEnabled() {
            if (!super.isEnabled()) {
                return false;
            }
            CustomizationVersionTreeNode selectedNode = (CustomizationVersionTreeNode) getContext().getValue(INNER_SELECTEDNODE);
            return selectedNode != null && selectedNode != customizationVersionTreeModel.getRoot() && selectedNode.getChildCount() == 0;
        }

        public void actionPerformed(ActionEvent e) {
            if (!isEnabled()) {
                return;
            }
            CustomizationVersionTreeNode cvtn = (CustomizationVersionTreeNode) getContext().getValue(INNER_SELECTEDNODE);
            if (cvtn == null || cvtn.getChildCount() > 0) {
                return;
            }

            removeCustomizationVersion(cvtn.getCustomizationVersion().getId());

            customizationVersionTreeModel.removeNodeFromParent(cvtn);

        }

        @Override
        public void firePropertyChange(String propertyName, Object oldValue, Object newValue) {
            super.firePropertyChange(propertyName, oldValue, newValue);
        }
    }

    protected class CutFeatureAction extends AbstractAction {

        protected static final long serialVersionUID = 0L;

        public CutFeatureAction() {
            this.putValue(Action.NAME, "�ü�����ģ��");
        }

        public boolean isEnabled() {
            if (!super.isEnabled()) {
                return false;
            }
            CustomizationVersionTreeNode selectedNode = (CustomizationVersionTreeNode) getContext().getValue(INNER_SELECTEDNODE);
            return selectedNode != null && selectedNode != customizationVersionTreeModel.getRoot() && selectedNode.getChildCount() == 0;
        }

        public void actionPerformed(ActionEvent e) {
            if (!isEnabled()) {
                return;
            }
            CustomizationVersionTreeNode cvtn = (CustomizationVersionTreeNode) getContext().getValue(INNER_SELECTEDNODE);
            if (cvtn == null || cvtn.getChildCount() > 0) {
                return;
            }

            FeatureModel fm = (FeatureModel) getContext().getValue("fm");
            FeatureEditor featureEditor = (FeatureEditor) getContext().getValue("fe");
            FeatureModelEditor fme = (FeatureModelEditor) getContext().getValue("fme");
            ConstraintModel cm = (ConstraintModel) getContext().getValue("cm");
            ConstraintModelEditor cme = (ConstraintModelEditor) getContext().getValue("cme");
            CompositeConstraintEditor cce = (CompositeConstraintEditor) getContext().getValue("cce");
            GroupConstraintEditor gce = (GroupConstraintEditor) getContext().getValue("gce");
            DrawingView[] dvs = (DrawingView[]) getContext().getValue("dvs");

            HashMap<String, Figure> mFigure = new HashMap<String, Figure>();
            HashMap<Figure, DrawingView> mDrawingView = new HashMap<Figure, DrawingView>();
            for (int i = 0; i < dvs.length; i++) {
                DrawingView dv = dvs[i];
                for (FigureEnumeration fe = dv.getDrawing().getFigures(); fe.hasMoreElements();) {
                    Figure figure = fe.nextFigure();
                    if (!mFigure.containsKey(figure.getAttribute("id"))) {
                        mFigure.put((String) figure.getAttribute("id"), figure);
                    }
                    mDrawingView.put(figure, dv);
                }
            }

            CustomizationVersion cv = cvtn.getCustomizationVersion();
            ConstraintTransformHelper.transformConstraint(fm, featureEditor, fme, cm, cme, cce, gce, dvs, cv, mFigure, mDrawingView);
        }

        @Override
        public void firePropertyChange(String propertyName, Object oldValue, Object newValue) {
            super.firePropertyChange(propertyName, oldValue, newValue);
        }
    }

    private void setCurrentCustomizationVersionId(String currentCustomizationVersionId) {
        this.customizationModel.setCurrentCustomizationVersionId(currentCustomizationVersionId);
    }

    private CustomizationVersion newCustomizationVersion(String parentId, String id, String name, String bindingTimeId) {
        return this.customizationModel.newCustomizationVersion(parentId, id, name, bindingTimeId);
    }

    private void updateCustomizationVersion(String id, String name, String bindingTimeId) {
        CustomizationVersion target = this.customizationModel.getCustomizationVersion(id);
        target.setName(name);
        target.setBindingTimeId(bindingTimeId);
    }

    private boolean removeCustomizationVersion(String id) {
        return this.customizationModel.removeCustomizationVersion(id);
    }

    // ������Ϣ
    protected class CustomizationVersionExplorerProbe extends MouseAdapter {

        public void mouseClicked(MouseEvent e) {
            CustomizationVersionTreeNode cvtn = null;

            TreePath tp = treeTable.getPathForLocation(e.getX(), e.getY());
            if (tp != null) {
                Object node = tp.getLastPathComponent();
                if ((node != null) && node instanceof CustomizationVersionTreeNode) {
                    cvtn = (CustomizationVersionTreeNode) node;
                }
            }

            if (cvtn != null && (e.getButton() == MouseEvent.BUTTON1 || e.getButton() == MouseEvent.BUTTON3)) {
                CustomizationVersion newCustomizationVersion = cvtn.getCustomizationVersion();
                if (newCustomizationVersion == null) {
                    setCurrentCustomizationVersionId("");
                } else {
                    setCurrentCustomizationVersionId(newCustomizationVersion.getId());
                    if (constraintExplorer != null) {
                        constraintExplorer.getContext().putValue(ConstantDefinition.CUSTOMIZATION_VERSION, newCustomizationVersion);
                    }
                }
            }

            if (e.getButton() == MouseEvent.BUTTON3) {
                getContext().putValue(INNER_SELECTEDNODE, cvtn);

                updateCustomizationVersionAction.firePropertyChange("enabled", !updateCustomizationVersionAction.isEnabled(), updateCustomizationVersionAction.isEnabled());
                removeCustomizationVersionAction.firePropertyChange("enabled", !removeCustomizationVersionAction.isEnabled(), removeCustomizationVersionAction.isEnabled());
                cutFeatureAction.firePropertyChange("enabled", !cutFeatureAction.isEnabled(), cutFeatureAction.isEnabled());

                popupMenu.show(treeTable, e.getX(), e.getY());
            }
        }
    }
    private ConstraintExplorer constraintExplorer = null;

    public ConstraintExplorer getConstraintExplorer(FeatureModel featureModel, ConstraintModel constraintModel) {
        if (this.constraintExplorer == null) {
            this.constraintExplorer = new ConstraintExplorerImplementation(featureModel, constraintModel);
        }
        return this.constraintExplorer;
    }
}
TOP

Related Classes of de.FeatureModellingTool.Customize.ui.CustomizationExplorerImplementation$CutFeatureAction

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.