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.main.saveload.core.extension;
005    
006    import graphlab.graph.graph.GraphModel;
007    import graphlab.graph.ui.GTabbedGraphPane;
008    import graphlab.platform.core.BlackBoard;
009    import graphlab.plugins.main.saveload.ExampleFileFilter;
010    import graphlab.plugins.main.saveload.core.GraphIOException;
011    import graphlab.ui.AbstractExtensionAction;
012    
013    import javax.swing.*;
014    import java.io.File;
015    import java.io.IOException;
016    
017    /**
018     * @author azin azadi
019    
020     */
021    public class GraphReaderExtensionAction extends AbstractExtensionAction {
022        private GraphReaderExtension gi;
023        private String prefix;
024    
025        public GraphReaderExtensionAction(BlackBoard bb, GraphReaderExtension gi) {
026            super(bb, gi);
027            this.gi = gi;
028        }
029    
030        public String getMenuNamePrefix() {
031            return "";
032        }
033    
034        public String getParentMenuName() {
035            return "File.Load Graph From";
036        }
037    
038        public void performExtension() {
039            try {
040                importGraph();
041            }
042            catch (Exception e) {
043                e.printStackTrace();
044            }
045        }
046    
047        String ss;
048        GraphModel g;
049        IOException ee;
050        GraphIOException gioe;
051    
052        private void importGraph() throws GraphIOException {
053            JFileChooser fileChooser = new JFileChooser();
054            ExampleFileFilter fileFilter = new ExampleFileFilter(gi.getExtension(), gi.getName());
055            fileFilter.setDescription(gi.getDescription());
056            fileChooser.setFileFilter(fileFilter);
057            if (GraphIOExtensionHandler.defaultFile != null)
058                fileChooser.setCurrentDirectory(new File(GraphIOExtensionHandler.defaultFile));
059            int l = fileChooser.showOpenDialog(null);
060            if (l == JFileChooser.APPROVE_OPTION) {
061                File selectedFile = fileChooser
062                        .getSelectedFile();
063                GraphIOExtensionHandler.defaultFile = selectedFile.getPath();
064    
065                if (gi.accepts(selectedFile)) {
066                    GraphModel g = gi.read(selectedFile);
067                    GTabbedGraphPane.getCurrentGTabbedGraphPane(blackboard).addGraph(g);
068                }
069            }
070    
071        }
072    
073    }