Subversion Repositories DevTools

Rev

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