Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

PlantUML Editor: A Fast and Simple UML Editor using WPF

, 11 Jun 2011 CPOL
A WPF smart client to generate UML diagrams from plain text using plantuml tool
PlantUmlEditor-src-v1.zip
GoogleCode
Article
plantumleditor
contextmenu.png
DesignTimeView.png
dotnetInstaller.png
dotnetInstaller_embed.png
jsmooth.png
sample_diagram.png
Screenshot.png
SolutionTree.png
PlantUmlEditor
Controls
.svn
entries
format
prop-base
props
text-base
ProgressBar.xaml.cs.svn-base
ProgressBar.xaml.svn-base
tmp
prop-base
props
text-base
CustomAnimation
.svn
all-wcprops
entries
prop-base
props
text-base
GridLengthAnimation.cs.svn-base
tmp
prop-base
props
text-base
DesignTimeData
.svn
all-wcprops
entries
prop-base
props
text-base
DiagramFiles.cs.svn-base
tmp
prop-base
props
text-base
Model
.svn
all-wcprops
entries
prop-base
props
text-base
DiagramFile.cs.svn-base
tmp
prop-base
props
text-base
PlantUmlEditor.csproj.user
Properties
.svn
all-wcprops
entries
prop-base
props
text-base
AssemblyInfo.cs.svn-base
Resources.Designer.cs.svn-base
Resources.resx.svn-base
Settings.Designer.cs.svn-base
Settings.settings.svn-base
tmp
prop-base
props
text-base
Settings.settings
samples
.svn
all-wcprops
entries
prop-base
props
text-base
sample activity.txt.svn-base
sample class.txt.svn-base
sample component.txt.svn-base
sample sequence.txt.svn-base
sample state.txt.svn-base
sample use case.txt.svn-base
tmp
prop-base
props
text-base
img
.svn
all-wcprops
entries
prop-base
activity_img20.png.svn-base
classes04.png.svn-base
component_img04.png.svn-base
sequence_img014.png.svn-base
state_img03.png.svn-base
usecase_img07.png.svn-base
props
text-base
activity_img20.png.svn-base
classes04.png.svn-base
component_img04.png.svn-base
sequence_img014.png.svn-base
state_img03.png.svn-base
usecase_img07.png.svn-base
tmp
prop-base
props
text-base
activity_img20.png
classes04.png
component_img04.png
sequence_img014.png
state_img03.png
usecase_img07.png
Skins
.svn
entries
format
prop-base
props
text-base
tmp
prop-base
props
text-base
Black
.svn
entries
format
prop-base
bg_blue.JPG.svn-base
bg_green.JPG.svn-base
bg_red.JPG.svn-base
SPO3.ico.svn-base
props
text-base
bg_blue.JPG.svn-base
bg_green.JPG.svn-base
bg_red.JPG.svn-base
BlackResources.xaml.svn-base
SPO3.ico.svn-base
tmp
prop-base
props
text-base
bg_green.JPG
Thirdparty
ICSharpCode.AvalonEdit.dll
ICSharpCode.AvalonEdit.pdb
ICSharpCode.AvalonEdit.shfb
plantuml.exe
PlantUmlEditor.Setup
Banner.bmp
dotnetInstaller
dotNetInstaller.exe
PlantUmlSetup.vdproj
PlantUmlEditor.suo
Test.Utilities
Properties
.svn
all-wcprops
entries
prop-base
props
text-base
AssemblyInfo.cs.svn-base
tmp
prop-base
props
text-base
TestLibraries
moq
Castle.Core.dll
Castle.Core.pdb
Castle.DynamicProxy2.dll
Moq.dll
Moq.pdb
xunit
SpecificationExample.dll
SpecificationExample.pdb
xunit.dll
xunit.dll.tdnet
xunit.runner.tdnet.dll
xunit.runner.utility.dll
Utilities
Properties
.svn
all-wcprops
entries
prop-base
props
text-base
AssemblyInfo.cs.svn-base
tmp
prop-base
props
text-base
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using PlantUmlEditor.Model;
using System.Diagnostics;
using PlantUmlEditor.Properties;
using System.IO;
using System.ComponentModel;
using Utilities;

namespace PlantUmlEditor
{
    /// <summary>
    /// Takes a DiagramFile object into DataContext and renders the text editor and 
    /// shows the generated diagram
    /// </summary>
    public partial class DiagramViewControl : UserControl
    {
        private WeakReference<MenuItem> _LastMenuItemClicked = default(WeakReference<MenuItem>);

        public DiagramViewControl()
        {
            InitializeComponent();

            foreach (MenuItem topLevelMenu in AddContextMenu.Items)
            {
                foreach (MenuItem itemMenu in topLevelMenu.Items)
                {
                    itemMenu.Click += new RoutedEventHandler(MenuItem_Click);
                }
            }
        }

        public event Action<DiagramFile> OnBeforeSave;
        public event Action<DiagramFile> OnAfterSave;
        public event Action<DiagramFile> OnClose;
        
        private DiagramFile CurrentDiagram
        {
            get
            {
                return this.DataContext as DiagramFile;
            }
        }

        private void SaveDiagram_Click(object sender, RoutedEventArgs e)
        {
            this.SaveAndRefreshDiagram();
        }

        private void SaveAndRefreshDiagram()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;

            if (this.CurrentDiagram == default(DiagramFile))
                return;

            var diagramFileName = this.CurrentDiagram.DiagramFilePath;
            var pathForContentEditor = ContentEditor.Tag as string;

            if (diagramFileName != pathForContentEditor)
            {
                MessageBox.Show(Window.GetWindow(this), 
                    "Aha! This is one of those weird race condition I am getting into." + Environment.NewLine
                    + "Close the app and start again", "Weird race condition", 
                    MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            var content = ContentEditor.Text; 
            this.CurrentDiagram.Content = content;

            OnBeforeSave(this.CurrentDiagram);

            string plantUmlPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Thirdparty\\plantuml.exe");
            if (!File.Exists(plantUmlPath))
            {
                MessageBox.Show(Window.GetWindow(this), 
                    "Cannot find file: " + Environment.NewLine
                    + plantUmlPath, "PlantUml.exe not found", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            BackgroundWork.WaitForAllWork(TimeSpan.FromSeconds(20));
            BackgroundWork.DoWork(
                () =>
                {
                    // Save the diagram content
                    File.WriteAllText(diagramFileName, content);

                    // Use plantuml to generate the graph again                    
                    using (var process = new Process())
                    {
                        var startInfo = new ProcessStartInfo();
                        startInfo.FileName = plantUmlPath;
                        startInfo.Arguments = "\"" + diagramFileName + "\"";
                        startInfo.WindowStyle = ProcessWindowStyle.Hidden; // OMAR: Trick #5
                        startInfo.CreateNoWindow = true; // OMAR: Trick #5
                        process.StartInfo = startInfo;
                        if (process.Start())
                        {
                            process.WaitForExit(10000);
                        }
                    }
                },
                () =>
                {
                    BindingOperations.GetBindingExpression(DiagramImage, Image.SourceProperty).UpdateTarget();

                    OnAfterSave(this.CurrentDiagram);
                },
                (exception) =>
                {
                    OnAfterSave(this.CurrentDiagram);
                    MessageBox.Show(Window.GetWindow(this), exception.Message, "Error running PlantUml",
                                    MessageBoxButton.OK, MessageBoxImage.Error);
                });
        }

        private void CloseDiagram_Click(object sender, RoutedEventArgs e)
        {
            OnClose(this.CurrentDiagram);
        }

        private void UserControl_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            if (e.NewValue != null)
            {
                var newDiagram = (e.NewValue as DiagramFile);
                ContentEditor.Text = newDiagram.Content;
                ContentEditor.Tag = newDiagram.DiagramFilePath;
            }            

            if (this._LastMenuItemClicked != default(WeakReference<MenuItem>))
            {
                this._LastMenuItemClicked.Dispose();
                this._LastMenuItemClicked = null;
            }
        }

        private void ContentEditor_TextChanged(object sender, EventArgs e)
        {
            if (AutoRefreshCheckbox.IsChecked.Value)
            {
                if (!BackgroundWork.IsAnyWorkRunning())
                {
                    BackgroundWork.DoWorkAfter(SaveAndRefreshDiagram, 
                                               TimeSpan.FromSeconds(
                                                   int.Parse(RefreshSecondsTextBox.Text)));
                }
            }
        }

        private void AddStuff_Click(object sender, RoutedEventArgs e)
        {
            // Trick: Open the context menu automatically whenever user
            // clicks the "Add" button
            AddContextMenu.IsOpen = true;

            // If user last added a particular diagram items, say Use case
            // item, then auto open the usecase menu so that user does not
            // have to click on use case again. Saves time when you are adding
            // a lot of items for the same diagram
            if (_LastMenuItemClicked != default(WeakReference<MenuItem>))
            {
                MenuItem parentMenu = (_LastMenuItemClicked.Target.Parent as MenuItem);
                parentMenu.IsSubmenuOpen = true;
            }
        }

        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            this._LastMenuItemClicked = e.Source as MenuItem;
            this.AddCode((e.Source as MenuItem).Tag as string);
        }

        private void AddCode(string code)
        {
            ContentEditor.SelectionLength = 0;

            var formattedCode = code.Replace("\\r", Environment.NewLine) 
                + Environment.NewLine
                + Environment.NewLine;

            Clipboard.SetText(formattedCode);
            ContentEditor.Paste();

            this.SaveAndRefreshDiagram();
        }

        private void CopyToClipboard_Click(object sender, RoutedEventArgs e)
        {            
            Clipboard.SetImage(DiagramImage.Source as BitmapSource);
        }

        private void OpenInExplorer_Click(object sender, RoutedEventArgs e)
        {
            Process
                .Start("explorer.exe","/select," + this.CurrentDiagram.ImageFilePath)
                .Dispose();
        }

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Omar Al Zabir
Architect BT, UK (ex British Telecom)
United Kingdom United Kingdom

| Advertise | Privacy | Mobile
Web01 | 2.8.141015.1 | Last Updated 11 Jun 2011
Article Copyright 2010 by Omar Al Zabir
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid