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

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

-small fix

File size: 7.9 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 ConntectorView 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<ConntectorViewEventArgs>(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            CryptoLineView conn = new CryptoLineView();
84            conn.SetBinding(CryptoLineView.StartPointProperty, CreateConnectorBinding(source));
85            conn.SetBinding(CryptoLineView.EndPointProperty, CreateConnectorBinding(target));
86            root.Children.Add(conn);
87            Canvas.SetZIndex(conn, 0);
88        }
89
90        private void AddConnectionSource(IConnectable source, CryptoLineView conn)
91        {
92            conn.SetBinding(CryptoLineView.StartPointProperty, CreateConnectorBinding(source));
93            conn.EndPoint = Mouse.GetPosition(this);
94        }
95
96        private MultiBinding CreateConnectorBinding(IConnectable connectable)
97        {
98            MultiBinding multiBinding = new MultiBinding();
99            multiBinding.Converter = new ConnectorBindingConverter();
100
101            Binding binding = new Binding();
102            binding.Source = connectable;
103            binding.Path = new PropertyPath(ConntectorView.PositionOnWorkSpaceXProperty);
104            multiBinding.Bindings.Add(binding);
105
106            binding = new Binding();
107            binding.Source = connectable;
108            binding.Path = new PropertyPath(ConntectorView.PositionOnWorkSpaceYProperty);
109            multiBinding.Bindings.Add(binding);
110
111            binding = new Binding();
112            binding.Source = connectable;
113            binding.Path = new PropertyPath(FrameworkElement.ActualHeightProperty);
114            multiBinding.Bindings.Add(binding);
115
116            binding = new Binding();
117            binding.Source = connectable;
118            binding.Path = new PropertyPath(FrameworkElement.ActualWidthProperty);
119            multiBinding.Bindings.Add(binding);
120
121            return multiBinding;
122        }
123
124        #region Controls
125
126        void shape_OnConnectorMouseLeftButtonDown(object sender, ConntectorViewEventArgs e)
127        {
128            if (selectedConnector != null)
129            {
130                this.root.Children.Remove(dummyLine);
131                this.AddConnection(selectedConnector, e.connector);
132                this.selectedConnector = null;
133                this.State = EditorState.READY;
134                return;
135            }
136
137            if (selectedConnector == null)
138            {
139                this.root.Children.Add(dummyLine);
140                this.selectedConnector = e.connector;
141                this.AddConnectionSource(e.connector, dummyLine);
142                this.State = EditorState.BUSY;
143                return;
144            }
145           
146        }
147
148        void WorkSpaceEditorView_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
149        {
150            this.selectedConnector = null;
151            this.root.Children.Remove(dummyLine);
152        }
153
154        void WorkSpaceEditorView_MouseMove(object sender, MouseEventArgs e)
155        {
156            if (e.LeftButton == MouseButtonState.Pressed && selectedPluginContainer != null)
157            {
158                p = selectedPluginContainer.GetPosition();
159                this.selectedPluginContainer.SetPosition(new Point(p.X + (Mouse.GetPosition(sender as WorkSpaceEditorView).X - previousDragPoint.X), p.Y + (Mouse.GetPosition(sender as WorkSpaceEditorView).Y - previousDragPoint.Y)));
160            }
161
162            if (selectedConnector != null && root.Children.Contains(dummyLine))
163            {
164                this.dummyLine.EndPoint = Mouse.GetPosition(sender as WorkSpaceEditorView);
165            }
166            this.previousDragPoint = Mouse.GetPosition(sender as WorkSpaceEditorView);
167        }
168
169        void shape_MouseLeave(object sender, MouseEventArgs e)
170        {
171            this.selectedPluginContainer = null;
172            this.State = EditorState.READY;
173        }
174
175        void shape_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
176        {
177            Canvas.SetZIndex(sender as PluginContainerView, 100);
178            this.selectedPluginContainer = null;
179            this.State = EditorState.BUSY;
180        }
181
182        void shape_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
183        {
184            Canvas.SetZIndex(sender as PluginContainerView, 101);
185            this.selectedPluginContainer = sender as PluginContainerView;
186            this.State = EditorState.BUSY;
187        }
188
189        void WorkSpaceEditorView_Drop(object sender, DragEventArgs e)
190        {
191            DragDropDataObject obj = e.Data.GetData("Cryptool.PluginBase.Editor.DragDropDataObject") as DragDropDataObject;
192            if(obj != null)
193                this.AddPluginContainerView(e.GetPosition(this), WorkspaceModel.newPluginModel(DragDropDataObjectToPluginConverter.CreatePluginInstance(obj.AssemblyFullName, obj.TypeFullName)));
194        }
195
196        void WorkSpaceEditorView_DragEnter(object sender, DragEventArgs e)
197        {
198            e.Effects = DragDropEffects.Copy;
199        }
200
201        #endregion
202    }
203}
Note: See TracBrowser for help on using the repository browser.