KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ch > ethz > prose > tools > WorksheetSingleClientModel


1 //
2
// This file is part of the prose package.
3
//
4
// The contents of this file are subject to the Mozilla Public License
5
// Version 1.1 (the "License"); you may not use this file except in
6
// compliance with the License. You may obtain a copy of the License at
7
// http://www.mozilla.org/MPL/
8
//
9
// Software distributed under the License is distributed on an "AS IS" basis,
10
// WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11
// for the specific language governing rights and limitations under the
12
// License.
13
//
14
// The Original Code is prose.
15
//
16
// The Initial Developer of the Original Code is Andrei Popovici. Portions
17
// created by Andrei Popovici are Copyright (C) 2002 Andrei Popovici.
18
// All Rights Reserved.
19
//
20
// Contributor(s):
21
/*
22  * WorksheetSingleClientModel.java
23  *
24  * Created on December 12, 2002, 10:06 AM
25  */

26
27 package ch.ethz.prose.tools;
28
29 import java.net.MalformedURLException JavaDoc;
30 import java.rmi.NotBoundException JavaDoc;
31 import java.rmi.RemoteException JavaDoc;
32 import java.util.Arrays JavaDoc;
33 import java.util.Iterator JavaDoc;
34 import java.util.List JavaDoc;
35 import java.util.Vector JavaDoc;
36 import java.util.HashSet JavaDoc;
37
38 import javax.swing.event.TableModelEvent JavaDoc;
39 import javax.swing.table.DefaultTableModel JavaDoc;
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 /**
51  *
52  * @author pschoch
53  */

54 public class WorksheetSingleClientModel extends DefaultTableModel JavaDoc {
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; // minimum number of rows displayed
64
private static Vector JavaDoc nullVector = new Vector JavaDoc();
65
66   protected List JavaDoc table = new Vector JavaDoc(); // query result of prose
67
protected Vector JavaDoc data = new Vector JavaDoc(); // the query result in a form so it is readable for a swing table
68
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; // used for storing the cells that get colored
73
protected RemoteAspectManager exMngr;
74   protected String JavaDoc address;
75   protected boolean isReal;
76   protected MultipleClientModel parentModel;
77   protected String JavaDoc name;
78   protected String JavaDoc host;
79   protected String JavaDoc port;
80
81   private boolean addedFlag = false; // so the extra empty cell at the bottom of the table is inserted only when required
82

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   /** Creates a new instance of WorksheetSingleClientModel */
94   public WorksheetSingleClientModel(MultipleClientModel ownerModel, Object JavaDoc[][] initData, String JavaDoc[] initColumnNames, String JavaDoc name, String JavaDoc host,String JavaDoc 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 JavaDoc dataElement = new Vector JavaDoc();
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 JavaDoc getName()
118     {
119       return name;
120     }
121
122   public String JavaDoc getHost()
123     {
124       return host;
125     }
126
127   public String JavaDoc getPort()
128     {
129       return port;
130     }
131
132   public String JavaDoc 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 JavaDoc,IllegalUserInputException
197     {
198       checkIsConnected();
199          table = new Vector JavaDoc();
200          Iterator JavaDoc 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   // NOTE: it will be better to have a function in the Aspect Manager Interface e.g. 'getAllJoinpoints'
210
// that provides all joinpoints as JoinPointRequestSurrogate's. But this will not been implemented
211
// at the moment as discussed with Andrei. So it is choosed another way to come to a solution...
212
public synchronized void allJoinpoints() throws RemoteException JavaDoc,IllegalUserInputException
213     {
214       checkIsConnected();
215
216       table = new Vector JavaDoc();
217       Iterator JavaDoc i = ((RemoteAspectManager)exMngr).allJoinpoints().iterator();
218       while(i.hasNext())
219           table.add(new Tuple(null, null, (JoinPointRequestSurrogate)i.next()));
220 // table = exMngr.queryAspects(exMngr.getAllAspects(), SELECT_JOINPOINT, BY_JOINPOINT);
221
convertDataForGUI();
222       updateTable(data);
223     }
224
225
226   public synchronized void query() throws RemoteException JavaDoc, IllegalUserInputException
227     {
228       checkIsConnected();
229       try{
230       if (selectedColumn == -1)
231         {
232           throw new RuntimeException JavaDoc("BUG: multiple selection over columns not allowed");
233         }
234
235       List JavaDoc inputList = new Vector JavaDoc();
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 JavaDoc("Row selection failed! [rowNumber: " + selectedColumn + "]");
255
256       convertDataForGUI();
257       updateTable(data);
258       }
259       catch (RemoteException JavaDoc 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 JavaDoc 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 JavaDoc dataElement = new Vector JavaDoc();
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 // basicIndex + i < data.size() -1
324
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); // actually not needed anymore since it is sorted already
337

338       updateTable(data);
339     }
340
341
342   // this is a list of aspect surrogates
343
protected synchronized void pasteAspects(List JavaDoc pasteList) throws RemoteException JavaDoc, 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 JavaDoc errorList = new Vector JavaDoc();
362       List JavaDoc 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 JavaDoc[] 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 JavaDoc p = Runtime.getRuntime().exec(execArgs);
383           parentModel.getConsole().useProcessOutput(p);
384           try { p.waitFor();} catch (InterruptedException JavaDoc e){}
385           if (p.exitValue() != 0)
386         throw new RuntimeException JavaDoc("Cannot insert aspect");
387
388          }
389       catch (Exception JavaDoc 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 JavaDoc dataElement = new Vector JavaDoc();
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 // basicIndex + j < data.size() -1
415
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 JavaDoc,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       // find out what aspects to cut
435
HashSet JavaDoc myRememberList = new HashSet JavaDoc();
436       for (int i=0; i < selectedCells.length; i++)
437         {
438           AspectSurrogate aspect = (AspectSurrogate)((Vector JavaDoc)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       // cut the aspects
445
Iterator JavaDoc 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 JavaDoc e)
457         {
458           maE.add(e);
459         }
460     }
461
462       // update the table
463
for (int i=0; i < table.size(); i++)
464         {
465           AspectSurrogate aspect = (AspectSurrogate)((Vector JavaDoc)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 JavaDoc 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 JavaDoc(this));
492     }
493
494   private Vector JavaDoc createHeaders()
495     {
496       Vector JavaDoc v = new Vector JavaDoc();
497       v.add(new String JavaDoc("Aspect"));
498       v.add(new String JavaDoc("Crosscut"));
499       v.add(new String JavaDoc("Joinpoint"));
500       return v;
501     }
502
503   private void convertDataForGUI()
504     {
505       pastedCells = null; // you can say, that at this point the pastedCells could be reset to null.
506
data = new Vector JavaDoc();
507       if (table == null)
508           return;
509       for (int i=0; i < table.size(); i++)
510         {
511           Vector JavaDoc dataElement = new Vector JavaDoc();
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 JavaDoc,java.rmi.NotBoundException JavaDoc,java.net.MalformedURLException JavaDoc, java.net.UnknownHostException JavaDoc
522     {
523       int intPort;
524       RemoteAspectManager[] rams;
525     try
526       {
527         intPort = Integer.parseInt(port);
528       }
529     catch (NumberFormatException JavaDoc thisIsNotAPortNum)
530       {
531         throw new MalformedURLException JavaDoc("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 JavaDoc e)
542       {
543         throw new RemoteException JavaDoc(e.toString());
544       }
545     }
546
547   public synchronized void commit(Object JavaDoc transactionId) throws java.rmi.RemoteException JavaDoc
548     {
549       exMngr.commit(transactionId);
550     }
551
552   public synchronized void abort(Object JavaDoc transactionId) throws java.rmi.RemoteException JavaDoc
553     {
554       exMngr.abort(transactionId);
555     }
556
557   public String JavaDoc toString()
558     {
559       return name;
560     }
561 }
562
Popular Tags