1 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 agentList; 22 protected javax.swing.JTextArea 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 createRootPanel() { 41 javax.swing.JPanel rootPanel = new javax.swing.JPanel (new java.awt.BorderLayout ()); 42 43 javax.swing.JPanel p1 = new javax.swing.JPanel (new java.awt.GridLayout (0, 1)); 45 javax.swing.JButton bStart = new javax.swing.JButton ("Start"); 46 bStart.setToolTipText("Start itinerary"); 47 bStart.addActionListener(new java.awt.event.ActionListener () { 48 public void actionPerformed(java.awt.event.ActionEvent e) { 49 executeAction(agentList.getSelectedIndices(), startAction); 50 } 51 }); 52 p1.add(bStart); 53 54 javax.swing.JButton bSuspend = new javax.swing.JButton ("Suspend"); 55 bSuspend.setToolTipText("Suspend itinerary"); 56 bSuspend.addActionListener(new java.awt.event.ActionListener () { 57 public void actionPerformed(java.awt.event.ActionEvent e) { 58 executeAction(agentList.getSelectedIndices(), suspendAction); 59 } 60 }); 61 p1.add(bSuspend); 62 63 javax.swing.JButton bResume = new javax.swing.JButton ("Resume"); 64 bResume.setToolTipText("Resume itinerary"); 65 bResume.addActionListener(new java.awt.event.ActionListener () { 66 public void actionPerformed(java.awt.event.ActionEvent e) { 67 executeAction(agentList.getSelectedIndices(), resumeAction); 68 } 69 }); 70 p1.add(bResume); 71 72 javax.swing.JButton bSet = new javax.swing.JButton ("Set itinerary"); 73 bSet.setToolTipText("Set new itinerary"); 74 bSet.addActionListener(new java.awt.event.ActionListener () { 75 public void actionPerformed(java.awt.event.ActionEvent e) { 76 setItinerary(agentList.getSelectedIndices()); 77 } 78 }); 79 p1.add(bSet); 80 81 rootPanel.add(p1, java.awt.BorderLayout.WEST); 82 83 javax.swing.JPanel p2 = new javax.swing.JPanel (new java.awt.GridLayout (0, 1)); 85 javax.swing.JButton bAddAgent = new javax.swing.JButton ("Add agent"); 86 bAddAgent.setToolTipText("Add one new agent"); 87 bAddAgent.addActionListener(new java.awt.event.ActionListener () { 88 public void actionPerformed(java.awt.event.ActionEvent e) { 89 addAgent(); 91 } 92 }); 93 p2.add(bAddAgent); 94 95 javax.swing.JButton bCall = new javax.swing.JButton ("Call"); 96 bCall.setToolTipText("Call"); 97 bCall.addActionListener(new java.awt.event.ActionListener () { 98 public void actionPerformed(java.awt.event.ActionEvent e) { 99 executeAction(agentList.getSelectedIndices(), callAction); 100 } 101 }); 102 p2.add(bCall); 103 104 javax.swing.JButton bCallOther = new javax.swing.JButton ("Chained calls"); 105 bCallOther.setToolTipText("Chained calls from selected agent"); 106 bCallOther.addActionListener(new java.awt.event.ActionListener () { 107 public void actionPerformed(java.awt.event.ActionEvent e) { 108 executeAction(agentList.getSelectedIndices(), chainedCallAction); 109 } 110 }); 111 p2.add(bCallOther); 112 113 javax.swing.JButton bGet = new javax.swing.JButton ("Get itinerary"); 114 bGet.setToolTipText("Get current itinerary"); 115 bGet.addActionListener(new java.awt.event.ActionListener () { 116 public void actionPerformed(java.awt.event.ActionEvent e) { 117 itineraryField.setText(getItinerary(agentList.getSelectedIndices())); 118 } 119 }); 120 p2.add(bGet); 121 122 rootPanel.add(p2, java.awt.BorderLayout.EAST); 123 124 javax.swing.JPanel p3 = new javax.swing.JPanel (new java.awt.GridLayout (1, 0)); 126 127 agentList = new javax.swing.JList (penguinListModel); 128 agentList.setToolTipText("Agent list"); 129 p3.add(new javax.swing.JScrollPane (agentList)); 130 131 itineraryField = new javax.swing.JTextArea (); 132 itineraryField.setToolTipText("Itinerary of selected agent"); 133 p3.add(new javax.swing.JScrollPane (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 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 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 [] tabItinerary = p.getItinerary(); 181 String itinerary = new String (); 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; 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 { 244 245 private CircularArrayList penguinList; 246 247 public PenguinListModel(CircularArrayList penguinList) { 251 this.penguinList = penguinList; 252 } 253 254 258 public boolean isEmpty() { 262 return penguinList.isEmpty(); 263 } 264 265 public int getSize() { 266 return penguinList.size(); 267 } 268 269 public Object 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 } 289 290 private static class PenguinWrapper implements java.io.Serializable { 291 Penguin penguin; 292 String name; 293 PenguinWrapper(Penguin penguin, String name) { 294 this.penguin = penguin; 295 this.name = name; 296 } 297 298 public String [] getItinerary() { 299 return penguin.getItinerary(); 300 } 301 302 public void setItinerary(String itinerary) { 303 java.util.ArrayList itineraryList = new java.util.ArrayList (); 304 java.util.StringTokenizer tokenizer = new java.util.StringTokenizer (itinerary, "\n"); 305 while (tokenizer.hasMoreTokens()) 306 itineraryList.add(tokenizer.nextToken()); 307 String [] intineraryArray = (String [])itineraryList.toArray(new String [0]); 308 penguin.setItinerary(intineraryArray); 309 } 310 311 public String toString() { 312 return name; 313 } 314 } 315 } 316 317 318 | Popular Tags |