~ubuntu-branches/ubuntu/precise/jcsp/precise

« back to all changes in this revision

Viewing changes to src/jcsp-demos/doc-ex/jcsp/plugNplay/FramedButtonGridExample.java

  • Committer: Bazaar Package Importer
  • Author(s): Miguel Landaeta
  • Date: 2010-06-20 18:12:26 UTC
  • Revision ID: james.westby@ubuntu.com-20100620181226-8yg8d9rjjjiuy7oz
Tags: upstream-1.1-rc4
ImportĀ upstreamĀ versionĀ 1.1-rc4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import org.jcsp.lang.*;
 
2
import org.jcsp.util.*;
 
3
import org.jcsp.plugNplay.*;
 
4
 
 
5
public class FramedButtonGridExample {
 
6
 
 
7
  public static void main (String argv[]) {
 
8
  
 
9
    // labels for the grid of buttons
 
10
 
 
11
    final String[][] label = {
 
12
      new String[] {"Java", "occam-pi", "Handel-C"},
 
13
      new String[] {"C", "C++", "C#"},
 
14
      new String[] {"Haskell", "Modula", "Goodbye World"}
 
15
    };
 
16
 
 
17
    final int nDown = label.length;
 
18
    final int nAcross = label[0].length;
 
19
 
 
20
    // initial pixel sizes for the frame for the grid of buttons
 
21
 
 
22
    final int pixDown = 20 + (nDown*100);
 
23
    final int pixAcross = nAcross*120;
 
24
  
 
25
    // all button events are wired (for this example) to the same channel ...
 
26
 
 
27
    final Any2OneChannel allEvents =
 
28
      Channel.any2one (new OverWriteOldestBuffer (10));
 
29
 
 
30
    final Any2OneChannel[][] event = new Any2OneChannel[nDown][nAcross];
 
31
    
 
32
    for (int i = 0; i < nDown; i++) {
 
33
      for (int j = 0; j < nAcross; j++) {
 
34
        event[i][j] = allEvents;
 
35
      }
 
36
    }
 
37
 
 
38
    // make the grid of buttons (each one separately configured) ...
 
39
 
 
40
    final One2OneChannel[][] configure = new One2OneChannel[nDown][nAcross];
 
41
    
 
42
    for (int i = 0; i < nDown; i++) {
 
43
      configure[i] = Channel.one2oneArray (nAcross);
 
44
    }
 
45
 
 
46
    final ChannelInput[][] configureIn = new ChannelInput[nDown][nAcross];
 
47
    final ChannelOutput[][] eventOut = new ChannelOutput[nDown][nAcross];
 
48
    
 
49
    for (int i = 0; i < nDown; i++) {
 
50
      configureIn[i] = Channel.getInputArray (configure[i]);
 
51
      eventOut[i] = Channel.getOutputArray (event[i]);
 
52
    }
 
53
 
 
54
    final FramedButtonGrid grid =
 
55
      new FramedButtonGrid (
 
56
        "FramedButtonGrid Demo", nDown, nAcross,
 
57
        pixDown, pixAcross, configureIn, eventOut
 
58
      );
 
59
 
 
60
    // testrig ...
 
61
 
 
62
    new Parallel (
 
63
    
 
64
      new CSProcess[] {
 
65
      
 
66
        grid,
 
67
        
 
68
        new CSProcess () {
 
69
        
 
70
          public void run () {
 
71
    
 
72
            for (int i = 0; i < nDown; i++) {
 
73
              for (int j = 0; j < nAcross; j++) {
 
74
                configure[i][j].out ().write (label[i][j]);
 
75
              }
 
76
            }
 
77
            
 
78
            boolean running = true;
 
79
            while (running) {
 
80
              final String s = (String) allEvents.in ().read ();
 
81
              System.out.println ("Button `" + s + "' pressed ...");
 
82
              running = (s != label[nDown - 1][nAcross - 1]);
 
83
            }
 
84
            
 
85
            System.exit (0);
 
86
            
 
87
          }
 
88
          
 
89
        }
 
90
        
 
91
      }
 
92
    ).run ();
 
93
 
 
94
  }
 
95
 
 
96
}