Subversion Repositories DevTools

Rev

Rev 2167 | Rev 2175 | 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
            {
2159 ghuddy 96
               EA.Package package = repository.GetPackageByID(identifier);
97
               repository.ShowInProjectView(package);
2141 ghuddy 98
            }
99
            catch
100
            {
101
               try
102
               {
2159 ghuddy 103
                  EA.Element element = repository.GetElementByID(identifier);
104
                  if (element.MetaType == "Package")
105
                  {
106
                     EA.Package package = repository.GetPackageByID(element.PackageID);
107
                     repository.ShowInProjectView(package);
108
                  }
109
                  else
110
                  {
111
                     repository.ShowInProjectView(element);
112
                  }
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
            }
126
         }
127
      }
2139 ghuddy 128
 
2141 ghuddy 129
 
130
      public object EA_GetMenuItems(EA.Repository repository, string location, string menu)
2139 ghuddy 131
      {
132
         switch( menu )
133
         {
134
            case "":
2161 ghuddy 135
               return MI_Root;
2139 ghuddy 136
 
2161 ghuddy 137
            case MI_Root:
2151 ghuddy 138
               if (ourThread != null && ourThread.IsAlive == true)
139
               {
2161 ghuddy 140
                  return MI_AbortMenu;
2151 ghuddy 141
               }
142
               else
143
               {
2161 ghuddy 144
                  return MI_RootMenu;
2151 ghuddy 145
               }
2139 ghuddy 146
         }
2151 ghuddy 147
 
2139 ghuddy 148
         return "";
149
      }
150
 
151
 
152
      bool IsProjectOpen(EA.Repository Repository)
153
      {
154
         try
155
         {
156
            EA.Collection collection = Repository.Models;
157
            return true;
158
         }
159
         catch
160
         {
161
            return false;
162
         }
163
      }
164
 
165
 
166
      public void EA_GetMenuState(EA.Repository repository, string location, string menuName, string itemName, ref bool isEnabled, ref bool isChecked)
167
      {
2151 ghuddy 168
         EA_Repository = repository;
169
 
2139 ghuddy 170
         object o;
171
         EA.ObjectType type;
172
 
173
         isChecked = false;
174
 
2151 ghuddy 175
 
2139 ghuddy 176
         if (IsProjectOpen(repository))
177
         {
178
            switch (itemName)
179
            {
2161 ghuddy 180
               case MI_Abort:
181
               case MI_ImportForDocModelUse:
182
               case MI_ExportToReqPro:
183
               case MI_ExportToReqProCSV:
184
               case MI_CreateReqDiagram:
185
               case MI_CreateReqDiagramUsingSrch:
186
               case MI_ImportForTraceabilityUse:
2141 ghuddy 187
                  isEnabled = true;
188
                  break;
189
 
2161 ghuddy 190
               case MI_DisplayChangeLog:
2141 ghuddy 191
                  type = repository.GetTreeSelectedItem(out o);
2167 ghuddy 192
                  if ( (type == EA.ObjectType.otElement) && (((EA.Element)o).Name.StartsWith("Change Log")))
2139 ghuddy 193
                  {
194
                     isEnabled = true;
195
                  }
196
                  else
197
                  {
198
                     isEnabled = false;
199
                  }
200
                  break;
201
            }
202
         }
203
         else
204
         {
205
            isEnabled = false;
206
         }
207
      }
208
 
209
 
210
      public void EA_MenuClick(EA.Repository repository, string location, string menu, string itemName)
211
      {
2151 ghuddy 212
         EA_Repository = repository;
213
 
2139 ghuddy 214
         switch( itemName )
215
         {
2155 ghuddy 216
            // This menu option appears only when a thread is active. It allows the user to initiate
217
            // an abort of the thread.
2161 ghuddy 218
            case MI_Abort:
2151 ghuddy 219
               if (ourThread != null && ourThread.IsAlive == true)
220
               {
2155 ghuddy 221
                  if (abortRequests > 0)
222
                  {
223
                     // if user has made a second abort request, it is probably because the thread does not
224
                     // appear to be aborting. This can happen sometimes. I have noticed occasional lockups,
225
                     // and every time this happens, the thread seems to be locked up in a call to an EA
226
                     // automation interface function, so perhaps there is some wierd interaction between
227
                     // the thread and the use of the menu in the main EA process? Anyway, lets try to abort
228
                     // the thread now because it does not appear as if it is going to quit gracefully.
229
                     ourThread.Abort();
230
                     ReqProDatabase.close();
231
                     break;
232
                  }
233
                  else
234
                  {
235
                     // try to quit gracefully.
236
                     mustAbort = true;
237
                     abortRequests++;
238
                  }
2151 ghuddy 239
               }
240
               break;
241
 
2161 ghuddy 242
            case MI_ImportForTraceabilityUse:
2151 ghuddy 243
               if (ourThread == null || ourThread.IsAlive == false)
244
               {
245
                  ourThread = new Thread(new ThreadStart(ImportReqProDatabase_Thread));
246
                  ourThread.ApartmentState = ApartmentState.STA;
247
                  ourThread.Start();
248
               }
2139 ghuddy 249
               break;
250
 
2161 ghuddy 251
            case MI_ImportForDocModelUse:
2151 ghuddy 252
               if (ourThread == null || ourThread.IsAlive == false)
253
               {
254
                  ourThread = new Thread(new ThreadStart(CopyReqProDatabase_Thread));
255
                  ourThread.ApartmentState = ApartmentState.STA;
256
                  ourThread.Start();
257
               }
2139 ghuddy 258
               break;
259
 
2161 ghuddy 260
            case MI_ExportToReqPro:
2155 ghuddy 261
               if (ourThread == null || ourThread.IsAlive == false)
262
               {
263
                  ourThread = new Thread(new ThreadStart(ExportToReqPro_Thread));
264
                  ourThread.ApartmentState = ApartmentState.STA;
265
                  ourThread.Start();
266
               }
267
               break;
268
 
2161 ghuddy 269
            case MI_ExportToReqProCSV:
2141 ghuddy 270
               repository.EnsureOutputVisible(GUI_OUTPUT_TAB_NAME);
2151 ghuddy 271
               repository.WriteOutput(GUI_OUTPUT_TAB_NAME,"",-1);
2141 ghuddy 272
               ExportToReqProCSVForm export_dlg = new ExportToReqProCSVForm();
273
               DialogResult dlgRes = export_dlg.ShowDialog();
274
               if (dlgRes == DialogResult.OK)
2139 ghuddy 275
               {
2151 ghuddy 276
                  export_dlg.Export();
2139 ghuddy 277
               }
2141 ghuddy 278
               break;
2139 ghuddy 279
 
2161 ghuddy 280
            case MI_DisplayChangeLog:
2139 ghuddy 281
            {
2151 ghuddy 282
               ImportReqProDatabase import_parser = new ImportReqProDatabase();
2141 ghuddy 283
               EA.Element changeLog = (EA.Element)repository.GetTreeSelectedObject();
284
               repository.EnsureOutputVisible(GUI_OUTPUT_TAB_NAME);
2151 ghuddy 285
               import_parser.displayChangeLog(changeLog);
2141 ghuddy 286
               break;
2139 ghuddy 287
            }
2145 ghuddy 288
 
2161 ghuddy 289
            case MI_CreateReqDiagram:
2145 ghuddy 290
            {
291
               ArrayList allowedElementTypes = new ArrayList();
292
               allowedElementTypes.Add("Requirement");
293
               //allowedElementTypes.Add("UseCase");
2169 ghuddy 294
               EA_ElementAccumulator reqLister = new EA_ElementAccumulator(allowedElementTypes);
2145 ghuddy 295
               EA.Package thePackage = (EA.Package)repository.GetTreeSelectedObject();
2169 ghuddy 296
               EA_Parsing.findAndProcessPackageElements( thePackage, reqLister, true );
2155 ghuddy 297
               createRequirementDiagram(thePackage, reqLister.Elements);
298
               break;
299
            }
2145 ghuddy 300
 
2161 ghuddy 301
            case MI_CreateReqDiagramUsingSrch:
2155 ghuddy 302
            {
303
               QueryForm qf = new QueryForm();
304
               DialogResult qf_dlgRes = qf.ShowDialog();
305
               if (qf_dlgRes == DialogResult.OK)
2145 ghuddy 306
               {
2155 ghuddy 307
                  EA.Package thePackage = (EA.Package)repository.GetTreeSelectedObject();
308
                  EA.Collection c = EA_Repository.GetElementsByQuery(qf.textBox_query_name.Text, qf.textBox_search_term.Text);
309
                  createRequirementDiagram(thePackage, c);
2145 ghuddy 310
               }
311
               break;
312
            }
313
 
2161 ghuddy 314
            case MI_About:
2169 ghuddy 315
               AboutForm anAbout = new AboutForm();
316
               anAbout.ShowDialog();
2161 ghuddy 317
               break;
2139 ghuddy 318
         }
319
      }
320
 
2155 ghuddy 321
      private void createRequirementDiagram(EA.Package containerPackage, EA.Collection c)
322
      {
323
         EA.Diagram newDiagram = (EA.Diagram)containerPackage.Diagrams.AddNew("Requirements","Logical");
324
         newDiagram.Update();
325
         containerPackage.Update();
326
 
327
         foreach (EA.Element element in c)
328
         {
329
            EA.DiagramObject newDiagramObject = (EA.DiagramObject)newDiagram.DiagramObjects.AddNew("l=1;r=10;t=1;b=1", "");
330
            newDiagramObject.ElementID = element.ElementID;
331
            newDiagramObject.Update();
332
         }
333
         newDiagram.DiagramObjects.Refresh();
334
         containerPackage.Update();
2159 ghuddy 335
         EA_Repository.GetProjectInterface().LayoutDiagramEx( newDiagram.DiagramGUID,
2155 ghuddy 336
            EA.ConstLayoutStyles.lsDiagramDefault, 4, 20, 20, false);
337
      }
338
 
339
      private void createRequirementDiagram(EA.Package containerPackage, ArrayList elements)
340
      {
341
         EA.Diagram newDiagram = (EA.Diagram)containerPackage.Diagrams.AddNew("Requirements","Logical");
342
         newDiagram.Update();
343
         containerPackage.Update();
344
 
345
         foreach (EA.Element element in elements)
346
         {
347
            EA.DiagramObject newDiagramObject = (EA.DiagramObject)newDiagram.DiagramObjects.AddNew("l=1;r=10;t=1;b=1", "");
348
            newDiagramObject.ElementID = element.ElementID;
349
            newDiagramObject.Update();
350
         }
351
         newDiagram.DiagramObjects.Refresh();
352
         containerPackage.Update();
2159 ghuddy 353
         EA_Repository.GetProjectInterface().LayoutDiagramEx( newDiagram.DiagramGUID,
2155 ghuddy 354
            EA.ConstLayoutStyles.lsDiagramDefault, 4, 20, 20, false);
355
      }
356
 
357
 
2159 ghuddy 358
      /// <summary>
359
      /// Thread function for import for traceability use operation
2155 ghuddy 360
      /// </summary>
2151 ghuddy 361
      private static void ImportReqProDatabase_Thread()
362
      {
2155 ghuddy 363
         bool cancelled = false;
2151 ghuddy 364
         mustAbort = false;
2155 ghuddy 365
         abortRequests = 0;
2151 ghuddy 366
         EA_Repository.EnsureOutputVisible(GUI_OUTPUT_TAB_NAME);
367
         EA_Repository.WriteOutput(GUI_OUTPUT_TAB_NAME,"",-1);
2155 ghuddy 368
         ImportReqProDatabase import_parser = new ImportReqProDatabase();
2159 ghuddy 369
         import_parser.prompt_and_parse(ReqProDB_Artifact.MODE.TRACEABILITY, out cancelled);
2151 ghuddy 370
         if (mustAbort)
2155 ghuddy 371
         {
372
            EA_Repository.WriteOutput(GUI_OUTPUT_TAB_NAME, "Import Aborted", -1);
2153 ghuddy 373
            MessageBoxEx.Show("Import Aborted", "Progress");
2155 ghuddy 374
         }
2159 ghuddy 375
         else if (cancelled)
376
         {
377
            Main.WriteOutput("Import Cancelled",-1);
2155 ghuddy 378
         }
379
         abortRequests = 0;
380
         ReqProDatabase.close();
2151 ghuddy 381
      }
2139 ghuddy 382
 
2159 ghuddy 383
      /// <summary>
384
      /// Thread function for import for document model use operation
2155 ghuddy 385
      /// </summary>
2151 ghuddy 386
      private static void CopyReqProDatabase_Thread()
387
      {
2155 ghuddy 388
         bool cancelled = false;
2151 ghuddy 389
         mustAbort = false;
2155 ghuddy 390
         abortRequests = 0;
2151 ghuddy 391
         EA_Repository.EnsureOutputVisible(GUI_OUTPUT_TAB_NAME);
392
         EA_Repository.WriteOutput(GUI_OUTPUT_TAB_NAME,"",-1);
393
         CopyReqProDatabase copy_parser = new CopyReqProDatabase();
2155 ghuddy 394
         copy_parser.prompt_and_parse(ReqProDB_Artifact.MODE.DOC_MODEL, out cancelled);
2151 ghuddy 395
         if (mustAbort)
2155 ghuddy 396
         {
397
            EA_Repository.WriteOutput(GUI_OUTPUT_TAB_NAME, "Import Aborted", -1);
2153 ghuddy 398
            MessageBoxEx.Show("Import Aborted", "Progress");
2155 ghuddy 399
         }
2159 ghuddy 400
         else if (cancelled)
401
         {
402
            Main.WriteOutput("Import Cancelled",-1);
2155 ghuddy 403
         }
404
         abortRequests = 0;
405
         ReqProDatabase.close();
2151 ghuddy 406
      }
407
 
2159 ghuddy 408
      /// <summary>
409
      /// Thread function for export to reqpro database operation
2155 ghuddy 410
      /// </summary>
411
      private static void ExportToReqPro_Thread()
412
      {
413
         bool cancelled = false;
414
         mustAbort = false;
415
         abortRequests = 0;
416
         EA_Repository.EnsureOutputVisible(GUI_OUTPUT_TAB_NAME);
417
         EA_Repository.WriteOutput(GUI_OUTPUT_TAB_NAME,"",-1);
418
         ExportToReqProDatabase export_parser = new ExportToReqProDatabase();
419
         export_parser.prompt_and_parse(ReqProDB_Artifact.MODE.EXPORT, out cancelled);
420
         if (mustAbort)
421
         {
422
            EA_Repository.WriteOutput(GUI_OUTPUT_TAB_NAME, "Export Aborted", -1);
423
            MessageBoxEx.Show("Export Aborted", "Progress");
424
         }
2159 ghuddy 425
         else if (cancelled)
426
         {
427
            Main.WriteOutput("Export Cancelled",-1);
2155 ghuddy 428
         }
429
         abortRequests = 0;
430
         ReqProDatabase.close();
431
      }
432
 
2153 ghuddy 433
      public static void MessageBoxException(Exception exc)
434
      {
435
         MessageBoxException(exc, "Exception");
436
      }
437
 
438
      public static void MessageBoxException(Exception exc, string caption)
439
      {
2159 ghuddy 440
         MessageBoxEx.Show(exc.ToString(), caption, MessageBoxIcon.Error);
2153 ghuddy 441
      }
2155 ghuddy 442
 
2159 ghuddy 443
      public static void WriteOutput(string s, int id)
444
      {
445
         EA_Repository.WriteOutput( GUI_OUTPUT_TAB_NAME, s, id);
446
      }
2155 ghuddy 447
 
448
      public static void ClearOutput()
449
      {
450
         EA_Repository.ClearOutput(GUI_OUTPUT_TAB_NAME);
451
      }
452
      public static void WriteSeperator()
453
      {
454
         EA_Repository.WriteOutput( GUI_OUTPUT_TAB_NAME, "----------------------------------------------------------------------------------------------------------------------", -1);
455
      }
2139 ghuddy 456
   }
457
}