2
* TouchGraph LLC. Apache-Style Software License
5
* Copyright (c) 2001-2002 Alexander Shapiro. All rights reserved.
7
* Redistribution and use in source and binary forms, with or without
8
* modification, are permitted provided that the following conditions
11
* 1. Redistributions of source code must retain the above copyright
12
* notice, this list of conditions and the following disclaimer.
14
* 2. Redistributions in binary form must reproduce the above copyright
15
* notice, this list of conditions and the following disclaimer in
16
* the documentation and/or other materials provided with the
19
* 3. The end-user documentation included with the redistribution,
20
* if any, must include the following acknowledgment:
21
* "This product includes software developed by
22
* TouchGraph LLC (http://www.touchgraph.com/)."
23
* Alternately, this acknowledgment may appear in the software itself,
24
* if and wherever such third-party acknowledgments normally appear.
26
* 4. The names "TouchGraph" or "TouchGraph LLC" must not be used to endorse
27
* or promote products derived from this software without prior written
28
* permission. For written permission, please contact
31
* 5. Products derived from this software may not be called "TouchGraph",
32
* nor may "TouchGraph" appear in their name, without prior written
33
* permission of alex@touchgraph.com.
35
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38
* DISCLAIMED. IN NO EVENT SHALL TOUCHGRAPH OR ITS CONTRIBUTORS BE
39
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
40
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
41
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
42
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
43
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
44
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
45
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
46
* ====================================================================
50
package com.touchgraph.graphlayout;
51
import com.touchgraph.graphlayout.interaction.*;
52
import com.touchgraph.graphlayout.graphelements.*;
55
import java.awt.event.*;
56
//import javax.swing.*;
57
import java.util.Hashtable;
58
import java.util.StringTokenizer;
59
import java.util.Vector;
60
import java.util.Enumeration;
62
import java.io.InputStreamReader;
64
/** GLPanel contains code for adding scrollbars and interfaces to the TGPanel
65
* The "GL" prefix indicates that this class is GraphLayout specific, and
66
* will probably need to be rewritten for other applications.
68
* @author Alexander Shapiro
69
* @version 1.22-jre1.1 $Id: GLPanel.java,v 1.3 2002/09/23 18:45:56 ldornbusch Exp $
71
public class GLPanel extends Panel {
73
public String zoomLabel = "Zoom"; // label for zoom menu item
74
public String rotateLabel = "Rotate"; // label for rotate menu item
75
public String localityLabel = "Radius"; // label for locality menu item
76
public String hyperLabel = "Hyperbolic"; // label for Hyper menu item
78
public HVScroll hvScroll;
79
public ZoomScroll zoomScroll;
80
public HyperScroll hyperScroll; // unused
81
public RotateScroll rotateScroll;
82
public LocalityScroll localityScroll;
83
public PopupMenu glPopup;
84
public Hashtable scrollBarHash; //= new Hashtable();
86
protected TGPanel tgPanel;
87
protected TGLensSet tgLensSet;
88
protected TGUIManager tgUIManager;
90
private Scrollbar currentSB =null;
93
private Color defaultBackColor = new Color(0x01,0x11,0x44);
94
private Color defaultBorderBackColor = new Color(0x02,0x35,0x81);
95
private Color defaultForeColor = new Color((float)0.95,(float)0.85,(float)0.55);
100
/** Default constructor.
103
this.setBackground(defaultBorderBackColor);
104
this.setForeground(defaultForeColor);
105
scrollBarHash = new Hashtable();
106
tgLensSet = new TGLensSet();
107
tgPanel = new TGPanel();
108
tgPanel.setBackColor(defaultBackColor);
109
hvScroll = new HVScroll(tgPanel, tgLensSet);
110
zoomScroll = new ZoomScroll(tgPanel);
111
hyperScroll = new HyperScroll(tgPanel);
112
rotateScroll = new RotateScroll(tgPanel);
113
localityScroll = new LocalityScroll(tgPanel);
118
/** Initialize panel, lens, and establish a random graph as a demonstration.
120
public void initialize() {
123
tgPanel.setLensSet(tgLensSet);
125
//tgPanel.addNode(); //Add a starting node.
128
} catch ( TGException tge ) {
129
System.err.println(tge.getMessage());
130
tge.printStackTrace(System.err);
135
/** Return the TGPanel used with this GLPanel. */
136
public TGPanel getTGPanel() {
140
// navigation .................
142
/** Return the HVScroll used with this GLPanel. */
143
public HVScroll getHVScroll()
148
/** Return the HyperScroll used with this GLPanel. */
149
public HyperScroll getHyperScroll()
154
/** Sets the horizontal offset to p.x, and the vertical offset to p.y
155
* given a Point <tt>p<tt>.
157
public void setOffset( Point p ) {
158
hvScroll.setOffset(p);
161
/** Return the horizontal and vertical offset position as a Point. */
162
public Point getOffset() {
163
return hvScroll.getOffset();
166
// rotation ...................
168
/** Return the RotateScroll used with this GLPanel. */
169
public RotateScroll getRotateScroll()
174
/** Set the rotation angle of this GLPanel (allowable values between 0 to 359). */
175
public void setRotationAngle( int angle ) {
176
rotateScroll.setRotationAngle(angle);
179
/** Return the rotation angle of this GLPanel. */
180
public int getRotationAngle() {
181
return rotateScroll.getRotationAngle();
184
// locality ...................
186
/** Return the LocalityScroll used with this GLPanel. */
187
public LocalityScroll getLocalityScroll()
189
return localityScroll;
192
/** Set the locality radius of this TGScrollPane
193
* (allowable values between 0 to 4, or LocalityUtils.INFINITE_LOCALITY_RADIUS).
195
public void setLocalityRadius( int radius ) {
196
localityScroll.setLocalityRadius(radius);
199
/** Return the locality radius of this GLPanel. */
200
public int getLocalityRadius() {
201
return localityScroll.getLocalityRadius();
204
// zoom .......................
206
/** Return the ZoomScroll used with this GLPanel. */
207
public ZoomScroll getZoomScroll()
212
/** Set the zoom value of this GLPanel (allowable values between -100 to 100). */
213
public void setZoomValue( int zoomValue ) {
214
zoomScroll.setZoomValue(zoomValue);
217
/** Return the zoom value of this GLPanel. */
218
public int getZoomValue() {
219
return zoomScroll.getZoomValue();
224
public PopupMenu getGLPopup()
229
public void buildLens() {
230
tgLensSet.addLens(hvScroll.getLens());
231
tgLensSet.addLens(zoomScroll.getLens());
232
tgLensSet.addLens(hyperScroll.getLens());
233
tgLensSet.addLens(rotateScroll.getLens());
234
tgLensSet.addLens(tgPanel.getAdjustOriginLens());
237
public void buildPanel() {
238
final Scrollbar horizontalSB = hvScroll.getHorizontalSB();
239
final Scrollbar verticalSB = hvScroll.getVerticalSB();
240
final Scrollbar zoomSB = zoomScroll.getZoomSB();
241
final Scrollbar rotateSB = rotateScroll.getRotateSB();
242
final Scrollbar localitySB = localityScroll.getLocalitySB();
243
final Scrollbar hyperSB = hyperScroll.getHyperSB();
245
setLayout(new BorderLayout());
247
Panel scrollPanel = new Panel();
248
scrollPanel.setBackground(defaultBackColor);
249
scrollPanel.setForeground(defaultForeColor);
250
scrollPanel.setLayout(new GridBagLayout());
251
GridBagConstraints c = new GridBagConstraints();
254
Panel modeSelectPanel = new Panel();
255
modeSelectPanel.setBackground(defaultBackColor);
256
modeSelectPanel.setForeground(defaultForeColor);
257
modeSelectPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 0,0));
259
final Panel topPanel = new Panel();
260
topPanel.setBackground(defaultBorderBackColor);
261
topPanel.setForeground(defaultForeColor);
262
topPanel.setLayout(new GridBagLayout());
263
c.gridy=0; c.fill=GridBagConstraints.HORIZONTAL;
265
c.gridx=0;c.weightx=0;
267
c.insets=new Insets(0,0,0,0);
268
c.gridy=0;c.weightx=1;
270
scrollBarHash.put(zoomLabel, zoomSB);
271
scrollBarHash.put(rotateLabel, rotateSB);
272
scrollBarHash.put(localityLabel, localitySB);
273
scrollBarHash.put(hyperLabel, hyperSB);
275
Panel scrollselect = scrollSelectPanel(new String[] {zoomLabel, rotateLabel /*, localityLabel*/, hyperLabel});
276
scrollselect.setBackground(defaultBorderBackColor);
277
scrollselect.setForeground(defaultForeColor);
278
topPanel.add(scrollselect,c);
280
add(topPanel, BorderLayout.SOUTH);
282
c.fill = GridBagConstraints.BOTH;
284
c.gridx = 0; c.gridy = 1; c.weightx = 1; c.weighty = 1;
285
scrollPanel.add(tgPanel,c);
287
c.gridx = 1; c.gridy = 1; c.weightx = 0; c.weighty = 0;
288
// scrollPanel.add(verticalSB,c); // For WDR We do not need scrollbars
290
c.gridx = 0; c.gridy = 2;
291
// scrollPanel.add(horizontalSB,c); // For WDR We do not need scrollbars
293
add(scrollPanel,BorderLayout.CENTER);
295
glPopup = new PopupMenu();
296
add(glPopup); // needed by JDK11 Popupmenu..
298
MenuItem menuItem = new MenuItem("Toggle Controls");
299
ActionListener toggleControlsAction = new ActionListener() {
300
boolean controlsVisible = true;
301
public void actionPerformed(ActionEvent e) {
302
controlsVisible = !controlsVisible;
303
horizontalSB.setVisible(controlsVisible);
304
verticalSB.setVisible(controlsVisible);
305
topPanel.setVisible(controlsVisible);
306
GLPanel.this.doLayout();
309
menuItem.addActionListener(toggleControlsAction);
310
glPopup.add(menuItem);
313
protected Panel scrollSelectPanel(final String[] scrollBarNames) {
314
final Panel sbp = new Panel(new GridBagLayout());
316
// UI: Scrollbarselector via Radiobuttons.................................
318
sbp.setBackground(defaultBorderBackColor);
319
sbp.setForeground(defaultForeColor);
321
Panel firstRow=new Panel(new GridBagLayout());
323
final CheckboxGroup bg = new CheckboxGroup();
325
int cbNumber = scrollBarNames.length;
326
Checkbox checkboxes[] = new Checkbox[cbNumber];
328
GridBagConstraints c = new GridBagConstraints();
329
c.anchor=GridBagConstraints.WEST;
330
c.gridy = 0; c.weightx= 0; c.fill = GridBagConstraints.HORIZONTAL;
332
for (int i=0;i<cbNumber;i++) {
333
checkboxes[i] = new Checkbox(scrollBarNames[i],true,bg);
335
firstRow.add(checkboxes[i],c);
337
checkboxes[0].setState(true);
339
c.gridx=cbNumber;c.weightx=1;
340
Label lbl = new Label(" Right-click nodes and background for more options");
343
class radioItemListener implements ItemListener{
344
private String scrollBarName;
345
public radioItemListener(String str2Act){
346
this.scrollBarName=str2Act;
348
public void itemStateChanged(ItemEvent e){
349
Scrollbar selectedSB = (Scrollbar) scrollBarHash.get((String) bg.getSelectedCheckbox().getLabel());
350
if (e.getStateChange()==ItemEvent.SELECTED){
351
for (int i = 0;i<scrollBarNames.length;i++) {
352
Scrollbar sb = (Scrollbar) scrollBarHash.get(scrollBarNames[i]);
353
sb.setVisible(false);
355
selectedSB.setBounds(currentSB.getBounds());
356
if (selectedSB!=null)
357
selectedSB.setVisible(true);
358
currentSB = selectedSB;
364
for (int i=0;i<cbNumber;i++) {
365
checkboxes[i].addItemListener(new radioItemListener(scrollBarNames[0]));
368
c.anchor = GridBagConstraints.NORTHWEST;
369
c.insets=new Insets(1,5,1,5);
370
c.gridx = 0; c.gridy = 0; c.weightx = 10;
371
c.gridwidth=3; //Radiobutton UI
373
c.fill=GridBagConstraints.NONE;
374
c.anchor=GridBagConstraints.WEST;
378
c.fill=GridBagConstraints.HORIZONTAL;
379
for (int i = 0;i<scrollBarNames.length;i++) {
380
Scrollbar sb = (Scrollbar) scrollBarHash.get(scrollBarNames[i]);
381
if(sb==null) continue;
382
if(currentSB==null) currentSB = sb;
389
public void addUIs() {
390
tgUIManager = new TGUIManager();
391
GLEditUI editUI = new GLEditUI(this);
392
GLNavigateUI navigateUI = new GLNavigateUI(this);
393
tgUIManager.addUI(editUI,"Edit");
394
tgUIManager.addUI(navigateUI,"Navigate");
395
tgUIManager.activate("Navigate");
398
public void randomGraph() throws TGException {
399
Node n1= tgPanel.addNode();
401
for ( int i=0; i<249; i++ ) {
405
TGForEachNode fen = new TGForEachNode() {
406
public void forEachNode(Node n) {
407
for(int i=0;i<5;i++) {
408
Node r = tgPanel.getGES().getRandomNode();
409
if(r!=n && tgPanel.findEdge(r,n)==null)
410
tgPanel.addEdge(r,n,Edge.DEFAULT_LENGTH);
414
tgPanel.getGES().forAllNodes(fen);
416
tgPanel.setLocale(n1,1);
417
tgPanel.setSelect(n1);
419
Thread.currentThread().sleep(2000);
420
} catch (InterruptedException ex) {}
422
getHVScroll().slowScrollToCenter(n1);
425
public static void main(String [] args) {
428
final GLPanel glPanel = new GLPanel();
429
frame = new Frame("TouchGraph GraphLayout");
430
frame.addWindowListener(new WindowAdapter() {
431
public void windowClosing(WindowEvent e) {
432
frame.remove(glPanel);
436
frame.add("Center", glPanel);
437
frame.setSize(800,600);
438
frame.setVisible(true);
442
} // end com.touchgraph.graphlayout.GLPanel