umbrello API Documentation

javacodeaccessormethod.cpp

00001 /***************************************************************************
00002  *                                                                         *
00003  *   This program is free software; you can redistribute it and/or modify  *
00004  *   it under the terms of the GNU General Public License as published by  *
00005  *   the Free Software Foundation; either version 2 of the License, or     *
00006  *   (at your option) any later version.                                   *
00007  *                                                                         *
00008  *   copyright (C) 2004-2007                                               *
00009  *   Umbrello UML Modeller Authors <uml-devel@uml.sf.net>                  *
00010  ***************************************************************************/
00011 
00012 /*  This code generated by:
00013  *      Author : thomas
00014  *      Date   : Fri Jun 20 2003
00015  */
00016 
00017 // own header
00018 #include "javacodeaccessormethod.h"
00019 
00020 // qt/kde includes
00021 #include <kdebug.h>
00022 
00023 // local includes
00024 #include "../attribute.h"
00025 #include "../codegenerator.h"
00026 #include "../codegenerationpolicy.h"
00027 #include "../classifiercodedocument.h"
00028 #include "../umlobject.h"
00029 #include "../umlrole.h"
00030 #include "../uml.h"
00031 
00032 #include "javaclassifiercodedocument.h"
00033 #include "javacodegenerationpolicy.h"
00034 #include "javacodeclassfield.h"
00035 #include "javacodedocumentation.h"
00036 
00037 // Constructors/Destructors
00038 //
00039 
00040 JavaCodeAccessorMethod::JavaCodeAccessorMethod ( CodeClassField * field, CodeAccessorMethod::AccessorType type)
00041         : CodeAccessorMethod ( field )
00042 {
00043     setType(type);
00044 
00045     // lets use full-blown comment
00046     JavaClassifierCodeDocument* jccd = dynamic_cast<JavaClassifierCodeDocument*>(field->getParentDocument());
00047     setComment(new JavaCodeDocumentation(jccd));
00048 }
00049 
00050 JavaCodeAccessorMethod::~JavaCodeAccessorMethod ( ) { }
00051 
00052 // Other methods
00053 //
00054 
00055 void JavaCodeAccessorMethod::setAttributesOnNode ( QDomDocument & doc, QDomElement & blockElement)
00056 {
00057 
00058     // set super-class attributes
00059     CodeAccessorMethod::setAttributesOnNode(doc, blockElement);
00060 
00061     // set local attributes now
00062 }
00063 
00064 void JavaCodeAccessorMethod::setAttributesFromNode( QDomElement & root)
00065 {
00066 
00067     // set attributes from superclass method the XMI
00068     CodeAccessorMethod::setAttributesFromNode(root);
00069 
00070     // load local stuff
00071 
00072 }
00073 
00074 void JavaCodeAccessorMethod::updateContent( )
00075 {
00076 
00077     CodeClassField * parentField = getParentClassField();
00078     JavaCodeClassField * javafield = dynamic_cast<JavaCodeClassField*>(parentField);
00079     QString fieldName = javafield->getFieldName();
00080 
00081     QString text = "";
00082     switch(getType()) {
00083     case CodeAccessorMethod::ADD:
00084         {
00085             int maxOccurs = javafield->maximumListOccurances();
00086             QString fieldType = javafield->getTypeName();
00087             QString indent = getIndentation();
00088             QString endLine = UMLApp::app()->getCommonPolicy()->getNewLineEndingChars();
00089             if(maxOccurs > 0)
00090                 text += "if ("+fieldName+".size() < "+ QString::number(maxOccurs)+") {"+endLine+indent;
00091             text += fieldName+".add(value);";
00092             if(maxOccurs > 0)
00093             {
00094                 text += endLine+"} else {"+endLine;
00095                 text += indent + "System.err.println(\"ERROR: Cant add"+fieldType+" to "+fieldName+", minimum number of items reached.\");"+endLine+'}'+endLine;
00096             }
00097             break;
00098         }
00099     case CodeAccessorMethod::GET:
00100         text = "return "+fieldName+';';
00101         break;
00102     case CodeAccessorMethod::LIST:
00103         text = "return (List) "+fieldName+';';
00104         break;
00105     case CodeAccessorMethod::REMOVE:
00106         {
00107             int minOccurs = javafield->minimumListOccurances();
00108             QString fieldType = javafield->getTypeName();
00109             QString endLine = UMLApp::app()->getCommonPolicy()->getNewLineEndingChars();
00110             QString indent = getIndentation();
00111 
00112             if(minOccurs > 0)
00113                 text += "if ("+fieldName+".size() >= "+ QString::number(minOccurs)+") {"+endLine+indent;
00114             text += fieldName+".remove(value);";
00115             if(minOccurs > 0)
00116             {
00117                 text += endLine+"} else {"+endLine;
00118                 text += indent + "System.err.println(\"ERROR: Cant remove"+fieldType+" from "+fieldName+", minimum number of items reached.\");"+endLine+'}'+endLine;
00119             }
00120             break;
00121         }
00122     case CodeAccessorMethod::SET:
00123         text = fieldName+" = value;";
00124         break;
00125     default:
00126         // do nothing
00127         break;
00128     }
00129 
00130     setText(text);
00131 
00132 }
00133 
00134 void JavaCodeAccessorMethod::updateMethodDeclaration()
00135 {
00136 
00137     JavaCodeClassField * javafield = dynamic_cast<JavaCodeClassField*>(getParentClassField());
00138     JavaClassifierCodeDocument * javadoc = dynamic_cast<JavaClassifierCodeDocument*>(javafield->getParentDocument());
00139     CodeGenerationPolicy *commonpolicy = UMLApp::app()->getCommonPolicy();
00140 
00141     // gather defs
00142     CodeGenerationPolicy::ScopePolicy scopePolicy = commonpolicy->getAttributeAccessorScope();
00143     QString strVis = javadoc->scopeToJavaDecl(javafield->getVisibility());
00144     QString fieldName = javafield->getFieldName();
00145     QString fieldType = javafield->getTypeName();
00146     QString objectType = javafield->getListObjectType();
00147     if(objectType.isEmpty())
00148         objectType = fieldName;
00149     QString endLine = UMLApp::app()->getCommonPolicy()->getNewLineEndingChars();
00150 
00151     // set scope of this accessor appropriately..if its an attribute,
00152     // we need to be more sophisticated
00153     if(javafield->parentIsAttribute())
00154         switch (scopePolicy) {
00155         case CodeGenerationPolicy::Public:
00156         case CodeGenerationPolicy::Private:
00157         case CodeGenerationPolicy::Protected:
00158               strVis = javadoc->scopeToJavaDecl((Uml::Visibility::Value) scopePolicy);
00159             break;
00160         default:
00161         case CodeGenerationPolicy::FromParent:
00162             // do nothing..already have taken parent value
00163             break;
00164         }
00165 
00166     // some variables we will need to populate
00167     QString headerText = "";
00168     QString methodReturnType = "";
00169     QString methodName = "";
00170     QString methodParams = "";
00171 
00172     switch(getType()) {
00173     case CodeAccessorMethod::ADD:
00174         methodName = "add"+javadoc->capitalizeFirstLetter(fieldType);
00175         methodReturnType = "void";
00176         methodParams = objectType+" value ";
00177         headerText = "Add an object of type "+objectType+" to the List "+fieldName+endLine+getParentObject()->getDoc()+endLine+"@return void";
00178         break;
00179     case CodeAccessorMethod::GET:
00180         methodName = "get"+javadoc->capitalizeFirstLetter(fieldName);
00181         methodReturnType = fieldType;
00182         headerText = "Get the value of "+fieldName+endLine+getParentObject()->getDoc()+endLine+"@return the value of "+fieldName;
00183         break;
00184     case CodeAccessorMethod::LIST:
00185         methodName = "get"+javadoc->capitalizeFirstLetter(fieldType)+"List";
00186         methodReturnType = "List";
00187         headerText = "Get the list of "+fieldName+endLine+getParentObject()->getDoc()+endLine+"@return List of "+fieldName;
00188         break;
00189     case CodeAccessorMethod::REMOVE:
00190         methodName = "remove"+javadoc->capitalizeFirstLetter(fieldType);
00191         methodReturnType = "void";
00192         methodParams = objectType+" value ";
00193         headerText = "Remove an object of type "+objectType+" from the List "+fieldName+endLine+getParentObject()->getDoc();
00194         break;
00195     case CodeAccessorMethod::SET:
00196         methodName = "set"+javadoc->capitalizeFirstLetter(fieldName);
00197         methodReturnType = "void";
00198         methodParams = fieldType + " value ";
00199         headerText = "Set the value of "+fieldName+endLine+getParentObject()->getDoc()+endLine;
00200         break;
00201     default:
00202         // do nothing..no idea what this is
00203         kWarning()<<"Warning: cant generate JavaCodeAccessorMethod for type: "<<getType()<<endl;
00204         break;
00205     }
00206 
00207     // set header once.
00208     if(getComment()->getText().isEmpty())
00209         getComment()->setText(headerText);
00210 
00211     // set start/end method text
00212     setStartMethodText(strVis+' '+methodReturnType+' '+methodName+" ( "+methodParams+" ) {");
00213     setEndMethodText("}");
00214 
00215 }
00216 
00217 void JavaCodeAccessorMethod::update()
00218 {
00219     updateMethodDeclaration();
00220     updateContent();
00221 }
00222 
00223 #include "javacodeaccessormethod.moc"
KDE Logo
This file is part of the documentation for umbrello Version 3.1.0.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Tue Jun 26 08:07:57 2007 by doxygen 1.4.1 written by Dimitri van Heesch, © 1997-2003