Package net.sf.jpluck.swing

Source Code of net.sf.jpluck.swing.FileHistoryMenu$FileAction

package net.sf.jpluck.swing;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class FileHistoryMenu extends JMenu {
    private int maxSize;
    private List actionList = new ArrayList();

    public FileHistoryMenu(int maxSize, File[] files) {
        this.maxSize = maxSize;
        setEnabled(files.length > 0);
        for (int i = 0; i < files.length; i++) {
            File file = files[i];
            FileAction fileAction = new FileAction(file);
            actionList.add(fileAction);
            add(fileAction);
        }
        setCaptions();
    }

    public void updateUI() {
        super.updateUI();
        for (int i = 0, n = getItemCount(); i < n; i++) {
            getItem(i).updateUI();
        }
    }

    public void add(File file) {
        setEnabled(true);
        FileAction newFileAction = null;
        for (int i = 0,n = actionList.size(); i < n; i++) {
            FileAction fileAction = (FileAction) actionList.get(i);
            if (fileAction.getFile().equals(file)) {
                int idx = actionList.indexOf(fileAction);
                actionList.remove(idx);
                remove(idx);
                newFileAction = fileAction;
                break;
            }
        }
        if (newFileAction == null) {
            newFileAction = new FileAction(file);
        }
        actionList.add(0, newFileAction);
        insert(newFileAction, 0);
        if (actionList.size() > maxSize) {
            remove(actionList.size() - 1);
            actionList.remove(actionList.size() - 1);
        }
        setCaptions();
    }

    private void setCaptions() {
        for (int i = 0,n = actionList.size(); i < n; i++) {
            FileAction fileAction = (FileAction) actionList.get(i);
            fileAction.putValue(Action.NAME, i + 1 + " " + fileAction.getFile().getName());
            //fileAction.putValue(Action.SHORT_DESCRIPTION,);
            JMenuItem menuItem = getItem(i);
            menuItem.setMnemonic(i + 49);
            menuItem.setToolTipText(fileAction.getFile().getAbsolutePath());
        }
    }

    public void remove(File file) {
        for (int i = 0,n = actionList.size(); i < n; i++) {
            FileAction fileAction = (FileAction) actionList.get(i);
            if (fileAction.getFile().equals(file)) {
                int idx = actionList.indexOf(fileAction);
                actionList.remove(idx);
                remove(idx);
                break;
            }
        }
        setEnabled(actionList.size() > 0);
        setCaptions();
    }

    public File[] getFiles() {
        List fileList = new ArrayList();
        for (Iterator iterator = actionList.iterator(); iterator.hasNext();) {
            FileAction action = (FileAction) iterator.next();
            fileList.add(action.getFile());
        }
        return (File[]) fileList.toArray(new File[fileList.size()]);
    }


    public class FileAction extends AbstractAction {
        private File file;

        public FileAction(File file) {
            this.file = file;
        }

        public void actionPerformed(ActionEvent e) {
            ActionListener[] actionListeners = getActionListeners();
            for (int i = 0; i < actionListeners.length; i++) {
                ActionListener actionListener = actionListeners[i];
                actionListener.actionPerformed(new ActionEvent(this, actionList.indexOf(this), "open"));
            }
        }

        public File getFile() {
            return file;
        }
    }
}
TOP

Related Classes of net.sf.jpluck.swing.FileHistoryMenu$FileAction

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.