Package de.timefinder.core.ui.command

Source Code of de.timefinder.core.ui.command.TimeFinderCommand

/*
*  Copyright 2009 Peter Karich, peat_hal ‘at’ users ‘dot’ sourceforge ‘dot’ net.
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*       http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*  under the License.
*/
package de.timefinder.core.ui.command;

import de.timefinder.core.util.ApplicationSettings;
import de.timefinder.core.util.Translator;
import de.timefinder.data.DataPool;
import de.timefinder.data.access.Dao;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import javax.swing.SwingWorker;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.richclient.application.ApplicationServicesLocator;
import org.springframework.richclient.application.PageComponent;
import org.springframework.richclient.application.ViewDescriptor;
import org.springframework.richclient.application.ViewDescriptorRegistry;
import org.springframework.richclient.application.statusbar.StatusBar;
import org.springframework.richclient.command.support.ApplicationWindowAwareCommand;
import org.springframework.richclient.util.Assert;

/**
* @author Peter Karich, peat_hal ‘at’ users ‘dot’ sourceforge ‘dot’ net
*/
public abstract class TimeFinderCommand extends ApplicationWindowAwareCommand {

    protected Translator tr;
    private Log log = LogFactory.getLog(getClass());
    private boolean finished = true;
    protected DataPool dataPool;
    protected ApplicationSettings appSettings;

    public TimeFinderCommand(String id) {
        super(id);
    }

    @Autowired
    public void setApplicationSettings(ApplicationSettings appSettings) {
        this.appSettings = appSettings;
    }

    @Autowired
    public void setTranslator(Translator tr) {
        Assert.notNull(tr);
        this.tr = tr;
    }

    @Autowired
    public void setDataPool(DataPool dataPool) {
        this.dataPool = dataPool;
    }

    @Override
    final protected void doExecuteCommand() {
        if (isFinished()) {
            doOnce();
        } else {
            JOptionPane.showMessageDialog(null, tr.get("alreadyStarted"));
        }
    }

    /**
     * Within this method start a SwingWorker or only small/fast code pieces
     */
    protected abstract void doOnce();

    public boolean isFinished() {
        return finished;
    }

    public void setFinished(boolean finished) {
        this.finished = finished;
    }

    /**
     * Handles the finished method and the exceptions.
     */
    public abstract class MySwingWorker extends SwingWorker {

        private String ID;
        private Throwable exception;

        public MySwingWorker(String id) {
            ID = id;
        }

        public void setException(Throwable ex) {
            exception = ex;
        }

        public Throwable getException() {
            return exception;
        }

        protected abstract void myconstruct() throws Exception;

//        protected Object construct() {
        @Override
        protected Object doInBackground() throws Exception {
            setFinished(false);
            try {               
                myconstruct();
            } catch (Exception ex) {
                ex.printStackTrace();
                log.error(tr.get(ID + ".exception"), ex);
                setException(ex);
            }

            return null;
        }

        @Override
        protected void done() {
            // all of the following code will be called on the Event Dispatching Thread
            StatusBar bar = getApplicationWindow().getStatusBar();
            if (bar.getProgressMonitor().isCanceled()) {
                bar.setErrorMessage(tr.get(ID + ".canceled"));
            } else {
                if (getException() != null)
                    bar.setErrorMessage(tr.get(ID + ".exception") + ": "
                            + getException().getLocalizedMessage());
                else if (getException() != null && getException().getCause() != null)
                    bar.setErrorMessage(tr.get(ID + ".exception") + ": "
                            + getException().getCause().getLocalizedMessage());
                else
                    bar.setMessage(tr.get(ID + ".finished"));
            }
            bar.getProgressMonitor().done();
            setFinished(true);
        }
    }

    public void closeAllViews() {
//        ViewDescriptorRegistry viewDescriptorRegistry = (ViewDescriptorRegistry) ApplicationServicesLocator.services().getService(ViewDescriptorRegistry.class);
//        ViewDescriptor[] views = viewDescriptorRegistry.getViewDescriptors();
//        for (int i = 0; i < views.length; i++) {
//            ViewDescriptor view = views[i];
//            view.getId();
//            TODO how to get a pageComponent from the id?
//            getApplicationWindow().getPage().close(pageComponent);
//        }

        for (PageComponent active : new ArrayList<PageComponent>(
                getApplicationWindow().getPage().getPageComponents())) {
            if (dataViews.contains(active.getId()))
                getApplicationWindow().getPage().close(active);
        }
    }
    private static final String dataViews = "personView, eventView, locationView";

    public void openAllViews() {
        ViewDescriptorRegistry viewDescriptorRegistry = (ViewDescriptorRegistry) ApplicationServicesLocator.services().getService(ViewDescriptorRegistry.class);

        ViewDescriptor[] views = viewDescriptorRegistry.getViewDescriptors();
        for (int i = 0; i < views.length; i++) {
            ViewDescriptor view = views[i];
            if (dataViews.contains(view.getId()))
                view.createShowViewCommand(getApplicationWindow()).execute();
        }
//        new OpenAllViews(getApplicationWindow()).doExecuteCommand();
    }

    protected void clear() {
        for (Dao dao : dataPool.getDaos()) {
            dao.detachAll();
        }
    }
}
TOP

Related Classes of de.timefinder.core.ui.command.TimeFinderCommand

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.