KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > examples > penguin > PenguinApplet


1 /**
2  * Created on 27 mai 2002
3  *
4  * To change this generated comment edit the template variable "filecomment":
5  * Window>Preferences>Java>Templates.
6  */

7 package org.objectweb.proactive.examples.penguin;
8
9 import org.objectweb.proactive.examples.StandardFrame;
10 import org.objectweb.proactive.core.util.CircularArrayList;
11
12 public class PenguinApplet extends StandardFrame implements PenguinMessageReceiver {
13     
14     private PenguinControler controler;
15     private Action startAction;
16     private Action suspendAction;
17     private Action resumeAction;
18     private Action callAction;
19     private Action chainedCallAction;
20     protected PenguinListModel penguinListModel;
21     protected javax.swing.JList JavaDoc agentList;
22     protected javax.swing.JTextArea JavaDoc itineraryField;
23
24     public PenguinApplet(PenguinControler c, CircularArrayList penguinList) {
25     super("Advanced Penguin Controler");
26     this.controler = c;
27     this.penguinListModel = new PenguinListModel(penguinList);
28     this.startAction = new StartAction();
29     this.suspendAction = new SuspendAction();
30     this.resumeAction = new ResumeAction();
31     this.callAction = new CallAction();
32     this.chainedCallAction = new ChainedCallAction();
33     init(600, 300);
34     }
35     
36     public void start() {
37     receiveMessage("Started...");
38     }
39     
40     protected javax.swing.JPanel JavaDoc createRootPanel() {
41     javax.swing.JPanel JavaDoc rootPanel = new javax.swing.JPanel JavaDoc(new java.awt.BorderLayout JavaDoc());
42     
43     // WEST PANEL
44
javax.swing.JPanel JavaDoc p1 = new javax.swing.JPanel JavaDoc(new java.awt.GridLayout JavaDoc(0, 1));
45     javax.swing.JButton JavaDoc bStart = new javax.swing.JButton JavaDoc("Start");
46     bStart.setToolTipText("Start itinerary");
47     bStart.addActionListener(new java.awt.event.ActionListener JavaDoc() {
48         public void actionPerformed(java.awt.event.ActionEvent JavaDoc e) {
49         executeAction(agentList.getSelectedIndices(), startAction);
50         }
51     });
52     p1.add(bStart);
53     
54     javax.swing.JButton JavaDoc bSuspend = new javax.swing.JButton JavaDoc("Suspend");
55     bSuspend.setToolTipText("Suspend itinerary");
56     bSuspend.addActionListener(new java.awt.event.ActionListener JavaDoc() {
57         public void actionPerformed(java.awt.event.ActionEvent JavaDoc e) {
58         executeAction(agentList.getSelectedIndices(), suspendAction);
59         }
60     });
61     p1.add(bSuspend);
62     
63     javax.swing.JButton JavaDoc bResume = new javax.swing.JButton JavaDoc("Resume");
64     bResume.setToolTipText("Resume itinerary");
65     bResume.addActionListener(new java.awt.event.ActionListener JavaDoc() {
66         public void actionPerformed(java.awt.event.ActionEvent JavaDoc e) {
67         executeAction(agentList.getSelectedIndices(), resumeAction);
68         }
69     });
70     p1.add(bResume);
71     
72     javax.swing.JButton JavaDoc bSet = new javax.swing.JButton JavaDoc("Set itinerary");
73     bSet.setToolTipText("Set new itinerary");
74     bSet.addActionListener(new java.awt.event.ActionListener JavaDoc() {
75         public void actionPerformed(java.awt.event.ActionEvent JavaDoc e) {
76         setItinerary(agentList.getSelectedIndices());
77         }
78     });
79     p1.add(bSet);
80     
81     rootPanel.add(p1, java.awt.BorderLayout.WEST);
82     
83     // EAST PANEL
84
javax.swing.JPanel JavaDoc p2 = new javax.swing.JPanel JavaDoc(new java.awt.GridLayout JavaDoc(0, 1));
85     javax.swing.JButton JavaDoc bAddAgent = new javax.swing.JButton JavaDoc("Add agent");
86     bAddAgent.setToolTipText("Add one new agent");
87     bAddAgent.addActionListener(new java.awt.event.ActionListener JavaDoc() {
88         public void actionPerformed(java.awt.event.ActionEvent JavaDoc e) {
89         //receiveMessage("Add new agent pressed");
90
addAgent();
91         }
92     });
93     p2.add(bAddAgent);
94
95     javax.swing.JButton JavaDoc bCall = new javax.swing.JButton JavaDoc("Call");
96     bCall.setToolTipText("Call");
97     bCall.addActionListener(new java.awt.event.ActionListener JavaDoc() {
98         public void actionPerformed(java.awt.event.ActionEvent JavaDoc e) {
99         executeAction(agentList.getSelectedIndices(), callAction);
100         }
101     });
102     p2.add(bCall);
103     
104     javax.swing.JButton JavaDoc bCallOther = new javax.swing.JButton JavaDoc("Chained calls");
105     bCallOther.setToolTipText("Chained calls from selected agent");
106     bCallOther.addActionListener(new java.awt.event.ActionListener JavaDoc() {
107         public void actionPerformed(java.awt.event.ActionEvent JavaDoc e) {
108         executeAction(agentList.getSelectedIndices(), chainedCallAction);
109       }
110     });
111     p2.add(bCallOther);
112     
113     javax.swing.JButton JavaDoc bGet = new javax.swing.JButton JavaDoc("Get itinerary");
114     bGet.setToolTipText("Get current itinerary");
115     bGet.addActionListener(new java.awt.event.ActionListener JavaDoc() {
116         public void actionPerformed(java.awt.event.ActionEvent JavaDoc e) {
117         itineraryField.setText(getItinerary(agentList.getSelectedIndices()));
118         }
119     });
120     p2.add(bGet);
121     
122     rootPanel.add(p2, java.awt.BorderLayout.EAST);
123     
124     // CENTER PANEL
125
javax.swing.JPanel JavaDoc p3 = new javax.swing.JPanel JavaDoc(new java.awt.GridLayout JavaDoc(1, 0));
126     
127     agentList = new javax.swing.JList JavaDoc(penguinListModel);
128     agentList.setToolTipText("Agent list");
129     p3.add(new javax.swing.JScrollPane JavaDoc(agentList));
130     
131     itineraryField = new javax.swing.JTextArea JavaDoc();
132     itineraryField.setToolTipText("Itinerary of selected agent");
133     p3.add(new javax.swing.JScrollPane JavaDoc(itineraryField));
134     
135     rootPanel.add(p3, java.awt.BorderLayout.CENTER);
136     return rootPanel;
137     }
138     
139
140     private void addAgent() {
141     int n = penguinListModel.getSize();
142     try {
143         Penguin newPenguin = controler.createPenguin(n);
144         PenguinWrapper pw = new PenguinWrapper(newPenguin, "Agent " + n);
145         penguinListModel.addPenguin(pw);
146     } catch (Exception JavaDoc e) {
147         e.printStackTrace();
148     }
149     if (n > 0) {
150         PenguinWrapper p1 = (PenguinWrapper) penguinListModel.getElementAt(n - 1);
151         PenguinWrapper p2 = (PenguinWrapper) penguinListModel.getElementAt(n);
152         (p1.penguin).setOther(p2.penguin);
153     } else {
154         agentList.setSelectedIndex(0);
155     }
156     }
157
158
159     private void executeAction(int[] selection, Action action) {
160     if (selection.length == 0) {
161         receiveMessage("You must select an agent in the list first or add one if none is already present");
162     } else {
163         for (int i = 0; i < selection.length; i++) {
164         int value = selection[i];
165         PenguinWrapper p = (PenguinWrapper) penguinListModel.getElementAt(value);
166         action.execute(p.penguin);
167         }
168     }
169     }
170     
171     
172     private String JavaDoc getItinerary(int[] selection) {
173     if (selection.length == 0)
174         receiveMessage("You must select an agent in the list first or add one if none is already present");
175     else if (selection.length > 1)
176         receiveMessage("You must select one agent");
177     else {
178         int value = selection[0];
179         PenguinWrapper p = (PenguinWrapper) penguinListModel.getElementAt(value);
180         String JavaDoc[] tabItinerary = p.getItinerary();
181         String JavaDoc itinerary = new String JavaDoc();
182         if (tabItinerary.length != 0) {
183         itinerary = tabItinerary[0];
184         for (int i = 1; i < tabItinerary.length; i++) {
185             itinerary = itinerary + "\n" + tabItinerary[i];
186         }
187         }
188         return itinerary;
189     }
190     //return null if problem of selection
191
return null;
192     }
193
194
195     private void setItinerary(int[] selection) {
196     if (selection.length == 0) {
197         receiveMessage("You must select an agent in the list first or add one if none is already present");
198     } else {
199         for (int i = 0; i < selection.length; i++) {
200         int value = selection[i];
201         PenguinWrapper p = (PenguinWrapper) penguinListModel.getElementAt(value);
202         p.setItinerary(itineraryField.getText());
203         }
204     }
205     }
206
207
208   private interface Action {
209     public void execute(Penguin p);
210   }
211
212   private class StartAction implements Action {
213     public void execute(Penguin p) {
214       p.start();
215     }
216   }
217   
218   private class SuspendAction implements Action {
219     public void execute(Penguin p) {
220       p.suspend();
221     }
222   }
223
224   private class ResumeAction implements Action {
225     public void execute(Penguin p) {
226       p.resume();
227     }
228   }
229
230   private class CallAction implements Action {
231     public void execute(Penguin p) {
232       receiveMessage(p.call());
233     }
234   }
235
236   private class ChainedCallAction implements Action {
237     public void execute(Penguin p) {
238       p.chainedCall();
239     }
240   }
241   
242   
243   private static class PenguinListModel extends javax.swing.AbstractListModel JavaDoc {
244   
245     private CircularArrayList penguinList;
246   
247     //
248
// -- CONSTRUCTORS -----------------------------------------------
249
//
250
public PenguinListModel(CircularArrayList penguinList) {
251       this.penguinList = penguinList;
252     }
253
254     //
255
// -- Public methods -----------------------------------------------
256
//
257

258     //
259
// -- implements ListModel -----------------------------------------------
260
//
261
public boolean isEmpty() {
262       return penguinList.isEmpty();
263     }
264     
265     public int getSize() {
266       return penguinList.size();
267     }
268
269     public Object JavaDoc getElementAt(int index) {
270       return penguinList.get(index);
271     }
272     
273     public void addPenguin(PenguinWrapper pw) {
274       int n = penguinList.size();
275       penguinList.add(pw);
276       fireIntervalAdded(this, n, n);
277     }
278     
279     public void clear() {
280       int n = penguinList.size();
281       if (n>0) {
282         penguinList.clear();
283         fireIntervalRemoved(this, 0, n-1);
284       }
285     }
286     
287   } // end inner class PenguinListModel
288

289
290   private static class PenguinWrapper implements java.io.Serializable JavaDoc {
291     Penguin penguin;
292     String JavaDoc name;
293     PenguinWrapper(Penguin penguin, String JavaDoc name) {
294       this.penguin = penguin;
295       this.name = name;
296     }
297
298     public String JavaDoc[] getItinerary() {
299       return penguin.getItinerary();
300     }
301
302     public void setItinerary(String JavaDoc itinerary) {
303     java.util.ArrayList JavaDoc itineraryList = new java.util.ArrayList JavaDoc();
304     java.util.StringTokenizer JavaDoc tokenizer = new java.util.StringTokenizer JavaDoc(itinerary, "\n");
305     while (tokenizer.hasMoreTokens())
306         itineraryList.add(tokenizer.nextToken());
307     String JavaDoc[] intineraryArray = (String JavaDoc[])itineraryList.toArray(new String JavaDoc[0]);
308     penguin.setItinerary(intineraryArray);
309     }
310     
311     public String JavaDoc toString() {
312       return name;
313     }
314   }
315 }
316
317
318
Popular Tags