Main Page | Packages | Class Hierarchy | Class List | File List | Class Members

UMLInterface.java

Go to the documentation of this file.
00001 //$Id: UMLInterface_8java-source.html 336 2010-01-12 20:03:17Z linus $
00002 //Copyright (c) 2003, Mikael Albertsson, Mattias Danielsson, Per Engström, 
00003 //Fredrik Gröndahl, Martin Gyllensten, Anna Kent, Anders Olsson, 
00004 //Mattias Sidebäck.
00005 //All rights reserved.
00006 //
00007 //Redistribution and use in source and binary forms, with or without 
00008 //modification, are permitted provided that the following conditions are met:
00009 //
00010 //* Redistributions of source code must retain the above copyright notice, 
00011 //  this list of conditions and the following disclaimer.
00012 // 
00013 //* Redistributions in binary form must reproduce the above copyright 
00014 //  notice, this list of conditions and the following disclaimer in the 
00015 //  documentation and/or other materials provided with the distribution.
00016 //
00017 //* Neither the name of the University of Linköping nor the names of its 
00018 //  contributors may be used to endorse or promote products derived from 
00019 //  this software without specific prior written permission. 
00020 //
00021 //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
00022 //AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
00023 //IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
00024 //ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
00025 //LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
00026 //CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
00027 //SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
00028 //INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
00029 //CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
00030 //ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 
00031 //THE POSSIBILITY OF SUCH DAMAGE.
00032 
00033 package org.argoprint.uml_interface;
00034 
00035 import org.argoprint.*;
00036 
00037 import java.awt.event.ActionEvent;
00038 
00039 import java.io.File;
00040 import java.io.FileNotFoundException;
00041 import java.io.FileOutputStream;
00042 import java.io.IOException;
00043 
00044 import javax.swing.JFileChooser;
00045 import javax.swing.JOptionPane;
00046 import javax.swing.filechooser.FileFilter;
00047 
00048 import org.apache.log4j.Logger;
00049 import org.argouml.kernel.Project;
00050 import org.argouml.kernel.ProjectManager;
00051 import org.argouml.kernel.ProjectMember;
00052 
00053 import org.argouml.ui.ArgoDiagram;
00054 import org.argouml.ui.ProjectBrowser;
00055 
00056 import org.argouml.uml.diagram.ui.*;
00057 
00058 import org.argouml.util.FileFilters;
00059 import org.argouml.util.SuffixFilter;
00060 import org.argouml.util.osdep.OsUtil;
00061 
00062 import org.argouml.model.ModelFacade;
00063 import org.argouml.application.api.*;
00064 
00065 import org.tigris.gef.base.CmdSaveEPS;
00066 import org.tigris.gef.base.CmdSaveGIF;
00067 import org.tigris.gef.base.CmdSaveGraphics;
00068 import org.tigris.gef.base.CmdSavePS;
00069 import org.tigris.gef.base.CmdSaveSVG;
00070 import org.tigris.gef.base.Diagram;
00071 import org.tigris.gef.util.Util;
00072 
00073 import java.util.*;
00074 import java.io.*;
00075 import java.awt.Rectangle;
00076 
00077 import java.lang.reflect.*;
00078 import java.lang.Class;
00079 
00080 import java.lang.Boolean;
00081 
00082 import org.tigris.gef.base.*;
00083 import org.tigris.gef.persistence.*;
00084 
00085 /** 
00086  * The class ArgoPrint uses to interface to the ArgoUML model.
00087  * It primarily communicates with ModelFacade using java.lang.reflect.*
00088  * To save diagrams gef is used. TODO: fix bug that causes only
00089  * the open diagram to be saved correctly.
00090  *
00091  * @author matda701, Mattias Danielsson
00092  */
00093 public class UMLInterface 
00094     implements ArgoPrintDataSource{
00095     public static final String separator = "/";
00096 
00097     /**
00098      * The ArgoUML project that ArgoPrint is applied to. Must be
00099      * set prior to use by using the setProject(..) method
00100      */
00101     private Project _project;
00102 
00103     /**
00104      * The ArgoUML projectbrowser that ArgoPrint is applied to. Must be
00105      * set prior to use by using the setProjectBrowser(..) method
00106      */
00107     private ProjectBrowser _projectBrowser;
00108     
00109     /**
00110      * A reference to the ModelFacade
00111      */
00112     private ModelFacade _facade; 
00113 
00114     /**
00115      * A reference to the ArgoUML Logger. (Uses log4java) 
00116      */
00117     private Logger _log;
00118     
00119     /**
00120      * The ArgoPrint output dir. Used when saving diagrams as pictures. 
00121      * Must be set prior to use.
00122      */
00123     String _outputDir;
00124 
00125     ////////////////////////////////////////////////////////////////
00126     // constructors
00127     
00128     /**
00129      * Constructor
00130      */
00131     public UMLInterface() {
00132     //super( "action.save-graphics", NO_ICON);
00133     //_facade = new ModelFacade(); 
00134     _facade = ModelFacade.getFacade();
00135     }
00136 
00137     /**
00138      * Initializes fields prior to usage. Can be used instead of the 
00139      * individual setters.
00140      */
00141     public void initialize(Logger log){
00142     _log = log;
00143     _projectBrowser = ProjectBrowser.getInstance();
00144     _project = ProjectManager.getManager().getCurrentProject();
00145     }
00146 
00147     ////////////////////////////////////////////////////////////////
00148     // setters
00149 
00150     /**
00151      * Sets the logger (_log) to logger
00152      */
00153     public void setLog(Logger logger){ _log = logger; }
00154     
00155     /**
00156      * Sets the projectBrowswer to browser
00157      */
00158     public void setProjectBrowser(ProjectBrowser browser){
00159     _projectBrowser = browser;
00160     }
00161     
00162     /**
00163      * Sets the project to proj
00164      */
00165     public void setProject(Project proj){ _project = proj; }
00166 
00167     /**
00168      * Sets the outputPath to path
00169      */
00170     public void setOutputDir(String dir){ _outputDir = new String(dir); }
00171 
00172     ////////////////////////////////////////////////////////////////
00173     // main methods
00174     
00175     /**
00176      * Checks if ModelFacade has a method named method. Depracated!
00177      */
00178     public boolean hasMethod(String method){
00179     Class c = _facade.getClass();
00180     Method[] theMethods = c.getMethods();
00181       
00182     for (int i = 0; i < theMethods.length; i++) {
00183         if(method.equals(theMethods[i].getName())){
00184         return true;
00185         }
00186     }
00187     return false;
00188     }
00189     
00190     /**
00191      * Calls method named call in ModelFacade 
00192      * returns Object, which often is String or Collection
00193      * iteratorObject is the argument for the method 
00194      */
00195     public Object caller(String call, Object iteratorObject)
00196     throws Exception{
00197     //_log.info("Arg call: " + call + " Arg: " + 
00198     //    _facade.getName(iteratorObject)); 
00199     Class c = _facade.getClass();
00200     Method[] theMethods = c.getMethods();   
00201     
00202     Object args[] = new Object[1];
00203     args[0] = iteratorObject;
00204 
00205     if(call.endsWith(new String("()"))){
00206         int callLength = call.length()-2;
00207         
00208         String callName = new String(call.substring(0, callLength));
00209         for (int i = 0; i < theMethods.length; i++) {
00210         
00211         if(callName.equals(theMethods[i].getName())){
00212             try{
00213             return theMethods[i].invoke(null, args);
00214             }
00215             catch (IllegalAccessException e){
00216             _log.info("Crash" + e.getMessage());
00217             }
00218             catch (IllegalArgumentException e){
00219             _log.info("Crash" + e.getMessage());
00220             }
00221             catch (InvocationTargetException e){
00222             _log.info("Crash" + e.getMessage());
00223             }
00224             catch (NullPointerException e){
00225             _log.info("Crash" + e.getMessage());
00226             }
00227             catch (ExceptionInInitializerError e){
00228             _log.info("Crash" + e.getMessage());
00229             }          
00230         }       
00231         }
00232     }
00233     //throw new Exception("Illegal method call");
00234     return new String("Not a known method");
00235     }
00236 
00237     
00238     /**
00239      * Calls method named call in ModelFacade. Used when argument is to
00240      * be on of the default. ex. calledMethodName(model) and not an
00241      * iteratorObject
00242      */
00243     public Object caller(String call)
00244     throws Exception{
00245     //_log.info("call: " + call);
00246     //Class c = _facade.getClass();
00247     //Method[] theMethods = c.getMethods();
00248 
00249     if(call.endsWith(new String("()"))){
00250        Class c = _facade.getClass();
00251        Method[] theMethods = c.getMethods();
00252        int callLength = call.length()-2;
00253         
00254         String callName = new String(call.substring(0, callLength - 1));
00255         
00256         for (int i = 0; i < theMethods.length; i++) {
00257         if(callName.equals(theMethods[i].getName())){
00258             try{
00259             return theMethods[i].invoke(null, null);
00260             }
00261             catch (IllegalAccessException ignore ){
00262             }
00263             catch (IllegalArgumentException ignore ){
00264             }
00265             catch (InvocationTargetException ignore ){
00266             }
00267             catch (NullPointerException ignore ){
00268             }
00269             catch (ExceptionInInitializerError ignore ){
00270             }          
00271         }       
00272         }
00273     } else if(call.endsWith(new String(")"))){
00274         Class c= _facade.getClass();;
00275         Method[] theMethods = c.getMethods();
00276         int callLength = call.indexOf((int) '(') + 1;
00277         
00278         String callName = new String(call.substring(0, callLength - 1));
00279         String arg = 
00280         new String(call.substring(callLength, call.length() - 1));
00281         Object args[] = new Object[1];  
00282         Object thisObject = null;
00283 
00284         if(arg.equals(new String("model"))){
00285         args[0] = _project.getModel();
00286         //c = _facade.getClass();
00287         //theMethods = c.getMethods();
00288         } else if(arg.equals(new String("project"))){
00289         thisObject = _project;
00290         args = null;
00291         c = _project.getClass();
00292         int vectorLen = c.getMethods().length;
00293         theMethods = new Method[vectorLen];
00294         theMethods = c.getMethods();
00295         } else {
00296         //c = _facade.getClass();
00297         //theMethods = c.getMethods();
00298         }
00299  
00300         for (int i = 0; i < theMethods.length; i++) {
00301         if(callName.equals(theMethods[i].getName())){
00302             try{    
00303             //_log.info("Call hit: " + arg + " " + callName);
00304             //Object args[] = new Object[1];
00305             //args[0] = _project.getModel(); 
00306             return theMethods[i].invoke(thisObject, 
00307                             args);
00308             }catch(Exception e){
00309             throw e;
00310             }
00311         }       
00312         }
00313         
00314         //return null;
00315     }
00316     throw new Exception("Illegal method call");
00317     //return new String("Illegal method call");
00318     }
00319     
00320     /**
00321      * Calls method named call in ModelFacade 
00322      * returns boolean, caller(..) can be used instead but
00323      * then Booolean.booleanValue() must be used. Depracated! 
00324      */
00325     public boolean booleanCaller(String call, Object args[]){
00326     if(hasMethod(call)){
00327         Class c = _facade.getClass();
00328         Method[] theMethods = c.getMethods();   
00329         
00330         for (int i = 0; i < theMethods.length; i++) {
00331         if(call.equals(theMethods[i].getName())){
00332             try{
00333             Object answer = theMethods[i].invoke(null, args);
00334             return ((Boolean)answer).booleanValue();
00335             //break;
00336             }
00337             catch(IllegalAccessException ignore ){
00338             
00339             }
00340             catch(IllegalArgumentException ignore ){
00341                
00342             }
00343             catch(InvocationTargetException ignore ){
00344             
00345             }
00346             catch(NullPointerException ignore ){
00347             
00348             }
00349             catch(ExceptionInInitializerError ignore ){
00350             
00351             }           
00352         }       
00353         }
00354     }
00355     //should be throw exeption
00356     return false;
00357     //return new String("Not a known method");
00358     }
00359 
00360     /**
00361      * Returns all diagrams in the project. TODO: Figure out a clever
00362      * way to invoke with caller. Solved by using reflections on Project
00363      * Therefor not needed.
00364      */    
00365     public Collection getAllDiagrams(){
00366     return _project.getDiagrams();
00367     }
00368 
00369 
00370     /**
00371      * Saves a diagram as gif in the directory specified by _outputDir.
00372      * Returns a String with the path to the saved gif file.
00373      * TODO: Solve same Bug as in trySaveAllDiagrams() and implement 
00374      * better control for overwrite of old files. 
00375      */
00376     public String saveDiagram(UMLDiagram diagram) 
00377     throws Exception{
00378     //Todo: fix bug mentioned i trySaveAllDiagrams
00379 
00380     if( diagram instanceof Diagram ) {
00381         String defaultName = ((Diagram) diagram).getName();
00382         _log.info("active diagram" + 
00383               _project.getActiveDiagram().getName());
00384         _project.setActiveDiagram((ArgoDiagram) diagram);
00385         _log.info("active diagram" + 
00386               _project.getActiveDiagram().getName());
00387         
00388         defaultName = Util.stripJunk(defaultName);
00389         
00390         _log.info("diagram name " + defaultName);
00391         
00392         try {
00393         File defFile = 
00394             new File(_outputDir + 
00395                  defaultName + "."
00396                  + FileFilters.GIFFilter._suffix);
00397         
00398         _log.info("diagram filename " + defaultName + "."
00399               + FileFilters.GIFFilter._suffix);
00400         
00401         if (defFile != null) {
00402             String path = defFile.getParent();
00403             _log.info("diagram path " + path); 
00404             
00405             String name = defFile.getName();
00406             _log.info("diagram name " + name);
00407             
00408             String extension = SuffixFilter.getExtension(defFile);
00409             _log.info("diagram ext " + extension);
00410             
00411             CmdSaveGraphics cmd = null;
00412             
00413             cmd = new CmdSaveGIF();
00414                 
00415 
00416             if ( !path.endsWith( separator ) ) {
00417             path += separator;
00418             }
00419             
00420             _projectBrowser.showStatus( "Writing " + path + name + "..." );
00421             _log.info( "Writing " + path + name + "..." );    
00422 
00423             if ( defFile.exists() ) {
00424             String t = "Overwrite " + path + name;
00425             int response =
00426                 JOptionPane.showConfirmDialog(_projectBrowser,
00427                               t, t,
00428                               JOptionPane.YES_NO_OPTION);
00429             if (response == JOptionPane.NO_OPTION){ 
00430                 throw new Exception("Cannot overwrite file");
00431             }
00432             }
00433             
00434             FileOutputStream fo = new FileOutputStream( defFile );
00435             cmd.setStream(fo);
00436             cmd.doIt();
00437             fo.close();
00438             _projectBrowser.showStatus( "Wrote " + path + name );
00439             _log.info( "Wrote " + path + name + "..." );
00440             //return true;
00441             return new String(path + name);
00442         }
00443         
00444         }
00445         catch ( FileNotFoundException ignore )
00446         {
00447             throw ignore;
00448         }
00449         catch ( IOException ignore )
00450         {
00451             throw ignore;
00452         }
00453     }
00454     
00455     throw new Exception("Not a valid diagram");
00456     }
00457 
00458     /**
00459      * Tests saving of diagrams as gif-files.
00460      * TODO: Solve bug that causes only open diagram to
00461      * be saved. 
00462      */
00463     public boolean trySaveAllDiagrams( boolean overwrite ) {
00464     
00465     _log.info("trySaveAllDiagrams started");
00466 
00467     Vector diagramVector =
00468         _project.getDiagrams();
00469     
00470     int diagramVectorSize = diagramVector.size();
00471         
00472     for(int i = 0; i < diagramVectorSize; i++){
00473         Object target = diagramVector.elementAt(i); 
00474         
00475         if( target instanceof Diagram ) {
00476         String defaultName = ((Diagram) target).getName();
00477         _log.info("active diagram" + 
00478               _project.getActiveDiagram().getName());
00479         _project.setActiveDiagram((ArgoDiagram) target);
00480         _log.info("active diagram" + 
00481             _project.getActiveDiagram().getName());
00482 
00483         defaultName = Util.stripJunk(defaultName);
00484 
00485         _log.info("diagram name " + defaultName);
00486 
00487         try {
00488             File defFile = 
00489             new File("/home/pum3/danielsson/" + 
00490                  defaultName + "."
00491                  + FileFilters.GIFFilter._suffix);
00492 
00493             _log.info("diagram filename " + defaultName + "."
00494                  + FileFilters.GIFFilter._suffix);
00495 
00496             if (defFile != null) {
00497             String path = defFile.getParent();
00498             _log.info("diagram path " + path); 
00499 
00500             String name = defFile.getName();
00501             _log.info("diagram name " + name);
00502 
00503             String extension = SuffixFilter.getExtension(defFile);
00504             _log.info("diagram ext " + extension);
00505      
00506             CmdSaveGraphics cmd = null;
00507                 
00508             cmd = new CmdSaveGIF();
00509                 
00510 
00511             if ( !path.endsWith( separator ) ) {
00512                 path += separator;
00513             }
00514             
00515             _projectBrowser.showStatus( "Writing " + path + name + "..." );
00516             _log.info( "Writing " + path + name + "..." );    
00517 
00518             if ( defFile.exists() && !overwrite ) {
00519                 String t = "Overwrite " + path + name;
00520                 int response =
00521                 JOptionPane.showConfirmDialog(_projectBrowser,
00522                                   t, t,
00523                                   JOptionPane.YES_NO_OPTION);
00524                 if (response == JOptionPane.NO_OPTION) return false;
00525             }
00526                 
00527             FileOutputStream fo = new FileOutputStream( defFile );
00528             cmd.setStream(fo);
00529             cmd.doIt();
00530             fo.close();
00531             _projectBrowser.showStatus( "Wrote " + path + name );
00532             _log.info( "Wrote " + path + name + "..." );
00533             //return true;
00534             }
00535             
00536         }
00537         catch ( FileNotFoundException ignore )
00538             {
00539             //cat.error("got a FileNotFoundException", ignore);
00540             }
00541         catch ( IOException ignore )
00542             {
00543             //cat.error("got an IOException", ignore);
00544             }
00545         
00546         //diagramVector.removeElementAt(0);
00547         }
00548 
00549         //return false;
00550     }
00551     return true;
00552     }/*end of method save all diags */
00553    
00554 } /* end class UMLInteface */
00555 
00556 
00557 
00558 
00559 
00560 
00561 

Generated on Tue Dec 2 22:50:56 2003 for ArgoPrint by doxygen 1.3.4