Datei: SimpleMappingTool/MainForm.cs

Last Commit (8c4b1f0)
1 //
2 // Copyright (c) 2002-2025 Mirko Matytschak
3 // (www.netdataobjects.de)
4 //
5 // Author: Mirko Matytschak
6 //
7 // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
8 // documentation files (the "Software"), to deal in the Software without restriction, including without limitation
9 // the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
10 // Software, and to permit persons to whom the Software is furnished to do so, subject to the following
11 // conditions:
12
13 // The above copyright notice and this permission notice shall be included in all copies or substantial portions
14 // of the Software.
15 //
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
17 // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
19 // CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 // DEALINGS IN THE SOFTWARE.
21
22
23 using System;
24 using System.Reflection;
25 using System.IO;
26 using System.Text.RegularExpressions;
27 using System.Drawing;
28 using System.Collections;
29 using System.ComponentModel;
30 using System.Windows.Forms;
31 using NDO.Mapping;
32
33 namespace SimpleMappingTool
34 {
35 ····/// <summary>
36 ····/// Zusammenfassung für MainForm.
37 ····/// </summary>
38 ····internal class MainForm : Form
39 ····{
40 ········private IContainer components;
41 ········private MenuStrip mainMenu;
42 ········private ToolStripMenuItem fileToolStripMenuItem;
43 ········private ToolStripMenuItem menuOpen;
44 ········private ToolStripMenuItem menuSave;
45 ········private ToolStripMenuItem menuSaveAs;
46 ········private bool saveOnClose = false;
47 ········private Splitter splitter1;
48 ········private PropertyGrid propertyGrid1;
49 ········private TreeView allObjects;
50 ········private ImageList imageList1;
51
52 ········NDOMapping? mapping = null;
53
54 #pragma warning disable 8618
55 ········public MainForm( string[] args )
56 ········{
57 ············try
58 ············{
59 ················InitializeComponent();
60 ················ScanArgs( args );
61 ················Application.Idle += new EventHandler( OnIdle );
62 ············}
63 ············catch (Exception ex)
64 ············{
65 ················MessageBox.Show( ex.ToString(), "Mapping tool error" );
66 ············}
67 ········}
68 #pragma warning restore 8618
69
70 ········/// <summary>
71 ········/// Die verwendeten Ressourcen bereinigen.
72 ········/// </summary>
73 ········protected override void Dispose( bool disposing )
74 ········{
75 ············if (disposing)
76 ············{
77 ················if (components != null)
78 ················{
79 ····················components.Dispose();
80 ················}
81 ············}
82 ············base.Dispose( disposing );
83 ········}
84
85 ········#region Vom Windows Form-Designer generierter Code
86 ········/// <summary>
87 ········/// Erforderliche Methode für die Designerunterstützung.
88 ········/// Der Inhalt der Methode darf nicht mit dem Code-Editor geändert werden.
89 ········/// </summary>
90 ········private void InitializeComponent()
91 ········{
92 ············components = new Container();
93 ············ComponentResourceManager resources = new ComponentResourceManager(typeof(MainForm));
94 ············allObjects = new TreeView();
95 ············imageList1 = new ImageList( components );
96 ············splitter1 = new Splitter();
97 ············propertyGrid1 = new PropertyGrid();
98 ············mainMenu = new MenuStrip();
99 ············fileToolStripMenuItem = new ToolStripMenuItem();
100 ············menuOpen = new ToolStripMenuItem();
101 ············menuSave = new ToolStripMenuItem();
102 ············menuSaveAs = new ToolStripMenuItem();
103 ············mainMenu.SuspendLayout();
104 ············SuspendLayout();
105 ············//
106 ············// allObjects
107 ············//
108 ············allObjects.Dock = DockStyle.Left;
109 ············allObjects.ImageIndex = 0;
110 ············allObjects.ImageList = imageList1;
111 ············allObjects.Location = new Point( 0, 24 );
112 ············allObjects.Name = "allObjects";
113 ············allObjects.SelectedImageIndex = 0;
114 ············allObjects.Size = new Size( 464, 408 );
115 ············allObjects.TabIndex = 0;
116 ············allObjects.AfterSelect +=··allObjects_AfterSelect ;
117 ············allObjects.MouseUp +=··allObjects_MouseUp ;
118 ············//
119 ············// imageList1
120 ············//
121 ············imageList1.ColorDepth = ColorDepth.Depth8Bit;
122 ············imageList1.ImageStream = (ImageListStreamer) resources.GetObject( "imageList1.ImageStream" )!;
123 ············imageList1.TransparentColor = Color.Transparent;
124 ············imageList1.Images.SetKeyName( 0, "" );
125 ············imageList1.Images.SetKeyName( 1, "" );
126 ············imageList1.Images.SetKeyName( 2, "" );
127 ············imageList1.Images.SetKeyName( 3, "" );
128 ············imageList1.Images.SetKeyName( 4, "" );
129 ············imageList1.Images.SetKeyName( 5, "" );
130 ············imageList1.Images.SetKeyName( 6, "" );
131 ············imageList1.Images.SetKeyName( 7, "" );
132 ············imageList1.Images.SetKeyName( 8, "" );
133 ············imageList1.Images.SetKeyName( 9, "" );
134 ············imageList1.Images.SetKeyName( 10, "" );
135 ············imageList1.Images.SetKeyName( 11, "" );
136 ············imageList1.Images.SetKeyName( 12, "" );
137 ············imageList1.Images.SetKeyName( 13, "" );
138 ············//
139 ············// splitter1
140 ············//
141 ············splitter1.Location = new Point( 464, 24 );
142 ············splitter1.Name = "splitter1";
143 ············splitter1.Size = new Size( 3, 408 );
144 ············splitter1.TabIndex = 1;
145 ············splitter1.TabStop = false;
146 ············//
147 ············// propertyGrid1
148 ············//
149 ············propertyGrid1.Dock = DockStyle.Fill;
150 ············propertyGrid1.LineColor = SystemColors.ScrollBar;
151 ············propertyGrid1.Location = new Point( 467, 24 );
152 ············propertyGrid1.Name = "propertyGrid1";
153 ············propertyGrid1.Size = new Size( 317, 408 );
154 ············propertyGrid1.TabIndex = 2;
155 ············//
156 ············// mainMenu
157 ············//
158 ············mainMenu.Items.AddRange( new ToolStripItem[] { fileToolStripMenuItem } );
159 ············mainMenu.Location = new Point( 0, 0 );
160 ············mainMenu.Name = "mainMenu";
161 ············mainMenu.Size = new Size( 784, 24 );
162 ············mainMenu.TabIndex = 0;
163 ············mainMenu.Text = "mainMenu";
164 ············//
165 ············// fileToolStripMenuItem
166 ············//
167 ············fileToolStripMenuItem.DropDownItems.AddRange( new ToolStripItem[] { menuOpen, menuSave, menuSaveAs } );
168 ············fileToolStripMenuItem.Name = "fileToolStripMenuItem";
169 ············fileToolStripMenuItem.Size = new Size( 37, 20 );
170 ············fileToolStripMenuItem.Text = "&File";
171 ············//
172 ············// menuOpen
173 ············//
174 ············menuOpen.Name = "menuOpen";
175 ············menuOpen.Size = new Size( 180, 22 );
176 ············menuOpen.Text = "&Open";
177 ············menuOpen.Click +=··menuOpen_Click ;
178 ············//
179 ············// menuSave
180 ············//
181 ············menuSave.Name = "menuSave";
182 ············menuSave.Size = new Size( 180, 22 );
183 ············menuSave.Text = "&Save";
184 ············menuSave.Click +=··menuSave_Click ;
185 ············//
186 ············// menuSaveAs
187 ············//
188 ············menuSaveAs.Name = "menuSaveAs";
189 ············menuSaveAs.Size = new Size( 180, 22 );
190 ············menuSaveAs.Text = "Save &as...";
191 ············menuSaveAs.Click +=··menuSaveAs_Click ;
192 ············//
193 ············// MainForm
194 ············//
195 ············AutoScaleBaseSize = new Size( 6, 16 );
196 ············ClientSize = new Size( 784, 432 );
197 ············Controls.Add( propertyGrid1 );
198 ············Controls.Add( splitter1 );
199 ············Controls.Add( allObjects );
200 ············Controls.Add( mainMenu );
201 ············Icon = (Icon) resources.GetObject( "$this.Icon" )!;
202 ············MainMenuStrip = mainMenu;
203 ············Name = "MainForm";
204 ············Text = "NDO Mapping Tool";
205 ············Closing +=··MainForm_Closing ;
206 ············Load +=··MainForm_Load ;
207 ············mainMenu.ResumeLayout( false );
208 ············mainMenu.PerformLayout();
209 ············ResumeLayout( false );
210 ············PerformLayout();
211 ········}
212 ········#endregion
213
214 ········/// <summary>
215 ········/// Der Haupteinstiegspunkt für die Anwendung.
216 ········/// </summary>
217 ········[STAThread]
218 ········static void Main( string[] args )
219 ········{
220 ············Application.Run( new MainForm( args ) );
221 ········}
222
223 ········private void LoadMapping( string fileName )
224 ········{
225 ············allObjects.Nodes.Clear();
226 ············try
227 ············{
228 mapping = new NDOMapping( fileName ) ;
229 ················PrepareRelations();
230 ············}
231 ············catch (Exception ex)
232 ············{
233 ················mapping = null;
234 ················this.Text = string.Empty;
235 #if DEBUG
236 ················MessageBox.Show( ex.ToString(), "Error" );
237 #else
238 ················MessageBox.Show(ex.Message, "Error");
239 #endif
240 ················return;
241 ············}
242 ············this.Text = fileName;
243 ············if (mapping != null)
244 ················FillNodes();
245 ········}
246
247
248 ········private void FillNodes()
249 ········{
250 ············NDOMappingNode mn = new NDOMappingNode(this.mapping!);
251 ············allObjects.Nodes.Add( mn );
252 ········}
253
254 ········private void ScanArgs( string[] args )
255 ········{
256 ············Regex regexm = new Regex(@"-m:(.*)");
257 ············foreach (string arg in args)
258 ············{
259 ················Match matchm = regexm.Match(arg);
260 ················if (matchm.Success)
261 ················{
262 ····················LoadMapping( matchm.Groups[1].Value );
263 ····················saveOnClose = true;
264 ················}
265 ············}
266 ········}
267
268
269 ········private void MainForm_Load( object? sender, System.EventArgs e )
270 ········{
271 ········}
272
273 ········private void allObjects_AfterSelect( object? sender, TreeViewEventArgs e )
274 ········{
275 ············var tn = e.Node as NDOTreeNode;
276 ············if (tn != null)
277 ················this.propertyGrid1.SelectedObject = tn.Object;
278 ············else
279 ················this.propertyGrid1.SelectedObject = null;
280 ········}
281
282 ········private void MainForm_Closing( object? sender, System.ComponentModel.CancelEventArgs e )
283 ········{
284 ············if (saveOnClose && mapping != null)
285 ················mapping.Save();
286 ············else if (mapping != null && mapping.HasChanges)
287 ············{
288 ················if (MessageBox.Show( "Save changes?", "NDO Mapping Tool", MessageBoxButtons.YesNo, MessageBoxIcon.Question ) == DialogResult.Yes)
289 ················{
290 ····················mapping.Save();
291 ················}
292 ············}
293 ········}
294
295 ········private void menuOpen_Click( object? sender, System.EventArgs e )
296 ········{
297 ············if (mapping != null && mapping.HasChanges)
298 ············{
299 ················if (MessageBox.Show( "Save changes?", "Mapping Tool", MessageBoxButtons.YesNo ) == DialogResult.Yes)
300 ················{
301 ····················mapping.Save();
302 ····················mapping = null;
303 ················}
304 ············}
305
306 ············OpenFileDialog ofd = new OpenFileDialog();
307 ············ofd.CheckFileExists = true;
308 ············ofd.DefaultExt = "xml";
309 ············ofd.Multiselect = false;
310 ············ofd.Filter = "Mapping Files (*.xml)|*.xml";
311 ············ofd.FileName = "NDOMapping.xml";
312 ············if (ofd.ShowDialog() == DialogResult.Cancel)
313 ················return;
314 ············LoadMapping( ofd.FileName );
315 ········}
316
317
318 ········bool PrepareRelations()
319 ········{
320 ············bool result = false;
321 ············if (mapping != null)
322 ············{
323 ················foreach (Class cl in mapping.Classes)
324 ················{
325 ····················foreach (Relation r in cl.Relations)
326 ····················{
327 ························// This computes the foreign relation, which is
328 ························// needed to avoid a null pointer exception
329 ························// while binding to the property grid.
330 ························result = result || r.Bidirectional;
331 ····················}
332 ················}
333 ············}
334
335 ············return result;··// Nobody reads this value, but we make shure, that the code won't get removed by the optimizer.
336 ········}
337
338
339 ········private void menuSave_Click( object? sender, System.EventArgs e )
340 ········{
341 ············if (mapping != null)
342 ················mapping.Save();
343 ········}
344
345 ········private void menuSaveAs_Click( object? sender, System.EventArgs e )
346 ········{
347 ············if (mapping != null)
348 ············{
349 ················SaveFileDialog sfd = new SaveFileDialog();
350 ················if (mapping.FileName != string.Empty)
351 ····················sfd.InitialDirectory = Path.GetDirectoryName( mapping.FileName );
352 ················sfd.CheckFileExists = false;
353 ················sfd.DefaultExt = "xml";
354 ················sfd.Filter = "Mapping Files (*.xml)|*.xml";
355 ················sfd.FileName = "NDOMapping.xml";
356 ················if (sfd.ShowDialog( this ) != DialogResult.OK)
357 ····················return;
358 ················mapping.SaveAs( sfd.FileName );
359 ················this.Text = sfd.FileName;
360 ············}
361 ········}
362
363 ········private void OnIdle( object? sender, EventArgs e )
364 ········{
365 ············menuSave.Enabled = mapping != null && mapping.HasChanges;
366 ············menuOpen.Enabled = !saveOnClose;
367 ········}
368
369 #if DEBUG
370 ········private void MainForm_KeyPress( object? sender, KeyPressEventArgs e )
371 ········{
372
373 ············if ((int) e.KeyChar == 27)
374 ················MessageBox.Show( "Escape" );
375 ········}
376 #endif
377
378 ········private void allObjects_MouseUp( object? sender, MouseEventArgs e )
379 ········{
380
381 ············if (e.Button == MouseButtons.Right)
382 ············{
383 ················var tn = this.allObjects.GetNodeAt(e.X, e.Y) as NDOTreeNode;
384 ················if (tn != null)
385 ················{
386 ····················allObjects.SelectedNode = tn;
387 ····················var menu = tn.GetContextMenu();
388 ····················if (menu.Items.Count > 0)
389 ························menu.Show(allObjects, new Point(e.X, e.Y));
390 ····················this.propertyGrid1.SelectedObject = tn.Object;
391 ················}
392 ············}
393 ········}
394 ····}
395 }
396
New Commit (db278e7)
1 //
2 // Copyright (c) 2002-2025 Mirko Matytschak
3 // (www.netdataobjects.de)
4 //
5 // Author: Mirko Matytschak
6 //
7 // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
8 // documentation files (the "Software"), to deal in the Software without restriction, including without limitation
9 // the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
10 // Software, and to permit persons to whom the Software is furnished to do so, subject to the following
11 // conditions:
12
13 // The above copyright notice and this permission notice shall be included in all copies or substantial portions
14 // of the Software.
15 //
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
17 // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
19 // CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 // DEALINGS IN THE SOFTWARE.
21
22
23 using System;
24 using System.Reflection;
25 using System.IO;
26 using System.Text.RegularExpressions;
27 using System.Drawing;
28 using System.Collections;
29 using System.ComponentModel;
30 using System.Windows.Forms;
31 using NDO.Mapping;
32
33 namespace SimpleMappingTool
34 {
35 ····/// <summary>
36 ····/// Zusammenfassung für MainForm.
37 ····/// </summary>
38 ····internal class MainForm : Form
39 ····{
40 ········private IContainer components;
41 ········private MenuStrip mainMenu;
42 ········private ToolStripMenuItem fileToolStripMenuItem;
43 ········private ToolStripMenuItem menuOpen;
44 ········private ToolStripMenuItem menuSave;
45 ········private ToolStripMenuItem menuSaveAs;
46 ········private bool saveOnClose = false;
47 ········private Splitter splitter1;
48 ········private PropertyGrid propertyGrid1;
49 ········private TreeView allObjects;
50 ········private ImageList imageList1;
51
52 ········NDOMapping? mapping = null;
53
54 #pragma warning disable 8618
55 ········public MainForm( string[] args )
56 ········{
57 ············try
58 ············{
59 ················InitializeComponent();
60 ················ScanArgs( args );
61 ················Application.Idle += new EventHandler( OnIdle );
62 ············}
63 ············catch (Exception ex)
64 ············{
65 ················MessageBox.Show( ex.ToString(), "Mapping tool error" );
66 ············}
67 ········}
68 #pragma warning restore 8618
69
70 ········/// <summary>
71 ········/// Die verwendeten Ressourcen bereinigen.
72 ········/// </summary>
73 ········protected override void Dispose( bool disposing )
74 ········{
75 ············if (disposing)
76 ············{
77 ················if (components != null)
78 ················{
79 ····················components.Dispose();
80 ················}
81 ············}
82 ············base.Dispose( disposing );
83 ········}
84
85 ········#region Vom Windows Form-Designer generierter Code
86 ········/// <summary>
87 ········/// Erforderliche Methode für die Designerunterstützung.
88 ········/// Der Inhalt der Methode darf nicht mit dem Code-Editor geändert werden.
89 ········/// </summary>
90 ········private void InitializeComponent()
91 ········{
92 ············components = new Container();
93 ············ComponentResourceManager resources = new ComponentResourceManager(typeof(MainForm));
94 ············allObjects = new TreeView();
95 ············imageList1 = new ImageList( components );
96 ············splitter1 = new Splitter();
97 ············propertyGrid1 = new PropertyGrid();
98 ············mainMenu = new MenuStrip();
99 ············fileToolStripMenuItem = new ToolStripMenuItem();
100 ············menuOpen = new ToolStripMenuItem();
101 ············menuSave = new ToolStripMenuItem();
102 ············menuSaveAs = new ToolStripMenuItem();
103 ············mainMenu.SuspendLayout();
104 ············SuspendLayout();
105 ············//
106 ············// allObjects
107 ············//
108 ············allObjects.Dock = DockStyle.Left;
109 ············allObjects.ImageIndex = 0;
110 ············allObjects.ImageList = imageList1;
111 ············allObjects.Location = new Point( 0, 24 );
112 ············allObjects.Name = "allObjects";
113 ············allObjects.SelectedImageIndex = 0;
114 ············allObjects.Size = new Size( 464, 408 );
115 ············allObjects.TabIndex = 0;
116 ············allObjects.AfterSelect +=··allObjects_AfterSelect ;
117 ············allObjects.MouseUp +=··allObjects_MouseUp ;
118 ············//
119 ············// imageList1
120 ············//
121 ············imageList1.ColorDepth = ColorDepth.Depth8Bit;
122 ············imageList1.ImageStream = (ImageListStreamer) resources.GetObject( "imageList1.ImageStream" )!;
123 ············imageList1.TransparentColor = Color.Transparent;
124 ············imageList1.Images.SetKeyName( 0, "" );
125 ············imageList1.Images.SetKeyName( 1, "" );
126 ············imageList1.Images.SetKeyName( 2, "" );
127 ············imageList1.Images.SetKeyName( 3, "" );
128 ············imageList1.Images.SetKeyName( 4, "" );
129 ············imageList1.Images.SetKeyName( 5, "" );
130 ············imageList1.Images.SetKeyName( 6, "" );
131 ············imageList1.Images.SetKeyName( 7, "" );
132 ············imageList1.Images.SetKeyName( 8, "" );
133 ············imageList1.Images.SetKeyName( 9, "" );
134 ············imageList1.Images.SetKeyName( 10, "" );
135 ············imageList1.Images.SetKeyName( 11, "" );
136 ············imageList1.Images.SetKeyName( 12, "" );
137 ············imageList1.Images.SetKeyName( 13, "" );
138 ············//
139 ············// splitter1
140 ············//
141 ············splitter1.Location = new Point( 464, 24 );
142 ············splitter1.Name = "splitter1";
143 ············splitter1.Size = new Size( 3, 408 );
144 ············splitter1.TabIndex = 1;
145 ············splitter1.TabStop = false;
146 ············//
147 ············// propertyGrid1
148 ············//
149 ············propertyGrid1.Dock = DockStyle.Fill;
150 ············propertyGrid1.LineColor = SystemColors.ScrollBar;
151 ············propertyGrid1.Location = new Point( 467, 24 );
152 ············propertyGrid1.Name = "propertyGrid1";
153 ············propertyGrid1.Size = new Size( 317, 408 );
154 ············propertyGrid1.TabIndex = 2;
155 ············//
156 ············// mainMenu
157 ············//
158 ············mainMenu.Items.AddRange( new ToolStripItem[] { fileToolStripMenuItem } );
159 ············mainMenu.Location = new Point( 0, 0 );
160 ············mainMenu.Name = "mainMenu";
161 ············mainMenu.Size = new Size( 784, 24 );
162 ············mainMenu.TabIndex = 0;
163 ············mainMenu.Text = "mainMenu";
164 ············//
165 ············// fileToolStripMenuItem
166 ············//
167 ············fileToolStripMenuItem.DropDownItems.AddRange( new ToolStripItem[] { menuOpen, menuSave, menuSaveAs } );
168 ············fileToolStripMenuItem.Name = "fileToolStripMenuItem";
169 ············fileToolStripMenuItem.Size = new Size( 37, 20 );
170 ············fileToolStripMenuItem.Text = "&File";
171 ············//
172 ············// menuOpen
173 ············//
174 ············menuOpen.Name = "menuOpen";
175 ············menuOpen.Size = new Size( 180, 22 );
176 ············menuOpen.Text = "&Open";
177 ············menuOpen.Click +=··menuOpen_Click ;
178 ············//
179 ············// menuSave
180 ············//
181 ············menuSave.Name = "menuSave";
182 ············menuSave.Size = new Size( 180, 22 );
183 ············menuSave.Text = "&Save";
184 ············menuSave.Click +=··menuSave_Click ;
185 ············//
186 ············// menuSaveAs
187 ············//
188 ············menuSaveAs.Name = "menuSaveAs";
189 ············menuSaveAs.Size = new Size( 180, 22 );
190 ············menuSaveAs.Text = "Save &as...";
191 ············menuSaveAs.Click +=··menuSaveAs_Click ;
192 ············//
193 ············// MainForm
194 ············//
195 ············AutoScaleBaseSize = new Size( 6, 16 );
196 ············ClientSize = new Size( 784, 432 );
197 ············Controls.Add( propertyGrid1 );
198 ············Controls.Add( splitter1 );
199 ············Controls.Add( allObjects );
200 ············Controls.Add( mainMenu );
201 ············Icon = (Icon) resources.GetObject( "$this.Icon" )!;
202 ············MainMenuStrip = mainMenu;
203 ············Name = "MainForm";
204 ············Text = "NDO Mapping Tool";
205 ············Closing +=··MainForm_Closing ;
206 ············Load +=··MainForm_Load ;
207 ············mainMenu.ResumeLayout( false );
208 ············mainMenu.PerformLayout();
209 ············ResumeLayout( false );
210 ············PerformLayout();
211 ········}
212 ········#endregion
213
214 ········/// <summary>
215 ········/// Der Haupteinstiegspunkt für die Anwendung.
216 ········/// </summary>
217 ········[STAThread]
218 ········static void Main( string[] args )
219 ········{
220 ············Application.Run( new MainForm( args ) );
221 ········}
222
223 ········private void LoadMapping( string fileName )
224 ········{
225 ············allObjects.Nodes.Clear();
226 ············try
227 ············{
228 mapping = new NDOMapping( fileName, null ) ;
229 ················PrepareRelations();
230 ············}
231 ············catch (Exception ex)
232 ············{
233 ················mapping = null;
234 ················this.Text = string.Empty;
235 #if DEBUG
236 ················MessageBox.Show( ex.ToString(), "Error" );
237 #else
238 ················MessageBox.Show(ex.Message, "Error");
239 #endif
240 ················return;
241 ············}
242 ············this.Text = fileName;
243 ············if (mapping != null)
244 ················FillNodes();
245 ········}
246
247
248 ········private void FillNodes()
249 ········{
250 ············NDOMappingNode mn = new NDOMappingNode(this.mapping!);
251 ············allObjects.Nodes.Add( mn );
252 ········}
253
254 ········private void ScanArgs( string[] args )
255 ········{
256 ············Regex regexm = new Regex(@"-m:(.*)");
257 ············foreach (string arg in args)
258 ············{
259 ················Match matchm = regexm.Match(arg);
260 ················if (matchm.Success)
261 ················{
262 ····················LoadMapping( matchm.Groups[1].Value );
263 ····················saveOnClose = true;
264 ················}
265 ············}
266 ········}
267
268
269 ········private void MainForm_Load( object? sender, System.EventArgs e )
270 ········{
271 ········}
272
273 ········private void allObjects_AfterSelect( object? sender, TreeViewEventArgs e )
274 ········{
275 ············var tn = e.Node as NDOTreeNode;
276 ············if (tn != null)
277 ················this.propertyGrid1.SelectedObject = tn.Object;
278 ············else
279 ················this.propertyGrid1.SelectedObject = null;
280 ········}
281
282 ········private void MainForm_Closing( object? sender, System.ComponentModel.CancelEventArgs e )
283 ········{
284 ············if (saveOnClose && mapping != null)
285 ················mapping.Save();
286 ············else if (mapping != null && mapping.HasChanges)
287 ············{
288 ················if (MessageBox.Show( "Save changes?", "NDO Mapping Tool", MessageBoxButtons.YesNo, MessageBoxIcon.Question ) == DialogResult.Yes)
289 ················{
290 ····················mapping.Save();
291 ················}
292 ············}
293 ········}
294
295 ········private void menuOpen_Click( object? sender, System.EventArgs e )
296 ········{
297 ············if (mapping != null && mapping.HasChanges)
298 ············{
299 ················if (MessageBox.Show( "Save changes?", "Mapping Tool", MessageBoxButtons.YesNo ) == DialogResult.Yes)
300 ················{
301 ····················mapping.Save();
302 ····················mapping = null;
303 ················}
304 ············}
305
306 ············OpenFileDialog ofd = new OpenFileDialog();
307 ············ofd.CheckFileExists = true;
308 ············ofd.DefaultExt = "xml";
309 ············ofd.Multiselect = false;
310 ············ofd.Filter = "Mapping Files (*.xml)|*.xml";
311 ············ofd.FileName = "NDOMapping.xml";
312 ············if (ofd.ShowDialog() == DialogResult.Cancel)
313 ················return;
314 ············LoadMapping( ofd.FileName );
315 ········}
316
317
318 ········bool PrepareRelations()
319 ········{
320 ············bool result = false;
321 ············if (mapping != null)
322 ············{
323 ················foreach (Class cl in mapping.Classes)
324 ················{
325 ····················foreach (Relation r in cl.Relations)
326 ····················{
327 ························// This computes the foreign relation, which is
328 ························// needed to avoid a null pointer exception
329 ························// while binding to the property grid.
330 ························result = result || r.Bidirectional;
331 ····················}
332 ················}
333 ············}
334
335 ············return result;··// Nobody reads this value, but we make shure, that the code won't get removed by the optimizer.
336 ········}
337
338
339 ········private void menuSave_Click( object? sender, System.EventArgs e )
340 ········{
341 ············if (mapping != null)
342 ················mapping.Save();
343 ········}
344
345 ········private void menuSaveAs_Click( object? sender, System.EventArgs e )
346 ········{
347 ············if (mapping != null)
348 ············{
349 ················SaveFileDialog sfd = new SaveFileDialog();
350 ················if (mapping.FileName != string.Empty)
351 ····················sfd.InitialDirectory = Path.GetDirectoryName( mapping.FileName );
352 ················sfd.CheckFileExists = false;
353 ················sfd.DefaultExt = "xml";
354 ················sfd.Filter = "Mapping Files (*.xml)|*.xml";
355 ················sfd.FileName = "NDOMapping.xml";
356 ················if (sfd.ShowDialog( this ) != DialogResult.OK)
357 ····················return;
358 ················mapping.SaveAs( sfd.FileName );
359 ················this.Text = sfd.FileName;
360 ············}
361 ········}
362
363 ········private void OnIdle( object? sender, EventArgs e )
364 ········{
365 ············menuSave.Enabled = mapping != null && mapping.HasChanges;
366 ············menuOpen.Enabled = !saveOnClose;
367 ········}
368
369 #if DEBUG
370 ········private void MainForm_KeyPress( object? sender, KeyPressEventArgs e )
371 ········{
372
373 ············if ((int) e.KeyChar == 27)
374 ················MessageBox.Show( "Escape" );
375 ········}
376 #endif
377
378 ········private void allObjects_MouseUp( object? sender, MouseEventArgs e )
379 ········{
380
381 ············if (e.Button == MouseButtons.Right)
382 ············{
383 ················var tn = this.allObjects.GetNodeAt(e.X, e.Y) as NDOTreeNode;
384 ················if (tn != null)
385 ················{
386 ····················allObjects.SelectedNode = tn;
387 ····················var menu = tn.GetContextMenu();
388 ····················if (menu.Items.Count > 0)
389 ························menu.Show(allObjects, new Point(e.X, e.Y));
390 ····················this.propertyGrid1.SelectedObject = tn.Object;
391 ················}
392 ············}
393 ········}
394 ····}
395 }
396