Coverage Report - org.argouml.uml.diagram.use_case.ui.UseCaseDiagramRenderer
 
Classes in this File Line Coverage Branch Coverage Complexity
UseCaseDiagramRenderer
3%
2/64
0%
0/24
7
 
 1  
 /* $Id: UseCaseDiagramRenderer.java 17867 2010-01-12 20:47:04Z linus $
 2  
  *****************************************************************************
 3  
  * Copyright (c) 2009 Contributors - see below
 4  
  * All rights reserved. This program and the accompanying materials
 5  
  * are made available under the terms of the Eclipse Public License v1.0
 6  
  * which accompanies this distribution, and is available at
 7  
  * http://www.eclipse.org/legal/epl-v10.html
 8  
  *
 9  
  * Contributors:
 10  
  *    bobtarling
 11  
  *****************************************************************************
 12  
  *
 13  
  * Some portions of this file was previously release using the BSD License:
 14  
  */
 15  
 
 16  
 // Copyright (c) 1996-2008 The Regents of the University of California. All
 17  
 // Rights Reserved. Permission to use, copy, modify, and distribute this
 18  
 // software and its documentation without fee, and without a written
 19  
 // agreement is hereby granted, provided that the above copyright notice
 20  
 // and this paragraph appear in all copies.  This software program and
 21  
 // documentation are copyrighted by The Regents of the University of
 22  
 // California. The software program and documentation are supplied "AS
 23  
 // IS", without any accompanying services from The Regents. The Regents
 24  
 // does not warrant that the operation of the program will be
 25  
 // uninterrupted or error-free. The end-user understands that the program
 26  
 // was developed for research purposes and is advised not to rely
 27  
 // exclusively on the program for any reason.  IN NO EVENT SHALL THE
 28  
 // UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
 29  
 // SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS,
 30  
 // ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
 31  
 // THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
 32  
 // SUCH DAMAGE. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY
 33  
 // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 34  
 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
 35  
 // PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
 36  
 // CALIFORNIA HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT,
 37  
 // UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
 38  
 
 39  
 package org.argouml.uml.diagram.use_case.ui;
 40  
 
 41  
 import java.util.Map;
 42  
 
 43  
 import org.apache.log4j.Logger;
 44  
 import org.argouml.model.Model;
 45  
 import org.argouml.uml.CommentEdge;
 46  
 import org.argouml.uml.diagram.ArgoDiagram;
 47  
 import org.argouml.uml.diagram.DiagramEdgeSettings;
 48  
 import org.argouml.uml.diagram.DiagramSettings;
 49  
 import org.argouml.uml.diagram.DiagramUtils;
 50  
 import org.argouml.uml.diagram.GraphChangeAdapter;
 51  
 import org.argouml.uml.diagram.UmlDiagramRenderer;
 52  
 import org.argouml.uml.diagram.static_structure.ui.FigEdgeNote;
 53  
 import org.argouml.uml.diagram.ui.FigAssociation;
 54  
 import org.argouml.uml.diagram.ui.FigDependency;
 55  
 import org.argouml.uml.diagram.ui.FigEdgeModelElement;
 56  
 import org.argouml.uml.diagram.ui.FigGeneralization;
 57  
 import org.argouml.uml.diagram.ui.FigNodeModelElement;
 58  
 import org.argouml.uml.diagram.ui.UMLDiagram;
 59  
 import org.tigris.gef.base.Layer;
 60  
 import org.tigris.gef.base.LayerPerspective;
 61  
 import org.tigris.gef.graph.GraphModel;
 62  
 import org.tigris.gef.presentation.FigEdge;
 63  
 import org.tigris.gef.presentation.FigNode;
 64  
 
 65  
 // could be singleton
 66  
 
 67  
 /**
 68  
  * This class defines a renderer object for UML Use Case Diagrams. In a
 69  
  * Class Diagram the following UML objects are displayed with the
 70  
  * following Figs:<p>
 71  
  *
 72  
  * <pre>
 73  
  *   UML Object       ---  Fig
 74  
  *   ---------------------------------------
 75  
  *   MActor           ---  FigActor
 76  
  *   MUseCase         ---  FigUseCase
 77  
  * </pre>
 78  
  *
 79  
  * Provides {@link #getFigNodeFor} to implement the
 80  
  * {@link org.tigris.gef.graph.GraphNodeRenderer} interface and
 81  
  * {@link #getFigEdgeFor} to implement the
 82  
  * {@link org.tigris.gef.graph.GraphEdgeRenderer} interface.<p>
 83  
  *
 84  
  * <em>Note</em>. Should be implemented as a singleton - we don't really
 85  
  * need a separate instance for each use case diagram.<p>
 86  
  *
 87  
  * @author abonner
 88  
  */
 89  1873
 public class UseCaseDiagramRenderer extends UmlDiagramRenderer {
 90  
     
 91  
     static final long serialVersionUID = 2217410137377934879L;
 92  
 
 93  
     /**
 94  
      * Logger.
 95  
      */
 96  900
     private static final Logger LOG =
 97  
         Logger.getLogger(UseCaseDiagramRenderer.class);
 98  
 
 99  
 
 100  
     /**
 101  
      * Return a Fig that can be used to represent the given node.<p>
 102  
      *
 103  
      * @param gm    The graph model for which we are rendering.
 104  
      *
 105  
      * @param lay   The layer in the graph on which we want this figure.
 106  
      *
 107  
      * @param node  The node to be rendered (an model element object)
 108  
      *
 109  
      * @param styleAttributes an optional map of attributes to style the fig
 110  
      *
 111  
      * @return      The fig to be used, or <code>null</code> if we can't create
 112  
      *              one.
 113  
      */
 114  
     public FigNode getFigNodeFor(GraphModel gm, Layer lay, Object node,
 115  
             Map styleAttributes) {
 116  
 
 117  0
         FigNodeModelElement figNode = null;
 118  
 
 119  
         // Create a new version of the relevant fig
 120  
 
 121  0
         ArgoDiagram diag = DiagramUtils.getActiveDiagram();
 122  0
         if (diag instanceof UMLDiagram
 123  
             && ((UMLDiagram) diag).doesAccept(node)) {
 124  0
             figNode = 
 125  
                 (FigNodeModelElement) ((UMLDiagram) diag).drop(node, null);
 126  
 
 127  
         } else {
 128  0
             LOG.debug(this.getClass().toString()
 129  
                   + ": getFigNodeFor(" + gm.toString() + ", "
 130  
                   + lay.toString() + ", " + node.toString()
 131  
                   + ") - cannot create this sort of node.");
 132  0
             return null;
 133  
             // TODO: Shouldn't we throw an exception here?!?!
 134  
         }
 135  
 
 136  0
         lay.add(figNode);
 137  0
         figNode.setDiElement(
 138  
                 GraphChangeAdapter.getInstance().createElement(gm, node));
 139  
 
 140  0
         return figNode;
 141  
     }
 142  
 
 143  
 
 144  
     /**
 145  
      * Return a Fig that can be used to represent the given edge.<p>
 146  
      *
 147  
      * Generally the same code as for the ClassDiagram, since it's very
 148  
      * related to it. Deal with each of the edge types in turn.<p>
 149  
      *
 150  
      * @param gm    The graph model for which we are rendering.
 151  
      *
 152  
      * @param lay   The layer in the graph on which we want this figure.
 153  
      *
 154  
      * @param edge  The edge to be rendered (an model element object)
 155  
      *
 156  
      * @param styleAttributes an optional map of attributes to style the fig
 157  
      *
 158  
      * @return      The fig to be used, or <code>null</code> if we can't create
 159  
      *              one.
 160  
      *
 161  
      * @see org.tigris.gef.graph.GraphEdgeRenderer#getFigEdgeFor(
 162  
      *         org.tigris.gef.graph.GraphModel, org.tigris.gef.base.Layer,
 163  
      *         java.lang.Object, java.util.Map)
 164  
      */
 165  
     public FigEdge getFigEdgeFor(GraphModel gm, Layer lay, Object edge,
 166  
             Map styleAttributes) {
 167  
 
 168  0
         if (LOG.isDebugEnabled()) {
 169  0
             LOG.debug("making figedge for " + edge);
 170  
         }
 171  
 
 172  0
         if (edge == null) {
 173  0
             throw new IllegalArgumentException("A model edge must be supplied");
 174  
         }
 175  
 
 176  0
         assert lay instanceof LayerPerspective;
 177  0
         ArgoDiagram diag = (ArgoDiagram) ((LayerPerspective) lay).getDiagram();
 178  0
         DiagramSettings settings = diag.getDiagramSettings();
 179  
         
 180  0
         FigEdge newEdge = null;
 181  
 
 182  0
         if (Model.getFacade().isAAssociation(edge)) {
 183  0
             final Object[] associationEnds = 
 184  
                 Model.getFacade().getConnections(edge).toArray();
 185  0
             newEdge = new FigAssociation(
 186  
                     new DiagramEdgeSettings(
 187  
                             edge, 
 188  
                             associationEnds[0], 
 189  
                             associationEnds[1]), 
 190  
                             settings);
 191  0
             final FigNode sourceFig =
 192  
                 getFigNodeForAssociationEnd(diag, associationEnds[0]);
 193  0
             final FigNode destFig =
 194  
                 getFigNodeForAssociationEnd(diag, associationEnds[1]);
 195  0
             newEdge.setSourceFigNode(sourceFig);
 196  0
             newEdge.setSourcePortFig(sourceFig);
 197  0
             newEdge.setDestFigNode(destFig);
 198  0
             newEdge.setDestPortFig(destFig);
 199  0
         } else if (Model.getFacade().isAGeneralization(edge)) {
 200  0
             newEdge = new FigGeneralization(edge, settings);
 201  0
         } else if (Model.getFacade().isAExtend(edge)) {
 202  0
             newEdge = new FigExtend(edge, settings);
 203  
 
 204  
             // The nodes at the two ends
 205  0
             Object base = Model.getFacade().getBase(edge);
 206  0
             Object extension = Model.getFacade().getExtension(edge);
 207  
 
 208  
             // The figs for the two end nodes
 209  0
             FigNode baseFN = (FigNode) lay.presentationFor(base);
 210  0
             FigNode extensionFN = (FigNode) lay.presentationFor(extension);
 211  
 
 212  
             // Link the new extend relationship in to the ends. Remember we
 213  
             // draw from the extension use case to the base use case.
 214  0
             newEdge.setSourcePortFig(extensionFN);
 215  0
             newEdge.setSourceFigNode(extensionFN);
 216  
 
 217  0
             newEdge.setDestPortFig(baseFN);
 218  0
             newEdge.setDestFigNode(baseFN);
 219  
 
 220  0
         } else if (Model.getFacade().isAInclude(edge)) {
 221  0
             newEdge = new FigInclude(edge, settings);
 222  
 
 223  0
             Object base = Model.getFacade().getBase(edge);
 224  0
             Object addition = Model.getFacade().getAddition(edge);
 225  
 
 226  
             // The figs for the two end nodes
 227  0
             FigNode baseFN = (FigNode) lay.presentationFor(base);
 228  0
             FigNode additionFN = (FigNode) lay.presentationFor(addition);
 229  
 
 230  
             // Link the new include relationship in to the ends
 231  0
             newEdge.setSourcePortFig(baseFN);
 232  0
             newEdge.setSourceFigNode(baseFN);
 233  
 
 234  0
             newEdge.setDestPortFig(additionFN);
 235  0
             newEdge.setDestFigNode(additionFN);
 236  0
         } else if (Model.getFacade().isADependency(edge)) {
 237  0
             newEdge = new FigDependency(edge, settings);
 238  
 
 239  
             // Where there is more than one supplier or client, take the first
 240  
             // element in each case. There really ought to be a check that
 241  
             // there are some here for safety.
 242  
 
 243  0
             Object supplier =
 244  
                  ((Model.getFacade().getSuppliers(edge).toArray())[0]);
 245  0
             Object client =
 246  
                  ((Model.getFacade().getClients(edge).toArray())[0]);
 247  
 
 248  
             // The figs for the two end nodes
 249  0
             FigNode supplierFN = (FigNode) lay.presentationFor(supplier);
 250  0
             FigNode clientFN = (FigNode) lay.presentationFor(client);
 251  
 
 252  
             // Link the new dependency in to the ends
 253  0
             newEdge.setSourcePortFig(clientFN);
 254  0
             newEdge.setSourceFigNode(clientFN);
 255  
 
 256  0
             newEdge.setDestPortFig(supplierFN);
 257  0
             newEdge.setDestFigNode(supplierFN);
 258  
 
 259  0
         } else if (edge instanceof CommentEdge) {
 260  0
             newEdge = new FigEdgeNote(edge, settings);
 261  
         }
 262  
 
 263  0
         addEdge(lay, newEdge, edge);
 264  0
         return newEdge;
 265  
     }
 266  
 
 267  
 }