~ubuntu-branches/ubuntu/saucy/libtggraphlayout-java/saucy

« back to all changes in this revision

Viewing changes to com/touchgraph/graphlayout/GLPanel.java

  • Committer: Bazaar Package Importer
  • Author(s): Morten Sørensen
  • Date: 2009-12-06 17:57:32 UTC
  • Revision ID: james.westby@ubuntu.com-20091206175732-auvd39j1u30e0x50
Tags: upstream-122
Import upstream version 122

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * TouchGraph LLC. Apache-Style Software License
 
3
 *
 
4
 *
 
5
 * Copyright (c) 2001-2002 Alexander Shapiro. All rights reserved.
 
6
 *
 
7
 * Redistribution and use in source and binary forms, with or without
 
8
 * modification, are permitted provided that the following conditions
 
9
 * are met:
 
10
 *
 
11
 * 1. Redistributions of source code must retain the above copyright
 
12
 *    notice, this list of conditions and the following disclaimer.
 
13
 *
 
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
 
17
 *    distribution.
 
18
 *
 
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.
 
25
 *
 
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
 
29
 *    alex@touchgraph.com
 
30
 *
 
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.
 
34
 *
 
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
 * ====================================================================
 
47
 *
 
48
 */
 
49
 
 
50
package com.touchgraph.graphlayout;
 
51
import com.touchgraph.graphlayout.interaction.*;
 
52
import com.touchgraph.graphlayout.graphelements.*;
 
53
 
 
54
import  java.awt.*;
 
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;
 
61
import java.net.URL;
 
62
import java.io.InputStreamReader;
 
63
 
 
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.
 
67
  *
 
68
  * @author   Alexander Shapiro
 
69
  * @version  1.22-jre1.1  $Id: GLPanel.java,v 1.3 2002/09/23 18:45:56 ldornbusch Exp $
 
70
  */
 
71
public class GLPanel extends Panel {
 
72
 
 
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
 
77
 
 
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();
 
85
 
 
86
    protected TGPanel tgPanel;
 
87
    protected TGLensSet tgLensSet;
 
88
    protected TGUIManager tgUIManager;
 
89
 
 
90
    private Scrollbar currentSB =null;
 
91
 
 
92
 
 
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);
 
96
 
 
97
  // ............
 
98
 
 
99
 
 
100
   /** Default constructor.
 
101
     */
 
102
    public GLPanel() {
 
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);
 
114
        initialize();
 
115
    }
 
116
 
 
117
 
 
118
   /** Initialize panel, lens, and establish a random graph as a demonstration.
 
119
     */
 
120
    public void initialize() {
 
121
        buildPanel();
 
122
        buildLens();
 
123
        tgPanel.setLensSet(tgLensSet);
 
124
        addUIs();
 
125
      //tgPanel.addNode();  //Add a starting node.
 
126
        try {
 
127
            randomGraph();
 
128
        } catch ( TGException tge ) {
 
129
            System.err.println(tge.getMessage());
 
130
            tge.printStackTrace(System.err);
 
131
        }
 
132
        setVisible(true);
 
133
    }
 
134
 
 
135
    /** Return the TGPanel used with this GLPanel. */
 
136
    public TGPanel getTGPanel() {
 
137
        return tgPanel;
 
138
    }
 
139
 
 
140
  // navigation .................
 
141
 
 
142
    /** Return the HVScroll used with this GLPanel. */
 
143
    public HVScroll getHVScroll()
 
144
    {
 
145
        return hvScroll;
 
146
    }
 
147
 
 
148
    /** Return the HyperScroll used with this GLPanel. */
 
149
    public HyperScroll getHyperScroll()
 
150
    {
 
151
        return hyperScroll;
 
152
    }
 
153
 
 
154
    /** Sets the horizontal offset to p.x, and the vertical offset to p.y
 
155
      * given a Point <tt>p<tt>.
 
156
      */
 
157
    public void setOffset( Point p ) {
 
158
        hvScroll.setOffset(p);
 
159
    };
 
160
 
 
161
    /** Return the horizontal and vertical offset position as a Point. */
 
162
    public Point getOffset() {
 
163
        return hvScroll.getOffset();
 
164
    };
 
165
 
 
166
  // rotation ...................
 
167
 
 
168
    /** Return the RotateScroll used with this GLPanel. */
 
169
    public RotateScroll getRotateScroll()
 
170
    {
 
171
        return rotateScroll;
 
172
    }
 
173
 
 
174
    /** Set the rotation angle of this GLPanel (allowable values between 0 to 359). */
 
175
     public void setRotationAngle( int angle ) {
 
176
        rotateScroll.setRotationAngle(angle);
 
177
    }
 
178
 
 
179
    /** Return the rotation angle of this GLPanel. */
 
180
    public int getRotationAngle() {
 
181
        return rotateScroll.getRotationAngle();
 
182
    }
 
183
 
 
184
  // locality ...................
 
185
 
 
186
    /** Return the LocalityScroll used with this GLPanel. */
 
187
    public LocalityScroll getLocalityScroll()
 
188
    {
 
189
        return localityScroll;
 
190
    }
 
191
 
 
192
    /** Set the locality radius of this TGScrollPane
 
193
      * (allowable values between 0 to 4, or LocalityUtils.INFINITE_LOCALITY_RADIUS).
 
194
      */
 
195
    public void setLocalityRadius( int radius ) {
 
196
        localityScroll.setLocalityRadius(radius);
 
197
    }
 
198
 
 
199
    /** Return the locality radius of this GLPanel. */
 
200
    public int getLocalityRadius() {
 
201
        return localityScroll.getLocalityRadius();
 
202
    }
 
203
 
 
204
  // zoom .......................
 
205
 
 
206
    /** Return the ZoomScroll used with this GLPanel. */
 
207
    public ZoomScroll getZoomScroll()
 
208
    {
 
209
        return zoomScroll;
 
210
    }
 
211
 
 
212
    /** Set the zoom value of this GLPanel (allowable values between -100 to 100). */
 
213
    public void setZoomValue( int zoomValue ) {
 
214
        zoomScroll.setZoomValue(zoomValue);
 
215
    }
 
216
 
 
217
    /** Return the zoom value of this GLPanel. */
 
218
    public int getZoomValue() {
 
219
        return zoomScroll.getZoomValue();
 
220
    }
 
221
 
 
222
  // ....
 
223
 
 
224
    public PopupMenu getGLPopup()
 
225
    {
 
226
        return glPopup;
 
227
    }
 
228
 
 
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());
 
235
    }
 
236
 
 
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();
 
244
 
 
245
        setLayout(new BorderLayout());
 
246
 
 
247
        Panel scrollPanel = new Panel();
 
248
        scrollPanel.setBackground(defaultBackColor);
 
249
        scrollPanel.setForeground(defaultForeColor);
 
250
        scrollPanel.setLayout(new GridBagLayout());
 
251
        GridBagConstraints c = new GridBagConstraints();
 
252
 
 
253
 
 
254
        Panel modeSelectPanel = new Panel();
 
255
        modeSelectPanel.setBackground(defaultBackColor);
 
256
        modeSelectPanel.setForeground(defaultForeColor);
 
257
        modeSelectPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 0,0));
 
258
 
 
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;
 
264
 
 
265
        c.gridx=0;c.weightx=0;
 
266
 
 
267
        c.insets=new Insets(0,0,0,0);
 
268
        c.gridy=0;c.weightx=1;
 
269
 
 
270
        scrollBarHash.put(zoomLabel, zoomSB);
 
271
        scrollBarHash.put(rotateLabel, rotateSB);
 
272
                scrollBarHash.put(localityLabel, localitySB);
 
273
                scrollBarHash.put(hyperLabel, hyperSB);
 
274
 
 
275
        Panel scrollselect = scrollSelectPanel(new String[] {zoomLabel, rotateLabel /*, localityLabel*/, hyperLabel});
 
276
        scrollselect.setBackground(defaultBorderBackColor);
 
277
        scrollselect.setForeground(defaultForeColor);
 
278
        topPanel.add(scrollselect,c);
 
279
 
 
280
        add(topPanel, BorderLayout.SOUTH);
 
281
 
 
282
        c.fill = GridBagConstraints.BOTH;
 
283
        c.gridwidth = 1;
 
284
        c.gridx = 0; c.gridy = 1; c.weightx = 1; c.weighty = 1;
 
285
        scrollPanel.add(tgPanel,c);
 
286
 
 
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
 
289
 
 
290
        c.gridx = 0; c.gridy = 2;
 
291
 //       scrollPanel.add(horizontalSB,c);  // For WDR We do not need scrollbars
 
292
 
 
293
        add(scrollPanel,BorderLayout.CENTER);
 
294
 
 
295
        glPopup = new PopupMenu();
 
296
              add(glPopup);     // needed by JDK11 Popupmenu..
 
297
 
 
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();
 
307
                }
 
308
            };
 
309
        menuItem.addActionListener(toggleControlsAction);
 
310
        glPopup.add(menuItem);
 
311
    }
 
312
 
 
313
    protected Panel scrollSelectPanel(final String[] scrollBarNames) {
 
314
      final Panel sbp = new Panel(new GridBagLayout());
 
315
 
 
316
//    UI: Scrollbarselector via Radiobuttons.................................
 
317
 
 
318
      sbp.setBackground(defaultBorderBackColor);
 
319
      sbp.setForeground(defaultForeColor);
 
320
 
 
321
      Panel firstRow=new Panel(new GridBagLayout());
 
322
 
 
323
      final CheckboxGroup bg = new CheckboxGroup();
 
324
      
 
325
      int cbNumber = scrollBarNames.length;
 
326
      Checkbox checkboxes[] = new Checkbox[cbNumber];
 
327
      
 
328
      GridBagConstraints c = new GridBagConstraints();      
 
329
      c.anchor=GridBagConstraints.WEST;
 
330
      c.gridy = 0; c.weightx= 0; c.fill = GridBagConstraints.HORIZONTAL;
 
331
 
 
332
      for (int i=0;i<cbNumber;i++) {
 
333
        checkboxes[i] = new Checkbox(scrollBarNames[i],true,bg);
 
334
        c.gridx = i; 
 
335
        firstRow.add(checkboxes[i],c);
 
336
      }
 
337
      checkboxes[0].setState(true);
 
338
      
 
339
      c.gridx=cbNumber;c.weightx=1;
 
340
      Label lbl = new Label("     Right-click nodes and background for more options");
 
341
      firstRow.add(lbl,c);
 
342
        
 
343
      class radioItemListener implements ItemListener{
 
344
        private String scrollBarName;
 
345
        public radioItemListener(String str2Act){
 
346
          this.scrollBarName=str2Act;
 
347
        }
 
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);
 
354
            }
 
355
            selectedSB.setBounds(currentSB.getBounds());
 
356
            if (selectedSB!=null)
 
357
              selectedSB.setVisible(true);
 
358
              currentSB = selectedSB;
 
359
            sbp.invalidate();
 
360
          }
 
361
        }
 
362
      };
 
363
 
 
364
      for (int i=0;i<cbNumber;i++) {      
 
365
        checkboxes[i].addItemListener(new radioItemListener(scrollBarNames[0]));
 
366
      }
 
367
      
 
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
 
372
      c.gridheight=1;
 
373
      c.fill=GridBagConstraints.NONE;
 
374
      c.anchor=GridBagConstraints.WEST;
 
375
      sbp.add(firstRow,c);
 
376
      
 
377
      c.gridy=1;
 
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;
 
383
          sbp.add(sb,c);
 
384
      }
 
385
      
 
386
      return sbp;
 
387
    }
 
388
 
 
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");
 
396
    }
 
397
 
 
398
        public void randomGraph() throws TGException {
 
399
        Node n1= tgPanel.addNode();
 
400
        n1.setType(0);
 
401
        for ( int i=0; i<249; i++ ) {
 
402
                tgPanel.addNode();
 
403
        }
 
404
        
 
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);   
 
411
                            }
 
412
                        }
 
413
                };      
 
414
                tgPanel.getGES().forAllNodes(fen);
 
415
                
 
416
        tgPanel.setLocale(n1,1);
 
417
        tgPanel.setSelect(n1);
 
418
        try {
 
419
            Thread.currentThread().sleep(2000); 
 
420
        } catch (InterruptedException ex) {}                                                    
 
421
 
 
422
        getHVScroll().slowScrollToCenter(n1);
 
423
    }    
 
424
 
 
425
    public static void main(String [] args) {
 
426
 
 
427
        final Frame frame;
 
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);
 
433
              frame.dispose();
 
434
            }
 
435
        });
 
436
        frame.add("Center", glPanel);
 
437
        frame.setSize(800,600);
 
438
        frame.setVisible(true);
 
439
    }
 
440
 
 
441
 
 
442
} // end com.touchgraph.graphlayout.GLPanel