~ubuntu-branches/ubuntu/intrepid/tcm/intrepid

« back to all changes in this revision

Viewing changes to src/dg/reindexcmd.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 1995, Vrije Universiteit Amsterdam.
 
5
// Author: Frank Dehne (frank@cs.vu.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
 
19
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 
20
// 02111-1307, USA.
 
21
////////////////////////////////////////////////////////////////////////////////
 
22
#include "reindexcmd.h"
 
23
#include "diagram.h"
 
24
#include "diagramviewer.h"
 
25
#include "node.h"
 
26
#include "nodeshape.h"
 
27
 
 
28
ReindexCmd::ReindexCmd(ShapeView *v, List<Node *> *n): 
 
29
                Command(v->GetViewer()->GetDiagram(), v->GetViewer()) {
 
30
        for (n->first(); !n->done(); n->next()) {
 
31
                Diagram *d = v->GetViewer()->GetDiagram();
 
32
                if (d->HasIndexNode(n->cur()->GetClassType()) &&
 
33
                    *n->cur()->GetIndex() != "") 
 
34
                        nodes.add(n->cur());
 
35
        }
 
36
        if (nodes.first()) {
 
37
                nodes.cur()->GetParentIndex(&parent);
 
38
                do {
 
39
                        Node *node = nodes.cur();
 
40
                        string *s = new string(*node->GetIndex());
 
41
                        oldIndexes.add(s);
 
42
                } while (nodes.next());
 
43
        }
 
44
}
 
45
 
 
46
ReindexCmd::~ReindexCmd() {
 
47
        oldIndexes.clear();
 
48
}
 
49
 
 
50
void ReindexCmd::Execute() {
 
51
        // avoid index clashes.
 
52
        for (nodes.first(); !nodes.done(); nodes.next()) {
 
53
                string empty("");
 
54
                nodes.cur()->SetIndex(&empty);
 
55
        } 
 
56
        int n = 1;
 
57
        for (nodes.first(); !nodes.done(); nodes.next()) {
 
58
                string s = parent;
 
59
                Node *node = nodes.cur();
 
60
                if (s != "")
 
61
                        s += '.';
 
62
                s += n;
 
63
                n++;
 
64
                SetIndex(node, &s);
 
65
        } 
 
66
        GetDocument()->IncChanges();
 
67
        SetCmdDone(True);
 
68
        SayCommited();
 
69
}
 
70
 
 
71
void ReindexCmd::SetIndex(Node *node, const string *index) {
 
72
        List<GShape *> shapes;
 
73
        ((DiagramViewer *)GetViewer())->GetShapes(node, &shapes);
 
74
        Subject::NameErrType err = node->Node::SetIndex(index);
 
75
        if (err == Subject::OK) {
 
76
                for (shapes.first(); !shapes.done(); shapes.next()) {
 
77
                        NodeShape *t = (NodeShape *)shapes.cur();
 
78
                        if (((DiagramViewer *)GetViewer())->IsShowIndexes())
 
79
                                t->UpdateIndexLabel(index);
 
80
                        else {
 
81
                                string empty("");
 
82
                                t->UpdateIndexLabel(&empty);
 
83
                        }
 
84
                }
 
85
                if (!shapes.first()) {
 
86
                        error("%s, line %d: shape does not exist\n",
 
87
                                __FILE__, __LINE__);
 
88
                        return;
 
89
                }
 
90
        }
 
91
}
 
92
 
 
93
void ReindexCmd::UnExecute() {
 
94
        // avoid index clashes.
 
95
        for (nodes.first(); !nodes.done(); nodes.next()) {
 
96
                string empty("");
 
97
                nodes.cur()->SetIndex(&empty);
 
98
        } 
 
99
        if (oldIndexes.first() && nodes.first()) {
 
100
                do {
 
101
                        Node *node = nodes.cur();
 
102
                        string s = *oldIndexes.cur();
 
103
                        SetIndex(node, &s);
 
104
                } while (oldIndexes.next() && nodes.next());
 
105
        }
 
106
        GetDocument()->DecChanges();
 
107
        SetCmdDone(False);
 
108
        SayUndone();
 
109
}