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    
005    /*
006     * UI.java
007     *
008     * Created on March 2, 2005, 9:38 AM
009     */
010    package graphlab.ui;
011    
012    import graphlab.platform.core.AbstractAction;
013    import graphlab.platform.core.BlackBoard;
014    import graphlab.platform.extension.ExtensionLoader;
015    import graphlab.platform.StaticUtils;
016    import graphlab.platform.plugin.Plugger;
017    import graphlab.ui.actions.UIEventHandler;
018    import graphlab.ui.components.GFrame;
019    import graphlab.ui.components.utils.GFrameLocationProvider;
020    import graphlab.ui.extension.UIActionExtensionHandler;
021    import graphlab.ui.xml.UIHandlerImpl;
022    import graphlab.ui.xml.UIParser;
023    import org.xml.sax.InputSource;
024    import org.xml.sax.SAXException;
025    
026    import javax.xml.parsers.ParserConfigurationException;
027    import java.io.*;
028    import java.util.Enumeration;
029    import java.util.HashMap;
030    import java.util.jar.JarEntry;
031    import java.util.jar.JarFile;
032    
033    /**
034     * this class is the base class of UI actions,
035     * it contains the methods to load UserInterfaces from XML
036     * it contains the methods to access different parts of a UI...
037     *
038     * @author Azin Azadi
039     */
040    public class UI {
041        public static final String name = "GraphUI.UI";
042    
043        BlackBoard blackboard = null;
044    
045        GFrame frame;
046    
047    
048        public HashMap<String, AbstractAction> actions = null;
049    
050        /**
051         * constructor
052         *
053         * @param bb
054         * @param visible
055         */
056        public UI(BlackBoard bb, boolean visible) {
057            this.blackboard = bb;
058            //add the UI to the blackboard so the plugins can get it from blackboard
059            bb.setData(name, this);
060            frame = new GFrame(blackboard);
061            frame.setLocation(GFrameLocationProvider.getLocation());
062            frame.setSize(GFrameLocationProvider.getSize());
063            frame.validate();
064            // frame.pack();
065    //        frame.setVisible(visible);
066            actions = new HashMap<String, AbstractAction>();
067            blackboard.setData(UIEventHandler.ACTIONS_MAP, actions);
068            //initialize the event handler to handle menu and toolbar events
069            new UIEventHandler(blackboard);
070            ExtensionLoader.registerExtensionHandler(new UIActionExtensionHandler());
071        }
072    
073        /**
074         * fek mikonam ke in add xml kare plugin ha ro rah bendaze, vali hala kheili
075         * ghatiam, in tarif methoda ro ehtemalan taghir baies dad.
076         *
077         * @param XMLFilePath
078         * @throws IOException
079         */
080        public void addXML(String XMLFilePath, Class resClass) throws IOException, SAXException {
081            loadXML(XMLFilePath, resClass);
082        }
083    
084        public void loadXML(String XMLFilePath, Class resClass) throws IOException, SAXException {
085            loadXML(XMLFilePath, resClass, frame);
086        }
087    
088        //this method is not tested yet
089        public void addXMLFromString(String XMLString, Class resClass) throws IOException, SAXException {
090            UIHandlerImpl i = new UIHandlerImpl(frame, blackboard, actions, resClass);
091            try {
092                UIParser.parse(new InputSource(XMLString), i);
093            } catch (SAXException e) {
094                throw e;
095            } catch (ParserConfigurationException e) {
096                e.printStackTrace();
097            }
098    
099        }
100    
101        public void loadXML(String XMLFilePath, Class resClass, GFrame frame
102        ) throws IOException, SAXException {
103            UIHandlerImpl hi = new UIHandlerImpl(frame, blackboard, actions,
104                    resClass);
105            try {
106                if (resClass == null)
107                    UIParser.parse(new InputSource(XMLFilePath), hi);
108                else
109                    UIParser.parse(resClass.getResource(XMLFilePath), hi);
110            } catch (SAXException e) {
111                throw e;
112            } catch (ParserConfigurationException e) {
113                e.printStackTrace();
114            }
115            frame.validate();
116        }
117    
118        //todo: no usage of this method,
119        public static String extractHelpPlugin(BlackBoard blackboard, Plugger plugger, String index, String dest, String filter) {
120            try {
121                File f = new File(dest);
122                if (!f.isDirectory())
123                    f.mkdir();
124                f = new File(dest + "/" + index);
125                if (!f.isFile()) {
126                    JarFile jarFile = new JarFile(plugger.files.get("help"));
127                    Enumeration<JarEntry> entries = jarFile.entries();
128                    while (entries.hasMoreElements()) {
129                        JarEntry je = entries.nextElement();
130    
131                        if (!je.getName().startsWith(filter))
132                            continue;
133    
134                        System.out.println("Extracting " + je.getName());
135                        String fname = je.getName().substring(filter.length());
136    
137                        if (je.isDirectory())
138                            (new File(dest + "/" + fname)).mkdir();
139                        else {
140                            File efile = new File(dest, fname);
141    
142                            InputStream in = new BufferedInputStream(jarFile
143                                    .getInputStream(je));
144                            OutputStream out = new BufferedOutputStream(
145                                    new FileOutputStream(efile));
146                            byte[] buffer = new byte[2048];
147                            for (; ;) {
148                                int nBytes = in.read(buffer);
149                                if (nBytes <= 0)
150                                    break;
151                                out.write(buffer, 0, nBytes);
152                            }
153                            out.flush();
154                            out.close();
155                            in.close();
156                        }
157                    }
158                }
159                return f.getAbsolutePath();
160            } catch (Exception e) {
161                StaticUtils.addExceptiontoLog(e, blackboard);
162            }
163            return null;
164        }
165    
166    
167        public GFrame getGFrame() {
168            return frame;
169        }
170    
171    //    public void sendUIEvent(UIEventData ued){
172    //
173    //    }
174    
175        //---------------------------------------------------------------------------------
176    //    public static GTP
177    }