Coverage Report - org.argouml.uml.ui.TabSrc
 
Classes in this File Line Coverage Branch Coverage Complexity
TabSrc
32%
25/78
10%
4/38
2.818
TabSrc$1
N/A
N/A
2.818
TabSrc$DefaultPredicate
100%
2/2
N/A
2.818
 
 1  
 /* $Id: TabSrc.java 18632 2010-08-08 08:21:55Z 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  
  *    euluis
 11  
  *****************************************************************************
 12  
  *
 13  
  * Some portions of this file was previously release using the BSD License:
 14  
  */
 15  
 
 16  
 // Copyright (c) 1996-2006 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.ui;
 40  
 
 41  
 import java.awt.event.ItemEvent;
 42  
 import java.awt.event.ItemListener;
 43  
 import java.util.ArrayList;
 44  
 import java.util.Collection;
 45  
 import java.util.List;
 46  
 
 47  
 import javax.swing.JComboBox;
 48  
 
 49  
 import org.apache.log4j.Logger;
 50  
 import org.argouml.application.api.Predicate;
 51  
 import org.argouml.language.ui.LanguageComboBox;
 52  
 import org.argouml.model.Model;
 53  
 import org.argouml.ui.TabText;
 54  
 import org.argouml.uml.generator.GeneratorHelper;
 55  
 import org.argouml.uml.generator.Language;
 56  
 import org.argouml.uml.generator.SourceUnit;
 57  
 import org.tigris.gef.presentation.Fig;
 58  
 import org.tigris.gef.presentation.FigEdge;
 59  
 import org.tigris.gef.presentation.FigNode;
 60  
 
 61  
 /**
 62  
  * Details panel tabbed panel for displaying a source code representation of
 63  
  * a UML model element in a particular Language.
 64  
  */
 65  
 public class TabSrc
 66  
     extends TabText
 67  
     implements ItemListener {
 68  
 
 69  
     private static final long serialVersionUID = -4958164807996827484L;
 70  
 
 71  900
     private static final Logger LOG = Logger.getLogger(TabSrc.class);
 72  
 
 73  900
     private Language langName = null;
 74  900
     private String fileName = null;
 75  900
     private SourceUnit[] files = null;
 76  
 
 77  900
     private LanguageComboBox cbLang = new LanguageComboBox();
 78  900
     private JComboBox cbFiles = new JComboBox();
 79  
     
 80  
     /**
 81  
      * These predicates determine if this tab is enabled.
 82  
      */
 83  900
     private static List<Predicate> predicates = new ArrayList<Predicate>();
 84  
     static {
 85  
         /* Add a predicate for ArgoUML's default capabilities: */
 86  900
         predicates.add(new DefaultPredicate());
 87  900
     }
 88  
 
 89  
 
 90  
     /**
 91  
      * Create a tab that contains a toolbar.
 92  
      * Then add a language selector onto it.
 93  
      */
 94  
     public TabSrc() {
 95  900
         super("tab.source", true);
 96  
 
 97  900
         setEditable(false);
 98  900
         langName = (Language) cbLang.getSelectedItem();
 99  900
         fileName = null;
 100  900
         getToolbar().add(cbLang);
 101  900
         getToolbar().addSeparator();
 102  900
         cbLang.addItemListener(this);
 103  900
         getToolbar().add(cbFiles);
 104  900
         getToolbar().addSeparator();
 105  900
         cbFiles.addItemListener(this);
 106  900
     }
 107  
 
 108  
 
 109  
     @Override
 110  
     protected void finalize() {
 111  0
         cbLang.removeItemListener(this);
 112  0
     }
 113  
 
 114  
     
 115  
     /**
 116  
      * Populate files[] and cbFiles, using the specified element.
 117  
      */
 118  
     private void generateSource(Object elem) {
 119  0
         LOG.debug("TabSrc.genText(): getting src for "
 120  
                   + Model.getFacade().getName(elem));
 121  0
         Collection code =
 122  
             GeneratorHelper.generate(langName, elem, false);
 123  0
         cbFiles.removeAllItems();
 124  0
         if (!code.isEmpty()) {
 125  0
             files = new SourceUnit[code.size()];
 126  0
             files = (SourceUnit[]) code.toArray(files);
 127  0
             for (int i = 0; i < files.length; i++) {
 128  0
                 StringBuilder title = new StringBuilder(files[i].getName());
 129  0
                 if (files[i].getBasePath().length() > 0) {
 130  0
                     title.append(" ( " + files[i].getFullName() + ")");
 131  
                 }
 132  0
                 cbFiles.addItem(title.toString());
 133  
             }
 134  
         }
 135  0
     }
 136  
 
 137  
 
 138  
     @Override
 139  
     protected String genText(Object modelObject) {
 140  0
         if (files == null) {
 141  0
             generateSource(modelObject);
 142  
         }
 143  0
         if (files != null && files.length > cbFiles.getSelectedIndex()) {
 144  0
             return files[cbFiles.getSelectedIndex()].getContent();
 145  
         }
 146  0
         return null;
 147  
     }
 148  
 
 149  
     @Override
 150  
     protected void parseText(String s) {
 151  0
         LOG.debug("TabSrc   setting src for " 
 152  
                 + Model.getFacade().getName(getTarget()));
 153  0
         Object modelObject = getTarget();
 154  0
         if (getTarget() instanceof FigNode) {
 155  0
             modelObject = ((FigNode) getTarget()).getOwner();
 156  
         }
 157  0
         if (getTarget() instanceof FigEdge) {
 158  0
             modelObject = ((FigEdge) getTarget()).getOwner();
 159  
         }
 160  0
         if (modelObject == null) {
 161  0
             return;
 162  
         }
 163  
         /* TODO: Implement this! */
 164  
         //Parser.ParseAndUpdate(modelObject, s);
 165  0
     }
 166  
 
 167  
 
 168  
     @Override
 169  
     public void setTarget(Object t) {
 170  0
         Object modelTarget = (t instanceof Fig) ? ((Fig) t).getOwner() : t;
 171  0
         setShouldBeEnabled(Model.getFacade().isAClassifier(modelTarget));
 172  0
         cbFiles.removeAllItems();
 173  0
         files = null;
 174  0
         super.setTarget(t);
 175  0
     }
 176  
 
 177  
     /**
 178  
      * Determines if the current tab should be enabled with the given target.
 179  
      * Returns true if the given target is or represents a Classifier.
 180  
      *
 181  
      * {@inheritDoc}
 182  
      */
 183  
     @Override
 184  
     public boolean shouldBeEnabled(Object target) {
 185  1327
         target = (target instanceof Fig) ? ((Fig) target).getOwner() : target;
 186  
 
 187  1327
         setShouldBeEnabled(false);
 188  1327
         for (Predicate p : predicates) {
 189  1327
             if (p.evaluate(target)) {
 190  0
                 setShouldBeEnabled(true);
 191  
             }
 192  
         }
 193  
 
 194  1327
         return shouldBeEnabled();
 195  
     }
 196  
 
 197  
     /*
 198  
      * @see java.awt.event.ItemListener#itemStateChanged(java.awt.event.ItemEvent)
 199  
      */
 200  
     public void itemStateChanged(ItemEvent event) {
 201  0
         if (event.getSource() == cbLang) {
 202  0
             if (event.getStateChange() == ItemEvent.SELECTED) {
 203  0
                 Language newLang = (Language) cbLang.getSelectedItem();
 204  0
                 if (!newLang.equals(langName)) {
 205  0
                     langName = newLang;
 206  0
                     refresh();
 207  
                 }
 208  0
             }
 209  0
         } else if (event.getSource() == cbFiles) {
 210  0
             if (event.getStateChange() == ItemEvent.SELECTED) {
 211  0
                 String newFile = (String) cbFiles.getSelectedItem();
 212  0
                 if (!newFile.equals(fileName)) {
 213  0
                     fileName = newFile;
 214  0
                     super.setTarget(getTarget());
 215  
                 }
 216  
             }
 217  
         }
 218  0
     }
 219  
 
 220  
     @Override
 221  
     public void refresh() {
 222  0
         setTarget(getTarget());
 223  0
     }
 224  
 
 225  
     /**
 226  
      * This function allows extra predicates to be added.
 227  
      * The predicates are conditions for cases where the 
 228  
      * TabSrc should show source code. If a plugin module 
 229  
      * is able to generate code for certain objects, for
 230  
      * which ArgoUML itself does not generate code, then
 231  
      * this function will allow the module to show the tab.
 232  
      *  
 233  
      * @param predicate the predicate to be added
 234  
      */
 235  
     public static void addPredicate(Predicate predicate) {
 236  0
         predicates.add(predicate);
 237  0
     }
 238  
 
 239  1800
     private static class DefaultPredicate implements Predicate {
 240  
         public boolean evaluate(Object object) {
 241  1327
             return (Model.getFacade().isAClassifier(object));
 242  
         }
 243  
     }
 244  
 }