summaryrefslogtreecommitdiffstats
path: root/umbrello/umbrello/codeparameter.cpp
blob: cd10e43859949dca65ba3975869913f15ebb8eb6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   copyright (C) 2004-2007                                               *
 *   Umbrello UML Modeller Authors <uml-devel@uml.sf.net>                  *
 ***************************************************************************/

/*  This code generated by:
 *      Author : thomas
 *      Date   : Fri Jun 20 2003
 */

// own header
#include "codeparameter.h"

// qt/kde includes
#include <kdebug.h>

// local includes
#include "association.h"
#include "attribute.h"
#include "classifiercodedocument.h"
#include "umldoc.h"
#include "umlobject.h"
#include "umlrole.h"
#include "uml.h"
#include "codegenerators/codegenfactory.h"

// Constructors/Destructors
//

CodeParameter::CodeParameter ( ClassifierCodeDocument * parentDoc, UMLObject * parentObject )
        : TQObject ( (TQObject*) parentObject, "ACodeParam")
{
    initFields( parentDoc, parentObject );
}

CodeParameter::~CodeParameter ( ) { }

//
// Methods
//


// Accessor methods
//

/**
 * Utility method to get the value of parent object abstract value
 * @return the value of parent object abstrtact
 */
bool CodeParameter::getAbstract ( ) {
    return m_parentObject->getAbstract();
}

/**
 * Utility method to get the value of parent object static
 * Whether or not this is static.
 * @return the value of static
 */
bool CodeParameter::getStatic ( ) {
    return m_parentObject->getStatic();
}

/**
 * Utility method to get the value of parent object name
 * The name of this code parameter.
 * @return the value
 */
TQString CodeParameter::getName ( ) const {
    return m_parentObject->getName();
}

/**
 * Utility method to get the value of parent object type.
 * the typeName of this parameters (e.g. boolean, int, etc or perhaps Class name of
 * an object)
 * @return the value of type
 */
TQString CodeParameter::getTypeName ( ) {
    UMLAttribute * at = (UMLAttribute*) m_parentObject;
    return at->getTypeName();
}

/**
 * Utility method to get the value of parent object scope.
 * The visibility of this code parameter.
 * @return the value of parent object scope
 */
Uml::Visibility CodeParameter::getVisibility ( ) const {
    return m_parentObject->getVisibility();
}

/**
 * Set the value of m_initialValue
 * The initial value of this code parameter
 * @param new_var the new value of m_initialValue
 */
void CodeParameter::setInitialValue ( const TQString &new_var ) {
    m_initialValue = new_var;
}

/**
 * Get the value of m_initialValue
 * The initial value of this code parameter
 * @return the value of m_initialValue
 */
TQString CodeParameter::getInitialValue ( ) {
    return m_initialValue;
}

/**
 * Set a Comment object.
 */
void CodeParameter::setComment ( CodeComment * object ) {
    m_comment = object;
}

/**
 * Get the Comment on this object.
 */
CodeComment * CodeParameter::getComment ( ) {
    return m_comment;
}


ClassifierCodeDocument * CodeParameter::getParentDocument ( ) {
    return m_parentDocument;
}

/**
 * Get the ParentObject object
 */
UMLObject * CodeParameter::getParentObject ( ) {
    return m_parentObject;
}

// need to get the ID of the parent object
// this is kind of broken for UMLRoles.
TQString CodeParameter::getID () {
    UMLRole * role = dynamic_cast<UMLRole*>(m_parentObject);
    if(role)
    {
        // cant use Role "ID" as that is used to distinquish if its
        // role "A" or "B"
        UMLAssociation *assoc = role->getParentAssociation();
        return ID2STR(assoc->getID());
    } else
        return ID2STR(m_parentObject->getID());

}

// Other methods
//

void CodeParameter::setAttributesOnNode ( TQDomDocument & doc, TQDomElement & blockElement)
{


    // set local attributes
    blockElement.setAttribute("parent_id",getID());

    // setting ID's takes special treatment
    // as UMLRoles arent properly stored in the XMI right now.
    // (change would break the XMI format..save for big version change )
    UMLRole * role = dynamic_cast<UMLRole*>(m_parentObject);
    if(role)
        blockElement.setAttribute("role_id", role->getRole());
    else
        blockElement.setAttribute("role_id","-1");

    blockElement.setAttribute("initialValue",getInitialValue());

    // a comment which we will store in its own separate child node block
    TQDomElement commElement = doc.createElement( "header" );
    getComment()->saveToXMI(doc, commElement); // comment
    blockElement.appendChild( commElement);

}

/** set the class attributes of this object from
 * the passed element node.
 */
void CodeParameter::setAttributesFromNode ( TQDomElement & root) {

    // set local attributes, parent object first
    TQString idStr = root.attribute("parent_id","-1");
    Uml::IDType id = STR2ID(idStr);

    // always disconnect
    m_parentObject->disconnect(this);

    // now, what is the new object we want to set?
    UMLObject * obj = UMLApp::app()->getDocument()->findObjectById(id);
    if(obj)
    {

        // FIX..one day.
        // Ugh. This is UGLY, but we have to do it this way because UMLRoles
        // don't go into the document list of UMLobjects, and have the same
        // ID as their parent UMLAssociations. So..the drill is then special
        // for Associations..in that case we need to find out which role will
        // serve as the parameter here. The REAL fix, of course, would be to
        // treat UMLRoles on a more even footing, but im not sure how that change
        // might ripple throughout the code and cause problems. Thus, since the
        // change appears to be needed for only this part, I'll do this crappy
        // change instead. -b.t.
        UMLAssociation * assoc = dynamic_cast<UMLAssociation*>(obj);
        if(assoc) {
            // In this case we init with indicated role child obj.
            UMLRole * role = 0;
            int role_id = root.attribute("role_id","-1").toInt();
            if(role_id == 1)
                role = assoc->getUMLRole(Uml::A);
            else if(role_id == 0)
                role = assoc->getUMLRole(Uml::B);
            else
                kError() << "corrupt save file? "
                << "cant get proper UMLRole for codeparameter uml id:"
                << ID2STR(id) << " w/role_id:" << role_id << endl;

            // init using UMLRole obj
            initFields ( m_parentDocument, role);

        } else
            initFields ( m_parentDocument, obj); // just the regular approach

    } else
        kError() << "Cant load CodeParam: parentUMLObject w/id:"
        << ID2STR(id) << " not found, corrupt save file?" << endl;

    // other attribs now
    setInitialValue(root.attribute("initialValue",""));

    // load comment now
    // by looking for our particular child element
    TQDomNode node = root.firstChild();
    TQDomElement element = node.toElement();
    bool gotComment = false;
    while( !element.isNull() ) {
        TQString tag = element.tagName();
        if( tag == "header" ) {
            TQDomNode cnode = element.firstChild();
            TQDomElement celem = cnode.toElement();
            getComment()->loadFromXMI(celem);
            gotComment = true;
            break;
        }
        node = element.nextSibling();
        element = node.toElement();
    }

    if(!gotComment)
        kWarning()<<" loadFromXMI : Warning: unable to initialize CodeComment in codeparam:"<<this<<endl;


}

/**
 * create the string representation of this code parameter.
 * @return      TQString
 */
void CodeParameter::syncToParent( ) {

    getComment()->setText(getParentObject()->getDoc());

    updateContent();
}

void CodeParameter::initFields ( ClassifierCodeDocument * doc, UMLObject * obj) {

    m_parentObject = obj;

    m_parentDocument = doc;
    m_initialValue = TQString("");

    m_comment = CodeGenFactory::newCodeComment(m_parentDocument);
    m_comment->setText(getParentObject()->getDoc());

    connect(m_parentObject,TQT_SIGNAL(modified()),this,TQT_SLOT(syncToParent()));
}

#include "codeparameter.moc"