1 26 27 package ch.ethz.prose.tools; 28 29 import java.net.MalformedURLException ; 30 import java.rmi.NotBoundException ; 31 import java.rmi.RemoteException ; 32 import java.util.Arrays ; 33 import java.util.Iterator ; 34 import java.util.List ; 35 import java.util.Vector ; 36 import java.util.HashSet ; 37 38 import javax.swing.event.TableModelEvent ; 39 import javax.swing.table.DefaultTableModel ; 40 41 import ch.ethz.prose.Aspect; 42 import ch.ethz.prose.AspectManagerException; 43 import ch.ethz.prose.LocalAspectManager; 44 import ch.ethz.prose.query.QueryManager; 45 import ch.ethz.prose.query.JoinPointRequestSurrogate; 46 import ch.ethz.prose.query.AspectSurrogate; 47 import ch.ethz.prose.query.Tuple; 48 49 50 54 public class WorksheetSingleClientModel extends DefaultTableModel { 55 56 public static final int BY_ASPECT = QueryManager.GROUP_BY_ASPECT; 57 public static final int BY_CROSSCUT = QueryManager.GROUP_BY_CROSSCUT; 58 public static final int BY_JOINPOINT = QueryManager.GROUP_BY_JOINPOINT; 59 public static final int SELECT_ASPECT = QueryManager.SELECT_ASPECT; 60 public static final int SELECT_CROSSCUT = QueryManager.SELECT_CROSSCUT; 61 public static final int SELECT_JOINPOINT = QueryManager.SELECT_JOINPOINT; 62 public static final int SELECT_ALL = QueryManager.SELECT_ALL; 63 private static final int NR_OF_ROWS = 40; private static Vector nullVector = new Vector (); 65 66 protected List table = new Vector (); protected Vector data = new Vector (); protected int select = SELECT_ALL; 69 protected int groupBy = BY_ASPECT; 70 protected int[] selectedCells = null; 71 protected int selectedColumn = -1; 72 protected int[] pastedCells = null; protected RemoteAspectManager exMngr; 74 protected String address; 75 protected boolean isReal; 76 protected MultipleClientModel parentModel; 77 protected String name; 78 protected String host; 79 protected String port; 80 81 private boolean addedFlag = false; 83 { nullVector.add(null); nullVector.add(null); nullVector.add(null); } 84 85 86 private void checkIsConnected() throws IllegalUserInputException 87 { 88 if (exMngr == null) 89 throw new IllegalUserInputException("Please select an active worksheet"); 90 } 91 92 93 94 public WorksheetSingleClientModel(MultipleClientModel ownerModel, Object [][] initData, String [] initColumnNames, String name, String host,String port, boolean isReal) 95 96 { 97 super(initData, initColumnNames); 98 this.parentModel = ownerModel; 99 this.isReal = isReal; 100 this.host = host; 101 this.port = port; 102 this.address = host + ":" + port; 103 this.name = name; 104 selectedCells = new int[0]; 105 int i=0; 106 for (i=0; i < initData.length; i++) 107 { 108 Vector dataElement = new Vector (); 109 dataElement.add(initData[i][1]); 110 dataElement.add(initData[i][2]); 111 dataElement.add(initData[i][3]); 112 } 113 114 updateTable(data); 115 } 116 117 public String getName() 118 { 119 return name; 120 } 121 122 public String getHost() 123 { 124 return host; 125 } 126 127 public String getPort() 128 { 129 return port; 130 } 131 132 public String getAddress() 133 { 134 return address; 135 } 136 137 public boolean isReal() 138 { 139 return isReal; 140 } 141 142 143 144 145 public void setDisplayAspect() 146 { 147 select ^= SELECT_ASPECT; 148 } 149 150 public void setDisplayCrosscut() 151 { 152 select ^= SELECT_CROSSCUT; 153 } 154 155 public void setDisplayJoinpoint() 156 { 157 select ^= SELECT_JOINPOINT; 158 } 159 160 public void setGroupByAspect() 161 { 162 groupBy = BY_ASPECT; 163 } 164 165 public void setGroupByCrosscut() 166 { 167 groupBy = BY_CROSSCUT; 168 } 169 170 public void setGroupByJoinpoint() 171 { 172 groupBy = BY_JOINPOINT; 173 } 174 175 public void setSelectedCells(int[] indices) 176 { 177 selectedCells = indices; 178 } 179 180 public int getSelectedColumn() 181 { 182 return selectedColumn; 183 } 184 185 public void setSelectedColumn(int ind) 186 { 187 selectedColumn = ind; 188 } 189 190 public boolean isCellEditable(int rowIndex, int columnIndex) 191 { 192 return false; 193 } 194 195 196 public synchronized void allAspects() throws RemoteException ,IllegalUserInputException 197 { 198 checkIsConnected(); 199 table = new Vector (); 200 Iterator i = exMngr.allAspects().iterator(); 201 while (i.hasNext()) 202 { 203 table.add(new Tuple((AspectSurrogate)i.next(), null, null)); 204 } 205 convertDataForGUI(); 206 updateTable(data); 207 } 208 209 public synchronized void allJoinpoints() throws RemoteException ,IllegalUserInputException 213 { 214 checkIsConnected(); 215 216 table = new Vector (); 217 Iterator i = ((RemoteAspectManager)exMngr).allJoinpoints().iterator(); 218 while(i.hasNext()) 219 table.add(new Tuple(null, null, (JoinPointRequestSurrogate)i.next())); 220 convertDataForGUI(); 222 updateTable(data); 223 } 224 225 226 public synchronized void query() throws RemoteException , IllegalUserInputException 227 { 228 checkIsConnected(); 229 try{ 230 if (selectedColumn == -1) 231 { 232 throw new RuntimeException ("BUG: multiple selection over columns not allowed"); 233 } 234 235 List inputList = new Vector (); 236 if (selectedColumn == 0) 237 { 238 for (int j=0; j < selectedCells.length; j++) 239 inputList.add(((Tuple)table.get(selectedCells[j])).getAspectSurrogate()); 240 table = exMngr.queryAspects(inputList, select, groupBy); 241 } 242 else if (selectedColumn == 1) 243 { 244 for (int j=0; j < selectedCells.length; j++) 245 inputList.add(((Tuple)table.get(selectedCells[j])).getCrosscutSurrogate()); 246 table = exMngr.queryCrosscuts(inputList, select, groupBy); 247 } 248 else if (selectedColumn == 2) 249 { 250 for (int j=0; j < selectedCells.length; j++) 251 inputList.add(((Tuple)table.get(selectedCells[j])).getRequestSurrogate()); 252 table = exMngr.queryJoinpoints(inputList, select, groupBy); 253 } 254 else throw new RuntimeException ("Row selection failed! [rowNumber: " + selectedColumn + "]"); 255 256 convertDataForGUI(); 257 updateTable(data); 258 } 259 catch (RemoteException e) 260 { 261 throw e; 262 } 263 } 264 265 266 public int getSelect() 267 { 268 return select; 269 } 270 271 public int getGroupBy() 272 { 273 return groupBy; 274 } 275 276 public boolean isMixedSelection() 277 { 278 if (pastedCells == null) 279 return false; 280 int mixdex = 0; 281 for (int j=0; j < selectedCells.length; j++) 282 if ((selectedCells[j] >= pastedCells[0]) && (selectedCells[j] <= pastedCells[pastedCells.length-1])) 283 mixdex++; 284 else 285 mixdex--; 286 if (Math.abs(mixdex) != selectedCells.length) 287 return true; 288 else 289 return false; 290 } 291 292 293 protected void pasteJoinpoints(List pasteList) 294 { 295 296 int pastedCellsTmp[]; 297 if (pastedCells == null) 298 pastedCellsTmp = new int[pasteList.size()]; 299 else 300 { 301 pastedCellsTmp = new int[pasteList.size() + pastedCells.length]; 302 for (int i=0; i < pastedCells.length; i++) 303 pastedCellsTmp[i] = pastedCells[i]; 304 } 305 306 int basicIndex = table.size(); 307 for (int i=0; i < pasteList.size(); i++) 308 { 309 Vector dataElement = new Vector (); 310 dataElement.add(null); 311 dataElement.add(null); 312 dataElement.add(pasteList.get(i)); 313 if (basicIndex + i == data.size() -1) 314 { 315 addedFlag = true; 316 data.set(basicIndex + i, dataElement); 317 } 318 else if (basicIndex + i > data.size() -1) 319 { 320 addedFlag = true; 321 data.add(dataElement); 322 } 323 else data.set(basicIndex + i, dataElement); 325 326 Tuple tup = new Tuple(null, null, (JoinPointRequestSurrogate)pasteList.get(i)); 327 table.add(tup); 328 329 if (pastedCells == null) 330 pastedCellsTmp[i] = basicIndex +i; 331 else 332 pastedCellsTmp[pastedCells.length + i] = basicIndex +i; 333 } 334 335 pastedCells = pastedCellsTmp; 336 Arrays.sort(pastedCells); 338 updateTable(data); 339 } 340 341 342 protected synchronized void pasteAspects(List pasteList) throws RemoteException , IllegalUserInputException, PasteAspectsException,TableSelectionException 344 { 345 checkIsConnected(); 346 PasteAspectsException pae = new PasteAspectsException(); 347 348 if (pasteList == null) 349 throw new TableSelectionException("Nothing to paste!"); 350 351 352 if (parentModel.getClasspath() == null || 353 parentModel.getClasspath().trim().equals("")) 354 throw new IllegalUserInputException("Missing classpath information"); 355 356 357 parentModel.addVMToActiveTransaction(this); 358 359 int j=0; 360 int basicIndex = table.size(); 361 List errorList = new Vector (); 362 List alreadyContained = exMngr.allAspects(); 363 for (int i=0; i < pasteList.size(); i++) 364 { 365 AspectSurrogate aspectSur = (AspectSurrogate)pasteList.get(i); 366 if (alreadyContained.contains(aspectSur)) 367 continue; 368 369 Aspect realAsp = null; 370 try 371 { 372 String [] execArgs = CommandlineProseClient.insertScriptCommandline(parentModel.getClasspath(), 373 aspectSur.getAspectClassName(), 374 aspectSur.getAssociatedObject(), 375 address, 376 parentModel.getActiveTransactionID(), 377 isReal); 378 379 for (int k = 0; k < execArgs.length; k++) 380 parentModel.getConsole().append(execArgs[k] + " "); 381 parentModel.getConsole().append("\n"); 382 Process p = Runtime.getRuntime().exec(execArgs); 383 parentModel.getConsole().useProcessOutput(p); 384 try { p.waitFor();} catch (InterruptedException e){} 385 if (p.exitValue() != 0) 386 throw new RuntimeException ("Cannot insert aspect"); 387 388 } 389 catch (Exception e) 390 { 391 e.printStackTrace(); 392 pae.add(e); 393 continue; 394 } 395 396 397 Tuple tup = new Tuple(aspectSur, null, null); 398 table.add(tup); 399 400 Vector dataElement = new Vector (); 401 dataElement.add(aspectSur); 402 dataElement.add(null); 403 dataElement.add(null); 404 if (basicIndex + j == data.size() -1) 405 { 406 addedFlag = true; 407 data.set(basicIndex + j, dataElement); 408 } 409 else if (basicIndex + j > data.size() -1) 410 { 411 addedFlag = true; 412 data.add(dataElement); 413 } 414 else data.set(basicIndex + j, dataElement); 416 417 j++; 418 } 419 updateTable(data); 420 if (pae.containsExceptions()) 421 { 422 pae.fillInStackTrace(); 423 throw pae; 424 } 425 } 426 427 protected synchronized void cutAspects() throws RemoteException ,TableSelectionException,WithdrawAspectsException 428 { 429 if (getSelectedColumn() == -1) 430 throw new TableSelectionException("Nothing to Cut!"); 431 if (getSelectedColumn() != JWorksheetProseClientPane.ASPECT_COL_INDEX) 432 throw new TableSelectionException("Cutting is allowed only on 'Aspects'."); 433 434 HashSet myRememberList = new HashSet (); 436 for (int i=0; i < selectedCells.length; i++) 437 { 438 AspectSurrogate aspect = (AspectSurrogate)((Vector )data.get(selectedCells[i])).get(JWorksheetProseClientPane.ASPECT_COL_INDEX); 439 if (aspect == null) 440 throw new TableSelectionException("Empty cells in selection!"); 441 myRememberList.add(aspect); 442 } 443 444 Iterator toCut = myRememberList.iterator(); 446 WithdrawAspectsException maE = new WithdrawAspectsException(); 447 while (toCut.hasNext()) 448 { 449 try 450 { 451 if (parentModel.getActiveTransactionID() == null) 452 exMngr.withdraw((AspectSurrogate)toCut.next()); 453 else 454 exMngr.withdraw((AspectSurrogate)toCut.next(),parentModel.getActiveTransactionID()); 455 } 456 catch (Exception e) 457 { 458 maE.add(e); 459 } 460 } 461 462 for (int i=0; i < table.size(); i++) 464 { 465 AspectSurrogate aspect = (AspectSurrogate)((Vector )data.get(i)).get(JWorksheetProseClientPane.ASPECT_COL_INDEX); 466 if (myRememberList.contains(aspect)) 467 { 468 data.remove(i); 469 table.remove(i); 470 i--; 471 } 472 } 473 updateTable(data); 474 475 476 if (maE.containsExceptions()) 477 throw maE; 478 } 479 480 481 482 public void updateTable(Vector data) 483 { 484 for (int i=data.size(); i < NR_OF_ROWS; i++) 485 data.add(nullVector); 486 487 if (addedFlag) 488 data.add(nullVector); 489 addedFlag = false; 490 this.setDataVector(data, createHeaders()); 491 fireTableChanged(new TableModelEvent (this)); 492 } 493 494 private Vector createHeaders() 495 { 496 Vector v = new Vector (); 497 v.add(new String ("Aspect")); 498 v.add(new String ("Crosscut")); 499 v.add(new String ("Joinpoint")); 500 return v; 501 } 502 503 private void convertDataForGUI() 504 { 505 pastedCells = null; data = new Vector (); 507 if (table == null) 508 return; 509 for (int i=0; i < table.size(); i++) 510 { 511 Vector dataElement = new Vector (); 512 dataElement.add(((Tuple)table.get(i)).getAspectSurrogate()); 513 dataElement.add(((Tuple)table.get(i)).getCrosscutSurrogate()); 514 dataElement.add(((Tuple)table.get(i)).getRequestSurrogate()); 515 data.add(dataElement); 516 } 517 if (data.size() >= NR_OF_ROWS) 518 addedFlag = true; 519 } 520 521 public synchronized void refresh() throws java.rmi.RemoteException ,java.rmi.NotBoundException ,java.net.MalformedURLException , java.net.UnknownHostException 522 { 523 int intPort; 524 RemoteAspectManager[] rams; 525 try 526 { 527 intPort = Integer.parseInt(port); 528 } 529 catch (NumberFormatException thisIsNotAPortNum) 530 { 531 throw new MalformedURLException ("Illegal port number"); 532 } 533 try 534 { 535 rams = RemoteProseComponent.doGetRemoteAspectManagers(host,intPort); 536 if (isReal) 537 exMngr= rams[0]; 538 else 539 exMngr = rams[1]; 540 } 541 catch (java.io.IOException e) 542 { 543 throw new RemoteException (e.toString()); 544 } 545 } 546 547 public synchronized void commit(Object transactionId) throws java.rmi.RemoteException 548 { 549 exMngr.commit(transactionId); 550 } 551 552 public synchronized void abort(Object transactionId) throws java.rmi.RemoteException 553 { 554 exMngr.abort(transactionId); 555 } 556 557 public String toString() 558 { 559 return name; 560 } 561 } 562 | Popular Tags |