Beispiel #1
0
 private void OnExecuteCloseCommand()
 {
     //umlDiagram.RemoveClassesFromDiagram(new List<UmlClass> { Class });
     umlDiagram.RemoveClassesFromModel(new List <UmlClass> {
         Class
     });
 }
        private static void ProcessToken(
            string token,
            ref State state,
            ref bool invert,
            ref List <UmlClass> selectedClasses,
            UmlDiagram diagram
            )
        {
            string lowerToken = token.ToLower();

            switch (lowerToken)
            {
            case "not":
                invert = true;
                break;

            case "clearmodel":
                if (state == State.None)
                {
                    diagram.ClearModel();
                }
                break;

            case "cleardiagram":
                if (state == State.None)
                {
                    diagram.ClearDiagram();
                }
                break;

            case "showmembers":
                if (state == State.None)
                {
                    diagram.ShowsMembers = true;
                }
                break;

            case "hidemembers":
                if (state == State.None)
                {
                    diagram.ShowsMembers = false;
                }
                break;

            case "find":
                if (state == State.None)
                {
                    state = State.FindKeyword;
                }
                break;

            //case "showall":
            //    if (state == State.None) {
            //        diagram.ShowAll();
            //    }
            //    break;
            case "startattracting":
                if (state == State.None)
                {
                    diagram.StartAttracting();
                }
                break;

            case "stopattracting":
                if (state == State.None)
                {
                    diagram.StopAttracting();
                }
                break;

            case "pause":
                diagram.PauseSimulation();
                break;

            case "resume":
                diagram.ResumeSimulation();
                break;

            case "show":
                //
                // Show is the default command
                //
                break;

            case "showrelation":
                if (state == State.None)
                {
                    state = State.ShowRelation1;
                }
                break;

            case "hide":
                if (state == State.None)
                {
                    state = State.Hide;
                }
                break;

            case "delete":
                if (state == State.None)
                {
                    state = State.Delete;
                }
                break;

            case "hasnote":
                if (state == State.None)
                {
                    state = State.Note;
                }
                break;

            case "hasm":
            case "hasmethod":
                if (state == State.None)
                {
                    state = State.HasMethod;
                }
                break;

            case "is":
                state = State.Inheritance;
                break;

            case "has":
                state = State.Association;
                break;

            case "hasn":
                state = State.AssociationN;
                break;

            case "implements":
                state = State.ImplementsInterface;
                break;

            case "dependson":
                state = State.Dependence;
                break;

            case "owns":
                state = State.Composition;
                break;

            case "ownsn":
                state = State.CompositionN;
                break;

            case "contains":
                state = State.Aggregation;
                break;

            case "containsn":
                state = State.AggregationN;
                break;

            default:
                string name = null;
                if (state == State.Association || state == State.AssociationN ||
                    state == State.Aggregation || state == State.AggregationN ||
                    state == State.Composition || state == State.CompositionN)
                {
                    //
                    // Check for attributes and operations
                    // Attribute: a has name:type
                    // Operation: a has name():type
                    //
                    string type         = null;
                    bool   isOperation  = false;
                    int    indexOfColon = token.IndexOf(":", StringComparison.InvariantCulture);
                    if (indexOfColon > -1)
                    {
                        //
                        // There is a ':' in the string, indicating an attribute or operation
                        //
                        name = token.Substring(0, indexOfColon);
                        if (name.EndsWith("()"))
                        {
                            isOperation = true;
                            name        = name.Substring(0, name.Length - 2);
                        }
                        if (indexOfColon + 1 < token.Length)
                        {
                            type = token.Substring(indexOfColon + 1);
                        }
                        //
                        // If type == null (i.e. a has b:), add an attribute or operation and break.
                        //
                        if (type == null || ValueTypes.Contains(type.ToLower()))
                        {
                            foreach (var umlClass in selectedClasses)
                            {
                                if (isOperation)
                                {
                                    if (!invert)
                                    {
                                        diagram.CreateModelOperation(umlClass, name, type);
                                    }
                                    else
                                    {
                                        diagram.RemoveModelOperation(umlClass, name, type);
                                    }
                                }
                                else
                                {
                                    if (!invert)
                                    {
                                        diagram.CreateModelAttribute(umlClass, name, type);
                                    }
                                    else
                                    {
                                        diagram.RemoveModelAttribute(umlClass, name, type);
                                    }
                                }
                            }
                            break;     // ?
                        }
                        // Type is not a value type, proceed with processing Collection.
                        token = type;
                        if (isOperation)
                        {
                            name += "()";
                        }
                    }
                }

                // TODO: je hebt nu method nodes aangemaakt; wat je beter kunt doen:
                // je kunt al operations maken met a has name():type. Nu hoef je alleen nog maar een switch maken waarmee je tussen operatie-in-class-blokje of apart-blokje-voor-operation
                // kunt togglen. Maak er een view specifiek dingetje van dus ipv het model aan te passen.
                // Niet triviaal trouwens: er moeten wel nodes bij komen als je de operaties als blokjes laat zien.
                // Wel moet deze parser met Nodes gaan werken want je wil net zo goed notes kunnen selecteren.

                List <UmlClass> operand = null;
                if (state != State.Note && state != State.FindKeyword && state != State.HasMethod)
                {
                    operand = ProcessCollection(token, diagram.Model).Cast <UmlClass>().ToList();
                }

                switch (state)
                {
                case State.None:
                    diagram.Show(operand);
                    selectedClasses = operand;
                    break;

                case State.Note:
                    foreach (var startClass in selectedClasses)
                    {
                        diagram.CreateModelNote(startClass, token);
                    }
                    state = State.None;
                    break;

                case State.HasMethod:
                    foreach (var startClass in selectedClasses)
                    {
                        diagram.CreateModelMethodNode(startClass, token);
                    }
                    state = State.None;
                    break;

                case State.FindKeyword:
                    var classes =
                        FindClassesWithCertainFields(token, diagram.Model).Concat(
                            FindClassesWithCertainProperties(token, diagram.Model)).ToList();
                    diagram.Show(classes);
                    state = State.None;
                    break;

                case State.ShowRelation1:
                    selectedClasses = operand;
                    state           = State.ShowRelation2;
                    break;

                case State.ShowRelation2:
                    diagram.ShowRelation(selectedClasses, operand);
                    state = State.None;
                    break;

                case State.Hide:
                    diagram.RemoveClassesFromDiagram(operand);
                    state = State.None;
                    break;

                case State.Delete:
                    diagram.RemoveClassesFromModel(operand);
                    state = State.None;
                    break;

                default:
                    diagram.Show(operand);
                    foreach (var startClass in selectedClasses)
                    {
                        foreach (var endClass in operand)
                        {
                            switch (state)
                            {
                            case State.Inheritance:
                                if (!invert)
                                {
                                    diagram.CreateModelRelation(RelationType.Inheritance, startClass, endClass, false);
                                }
                                else
                                {
                                    diagram.RemoveModelRelation(startClass, endClass, RelationType.Inheritance, name);
                                }
                                break;

                            case State.Composition:
                                if (!invert)
                                {
                                    diagram.CreateModelRelation(RelationType.Composition, endClass, startClass, false, name);
                                }
                                else
                                {
                                    diagram.RemoveModelRelation(endClass, startClass, RelationType.Composition, name);
                                }
                                break;

                            case State.CompositionN:
                                if (!invert)
                                {
                                    diagram.CreateModelRelation(RelationType.Composition, endClass, startClass, true, name);
                                }
                                else
                                {
                                    diagram.RemoveModelRelation(endClass, startClass, RelationType.Composition, name);
                                }
                                break;

                            case State.Aggregation:
                                if (!invert)
                                {
                                    diagram.CreateModelRelation(RelationType.Aggregation, endClass, startClass, false, name);
                                }
                                else
                                {
                                    diagram.RemoveModelRelation(endClass, startClass, RelationType.Aggregation, name);
                                }
                                break;

                            case State.AggregationN:
                                if (!invert)
                                {
                                    diagram.CreateModelRelation(RelationType.Aggregation, endClass, startClass, true, name);
                                }
                                else
                                {
                                    diagram.RemoveModelRelation(endClass, startClass, RelationType.Aggregation, name);
                                }
                                break;

                            case State.Association:
                                if (!invert)
                                {
                                    diagram.CreateModelRelation(RelationType.Association, startClass, endClass, false, name);
                                }
                                else
                                {
                                    diagram.RemoveModelRelation(startClass, endClass, RelationType.Association, name);
                                }
                                break;

                            case State.AssociationN:
                                if (!invert)
                                {
                                    diagram.CreateModelRelation(RelationType.Association, startClass, endClass, true, name);
                                }
                                else
                                {
                                    diagram.RemoveModelRelation(startClass, endClass, RelationType.Association, name);
                                }
                                break;

                            case State.ImplementsInterface:
                                if (!invert)
                                {
                                    diagram.CreateModelRelation(RelationType.ImplementsInterface, startClass, endClass, false);
                                }
                                else
                                {
                                    diagram.RemoveModelRelation(startClass, endClass, RelationType.ImplementsInterface, name);
                                }
                                break;

                            case State.Dependence:
                                if (!invert)
                                {
                                    diagram.CreateModelRelation(RelationType.Dependence, startClass, endClass, false, name);
                                }
                                else
                                {
                                    diagram.RemoveModelRelation(startClass, endClass, RelationType.Dependence, name);
                                }
                                break;

                            case State.HasMethod:
                                if (!invert)
                                {
                                    diagram.CreateModelRelation(RelationType.HasMethod, startClass, endClass, false, name);
                                }
                                else
                                {
                                    diagram.RemoveModelRelation(startClass, endClass, RelationType.HasMethod, name);
                                }
                                break;
                            }
                        }
                    }
                    state = State.None;
                    break;
                }
                break;
            }
        }