Datei: SimpleMappingTool/MainForm.cs

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