Coverage Report - org.argouml.persistence.PgmlUtility
 
Classes in this File Line Coverage Branch Coverage Complexity
PgmlUtility
0%
0/59
0%
0/32
4.286
 
 1  
 /* $Id: PgmlUtility.java 17832 2010-01-12 19:02:29Z 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  
  *    mvw
 11  
  *****************************************************************************
 12  
  *
 13  
  * Some portions of this file was previously release using the BSD License:
 14  
  */
 15  
 
 16  
 // Copyright (c) 2005-2007 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.persistence;
 40  
 
 41  
 import java.util.ArrayList;
 42  
 import java.util.Collection;
 43  
 import java.util.Iterator;
 44  
 import java.util.List;
 45  
 
 46  
 import org.argouml.uml.diagram.static_structure.ui.FigEdgeNote;
 47  
 import org.argouml.uml.diagram.ui.FigEdgeAssociationClass;
 48  
 import org.tigris.gef.base.Diagram;
 49  
 import org.tigris.gef.base.Layer;
 50  
 import org.tigris.gef.presentation.Fig;
 51  
 import org.tigris.gef.presentation.FigEdge;
 52  
 import org.tigris.gef.presentation.FigGroup;
 53  
 
 54  
 /**
 55  
  * Utility class for use by pgml.tee.
 56  
  *
 57  
  * @author Bob Tarling
 58  
  */
 59  
 public final class PgmlUtility {
 60  
 
 61  
     /**
 62  
      * Constructor.
 63  
      */
 64  0
     private PgmlUtility() {
 65  0
     }
 66  
     
 67  
     /**
 68  
      * Translate the visibility flag of a Fig to the PGML "visibility" attribute
 69  
      * value.
 70  
      * The PGML values are 0=hidden and 1=shown.
 71  
      * If not specified then 1 is the default so we return null for this to
 72  
      * prevent redundent data being written to PGML.
 73  
      * TODO: Remove on GEF release after 0.11.9 as it will be provided there.
 74  
      * 
 75  
      * @param f The Fig
 76  
      * @return "0"=hidden, null=shown
 77  
      */
 78  
     public static String getVisibility(Fig f) {
 79  0
         if (f.isVisible()) return null;
 80  0
         return "0";
 81  
     }
 82  
     
 83  
     /**
 84  
      * Return just the comment edges for a specific layer.
 85  
      * TODO: Document: Diagram / layer?
 86  
      *
 87  
      * @param diagram The diagram.
 88  
      * @return a {@link List} with the edges.
 89  
      */
 90  
     public static List getEdges(Diagram diagram) {
 91  0
         Layer lay = diagram.getLayer();
 92  0
         Collection edges = lay.getContentsEdgesOnly();
 93  0
         List returnEdges = new ArrayList(edges.size());
 94  0
         getEdges(diagram, edges, returnEdges);
 95  0
         return returnEdges;
 96  
     }
 97  
 
 98  
     /**
 99  
      * Return the diagram contents in the order to save to PGML
 100  
      * Nodes first, then edges connecting nodes and lastly
 101  
      * edges that connect edges to other edges.
 102  
      *
 103  
      * @param diagram The {@link Diagram}.
 104  
      * @return a {@link List} with the contents.
 105  
      */
 106  
     public static List getContents(Diagram diagram) {
 107  0
         Layer lay = diagram.getLayer();
 108  0
         List contents = lay.getContents();
 109  0
         int size = contents.size();
 110  0
         List list = new ArrayList(size);
 111  0
         for (int i = 0; i < size; i++) {
 112  0
             Object o = contents.get(i);
 113  0
             if (!(o instanceof FigEdge)) {
 114  0
                 list.add(o);
 115  
             }
 116  
         }
 117  0
         getEdges(diagram, lay.getContentsEdgesOnly(), list);
 118  0
         return list;
 119  
     }
 120  
     
 121  
     private static void getEdges(Diagram diagram, 
 122  
             Collection edges, List returnEdges) {
 123  0
         Iterator it = edges.iterator();
 124  0
         while (it.hasNext()) {
 125  0
             Object o = it.next();
 126  0
             if (!(o instanceof FigEdgeNote) 
 127  
                     && !(o instanceof FigEdgeAssociationClass)) {
 128  0
                 returnEdges.add(o);
 129  
             }
 130  0
         }
 131  0
         it = edges.iterator();
 132  0
         while (it.hasNext()) {
 133  0
             Object o = it.next();
 134  0
             if (o instanceof FigEdgeAssociationClass) {
 135  0
                 returnEdges.add(o);
 136  
             }
 137  0
         }
 138  0
         it = edges.iterator();
 139  0
         while (it.hasNext()) {
 140  0
             Object o = it.next();
 141  0
             if (o instanceof FigEdgeNote) {
 142  0
                 returnEdges.add(o);
 143  
             }
 144  0
         }
 145  0
     }
 146  
 
 147  
     /**
 148  
      * Return the identifier for this Fig which is the encloser 
 149  
      * of the given Fig
 150  
      * @param f the Fig to generate the id for
 151  
      * @return a unique string
 152  
      */
 153  
     public static String getEnclosingId(Fig f) {
 154  
         
 155  0
         Fig encloser = f.getEnclosingFig();
 156  
         
 157  0
         if (encloser == null) {
 158  0
             return null;
 159  
         }
 160  
         
 161  0
         return getId(encloser);
 162  
     }
 163  
 
 164  
 
 165  
     /**
 166  
      * Generate an identifier for this Fig which is unique within the 
 167  
      * diagram.
 168  
      * @param f the Fig to generate the id for
 169  
      * @return a unique string
 170  
      */
 171  
     public static String getId(Fig f) {
 172  0
         if (f == null) {
 173  0
             throw new IllegalArgumentException("A fig must be supplied");
 174  
         }
 175  0
         if (f.getGroup() != null) {
 176  0
             String groupId = f.getGroup().getId();
 177  0
             if (f.getGroup() instanceof FigGroup) {
 178  0
                 FigGroup group = (FigGroup) f.getGroup();
 179  0
                 return groupId + "." + (group.getFigs()).indexOf(f);
 180  0
             } else if (f.getGroup() instanceof FigEdge) {
 181  0
                 FigEdge edge = (FigEdge) f.getGroup();
 182  0
                 return groupId + "."
 183  
                         + (((List) edge.getPathItemFigs()).indexOf(f) + 1);
 184  
             } else {
 185  0
                 return groupId + ".0";
 186  
             }
 187  
         }
 188  
 
 189  0
         Layer layer = f.getLayer();
 190  0
         if (layer == null) {
 191  0
             return "LAYER_NULL";
 192  
         }
 193  
 
 194  0
         List c = layer.getContents();
 195  0
         int index = c.indexOf(f);
 196  0
         return "Fig" + index;
 197  
     }
 198  
 
 199  
 }