Ejemplo n.º 1
0
        void ICommandClient.DoCommand(object commandTag)
        {
            var target = TreeControlAdapter.LastHit;

            // If the command isn't one of our immediate commands, it
            // might belong to one of our sub providers.
            // Try to cast the targetted node to a command client and
            // execute from there
            if (!(commandTag is Command))
            {
                var targetClient = target.As <ICommandClient>();
                if (targetClient != null)
                {
                    targetClient.DoCommand(commandTag);
                }
                return;
            }

            IDocument gameDocument = null;
            string    filePath     = null;

            IGame game = target.As <IGame>();

            if (game == null)
            {
                GameReference gameRef = target.As <GameReference>();
                if (gameRef != null)
                {
                    game = gameRef.Target;
                }
            }

            if (game != null)
            {
                gameDocument = game.As <IDocument>();
            }

            switch ((Command)commandTag)
            {
            case Command.CreateNewSubGame:
                if (gameDocument != null)
                {
                    filePath = Util.GetFilePath(m_fileFilter,
                                                System.IO.Path.GetDirectoryName(gameDocument.Uri.LocalPath), true);
                    if (!string.IsNullOrEmpty(filePath))
                    {
                        try
                        {
                            if (!m_gameEditor.Info.IsCompatiblePath(filePath))
                            {
                                throw new Exception("Incompatible file type " + filePath);
                            }

                            Uri ur = new Uri(filePath);
                            if (m_gameDocumentRegistry.FindDocument(ur) != null)
                            {
                                throw new Exception(filePath + " is already open");
                            }
                            GameDocument subGame = GameDocument.OpenOrCreate(ur, m_schemaLoader);
                            subGame.Dirty = true;
                            GameReference gameRef = GameReference.CreateNew(subGame);

                            // try all implementation of IHierarchical until one works
                            var parent = game.AsAll <IHierarchical>();
                            foreach (var p in parent)
                            {
                                if (p.AddChild(gameRef))
                                {
                                    break;
                                }
                            }

                            // because we performing this operation outside of TransactionContext
                            // we must set Document Dirty flag.
                            gameDocument.Dirty = true;
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(m_mainWindow.DialogOwner, ex.Message);
                        }
                    }
                }
                break;

            case Command.AddSubGame:
                if (gameDocument != null)
                {
                    filePath = Util.GetFilePath(m_fileFilter,
                                                System.IO.Path.GetDirectoryName(gameDocument.Uri.LocalPath),
                                                false);

                    if (!string.IsNullOrEmpty(filePath))
                    {
                        try
                        {
                            if (!m_gameEditor.Info.IsCompatiblePath(filePath))
                            {
                                throw new Exception("Incompatible file type " + filePath);
                            }

                            Uri ur = new Uri(filePath);
                            if (m_gameDocumentRegistry.FindDocument(ur) != null)
                            {
                                throw new Exception(filePath + " is already open");
                            }

                            GameReference gameRef = GameReference.CreateNew(ur);
                            gameRef.Resolve();

                            // try all implementation of IHierarchical until one works
                            var parent = game.AsAll <IHierarchical>();
                            foreach (var p in parent)
                            {
                                if (p.AddChild(gameRef))
                                {
                                    break;
                                }
                            }

                            // because we performing this operation outside of TransactionContext
                            // we must set Document Dirty flag.
                            gameDocument.Dirty = true;
                            RefreshLayerContext();
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(m_mainWindow.DialogOwner, ex.Message);
                        }
                    }
                }
                break;

            case Command.Exclude:
            {
                GameReference gameRef = target.As <GameReference>();
                if (gameRef == null)
                {
                    break;
                }

                gameDocument = gameRef.DomNode.Parent.Cast <IDocument>();
                IGameDocument subDoc = gameRef.Target.Cast <IGameDocument>();

                bool exclue = true;
                bool save   = false;
                if (subDoc.Dirty)
                {
                    string       msg       = "Save changes\r\n" + subDoc.Uri.LocalPath;
                    DialogResult dlgResult =
                        MessageBox.Show(m_mainWindow.DialogOwner, msg, m_mainWindow.Text
                                        , MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);

                    save   = dlgResult == DialogResult.Yes;
                    exclue = dlgResult != DialogResult.Cancel;
                }

                if (save)
                {
                    subDoc.Save(subDoc.Uri, m_schemaLoader);
                }

                if (exclue)
                {
                    gameRef.DomNode.RemoveFromParent();
                    // because we performing this operation outside of TransactionContext
                    // we must set Document Dirty flag.
                    gameDocument.Dirty = true;
                    UpdateGameObjectReferences();
                    RefreshLayerContext();
                }
            }
            break;

            case Command.Resolve:
            {
                bool          madeChange = false;
                GameReference gameRef    = target.As <GameReference>();
                if (gameRef != null)
                {
                    gameRef.Resolve();
                    madeChange = true;
                }
                else
                {
                    var resolveable = target.As <IResolveable>();
                    if (resolveable != null && !resolveable.IsResolved())
                    {
                        resolveable.Resolve();
                        madeChange = true;
                    }
                }

                if (madeChange)
                {
                    TreeControlAdapter.Refresh(target);
                    RefreshLayerContext();
                }
            }
            break;

            case Command.Unresolve:
            {
                try
                {
                    GameReference gameRef = target.As <GameReference>();
                    if (gameRef != null)
                    {
                        IGameDocument subDoc    = gameRef.Target.Cast <IGameDocument>();
                        bool          unresolve = true;
                        bool          save      = false;
                        if (subDoc.Dirty)
                        {
                            string       msg       = "Save changes\r\n" + subDoc.Uri.LocalPath;
                            DialogResult dlgResult =
                                MessageBox.Show(m_mainWindow.DialogOwner, msg, m_mainWindow.Text
                                                , MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);

                            save      = dlgResult == DialogResult.Yes;
                            unresolve = dlgResult != DialogResult.Cancel;
                        }
                        //cando = gameRef != null && gameRef.Target != null;
                        if (save)
                        {
                            subDoc.Save(subDoc.Uri, m_schemaLoader);
                        }
                        if (unresolve)
                        {
                            gameRef.Unresolve();
                            UpdateGameObjectReferences();
                            RefreshLayerContext();
                        }
                        TreeControlAdapter.Refresh(gameRef);
                    }
                    else
                    {
                        var resolveable = target.As <IResolveable>();
                        if (resolveable != null && resolveable.IsResolved())
                        {
                            resolveable.Unresolve();
                            RefreshLayerContext();
                            TreeControlAdapter.Refresh(target);
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(m_mainWindow.DialogOwner, ex.Message);
                }
            }
            break;

            case Command.CreateNewResolveable:
            {
                try
                {
                    var resolveable = target.As <IResolveable>();
                    if (resolveable != null && !resolveable.IsResolved() && resolveable.CanCreateNew())
                    {
                        resolveable.CreateAndResolve();
                        RefreshLayerContext();
                        TreeControlAdapter.Refresh(target);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(m_mainWindow.DialogOwner, ex.Message);
                }
            }
            break;

            case Command.SaveSubDoc:
            {
                try
                {
                    var resolveable = target.As <IResolveable>();
                    if (resolveable != null && resolveable.CanSave())
                    {
                        resolveable.Save(m_schemaLoader);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(m_mainWindow.DialogOwner, ex.Message);
                }
            }
            break;

            default:
                throw new ArgumentOutOfRangeException("commandTag");
            }
            m_designView.InvalidateViews();
            Refresh();
        }
Ejemplo n.º 2
0
        void ICommandClient.DoCommand(object commandTag)
        {
            IGame game = TreeControlAdapter.LastHit.As <IGame>();

            if (game == null)
            {
                GameReference gameRef = TreeControlAdapter.LastHit.As <GameReference>();
                game = gameRef.Target;
            }
            IDocument gameDocument = game.As <IDocument>();
            string    filePath     = null;

            switch ((Command)commandTag)
            {
            case Command.CreateNewSubGame:
                filePath = Util.GetFilePath(m_fileFilter,
                                            System.IO.Path.GetDirectoryName(gameDocument.Uri.LocalPath), true);
                if (!string.IsNullOrEmpty(filePath))
                {
                    try
                    {
                        if (!m_gameEditor.Info.IsCompatiblePath(filePath))
                        {
                            throw new Exception("Incompatible file type " + filePath);
                        }

                        Uri ur = new Uri(filePath);
                        if (m_gameDocumentRegistry.FindDocument(ur) != null)
                        {
                            throw new Exception(filePath + " is already open");
                        }
                        GameDocument subGame = GameDocument.OpenOrCreate(ur, m_schemaLoader);
                        subGame.Dirty = true;
                        GameReference gameRef = GameReference.CreateNew(subGame);
                        IHierarchical parent  = game.As <IHierarchical>();
                        parent.AddChild(gameRef);
                        // because we performing this operation outside of TransactionContext
                        // we must set Document Dirty flag.
                        gameDocument.Dirty = true;
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(m_mainWindow.DialogOwner, ex.Message);
                    }
                }
                break;

            case Command.AddSubGame:

                filePath = Util.GetFilePath(m_fileFilter,
                                            System.IO.Path.GetDirectoryName(gameDocument.Uri.LocalPath),
                                            false);

                if (!string.IsNullOrEmpty(filePath))
                {
                    try
                    {
                        if (!m_gameEditor.Info.IsCompatiblePath(filePath))
                        {
                            throw new Exception("Incompatible file type " + filePath);
                        }

                        Uri ur = new Uri(filePath);
                        if (m_gameDocumentRegistry.FindDocument(ur) != null)
                        {
                            throw new Exception(filePath + " is already open");
                        }

                        GameReference gameRef = GameReference.CreateNew(ur);
                        gameRef.Resolve();
                        IHierarchical parent = game.As <IHierarchical>();
                        parent.AddChild(gameRef);

                        // because we performing this operation outside of TransactionContext
                        // we must set Document Dirty flag.
                        gameDocument.Dirty = true;
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(m_mainWindow.DialogOwner, ex.Message);
                    }
                }
                break;

            case Command.Exclude:
            {
                GameReference gameRef = TreeControlAdapter.LastHit.As <GameReference>();
                gameDocument = gameRef.DomNode.Parent.Cast <IDocument>();
                GameDocument subDoc = gameRef.Target.Cast <GameDocument>();

                bool exclue = true;
                bool save   = false;
                if (subDoc.Dirty)
                {
                    string       msg       = "Save changes\r\n" + subDoc.Uri.LocalPath;
                    DialogResult dlgResult =
                        MessageBox.Show(m_mainWindow.DialogOwner, msg, m_mainWindow.Text
                                        , MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);

                    save   = dlgResult == DialogResult.Yes;
                    exclue = dlgResult != DialogResult.Cancel;
                }

                if (save)
                {
                    subDoc.Save(subDoc.Uri, m_schemaLoader);
                }

                if (exclue)
                {
                    gameRef.DomNode.RemoveFromParent();
                    // because we performing this operation outside of TransactionContext
                    // we must set Document Dirty flag.
                    gameDocument.Dirty = true;
                }
            }
            break;

            case Command.Resolve:
            {
                GameReference gameRef = TreeControlAdapter.LastHit.As <GameReference>();
                gameRef.Resolve();
                TreeControlAdapter.Refresh(gameRef);
            }
            break;

            case Command.Unresolve:
            {
                try
                {
                    GameReference gameRef   = TreeControlAdapter.LastHit.As <GameReference>();
                    GameDocument  subDoc    = gameRef.Target.Cast <GameDocument>();
                    bool          unresolve = true;
                    bool          save      = false;
                    if (subDoc.Dirty)
                    {
                        string       msg       = "Save changes\r\n" + subDoc.Uri.LocalPath;
                        DialogResult dlgResult =
                            MessageBox.Show(m_mainWindow.DialogOwner, msg, m_mainWindow.Text
                                            , MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);

                        save      = dlgResult == DialogResult.Yes;
                        unresolve = dlgResult != DialogResult.Cancel;
                    }
                    //cando = gameRef != null && gameRef.Target != null;
                    if (save)
                    {
                        subDoc.Save(subDoc.Uri, m_schemaLoader);
                    }
                    if (unresolve)
                    {
                        gameRef.Unresolve();
                    }
                    TreeControlAdapter.Refresh(gameRef);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(m_mainWindow.DialogOwner, ex.Message);
                }
            }
            break;

            default:
                throw new ArgumentOutOfRangeException("commandTag");
            }
            m_designView.InvalidateViews();
            Refresh();
        }