Datei: SimpleMappingTool/MainForm.cs

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