/*
 * @(#)LoadNextAction.java  2.0  2009-12-22
 *
 * Copyright (c) 2005-2009 Werner Randelshofer, Immensee, Switzerland.
 * All rights reserved.
 *
 * You may not use, copy or modify this file, except in compliance with the
 * license agreement you entered into with Werner Randelshofer.
 * For details see accompanying license terms.
 */
package ch.randelshofer.app.action;

import ch.randelshofer.text.OSXCollator;
import java.awt.*;
import java.text.*;
import java.io.*;
import java.util.*;
import java.awt.event.*;
import java.net.URI;
import org.jhotdraw.app.Application;
import org.jhotdraw.app.View;
import org.jhotdraw.app.action.AbstractViewAction;
import org.jhotdraw.gui.Worker;
import org.jhotdraw.util.ResourceBundleUtil;

/**
 * LoadNextAction.
 *
 * @author  Werner Randelshofer
 * @version 2.0 2009-12-22 Adapted for JHotDraw application framework.
 * <br>1.1 2005-07-10 Use OSXCollator,
 * <br>1.0  04 January 2005  Created.
 */
public class LoadNextAction extends AbstractViewAction {

    public final static String ID = "file.loadNext";

    /** Creates a new instance. */
    public LoadNextAction(Application app, View v) {
        super(app, v);
        ResourceBundleUtil labels = ResourceBundleUtil.getBundle("ch.randelshofer.app.Labels");
        labels.configureAction(this, ID);
    }

    public void actionPerformed(ActionEvent evt) {
        loadNextFile();
    }

    /**
     * Opens the next project file in the same
     * directory where the current project file is located.
     */
    public void loadNextFile() {
        final View view;
        URI uri;
        if ((view = getActiveView()) == null || (uri = view.getURI()) == null) {
            Toolkit.getDefaultToolkit().beep();
            return;
        }

        File file = new File(uri);

        String filename = file.getName();
        File directory = new File(file.getParent());
        String[] list = directory.list();
        if (list == null) {
            Toolkit.getDefaultToolkit().beep();
            return;
        }

        int i;
        Collator comparator = new OSXCollator();
        Arrays.sort(list, comparator);
        for (i = 0;
                i < list.length
                && (comparator.compare(filename, list[i]) >= 0
                || new File(directory, list[i]).isDirectory() == true);
                i++) {
        }
        if (i == list.length) {
            Toolkit.getDefaultToolkit().beep();
        } else {
            final URI nextURI = new File(directory, list[i]).toURI();
            view.setEnabled(false);
            view.execute(new Worker() {

                @Override
                protected Object construct() throws Exception {
                    view.read(nextURI,null);
                    return null;
                }

                @Override
                protected void done(Object value) {
                    view.setURI(nextURI);
                    view.getApplication().addRecentURI(nextURI);
                }

                @Override
                protected void failed(Throwable error) {
                    Toolkit.getDefaultToolkit().beep();
                }

                @Override
                protected void finished() {
                    view.setEnabled(true);
                }
            });
        }
    }
}
