001    /*
002     * $Id: DirectoryOption.java,v 1.10 2012/02/19 17:35:49 davep Exp $
003     *
004     * This file is part of McIDAS-V
005     *
006     * Copyright 2007-2012
007     * Space Science and Engineering Center (SSEC)
008     * University of Wisconsin - Madison
009     * 1225 W. Dayton Street, Madison, WI 53706, USA
010     * https://www.ssec.wisc.edu/mcidas
011     * 
012     * All Rights Reserved
013     * 
014     * McIDAS-V is built on Unidata's IDV and SSEC's VisAD libraries, and
015     * some McIDAS-V source code is based on IDV and VisAD source code.  
016     * 
017     * McIDAS-V is free software; you can redistribute it and/or modify
018     * it under the terms of the GNU Lesser Public License as published by
019     * the Free Software Foundation; either version 3 of the License, or
020     * (at your option) any later version.
021     * 
022     * McIDAS-V is distributed in the hope that it will be useful,
023     * but WITHOUT ANY WARRANTY; without even the implied warranty of
024     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
025     * GNU Lesser Public License for more details.
026     * 
027     * You should have received a copy of the GNU Lesser Public License
028     * along with this program.  If not, see http://www.gnu.org/licenses.
029     */
030    
031    package edu.wisc.ssec.mcidasv.startupmanager.options;
032    
033    import java.awt.BorderLayout;
034    import java.awt.Dimension;
035    import java.awt.event.ActionEvent;
036    import java.awt.event.ActionListener;
037    import java.io.File;
038    
039    import javax.swing.JCheckBox;
040    import javax.swing.JComponent;
041    import javax.swing.JLabel;
042    import javax.swing.JPanel;
043    import javax.swing.JScrollPane;
044    import javax.swing.JTree;
045    import javax.swing.ToolTipManager;
046    import javax.swing.event.TreeSelectionEvent;
047    import javax.swing.event.TreeSelectionListener;
048    import javax.swing.tree.DefaultMutableTreeNode;
049    import javax.swing.tree.TreePath;
050    import javax.swing.tree.TreeSelectionModel;
051    
052    import edu.wisc.ssec.mcidasv.ArgumentManager;
053    import edu.wisc.ssec.mcidasv.startupmanager.StartupManager;
054    import edu.wisc.ssec.mcidasv.startupmanager.StartupManager.TreeCellRenderer;
055    import edu.wisc.ssec.mcidasv.startupmanager.options.OptionMaster.OptionPlatform;
056    import edu.wisc.ssec.mcidasv.startupmanager.options.OptionMaster.Type;
057    import edu.wisc.ssec.mcidasv.startupmanager.options.OptionMaster.Visibility;
058    
059    public class DirectoryOption extends AbstractOption {
060        private DefaultMutableTreeNode selected = null;
061        private String value = "";
062        private final String defaultValue;
063    
064        public DirectoryOption(final String id, final String label, final String defaultValue, final OptionPlatform optionPlatform, final Visibility optionVisibility) {
065            super(id, label, Type.DIRTREE, optionPlatform, optionVisibility);
066            this.defaultValue = defaultValue;
067    
068            setValue(defaultValue);
069        }
070    
071        private void exploreDirectory(final String directory, final DefaultMutableTreeNode parent) {
072            assert directory != null : "Cannot traverse a null directory";
073    
074            File dir = new File(directory);
075            assert dir.exists() : "Cannot traverse a directory that does not exist";
076    
077            for (File f : dir.listFiles()) {
078                DefaultMutableTreeNode current = new DefaultMutableTreeNode(f);
079                if (f.isDirectory()) {
080                    parent.add(current);
081                    exploreDirectory(f.getPath(), current);
082                } else if (ArgumentManager.isBundle(f.getPath())){
083                    parent.add(current);
084                    if (f.getPath().equals(getUnquotedValue()))
085                        selected = current;
086                }
087            }
088        }
089    
090        private DefaultMutableTreeNode getRootNode(final String path) {
091            File bundleDir = new File(path);
092            if (bundleDir.isDirectory()) {
093                DefaultMutableTreeNode root = new DefaultMutableTreeNode(bundleDir);
094                return root;
095            }
096            return null;
097        }
098    
099        private void useSelectedTreeValue(final JTree tree) {
100            assert tree != null : "cannot use a null JTree";
101            DefaultMutableTreeNode node = (DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
102    
103            File f = (File)node.getUserObject();
104            if (f.isDirectory())
105                setValue(defaultValue);
106            else
107                setValue(node.toString());
108    
109            TreePath nodePath = new TreePath(node.getPath());
110            tree.setSelectionPath(nodePath);
111            tree.scrollPathToVisible(nodePath);
112        }
113        
114        public JComponent getComponent() {
115            
116            JPanel panel = new JPanel(new BorderLayout());
117    
118            String path = StartupManager.INSTANCE.getPlatform().getUserBundles();
119            DefaultMutableTreeNode root = getRootNode(path);
120            if (root == null) return panel;
121    //            throw new AssertionError("Directory missing; can't traverse "+path);
122            final JTree tree = new JTree(root);
123            tree.addTreeSelectionListener(new TreeSelectionListener() {
124                public void valueChanged(final TreeSelectionEvent e) {
125                    useSelectedTreeValue(tree);
126                }
127            });
128            tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
129            JScrollPane scroller = new JScrollPane(tree);
130            exploreDirectory(path, root);
131    
132            ToolTipManager.sharedInstance().registerComponent(tree);
133            tree.setCellRenderer(new TreeCellRenderer());
134            scroller.setPreferredSize(new Dimension(140,130));
135            tree.expandRow(0);
136    
137            if (selected != null) {
138                TreePath nodePath = new TreePath(selected.getPath());
139                tree.setSelectionPath(nodePath);
140                tree.scrollPathToVisible(nodePath);
141            }
142            
143            final JCheckBox enabled = new JCheckBox("Specify default bundle:", true);
144            enabled.addActionListener(new ActionListener() {
145                public void actionPerformed(final ActionEvent e) {
146                    tree.setEnabled(enabled.isSelected());
147                    if (!tree.isEnabled()) {
148                        setValue(defaultValue);
149                    } else {
150                        useSelectedTreeValue(tree);
151                    }
152                }
153            });
154    
155            panel.add(enabled, BorderLayout.PAGE_START);
156            panel.add(scroller, BorderLayout.PAGE_END);
157            return panel;
158        }
159    
160        public String getValue() {
161            return "\""+value+"\"";
162        }
163    
164        public String getUnquotedValue() {
165            return value;
166        }
167    
168        public void setValue(final String newValue) {
169            value = newValue.replaceAll("\"", "");
170        }
171    
172        public String toString() {
173            return String.format("[DirectoryOption@%x: optionId=%s, value=%s]", hashCode(), getOptionId(), getValue());
174        }
175    }