Skip to main content
summaryrefslogtreecommitdiffstats
blob: 553469d8ae5ea2a79cda5a8843f71d38330d73cc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
/*******************************************************************************
 * Copyright (c) 2004, 2007 Boeing.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.ote.ui.test.manager.core;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.logging.Level;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.QualifiedName;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.osee.connection.service.IServiceConnector;
import org.eclipse.osee.framework.jdk.core.type.IPropertyStore;
import org.eclipse.osee.framework.jdk.core.type.PropertyStore;
import org.eclipse.osee.framework.jdk.core.util.Lib;
import org.eclipse.osee.framework.plugin.core.IActionable;
import org.eclipse.osee.framework.plugin.core.util.OseeData;
import org.eclipse.osee.framework.ui.swt.ImageManager;
import org.eclipse.osee.ote.core.environment.interfaces.IHostTestEnvironment;
import org.eclipse.osee.ote.core.environment.interfaces.ITestEnvironment;
import org.eclipse.osee.ote.service.ConnectionEvent;
import org.eclipse.osee.ote.service.ITestConnectionListener;
import org.eclipse.osee.ote.ui.test.manager.ITestManagerFactory;
import org.eclipse.osee.ote.ui.test.manager.OteTestManagerImage;
import org.eclipse.osee.ote.ui.test.manager.internal.TestManagerPlugin;
import org.eclipse.osee.ote.ui.test.manager.pages.contributions.TestManagerStorageKeys;
import org.eclipse.osee.ote.ui.test.manager.util.ClassServerInst;
import org.eclipse.osgi.service.datalocation.Location;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.IFileEditorInput;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.editors.text.TextEditor;
import org.eclipse.ui.part.MultiPageEditorPart;

/**
 * Resource Test Manager Editor Pages:
 * <ul>
 * <li>Overview Page
 * <li>Target Page
 * <li>Scripts Page
 * <li>Advanced Page
 * <li>Source Page
 * </ul>
 */
public abstract class TestManagerEditor extends MultiPageEditorPart implements IActionable, ITestConnectionListener {
   private static final Image errorImage = ImageManager.getImage(OteTestManagerImage.ERROR);
   public static final String namespace = "org.eclipse.osee.ote.ui.test.manager.editors.TestManagerEditor";

   public final QualifiedName clearCaseViewName = new QualifiedName(namespace, "CLEARCASEVIEW");
   public final QualifiedName configFileName = new QualifiedName(namespace, "CONFIGFILENAME");
   public final QualifiedName ofpQualName = new QualifiedName(namespace, "OFP");
   public final QualifiedName scriptsQualName = new QualifiedName(namespace, "SCRIPTS");

   private boolean fileIsDirty = false;

   private boolean fileWasSaved = false;

   private int lastPageIndex = 0;

   private final TestManagerModel model;

   private boolean reloadSourcePage = false;

   private int scriptPageIndex;

   private TextEditor sourceEditor;

   private int sourcePage;

   private final ITestManagerFactory testManagerFactory;

   private IFile thisIFile = null;

   private String xmlText;

   private final IPropertyStore propertyStore;

   private final PageManager pageManager;

   private ITestEnvironment connectedEnv = null;
   private IServiceConnector connector = null;
   private IHostTestEnvironment connectedHost;

   public TestManagerEditor(final ITestManagerFactory testManagerFactory) {
      super();

      this.testManagerFactory = testManagerFactory;
      this.pageManager = new PageManager(testManagerFactory, this);
      this.model = new TestManagerModel();
      this.propertyStore = new PropertyStore(testManagerFactory.getClass().getSimpleName());
   }

   public void activateScriptsPage() {
      setActivePage(scriptPageIndex);
   }

   public void addFile(String fullPath) {
      pageManager.getScriptPage().addFile(fullPath);
   }

   @Override
   public void dispose() {
      super.dispose();
      TestManagerPlugin.getInstance().getOteClientService().removeConnectionListener(this);
      try {
         pageManager.dispose();
      } catch (Throwable t) {
         TestManagerPlugin.log(Level.SEVERE, "exception while disposing test manager", t);
      }
   }

   /**
    * Saves the multi-page editor's document.
    */
   @Override
   public void doSave(IProgressMonitor monitor) {
      if (getActivePage() != sourcePage) {
         pageSourceLoad();
      }
      getEditor(sourcePage).doSave(monitor);
      fileIsDirty = false;
      fileWasSaved = true;
      doSave();
      firePropertyChange(PROP_DIRTY);
   }

   /**
    * Saves the multi-page editor's document as another file. Also updates the text for page 0's tab, and updates this
    * multi-page editor's input to correspond to the nested editor's.
    */
   @Override
   public void doSaveAs() {
      if (getActivePage() != sourcePage) {
         pageSourceLoad();
      }
      IEditorPart editor = getEditor(sourcePage);
      editor.doSaveAs();
      setPageText(sourcePage, "Source");
      setInput(editor.getEditorInput());
      readXmlData();
      doSave();
   }

   protected void registerPage(int pageNumber, String pageName) {
      setPageText(pageNumber, pageName);
   }

   public void executionCompleted() {
      pageManager.getScriptPage().onScriptRunning(false);
   }

   public void fireSaveNeeded() {
      fileIsDirty = true;
      firePropertyChange(PROP_DIRTY);
   }

   public String getAlternateOutputDir() {
      String scriptOutput = "";

      IPropertyStore propertyStore = getPropertyStore();
      scriptOutput = propertyStore.get(TestManagerStorageKeys.SCRIPT_OUTPUT_DIRECTORY_KEY);
      if (scriptOutput == null) {
         scriptOutput = "";
         // TODO: Escobar
         // try {
         // IEditorInput coreinput = getEditorInput();
         // if (coreinput instanceof IFileEditorInput) {
         // scriptOutput =
         // thisIFile.getPersistentProperty(scriptOutputQualName);
         // } else if (coreinput instanceof TestManagerInput) {
         // TestManagerInput input = (TestManagerInput) getEditorInput();
         // scriptOutput =
         // input.getValue(scriptOutputQualName.getLocalName());
         // }
         //
         // scriptOutput =
         // thisIFile.getPersistentProperty(scriptOutputQualName);
         // } catch (CoreException e) {
         // e.printStackTrace();
         // }
      }
      return scriptOutput;
   }

   public String getDefaultConfigPath() {
      Location user = Platform.getUserLocation();
      String path = user.getURL().getPath();
      File file = new File(path + File.separator + "org.eclipse.osee.ote.ui.test.manager");
      file.mkdirs();
      file =
            new File(
                  path + File.separator + "org.eclipse.osee.ote.ui.test.manager" + File.separator + this.getClass().getName() + ".scriptConfig.xml");
      file.getParentFile().mkdirs();
      return file.getAbsolutePath();
   }

   /**
    * @return Returns the model.
    */
   public TestManagerModel getModel() {
      return model;
   }

   public String getName() {
      return this.getTitle();
   }

   public ClassServerInst getScriptClassServer() {
      return ClassServerInst.getInstance();
   }

   public ITestManagerFactory getTestManagerFactory() {
      return testManagerFactory;
   }

   @Override
   public void init(IEditorSite site, IEditorInput editorInput) throws PartInitException {
      if (!(editorInput instanceof IFileEditorInput || editorInput instanceof TestManagerInput || editorInput instanceof IEditorInput)) {
         throw new PartInitException("Invalid Input: Must be IFileEditorInput");
      }
      super.init(site, editorInput);
   }

   @Override
   public boolean isDirty() {
      if (super.isDirty()) {
         return true;
      }
      return fileIsDirty;
   }

   @Override
   public boolean isSaveAsAllowed() {
      return true;
   }

   /**
    * Retrieves the value for the key. See <code>storeValue</code>. If the key could not be found, an empty string is
    * returned.
    * 
    * @param key The <code>QualifiedName</code> whose value is to be retrieved.
    * @return The value of key, or an empty string if the key does not exist.
    */
   public String loadValue(QualifiedName key) {
      TestManagerPlugin.log(Level.INFO, "loadValue: " + key.getQualifier());
      try {
         IEditorInput coreinput = getEditorInput();
         if (coreinput instanceof IFileEditorInput) {
            return thisIFile.getPersistentProperty(key);
         } else if (coreinput instanceof TestManagerInput) {
            TestManagerInput input = (TestManagerInput) getEditorInput();
            return input.getValue(key.getLocalName());
         }
      } catch (CoreException ex) {
         TestManagerPlugin.log(Level.SEVERE, "Can't get value: " + ex);
      }
      return "";
   }

   public void setPageError(int page, boolean set) {
      if (set) {
         setPageImage(page, errorImage);
      } else {
         setPageImage(page, null);
      }
   }

   /**
    * Stores the value for the key. The key should be one of the publicly available <code>QualifiedName</code>'s in
    * <code>this</code>.
    * 
    * @param key The <code>QualifiedName</code> associated with the value to be stored
    * @param value What will be stored under the key.
    */
   public void storeValue(QualifiedName key, String value) {
      TestManagerPlugin.log(Level.INFO, "storeValue: " + key.getQualifier());
      try {
         IEditorInput coreinput = getEditorInput();
         if (coreinput instanceof IFileEditorInput) {
            thisIFile.setPersistentProperty(key, value);
         } else if (coreinput instanceof TestManagerInput) {
            TestManagerInput input = (TestManagerInput) getEditorInput();
            input.storeValue(key.getLocalName(), value);
         }
      } catch (Exception ex) {
         TestManagerPlugin.log(Level.SEVERE, "Can't set value: " + ex);
      }
   }

   private void pageSourceCheck() {
      setPageError(sourcePage, model.hasParseExceptions());
   }

   private void readXmlData() {
      TestManagerPlugin.log(Level.INFO, "readXmlData");
      IEditorInput coreinput = getEditorInput();
      if (coreinput instanceof IFileEditorInput) {
         IFileEditorInput input = (IFileEditorInput) getEditorInput();
         thisIFile = input.getFile();
         String name = thisIFile.getName();
         this.setPartName(name);
         model.setConfiguration(name);
         if (thisIFile != null) {
            IPath containerPath = thisIFile.getRawLocation();
            model.filename = containerPath.toOSString();
            try {

               xmlText = Lib.inputStreamToString(thisIFile.getContents());
            } catch (Exception ex) {
               ex.printStackTrace();
            }
         } else {
            TestManagerPlugin.log(Level.SEVERE, "Can't open xml file!");
         }
      } else if (coreinput instanceof TestManagerInput) {
         TestManagerInput input = (TestManagerInput) getEditorInput();
         String name = "TestManager";
         this.setPartName(name);
         model.setConfiguration(name);
         xmlText = input.getDefaultXML();
      }
   }

   /**
    * Creates the pages of the multi-page editor.
    */
   @Override
   protected void createPages() {
      readXmlData();

      if (model.setFromXml(xmlText)) {
         pageManager.createPages(getContainer());
         pageSourceCreate();

         fileIsDirty = false;
         reloadSourcePage = false;
         pageSourceCheck();
         restoreSettings();
      }
      // If parse errors, send to sourcePage and set error on page
      else {
         if (sourceEditor == null) {
            pageSourceCreate();
            setActivePage(sourcePage);
            return;
         }
         pageSourceCheck();
         setPageImage(sourcePage, errorImage);
         setActivePage(sourcePage);
      }
      fileIsDirty = false;
      firePropertyChange(PROP_DIRTY);
      TestManagerPlugin.getInstance().getOteClientService().addConnectionListener(this);
   }

   protected void handleSelection() {
      fireSaveNeeded();
      reloadSourcePage = true;
   }

   /**
    * reloads pages as necessary
    */
   @Override
   protected void pageChange(int newPageIndex) {
      // NOTE: Hosts page will be updated continuously, even it if it is not
      // the current page.
      // so it is unnecessary to update it on pageChange.

      super.pageChange(newPageIndex);
      if (newPageIndex == sourcePage) {
         pageSourceLoad();
      } else {
         if (sourceEditor == null) {
            return;
         }
         String newXml = sourceEditor.getDocumentProvider().getDocument(sourceEditor.getEditorInput()).get();
         if (sourceEditor.isDirty() || fileWasSaved) {
            fileWasSaved = false;
            // If we just came from sourcePage, re-parse
            if (lastPageIndex == sourcePage) {
               // if parse error, goto source and error
               if (!model.setFromXml(newXml)) {
                  setActivePage(sourcePage);
                  setPageError(sourcePage, true);
                  MessageDialog.openError(getSite().getShell(), "Source Page Error",
                        "Error parsing Source page\n\n" + model.getParseExceptions());
                  return;
               }
               setPageError(sourcePage, false);
            }
         }
      }
      lastPageIndex = newPageIndex;
   }

   void pageSourceCreate() {
      try {
         if (getEditorInput() instanceof IFileEditorInput) {
            sourceEditor = new TextEditor();
            int index = addPage(sourceEditor, getEditorInput());
            sourcePage = index;
            setPageText(sourcePage, "Source");
         }
      } catch (PartInitException e) {
         TestManagerPlugin.log(Level.SEVERE, "Error creating nested text editor", e);
         ErrorDialog.openError(getSite().getShell(), "Error creating nested text editor", null, e.getStatus());
      }
   }

   void pageSourceLoad() {
      if (reloadSourcePage) {
         sourceEditor.getDocumentProvider().getDocument(sourceEditor.getEditorInput()).set(model.getRawXml());
         reloadSourcePage = false;
      }
      pageSourceCheck();
   }

   public void doSave() {
      readXmlData();
      model.setFromXml(xmlText);
      pageManager.save();
      OutputStream outputStream = null;
      try {
         File file = OseeData.getFile("testManagerSettings.xml");
         outputStream = new FileOutputStream(file);
         getPropertyStore().save(outputStream);
      } catch (Exception ex) {
         TestManagerPlugin.log(Level.SEVERE, "Error storing settings.", ex);
      } finally {
         if (outputStream != null) {
            try {
               outputStream.close();
            } catch (IOException ex) {
               TestManagerPlugin.log(Level.WARNING, "Error closing stream during settings storage.", ex);
            }
         }
      }
   }

   public void restoreSettings() {
      InputStream inputStream = null;
      try {
         File file = OseeData.getFile("testManagerSettings.xml");
         inputStream = new FileInputStream(file);
         getPropertyStore().load(inputStream);
         pageManager.restore();
      } catch (Exception ex) {
         TestManagerPlugin.log(Level.WARNING, "Stored settings not available. Using defaults.", ex);
      } finally {
         if (inputStream != null) {
            try {
               inputStream.close();
            } catch (IOException ex) {
               TestManagerPlugin.log(Level.WARNING, "Error closing stream while loading settings.", ex);
            }
         }
      }
   }

   public IPropertyStore getPropertyStore() {
      return propertyStore;
   }

   public PageManager getPageManager() {
      return pageManager;
   }

   public String getActionDescription() {
      String version =
            (String) Platform.getBundle("org.eclipse.osee.ote.ui.test.manager").getHeaders().get("Bundle-Version");
      String serverVersion = "";
      try {
         serverVersion = (String) Platform.getBundle("org.eclipse.osee.ote.core").getHeaders().get("Bundle-Version");
      } catch (Exception ex) {
         // do nothing
      }
      StringBuilder builder = new StringBuilder();
      builder.append("Problem found on OSEE TM Version: ");
      builder.append(version);
      builder.append("\nServer Version: ");
      builder.append(serverVersion);
      builder.append("\nTM File: ");
      builder.append(getTitle());
      builder.append("\n");
      builder.append("OFP: \"");
      builder.append(pageManager.getScriptPage().getOFP());
      builder.append("\"\n");
      builder.append("Scripts: \n");
      builder.append(pageManager.getScriptPage().getScripts());
      builder.append("\n");

      builder.append(getPropertyStore().toString());

      return builder.toString();
   }

   @Override
   public void onConnectionLost(IServiceConnector connector, IHostTestEnvironment testHost) {
      connectedEnv = null;
      connector = null;
      boolean problemEncountered = pageManager.onConnectionLost(testHost);
      if (problemEncountered) {
         Display.getDefault().asyncExec(new Runnable() {
            @Override
            public void run() {
               MessageDialog.openError(Display.getDefault().getActiveShell(), "Disconnect Error",
                     "Test manager has encountered a problem while processing the disconnect event. See Error Log for details");
            }
         });
      }
      connectedHost = null;
   }

   @Override
   public void onPostConnect(ConnectionEvent event) {
      connectedEnv = event.getEnvironment();
      connectedHost = event.getHostEnvironment();
      connector = event.getConnector();
      boolean problemEncountered = pageManager.onPostConnect(event);
      if (problemEncountered) {
         Display.getDefault().asyncExec(new Runnable() {
            @Override
            public void run() {
               MessageDialog.openError(Display.getDefault().getActiveShell(), "Connection Error",
                     "Test manager has encountered a problem while processing the connection event. See Error Log for details");
            }
         });
      }
   }

   @Override
   public void onPreDisconnect(ConnectionEvent event) {
      event.getEnvironment();
      connectedEnv = null;
      
      connector = null;
      boolean problemEncountered = pageManager.onPreDisconnect(event);
      if (problemEncountered) {
         Display.getDefault().asyncExec(new Runnable() {
            @Override
            public void run() {
               MessageDialog.openError(Display.getDefault().getActiveShell(), "Disconnect Error",
                     "Test manager has encountered a problem while processing the disconnect event. See Error Log for details");
            }
         });
      }
      connectedHost = null;
   }

   public boolean isConnected() {
      return connectedEnv != null;
   }

   public ITestEnvironment getConnectedEnvironment() {
      return connectedEnv;
   }
   
   public IHostTestEnvironment getConnectedHostEnvironment(){
      return connectedHost;
   }

   public IServiceConnector getConnector() {
      return connector;
   }

   public abstract void createHostWidget(Composite parent);

   /**
    * @param array
    */
   public void addFiles(String[] files) {
      pageManager.getScriptPage().addFiles(files);
   }

}

Back to the top