1 27 package ch.ethz.prose.tools; 28 29 import java.awt.Color ; 31 import java.util.HashMap ; 32 import java.util.HashSet ; 33 import java.util.List ; 34 import java.util.Map ; 35 import java.util.Vector ; 36 import javax.swing.text.Document ; 37 import ch.ethz.prose.Aspect; 38 import ch.ethz.prose.query.AspectSurrogate; 39 40 import javax.swing.JTable ; 41 import javax.swing.JToggleButton ; 42 43 49 public class MultipleClientModel { 50 51 52 53 protected JTable actualTable = null; 54 protected WorksheetSingleClientModel actualTableModel = null; 55 protected List copyList; 56 protected int copyColumn; 57 protected String txId = null; 58 protected int txCnt = 0; 59 protected Document classPathModel; 60 protected Document aspectNameModel; 61 protected ConsoleDocumentModel wbConsole; 62 63 protected List createdTableModels; 64 protected List activeTransactionVMs; 65 JToggleButton.ToggleButtonModel selectAspectModel; 66 JToggleButton.ToggleButtonModel selectCrosscutModel; 67 JToggleButton.ToggleButtonModel selectJoinpointModel; 68 JToggleButton.ToggleButtonModel groupByAspectModel; 69 JToggleButton.ToggleButtonModel groupByCrosscutModel; 70 JToggleButton.ToggleButtonModel groupByJoinpointModel; 71 JToggleButton.ToggleButtonModel insertInAllVM; 72 JToggleButton.ToggleButtonModel insertInSelection; 73 74 75 private JMultipleProseClient view; 76 77 81 public MultipleClientModel(JMultipleProseClient view) 82 { 83 this.view = view; 84 classPathModel = new javax.swing.text.DefaultStyledDocument (); 85 aspectNameModel = new javax.swing.text.DefaultStyledDocument (); 86 wbConsole = new ConsoleDocumentModel(); 87 activeTransactionVMs = new Vector (); 88 createdTableModels = new Vector (); 89 90 insertInSelection = new JToggleButton.ToggleButtonModel () ; 91 insertInAllVM = new JToggleButton.ToggleButtonModel () ; 92 selectAspectModel = new JToggleButton.ToggleButtonModel () ; 93 selectCrosscutModel= new JToggleButton.ToggleButtonModel () ; 94 selectJoinpointModel = new JToggleButton.ToggleButtonModel (); 95 groupByAspectModel = new JToggleButton.ToggleButtonModel (); 96 groupByCrosscutModel = new JToggleButton.ToggleButtonModel (); 97 groupByJoinpointModel = new JToggleButton.ToggleButtonModel (); 98 99 } 100 101 102 public WorksheetSingleClientModel createTableModel(String pname, String host, String port, boolean pisReal) 103 { 104 WorksheetSingleClientModel mtm = new WorksheetSingleClientModel(this, 105 new Object [][] { }, 106 new String [] { "Aspect", "Crosscut", "JoinPointRequest" }, 107 pname, 108 host,port, 109 pisReal); 110 createdTableModels.add(mtm); 111 return mtm; 112 } 113 114 public List getAllVM() 115 { 116 return createdTableModels; 117 } 118 119 public void removeVM(String name) 120 { 121 java.util.Iterator i = createdTableModels.iterator(); 122 Vector toRemove = new Vector (); 123 while (i.hasNext()) 124 { 125 WorksheetSingleClientModel m = (WorksheetSingleClientModel)i.next(); 126 if (m.getName().equals(name)) 127 toRemove.add(m); 128 } 129 createdTableModels.removeAll(toRemove); 130 } 131 132 public ConsoleDocumentModel getConsole() 133 { 134 return wbConsole; 135 } 136 137 public void setActualTableModel(WorksheetSingleClientModel atm) 138 { 139 actualTableModel = atm; 140 loadTableSettings(actualTableModel.getSelect(), actualTableModel.getGroupBy()); 141 } 142 143 public WorksheetSingleClientModel getActualTableModel() throws IllegalUserInputException 144 { 145 if (actualTableModel == null) 146 throw new IllegalUserInputException("No VM available; Please add a VM"); 147 return actualTableModel; 148 } 149 150 public List getCopySelection() 151 { 152 return copyList; 153 } 154 155 public int getCopyColumn() 156 { 157 return copyColumn; 158 } 159 160 public void setCopySelection(int column, List copyList) 161 { 162 copyColumn = column; 163 this.copyList = new Vector (new HashSet (copyList)); 165 } 166 167 168 public Document getAspectClassNameModel() 169 { 170 return aspectNameModel; 171 } 172 173 public Document getClasspathModel() 174 { 175 return classPathModel; 176 } 177 178 public String getAspectClassName() 179 { 180 try 181 { 182 return getAspectClassNameModel().getText(0,getAspectClassNameModel().getLength()); 183 } 184 catch (javax.swing.text.BadLocationException e) 185 { 186 throw new Error (e.toString()); 187 } 188 } 189 190 public String getClasspath() 191 { 192 try 193 { 194 return getClasspathModel().getText(0,getClasspathModel().getLength()); 195 } 196 catch (javax.swing.text.BadLocationException e) 197 { 198 throw new Error (e.toString()); 199 } 200 } 201 202 public void appendToClasspath(String toBeDo) 203 { 204 try 205 { 206 getClasspathModel().insertString(getClasspathModel().getLength(),System.getProperty("path.separator",":") + toBeDo,null); 207 } 208 catch (javax.swing.text.BadLocationException e) 209 { 210 throw new Error (e.toString()); 211 } 212 213 214 } 216 217 public void cutSelection() throws java.rmi.RemoteException ,TableSelectionException,WithdrawAspectsException,IllegalUserInputException 218 { 219 addVMToActiveTransaction(getActualTableModel()); 220 copySelection(); 221 getActualTableModel().cutAspects(); 222 } 223 224 225 public void copySelection() throws TableSelectionException,java.rmi.RemoteException ,IllegalUserInputException 226 { 227 int column = getActualTableModel().getSelectedColumn(); 228 if ((column != JWorksheetProseClientPane.JOINPOINT_COL_INDEX) && (column != JWorksheetProseClientPane.ASPECT_COL_INDEX)) 229 throw new TableSelectionException("Cut and copy only on 'Aspects' or 'Joinpoints'"); 230 231 List copyList = new Vector (); 232 for (int i=0; i < getActualTableModel().selectedCells.length; i++) 233 { 234 Object value = getActualTableModel().getValueAt(getActualTableModel().selectedCells[i], column); 235 if (value == null) 236 throw new TableSelectionException("Cut and copy of 'null' values is not permitted."); 237 copyList.add(value); 238 } 239 setCopySelection(column, copyList); 240 } 241 242 public void pasteSelection() throws TableSelectionException,java.rmi.RemoteException ,IllegalUserInputException,PasteAspectsException 243 { 244 if (getCopySelection() == null) 245 throw new TableSelectionException("Nothing to paste!"); 246 247 248 249 250 getActualTableModel().setSelectedColumn(getCopyColumn()); 251 if (getCopyColumn() == JWorksheetProseClientPane.JOINPOINT_COL_INDEX) 252 getActualTableModel().pasteJoinpoints(getCopySelection()); 253 else if (getCopyColumn() == JWorksheetProseClientPane.ASPECT_COL_INDEX) 254 { 255 addVMToActiveTransaction(getActualTableModel()); 256 getActualTableModel().pasteAspects(getCopySelection()); 257 } 258 else 259 throw new Error ("Error: wrong copy action"); 260 } 261 262 public void insertAspect() throws IllegalUserInputException,PasteAspectsException,java.rmi.RemoteException ,TableSelectionException 263 { 264 265 if (getAspectClassName() == null || getAspectClassName().trim().equals("")) 266 throw new IllegalUserInputException("Aspect class name not specified"); 267 List aspectList = new Vector (); 268 aspectList.add(new AspectSurrogate(getAspectClassName(),Aspect.generateUniqueAssociation())); 269 270 addVMToActiveTransaction(getActualTableModel()); 271 getActualTableModel().pasteAspects(aspectList); 272 } 273 274 275 276 public void startTransaction() throws IllegalStateException 277 { 278 if (txId != null) 279 throw new IllegalStateException ("Transaction still active. Commit/abort first"); 280 281 txId=""+System.currentTimeMillis() + txCnt--; 282 } 283 284 public String getActiveTransactionID() 285 { 286 return txId; 287 } 288 289 public void commitTransaction(boolean force) throws Exception 290 { 291 TransactionFinishException exceptionList = new TransactionFinishException(); 292 if (txId == null) 293 throw new IllegalUserInputException("Nothing to commit"); 294 java.util.Iterator i = activeTransactionVMs.iterator(); 295 while(i.hasNext()) 296 { 297 try 298 { 299 WorksheetSingleClientModel qClient = (WorksheetSingleClientModel)(i.next()); 300 qClient.commit(txId); 301 } 302 catch (Exception e) 303 { 304 if (force) 305 exceptionList.add(e); 306 else 307 throw e; 308 } 309 } 310 txId = null; 311 activeTransactionVMs.clear(); 312 } 313 314 public void abortTransaction(boolean force) throws Exception 315 { 316 TransactionFinishException exceptionList = new TransactionFinishException(); 317 if (txId == null) 318 throw new IllegalUserInputException("Nothing to abort"); 319 java.util.Iterator i = activeTransactionVMs.iterator(); 320 while(i.hasNext()) 321 { 322 try 323 { 324 WorksheetSingleClientModel qClient = (WorksheetSingleClientModel)(i.next()); 325 qClient.abort(txId); 326 } 327 catch (Exception e) 328 { 329 if (force) 330 exceptionList.add(e); 331 else 332 throw e; 333 } 334 } 335 336 txId = null; 337 activeTransactionVMs.clear(); 338 } 339 340 341 public void addVMToActiveTransaction(WorksheetSingleClientModel n) 342 { 343 if (getActiveTransactionID() == null) 344 return; 345 activeTransactionVMs.add(n); 346 } 347 348 protected void loadTableSettings(int select, int groupBy) 349 { 350 selectAspectModel.setSelected((select & WorksheetSingleClientModel.SELECT_ASPECT) > 0); 351 selectCrosscutModel.setSelected((select & WorksheetSingleClientModel.SELECT_CROSSCUT) > 0); 352 selectJoinpointModel.setSelected((select & WorksheetSingleClientModel.SELECT_JOINPOINT) > 0); 353 switch (groupBy) { 354 case WorksheetSingleClientModel.BY_ASPECT: groupByAspectModel.setSelected(true); break; 355 case WorksheetSingleClientModel.BY_CROSSCUT: groupByCrosscutModel.setSelected(true); break; 356 case WorksheetSingleClientModel.BY_JOINPOINT: groupByJoinpointModel.setSelected(true); break; 357 default: throw new IndexOutOfBoundsException ("BUG: 'GroupBy' value is out of Range: " + groupBy); 358 } 359 } 360 361 362 363 public JToggleButton.ToggleButtonModel getInsertInSelection() 364 { 365 return insertInSelection; 366 } 367 368 public JToggleButton.ToggleButtonModel getInsertInAllVM() 369 { 370 return insertInAllVM; 371 } 372 373 public JToggleButton.ToggleButtonModel getSelectAspect() 374 { 375 return selectAspectModel; 376 } 377 378 public JToggleButton.ToggleButtonModel getSelectCrosscut() 379 { 380 return selectCrosscutModel; 381 } 382 383 public JToggleButton.ToggleButtonModel getSelectJoinpoint() 384 { 385 return selectJoinpointModel; 386 } 387 388 public JToggleButton.ToggleButtonModel getGroupByAspect() 389 { 390 return groupByAspectModel; 391 } 392 public JToggleButton.ToggleButtonModel getGroupByCrosscut() 393 { 394 return groupByCrosscutModel; 395 } 396 397 public JToggleButton.ToggleButtonModel getGroupByJoinpoint() 398 { 399 return groupByJoinpointModel; 400 } 401 402 403 404 } 405 406 407 | Popular Tags |