001    // GraphLab Project: http://graphlab.sharif.edu
002    // Copyright (C) 2008 Mathematical Science Department of Sharif University of Technology
003    // Distributed under the terms of the GNU General Public License (GPL): http://www.gnu.org/licenses/
004    package graphlab.plugins.commandline;
005    
006    import graphlab.graph.atributeset.GraphAttrSet;
007    import graphlab.graph.graph.GraphModel;
008    import graphlab.graph.ui.GHTMLPageComponent;
009    import graphlab.platform.Application;
010    import graphlab.platform.core.BlackBoard;
011    import graphlab.platform.core.Listener;
012    import graphlab.platform.extension.ExtensionLoader;
013    import graphlab.platform.plugin.PluginInterface;
014    import graphlab.plugins.commandline.extensionloader.BSHExtensionLoader;
015    import graphlab.ui.ExtensionShellCommandProvider;
016    import graphlab.ui.UI;
017    import org.xml.sax.SAXException;
018    
019    import java.io.IOException;
020    
021    /**
022     * @author Mohammad Ali Rostami
023     * @email ma.rostami@yahoo.com
024     */
025    
026    public class Init implements PluginInterface {
027        Shell shell;
028        BlackBoard bb;
029    
030        public void init(BlackBoard blackboard) {
031            bb = blackboard;
032            UI ui = blackboard.getData(UI.name);
033            try {
034                ui.addXML("/graphlab/plugins/commandline/config.xml", getClass());
035                shell = new Shell(blackboard);
036    
037                shell.performJob("SS");
038                bb.setData(Shell.NAME, shell);
039    
040                ExtensionLoader.registerUnknownExtensionLoader(new BSHExtensionLoader(shell));
041    
042                GHTMLPageComponent.registerHyperLinkHandler("BSH", new ShellHyperlinkHandler(shell));
043                blackboard.addListener(Application.POST_INIT_EVENT, new Listener() {
044                    public void keyChanged(String key, Object value) {
045                        postInit();
046                    }
047                });
048    
049            } catch (IOException e) {
050                e.printStackTrace();
051                System.out.println("xml file was not found , or IO error");
052    
053            } catch (SAXException e) {
054                e.printStackTrace();
055            }
056        }
057    
058        void postInit() {
059            for (ExtensionShellCommandProvider c : ExtensionShellCommandProvider.commands) {
060                try {
061                    String var = shell.newVariable();
062                    shell.set_variable("_" + c.trgClass.getClass().getSimpleName(), c.trgClass);
063                    shell.set_variable(var, c.ths);
064                    c.command += c.help
065                            + "return " + var + "." + "performExtensionInCommandLine()" + ";"
066                            + "}";
067                    shell.evaluateCommand(c.command, c.name, c.abrv);
068                    shell.addCodeCompletionDictionary(c.name, c.trgClass.getClass());
069                } catch (Exception e) {
070                    e.printStackTrace();
071                }
072            }
073    
074            // G0 Initialization
075            GraphModel gm = bb.getData(GraphAttrSet.name);
076            if (gm != null)
077                shell.set_variable(gm.getLabel(), gm);
078            shell.ext_console.shell = shell;
079        }
080    
081    }