Subversion Repositories DevTools

Rev

Rev 2161 | 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
            {
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");
294
               ElementAccumulator reqLister = new ElementAccumulator(allowedElementTypes);
295
               EA.Package thePackage = (EA.Package)repository.GetTreeSelectedObject();
2153 ghuddy 296
               EA_Utilities.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:
315
               break;
2139 ghuddy 316
         }
317
      }
318
 
2155 ghuddy 319
      private void createRequirementDiagram(EA.Package containerPackage, EA.Collection c)
320
      {
321
         EA.Diagram newDiagram = (EA.Diagram)containerPackage.Diagrams.AddNew("Requirements","Logical");
322
         newDiagram.Update();
323
         containerPackage.Update();
324
 
325
         foreach (EA.Element element in c)
326
         {
327
            EA.DiagramObject newDiagramObject = (EA.DiagramObject)newDiagram.DiagramObjects.AddNew("l=1;r=10;t=1;b=1", "");
328
            newDiagramObject.ElementID = element.ElementID;
329
            newDiagramObject.Update();
330
         }
331
         newDiagram.DiagramObjects.Refresh();
332
         containerPackage.Update();
2159 ghuddy 333
         EA_Repository.GetProjectInterface().LayoutDiagramEx( newDiagram.DiagramGUID,
2155 ghuddy 334
            EA.ConstLayoutStyles.lsDiagramDefault, 4, 20, 20, false);
335
      }
336
 
337
      private void createRequirementDiagram(EA.Package containerPackage, ArrayList elements)
338
      {
339
         EA.Diagram newDiagram = (EA.Diagram)containerPackage.Diagrams.AddNew("Requirements","Logical");
340
         newDiagram.Update();
341
         containerPackage.Update();
342
 
343
         foreach (EA.Element element in elements)
344
         {
345
            EA.DiagramObject newDiagramObject = (EA.DiagramObject)newDiagram.DiagramObjects.AddNew("l=1;r=10;t=1;b=1", "");
346
            newDiagramObject.ElementID = element.ElementID;
347
            newDiagramObject.Update();
348
         }
349
         newDiagram.DiagramObjects.Refresh();
350
         containerPackage.Update();
2159 ghuddy 351
         EA_Repository.GetProjectInterface().LayoutDiagramEx( newDiagram.DiagramGUID,
2155 ghuddy 352
            EA.ConstLayoutStyles.lsDiagramDefault, 4, 20, 20, false);
353
      }
354
 
355
 
2159 ghuddy 356
      /// <summary>
357
      /// Thread function for import for traceability use operation
2155 ghuddy 358
      /// </summary>
2151 ghuddy 359
      private static void ImportReqProDatabase_Thread()
360
      {
2155 ghuddy 361
         bool cancelled = false;
2151 ghuddy 362
         mustAbort = false;
2155 ghuddy 363
         abortRequests = 0;
2151 ghuddy 364
         EA_Repository.EnsureOutputVisible(GUI_OUTPUT_TAB_NAME);
365
         EA_Repository.WriteOutput(GUI_OUTPUT_TAB_NAME,"",-1);
2155 ghuddy 366
         ImportReqProDatabase import_parser = new ImportReqProDatabase();
2159 ghuddy 367
         import_parser.prompt_and_parse(ReqProDB_Artifact.MODE.TRACEABILITY, out cancelled);
2151 ghuddy 368
         if (mustAbort)
2155 ghuddy 369
         {
370
            EA_Repository.WriteOutput(GUI_OUTPUT_TAB_NAME, "Import Aborted", -1);
2153 ghuddy 371
            MessageBoxEx.Show("Import Aborted", "Progress");
2155 ghuddy 372
         }
2159 ghuddy 373
         else if (cancelled)
374
         {
375
            Main.WriteOutput("Import Cancelled",-1);
2155 ghuddy 376
         }
377
         abortRequests = 0;
378
         ReqProDatabase.close();
2151 ghuddy 379
      }
2139 ghuddy 380
 
2159 ghuddy 381
      /// <summary>
382
      /// Thread function for import for document model use operation
2155 ghuddy 383
      /// </summary>
2151 ghuddy 384
      private static void CopyReqProDatabase_Thread()
385
      {
2155 ghuddy 386
         bool cancelled = false;
2151 ghuddy 387
         mustAbort = false;
2155 ghuddy 388
         abortRequests = 0;
2151 ghuddy 389
         EA_Repository.EnsureOutputVisible(GUI_OUTPUT_TAB_NAME);
390
         EA_Repository.WriteOutput(GUI_OUTPUT_TAB_NAME,"",-1);
391
         CopyReqProDatabase copy_parser = new CopyReqProDatabase();
2155 ghuddy 392
         copy_parser.prompt_and_parse(ReqProDB_Artifact.MODE.DOC_MODEL, out cancelled);
2151 ghuddy 393
         if (mustAbort)
2155 ghuddy 394
         {
395
            EA_Repository.WriteOutput(GUI_OUTPUT_TAB_NAME, "Import Aborted", -1);
2153 ghuddy 396
            MessageBoxEx.Show("Import Aborted", "Progress");
2155 ghuddy 397
         }
2159 ghuddy 398
         else if (cancelled)
399
         {
400
            Main.WriteOutput("Import Cancelled",-1);
2155 ghuddy 401
         }
402
         abortRequests = 0;
403
         ReqProDatabase.close();
2151 ghuddy 404
      }
405
 
2159 ghuddy 406
      /// <summary>
407
      /// Thread function for export to reqpro database operation
2155 ghuddy 408
      /// </summary>
409
      private static void ExportToReqPro_Thread()
410
      {
411
         bool cancelled = false;
412
         mustAbort = false;
413
         abortRequests = 0;
414
         EA_Repository.EnsureOutputVisible(GUI_OUTPUT_TAB_NAME);
415
         EA_Repository.WriteOutput(GUI_OUTPUT_TAB_NAME,"",-1);
416
         ExportToReqProDatabase export_parser = new ExportToReqProDatabase();
417
         export_parser.prompt_and_parse(ReqProDB_Artifact.MODE.EXPORT, out cancelled);
418
         if (mustAbort)
419
         {
420
            EA_Repository.WriteOutput(GUI_OUTPUT_TAB_NAME, "Export Aborted", -1);
421
            MessageBoxEx.Show("Export Aborted", "Progress");
422
         }
2159 ghuddy 423
         else if (cancelled)
424
         {
425
            Main.WriteOutput("Export Cancelled",-1);
2155 ghuddy 426
         }
427
         abortRequests = 0;
428
         ReqProDatabase.close();
429
      }
430
 
2153 ghuddy 431
      public static void MessageBoxException(Exception exc)
432
      {
433
         MessageBoxException(exc, "Exception");
434
      }
435
 
436
      public static void MessageBoxException(Exception exc, string caption)
437
      {
2159 ghuddy 438
         MessageBoxEx.Show(exc.ToString(), caption, MessageBoxIcon.Error);
2153 ghuddy 439
      }
2155 ghuddy 440
 
2159 ghuddy 441
      public static void WriteOutput(string s, int id)
442
      {
443
         EA_Repository.WriteOutput( GUI_OUTPUT_TAB_NAME, s, id);
444
      }
2155 ghuddy 445
 
446
      public static void ClearOutput()
447
      {
448
         EA_Repository.ClearOutput(GUI_OUTPUT_TAB_NAME);
449
      }
450
      public static void WriteSeperator()
451
      {
452
         EA_Repository.WriteOutput( GUI_OUTPUT_TAB_NAME, "----------------------------------------------------------------------------------------------------------------------", -1);
453
      }
2139 ghuddy 454
   }
455
}