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.core;
005    
006    import graphlab.graph.atributeset.GraphAttrSet;
007    import graphlab.graph.graph.EdgeModel;
008    import graphlab.graph.graph.GraphModel;
009    import graphlab.graph.graph.SubGraph;
010    import graphlab.graph.graph.VertexModel;
011    import graphlab.graph.ui.GHTMLPageComponent;
012    import graphlab.platform.core.BlackBoard;
013    import graphlab.platform.plugin.PluginMethods;
014    import graphlab.plugins.commonplugin.undo.UndoableActionOccuredData;
015    import graphlab.plugins.commonplugin.undo.undo.RedoAction;
016    import graphlab.plugins.commonplugin.undo.undo.UndoAction;
017    import graphlab.plugins.main.ccp.Copy;
018    import graphlab.plugins.main.ccp.Cut;
019    import graphlab.plugins.main.core.actions.AddTab;
020    import graphlab.plugins.main.core.actions.CloseTab;
021    import graphlab.plugins.main.core.actions.ResetGraph;
022    import graphlab.plugins.main.core.actions.StatusBarMessage;
023    import graphlab.plugins.main.core.actions.edge.AddEdge;
024    import graphlab.plugins.main.core.actions.graph.ClearGraph;
025    import graphlab.plugins.main.core.actions.vertex.AddVertex;
026    import graphlab.plugins.main.core.actions.vertex.DeleteVertex;
027    
028    import javax.swing.*;
029    import java.net.MalformedURLException;
030    import java.net.URL;
031    
032    /**
033     * @author azin azadi
034    
035     */
036    public class CorePluginMethods implements PluginMethods {
037        BlackBoard blackboard;
038    //************************    E D G E      ********************************
039    
040        public CorePluginMethods(BlackBoard blackboard) {
041            this.blackboard = blackboard;
042        }
043    
044        /**
045         * adds e to g
046         */
047        public void addEdge(GraphModel g, EdgeModel e) {
048            AddEdge.doJob(g, e.source, e.target);
049        }
050    
051        /**
052         * adds e to current editing graph
053         */
054        public void addEdge(EdgeModel e) {
055            AddEdge.doJob(getGraph(), e.source, e.target);
056        }
057    
058        /**
059         * create and adds a new edge from v1, v2 to g
060         */
061        public void addEdge(GraphModel g, VertexModel v1, VertexModel v2) {
062            AddEdge.doJob(g, v1, v2);
063        }
064    
065        public void addEdge(VertexModel v1, VertexModel v2) {
066            AddEdge.doJob(getGraph(), v1, v2);
067        }
068    
069        public void deleteEdge(GraphModel g, EdgeModel e) {
070            g.removeEdge(e);
071        }
072    
073    //*****************         G R A P H      *************************
074    
075        /**
076         * removes all edges and vertices of g
077         */
078        public void clearGraph(GraphModel g) {
079            ClearGraph.destroyGraph(g);
080        }
081    
082        /**
083         * removes all edges and vertices of current graph
084         */
085        public void clearGraph() {
086            ClearGraph.destroyGraph(getGraph());
087        }
088    
089    //****************       V E R T E X      ***************************
090    
091        /**
092         * adds a new vertex to a random point of the graph and returns it
093         */
094        public VertexModel addVertex(GraphModel g) {
095            return AddVertex.addVertexToRandomPosition(g);
096        }
097    
098        /**
099         * add a new vertex to a random position of the current graph and returns it
100         */
101        public VertexModel addVertex() {
102            return AddVertex.addVertexToRandomPosition(getGraph());
103        }
104    
105        /**
106         * adds a vertex to the given point of graph
107         */
108        public VertexModel addVertex(GraphModel g, int x, int y) {
109            return AddVertex.doJob(g, x, y);
110        }
111    
112        /**
113         * adds a vertex to the given point of current graph
114         */
115        public VertexModel addVertex(int x, int y) {
116            return AddVertex.doJob(getGraph(), x, y);
117        }
118    
119        /**
120         * deletes a vertex from it's coressponding graph
121         */
122        public void deleteVertex(GraphModel g, VertexModel v) {
123            DeleteVertex.doJob(g, v);
124        }
125    
126    //*********************      U N D O / R E D O        *************************
127    
128        /**
129         * @see graphlab.plugins.commonplugin.undo.undo.UndoAction#undo(graphlab.platform.core.BlackBoard)
130         */
131        public void undo() {
132            UndoAction.undo(blackboard);
133        }
134    
135        /**
136         * @see graphlab.plugins.commonplugin.undo.undo.RedoAction#redo(graphlab.platform.core.BlackBoard)
137         */
138        public void redo() {
139            RedoAction.redo(blackboard);
140        }
141    
142        /**
143         * puts data in the stack of undo/redo actions, so it will be regarded as an undoable action and will be undone by the rules of undo/redo.
144         */
145        public void addUndoData(UndoableActionOccuredData data) {
146            blackboard.setData(UndoableActionOccuredData.EVENT_KEY, data);
147        }
148    //*********************   TABBED EDITING      *******************************
149    
150        /**
151         * @see graphlab.plugins.main.core.actions.AddTab#addTab(graphlab.platform.core.BlackBoard)
152         */
153        public void addTab() {
154            AddTab.addTab(blackboard);
155        }
156    
157        /**
158         * @see graphlab.plugins.main.core.actions.AddTab#addTabNoGUI(boolean, graphlab.platform.core.BlackBoard)
159         */
160        public void addTabNoGUI(boolean isdirected , BlackBoard blackboard) {
161            AddTab.addTabNoGUI(isdirected, blackboard);
162        }
163        /**
164         * @see graphlab.plugins.main.core.actions.AddTab#displayGraph(graphlab.graph.graph.GraphModel,graphlab.platform.core.BlackBoard)
165         */
166        public void showGraph(GraphModel g) {
167            AddTab.displayGraph(g, blackboard);
168        }
169    
170        /**
171         * @see graphlab.plugins.main.core.actions.CloseTab#dojob(graphlab.platform.core.BlackBoard)
172         */
173        public void closeTab() {
174            CloseTab.dojob(blackboard);
175        }
176    //*****************************
177    
178        /**
179         * @see graphlab.plugins.main.core.actions.ResetGraph#ResetGraph(graphlab.platform.core.BlackBoard)
180         */
181        public void resetGraph() {
182            ResetGraph.resetGraph(getGraph());
183        }
184    
185        /**
186         * @see graphlab.plugins.main.core.actions.ResetGraph#ResetGraph(graphlab.platform.core.BlackBoard)
187         */
188        public void resetGraph(GraphModel g) {
189            ResetGraph.resetGraph(g);
190        }
191    
192        /**
193         * @see graphlab.plugins.main.core.actions.StatusBarMessage#setMessage(graphlab.platform.core.BlackBoard,String)
194         */
195        public void showStatusBarMessage(String s) {
196            StatusBarMessage.setMessage(blackboard, s);
197        }
198    
199        /**
200         * @see graphlab.plugins.main.core.actions.StatusBarMessage#showQuickMessage(graphlab.platform.core.BlackBoard,String)
201         */
202        public void showQuickMessageInStatusbar(String message) {
203            StatusBarMessage.showQuickMessage(blackboard, message);
204        }
205    
206    //******************   Cut Copy Paste  ************************
207    
208        /**
209         * @see graphlab.plugins.main.ccp.Copy#copy(graphlab.graph.graph.SubGraph)
210         */
211        public void copyToClipboard(SubGraph selection) {
212            Copy.copy(selection);
213        }
214    
215        public void pasteFromClipboard() {
216            //Todo:Azin jaan een ye giri daare ke behet migam hala:D (rouzbeh)
217    
218        }
219    
220        public void cutToClipboard(SubGraph selection) {
221            Cut.cut(selection, getGraph(), blackboard);
222    
223        }
224    
225        private GraphModel getGraph() {
226            return blackboard.getData(GraphAttrSet.name);
227        }
228    
229        public static GraphModel getGraph(BlackBoard blackboard) {
230            return blackboard.getData(GraphAttrSet.name);
231        }
232    
233        //***************************
234        /**
235         * shows the givve page in a new dialog,
236         * Note that the used html viewer is GHTMLPageComponent, which is internally
237         * uses a JEditorPane, but the blackboard in dialog will be a new blackboard,
238         * (in the case of you want to use "bsh:" feature of GHTMLPageComponent,
239         * for this use showPageInDialog(URL, blackboard).
240         */
241        public static void showPageInDialog(String pageUrl, String title) {
242            BlackBoard blackboard = new BlackBoard();
243            URL page = null;
244            try {
245                page = new URL(pageUrl);
246            } catch (MalformedURLException e) {
247                e.printStackTrace();
248            }
249            if (page != null)
250                showPageInDialog(title, blackboard, page);
251    //        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
252        }
253    
254        /**
255         * shows the givve page in a new dialog,
256         * Note that the used html viewer is GHTMLPageComponent, which is internally
257         * uses a JEditorPane,
258         */
259        private static void showPageInDialog(String title, BlackBoard blackboard, URL page) {
260            JFrame f = new JFrame(title);
261            GHTMLPageComponent browserPane = new GHTMLPageComponent(blackboard);
262            browserPane.setPage(page);
263            f.add(new JScrollPane(browserPane));
264            f.setVisible(true);
265            f.setSize(500, 500);
266            f.validate();
267            f.setResizable(false);
268        }
269    
270    }