Package de.timefinder.core.ui.command

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

/*
* Copyright 2008 the original author or authors.
*
* 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.
*/
package de.timefinder.core.ui.command;

import de.timefinder.data.algo.DataPoolSettings;
import de.timefinder.algo.io.DataPool4Track2;
import de.timefinder.core.ui.StatusBarBridge;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.Reader;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import org.springframework.core.io.UrlResource;
import org.springframework.richclient.application.Application;
import org.springframework.richclient.application.ApplicationWindow;
import org.springframework.richclient.application.statusbar.StatusBar;

/**
* This creates a command to open a file with the special format
* from the International Timetabling Competition 2008.
*
* @see de.timefinder.core.gstpl.data.DataPool4Track2
* @author Peter Karich
*/
public class ImportTimFileCommand extends AbstractImportCommand {

    public static final String ID = "importTimFile";
    private DataPool4Track2 track2allData;
    private boolean download = false;
    private Reader reader;

    /**
     * Creates a new {@code importTimFileCommand} with an id of {@value #ID}.
     */
    public ImportTimFileCommand() {
        super(ID);
    }

    public ImportTimFileCommand(boolean download) {
        super("downloadTimFile");
        this.download = download;
    }

    public void setAllData(DataPool4Track2 dataPool) {
        this.track2allData = dataPool;
        setDataPool(dataPool.getDataPool());
    }

    /**
     * Starts a new frame and close the active window.
     *
     * @see Application#close()
     */
    @Override
    protected void doOnce() {
        checkIfDataIsAlreadyAvailable();
        closeAllViews();

        DataPoolSettings settings = track2allData.getSettings();

        int ret = JOptionPane.showConfirmDialog(null, tr.get(ID + ".question",
                settings.getNumberOfDays(), settings.getTimeslotsPerDay()));
        if (ret != JOptionPane.OK_OPTION)
            return;

        track2allData.changeSettings();
        ApplicationWindow aw = getApplicationWindow();
        final StatusBar bar = aw.getStatusBar();

        try {
            if (download) {
                // see MySwingWorker.construct -> it could be that site is not accessible
                // -> then the ui should not freeze
            } else {
                JFileChooser fc = new JFileChooser();
                fc.setDialogTitle(tr.get(ID + ".fileDialog"));
                int resultFC = fc.showOpenDialog(aw.getControl());
                if (resultFC != JFileChooser.APPROVE_OPTION) {
                    bar.setMessage(tr.get(ID + ".noFileSelected"));
                    return;
                }
                file = fc.getSelectedFile();
                if (!file.getName().endsWith(".tim")) {
                    int resultOP = JOptionPane.showConfirmDialog(null, tr.get(ID + ".confirmation.message"),
                            tr.get(ID + ".confirmation.title"), JOptionPane.OK_CANCEL_OPTION);
                    if (resultOP != JOptionPane.OK_OPTION) {
                        return;
                    }
                }
                reader = new FileReader(file);
            }
        } catch (Exception ex) {
            String errorMsg = "Cannot read file! " + ex.getLocalizedMessage();
            logger.fatal(errorMsg, ex);
            bar.setErrorMessage(errorMsg);
            return;
        }

        bar.getProgressMonitor().taskStarted(tr.get(ID + ".startTask"), 100);       
        MySwingWorker sw = new MySwingWorker(ID) {

            @Override
            protected void myconstruct() throws Exception {
                if (download) {
                    String str = "http://timefinder.sourceforge.net/files/comp-2007-2-09.tim";
                    bar.setMessage(tr.get("downloadFrom", str));
                    // why does decompressing not work?? -> first line == null !?
                    // reader = new InputStreamReader(new ZipInputStream(
                    //          new UrlResource("http://timefinder.sourceforge.net/files/comp-2007-2-09.zip").getInputStream()));
                    reader = new InputStreamReader(new UrlResource(str).getInputStream());
                    bar.setMessage("");
                }

                track2allData.setSource(reader);
                track2allData.setStatusBar(new StatusBarBridge(bar));
                track2allData.doWork();
                bar.getProgressMonitor().worked(90);
            }

            @Override
            protected void done() {
                openAllViews();
                bar.getProgressMonitor().worked(100);
                super.done();
            }
        };

        sw.execute();
    }
}
TOP

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

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.