Beispiel #1
0
        protected void HandleAddTableToolStripButtonClick(object sender, EventArgs e)
        {
            if (_EditorCanvas == null)
            {
                return;
            }

            var dialog = new CreateTableForm();

            if (dialog.ShowDialog(this) != DialogResult.OK)
            {
                return;
            }

            var colCount = dialog.ColumnCount;
            var rowCount = dialog.RowCount;

            var target       = _EditorCanvas.RootEditor.Content;
            var bounds       = new Rectangle(32, 32, 100, 100);
            var modelFactory = new DelegatingModelFactory <MemoTable>(
                () => {
                var ret = MemoFactory.CreateTable();
                return(ret);
            }
                );

            var req = new CreateNodeRequest();

            req.ModelFactory = modelFactory;
            req.Bounds       = bounds;

            var createdBounds = Rectangle.Empty;
            var cmd1          = target.GetCommand(req) as CreateNodeCommand;
            var cmd2          = new DelegatingCommand(
                () => {
                var created = cmd1.CreatedEditor;
                if (created != null)
                {
                    var table = created.Model as MemoTable;
                    for (int i = 0; i < colCount; ++i)
                    {
                        table.AddColumn();
                    }
                    for (int i = 0; i < rowCount; ++i)
                    {
                        table.AddRow();
                    }
                }
            },
                () => {},
                () => {
                // todo: これでもundo,redoで行,列の幅が復元できない
                // Boundsは復元される
                var created           = cmd1.CreatedEditor;
                created.Figure.Bounds = createdBounds;
            }
                );

            var cmd = cmd1.Chain(cmd2);

            _EditorCanvas.CommandExecutor.Execute(cmd);
            createdBounds = cmd1.CreatedEditor.Figure.Bounds;

            dialog.Dispose();

            FocusEditorCanvas();

            //var cmd = target.RequestCreateNode(modelFactory, bounds) as CreateNodeCommand;
            //var created = cmd.CreatedEditor;
            //if (created != null) {
            //    var colCount = 3;
            //    var rowCount = 3;
            //    var table = created.Model as MemoTable;
            //    for (int i = 0; i < colCount; ++i) {
            //        table.AddColumn();
            //    }
            //    for (int i = 0; i < rowCount; ++i) {
            //        foreach (var cell in table.AddRow().Cells) {
            //            var run = new Run("テスト" + i);
            //            var para = new Paragraph(run);
            //            para.Padding = new Insets();
            //            para.HorizontalAlignment = HorizontalAlignment.Left;
            //            var stext = new StyledText(para);
            //            stext.VerticalAlignment = VerticalAlignment.Center;
            //            stext.Font = _facade.Settings.GetDefaultMemoContentFont();
            //            cell.StyledText = stext;
            //        }
            //    }
            //}
        }
        public static IEnumerable <IEditor> AddFile(IEditor target, Point location, string filePath, bool embed, bool useCommandExecutor, bool arrange)
        {
            var isFile      = File.Exists(filePath);
            var isDirectory = Directory.Exists(filePath);

            if (isFile || isDirectory)
            {
                var fileName = Path.GetFileName(filePath);
                var path     = filePath;

                var embeddedId = "";

                if (!isFile && !isDirectory)
                {
                    MessageBox.Show("ファイルが見つかりませんでした。", "ファイルエラー");
                    return(EmptyEditors);
                }

                /// 埋め込む場合はEmbeddedFileRootにコピー
                if (embed && isFile)
                {
                    embeddedId = Guid.NewGuid().ToString();
                    path       = Path.Combine(embeddedId, fileName);
                    var fullPath = Path.Combine(MemopadConsts.EmbeddedFileRoot, path);
                    try {
                        PathUtil.EnsureDirectoryExists(Path.GetDirectoryName(fullPath));
                        File.Copy(filePath, fullPath);
                    } catch {
                        Logger.Warn("File copy failed. source=" + filePath + ",target=" + fullPath);
                        if (Directory.Exists(fullPath))
                        {
                            Directory.Delete(Path.GetDirectoryName(fullPath), true);
                        }
                        MessageBox.Show("ファイルのコピーに失敗しました。", "ファイルコピーエラー");
                    }
                }

                var bounds       = new Rectangle(location, new Size(1, 1));
                var modelFactory = new DelegatingModelFactory <MemoFile>(
                    () => {
                    var ret        = MemoFactory.CreateFile();
                    ret.Name       = fileName;
                    ret.Path       = path;
                    ret.IsEmbedded = embed && isFile;
                    ret.EmbeddedId = embeddedId;
                    return(ret);
                }
                    );

                if (useCommandExecutor && arrange)
                {
                    target.Site.CommandExecutor.BeginChain();
                }

                var existingEditorBounds = default(Rectangle[]);
                if (arrange)
                {
                    existingEditorBounds = target.Children.Select(e => e.Figure.Bounds).ToArray();
                }

                var req = new CreateNodeRequest();
                req.ModelFactory     = modelFactory;
                req.Bounds           = bounds;
                req.AdjustSizeToGrid = true;
                var cmd = target.GetCommand(req) as CreateNodeCommand;

                var sizeCmd = new DelegatingCommand(
                    () => {
                    var node = cmd.CreatedEditor.Figure as INode;
                    if (node != null)
                    {
                        node.AdjustSize();
                    }
                },
                    () => {
                    cmd.CreatedEditor.Figure.Bounds = bounds;
                }
                    );

                var chain = cmd.Chain(sizeCmd);

                if (useCommandExecutor)
                {
                    target.Site.CommandExecutor.Execute(chain);
                }
                else
                {
                    chain.Execute();
                }

                if (arrange)
                {
                    var newLoc = RectUtil.GetPreferredLocation(
                        cmd.CreatedEditor.Figure.Bounds,
                        existingEditorBounds,
                        target.Root.Figure.Right,
                        MemopadConsts.DefaultCaretPosition.X,
                        MemopadConsts.DefaultCaretPosition.Y
                        );
                    var move = new ChangeBoundsCommand(
                        cmd.CreatedEditor,
                        (Size)newLoc - (Size)cmd.CreatedEditor.Figure.Location,
                        Size.Empty,
                        Directions.None,
                        new [] { cmd.CreatedEditor }
                        );
                    if (useCommandExecutor)
                    {
                        target.Site.CommandExecutor.Execute(move);
                    }
                    else
                    {
                        move.Execute();
                    }
                    if (useCommandExecutor)
                    {
                        target.Site.CommandExecutor.EndChain();
                    }
                }

                return(new[] { cmd.CreatedEditor });
            }

            return(EmptyEditors);
        }