Package com.nexirius.multimail

Source Code of com.nexirius.multimail.MultiMailApplication$ResetCommand

//{HEADER
/**
* This class is part of jnex 'Nexirius Application Framework for Java'
* Source File Date:   Mon Feb 16 22:50:51 GMT+01:00 2004
* @version:           2.8 (Wed Feb 18 07:27:51 GMT+01:00 2004)
* Class name:         com.nexirius.com.nexirius.multimail.MultiMailApplication
*
* Copyright (C) Nexirius GmbH, CH-4450 Sissach, Switzerland (www.nexirius.ch)
*
* <p>This library is free software; you can redistribute it and/or<br>
* modify it under the terms of the GNU Lesser General Public<br>
* License as published by the Free Software Foundation; either<br>
* version 2.1 of the License, or (at your option) any later version.</p>
*
* <p>This library is distributed in the hope that it will be useful,<br>
* but WITHOUT ANY WARRANTY; without even the implied warranty of<br>
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU<br>
* Lesser General Public License for more details.</p>
*
* <p>You should have received a copy of the GNU Lesser General Public<br>
* License along with this library; if not, write to the Free Software<br>
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA</p>
* </blockquote>
*
* <p>
* Nexirius GmbH, hereby disclaims all copyright interest in<br>
* the library jnex' 'Nexirius Application Framework for Java' written<br>
* by Marcel Baumann.</p>
*/
//}HEADER
package com.nexirius.multimail;

import com.nexirius.framework.application.Application;
import com.nexirius.framework.application.DialogManager;
import com.nexirius.framework.datamodel.*;
import com.nexirius.framework.dataviewer.*;
import com.nexirius.framework.swing.SwingViewerCreator;
import com.nexirius.multimail.datamodel.*;
import com.nexirius.multimail.dataviewer.*;
import com.nexirius.multimail.layout.MailArrayLayout;
import com.nexirius.multimail.layout.MailListLayout;
import com.nexirius.multimail.layout.ReplacePatternLayout;
import com.nexirius.util.XFile;
import com.nexirius.util.resource.ClientResourceImpl;
import com.nexirius.license.LicenseModel;
import com.nexirius.license.LicenseViewer;
import com.nexirius.license.OrderLicenseModel;
import com.nexirius.license.OrderLicenseViewer;

import javax.swing.*;
import java.awt.*;
import java.io.PushbackInputStream;

public class MultiMailApplication extends Application {
    public static final String SETTINGS_FILENAME = new String("MultiMailSettings.txt");
    public static final String _NEXIRIUS_VERSION = "2.8";
    MainModel mainModel;
    SettingsModel settings;
    JWindow startFrame;

    public MultiMailApplication(String argv[]) {
        super(argv);
    }

    public DataModel getApplicationModel() {
        return mainModel;
    }

    public void exit() {
        if (mainModel.needSaving()) {
            if (!DialogManager.ask("ReallyExit", "yes", "no", true)) {
                return;
            }
        }

        XFile settingsFile = new XFile(SETTINGS_FILENAME);

        try {
            settingsFile.writeText(settings.dragData());
        } catch (Exception e) {
            e.printStackTrace()//TODO
        }

        super.exit();
    }

    public String getApplicationName() {
        return "MultiMailApplication";
    }

    public void showStartFrame() {
        JLabel label = new JLabel(getClientResource().getIcon("startIcon"));

        label.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));

        startFrame = new JWindow();
        startFrame.getContentPane().setLayout(new BorderLayout());
        startFrame.getContentPane().add(BorderLayout.CENTER, label);

        Dimension dim = startFrame.getToolkit().getScreenSize();
        Dimension dimw = startFrame.getPreferredSize();

        int w = dimw.width;
        int h = dimw.height;

        startFrame.setSize(w, h);
        startFrame.setLocation((dim.width - w) / 2, (dim.height - h) / 2);
        startFrame.setVisible(true);
    }

    public void hideStartFrame() {
        startFrame.setVisible(false);

        startFrame = null;
    }

    public void preInit() {
        showStartFrame();
        mainModel = new MainModel();
        // append the exit command to the main model
        mainModel.appendMethod(new ExitCommand());
        // attach the reset command to the main model
        mainModel.appendMethod(new ResetCommand());
        // attach the about command to the main model
        mainModel.appendMethod(new AboutCommand());

        getClientResource().setParent(new ClientResourceImpl("License"));

        ViewerCreatorMap map = getFactory().getViewerCreatorMap();
        map.register(ProgressModel.class, new ProgressViewerCreator());
        map.register(MailArrayModel.class, new MailArrayLayout(), false);
        map.register(MainModel.class, new SwingViewerCreator(MainViewer.class));
        map.register(MailModel.class, new SwingViewerCreator(MailViewer.class));
        map.register(ImportArrayModel.class, new SwingViewerCreator(ImportArrayEditor.class));
//        getFactory().getViewerCreatorMap().registerTreeComponentViewerCreator(MailModel.class, new DataModelTreeComponentCreator() {
//            public DataModelTreeComponent createDataModelTreeComponent(DataModelTreeViewer treeViewer, DataModel model) {
//                return new DataModelTreeComponent(treeViewer, model) {
//
//                    public JPanel getBodyPart() {
//                        setDisplayAsEditor(true);
//                        return super.getBodyPart();
//                    }
//                };
//            }
//        }
//        );
        map.register(AttachmentModel.class, new SwingViewerCreator(AttachmentViewer.class));
        map.register(AttachmentListModel.class, new SwingViewerCreator(AttachmentListViewer.class));
        map.register(ReplaceStringsModel.class, new SwingViewerCreator(ReplaceStringsViewer.class));
        map.register(MailListModel.class, new MailListLayout(), true);
        map.register(ReplacePatternModel.class, new ReplacePatternLayout(), true);
        map.register(LicenseModel.class, new SwingViewerCreator(LicenseViewer.class));
        map.register(OrderLicenseModel.class, new SwingViewerCreator(OrderLicenseViewer.class));
    }

    public void init() {
        try {
            DataViewer viewer = getFactory().createDefaultEditor(mainModel);

            // access the Swing JComponents
            JComponent viewerComponent = viewer.getJComponent();

            // insert generated JComponent into the application window
            getMainPanel().setLayout(new BorderLayout());
            getMainPanel().add(viewerComponent, BorderLayout.CENTER);
            getMainFrame().pack();
            getMainFrame().setIconImage(((ImageIcon) getClientResource().getIcon(getApplicationName())).getImage());
            DialogManager.center(getMainFrame(), true);
            Thread.sleep(2000);
            hideStartFrame();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void postInit() {
        mainModel.addDataModelListener(new MyCommandListener());

        settings = new SettingsModel();

        mainModel.setFileNameModel(settings.getFileNameModel());

        XFile settingsFile = new XFile(SETTINGS_FILENAME);

        if (settingsFile.exists() && settingsFile.canRead()) {
            String fullName = null;
            try {
                PushbackInputStream in = new PushbackInputStream(settingsFile.getBufferedInputStream());
                settings.readDataFrom(in);
                fullName = settings.getFileNameModel().getFullName();
                in.close();
            } catch (Exception e) {
                return;
            }

            try {

                XFile file = new XFile(fullName);

                if (file.canRead()) {
                    mainModel.openFile(fullName);
                    updateApplicationTitle();
                }
            } catch (Exception e) {
                error("Cannot open file: {0}", new Object[] {fullName});
            }
        }

        try {
            this.mainModel.initLicense(getClientResource());

            if (!testLicense()) {
                mainModel.doEditLicense();
            }
        } catch (Exception e) {
            DialogManager.error(e);
        }
    }

    public boolean testLicense() {
        return mainModel.testLicense();
    }

    class ExitCommand extends DefaultDataModelCommand {
        public ExitCommand() {
            super("Exit");
        }

        public void doAction() {
            exit();
        }
    }

    class ResetCommand extends DefaultDataModelCommand {
        public ResetCommand() {
            super("New");
        }

        public void doAction() {
            System.out.println("New");
            getDataModel().clear();
        }
    }

    class AboutCommand extends DefaultDataModelCommand {
        public AboutCommand() {
            super("About");
        }

        public void doAction() {
            System.out.println("About");
            StringModel about = new StringModel("");
            try {
                XFile aboutFile = new XFile("about.htm");

                about.setText(new String(aboutFile.getBytes()));
            } catch (Exception ex) {
                about.setText(ex.getClass().getName());
                ex.printStackTrace();
            }

            DialogManager.getPopupEditorAdaptor().noDuplicatePopupEdit(about, new HTMLViewerCreator(false), null);
        }
    }

    public static void main(String argv[]) {
        new MultiMailApplication(argv).run();
    }

    public String getApplicationTitle() {
        return getClientResource().getLabel(super.getApplicationTitle()) + " [" + mainModel.getFileName() + "]";
    }

    class MyCommandListener extends DataModelAdaptor {
        public void dataModelEdit(DataModelEvent ev) {
            if (ev.getId() == DataModelEvent.CALL_METHOD) {
                updateApplicationTitle();
            }
        }
    }
}
TOP

Related Classes of com.nexirius.multimail.MultiMailApplication$ResetCommand

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.