Skip to main content
summaryrefslogtreecommitdiffstats
blob: fd623270a1b916192e818a004e1020b5bbf22d6b (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
/*******************************************************************************
 * 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.framework.ui.skynet.explorer;

import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.IJobChangeEvent;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.core.runtime.jobs.JobChangeAdapter;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.ISelectionProvider;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.osee.framework.access.AccessControlManager;
import org.eclipse.osee.framework.core.data.BranchId;
import org.eclipse.osee.framework.core.data.IOseeBranch;
import org.eclipse.osee.framework.core.model.Branch;
import org.eclipse.osee.framework.core.operation.IOperation;
import org.eclipse.osee.framework.core.operation.Operations;
import org.eclipse.osee.framework.help.ui.OseeHelpContext;
import org.eclipse.osee.framework.jdk.core.type.OseeCoreException;
import org.eclipse.osee.framework.logging.OseeLevel;
import org.eclipse.osee.framework.logging.OseeLog;
import org.eclipse.osee.framework.plugin.core.util.Jobs;
import org.eclipse.osee.framework.skynet.core.OseeSystemArtifacts;
import org.eclipse.osee.framework.skynet.core.UserManager;
import org.eclipse.osee.framework.skynet.core.artifact.Artifact;
import org.eclipse.osee.framework.skynet.core.artifact.BranchManager;
import org.eclipse.osee.framework.skynet.core.artifact.IBranchProvider;
import org.eclipse.osee.framework.skynet.core.artifact.search.ArtifactQuery;
import org.eclipse.osee.framework.skynet.core.event.OseeEventManager;
import org.eclipse.osee.framework.skynet.core.event.filter.IEventFilter;
import org.eclipse.osee.framework.skynet.core.event.listener.IAccessControlEventListener;
import org.eclipse.osee.framework.skynet.core.event.listener.IBranchEventListener;
import org.eclipse.osee.framework.skynet.core.event.model.AccessControlEvent;
import org.eclipse.osee.framework.skynet.core.event.model.AccessControlEventType;
import org.eclipse.osee.framework.skynet.core.event.model.BranchEvent;
import org.eclipse.osee.framework.skynet.core.event.model.BranchEventType;
import org.eclipse.osee.framework.skynet.core.event.model.Sender;
import org.eclipse.osee.framework.ui.plugin.util.AWorkbench;
import org.eclipse.osee.framework.ui.plugin.util.HelpUtil;
import org.eclipse.osee.framework.ui.plugin.util.SelectionCountChangeListener;
import org.eclipse.osee.framework.ui.skynet.ArtifactContentProvider;
import org.eclipse.osee.framework.ui.skynet.ArtifactDecorator;
import org.eclipse.osee.framework.ui.skynet.ArtifactDoubleClick;
import org.eclipse.osee.framework.ui.skynet.ArtifactLabelProvider;
import org.eclipse.osee.framework.ui.skynet.ArtifactStructuredSelection;
import org.eclipse.osee.framework.ui.skynet.IArtifactExplorerEventHandler;
import org.eclipse.osee.framework.ui.skynet.OseeStatusContributionItemFactory;
import org.eclipse.osee.framework.ui.skynet.explorer.menu.ArtifactExplorerMenu;
import org.eclipse.osee.framework.ui.skynet.internal.Activator;
import org.eclipse.osee.framework.ui.skynet.listener.IRebuildMenuListener;
import org.eclipse.osee.framework.ui.skynet.menu.ArtifactTreeViewerGlobalMenuHelper;
import org.eclipse.osee.framework.ui.skynet.menu.IGlobalMenuHelper;
import org.eclipse.osee.framework.ui.skynet.util.DbConnectionExceptionComposite;
import org.eclipse.osee.framework.ui.skynet.util.SkynetViews;
import org.eclipse.osee.framework.ui.skynet.widgets.GenericViewPart;
import org.eclipse.osee.framework.ui.skynet.widgets.XBranchSelectWidget;
import org.eclipse.osee.framework.ui.swt.Displays;
import org.eclipse.osee.framework.ui.swt.TreeViewerUtility;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StackLayout;
import org.eclipse.swt.custom.TreeEditor;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.ui.IMemento;
import org.eclipse.ui.IViewSite;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.progress.UIJob;

/**
 * @author Ryan D. Brooks
 */
public class ArtifactExplorer extends GenericViewPart implements IArtifactExplorerEventHandler, IRebuildMenuListener, IAccessControlEventListener, IBranchEventListener, ISelectionProvider, IBranchProvider {
   public static final String VIEW_ID = "org.eclipse.osee.framework.ui.skynet.ArtifactExplorer";
   private static final String ROOT_UUID = "artifact.explorer.last.root_uuid";
   private static final String ROOT_BRANCH = "artifact.explorer.last.root_branch";

   private TreeViewer treeViewer;
   private Artifact explorerRoot;
   private TreeEditor myTreeEditor;
   private XBranchSelectWidget branchSelect;
   private Branch branch;
   private IGlobalMenuHelper globalMenuHelper;

   private ArtifactExplorerDragAndDrop dragAndDropWorker;

   private Composite stackComposite;
   private BranchWarningComposite branchWarningComposite;
   private StackLayout stackLayout;
   private ArtifactDecorator artifactDecorator;
   public ArtifactExplorerMenu artifactExplorerMenu;
   private ArtifactExplorerToolbar artifactExplorerToolbar;

   public static void explore(Collection<Artifact> artifacts) {
      explore(artifacts, AWorkbench.getActivePage());
   }

   private void setErrorString(String str) {
      branchWarningComposite.updateLabel(str);
      stackLayout.topControl = branchWarningComposite;
      stackComposite.layout();
      stackComposite.getParent().layout();
   }

   public static void explore(Collection<Artifact> artifacts, IWorkbenchPage page) {
      Artifact sampleArtifact = null;
      BranchId inputBranch = null;
      if (artifacts != null && !artifacts.isEmpty()) {
         sampleArtifact = artifacts.iterator().next();
         inputBranch = sampleArtifact.getBranch();
      }
      ArtifactExplorer artifactExplorer = ArtifactExplorerUtil.findView(inputBranch, page);

      artifactExplorer.setPartName("Artifacts");
      artifactExplorer.setContentDescription("These artifacts could not be handled");
      artifactExplorer.treeViewer.setInput(artifacts);
      artifactExplorer.initializeSelectionBox();
   }

   @Override
   public void createPartControl(Composite parent) {
      try {
         if (DbConnectionExceptionComposite.dbConnectionIsOk(parent)) {

            // TODO: Trigger User Loading to prevent lock up -- Need to remove this once service based
            UserManager.getUser();
            GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, true);
            gridData.heightHint = 1000;
            gridData.widthHint = 1000;

            parent.setLayout(new GridLayout(1, false));
            parent.setLayoutData(gridData);

            branchSelect = new XBranchSelectWidget("");
            branchSelect.setDisplayLabel(false);
            branchSelect.setSelection(branch);
            branchSelect.createWidgets(parent, 1);

            branchSelect.addListener(new Listener() {
               @Override
               public void handleEvent(Event event) {
                  try {
                     BranchId selectedBranch = branchSelect.getData();
                     if (selectedBranch != null) {
                        branch = BranchManager.getBranch(selectedBranch);
                        dragAndDropWorker.updateBranch(selectedBranch);
                        explore(OseeSystemArtifacts.getDefaultHierarchyRootArtifact(branch));
                     }
                  } catch (Exception ex) {
                     setErrorString("Error loading branch (see error log for details): " + ex.getLocalizedMessage());
                     OseeLog.log(getClass(), Level.SEVERE, ex);
                  }
               }

            });

            stackComposite = new Composite(parent, SWT.NONE);
            stackLayout = new StackLayout();
            stackComposite.setLayout(stackLayout);
            stackComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

            branchWarningComposite = new BranchWarningComposite(stackComposite);

            treeViewer = new TreeViewer(stackComposite);
            Tree tree = treeViewer.getTree();
            final ArtifactExplorer fArtExplorere = this;
            tree.addDisposeListener(new DisposeListener() {

               @Override
               public void widgetDisposed(DisposeEvent e) {
                  ArtifactExplorerEventManager.remove(fArtExplorere);
               }
            });
            artifactDecorator = new ArtifactDecorator(Activator.ARTIFACT_EXPLORER_ATTRIBUTES_PREF);

            treeViewer.setContentProvider(new ArtifactContentProvider(artifactDecorator));
            treeViewer.setLabelProvider(new ArtifactLabelProvider(artifactDecorator));
            treeViewer.addDoubleClickListener(new ArtifactDoubleClick());
            treeViewer.getControl().setLayoutData(gridData);
            treeViewer.addSelectionChangedListener(new ISelectionChangedListener() {

               @Override
               public void selectionChanged(SelectionChangedEvent event) {
                  if (treeViewer != null) {
                     Tree viewer = treeViewer.getTree();
                     if (viewer != null && !viewer.isDisposed()) {
                        viewer.redraw();
                     }
                     Control control = treeViewer.getControl();
                     if (control != null && !control.isDisposed()) {
                        control.redraw();
                     }
                  }
               }

            });

            /**
             * We can not use the hash lookup because an artifact may not have a good equals. This can be added back
             * once the content provider is converted over to use job node.
             */
            treeViewer.setUseHashlookup(false);

            treeViewer.addSelectionChangedListener(new SelectionCountChangeListener(getViewSite()));
            globalMenuHelper = new ArtifactTreeViewerGlobalMenuHelper(treeViewer);

            artifactExplorerToolbar = new ArtifactExplorerToolbar(this);
            artifactExplorerToolbar.createToolbar();

            artifactDecorator.setViewer(treeViewer);
            artifactDecorator.addActions(getViewSite().getActionBars().getMenuManager(), this);

            getSite().setSelectionProvider(treeViewer);
            addExploreSelection();

            artifactExplorerMenu = new ArtifactExplorerMenu(this);
            artifactExplorerMenu.create();
            artifactExplorerMenu.setupPopupMenu();

            myTreeEditor = new TreeEditor(getTreeViewer().getTree());
            myTreeEditor.horizontalAlignment = SWT.LEFT;
            myTreeEditor.grabHorizontal = true;
            myTreeEditor.minimumWidth = 50;

            dragAndDropWorker = new ArtifactExplorerDragAndDrop(treeViewer, VIEW_ID, this, branch);

            OseeStatusContributionItemFactory.addTo(this, false);

            artifactExplorerToolbar.updateEnablement();
            HelpUtil.setHelp(treeViewer.getControl(), OseeHelpContext.ARTIFACT_EXPLORER);

            refreshBranchWarning();

            getViewSite().getActionBars().updateActionBars();
            setFocusWidget(treeViewer.getControl());

            OseeEventManager.addListener(this);
            ArtifactExplorerEventManager.add(this);
         }
      } catch (Exception ex) {
         OseeLog.log(Activator.class, OseeLevel.SEVERE_POPUP, ex);
      }

   }

   public void refreshBranchWarning() {
      ArtifactExplorerUtil.refreshBranchWarning(this, treeViewer, globalMenuHelper, getBranch(),
         branchWarningComposite);
   }

   /**
    * Reveal an artifact in the viewer and select it.
    */
   public static void exploreBranch(BranchId branch) {
      if (branch != null) {
         IWorkbenchPage page = AWorkbench.getActivePage();
         ArtifactExplorerUtil.findView(branch, page);
      }
   }

   public void explore(Artifact artifact) throws OseeCoreException {
      if (artifact == null) {
         throw new IllegalArgumentException("Can not explore a null artifact.");
      }

      setPartName("Artifact Explorer: " + artifact.getFullBranch().getShortName());
      if (branch != null && branch != artifact.getBranch()) {
         explore(Arrays.asList(artifact));
         return;
      }

      explorerRoot = artifact;
      branch = artifact.getFullBranch();

      if (dragAndDropWorker != null) {
         dragAndDropWorker.updateBranch(branch);
      }

      refreshBranchWarning();

      initializeSelectionBox();

      if (treeViewer != null) {
         Object objects[] = treeViewer.getExpandedElements();
         treeViewer.setInput(explorerRoot);
         artifactExplorerMenu.setupPopupMenu();
         artifactExplorerToolbar.updateEnablement();
         // Attempt to re-expand what was expanded
         treeViewer.setExpandedElements(objects);
      }
   }

   public void setExpandedArtifacts(Object... artifacts) {
      if (treeViewer != null) {
         treeViewer.setExpandedElements(artifacts);
      }
   }

   /**
    * Add the selection from the define explorer
    */
   private void addExploreSelection() {
      if (explorerRoot != null) {
         try {
            treeViewer.setInput(explorerRoot);
            initializeSelectionBox();
         } catch (IllegalArgumentException ex) {
            OseeLog.log(Activator.class, Level.SEVERE, ex);
         }
      }
   }

   @Override
   public void init(IViewSite site, IMemento memento) throws PartInitException {
      super.init(site, memento);
      if (DbConnectionExceptionComposite.dbConnectionIsOk()) {
         try {
            if (memento != null && memento.getString(ROOT_UUID) != null && memento.getString(ROOT_BRANCH) != null) {
               Branch branch = BranchManager.getBranch(Long.parseLong(memento.getString(ROOT_BRANCH)));

               if (!branch.getArchiveState().isArchived() || AccessControlManager.isOseeAdmin()) {
                  Artifact previousArtifact =
                     ArtifactQuery.checkArtifactFromId(Long.valueOf(memento.getString(ROOT_UUID)), branch);
                  if (previousArtifact != null) {
                     explore(previousArtifact);
                  } else {
                     /*
                      * simply means that the previous artifact that was used as the root for the artiactExplorer does
                      * not exist because it was deleted or this workspace was last used with a different branch or
                      * database, so let the logic below get the default hierarchy root artifact
                      */
                  }
                  return;
               }
            }
         } catch (Exception ex) {
            OseeLog.log(Activator.class, Level.SEVERE, ex);
         }
      }
   }

   @Override
   public void saveState(IMemento memento) {
      super.saveState(memento);
      if (DbConnectionExceptionComposite.dbConnectionIsOk()) {
         if (explorerRoot != null) {
            memento.putString(ROOT_UUID, String.valueOf(explorerRoot.getUuid()));
            try {
               memento.putString(ROOT_BRANCH, String.valueOf(explorerRoot.getBranchId()));
            } catch (OseeCoreException ex) {
               OseeLog.log(Activator.class, Level.SEVERE, ex);
            }
         }
      }
   }

   @Override
   public void dispose() {
      OseeEventManager.removeListener(this);
      ArtifactExplorerEventManager.remove(this);
      artifactExplorerMenu.dispose();
      super.dispose();
   }

   @Override
   public void addSelectionChangedListener(ISelectionChangedListener listener) {
      treeViewer.addSelectionChangedListener(listener);
   }

   @Override
   public ArtifactStructuredSelection getSelection() {
      final List<Artifact> selectedItems = new LinkedList<>();
      TreeViewerUtility.getPreorderSelection(treeViewer, selectedItems);
      return new ArtifactStructuredSelection(selectedItems);
   }

   @Override
   public void removeSelectionChangedListener(ISelectionChangedListener listener) {
      treeViewer.removeSelectionChangedListener(listener);
   }

   @Override
   public void setSelection(ISelection selection) {
      treeViewer.setSelection(selection);
   }

   @Override
   public void handleAccessControlArtifactsEvent(Sender sender, AccessControlEvent accessControlEvent) {
      try {
         if (!accessControlEvent.isForBranch(branch)) {
            return;
         }
         if (accessControlEvent.getEventType() == AccessControlEventType.UserAuthenticated) {
            Displays.ensureInDisplayThread(new Runnable() {
               @Override
               public void run() {
                  treeViewer.refresh();
                  refreshBranchWarning();
               }
            });
         }
      } catch (Exception ex) {
         OseeLog.log(Activator.class, Level.SEVERE, ex);
      }
   }

   @Override
   public void rebuildMenu() {
      artifactExplorerMenu.setupPopupMenu();
   }

   public void setBranch(Branch branch) {
      this.branch = branch;
   }

   public void initializeSelectionBox() {
      if (branch != null && branchSelect != null && !branch.equals(branchSelect.getData())) {
         branchSelect.setSelection(branch);
         refreshBranchWarning();
      }
   }

   @Override
   public IOseeBranch getBranch(IProgressMonitor monitor) {
      return branch;
   }

   public Branch getBranch() {
      return branch;
   }

   /**
    * Reveal an artifact in the viewer and select it.
    */
   public static void revealArtifact(Artifact artifact) {
      final ArtifactData data = new ArtifactData(artifact);
      IOperation operation = new CheckArtifactBeforeReveal(data);
      Operations.executeAsJob(operation, true, Job.SHORT, new JobChangeAdapter() {

         @Override
         public void done(IJobChangeEvent event) {
            IStatus status = event.getResult();
            if (status.isOK()) {
               Job uiJob = new UIJob("Reveal in Artifact Explorer") {

                  @Override
                  public IStatus runInUIThread(IProgressMonitor monitor) {
                     Artifact artifact = data.getArtifact();
                     IWorkbenchPage page = AWorkbench.getActivePage();
                     ArtifactExplorer artifactExplorer = ArtifactExplorerUtil.findView(artifact.getBranch(), page);
                     artifactExplorer.treeViewer.setSelection(new StructuredSelection(artifact), true);
                     return Status.OK_STATUS;
                  }
               };
               Jobs.startJob(uiJob);
            }
         }
      });

   }

   @Override
   public void handleBranchEvent(Sender sender, final BranchEvent branchEvent) {
      if (branch == null) {
         return;
      }
      if (branch.equals(branchEvent.getSourceBranch())) {
         if (branchEvent.getEventType() == BranchEventType.Committing || branchEvent.getEventType() == BranchEventType.Committed) {
            SkynetViews.closeView(VIEW_ID, getViewSite().getSecondaryId());
         } else {
            refreshBranchWarning();
         }
      } else if (branch.equals(branchEvent.getDestinationBranch())) {
         if (branchEvent.getEventType() == BranchEventType.Committed) {
            Displays.ensureInDisplayThread(new Runnable() {
               @Override
               public void run() {
                  getTreeViewer().refresh();
               }
            });
         }
      }
   }

   public TreeViewer getTreeViewer() {
      return treeViewer;
   }

   public void setTreeViewer(TreeViewer treeViewer) {
      this.treeViewer = treeViewer;
   }

   @Override
   public ArtifactExplorer getArtifactExplorer() {
      return this;
   }

   @Override
   public boolean isDisposed() {
      return treeViewer.getTree() == null || treeViewer.getTree().isDisposed();
   }

   @Override
   public List<? extends IEventFilter> getEventFilters() {
      if (branch != null) {
         return OseeEventManager.getEventFiltersForBranch(branch);
      }
      return null;
   }

   public Artifact getExplorerRoot() {
      return explorerRoot;
   }

   public TreeEditor getMyTreeEditor() {
      return myTreeEditor;
   }

   public Label getBranchWarningLabel() {
      return branchWarningComposite.getBranchWarningLabel();
   }

   public StackLayout getStackLayout() {
      return stackLayout;
   }

   public Composite getStackComposite() {
      return stackComposite;
   }

}

Back to the top