Package de.grapheditor.ProjectManager

Source Code of de.grapheditor.ProjectManager.Implementation$NewProjectAction

package de.grapheditor.ProjectManager;

import applet.WebConstantDefinition;
import component.FComponent;

import javax.swing.*;
import javax.swing.filechooser.FileFilter;
import java.io.IOException;
import java.io.File;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.net.URL;
import java.util.Hashtable;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeEvent;
import java.awt.event.ActionEvent;

import de.reuse.Context;
import de.reuse.ContextImplementation;
import de.reuse.GroupTreeMap;
import de.reuse.CommonFileFilter;
import de.grapheditor.DrawingViewContainer;
import de.grapheditor.store.AdaptedStandardStorageFormat;
import research.Drawing;
import research.DrawingView;
import research.store.StorableOutput;

/**
* Created by IntelliJ IDEA.
* User: zhangwei
* Date: 2004-7-3
* Time: 21:26:29
* To change this template use File | Settings | File Templates.
*/
class Implementation implements Interface {

    /**
     * ��Ŀ���������������ɶ���(actions)
     */
    protected NewProjectAction newProjectAction = null;
    protected OpenProjectAction openProjectAction = null;
    protected ModifyProjectPropertiesAction modifyProjectPropertiesAction = null;
    protected SaveProjectAction saveProjectAction = null;
    protected SaveAsProjectAction saveAsProjectAction = null;
    protected CloseProjectAction closeProjectAction = null;

    protected NewViewAction newViewAction = null;
    protected CloseViewAction closeViewAction = null;
    protected OpenViewAction openViewAction = null;
    protected RemoveViewAction removeViewAction = null;
    protected ExportViewAction exportViewAction = null;

    protected Hashtable data = null;

    /**
     * ��Ŀ�������Ļ����д��ڵı���(environments)
     */
    //��ǰ��Ŀ���ڵĴ���
    protected JFrame uiFrame = null;
    //��ǰ��Ŀ
    protected Project project = null;

    //ͼԪ�༭�������б�
    private FComponent graphicalEditorActionMap = null;
    //DrawingView����
    private DrawingViewContainer drawingViewContainer = null;
    //�ļ�ѡ��Ի���
    private JFileChooser fileChooser = null;

    //��Ŀ��Ϣ���
    private ProjectInfoPane projectInfoPane = new ProjectInfoPane();
    //��ͼ��Ϣ���
    private ViewInfoPane viewInfoPane = new ViewInfoPane();

    //����Ŀ����������������
    protected final Context context = new ContextImplementation();


    //�ر���ͼ��Ч���Լ�����
    protected PropertyChangeListener closeViewEnabledPropertyChangeListener = new CloseViewEnabledPropertyChangeListener();
    //ɾ����ͼ��Ч���Լ�����
    protected PropertyChangeListener removeViewEnabledPropertyChangeListener = new RemoveViewEnabledPropertyChangeListener();
    //������ͼ��Ч���Լ�����
    protected PropertyChangeListener exportViewEnabledPropertyChangeListener = new ExportViewEnabledPropertyChangeListener();

    public Implementation() {
        /**
         * ������Ŀ���������������ɶ�������������뵽�б���
         */
        newProjectAction = new NewProjectAction();
        openProjectAction = new OpenProjectAction();
        modifyProjectPropertiesAction = new ModifyProjectPropertiesAction();
        saveProjectAction = new SaveProjectAction();
        saveAsProjectAction = new SaveAsProjectAction();
        closeProjectAction = new CloseProjectAction();

        newViewAction = new NewViewAction();
        closeViewAction = new CloseViewAction();
        removeViewAction = new RemoveViewAction();
        exportViewAction = new ExportViewAction();

        data = new Hashtable();

        data.put(ConstantDefinition.NEW_PROJECT_ACTION, newProjectAction);
        data.put(ConstantDefinition.OPEN_PROJECT_ACTION, openProjectAction);
        data.put(ConstantDefinition.MODIFY_PROJECT_PROPERTIES_ACTION, modifyProjectPropertiesAction);
        data.put(ConstantDefinition.SAVE_PROJECT_ACTION, saveProjectAction);
        data.put(ConstantDefinition.SAVE_AS_PROJECT_ACTION, saveAsProjectAction);
        data.put(ConstantDefinition.CLOSE_PROJECT_ACTION, closeProjectAction);
        data.put(ConstantDefinition.NEW_VIEW_ACTION, newViewAction);
        data.put(ConstantDefinition.CLOSE_VIEW_ACTION, closeViewAction);
        data.put(ConstantDefinition.REMOVE_VIEW_ACTION, removeViewAction);
        data.put(ConstantDefinition.EXPORT_VIEW_ACTION, exportViewAction);

        /**
         * ��ӻ����仯������
         */
        context.addContextChangeListener(new ContextChangeListener());
    }

    /**
     * ���ݶ������ƻ�ȡ����
     *
     * @param actionName
     * @return Action
     */
    public Action getAction(String actionName) {
        return (Action) data.get(actionName);
    }

    /**
     * ��ȡ����
     *
     * @return Context
     */
    public Context getContext() {
        return context;
    }

    /**
     * �����仯������
     */
    protected class ContextChangeListener implements PropertyChangeListener {
        public void propertyChange(PropertyChangeEvent e) {
            contextChange(e);
        }
    }

    /**
     * �ú�����ѵ�ǰ����Ȥ�Ļ����������������ñ�������Ӧ��������.
     * ��ʵ���Ͽ���ֱ�ӵ������л����Щ���ã���΢�е��鷳��
     *
     * @param e
     */
    protected void contextChange(PropertyChangeEvent e) {
        String propertyName = e.getPropertyName();
        /**
         * ÿһ�λ��������仯����Ŀ�����������Ķ����Ŀ�������Ҫ����
         */

        //��ö����ĵ�ǰ����״̬
        Object[] actions = data.values().toArray();
        int size = actions.length;
        int oldV = 0, newV = 1;
        boolean value[][] = new boolean[size][2];
        for (int i = 0; i < size; i++) {
            ProjectAction action = (ProjectAction) actions[i];
            value[i][oldV] = action.isEnabled();
        }

        if (ConstantDefinition.PROJECT.equals(propertyName)) {
            project = (Project) e.getNewValue();
        } else if (ConstantDefinition.UI_FRAME.equals(propertyName)) {
            uiFrame = (JFrame) e.getNewValue();
        } else if (ConstantDefinition.GRAPHICAL_EDITOR_ACTION_MAP.equals(propertyName)) {
            if (graphicalEditorActionMap != null) {
                ((Action) graphicalEditorActionMap.getInterface("closeView")).removePropertyChangeListener(closeViewEnabledPropertyChangeListener);
                ((Action) graphicalEditorActionMap.getInterface("removeView")).removePropertyChangeListener(removeViewEnabledPropertyChangeListener);
                ((Action) graphicalEditorActionMap.getInterface("exportView")).removePropertyChangeListener(exportViewEnabledPropertyChangeListener);
            }
            graphicalEditorActionMap = (FComponent) e.getNewValue();
            if (graphicalEditorActionMap != null) {
                ((Action) graphicalEditorActionMap.getInterface("closeView")).addPropertyChangeListener(closeViewEnabledPropertyChangeListener);
                ((Action) graphicalEditorActionMap.getInterface("removeView")).addPropertyChangeListener(removeViewEnabledPropertyChangeListener);
                ((Action) graphicalEditorActionMap.getInterface("exportView")).addPropertyChangeListener(exportViewEnabledPropertyChangeListener);
            }
        } else if (ConstantDefinition.DRAWING_VIEW_CONTAINER.equals(propertyName)) {
            drawingViewContainer = (DrawingViewContainer) e.getNewValue();
        } else if (ConstantDefinition.FILE_CHOOSER.equals(propertyName)) {
            fileChooser = (JFileChooser) e.getNewValue();
            projectInfoPane.setFileChooser(fileChooser);
        }

        //���¶����Ľ�����Ϣ
        for (int i = 0; i < size; i++) {
            ProjectAction action = (ProjectAction) actions[i];
            value[i][newV] = action.isEnabled();

            if (value[i][oldV] != value[i][newV]) {
                action.firePropertyChange("enabled", Boolean.valueOf(value[i][oldV]), Boolean.valueOf(value[i][newV]));
            }
        }

    }


    protected class CloseViewEnabledPropertyChangeListener implements PropertyChangeListener {
        public void propertyChange(PropertyChangeEvent e) {
            String propertyName = e.getPropertyName();

            if (propertyName.equals("enabled")) {

                boolean newV = ((Boolean) e.getNewValue()).booleanValue();

                if (newV && (closeViewAction.isEnabled())) {
                    closeViewAction.firePropertyChange("enabled", Boolean.FALSE, Boolean.TRUE);
                } else if (!newV && (!closeViewAction.isEnabled())) {
                    closeViewAction.firePropertyChange("enabled", Boolean.TRUE, Boolean.FALSE);
                }
            }
        }
    }


    protected class RemoveViewEnabledPropertyChangeListener implements PropertyChangeListener {
        public void propertyChange(PropertyChangeEvent e) {
            String propertyName = e.getPropertyName();

            if (propertyName.equals("enabled")) {

                boolean newV = ((Boolean) e.getNewValue()).booleanValue();

                if (newV && (removeViewAction.isEnabled())) {
                    removeViewAction.firePropertyChange("enabled", Boolean.FALSE, Boolean.TRUE);
                } else if (!newV && (!closeViewAction.isEnabled())) {
                    removeViewAction.firePropertyChange("enabled", Boolean.TRUE, Boolean.FALSE);
                }
            }
        }
    }

    protected class ExportViewEnabledPropertyChangeListener implements PropertyChangeListener {
        public void propertyChange(PropertyChangeEvent e) {
            String propertyName = e.getPropertyName();

            if (propertyName.equals("enabled")) {

                boolean newV = ((Boolean) e.getNewValue()).booleanValue();

                if (newV && (removeViewAction.isEnabled())) {
                    exportViewAction.firePropertyChange("enabled", Boolean.FALSE, Boolean.TRUE);
                } else if (!newV && (!closeViewAction.isEnabled())) {
                    exportViewAction.firePropertyChange("enabled", Boolean.TRUE, Boolean.FALSE);
                }
            }
        }
    }

    /**
     * Action definition of the ProjectManager
     */
    class NewProjectAction extends ProjectAction {
        public NewProjectAction()  {
            this.putValue(Action.NAME, "�½���Ŀ...");
            try{
            this.putValue(Action.SMALL_ICON, new ImageIcon(new URL(WebConstantDefinition.RES_HOST+"new_prj.gif")));
            }catch(Exception e){
              e.printStackTrace();
            }
            this.putValue(Action.SHORT_DESCRIPTION, "�½�һ��������ģ��Ŀ");
        }

        public boolean isEnabled() {
            if (!super.isEnabled()) return false;
            if (uiFrame == null) return false;

            return true;
        }

        public void actionPerformed(ActionEvent e) {
            if (!isEnabled()) return;

            JFrame frame = (JFrame) this.getValue(ConstantDefinition.UI_FRAME);

            projectInfoPane.setContainer(frame);

            projectInfoPane.setProjectNameEditable(true);
            projectInfoPane.setDescriptionEditable(true);
            projectInfoPane.setPathEditable(true);

            projectInfoPane.clear();
            projectInfoPane.setMode(ProjectInfoPane.NEW_PROJECT_MODE);

            for (; ;) {
                int result = JOptionPane.showConfirmDialog(frame, projectInfoPane, "������Ŀ����", JOptionPane.OK_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE);

                if (result == JOptionPane.OK_OPTION) {
                    if (projectInfoPane.isInfoValid() == false) {
                        JOptionPane.showMessageDialog(frame, projectInfoPane.getErrorInfo(), "���洰��", JOptionPane.ERROR_MESSAGE);
                        continue;
                    } else {
                        Project project = new ProjectImplementation(projectInfoPane.getProjectName(),
                                projectInfoPane.getDescription(),
                                projectInfoPane.getProjectFile());

                        de.grapheditor.ProjectManager.Interface newProjectManager = de.grapheditor.ProjectManager.Factory.createProjectManager();
                        newProjectManager.getContext().putValue(de.grapheditor.ProjectManager.ConstantDefinition.PROJECT, project);
                        newProjectManager.getContext().putValue(de.grapheditor.ProjectManager.ConstantDefinition.INFOMATION_MAP, new GroupTreeMap());

                        newProjectManager.getAction(ConstantDefinition.SAVE_PROJECT_ACTION).actionPerformed((ActionEvent) null);

                        this.firePropertyChange(ConstantDefinition.PROJECT_CREATED, null, newProjectManager);
                    }
                }

                break;
            }
        }

    }

    class OpenProjectAction extends ProjectAction {

        protected javax.swing.filechooser.FileFilter fileFilter = new CommonFileFilter("gef", "Graph Editor Files (*.gef)");

        public OpenProjectAction() {
            this.putValue(Action.NAME, "����Ŀ");
            this.putValue(Action.SMALL_ICON, new ImageIcon(getClass().getResource("image/open_prj.gif")));
            this.putValue(Action.SHORT_DESCRIPTION, "��һ��������ģ��Ŀ");
        }

        public boolean isEnabled() {
            if (!super.isEnabled()) return false;
            if (uiFrame == null) return false;

            return true;
        }

        public void actionPerformed(ActionEvent e) {
            if (!isEnabled()) return;

            fileChooser.setDialogTitle("ѡ����Ŀ�ļ�");
            fileChooser.setDialogType(JFileChooser.OPEN_DIALOG);

            fileChooser.setFileFilter(fileFilter);

            File selectedFile = null;

            if (fileChooser.showOpenDialog(uiFrame) == JFileChooser.APPROVE_OPTION) {
                selectedFile = fileChooser.getSelectedFile();
            } else {
                fileChooser.resetChoosableFileFilters();
                return;
            }

            ProjectReader projectReader = new ProjectReader(selectedFile);

            boolean result = projectReader.readProject();

            if (result == false) {
                JOptionPane.showMessageDialog(uiFrame, projectReader.getErrorInfo(), "������Ϣ����", JOptionPane.ERROR_MESSAGE);
                return;
            }

            String[] viewNameArray = projectReader.getViewNameArray();
            Drawing[] drawingArray = projectReader.getDrawingArray();

            ProjectImplementation pi = new ProjectImplementation();

            String projectName = selectedFile.getName();
            int lastIndex = projectName.lastIndexOf('.');
            pi.setProjectName(projectName.substring(0, lastIndex));
            pi.setStoreFile(selectedFile);

            for (int i = 0; i < viewNameArray.length; i++)
                pi.addView(viewNameArray[i]);

            Interface pmi = Factory.createProjectManager();
            pmi.getContext().putValue(ConstantDefinition.PROJECT, pi);
            pmi.getContext().putValue(ConstantDefinition.DRAWING_ARRAY, drawingArray);

            this.firePropertyChange(ConstantDefinition.PROJECT_OPENED, null, pmi);
        }
    }

    class ModifyProjectPropertiesAction extends ProjectAction {

        public ModifyProjectPropertiesAction() {
            this.putValue(Action.NAME, "��Ŀ����...");
            this.putValue(Action.SMALL_ICON, new ImageIcon(getClass().getResource("image/pro_prj.png")));
            this.putValue(Action.SHORT_DESCRIPTION, "�޸ĵ�ǰ��Ŀ������");
        }

        public boolean isEnabled() {
            if (!super.isEnabled()) return false;
            if (uiFrame == null) return false;
            if (project == null) return false;

            return true;
        }

        public void actionPerformed(ActionEvent e) {
            if (!isEnabled()) return;

            projectInfoPane.setContainer(uiFrame);
            projectInfoPane.setProjectNameEditable(false);
            projectInfoPane.setPathEditable(false);

            projectInfoPane.setDescriptionEditable(true);

            projectInfoPane.setProjectName(project.getProjectName());
            projectInfoPane.setDescription(project.getDescription());
            projectInfoPane.setStoreDirectory(project.getStoreFile().getParent());

            projectInfoPane.setMode(ProjectInfoPane.MODIFY_PROPERTY_MODE);

            for (; ;) {
                int result = JOptionPane.showConfirmDialog(uiFrame, projectInfoPane, "������Ŀ����", JOptionPane.OK_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE);

                if (result == JOptionPane.OK_OPTION) {
                    if (projectInfoPane.isInfoValid() == false) {
                        JOptionPane.showMessageDialog(uiFrame, projectInfoPane.getErrorInfo(), "���洰��", JOptionPane.ERROR_MESSAGE);
                        continue;

                    } else {
                        ProjectImplementation pi = (ProjectImplementation) project;
                        pi.setDescription(projectInfoPane.getDescription());
                    }
                }

                break;
            }

        }
    }

    class SaveAsProjectAction extends ProjectAction {
        public SaveAsProjectAction() {
            this.putValue(Action.NAME, "�����Ŀ...");
            this.putValue(Action.SMALL_ICON, new ImageIcon(getClass().getResource("image/save_as_prj.gif")));
            this.putValue(Action.SHORT_DESCRIPTION, "��ǰ��Ŀ���Ϊ");
        }

        public boolean isEnabled() {
            if (!super.isEnabled()) return false;
            if (uiFrame == null) return false;
            if (project == null) return false;
            //if (drawingViewContainer == null) return false;

            return true;
        }

        public void actionPerformed(ActionEvent e) {
            if (!isEnabled()) return;

            projectInfoPane.setContainer(uiFrame);
            projectInfoPane.setProjectNameEditable(true);
            projectInfoPane.setPathEditable(true);

            projectInfoPane.setDescriptionEditable(true);

            projectInfoPane.setProjectName(project.getProjectName());
            projectInfoPane.setDescription(project.getDescription());
            projectInfoPane.setStoreDirectory(project.getStoreFile().getParent());

            projectInfoPane.setMode(ProjectInfoPane.NEW_PROJECT_MODE);

            for (; ;) {
                int result = JOptionPane.showConfirmDialog(uiFrame, projectInfoPane, "���������Ŀ����", JOptionPane.OK_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE);

                if (result == JOptionPane.OK_OPTION) {
                    if (projectInfoPane.isInfoValid() == false) {
                        JOptionPane.showMessageDialog(uiFrame, projectInfoPane.getErrorInfo(), "���洰��", JOptionPane.ERROR_MESSAGE);
                        continue;

                    } else {
                        ProjectImplementation pi = (ProjectImplementation) project;

                        pi.setProjectName(projectInfoPane.getProjectName());
                        pi.setDescription(projectInfoPane.getDescription());
                        pi.setStoreFile(projectInfoPane.getProjectFile());

                        saveProjectAction.actionPerformed((ActionEvent) null);
                    }
                }

                break;
            }
        }
    }

    class SaveProjectAction extends ProjectAction {
        public SaveProjectAction() {
            this.putValue(Action.NAME, "������Ŀ");
            this.putValue(Action.SMALL_ICON, new ImageIcon(getClass().getResource("image/save_prj.gif")));
            this.putValue(Action.SHORT_DESCRIPTION, "���浱ǰ��Ŀ");
        }

        public boolean isEnabled() {
            if (!super.isEnabled()) return false;
            if (project == null) return false;

            return true;
        }

        public void actionPerformed(ActionEvent e) {
            if (!isEnabled()) return;

            File storeFile = project.getStoreFile();

            try {
                ZipOutputStream zipOutStream = new ZipOutputStream(new FileOutputStream(storeFile));

                DrawingView[] drawingViews;

                if (drawingViewContainer != null) {
                    Object[] objects = (drawingViewContainer.getAllDrawingViews()).values().toArray();
                    drawingViews = new DrawingView[objects.length];
                    for(int i = 0; i < objects.length; i++){
                        drawingViews[i] = (DrawingView)objects[i];
                    }
                } else
                    drawingViews = new DrawingView[0];

                ByteArrayOutputStream bufferStream = new ByteArrayOutputStream();

                //begin д����Ŀ�����ļ�
                StorableOutput textOutput = new StorableOutput(bufferStream);

                textOutput.writeInt(drawingViews.length);
                for (int i = 0; i < drawingViews.length; i++) {
                    textOutput.writeString(drawingViews[i].getDrawing().getTitle());
                }
                textOutput.close();

                ZipEntry indexZipEntry = new ZipEntry("index.txt");
                zipOutStream.putNextEntry(indexZipEntry);
                bufferStream.writeTo(zipOutStream);
                zipOutStream.closeEntry();
                //end д����Ŀ�����ļ�

                bufferStream.reset();

                //begin д����Ŀ��ͼ�ļ�
                AdaptedStandardStorageFormat store = new AdaptedStandardStorageFormat();
                for (int i = 0; i < drawingViews.length; i++) {
                    store.store(bufferStream, drawingViews[i].getDrawing());
                    ZipEntry viewZipEntry = new ZipEntry((String) drawingViews[i].getDrawing().getTitle() + ".draw");
                    zipOutStream.putNextEntry(viewZipEntry);
                    bufferStream.writeTo(zipOutStream);
                    zipOutStream.closeEntry();

                    bufferStream.reset();
                }
                //end д����Ŀ��ͼ�ļ�

                zipOutStream.finish();
                zipOutStream.close();

            } catch (Exception exception) {
                System.out.println("Exception occurs when save current project.\n" + exception.getCause());
            }
        }
    }

    class CloseProjectAction extends ProjectAction {
        public CloseProjectAction() {
            this.putValue(Action.NAME, "�ر���Ŀ...");
            this.putValue(Action.SMALL_ICON, new ImageIcon(getClass().getResource("image/close_prj.png")));
            this.putValue(Action.SHORT_DESCRIPTION, "�رյ�ǰ��Ŀ");
        }

        public boolean isEnabled() {
            if (!super.isEnabled()) return false;
            if (uiFrame == null) return false;
            if (project == null) return false;

            return true;
        }

        public void actionPerformed(ActionEvent e) {
            if (!isEnabled()) return;

            int result = JOptionPane.showConfirmDialog(uiFrame, "�Ƿ񱣴����ϴδ��̲�����������Ŀ���޸�", "����ȷ�ϴ���", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE);

            if (result == JOptionPane.CANCEL_OPTION)
                return;

            //�洢��Ŀ�ļ�
            if (result == JOptionPane.YES_OPTION) {
                Action saveAction = (Action) data.get(ConstantDefinition.SAVE_PROJECT_ACTION);
                if ((saveAction != null) && (saveAction.isEnabled())) {
                    saveAction.actionPerformed(e);
                } else {
                    int rst = JOptionPane.showConfirmDialog(uiFrame, "�޷�������Ŀ�洢ģ�飬�Ƿ�����ر���Ŀ�IJ�����/n����������ϴδ��̺���޸Ľ��޷�������", "���洰��", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
                    if (rst == JOptionPane.CANCEL_OPTION)
                        return;
                }
            }

            context.putValue(ConstantDefinition.CLOSE_REQUEST, Boolean.TRUE);
        }
    }

    class NewViewAction extends ProjectAction {
        public NewViewAction() {
            this.putValue(Action.NAME, "�½���ͼ...");
            this.putValue(Action.SMALL_ICON, new ImageIcon(getClass().getResource("image/new_view.gif")));
            this.putValue(Action.SHORT_DESCRIPTION, "�½�һ��������ͼ");
        }

        public boolean isEnabled() {
            if (!super.isEnabled()) return false;
            if (uiFrame == null) return false;
            if (project == null) return false;
            if (graphicalEditorActionMap == null) return false;

            return true;
        }

        public void actionPerformed(ActionEvent e) {
            if (!isEnabled()) return;

            for (; ;) {
                int result = JOptionPane.showConfirmDialog(uiFrame, viewInfoPane, "�½���ͼ", JOptionPane.OK_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE);

                if (result == JOptionPane.OK_OPTION) {
                    if (viewInfoPane.isInfoValid() == false) {
                        JOptionPane.showMessageDialog(uiFrame, viewInfoPane.getErrorInfo(), "���洰��", JOptionPane.ERROR_MESSAGE);
                        continue;
                    } else {
                        if (project.contains(viewInfoPane.getName())) {
                            JOptionPane.showMessageDialog(uiFrame, "��ͬ���ֵ���ͼ�Ѿ����ڣ�", "���洰��", JOptionPane.ERROR_MESSAGE);
                            continue;
                        }
                    }
                } else {
                    return;
                }

                break;
            }

            ((ProjectImplementation) project).addView(viewInfoPane.getName());

            Action addViewAction = (Action) graphicalEditorActionMap.getInterface("addView");
            addViewAction.putValue(de.grapheditor.ConstantDefinition.VIEW_NAME, viewInfoPane.getName());
            addViewAction.actionPerformed(null);

        }
    }

    class OpenViewAction extends ProjectAction {
        public OpenViewAction() {
            this.putValue(Action.NAME, "����ͼ");
            this.putValue(Action.SMALL_ICON, new ImageIcon(getClass().getResource("image/close_view.png")));
            this.putValue(Action.SHORT_DESCRIPTION, "��������ͼ");
        }

        public boolean isEnabled() {
            if (!super.isEnabled()) return false;
            if (uiFrame == null) return false;
            if (project == null) return false;
            if (project.getViewNum() <= 0) return false;
            if (graphicalEditorActionMap == null) return false;
            if (!((Action) graphicalEditorActionMap.getInterface("openView")).isEnabled()) return false;

            return true;
        }

        public void actionPerformed(ActionEvent e) {
            if (!isEnabled()) return;

            //will add in the future.
        }
    }

    class CloseViewAction extends ProjectAction {
        public CloseViewAction() {
            this.putValue(Action.NAME, "�ر���ͼ");
            this.putValue(Action.SMALL_ICON, new ImageIcon(getClass().getResource("image/close_view.png")));
            this.putValue(Action.SHORT_DESCRIPTION, "�رյ�ǰ������ͼ");
        }

        public boolean isEnabled() {
            if (!super.isEnabled()) return false;
            if (uiFrame == null) return false;
            if (project == null) return false;
            if (project.getViewNum() <= 0) return false;
            if (graphicalEditorActionMap == null) return false;
            if (!((Action) graphicalEditorActionMap.getInterface("closeView")).isEnabled()) return false;

            return true;
        }

        public void actionPerformed(ActionEvent e) {
            if (!isEnabled()) return;

            Action closeView = (Action) graphicalEditorActionMap.getInterface("closeView");

            closeView.actionPerformed(null);
            String viewName = (String) closeView.getValue(de.grapheditor.ConstantDefinition.VIEW_NAME);
            closeView.putValue(de.grapheditor.ConstantDefinition.VIEW_NAME, null);

        }
    }

    class RemoveViewAction extends ProjectAction {
        public RemoveViewAction() {
            this.putValue(Action.NAME, "ɾ����ͼ...");
            this.putValue(Action.SMALL_ICON, new ImageIcon(getClass().getResource("image/remove_view.gif")));
            this.putValue(Action.SHORT_DESCRIPTION, "ɾ����ǰ������ͼ");
        }

        public boolean isEnabled() {
            if (!super.isEnabled()) return false;
            if (uiFrame == null) return false;
            if (project == null) return false;
            if (project.getViewNum() <= 0) return false;
            if (graphicalEditorActionMap == null) return false;
            if (!((Action) graphicalEditorActionMap.getInterface("removeView")).isEnabled()) return false;

            return true;
        }

        public void actionPerformed(ActionEvent e) {
            if (!isEnabled()) return;

            Action removeView = (Action) graphicalEditorActionMap.getInterface("removeView");

            int result = JOptionPane.showConfirmDialog(uiFrame, "�����Ҫ����Ŀ��ɾ����ǰ��ͼ��?\n�ڴ��̺�,�⽫��ɲ�����صĺ��.\n(������ͨ���������̵ķ�ʽ,�ָ��ϴδ��̺�ɾ�����޸ĵ���ͼ)", "ɾ����ͼ", JOptionPane.OK_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE);

            if (result != JOptionPane.OK_OPTION)
                return;

            removeView.actionPerformed(null);
            String viewName = (String) removeView.getValue(de.grapheditor.ConstantDefinition.VIEW_NAME);
            removeView.putValue(de.grapheditor.ConstantDefinition.VIEW_NAME, null);

            ((ProjectImplementation) project).removeView(viewName);
        }

    }

    class ExportViewAction extends ProjectAction {

        protected javax.swing.filechooser.FileFilter gifFileFilter = new CommonFileFilter("gif", "GIF Image Files (*.gif)");
        protected javax.swing.filechooser.FileFilter svgFileFilter = new CommonFileFilter("svg", "Scalable Vector Graphics (*.svg)");
        protected javax.swing.filechooser.FileFilter moreFileFilter = new CommonFileFilter("more", "More Other Graphic Format (*.*)");

        public ExportViewAction() {
            this.putValue(Action.NAME, "�����ͼ...");
            this.putValue(Action.SMALL_ICON, new ImageIcon(getClass().getResource("image/export_view.gif")));
            this.putValue(Action.SHORT_DESCRIPTION, "�ѵ�ǰ������ͼ���Ϊͼ��");
        }

        public boolean isEnabled() {
            if (!super.isEnabled()) return false;
            if (uiFrame == null) return false;
            if (project == null) return false;
            if (project.getViewNum() <= 0) return false;
            if (graphicalEditorActionMap == null) return false;

            return true;
        }

        public void actionPerformed(ActionEvent e) {
            if (!isEnabled()) return;

            //begin: get the image file's path from user
            File imageFile = null;

            fileChooser.setDialogTitle("��ͼ����Ϊͼ���ļ�");
            fileChooser.setDialogType(JFileChooser.SAVE_DIALOG);

            FileFilter[] oldFilter = fileChooser.getChoosableFileFilters();

            for (int i = 0; i < oldFilter.length; i++) {
                fileChooser.removeChoosableFileFilter(oldFilter[i]);
            }

            fileChooser.addChoosableFileFilter(gifFileFilter);
            fileChooser.addChoosableFileFilter(svgFileFilter);
            fileChooser.addChoosableFileFilter(moreFileFilter);
            fileChooser.setFileFilter(gifFileFilter);

            PropertyChangeListener fileFilterChangeListener = new PropertyChangeListener() {
                public void propertyChange(PropertyChangeEvent e) {
                    CommonFileFilter ff = (CommonFileFilter) e.getNewValue();

                    if (!e.getPropertyName().equals(JFileChooser.FILE_FILTER_CHANGED_PROPERTY))
                        return;

                    if (ff.getSuffix().equalsIgnoreCase("more")) {
                        fileChooser.cancelSelection();
                        Action action = (Action) graphicalEditorActionMap.getInterface("exportView");
                        action.putValue(de.grapheditor.ConstantDefinition.IMAGE_TYPE, "more");
                        action.putValue(de.grapheditor.ConstantDefinition.UI_FRAME, uiFrame);
                        action.actionPerformed(null);
                        ((JFileChooser) e.getSource()).removePropertyChangeListener(JFileChooser.FILE_FILTER_CHANGED_PROPERTY, this);
                    }
                }
            };

            fileChooser.addPropertyChangeListener(JFileChooser.FILE_FILTER_CHANGED_PROPERTY, fileFilterChangeListener);

            for (; ;) {
                if (fileChooser.showDialog(uiFrame, null) == JFileChooser.APPROVE_OPTION) {
                    imageFile = fileChooser.getSelectedFile();
                    CommonFileFilter currentFilter = (CommonFileFilter) fileChooser.getFileFilter();

                    if (!currentFilter.getSuffix().equalsIgnoreCase(CommonFileFilter.getSuffix(imageFile))) {
                        String path = imageFile.getPath() + "." + currentFilter.getSuffix();
                        imageFile = new File(path);
                    }

                    if (imageFile.exists()) {
                        int result_tmp = JOptionPane.showConfirmDialog(uiFrame, "�ļ� " + imageFile.getPath() + " �Ѿ����ڣ��Ƿ񸲸Ǹ��ļ���", "ȷ�ϴ���", JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE);

                        if (result_tmp == JOptionPane.YES_OPTION) {
                            break;
                        } else {
                            continue;
                        }
                    }

                    break;
                } else {
                    break;
                }
            }

            fileChooser.removePropertyChangeListener(JFileChooser.FILE_FILTER_CHANGED_PROPERTY, fileFilterChangeListener);

            CommonFileFilter selectedFilter = (CommonFileFilter) fileChooser.getFileFilter();

            fileChooser.removeChoosableFileFilter(moreFileFilter);
            fileChooser.removeChoosableFileFilter(svgFileFilter);
            fileChooser.removeChoosableFileFilter(gifFileFilter);

            for (int i = 0; i < oldFilter.length; i++) {
                fileChooser.addChoosableFileFilter(oldFilter[i]);
            }
            fileChooser.setSelectedFile(null);
            //end: get the image file's path from user

            //begin:
            if (imageFile == null)
                return;

            if (!imageFile.exists()) {
                try {
                    imageFile.createNewFile();
                } catch (IOException ioe) {
                    JOptionPane.showMessageDialog(uiFrame, "�������ļ�ʧ��,����洢�豸�Ƿ�������", "���洰��", JOptionPane.ERROR_MESSAGE);
                    return;
                }
            }
            //end:

            Action action = (Action) graphicalEditorActionMap.getInterface("exportView");
            action.putValue(de.grapheditor.ConstantDefinition.FILE, imageFile);
            action.putValue(de.grapheditor.ConstantDefinition.IMAGE_TYPE, selectedFilter.getSuffix());
            action.putValue(de.grapheditor.ConstantDefinition.UI_FRAME, uiFrame);
            action.actionPerformed(null);
        }
    }

    abstract class ProjectAction extends AbstractAction {
        public void firePropertyChange(String propertyName, Object oldValue, Object newValue) {
            super.firePropertyChange(propertyName, oldValue, newValue);
        }
    }

}
TOP

Related Classes of de.grapheditor.ProjectManager.Implementation$NewProjectAction

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.