Newer
Older
KisoJikkenNWP / src / main / java / istlab / KisoJikken / Launcher.java
package istlab.KisoJikken;

import java.awt.BorderLayout;
import java.awt.event.MouseEvent;
import java.io.File;
import java.util.Arrays;
import java.util.Hashtable;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.event.MouseInputListener;
import javax.swing.event.TreeModelListener;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;

public class Launcher extends JFrame implements MouseInputListener {

    public static Launcher theapp;
    // JPanel mainP;
    JTree tree;
    Hashtable<File, Editor> file2editor;

    public Launcher(String[] args) {
        super("Kiso NWP Launcher");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        // mainP = new JPanel();
        file2editor = new Hashtable<File,Editor>();
        theapp = this;

        File root;
        if (args.length > 0)
            root = new File(args[0]);
        else
            root = new File(System.getProperty("user.home") + "/NWP/src");

        // Create a TreeModel object to represent our tree of files
        FileTreeModel model = new FileTreeModel(root);

        // Create a JTree and tell it to display our model
        tree = new JTree();
        tree.setModel(model);
        tree.addMouseListener(this);

        // The JTree can get big, so allow it to scroll.
        JScrollPane scrollpane = new JScrollPane(tree);
        // mainP.setLayout(new BoxLayout(mainP, BoxLayout.PAGE_AXIS));
        // for (int i = 1; i < 8; i++) {
        // JButton b = new JButton("j" + i);
        // mainP.add(b);
        // }
        // ShellScriptPanel run11 = new ShellScriptPanel("run1-1.sh");
        getContentPane().add(scrollpane, BorderLayout.CENTER);

        // getContentPane().add(mainP, BorderLayout.WEST);
        setSize(400, 600);
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        if (e.getClickCount() == 2) {
            TreePath tp = tree.getSelectionModel().getSelectionPath();
            File node = (File) tp.getLastPathComponent();
            Editor ed = file2editor.get(node);
            if (ed==null) {
                ed = new Editor(node);
                file2editor.put(node,ed);
            } else {
                ed.setVisible(true);
                ed.toFront();
            }
            System.out.println(node.toString());
        }

    }

    @Override
    public void mousePressed(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseReleased(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseEntered(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseExited(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseDragged(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseMoved(MouseEvent e) {
        // TODO Auto-generated method stub

    }

}

/**
 * The methods in this class allow the JTree component to traverse
 * the file system tree, and display the files and directories.
 **/
class FileTreeModel implements TreeModel {

    // We specify the root directory when we create the model.
    protected File root;

    public FileTreeModel(File root) {
        this.root = root;
    }

    // The model knows how to return the root object of the tree
    public Object getRoot() {
        return root;
    }

    // Tell JTree whether an object in the tree is a leaf or not
    public boolean isLeaf(Object node) {
        return ((File) node).isFile();
    }

    // Tell JTree how many children a node has
    public int getChildCount(Object parent) {
        String[] children = ((File) parent).list();
        if (children == null)
            return 0;
        return children.length;
    }

    // Fetch any numbered child of a node for the JTree.
    // Our model returns File objects for all nodes in the tree. The
    // JTree displays these by calling the File.toString() method.
    public Object getChild(Object parent, int index) {
        String[] children = ((File) parent).list();
        Arrays.sort(children);
        if ((children == null) || (index >= children.length)) return null;
        return new File((File) parent, children[index]);
    }

    // Figure out a child's position in its parent node.
    public int getIndexOfChild(Object parent, Object child) {
        String[] children = ((File) parent).list();
        Arrays.sort(children);
        if (children == null) return -1;
        String childname = ((File) child).getName();
        // if (childname.startsWith(".")) return -1;
        for (int i = 0; i < children.length; i++) {
            if (childname.equals(children[i]))
                return i;
        }
        return -1;
    }

    // This method is only invoked by the JTree for editable trees.
    // This TreeModel does not allow editing, so we do not implement
    // this method. The JTree editable property is false by default.
    public void valueForPathChanged(TreePath path, Object newvalue) {
    }

    // Since this is not an editable tree model, we never fire any events,
    // so we don't actually have to keep track of interested listeners.
    public void addTreeModelListener(TreeModelListener l) {
    }

    public void removeTreeModelListener(TreeModelListener l) {
    }
}