■ 노드를 편집하는 방법을 보여준다.
▶ MainForm.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 |
using System; using System.Collections.Generic; using System.Drawing; using System.Windows.Forms; using Lassalle.Flow; using Lassalle.Flow.Layout.Hierarchic; namespace TestProject { /// <summary> /// 메인 폼 /// </summary> public partial class MainForm : Form { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 이미지 딕셔너리 /// </summary> private Dictionary<NodeType, Image> imageDictionary; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainForm() /// <summary> /// 생성자 /// </summary> public MainForm() { InitializeComponent(); this.imageDictionary = new Dictionary<NodeType, Image>(); this.imageDictionary.Add(NodeType.DATA , Properties.Resources.data ); this.imageDictionary.Add(NodeType.FILTER , Properties.Resources.filter ); this.imageDictionary.Add(NodeType.GROUPBY, Properties.Resources.groupBy); this.imageDictionary.Add(NodeType.MERGE , Properties.Resources.merge ); this.imageDictionary.Add(NodeType.QUERY , Properties.Resources.query ); this.imageDictionary.Add(NodeType.SORT , Properties.Resources.sort ); this.addFlow.Images.Add(new FlowImage(this.imageDictionary[NodeType.DATA ])); this.addFlow.Images.Add(new FlowImage(this.imageDictionary[NodeType.FILTER ])); this.addFlow.Images.Add(new FlowImage(this.imageDictionary[NodeType.GROUPBY])); this.addFlow.Images.Add(new FlowImage(this.imageDictionary[NodeType.MERGE ])); this.addFlow.Images.Add(new FlowImage(this.imageDictionary[NodeType.QUERY ])); this.addFlow.Images.Add(new FlowImage(this.imageDictionary[NodeType.SORT ])); this.treeView.Nodes.Add(new TreeNode { Text = NodeType.DATA.ToString() , Tag = NodeType.DATA }); this.treeView.Nodes.Add(new TreeNode { Text = NodeType.FILTER.ToString() , Tag = NodeType.FILTER }); this.treeView.Nodes.Add(new TreeNode { Text = NodeType.GROUPBY.ToString(), Tag = NodeType.GROUPBY}); this.treeView.Nodes.Add(new TreeNode { Text = NodeType.MERGE.ToString() , Tag = NodeType.MERGE }); this.treeView.Nodes.Add(new TreeNode { Text = NodeType.QUERY.ToString() , Tag = NodeType.QUERY }); this.treeView.Nodes.Add(new TreeNode { Text = NodeType.SORT.ToString() , Tag = NodeType.SORT }); this.addFlow.AutoScroll = true; this.addFlow.AllowDrop = true; this.addFlow.CanDragScroll = true; this.addFlow.CanDrawNode = false; this.addFlow.CanSizeNode = false; this.addFlow.CanLabelEdit = false; this.addFlow.CanReflexLink = false; this.addFlow.PageUnit = GraphicsUnit.Pixel; this.addFlow.DefNodeProp.FillColor = Color.White; this.addFlow.DefNodeProp.Shape.Style = Lassalle.Flow.ShapeStyle.Data; this.treeView.ItemDrag += treeView_ItemDrag; this.layoutButton.Click += layoutButton_Click; this.addFlow.DragEnter += addFlow_DragEnter; this.addFlow.DragDrop += addFlow_DragDrop; this.addFlow.BeforeAddLink += addFlow_BeforeAddLink; this.addFlow.AfterAddLink += addFlow_AfterAddLink; this.addFlow.KeyDown += addFlow_KeyDown; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region 트리 뷰 항목 DRAG 처리하기 - treeView_ItemDrag(sender, e) /// <summary> /// 트리 뷰 항목 DRAG 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void treeView_ItemDrag(object sender, ItemDragEventArgs e) { this.addFlow.DoDragDrop(e.Item, DragDropEffects.Move); } #endregion #region Layout 버튼 클릭시 처리하기 - layoutButton_Click(sender, e) /// <summary> /// Layout 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void layoutButton_Click(object sender, EventArgs e) { HFlow flow = new HFlow(); flow.LayerDistance = 50; flow.VertexDistance = 50; flow.Orientation = Lassalle.Flow.Layout.Hierarchic.Orientation.North; flow.LayerWidth = 0; flow.Layout(this.addFlow); for(int i = 0; i < this.addFlow.Nodes.Count; i++) { Node node = this.addFlow.Nodes[i]; if(node.Parent != null) { Node parentNode = node.Parent as Node; //float textX = parentNode.Rect.Right + 5f; //float textY = parentNode.Rect.Bottom - node.Font.Height - 5f; float textX = parentNode.Rect.Left + (parentNode.Rect.Width - node.Rect.Width) / 2f; float textY = parentNode.Rect.Bottom + 5f; node.Rect = new RectangleF(textX, textY, node.Rect.Width, node.Rect.Height); } } } #endregion #region AddFlow DRAG ENTER 처리하기 - addFlow_DragEnter(sender, e) /// <summary> /// AddFlow DRAG ENTER 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void addFlow_DragEnter(object sender, DragEventArgs e) { TreeNode node = e.Data.GetData("System.Windows.Forms.TreeNode", true) as TreeNode; if(node != null) { e.Effect = DragDropEffects.Move; } } #endregion #region AddFlow DRAG & DROP 처리하기 - addFlow_DragDrop(sender, e) /// <summary> /// AddFlow DRAG & DROP 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void addFlow_DragDrop(object sender, DragEventArgs e) { TreeNode treeNode = e.Data.GetData("System.Windows.Forms.TreeNode", true) as TreeNode; if(treeNode != null) { NodeType nodeType = (NodeType)treeNode.Tag; Point clientPoint = this.addFlow.PointToClient(new Point(e.X, e.Y)); Point controlPoint = this.addFlow.PointToAddFlow(clientPoint); AddNode(this.addFlow, controlPoint.X, controlPoint.Y, 42, 42, nodeType); } } #endregion #region AddFlow KEY DOWN 처리하기 - addFlow_KeyDown(sender, e) /// <summary> /// AddFlow KEY DOWN 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void addFlow_KeyDown(object sender, KeyEventArgs e) { if(e.KeyCode == Keys.Delete) { if(this.addFlow.SelectedItems == null || this.addFlow.SelectedItems.Count == 0) { return; } List<Node> nodeList = new List<Node>(); List<Link> linkList = new List<Link>(); for(int i = 0; i < this.addFlow.SelectedItems.Count; i++) { object item = this.addFlow.SelectedItems[i]; if(item is Node) { nodeList.Add(item as Node); } if(item is Link) { linkList.Add(item as Link); } } foreach(Node node in nodeList) { foreach(Link link in node.Links) { node.Links.Remove(link); } this.addFlow.Items.Remove(node); } foreach(Link link in linkList) { if(nodeList.Contains(link.Org) || nodeList.Contains(link.Dst)) { continue; } BaseNode startBaseNode = link.Org as BaseNode; BaseNode endBaseNode = link.Dst as BaseNode; startBaseNode.Links.Remove(link); if(startBaseNode != null) { startBaseNode.Validate(); } if(endBaseNode != null) { endBaseNode.Validate(); } } this.addFlow.SelectedItems.Clear(); this.addFlow.Refresh(); } } #endregion #region AddFlow 링크 추가 전 처리하기 - addFlow_BeforeAddLink(sender, e) /// <summary> /// AddFlow 링크 추가 전 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void addFlow_BeforeAddLink(object sender, BeforeAddLinkEventArgs e) { Node startNode = e.Org; Node endNode = e.Dst; if(endNode is QueryNode) { e.Cancel.Cancel = true; } else if(endNode is DataNode) { e.Cancel.Cancel = true; } else if(endNode is SortNode) { if(endNode.InLinks.Count > 0) { e.Cancel.Cancel = true; } } else if(endNode is FilterNode) { if(endNode.InLinks.Count > 0) { e.Cancel.Cancel = true; } } else if(endNode is MergeNode) { if(endNode.InLinks.Count > 1) { e.Cancel.Cancel = true; } } else if(endNode is GroupByNode) { if(endNode.InLinks.Count > 0) { e.Cancel.Cancel = true; } } } #endregion #region AddFlow 링크 추가 후 처리하기 - addFlow_AfterAddLink(sender, e) /// <summary> /// AddFlow 링크 추가 후 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void addFlow_AfterAddLink(object sender, AfterAddLinkEventArgs e) { (e.Link.Org as BaseNode).Validate(); (e.Link.Dst as BaseNode).Validate(); } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region 노드 추가하기 - AddNode(addFlow, nodeX, nodeY, nodeWidth, nodeHeight, nodeType) /// <summary> /// 노드 추가하기 /// </summary> /// <param name="addFlow">AddFlow</param> /// <param name="nodeX">노드 X 좌표</param> /// <param name="nodeY">노드 Y 좌표</param> /// <param name="nodeWidth">노드 너비</param> /// <param name="nodeHeight">노드 높이</param> /// <param name="nodeType">노드 타입</param> /// <returns>추가 노드</returns> private BaseNode AddNode(AddFlow addFlow, int nodeX, int nodeY, int nodeWidth, int nodeHeight, NodeType nodeType) { BaseNode baseNode = null; if (nodeType == NodeType.DATA ) baseNode = new DataNode(); else if(nodeType == NodeType.FILTER ) baseNode = new FilterNode(); else if(nodeType == NodeType.GROUPBY) baseNode = new GroupByNode(); else if(nodeType == NodeType.MERGE ) baseNode = new MergeNode(); else if(nodeType == NodeType.QUERY ) baseNode = new QueryNode(); else if(nodeType == NodeType.SORT ) baseNode = new SortNode(); if(baseNode == null) { return null; } else { baseNode.Rect = new RectangleF(nodeX, nodeY, nodeWidth, nodeHeight); addFlow.Nodes.Add(baseNode); baseNode.Validate(); AddLabelNode(addFlow, baseNode); return baseNode; } } #endregion #region 레이블 노드 추가하기 - AddLabelNode(addFlow, baseNode) /// <summary> /// 레이블 노드 추가하기 /// </summary> /// <param name="graphics">그래픽스</param> /// <param name="baseNode">베이스 노드</param> /// <returns>추가 레이블 노드</returns> private Node AddLabelNode(AddFlow addFlow, BaseNode baseNode) { using(Graphics graphics = addFlow.CreateGraphics()) { SizeF textSizeF = graphics.MeasureString(baseNode.Text, baseNode.Font); float textX = baseNode.Rect.Left + (baseNode.Rect.Width - textSizeF.Width) / 2f; float textY = baseNode.Rect.Bottom + 5f; Node labelNode = new Node(textX, textY, textSizeF.Width, textSizeF.Height); labelNode.Shape.Style = ShapeStyle.Rectangle; labelNode.FillColor = Color.Transparent; labelNode.DrawColor = Color.Transparent; labelNode.TextColor = Color.Gray; labelNode.Font = baseNode.Font; labelNode.Text = baseNode.Text; labelNode.Logical = false; labelNode.Selectable = false; labelNode.InLinkable = false; labelNode.OutLinkable = false; labelNode.AutoSize = Lassalle.Flow.AutoSize.NodeToText; labelNode.AttachmentStyle = AttachmentStyle.Item; labelNode.Alignment = Alignment.CenterBOTTOM; addFlow.Nodes.Add(labelNode); labelNode.Parent = baseNode; return labelNode; } } #endregion } } |