KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgrapht > experimental > touchgraph > TouchgraphPanel


1 /* ==========================================
2  * JGraphT : a free Java graph-theory library
3  * ==========================================
4  *
5  * Project Info: http://jgrapht.sourceforge.net/
6  * Project Creator: Barak Naveh (http://sourceforge.net/users/barak_naveh)
7  *
8  * (C) Copyright 2003-2006, by Barak Naveh and Contributors.
9  *
10  * This library is free software; you can redistribute it and/or modify it
11  * under the terms of the GNU Lesser General Public License as published by
12  * the Free Software Foundation; either version 2.1 of the License, or
13  * (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful, but
16  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
18  * License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with this library; if not, write to the Free Software Foundation,
22  * Inc.,
23  * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
24  */

25 /* -------------------
26  * TouchgraphPanel.java
27  * -------------------
28  * (C) Copyright 2006-2006, by Carl Anderson and Contributors.
29  *
30  * Original Author: Carl Anderson
31  * Contributor(s): -
32  *
33  * $Id: TouchgraphPanel.java 504 2006-07-03 02:37:26Z perfecthash $
34  *
35  * Changes
36  * -------
37  * 8-May-2006 : Initial revision (CA);
38  *
39  */

40 package org.jgrapht.experimental.touchgraph;
41
42 import com.touchgraph.graphlayout.*;
43 import com.touchgraph.graphlayout.interaction.*;
44
45 import java.awt.*;
46
47 import java.util.*;
48
49 import org.jgrapht.*;
50
51
52 /**
53  * The Touchgraph panel that displays our graph
54  * http://sourceforge.net/projects/touchgraph
55  *
56  * @author canderson
57  */

58 public class TouchgraphPanel<V, E>
59     extends GLPanel
60 {
61
62     //~ Static fields/initializers --------------------------------------------
63

64     /**
65      */

66     private static final long serialVersionUID = -7441058429719746032L;
67
68     //~ Instance fields -------------------------------------------------------
69

70     private Color defaultBackColor = new Color(0x01, 0x11, 0x44);
71     private Color defaultBorderBackColor = new Color(0x02, 0x35, 0x81);
72     private Color defaultForeColor =
73         new Color((float) 0.95, (float) 0.85, (float) 0.55);
74
75     /**
76      * the JGraphT graph we are displaying
77      */

78     Graph<V, E> graph;
79
80     /**
81      * are self-references allowed? They will not show up in TouchGraph unless
82      * you override Touchgraph's Node or Edge class to do so
83      */

84     boolean selfReferencesAllowed = true;
85
86     // =================
87

88     //~ Constructors ----------------------------------------------------------
89

90     /**constructor*/
91     public TouchgraphPanel(Graph<V, E> graph, boolean selfReferencesAllowed)
92     {
93         this.graph = graph;
94         this.selfReferencesAllowed = selfReferencesAllowed;
95
96         /*
97          * The code that was in the super's constructor. As it also called
98          * super's initialize()
99          * it is impossible to subclass and insert our own graph into the
100          * initialization process
101          */

102         preinitialize();
103
104         initialize(); // now we can insert our own graph into this method
105
}
106
107     //~ Methods ---------------------------------------------------------------
108

109     /**
110      * get everything setup: this is the code that was in the super's
111      * constructor but which was followed by an initialize() call. Hence, it was
112      * impossible to subclass the superclass and insert our own graph
113      * initialization code without breaking it out as here.
114      */

115     public void preinitialize()
116     {
117         this.setBackground(defaultBorderBackColor);
118         this.setForeground(defaultForeColor);
119         scrollBarHash = new Hashtable();
120         tgLensSet = new TGLensSet();
121         tgPanel = new TGPanel();
122         tgPanel.setBackColor(defaultBackColor);
123         hvScroll = new HVScroll(tgPanel, tgLensSet);
124         zoomScroll = new ZoomScroll(tgPanel);
125         hyperScroll = new HyperScroll(tgPanel);
126         rotateScroll = new RotateScroll(tgPanel);
127         localityScroll = new LocalityScroll(tgPanel);
128     }
129
130     /**
131      * Initialize panel, lens, and establish a random graph as a demonstration.
132      */

133     public void initialize()
134     {
135         buildPanel();
136         buildLens();
137         tgPanel.setLensSet(tgLensSet);
138         addUIs();
139         try {
140             if (this.graph == null) {
141                 /*
142                  * Add a random graph
143                  */

144                 randomGraph();
145             } else {
146                 /*
147                  * Add users graph
148                  */

149                 TouchgraphConverter<V, E> converter =
150                     new TouchgraphConverter<V, E>();
151                 Node n =
152                     (Node) converter.convertToTouchGraph(
153                         this.graph,
154                         tgPanel,
155                         this.selfReferencesAllowed);
156                 getHVScroll().slowScrollToCenter(n);
157                 tgPanel.setLocale(n, Integer.MAX_VALUE);
158             }
159         } catch (TGException tge) {
160             System.err.println(tge.getMessage());
161             tge.printStackTrace(System.err);
162         }
163         setVisible(true);
164     }
165 }
166
167 // End TouchgraphPanel.java
168
Popular Tags