Package ch.ethz.prose.tools

Source Code of ch.ethz.prose.tools.MultipleClientModel

//
//  This file is part of the prose package.
//
//  The contents of this file are subject to the Mozilla Public License
//  Version 1.1 (the "License"); you may not use this file except in
//  compliance with the License. You may obtain a copy of the License at
//  http://www.mozilla.org/MPL/
//
//  Software distributed under the License is distributed on an "AS IS" basis,
//  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
//  for the specific language governing rights and limitations under the
//  License.
//
//  The Original Code is prose.
//
//  The Initial Developer of the Original Code is Andrei Popovici. Portions
//  created by Andrei Popovici are Copyright (C) 2002 Andrei Popovici.
//  All Rights Reserved.
//
//  Contributor(s):
//  $Id: MultipleClientModel.java,v 1.3 2008/11/18 11:43:39 anicoara Exp $
//  =====================================================================
//

package ch.ethz.prose.tools;

// used packages
import java.util.HashSet;
import java.util.List;
import java.util.Vector;
import javax.swing.text.Document;
import ch.ethz.prose.Aspect;
import ch.ethz.prose.query.AspectSurrogate;

import javax.swing.JTable;
import javax.swing.JToggleButton;

/**
* Class MultipleClientModel
*
* @version  $Revision: 1.3 $
* @author  Philippe Schoch
*/
public class MultipleClientModel {

  protected JTable actualTable = null;
  protected WorksheetSingleClientModel actualTableModel = null;
  protected List copyList;
  protected int copyColumn;
  protected String txId = null;
  protected int txCnt = 0;
  protected Document classPathModel;
  protected Document aspectNameModel;
  protected ConsoleDocumentModel wbConsole;

  protected List createdTableModels;
  protected List activeTransactionVMs;
  JToggleButton.ToggleButtonModel selectAspectModel;
  JToggleButton.ToggleButtonModel selectCrosscutModel;
  JToggleButton.ToggleButtonModel selectJoinpointModel;
  JToggleButton.ToggleButtonModel groupByAspectModel;
  JToggleButton.ToggleButtonModel groupByCrosscutModel;
  JToggleButton.ToggleButtonModel groupByJoinpointModel;
  JToggleButton.ToggleButtonModel insertInAllVM;
  JToggleButton.ToggleButtonModel insertInSelection;


  private JMultipleProseClient view;

  public MultipleClientModel(JMultipleProseClient view) {
    this.view = view;
    classPathModel  = new javax.swing.text.DefaultStyledDocument();
    aspectNameModel = new javax.swing.text.DefaultStyledDocument();
    wbConsole = new ConsoleDocumentModel();
    activeTransactionVMs = new Vector();
    createdTableModels = new Vector();

    insertInSelection = new JToggleButton.ToggleButtonModel() ;
    insertInAllVM     = new JToggleButton.ToggleButtonModel() ;
    selectAspectModel = new JToggleButton.ToggleButtonModel() ;
    selectCrosscutModel= new JToggleButton.ToggleButtonModel() ;
    selectJoinpointModel = new JToggleButton.ToggleButtonModel();
    groupByAspectModel = new JToggleButton.ToggleButtonModel();
    groupByCrosscutModel = new JToggleButton.ToggleButtonModel();
    groupByJoinpointModel = new JToggleButton.ToggleButtonModel();

  }

  public WorksheetSingleClientModel createTableModel(String pname, String host, String port, boolean pisReal) {
    WorksheetSingleClientModel mtm = new WorksheetSingleClientModel(this,
        new Object[][] { },
        new String [] { "Aspect", "Crosscut", "JoinPointRequest" },
        pname,
        host,port,
        pisReal);
    createdTableModels.add(mtm);
    return mtm;
  }

  public List getAllVM() {
    return createdTableModels;
  }

  public void removeVM(String name) {
    java.util.Iterator i = createdTableModels.iterator();
    Vector toRemove = new Vector();
    while (i.hasNext()) {
      WorksheetSingleClientModel m = (WorksheetSingleClientModel)i.next();
      if (m.getName().equals(name))
        toRemove.add(m);
    }
    createdTableModels.removeAll(toRemove);
  }

  public ConsoleDocumentModel getConsole() {
    return wbConsole;
  }

  public void setActualTableModel(WorksheetSingleClientModel atm) {
    actualTableModel = atm;
    loadTableSettings(actualTableModel.getSelect(), actualTableModel.getGroupBy());
  }

  public WorksheetSingleClientModel getActualTableModel() throws IllegalUserInputException {
    if (actualTableModel == null)
      throw new IllegalUserInputException("No VM available; Please add a VM");
    return actualTableModel;
  }

  public List getCopySelection() {
    return copyList;
  }

  public int getCopyColumn() {
    return copyColumn;
  }

  public void setCopySelection(int column, List copyList) {
    copyColumn = column;
    this.copyList = new Vector(new HashSet(copyList))// for elimination Duplicates

  }

  public Document getAspectClassNameModel() {
    return aspectNameModel;
  }

  public Document getClasspathModel() {
    return classPathModel;
  }

  public String getAspectClassName() {
    try {
      return getAspectClassNameModel().getText(0,getAspectClassNameModel().getLength());
    }
    catch (javax.swing.text.BadLocationException e) {
      throw new Error(e.toString());
    }
  }

  public String getClasspath() {
    try {
      return getClasspathModel().getText(0,getClasspathModel().getLength());
    }
    catch (javax.swing.text.BadLocationException e) {
      throw new Error(e.toString());
    }
  }

  public void appendToClasspath(String toBeDo) {
    try {
      getClasspathModel().insertString(getClasspathModel().getLength(),System.getProperty("path.separator",":") + toBeDo,null);
    }
    catch (javax.swing.text.BadLocationException e) {
      throw new Error(e.toString());
    }


    //GEN-LAST:event_classTextFieldActionPerformed
  }

  public void cutSelection() throws java.rmi.RemoteException,TableSelectionException,WithdrawAspectsException,IllegalUserInputException {
    addVMToActiveTransaction(getActualTableModel());
    copySelection();
    getActualTableModel().cutAspects();
  }


  public void copySelection() throws TableSelectionException,java.rmi.RemoteException,IllegalUserInputException {
    int column = getActualTableModel().getSelectedColumn();
    if ((column != JWorksheetProseClientPane.JOINPOINT_COL_INDEX) && (column != JWorksheetProseClientPane.ASPECT_COL_INDEX))
      throw new TableSelectionException("Cut and copy only on  'Aspects' or 'Joinpoints'");

    List copyList = new Vector();
    for (int i=0; i < getActualTableModel().selectedCells.length; i++) {
      Object value = getActualTableModel().getValueAt(getActualTableModel().selectedCells[i],  column);
      if (value == null)
        throw new TableSelectionException("Cut and copy of 'null' values is not permitted.");
      copyList.add(value);
    }
    setCopySelection(column, copyList);
  }

  public void pasteSelection() throws TableSelectionException,java.rmi.RemoteException,IllegalUserInputException,PasteAspectsException {
    if (getCopySelection() == null)
      throw new TableSelectionException("Nothing to paste!");

    getActualTableModel().setSelectedColumn(getCopyColumn());
    if (getCopyColumn() == JWorksheetProseClientPane.JOINPOINT_COL_INDEX)
      getActualTableModel().pasteJoinpoints(getCopySelection());
    else if (getCopyColumn() == JWorksheetProseClientPane.ASPECT_COL_INDEX) {
      addVMToActiveTransaction(getActualTableModel());
      getActualTableModel().pasteAspects(getCopySelection());
    }
    else
      throw new Error("Error: wrong copy action");
  }

  public void insertAspect() throws IllegalUserInputException,PasteAspectsException,java.rmi.RemoteException,TableSelectionException {
    if (getAspectClassName() == null || getAspectClassName().trim().equals(""))
      throw new IllegalUserInputException("Aspect class name not specified");
    List aspectList = new Vector();
    aspectList.add(new AspectSurrogate(getAspectClassName(),Aspect.generateUniqueAssociation()));

    addVMToActiveTransaction(getActualTableModel());
    getActualTableModel().pasteAspects(aspectList);
  }

  public void startTransaction() throws IllegalStateException {
    if (txId != null)
      throw new IllegalStateException("Transaction still active. Commit/abort first");

    txId=""+System.currentTimeMillis() + txCnt--;
  }

  public String getActiveTransactionID() {
    return txId;
  }

  public void commitTransaction(boolean force) throws Exception {
    TransactionFinishException exceptionList = new TransactionFinishException();
    if (txId == null)
      throw new IllegalUserInputException("Nothing to commit");
    java.util.Iterator i = activeTransactionVMs.iterator();
    while(i.hasNext()) {
      try {
        WorksheetSingleClientModel qClient = (WorksheetSingleClientModel)(i.next());
        qClient.commit(txId);
      }
      catch (Exception e) {
        if (force)
          exceptionList.add(e);
        else
          throw e;
      }
    }
    txId = null;
    activeTransactionVMs.clear();
  }

  public void abortTransaction(boolean force) throws Exception {
    TransactionFinishException exceptionList = new TransactionFinishException();
    if (txId == null)
      throw new IllegalUserInputException("Nothing to abort");
    java.util.Iterator i = activeTransactionVMs.iterator();
    while(i.hasNext()) {
      try {
        WorksheetSingleClientModel qClient = (WorksheetSingleClientModel)(i.next());
        qClient.abort(txId);
      }
      catch (Exception e) {
        if (force)
          exceptionList.add(e);
        else
          throw e;
      }
    }

    txId = null;
    activeTransactionVMs.clear();
  }


  public void addVMToActiveTransaction(WorksheetSingleClientModel n) {
    if (getActiveTransactionID() == null)
      return;
    activeTransactionVMs.add(n);
  }

  protected void loadTableSettings(int select, int groupBy) {
    selectAspectModel.setSelected((select & WorksheetSingleClientModel.SELECT_ASPECT) > 0);
    selectCrosscutModel.setSelected((select & WorksheetSingleClientModel.SELECT_CROSSCUT) > 0);
    selectJoinpointModel.setSelected((select & WorksheetSingleClientModel.SELECT_JOINPOINT) > 0);
    switch (groupBy) {
    case WorksheetSingleClientModel.BY_ASPECT:    groupByAspectModel.setSelected(true); break;
    case WorksheetSingleClientModel.BY_CROSSCUT:  groupByCrosscutModel.setSelected(true); break;
    case WorksheetSingleClientModel.BY_JOINPOINT: groupByJoinpointModel.setSelected(true); break;
    defaultthrow new IndexOutOfBoundsException("BUG: 'GroupBy' value is out of Range: " + groupBy);
    }
  }



  public JToggleButton.ToggleButtonModel getInsertInSelection() {
    return insertInSelection;
  }

  public JToggleButton.ToggleButtonModel getInsertInAllVM() {
    return insertInAllVM;
  }

  public JToggleButton.ToggleButtonModel getSelectAspect() {
    return selectAspectModel;
  }

  public JToggleButton.ToggleButtonModel getSelectCrosscut() {
    return selectCrosscutModel;
  }

  public JToggleButton.ToggleButtonModel getSelectJoinpoint() {
    return selectJoinpointModel;
  }

  public JToggleButton.ToggleButtonModel getGroupByAspect() {
    return groupByAspectModel;
  }
  public JToggleButton.ToggleButtonModel getGroupByCrosscut() {
    return groupByCrosscutModel;
  }

  public JToggleButton.ToggleButtonModel getGroupByJoinpoint() {
    return groupByJoinpointModel;
  }

}
TOP

Related Classes of ch.ethz.prose.tools.MultipleClientModel

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.