Subversion Repositories DevTools

Rev

Rev 2169 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2139 ghuddy 1
//-----------------------------------------------------------------------
2
// This is open source licensed under GPL
3
//
4
//
5
using System;
6
using System.Text;
7
using System.Globalization;
2141 ghuddy 8
using System.Collections;
2139 ghuddy 9
using System.Windows.Forms;
10
using ReqPro40;
2151 ghuddy 11
using System.Threading;
2139 ghuddy 12
 
13
namespace EA_ReqPro
14
{
15
   public class Main
16
   {
2141 ghuddy 17
      public String EA_Connect(EA.Repository Repository)
2139 ghuddy 18
      {
19
         return "a string";
20
      }
21
 
2151 ghuddy 22
      private Thread ourThread = null;
2139 ghuddy 23
 
2141 ghuddy 24
      public readonly static String GUI_OUTPUT_TAB_NAME = "EA_ReqPro";
2151 ghuddy 25
      public static EA.Repository EA_Repository = null;
2139 ghuddy 26
 
2155 ghuddy 27
      public static int abortRequests = 0;
2151 ghuddy 28
      public static bool mustAbort = false;
2139 ghuddy 29
 
2161 ghuddy 30
      // EA_ReqPro Menu strings
31
      private const string MI_Root            = "-&EA ReqPro";
32
 
33
      private const string MI_Separator                  = "-";
34
      private const string MI_ImportForTraceabilityUse   = "&Import for Traceability Use";
35
      private const string MI_DisplayChangeLog           = "&Display Change Log";
36
      private const string MI_ImportForDocModelUse       = "&Import for Document Model Use";
37
      private const string MI_ExportToReqPro             = "&Export to ReqPro";
38
      private const string MI_ExportToReqProCSV          = "&Export to ReqPro Compatible CSV File";
39
      private const string MI_CreateReqDiagram           = "&Create Requirement Diagram";
40
      private const string MI_CreateReqDiagramUsingSrch  = "&Create Requirement Diagram Using Model Search";
41
      private const string MI_About                      = "&About...";
42
      private const string MI_Abort                      = "&Abort Import/Export";
43
 
44
      private string[] MI_RootMenu = new string[] { MI_ImportForTraceabilityUse, 
45
                                                    MI_DisplayChangeLog,
46
                                                    MI_Separator,
47
                                                    MI_ImportForDocModelUse,
48
                                                    MI_Separator,
49
                                                    MI_ExportToReqPro,
50
                                                    MI_ExportToReqProCSV,
51
                                                    MI_Separator,
52
                                                    MI_CreateReqDiagram,
53
                                                    MI_CreateReqDiagramUsingSrch,
54
                                                    MI_Separator,
55
                                                    MI_About };
56
 
57
      private string[] MI_AbortMenu = new string[] { MI_Abort };
58
 
59
 
2141 ghuddy 60
      /// <summary>
61
      /// Called when EA initialised. Creates an output tab for the add in.
62
      /// </summary>
63
      /// <param name="repository"></param>
64
      public void EA_OnPostInitialized(EA.Repository repository)
65
      {
66
         repository.CreateOutputTab(GUI_OUTPUT_TAB_NAME);
67
         repository.EnsureOutputVisible(GUI_OUTPUT_TAB_NAME);
68
      }
2159 ghuddy 69
 
70
 
2141 ghuddy 71
      public void EA_Disconnect()
72
      {
73
         GC.Collect();
74
         GC.WaitForPendingFinalizers();
75
      }
2139 ghuddy 76
 
77
 
2141 ghuddy 78
      /// <summary>
79
      /// Event called when user clicks on an entry in the output
80
      /// window. If an element has been associated with the message, it will
81
      /// be automatically selected in the project browser.
82
      /// </summary>
83
      /// <param name="repository"></param>
84
      /// <param name="outputTabNea"></param>
85
      /// <param name="lineText"></param>
86
      /// <param name="lineIdentifier"></param>
87
      public void EA_OnOutputItemClicked( EA.Repository repository,
88
                                          String outputTabName,
89
                                          String lineText,
90
                                          Int32 identifier)
91
      {
92
         if ((outputTabName == GUI_OUTPUT_TAB_NAME) && (identifier > 0))
93
         {
94
            try
95
            {
2175 ghuddy 96
               EA.Element element = repository.GetElementByID(identifier);
97
               if (element.MetaType == "Package")
98
               {
99
                  EA.Package package = repository.GetPackageByID(element.PackageID);
100
                  repository.ShowInProjectView(package);
101
               }
102
               else
103
               {
104
                  repository.ShowInProjectView(element);
105
               }
2141 ghuddy 106
            }
107
            catch
108
            {
109
               try
110
               {
2175 ghuddy 111
                  EA.Package package = repository.GetPackageByID(identifier);
112
                  repository.ShowInProjectView(package);
2141 ghuddy 113
               }
114
               catch
115
               {
2159 ghuddy 116
                  try
117
                  {
118
                     EA.Diagram diagram = repository.GetDiagramByID(identifier);
119
                     repository.ShowInProjectView(diagram);
120
                  }
121
                  catch
122
                  {
123
                  }
2141 ghuddy 124
               }
125
            }
2175 ghuddy 126
 
2141 ghuddy 127
         }
128
      }
2139 ghuddy 129
 
2141 ghuddy 130
 
131
      public object EA_GetMenuItems(EA.Repository repository, string location, string menu)
2139 ghuddy 132
      {
133
         switch( menu )
134
         {
135
            case "":
2161 ghuddy 136
               return MI_Root;
2139 ghuddy 137
 
2161 ghuddy 138
            case MI_Root:
2151 ghuddy 139
               if (ourThread != null && ourThread.IsAlive == true)
140
               {
2161 ghuddy 141
                  return MI_AbortMenu;
2151 ghuddy 142
               }
143
               else
144
               {
2161 ghuddy 145
                  return MI_RootMenu;
2151 ghuddy 146
               }
2139 ghuddy 147
         }
2151 ghuddy 148
 
2139 ghuddy 149
         return "";
150
      }
151
 
152
 
153
      bool IsProjectOpen(EA.Repository Repository)
154
      {
155
         try
156
         {
157
            EA.Collection collection = Repository.Models;
158
            return true;
159
         }
160
         catch
161
         {
162
            return false;
163
         }
164
      }
165
 
166
 
167
      public void EA_GetMenuState(EA.Repository repository, string location, string menuName, string itemName, ref bool isEnabled, ref bool isChecked)
168
      {
2151 ghuddy 169
         EA_Repository = repository;
170
 
2139 ghuddy 171
         object o;
172
         EA.ObjectType type;
173
 
174
         isChecked = false;
175
 
2151 ghuddy 176
 
2139 ghuddy 177
         if (IsProjectOpen(repository))
178
         {
179
            switch (itemName)
180
            {
2161 ghuddy 181
               case MI_Abort:
182
               case MI_ImportForDocModelUse:
183
               case MI_ExportToReqPro:
184
               case MI_ExportToReqProCSV:
185
               case MI_CreateReqDiagram:
186
               case MI_CreateReqDiagramUsingSrch:
187
               case MI_ImportForTraceabilityUse:
2141 ghuddy 188
                  isEnabled = true;
189
                  break;
190
 
2161 ghuddy 191
               case MI_DisplayChangeLog:
2141 ghuddy 192
                  type = repository.GetTreeSelectedItem(out o);
2167 ghuddy 193
                  if ( (type == EA.ObjectType.otElement) && (((EA.Element)o).Name.StartsWith("Change Log")))
2139 ghuddy 194
                  {
195
                     isEnabled = true;
196
                  }
197
                  else
198
                  {
199
                     isEnabled = false;
200
                  }
201
                  break;
202
            }
203
         }
204
         else
205
         {
206
            isEnabled = false;
207
         }
208
      }
209
 
210
 
211
      public void EA_MenuClick(EA.Repository repository, string location, string menu, string itemName)
212
      {
2151 ghuddy 213
         EA_Repository = repository;
214
 
2139 ghuddy 215
         switch( itemName )
216
         {
2155 ghuddy 217
            // This menu option appears only when a thread is active. It allows the user to initiate
218
            // an abort of the thread.
2161 ghuddy 219
            case MI_Abort:
2151 ghuddy 220
               if (ourThread != null && ourThread.IsAlive == true)
221
               {
2155 ghuddy 222
                  if (abortRequests > 0)
223
                  {
224
                     // if user has made a second abort request, it is probably because the thread does not
225
                     // appear to be aborting. This can happen sometimes. I have noticed occasional lockups,
226
                     // and every time this happens, the thread seems to be locked up in a call to an EA
227
                     // automation interface function, so perhaps there is some wierd interaction between
228
                     // the thread and the use of the menu in the main EA process? Anyway, lets try to abort
229
                     // the thread now because it does not appear as if it is going to quit gracefully.
230
                     ourThread.Abort();
231
                     ReqProDatabase.close();
232
                     break;
233
                  }
234
                  else
235
                  {
236
                     // try to quit gracefully.
237
                     mustAbort = true;
238
                     abortRequests++;
239
                  }
2151 ghuddy 240
               }
241
               break;
242
 
2161 ghuddy 243
            case MI_ImportForTraceabilityUse:
2151 ghuddy 244
               if (ourThread == null || ourThread.IsAlive == false)
245
               {
246
                  ourThread = new Thread(new ThreadStart(ImportReqProDatabase_Thread));
247
                  ourThread.ApartmentState = ApartmentState.STA;
248
                  ourThread.Start();
249
               }
2139 ghuddy 250
               break;
251
 
2161 ghuddy 252
            case MI_ImportForDocModelUse:
2151 ghuddy 253
               if (ourThread == null || ourThread.IsAlive == false)
254
               {
255
                  ourThread = new Thread(new ThreadStart(CopyReqProDatabase_Thread));
256
                  ourThread.ApartmentState = ApartmentState.STA;
257
                  ourThread.Start();
258
               }
2139 ghuddy 259
               break;
260
 
2161 ghuddy 261
            case MI_ExportToReqPro:
2155 ghuddy 262
               if (ourThread == null || ourThread.IsAlive == false)
263
               {
264
                  ourThread = new Thread(new ThreadStart(ExportToReqPro_Thread));
265
                  ourThread.ApartmentState = ApartmentState.STA;
266
                  ourThread.Start();
267
               }
268
               break;
269
 
2161 ghuddy 270
            case MI_ExportToReqProCSV:
2141 ghuddy 271
               repository.EnsureOutputVisible(GUI_OUTPUT_TAB_NAME);
2151 ghuddy 272
               repository.WriteOutput(GUI_OUTPUT_TAB_NAME,"",-1);
2141 ghuddy 273
               ExportToReqProCSVForm export_dlg = new ExportToReqProCSVForm();
274
               DialogResult dlgRes = export_dlg.ShowDialog();
275
               if (dlgRes == DialogResult.OK)
2139 ghuddy 276
               {
2151 ghuddy 277
                  export_dlg.Export();
2139 ghuddy 278
               }
2141 ghuddy 279
               break;
2139 ghuddy 280
 
2161 ghuddy 281
            case MI_DisplayChangeLog:
2139 ghuddy 282
            {
2151 ghuddy 283
               ImportReqProDatabase import_parser = new ImportReqProDatabase();
2141 ghuddy 284
               EA.Element changeLog = (EA.Element)repository.GetTreeSelectedObject();
285
               repository.EnsureOutputVisible(GUI_OUTPUT_TAB_NAME);
2151 ghuddy 286
               import_parser.displayChangeLog(changeLog);
2141 ghuddy 287
               break;
2139 ghuddy 288
            }
2145 ghuddy 289
 
2161 ghuddy 290
            case MI_CreateReqDiagram:
2145 ghuddy 291
            {
292
               ArrayList allowedElementTypes = new ArrayList();
293
               allowedElementTypes.Add("Requirement");
294
               //allowedElementTypes.Add("UseCase");
2169 ghuddy 295
               EA_ElementAccumulator reqLister = new EA_ElementAccumulator(allowedElementTypes);
2145 ghuddy 296
               EA.Package thePackage = (EA.Package)repository.GetTreeSelectedObject();
2169 ghuddy 297
               EA_Parsing.findAndProcessPackageElements( thePackage, reqLister, true );
2155 ghuddy 298
               createRequirementDiagram(thePackage, reqLister.Elements);
299
               break;
300
            }
2145 ghuddy 301
 
2161 ghuddy 302
            case MI_CreateReqDiagramUsingSrch:
2155 ghuddy 303
            {
304
               QueryForm qf = new QueryForm();
305
               DialogResult qf_dlgRes = qf.ShowDialog();
306
               if (qf_dlgRes == DialogResult.OK)
2145 ghuddy 307
               {
2155 ghuddy 308
                  EA.Package thePackage = (EA.Package)repository.GetTreeSelectedObject();
309
                  EA.Collection c = EA_Repository.GetElementsByQuery(qf.textBox_query_name.Text, qf.textBox_search_term.Text);
310
                  createRequirementDiagram(thePackage, c);
2145 ghuddy 311
               }
312
               break;
313
            }
314
 
2161 ghuddy 315
            case MI_About:
2169 ghuddy 316
               AboutForm anAbout = new AboutForm();
317
               anAbout.ShowDialog();
2161 ghuddy 318
               break;
2139 ghuddy 319
         }
320
      }
321
 
2155 ghuddy 322
      private void createRequirementDiagram(EA.Package containerPackage, EA.Collection c)
323
      {
324
         EA.Diagram newDiagram = (EA.Diagram)containerPackage.Diagrams.AddNew("Requirements","Logical");
325
         newDiagram.Update();
326
         containerPackage.Update();
327
 
328
         foreach (EA.Element element in c)
329
         {
330
            EA.DiagramObject newDiagramObject = (EA.DiagramObject)newDiagram.DiagramObjects.AddNew("l=1;r=10;t=1;b=1", "");
331
            newDiagramObject.ElementID = element.ElementID;
332
            newDiagramObject.Update();
333
         }
334
         newDiagram.DiagramObjects.Refresh();
335
         containerPackage.Update();
2159 ghuddy 336
         EA_Repository.GetProjectInterface().LayoutDiagramEx( newDiagram.DiagramGUID,
2155 ghuddy 337
            EA.ConstLayoutStyles.lsDiagramDefault, 4, 20, 20, false);
338
      }
339
 
340
      private void createRequirementDiagram(EA.Package containerPackage, ArrayList elements)
341
      {
342
         EA.Diagram newDiagram = (EA.Diagram)containerPackage.Diagrams.AddNew("Requirements","Logical");
343
         newDiagram.Update();
344
         containerPackage.Update();
345
 
346
         foreach (EA.Element element in elements)
347
         {
348
            EA.DiagramObject newDiagramObject = (EA.DiagramObject)newDiagram.DiagramObjects.AddNew("l=1;r=10;t=1;b=1", "");
349
            newDiagramObject.ElementID = element.ElementID;
350
            newDiagramObject.Update();
351
         }
352
         newDiagram.DiagramObjects.Refresh();
353
         containerPackage.Update();
2159 ghuddy 354
         EA_Repository.GetProjectInterface().LayoutDiagramEx( newDiagram.DiagramGUID,
2155 ghuddy 355
            EA.ConstLayoutStyles.lsDiagramDefault, 4, 20, 20, false);
356
      }
357
 
358
 
2159 ghuddy 359
      /// <summary>
360
      /// Thread function for import for traceability use operation
2155 ghuddy 361
      /// </summary>
2151 ghuddy 362
      private static void ImportReqProDatabase_Thread()
363
      {
2155 ghuddy 364
         bool cancelled = false;
2151 ghuddy 365
         mustAbort = false;
2155 ghuddy 366
         abortRequests = 0;
2151 ghuddy 367
         EA_Repository.EnsureOutputVisible(GUI_OUTPUT_TAB_NAME);
368
         EA_Repository.WriteOutput(GUI_OUTPUT_TAB_NAME,"",-1);
2155 ghuddy 369
         ImportReqProDatabase import_parser = new ImportReqProDatabase();
2159 ghuddy 370
         import_parser.prompt_and_parse(ReqProDB_Artifact.MODE.TRACEABILITY, out cancelled);
2151 ghuddy 371
         if (mustAbort)
2155 ghuddy 372
         {
373
            EA_Repository.WriteOutput(GUI_OUTPUT_TAB_NAME, "Import Aborted", -1);
2153 ghuddy 374
            MessageBoxEx.Show("Import Aborted", "Progress");
2155 ghuddy 375
         }
2159 ghuddy 376
         else if (cancelled)
377
         {
378
            Main.WriteOutput("Import Cancelled",-1);
2155 ghuddy 379
         }
380
         abortRequests = 0;
381
         ReqProDatabase.close();
2151 ghuddy 382
      }
2139 ghuddy 383
 
2159 ghuddy 384
      /// <summary>
385
      /// Thread function for import for document model use operation
2155 ghuddy 386
      /// </summary>
2151 ghuddy 387
      private static void CopyReqProDatabase_Thread()
388
      {
2155 ghuddy 389
         bool cancelled = false;
2151 ghuddy 390
         mustAbort = false;
2155 ghuddy 391
         abortRequests = 0;
2151 ghuddy 392
         EA_Repository.EnsureOutputVisible(GUI_OUTPUT_TAB_NAME);
393
         EA_Repository.WriteOutput(GUI_OUTPUT_TAB_NAME,"",-1);
394
         CopyReqProDatabase copy_parser = new CopyReqProDatabase();
2155 ghuddy 395
         copy_parser.prompt_and_parse(ReqProDB_Artifact.MODE.DOC_MODEL, out cancelled);
2151 ghuddy 396
         if (mustAbort)
2155 ghuddy 397
         {
398
            EA_Repository.WriteOutput(GUI_OUTPUT_TAB_NAME, "Import Aborted", -1);
2153 ghuddy 399
            MessageBoxEx.Show("Import Aborted", "Progress");
2155 ghuddy 400
         }
2159 ghuddy 401
         else if (cancelled)
402
         {
403
            Main.WriteOutput("Import Cancelled",-1);
2155 ghuddy 404
         }
405
         abortRequests = 0;
406
         ReqProDatabase.close();
2151 ghuddy 407
      }
408
 
2159 ghuddy 409
      /// <summary>
410
      /// Thread function for export to reqpro database operation
2155 ghuddy 411
      /// </summary>
412
      private static void ExportToReqPro_Thread()
413
      {
414
         bool cancelled = false;
415
         mustAbort = false;
416
         abortRequests = 0;
417
         EA_Repository.EnsureOutputVisible(GUI_OUTPUT_TAB_NAME);
418
         EA_Repository.WriteOutput(GUI_OUTPUT_TAB_NAME,"",-1);
419
         ExportToReqProDatabase export_parser = new ExportToReqProDatabase();
420
         export_parser.prompt_and_parse(ReqProDB_Artifact.MODE.EXPORT, out cancelled);
421
         if (mustAbort)
422
         {
423
            EA_Repository.WriteOutput(GUI_OUTPUT_TAB_NAME, "Export Aborted", -1);
424
            MessageBoxEx.Show("Export Aborted", "Progress");
425
         }
2159 ghuddy 426
         else if (cancelled)
427
         {
428
            Main.WriteOutput("Export Cancelled",-1);
2155 ghuddy 429
         }
430
         abortRequests = 0;
431
         ReqProDatabase.close();
432
      }
433
 
2153 ghuddy 434
      public static void MessageBoxException(Exception exc)
435
      {
436
         MessageBoxException(exc, "Exception");
437
      }
438
 
439
      public static void MessageBoxException(Exception exc, string caption)
440
      {
2159 ghuddy 441
         MessageBoxEx.Show(exc.ToString(), caption, MessageBoxIcon.Error);
2153 ghuddy 442
      }
2155 ghuddy 443
 
2159 ghuddy 444
      public static void WriteOutput(string s, int id)
445
      {
446
         EA_Repository.WriteOutput( GUI_OUTPUT_TAB_NAME, s, id);
447
      }
2155 ghuddy 448
 
449
      public static void ClearOutput()
450
      {
451
         EA_Repository.ClearOutput(GUI_OUTPUT_TAB_NAME);
452
      }
453
      public static void WriteSeperator()
454
      {
455
         EA_Repository.WriteOutput( GUI_OUTPUT_TAB_NAME, "----------------------------------------------------------------------------------------------------------------------", -1);
456
      }
2139 ghuddy 457
   }
458
}