KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > examples > boundedbuffer > AppletBuffer


1 /*
2 * ################################################################
3 *
4 * ProActive: The Java(TM) library for Parallel, Distributed,
5 * Concurrent computing with Security and Mobility
6 *
7 * Copyright (C) 1997-2002 INRIA/University of Nice-Sophia Antipolis
8 * Contact: proactive-support@inria.fr
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23 * USA
24 *
25 * Initial developer(s): The ProActive Team
26 * http://www.inria.fr/oasis/ProActive/contacts.html
27 * Contributor(s):
28 *
29 * ################################################################
30 */

31 package org.objectweb.proactive.examples.boundedbuffer;
32
33 import org.objectweb.proactive.core.config.ProActiveConfiguration;
34
35 /**
36  * <p>
37  * A classical bounded buffer composed of cells. The buffer is shared by a
38  * producer and a consumer. The producer can only write in an empty cell, and the
39  * consumer can only read a full cell.
40  * </p>
41  *
42  * @author ProActive Team
43  * @version 1.0, 2001/10/23
44  * @since ProActive 0.9
45  *
46  */

47 public class AppletBuffer extends org.objectweb.proactive.examples.StandardFrame {
48
49   /**
50    * The graphic panes
51    */

52   public CellPanel cells[];
53   private int max;
54   private ActiveDisplay display;
55   private javax.swing.JButton JavaDoc bProd;
56   private javax.swing.JButton JavaDoc bCons;
57
58
59   public AppletBuffer(String JavaDoc name, int width, int height) {
60     super(name);
61     max = 7;
62     init(width, height);
63   }
64
65
66   public static void main(String JavaDoc args[]) {
67     ProActiveConfiguration.load();
68     new AppletBuffer("Bounded Buffer", 500, 300);
69   }
70
71
72   public void start() {
73     cells[0].setIn(true);
74     cells[0].setOut(true);
75     try {
76       display = (ActiveDisplay)org.objectweb.proactive.ProActive.turnActive(new ActiveDisplay(max, this));
77       receiveMessage("ActiveDisplay created : " + display.getClass().getName());
78     } catch (Exception JavaDoc e) {
79       e.printStackTrace();
80     }
81     display.start();
82   }
83
84
85   public void kill() {
86     display.done();
87   }
88
89
90   public void setCell(int pos, String JavaDoc str) {
91     //displayMessage("set["+pos+"]="+str);
92
cells[pos].setCaption(str);
93     repaint();
94   }
95
96
97   public void setOut(int pos, boolean val) {
98     //displayMessage("out["+pos+"]="+val);
99
cells[pos].setOut(val);
100     repaint();
101   }
102
103
104   public void setIn(int pos, boolean val) {
105     //displayMessage("in["+pos+"]="+val);
106
cells[pos].setIn(val);
107     repaint();
108   }
109
110
111   public void consumerStartRunning() {
112     receiveMessage("consumer now start running");
113     bCons.setText("Stop");
114   }
115
116
117   public void consumerStopRunning() {
118     receiveMessage("consumer now stop running");
119     bCons.setText("Start");
120   }
121
122
123   public void producerStartRunning() {
124     receiveMessage("producer now start running");
125     bProd.setText("Stop");
126   }
127
128
129   public void producerStopRunning() {
130     receiveMessage("producer now stop running");
131     bProd.setText("Start");
132   }
133
134   //
135
// -- PROTECTED METHODS -----------------------------------------------
136
//
137

138   protected javax.swing.JPanel JavaDoc createRootPanel() {
139     javax.swing.JPanel JavaDoc rootPanel = new javax.swing.JPanel JavaDoc();
140     rootPanel.setBackground(java.awt.Color.white);
141     rootPanel.setLayout(new java.awt.GridLayout JavaDoc(3, 1));
142
143     // Producer
144
javax.swing.JPanel JavaDoc panel = new javax.swing.JPanel JavaDoc();
145     javax.swing.JLabel JavaDoc label = new javax.swing.JLabel JavaDoc("Producer");
146     label.setForeground(java.awt.Color.red);
147     panel.add(label);
148     bProd = new javax.swing.JButton JavaDoc("Start");
149     bProd.setBackground(java.awt.Color.lightGray);
150     bProd.addActionListener(new java.awt.event.ActionListener JavaDoc() {
151
152       public void actionPerformed(java.awt.event.ActionEvent JavaDoc e) {
153         display.toggleProd();
154       }
155     });
156     panel.add(bProd);
157
158     rootPanel.add(panel);
159
160     // Cells panel
161
javax.swing.JPanel JavaDoc cellsPanel = new javax.swing.JPanel JavaDoc();
162     cells = new CellPanel[max];
163     for (int i = 0; i < max; i++) {
164       cells[i] = new CellPanel();
165       cellsPanel.add(cells[i]);
166     }
167     rootPanel.add(cellsPanel);
168
169     // Consumer panel
170
panel = new javax.swing.JPanel JavaDoc();
171     label = new javax.swing.JLabel JavaDoc("Consumer");
172     label.setForeground(java.awt.Color.green);
173     panel.add(label);
174     bCons = new javax.swing.JButton JavaDoc("Start");
175     bCons.setBackground(java.awt.Color.lightGray);
176     bCons.addActionListener(new java.awt.event.ActionListener JavaDoc() {
177
178       public void actionPerformed(java.awt.event.ActionEvent JavaDoc e) {
179         display.toggleCons();
180       }
181     });
182     panel.add(bCons);
183     rootPanel.add(panel);
184     return rootPanel;
185   }
186
187
188   //
189
// -- INNER CLASSES -----------------------------------------------
190
//
191

192   protected class CellPanel extends javax.swing.JPanel JavaDoc {
193
194     private javax.swing.JLabel JavaDoc caption;
195     private boolean in;
196     private boolean out;
197     private boolean empty;
198
199
200     public CellPanel() {
201       super();
202       in = out = false;
203       empty = true;
204       caption = new javax.swing.JLabel JavaDoc("-empty-");
205       add(caption);
206       java.awt.Dimension JavaDoc d = new java.awt.Dimension JavaDoc(80, 25);
207       this.setSize(d);
208       this.setPreferredSize(d);
209       this.setMinimumSize(d);
210       this.setMaximumSize(d);
211     }
212
213
214     public void setIn(boolean in) {
215       this.in = in;
216       repaint();
217     }
218
219
220     public void setOut(boolean out) {
221       this.out = out;
222       repaint();
223     }
224
225
226     public void setCaption(String JavaDoc str) {
227       if (str == null) {
228         str = "-empty-";
229         empty = true;
230       } else {
231         empty = false;
232       }
233       caption.setText(str);
234     }
235
236
237     protected void paintComponent(java.awt.Graphics JavaDoc g) {
238       super.paintComponent(g);
239       if (empty)
240         setBackground(java.awt.Color.gray);
241       else
242         setBackground(java.awt.Color.blue);
243       if (in) {
244         java.awt.Color JavaDoc old = g.getColor();
245         g.setColor(java.awt.Color.red);
246         g.fillRect(1, 1, 5, 5);
247         g.setColor(old);
248       }
249       if (out) {
250         java.awt.Color JavaDoc old = g.getColor();
251         g.setColor(java.awt.Color.green);
252         g.fillRect(1, 10, 5, 5);
253         g.setColor(old);
254       }
255     }
256   }
257 }
Popular Tags