source: trunk/CrypPlugins/WorkspaceManager/View/Container/WorkSpaceEditorView.xaml.cs @ 1595

Last change on this file since 1595 was 1595, checked in by matkovic, 12 years ago

-orthogonal line bridging now functional
-Various style changes

File size: 8.3 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Windows;
6using System.Windows.Controls;
7using System.Windows.Data;
8using System.Windows.Documents;
9using System.Windows.Input;
10using System.Windows.Media;
11using System.Windows.Media.Imaging;
12using System.Windows.Navigation;
13using System.Windows.Shapes;
14using WorkspaceManager.View.Converter;
15using WorkspaceManager.View.Interface;
16using WorkspaceManager.View.VisualComponents;
17using WorkspaceManager.Model;
18using Cryptool.PluginBase.Editor;
19using Cryptool.Core;
20using Cryptool.PluginBase;
21
22namespace WorkspaceManager.View.Container
23{
24    /// <summary>
25    /// Interaction logic for WorkSpaceEditorView.xaml
26    /// </summary>
27    public partial class WorkSpaceEditorView : UserControl
28    {
29        public enum EditorState
30        {
31            READY,
32            BUSY
33        };
34
35        private Point previousDragPoint = new Point();
36        private ConnectorView selectedConnector;
37        private PluginContainerView selectedPluginContainer;
38        private CryptoLineView dummyLine = new CryptoLineView();
39        private Point p;
40
41        public EditorState State;
42        public EditorState ConnectorState;
43        public List<CryptoLineView> LineList = new List<CryptoLineView>();
44        public List<PluginContainerView> PluginContainerList = new List<PluginContainerView>();
45        private WorkspaceModel WorkspaceModel;
46
47        public WorkSpaceEditorView()
48        {
49            InitializeComponent();
50        }
51
52        public WorkSpaceEditorView(WorkspaceModel WorkspaceModel)
53        {
54            this.Loaded += new RoutedEventHandler(WorkSpaceEditorView_Loaded);
55            this.DragEnter += new DragEventHandler(WorkSpaceEditorView_DragEnter);
56            this.Drop += new DragEventHandler(WorkSpaceEditorView_Drop);
57            this.MouseMove += new MouseEventHandler(WorkSpaceEditorView_MouseMove);
58            this.PreviewMouseRightButtonDown += new MouseButtonEventHandler(WorkSpaceEditorView_PreviewMouseRightButtonDown);
59            this.WorkspaceModel = WorkspaceModel;
60            this.State = EditorState.READY;
61            InitializeComponent();
62        }
63
64        void WorkSpaceEditorView_Loaded(object sender, RoutedEventArgs e)
65        {
66
67        }
68
69        public void AddPluginContainerView(Point position, PluginModel model)
70        {
71            PluginContainerView shape = new PluginContainerView(model);
72            shape.MouseLeave += new MouseEventHandler(shape_MouseLeave);
73            shape.OnConnectorMouseLeftButtonDown += new EventHandler<ConnectorViewEventArgs>(shape_OnConnectorMouseLeftButtonDown);
74            shape.MouseLeftButtonDown += new MouseButtonEventHandler(shape_MouseLeftButtonDown);
75            shape.MouseLeftButtonUp += new MouseButtonEventHandler(shape_MouseLeftButtonUp);
76            shape.SetPosition(position);
77            this.root.Children.Add(shape);
78            Canvas.SetZIndex(shape, 100);
79        }
80
81        public void AddConnection(IConnectable source, IConnectable target)
82        {
83            ConnectionModel connectionModel = this.WorkspaceModel.newConnectionModel(((ConnectorView)source).cModel, ((ConnectorView)target).cModel, ((ConnectorView)source).cModel.ConnectorType);
84            CryptoLineView conn = new CryptoLineView(connectionModel);
85            conn.SetBinding(CryptoLineView.StartPointProperty, CreateConnectorBinding(source));
86            conn.SetBinding(CryptoLineView.EndPointProperty, CreateConnectorBinding(target));
87            root.Children.Add(conn);
88            Canvas.SetZIndex(conn, 0);           
89        }
90
91        private void AddConnectionSource(IConnectable source, CryptoLineView conn)
92        {
93            Color color = ColorHelper.getColor((source as ConnectorView).cModel.ConnectorType);
94            conn.Stroke = new SolidColorBrush(color);
95            conn.SetBinding(CryptoLineView.StartPointProperty, CreateConnectorBinding(source));
96            conn.EndPoint = Mouse.GetPosition(this);
97        }
98
99        private MultiBinding CreateConnectorBinding(IConnectable connectable)
100        {
101            MultiBinding multiBinding = new MultiBinding();
102            multiBinding.Converter = new ConnectorBindingConverter();
103
104            Binding binding = new Binding();
105            binding.Source = connectable;
106            binding.Path = new PropertyPath(ConnectorView.PositionOnWorkSpaceXProperty);
107            multiBinding.Bindings.Add(binding);
108
109            binding = new Binding();
110            binding.Source = connectable;
111            binding.Path = new PropertyPath(ConnectorView.PositionOnWorkSpaceYProperty);
112            multiBinding.Bindings.Add(binding);
113
114            binding = new Binding();
115            binding.Source = connectable;
116            binding.Path = new PropertyPath(FrameworkElement.ActualHeightProperty);
117            multiBinding.Bindings.Add(binding);
118
119            binding = new Binding();
120            binding.Source = connectable;
121            binding.Path = new PropertyPath(FrameworkElement.ActualWidthProperty);
122            multiBinding.Bindings.Add(binding);
123
124            return multiBinding;
125        }
126
127        #region Controls
128
129        void shape_OnConnectorMouseLeftButtonDown(object sender, ConnectorViewEventArgs e)
130        {
131            if (selectedConnector != null && e.connector.cModel.ConnectorType.Name == selectedConnector.cModel.ConnectorType.Name)
132            {
133                this.root.Children.Remove(dummyLine);
134                this.AddConnection(selectedConnector, e.connector);
135                this.selectedConnector = null;
136                this.State = EditorState.READY;
137                return;
138            }
139
140            if (selectedConnector == null)
141            {
142                this.root.Children.Add(dummyLine);
143                this.selectedConnector = e.connector;
144                this.AddConnectionSource(e.connector, dummyLine);
145                this.State = EditorState.BUSY;
146                return;
147            }
148           
149        }
150
151        void WorkSpaceEditorView_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
152        {
153            this.selectedConnector = null;
154            this.root.Children.Remove(dummyLine);
155        }
156
157        void WorkSpaceEditorView_MouseMove(object sender, MouseEventArgs e)
158        {
159            if (e.LeftButton == MouseButtonState.Pressed && selectedPluginContainer != null)
160            {
161                p = selectedPluginContainer.GetPosition();
162                this.selectedPluginContainer.SetPosition(new Point(p.X + Mouse.GetPosition(root).X - previousDragPoint.X, p.Y + Mouse.GetPosition(root).Y - previousDragPoint.Y));
163            }
164
165            if (selectedConnector != null && root.Children.Contains(dummyLine))
166            {
167                this.dummyLine.EndPoint = Mouse.GetPosition(root);
168            }
169            this.previousDragPoint = Mouse.GetPosition(root);
170        }
171
172        void shape_MouseLeave(object sender, MouseEventArgs e)
173        {
174            this.selectedPluginContainer = null;
175            this.State = EditorState.READY;
176        }
177
178        void shape_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
179        {
180            Canvas.SetZIndex(sender as PluginContainerView, 100);
181            this.selectedPluginContainer = null;
182            this.State = EditorState.BUSY;
183        }
184
185        void shape_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
186        {
187            Canvas.SetZIndex(sender as PluginContainerView, 101);
188            this.selectedPluginContainer = sender as PluginContainerView;
189            this.State = EditorState.BUSY;
190        }
191
192        void WorkSpaceEditorView_Drop(object sender, DragEventArgs e)
193        {
194            DragDropDataObject obj = e.Data.GetData("Cryptool.PluginBase.Editor.DragDropDataObject") as DragDropDataObject;
195            if(obj != null)
196                this.AddPluginContainerView(e.GetPosition(root), WorkspaceModel.newPluginModel(DragDropDataObjectToPluginConverter.CreatePluginInstance(obj.AssemblyFullName, obj.TypeFullName)));
197        }
198
199        void WorkSpaceEditorView_DragEnter(object sender, DragEventArgs e)
200        {
201            e.Effects = DragDropEffects.Copy;
202        }
203
204        #endregion
205    }
206}
Note: See TracBrowser for help on using the repository browser.