~ubuntu-branches/ubuntu/utopic/tcm/utopic

« back to all changes in this revision

Viewing changes to src/sd/dv/cbdiagram.c

  • Committer: Bazaar Package Importer
  • Author(s): Otavio Salvador
  • Date: 2003-07-03 20:08:21 UTC
  • Revision ID: james.westby@ubuntu.com-20030703200821-se4xtqx25e5miczi
Tags: upstream-2.20
ImportĀ upstreamĀ versionĀ 2.20

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//------------------------------------------------------------------------------
 
2
//
 
3
// This file is part of Toolkit for Conceptual Modeling (TCM).
 
4
// (c) copyright 2001, University of Twente.
 
5
// Author: Henk van de Zandschulp (henkz@cs.utwente.nl).
 
6
//
 
7
// TCM is free software; you can redistribute it and/or modify
 
8
// it under the terms of the GNU General Public License as published by
 
9
// the Free Software Foundation; either version 2 of the License, or
 
10
// (at your option) any later version.
 
11
//
 
12
// TCM is distributed in the hope that it will be useful,
 
13
// but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
// GNU General Public License for more details.
 
16
//
 
17
// You should have received a copy of the GNU General Public License
 
18
// along with TCM; if not, write to the Free Software Foundation, Inc.,
 
19
// 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
20
//-----------------------------------------------------------------------------
 
21
 
 
22
#include "messagedialog.h"
 
23
#include "ssdsingleclassbox.h"
 
24
#include "cbsingleclassbox.h"
 
25
#include "ssdsingleobjectbox.h"
 
26
#include "c2r2mlistline.h"
 
27
#include "t4tlistline.h"
 
28
#include "commentlink.h"
 
29
#include "ssdclassnode.h"
 
30
#include "cbclassnode.h"
 
31
#include "cbobjectnode.h"
 
32
#include "ucdsingleclassbox.h"
 
33
 
 
34
#include "stickman.h"
 
35
#include "cbactor.h"
 
36
#include "cbclasslinkedge.h"
 
37
#include "cbobjectlinkedge.h"
 
38
 
 
39
#include "textbox.h"
 
40
#include "comment.h"
 
41
#include "notebox.h"
 
42
#include "note.h"
 
43
 
 
44
#include "cbgraph.h"
 
45
#include "cbwindow.h"
 
46
#include "cbviewer.h"
 
47
#include "cbchecks.h"
 
48
#include "cbdiagram.h"
 
49
#include "cbdmessage.h"
 
50
#include <stdio.h>
 
51
#include <limits.h>
 
52
 
 
53
CBDiagram::CBDiagram(Config *c, CBWindow *w, CBViewer *v, CBGraph *g): 
 
54
           ERDiagram(c, w, v, g) {
 
55
        UpdateNodeType(1);
 
56
        UpdateEdgeType(1);
 
57
        cbChecks = new CBChecks(this, g);
 
58
}
 
59
 
 
60
CBDiagram::~CBDiagram() {
 
61
        delete cbChecks;
 
62
}
 
63
 
 
64
Thing *CBDiagram::CreateThing(int classNr) {
 
65
        Grafport *g = GetDiagramViewer()->GetGrafport();
 
66
        ShapeView *v = GetDiagramViewer()->GetCurView();
 
67
        CBGraph *cg = (CBGraph *)GetGraph();
 
68
        Thing *thing = 0;
 
69
        if (classNr == Code::VIEW)
 
70
                thing = new ShapeView(GetDiagramViewer());
 
71
 
 
72
        // node shapes
 
73
        else if (classNr==Code::UCD_SINGLE_CLASS_BOX)
 
74
                thing = new UCDSingleClassBox(v, g, 0, 0); 
 
75
        else if (classNr==Code::STICKMAN)
 
76
                thing = new StickMan(v, g, 0, 0);
 
77
        else if (classNr==Code::SSD_SINGLE_CLASS_BOX)
 
78
                thing = new SSDSingleClassBox(v, g, 0, 0); 
 
79
        else if (classNr==Code::CBD_SINGLE_CLASS_BOX)
 
80
                thing = new CBSingleClassBox(v, g, 0, 0); 
 
81
        else if (classNr==Code::SSD_SINGLE_OBJECT_BOX)
 
82
                thing = new SSDSingleObjectBox(v, g, 0, 0); 
 
83
        else if (classNr==Code::TEXT_BOX)
 
84
                thing = new TextBox(v, g, 0, 0); 
 
85
        else if (classNr==Code::NOTE_BOX)
 
86
                thing = new NoteBox(v, g, 0, 0); 
 
87
 
 
88
        // lines
 
89
        else if (classNr==Code::T4_TLIST_LINE)
 
90
                thing = new T4TListLine(v, g, 0, 0, 0);
 
91
        else if (classNr==Code::C2R2_MLIST_LINE)
 
92
                thing = new C2R2MListLine(v, g, 0, 0, 0);
 
93
        else if (classNr==Code::LINE)
 
94
                thing = new Line(v, g, 0, 0, 0);
 
95
 
 
96
        // nodes
 
97
        else if (classNr==Code::UCD_ACTOR_NODE) //compatibility
 
98
                thing = new CBActor(cg);
 
99
        else if (classNr==Code::CBD_ACTOR_NODE)
 
100
                thing = new CBActor(cg);
 
101
        else if (classNr==Code::CBD_CLASS_NODE)
 
102
                thing = new CBClassNode(cg);
 
103
        else if (classNr==Code::CBD_OBJECT_NODE)
 
104
                thing = new CBObjectNode(cg);
 
105
 
 
106
        else if (classNr==Code::NOTE)
 
107
                thing = new Note(cg);
 
108
        else if (classNr==Code::COMMENT)
 
109
                thing = new Comment(cg);
 
110
 
 
111
        // edges
 
112
        else if (classNr==Code::CBD_CLASS_LINK_EDGE)
 
113
                thing = new CBClassLinkEdge(cg, 0, 0);
 
114
        else if (classNr==Code::CBD_OBJECT_LINK_EDGE)
 
115
                thing = new CBObjectLinkEdge(cg, 0, 0);
 
116
        else if (classNr==Code::COMMENT_LINK)
 
117
                thing = new CommentLink(cg, 0, 0);
 
118
 
 
119
        else
 
120
                error("%s, line %d: impl error: wrong class number %d\n", 
 
121
                        __FILE__, __LINE__, classNr);
 
122
        return thing;
 
123
}
 
124
 
 
125
Node *CBDiagram::CreateNode(){
 
126
        Node *node = 0;
 
127
        CBGraph *g = (CBGraph *)GetGraph();
 
128
 
 
129
        if (GetNodeType()==Code::CBD_ACTOR_NODE)
 
130
                node = new CBActor(g);
 
131
        else if (GetNodeType()==Code::CBD_CLASS_NODE)
 
132
                node = new CBClassNode(g);
 
133
        else if (GetNodeType()==Code::CBD_OBJECT_NODE)
 
134
                node = new CBObjectNode(g);
 
135
        else if (GetNodeType()==Code::NOTE)
 
136
                node = new Note(g);
 
137
        else if (GetNodeType()==Code::COMMENT)
 
138
                node = new Comment(g);
 
139
        else
 
140
                error( "%s, line %d: impl error: unknown node type\n", 
 
141
                        __FILE__, __LINE__);
 
142
        return node;
 
143
}
 
144
 
 
145
Edge *CBDiagram::CreateEdge(Subject *subj1, Subject* subj2){
 
146
        if (!CheckEdgeConstraints(subj1, subj2))
 
147
                return 0;
 
148
        Edge* edge = 0;
 
149
        CBGraph *g = (CBGraph *)GetGraph();
 
150
        if (GetEdgeType() == Code::CBD_CLASS_LINK_EDGE)
 
151
                edge = new CBClassLinkEdge(g, subj1, subj2);
 
152
        else if (GetEdgeType() == Code::CBD_OBJECT_LINK_EDGE)
 
153
                edge = new CBObjectLinkEdge(g, subj1, subj2);
 
154
        else if (GetEdgeType()==Code::COMMENT_LINK)
 
155
                edge = new CommentLink(g, subj1, subj2);
 
156
        else
 
157
                error( "%s, line %d: impl error: unknown edge type\n", 
 
158
                        __FILE__, __LINE__);
 
159
 
 
160
        return edge;
 
161
}
 
162
 
 
163
NodeShape *CBDiagram::CreateNodeShape(Node* node, int x, int y) {
 
164
        NodeShape* shape = 0;
 
165
        Grafport *g = GetDiagramViewer()->GetGrafport();
 
166
        ShapeView *v = GetDiagramViewer()->GetCurView();
 
167
 
 
168
        if (GetNodeShapeType() == Code::STICKMAN)
 
169
                shape = new StickMan(v, g, x, y);
 
170
 
 
171
        else if (GetNodeShapeType() == Code::OBJECT_STICKMAN) {
 
172
                SetNodeShapeType(Code::STICKMAN);       //reset
 
173
                shape = new StickMan(v, g, x, y);
 
174
                shape->GetName()->SetUnderlined(True);
 
175
        }
 
176
 
 
177
        else if (GetNodeShapeType() == Code::ACTOR_OBJECT_BOX) {
 
178
                SetNodeShapeType(Code::UCD_SINGLE_CLASS_BOX);   //reset
 
179
                shape = new UCDSingleClassBox(v, g, x, y);
 
180
                shape->GetName()->SetUnderlined(True);
 
181
        }
 
182
 
 
183
        else if (GetNodeShapeType() == Code::SSD_SINGLE_OBJECT_BOX)
 
184
                shape = new SSDSingleObjectBox(v, g, x, y); 
 
185
        else if (GetNodeShapeType() == Code::CBD_SINGLE_CLASS_BOX)
 
186
                        shape = new CBSingleClassBox(v, g, x, y);
 
187
        else if (GetNodeShapeType() == Code::SSD_SINGLE_CLASS_BOX)
 
188
                        shape = new SSDSingleClassBox(v, g, x, y);
 
189
 
 
190
        else if (GetNodeShapeType() == Code::UCD_SINGLE_CLASS_BOX)
 
191
                shape = new UCDSingleClassBox(v, g, x, y);
 
192
        else if (GetNodeShapeType() == Code::NOTE_BOX)
 
193
                shape = new NoteBox(v, g, x, y);
 
194
        else if (GetNodeShapeType() == Code::TEXT_BOX)
 
195
                shape = new TextBox(v, g, x, y);
 
196
        else
 
197
                error( "%s, line %d: impl error: "
 
198
                        "node shape type doesn't exist\n", __FILE__, __LINE__);
 
199
        
 
200
        if (check(shape)) {
 
201
                shape->SetSubject(node);
 
202
                shape->SetTextShape();
 
203
        }
 
204
        return shape;
 
205
}
 
206
 
 
207
Line *CBDiagram::CreateLine(Edge* edge, GShape *from, GShape *to, List<Point*> *l) {
 
208
        Grafport *g = GetDiagramViewer()->GetGrafport();
 
209
        ShapeView *v = GetDiagramViewer()->GetCurView();
 
210
        Line* line = 0;
 
211
        if (GetLineType()== Code::T4_TLIST_LINE)
 
212
                line = new C2R2MListLine(v, g, from, to, l, IsCurve());
 
213
        else if (GetLineType()==Code::C2R2_MLIST_LINE)
 
214
                line = new C2R2MListLine(v, g, from, to, l, IsCurve());
 
215
        else if (GetLineType()== Code::LINE)
 
216
                line = new Line(v, g, from, to, l, IsCurve());
 
217
        else
 
218
                error( "%s, line %d: impl error: "
 
219
                        "edge shape type does not exist\n", __FILE__, __LINE__);
 
220
        if (check(line)) {
 
221
                line->SetSubject(edge);
 
222
                if (edge->GetClassType()==Code::CBD_CLASS_LINK_EDGE) {
 
223
                        line->GetName()->SetUnderlined(False);
 
224
                }
 
225
                line->SetTextShape();
 
226
                line->SetEnd1(GetLineEnd1());
 
227
                line->SetEnd2(GetLineEnd2());
 
228
        }
 
229
        return line;
 
230
}
 
231
 
 
232
void CBDiagram::UpdateNodeType(int num) {
 
233
        ((DiagramWindow *)GetMainWindow())->SetNodeName(num);
 
234
        switch (num) {
 
235
        case 1: SetNodeType(Code::CBD_ACTOR_NODE);
 
236
                SetNodeShapeType(Code::STICKMAN);
 
237
                SetNodeLineStyle(LineStyle::SOLID);
 
238
                break;
 
239
        case 2: SetNodeType(Code::CBD_ACTOR_NODE);
 
240
                SetNodeShapeType(Code::UCD_SINGLE_CLASS_BOX);
 
241
                SetNodeLineStyle(LineStyle::SOLID);
 
242
                break;
 
243
        case 3: SetNodeType(Code::CBD_CLASS_NODE);
 
244
                SetNodeShapeType(Code::SSD_SINGLE_CLASS_BOX);
 
245
                SetNodeLineStyle(LineStyle::SOLID);
 
246
                break;
 
247
        case 4: SetNodeType(Code::COMMENT);
 
248
                SetNodeShapeType(Code::TEXT_BOX);
 
249
                SetNodeLineStyle(LineStyle::INVISIBLE);
 
250
                break;
 
251
        case 5: SetNodeType(Code::CBD_ACTOR_NODE);
 
252
                SetNodeShapeType(Code::OBJECT_STICKMAN);
 
253
                SetNodeLineStyle(LineStyle::SOLID);
 
254
                break;
 
255
        case 6: SetNodeType(Code::CBD_ACTOR_NODE);
 
256
                SetNodeShapeType(Code::ACTOR_OBJECT_BOX);
 
257
                SetNodeLineStyle(LineStyle::SOLID);
 
258
                break;
 
259
        case 7: SetNodeType(Code::CBD_OBJECT_NODE);
 
260
                SetNodeShapeType(Code::SSD_SINGLE_OBJECT_BOX);
 
261
                SetNodeLineStyle(LineStyle::SOLID);
 
262
                break;
 
263
        case 8: SetNodeType(Code::NOTE);
 
264
                SetNodeShapeType(Code::NOTE_BOX);
 
265
                SetNodeLineStyle(LineStyle::SOLID);
 
266
                break;
 
267
        default:
 
268
                error("%s, line %d: impl error: "
 
269
                        "unknown node type selected\n", __FILE__,__LINE__);
 
270
        }
 
271
}
 
272
 
 
273
 
 
274
void CBDiagram::UpdateEdgeType(int num) {
 
275
        ((DiagramWindow *)GetMainWindow())->SetEdgeName(num);
 
276
        switch(num) {
 
277
        case 1: SetEdgeType(Code::CBD_CLASS_LINK_EDGE);
 
278
                SetLineType(Code::T4_TLIST_LINE);
 
279
                SetEdgeLineStyle(LineStyle::SOLID);
 
280
                SetLineEnd1(LineEnd::EMPTY);
 
281
                SetLineEnd2(LineEnd::EMPTY);
 
282
                break;
 
283
        case 2: SetEdgeType(Code::COMMENT_LINK);
 
284
                SetLineType(Code::LINE);
 
285
                SetEdgeLineStyle(LineStyle::WIDE_DOTTED);
 
286
                SetLineEnd1(LineEnd::EMPTY);
 
287
                SetLineEnd2(LineEnd::EMPTY);
 
288
                break;
 
289
        case 3: SetEdgeType(Code::CBD_OBJECT_LINK_EDGE);
 
290
                SetLineType(Code::T4_TLIST_LINE);
 
291
                SetEdgeLineStyle(LineStyle::SOLID);
 
292
                SetLineEnd1(LineEnd::EMPTY);
 
293
                SetLineEnd2(LineEnd::EMPTY);
 
294
                break;
 
295
        default:
 
296
                error("%s, line %d: impl error: "
 
297
                        "unknown edge type selected\n", __FILE__,__LINE__);
 
298
        }
 
299
}
 
300
 
 
301
 
 
302
bool CBDiagram::SetText(TextShape *t, const string *s) {
 
303
        const string *d = t->GetDescription();
 
304
        Subject *subj = t->GetParent()->GetSubject();
 
305
        if (*d == "Stereotype" && subj->GetClassType()==Code::CBD_CLASS_NODE)
 
306
                return SetStereotype((CBClassNode*)subj, s);
 
307
        else if (*d == "Properties" && subj->GetClassType()==Code::CBD_CLASS_NODE)
 
308
                return SetProperties((CBClassNode*)subj, s);
 
309
        else if (*d == "Message")
 
310
                return SetMessage(subj, s, t->GetSequence());
 
311
        else
 
312
                return ERDiagram::SetText(t, s);
 
313
}
 
314
 
 
315
 
 
316
bool CBDiagram::SetStereotype(CBClassNode *object, const string *s) {
 
317
        (void)object->SetStereotype(s);
 
318
        // update all shapes in the viewer.
 
319
        List<GShape *> shapes;
 
320
        GetDiagramViewer()->GetShapes(object, &shapes);
 
321
        for (check(shapes.first()); !shapes.done(); shapes.next()) {
 
322
                const string *str = object->GetStereotype();
 
323
                GShape *shape = shapes.cur();
 
324
                if (check(!shape->IsTextShape())) {
 
325
                        if (shape->GetClassType()==Code::SSD_SINGLE_CLASS_BOX) { 
 
326
                                SSDSingleClassBox *bs = (SSDSingleClassBox *)shape;
 
327
                                bs->UpdateStereotypeLabelString(str); 
 
328
                        }
 
329
                        if (GetDiagramViewer()->IsAutoResize())
 
330
                                shape->AdjustSize();
 
331
                }
 
332
        }
 
333
        IncChanges();
 
334
        return True;
 
335
}
 
336
 
 
337
 
 
338
bool CBDiagram::SetProperties(CBClassNode *object, const string *s) {
 
339
        (void) object->SetProperties(s);
 
340
        // update all shapes in the viewer.
 
341
        List<GShape *> shapes;
 
342
        GetDiagramViewer()->GetShapes(object, &shapes);
 
343
        for (check(shapes.first()); !shapes.done(); shapes.next()) {
 
344
                const string *str = object->GetProperties();
 
345
                GShape *shape = shapes.cur();
 
346
                if (check(!shape->IsTextShape())) {
 
347
                        if (shape->GetClassType()==Code::SSD_SINGLE_CLASS_BOX) { 
 
348
                                SSDSingleClassBox *bs = (SSDSingleClassBox *)shape;
 
349
                                bs->UpdatePropertiesLabelString(str); 
 
350
                        }
 
351
                        if (GetDiagramViewer()->IsAutoResize())
 
352
                                shape->AdjustSize();
 
353
                }
 
354
        }
 
355
        IncChanges();
 
356
        return True;
 
357
}
 
358
 
 
359
bool CBDiagram::SetMessage(Subject *subj, const string *s, unsigned nr) {
 
360
        List<GShape *> shapes;
 
361
        GetDiagramViewer()->GetShapes(subj, &shapes);
 
362
 
 
363
        unsigned m = nr;
 
364
        // split string in different one line strings.
 
365
        string ss(*s);
 
366
        char *str = (char *)ss.getstr();
 
367
        char empty[2] = "";
 
368
        char *x = strtok(str, "\r");
 
369
        if (x == 0)
 
370
                x = empty;
 
371
        while (x != 0) {
 
372
                string *ns = new string(x);
 
373
                bool update = True;
 
374
                Subject::NameErrType result;
 
375
                unsigned numItems = ((CBObjectLinkEdge *)subj)->NrOfMessages();
 
376
                bool toggle = False;    //default
 
377
        for (check(shapes.first()); !shapes.done(); shapes.next()) {
 
378
                        GShape *shape = shapes.cur();
 
379
                        if (shape->GetClassType() == Code::C2R2_MLIST_LINE) {
 
380
                                Point from = *(((Line *)shape)->GetFromShape()->GetPosition());
 
381
                                Point to = *(((Line *)shape)->GetToShape()->GetPosition());
 
382
                                if (from.x > to.x)
 
383
                                        toggle = True;
 
384
                        }       
 
385
                }
 
386
                if ((m == nr) && (m < numItems)) {
 
387
                        result = ((CBObjectLinkEdge *)subj)->SetMessage(ns, m, True, toggle);
 
388
                }
 
389
                else {
 
390
                        result = ((CBObjectLinkEdge *)subj)->SetMessage(ns, m, False, toggle);
 
391
                        update = False;
 
392
                }
 
393
 
 
394
                // update and redraw  the shapes.
 
395
                if (check(shapes.first())) {
 
396
                        do {
 
397
                                Shape *shape = shapes.cur();
 
398
                                ((C2R2MListLine *)shape)->UpdateTListTextShape(ns,m);
 
399
                        } while (shapes.next());
 
400
                }
 
401
                else 
 
402
                        return False;
 
403
 
 
404
                m++;
 
405
                x = strtok(0, "\r");
 
406
                delete ns;
 
407
        }
 
408
        return True;
 
409
}
 
410
 
 
411
bool CBDiagram::SetRoleName(Subject *subject, const string *s, unsigned ) {
 
412
        List<GShape *> shapes;
 
413
        GetDiagramViewer()->GetShapes(subject, &shapes);
 
414
        bool succes = True;
 
415
        if (!succes) {
 
416
                string txt = "'" + *s + "' is not a possible role name";
 
417
                ShowDialog(MessageDialog::ERROR, "Error", &txt);
 
418
                return False;
 
419
        }
 
420
        return True;
 
421
}
 
422
 
 
423
 
 
424
void CBDiagram::CheckDocument() {
 
425
        chkbuf = "";
 
426
        unsigned total = 0;
 
427
        // Check that classes are named.
 
428
        total += cbChecks->CheckNamelessNodes(Code::CBD_CLASS_NODE, chkbuf);
 
429
        // Check that object classes are named.
 
430
        total += cbChecks->CheckNamelessNodes(Code::CBD_OBJECT_NODE, chkbuf);
 
431
 
 
432
        ReportCheck(total, &chkbuf);
 
433
}