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