001/*
002 * Copyright (c) 2010 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: barseghian $'
006 * '$Date: 2011-11-22 01:13:06 +0000 (Tue, 22 Nov 2011) $' 
007 * '$Revision: 28984 $'
008 * 
009 * Permission is hereby granted, without written agreement and without
010 * license or royalty fees, to use, copy, modify, and distribute this
011 * software and its documentation for any purpose, provided that the above
012 * copyright notice and the following two paragraphs appear in all copies
013 * of this software.
014 *
015 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
016 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
017 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
018 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
019 * SUCH DAMAGE.
020 *
021 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
022 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
023 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
024 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
025 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
026 * ENHANCEMENTS, OR MODIFICATIONS.
027 *
028 */
029
030package org.kepler.modulemanager;
031
032import java.io.File;
033import java.net.URL;
034
035import javax.swing.DefaultListModel;
036import javax.swing.JLabel;
037import javax.swing.JList;
038import javax.swing.JOptionPane;
039import javax.swing.JPanel;
040import javax.swing.JScrollPane;
041import javax.swing.event.ListSelectionEvent;
042import javax.swing.event.ListSelectionListener;
043
044import org.jdesktop.layout.GroupLayout;
045import org.kepler.build.modules.Module;
046import org.kepler.build.modules.ModuleTree;
047import org.kepler.configuration.ConfigurationManager;
048
049import ptolemy.actor.gui.BrowserLauncher;
050
051
052/**
053 * Created by Debi Staggs
054 * to facilitate the viewing of the
055 * module documentation
056 * 
057 * This will list only those active modules for which documentation has been installed.
058 * IMPORTANT:  In order for this to work correctly, the documentation must be named
059 * the same as the module, but without the version number.  Also it must be in .pdf format.
060 * 
061 * Date: 06/10/2010
062 * Time: 2:36:12 PM
063 */
064public class ActiveModulesDocumentationPanel extends JPanel {
065        
066        private JLabel activeModulesListLabel = new JLabel("Module Documentation available:");
067        private JList activeModulesList = new JList();
068        JScrollPane activeModulesListScrollPane = new JScrollPane(activeModulesList);
069        private static final String WIKI_DOCLIST_URL = "https://kepler-project.org/users/documentation";
070        private String extension = ".pdf";
071        private String modulename;
072        private File tmpfl;
073        private String docFilePath;
074        private String basePath;
075    
076   //JButton btnViewDocs = new JButton("View Documentation");
077   ConfigurationManager cman = ConfigurationManager.getInstance();
078
079        public ActiveModulesDocumentationPanel() {
080                super();
081                initActiveModulesList();
082                initComponents();
083                layoutComponents();
084        }
085
086        /**
087        *  This initializes the ActiveModulesList from the ModuleTree instance, iterates through the
088        *  tree, getting each module name + stripping out the version number, it then uses that to
089        *  get the local path to the documentation file, and checks to see if it exists.
090        *  If it is there, it adds the filename to the list and 
091        */
092        private void initActiveModulesList()
093    {
094                
095                DefaultListModel listModel = new DefaultListModel();
096        ModuleTree.init();
097        
098        // rollcursor = new Cursor(Cursor.HAND_CURSOR);
099
100        for(Module module : ModuleTree.instance()) {
101                modulename = module.getStemName().toString();
102                docFilePath = getDocFilePath(modulename);
103                        
104                try {
105                        File tmpfl = new File(docFilePath);
106                        if (tmpfl.exists() && tmpfl.canRead()){
107                                listModel.addElement(modulename);
108                        } else {
109                                // the file does not exist
110                        }
111                } catch (Exception e) {
112                        // exception
113                }
114        }
115        
116        activeModulesList.setModel(listModel);
117    }
118        
119
120        private void initComponents() {
121
122                activeModulesList.addListSelectionListener(new ListSelectionListener() {
123
124                        public void valueChanged(ListSelectionEvent e) {
125
126                                if (activeModulesList.getSelectedIndex() == -1) {
127                                        // No selection, disable fire button.
128                                        // fireButton.setEnabled(false);
129
130                                } else {
131                                        getDocFile();
132                                }
133
134                                activeModulesList.clearSelection();
135
136                        }
137                });
138        }
139        
140        private String getDocFilePath(String modulename) {
141                
142                // find the right directory & build the path
143                tmpfl= ConfigurationManager.getModule(modulename).getDir();
144                basePath = tmpfl.getAbsolutePath();
145                docFilePath = (basePath + "/docs/" + modulename + extension); 
146                return docFilePath;
147                
148        }
149
150        private void getDocFile() {
151                
152                Object selectedmodule = activeModulesList.getSelectedValue();
153                        
154                try {
155                        
156                        docFilePath = getDocFilePath(selectedmodule.toString());
157                        System.out.println("docFilePath:"+docFilePath);
158                        File file = new File(docFilePath);
159                        URL url = file.toURI().toURL();
160                        BrowserLauncher.openURL(url.toString());
161                        
162                } catch (Exception e) {
163                        JOptionPane.showMessageDialog(this.getParent(), "The Documentation for the module you have selelected \n" 
164                                        + "is not available, please try locating it at: \n" + WIKI_DOCLIST_URL, null, JOptionPane.WARNING_MESSAGE);
165                 }
166                
167        }
168
169        private void layoutComponents() {
170                GroupLayout layout = new GroupLayout(this);
171                setLayout(layout);
172                layout.setAutocreateContainerGaps(true);
173                layout.setAutocreateGaps(true);
174
175                layout.setHorizontalGroup(layout.createParallelGroup(
176                                GroupLayout.TRAILING).add(activeModulesListLabel).add(
177                                activeModulesListScrollPane).add(
178                                layout.createSequentialGroup())//.add(btnViewDocs))
179
180                );
181
182                layout.setVerticalGroup(layout.createSequentialGroup().add(
183                                activeModulesListLabel).add(activeModulesListScrollPane).add(
184                                layout.createParallelGroup())//.add(btnViewDocs))
185
186                );
187        }
188
189        
190}