Skip to main content
summaryrefslogtreecommitdiffstats
blob: ca78e9178d53f02ebe42ee03e8a41252c635b2c8 (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
/*******************************************************************************
/*******************************************************************************
 * 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.widgets.xHistory;

import java.util.Collections;
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.Job;
import org.eclipse.jface.action.ActionContributionItem;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.IMenuListener;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.nebula.widgets.xviewer.customize.XViewerCustomMenu;
import org.eclipse.osee.framework.access.AccessControlManager;
import org.eclipse.osee.framework.core.enums.TransactionDetailsType;
import org.eclipse.osee.framework.core.model.TransactionRecord;
import org.eclipse.osee.framework.help.ui.OseeHelpContext;
import org.eclipse.osee.framework.jdk.core.type.OseeCoreException;
import org.eclipse.osee.framework.jdk.core.util.Conditions;
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.artifact.Artifact;
import org.eclipse.osee.framework.skynet.core.artifact.Attribute;
import org.eclipse.osee.framework.skynet.core.artifact.BranchManager;
import org.eclipse.osee.framework.skynet.core.artifact.search.ArtifactQuery;
import org.eclipse.osee.framework.skynet.core.change.AttributeChange;
import org.eclipse.osee.framework.skynet.core.change.Change;
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.IBranchEventListener;
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.skynet.FrameworkImage;
import org.eclipse.osee.framework.ui.skynet.OpenContributionItem;
import org.eclipse.osee.framework.ui.skynet.OseeStatusContributionItemFactory;
import org.eclipse.osee.framework.ui.skynet.action.EditTransactionComment;
import org.eclipse.osee.framework.ui.skynet.action.ITransactionRecordSelectionProvider;
import org.eclipse.osee.framework.ui.skynet.action.WasIsCompareEditorAction;
import org.eclipse.osee.framework.ui.skynet.change.ChangeUiUtil;
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.CompareArtifactAction;
import org.eclipse.osee.framework.ui.skynet.util.SkynetViews;
import org.eclipse.osee.framework.ui.skynet.widgets.GenericViewPart;
import org.eclipse.osee.framework.ui.swt.Displays;
import org.eclipse.osee.framework.ui.swt.ImageManager;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MenuAdapter;
import org.eclipse.swt.events.MenuEvent;
import org.eclipse.swt.events.MenuListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.ui.IMemento;
import org.eclipse.ui.IViewSite;
import org.eclipse.ui.IWorkbenchActionConstants;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.progress.UIJob;

/**
 * Displays persisted changes made to an artifact.
 * 
 * @author Jeff C. Phillips
 */
public class HistoryView extends GenericViewPart implements IBranchEventListener, ITransactionRecordSelectionProvider, IRebuildMenuListener {

   public static final String VIEW_ID = "org.eclipse.osee.framework.ui.skynet.widgets.xHistory.HistoryView";
   private static final String INPUT = "input";
   private static final String ART_GUID = "artifactGuid";
   private static final String BRANCH_ID = "branchId";

   private XHistoryWidget xHistoryWidget;
   private Artifact artifact;

   public HistoryView() {
      super();
   }

   @Override
   public void dispose() {
      super.dispose();
      OseeEventManager.removeListener(this);
      if (xHistoryWidget != null) {
         xHistoryWidget.dispose();
      }
   }

   @Override
   public void createPartControl(Composite parent) {
      GridLayout layout = new GridLayout();
      layout.numColumns = 1;
      layout.verticalSpacing = 0;
      layout.marginWidth = 0;
      layout.marginHeight = 0;
      parent.setLayout(layout);
      parent.setLayoutData(new GridData(GridData.FILL_BOTH));

      xHistoryWidget = new XHistoryWidget() {

         @Override
         protected void onRefresh() {
            refreshTitle();
         }
      };
      xHistoryWidget.setDisplayLabel(false);
      xHistoryWidget.createWidgets(parent, 1);

      MenuManager menuManager = new MenuManager();
      menuManager.setRemoveAllWhenShown(true);
      menuManager.addMenuListener(new IMenuListener() {
         @Override
         public void menuAboutToShow(IMenuManager manager) {
            MenuManager menuManager = (MenuManager) manager;
            menuManager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
         }
      });

      menuManager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));

      xHistoryWidget.getXViewer().getTree().setMenu(
         menuManager.createContextMenu(xHistoryWidget.getXViewer().getTree()));

      getSite().registerContextMenu(VIEW_ID, menuManager, xHistoryWidget.getXViewer());
      getSite().setSelectionProvider(xHistoryWidget.getXViewer());

      HelpUtil.setHelp(parent, OseeHelpContext.HISTORY_VIEW);

      OseeStatusContributionItemFactory.addTo(this, true);

      setupMenus();

      setFocusWidget(xHistoryWidget.getXViewer().getControl());

      OseeEventManager.addListener(this);
   }

   private void setupMenus() {
      Menu popupMenu = new Menu(xHistoryWidget.getXViewer().getTree().getParent());
      OpenOnShowListener openListener = new OpenOnShowListener();
      popupMenu.addMenuListener(openListener);

      OpenContributionItem contributionItem = new OpenContributionItem(getClass().getSimpleName() + ".open");
      contributionItem.fill(popupMenu, -1);
      openListener.add(popupMenu.getItem(0));
      new MenuItem(popupMenu, SWT.SEPARATOR);

      createChangeReportMenuItem(popupMenu);

      new MenuItem(popupMenu, SWT.SEPARATOR);
      createReplaceAttributeWithVersionMenuItem(popupMenu);

      IAction action = new CompareArtifactAction("Compare two Artifacts", xHistoryWidget.getXViewer());
      (new ActionContributionItem(action)).fill(popupMenu, 3);

      (new ActionContributionItem(new EditTransactionComment(this))).fill(popupMenu, 3);
      (new ActionContributionItem(new WasIsCompareEditorAction())).fill(popupMenu, 3);

      XViewerCustomMenu xMenu = new XViewerCustomMenu(xHistoryWidget.getXViewer());
      new MenuItem(popupMenu, SWT.SEPARATOR);
      xMenu.createTableCustomizationMenuItem(popupMenu);
      xMenu.createViewTableReportMenuItem(popupMenu);
      new MenuItem(popupMenu, SWT.SEPARATOR);
      xMenu.addCopyViewMenuBlock(popupMenu);
      new MenuItem(popupMenu, SWT.SEPARATOR);
      xMenu.addFilterMenuBlock(popupMenu);
      new MenuItem(popupMenu, SWT.SEPARATOR);
      xHistoryWidget.getXViewer().getTree().setMenu(popupMenu);
   }

   private class OpenOnShowListener implements MenuListener {
      private final List<MenuItem> items = new LinkedList<MenuItem>();

      public void add(MenuItem item) {
         items.add(item);
      }

      @Override
      public void menuShown(MenuEvent e) {
         for (MenuItem item : items) {
            item.setEnabled(!xHistoryWidget.getXViewer().getSelection().isEmpty());
         }
      }

      @Override
      public void menuHidden(MenuEvent e) {
         // nothing
      }
   }

   private void createReplaceAttributeWithVersionMenuItem(Menu popupMenu) {
      final MenuItem replaceWithMenu = new MenuItem(popupMenu, SWT.CASCADE);
      replaceWithMenu.setText("&Replace Attribute with Version");
      try {
         replaceWithMenu.setEnabled(AccessControlManager.isOseeAdmin());
      } catch (Exception ex) {
         replaceWithMenu.setEnabled(false);
      }
      popupMenu.addMenuListener(new MenuAdapter() {

         @Override
         public void menuShown(MenuEvent e) {
            List<?> selections = ((IStructuredSelection) xHistoryWidget.getXViewer().getSelection()).toList();
            replaceWithMenu.setEnabled(selections.size() == 1 && selections.iterator().next() instanceof AttributeChange);
         }

      });

      replaceWithMenu.addSelectionListener(new SelectionAdapter() {

         @Override
         public void widgetSelected(SelectionEvent e) {
            IStructuredSelection selection = (IStructuredSelection) xHistoryWidget.getXViewer().getSelection();
            Object selectedObject = selection.getFirstElement();

            if (selectedObject instanceof AttributeChange) {
               try {
                  AttributeChange attributeChange = (AttributeChange) selectedObject;
                  Artifact artifact =
                     ArtifactQuery.getArtifactFromId(attributeChange.getArtId(), attributeChange.getBranch());

                  for (Attribute<?> attribute : artifact.getAttributes(attributeChange.getAttributeType())) {
                     if (attribute.getId() == attributeChange.getAttrId()) {
                        attribute.replaceWithVersion((int) attributeChange.getGamma());
                        break;
                     }
                  }

                  artifact.persist("Replace attribute with version");
                  artifact.reloadAttributesAndRelations();

               } catch (OseeCoreException ex) {
                  OseeLog.log(Activator.class, OseeLevel.SEVERE_POPUP, ex);
               }
            }
         }

      });
   }

   private void createChangeReportMenuItem(Menu popupMenu) {
      final MenuItem changeReportMenuItem = new MenuItem(popupMenu, SWT.CASCADE);
      changeReportMenuItem.setText("&Change Report");
      changeReportMenuItem.setImage(ImageManager.getImage(FrameworkImage.BRANCH_CHANGE));
      popupMenu.addMenuListener(new MenuAdapter() {

         @Override
         public void menuShown(MenuEvent e) {
            List<?> selections = ((IStructuredSelection) xHistoryWidget.getXViewer().getSelection()).toList();
            changeReportMenuItem.setEnabled(selections.size() == 1 && ((Change) selections.iterator().next()).getTxDelta().getStartTx().getTxType() != TransactionDetailsType.Baselined);
         }

      });

      changeReportMenuItem.addSelectionListener(new SelectionAdapter() {

         @Override
         public void widgetSelected(SelectionEvent e) {
            IStructuredSelection selection = (IStructuredSelection) xHistoryWidget.getXViewer().getSelection();
            Object selectedObject = selection.getFirstElement();

            if (selectedObject instanceof Change) {
               try {
                  ChangeUiUtil.open(((Change) selectedObject).getTxDelta().getStartTx());
               } catch (OseeCoreException ex) {
                  OseeLog.log(Activator.class, OseeLevel.SEVERE_POPUP, ex);
               }
            }
         }

      });
   }

   private void explore(final Artifact artifact, boolean loadHistory) {
      this.artifact = artifact;

      if (isInitialized()) {
         setPartName("History: " + artifact.getName());
         xHistoryWidget.setInputData(artifact, loadHistory);
      }
   }

   @Override
   public void saveState(IMemento memento) {
      super.saveState(memento);
      memento = memento.createChild(INPUT);
      if (artifact != null) {
         memento.putString(ART_GUID, artifact.getGuid());
         memento.putString(BRANCH_ID, artifact.getBranch().getGuid());
         SkynetViews.addDatabaseSourceId(memento);
      }
   }

   @Override
   public void init(IViewSite site, IMemento memento) throws PartInitException {
      super.init(site, memento);
      try {
         if (memento != null) {
            memento = memento.getChild(INPUT);
            if (memento != null) {
               if (SkynetViews.isSourceValid(memento)) {
                  String guid = memento.getString(ART_GUID);
                  String branchId = memento.getString(BRANCH_ID);
                  Artifact artifact = ArtifactQuery.getArtifactFromId(guid, BranchManager.getBranchByGuid(branchId));
                  openViewUpon(artifact, false);
               } else {
                  closeView();
               }
            }
         }
      } catch (Exception ex) {
         OseeLog.log(Activator.class, Level.WARNING, "History View error on init", ex);
      }
   }

   private void closeView() {
      SkynetViews.closeView(VIEW_ID, getViewSite().getSecondaryId());
   }

   @Override
   public void rebuildMenu() {
      setupMenus();
   }

   @Override
   public void handleBranchEvent(Sender sender, BranchEvent branchEvent) {
      BranchEventType branchModType = branchEvent.getEventType();
      if (branchModType == BranchEventType.Deleting || branchModType == BranchEventType.Deleted || branchModType == BranchEventType.Purging || branchModType == BranchEventType.Purged) {
         Displays.ensureInDisplayThread(new Runnable() {
            @Override
            public void run() {
               closeView();
            }
         });
         return;
      } else if (branchModType == BranchEventType.Committed) {
         Displays.ensureInDisplayThread(new Runnable() {
            @Override
            public void run() {
               try {
                  explore(artifact, true);
               } catch (Exception ex) {
                  OseeLog.log(Activator.class, OseeLevel.SEVERE_POPUP, ex);
               }
            }
         });
         // refresh view with new branch and transaction id
      }
   }

   @Override
   public List<? extends IEventFilter> getEventFilters() {
      return artifact != null ? OseeEventManager.getEventFiltersForBranch(artifact.getBranch()) : Collections.<IEventFilter> emptyList();
   }

   @Override
   public List<TransactionRecord> getSelectedTransactionRecords() {
      return isInitialized() ? xHistoryWidget.getSelectedTransactionRecords() : Collections.<TransactionRecord> emptyList();
   }

   @Override
   public void refreshUI(List<TransactionRecord> records) {
      if (isInitialized()) {
         setPartName("History: " + artifact.getName());
         xHistoryWidget.refresh();
      }
   }

   private void refreshTitle() {
      if (isInitialized()) {
         setPartName("History: " + artifact.getName());
      }
   }

   private boolean isInitialized() {
      return xHistoryWidget != null && artifact != null;
   }

   public static void open(Artifact artifact) throws OseeCoreException {
      Conditions.checkNotNull(artifact, "artifact");
      HistoryView.openViewUpon(artifact, true);
   }

   private static void openViewUpon(final Artifact artifact, final boolean loadHistory) throws OseeCoreException {
      Conditions.checkNotNull(artifact, "artifact");
      Job job = new UIJob("Open History: " + artifact.getName()) {

         @Override
         public IStatus runInUIThread(IProgressMonitor monitor) {
            try {
               IWorkbenchPage page = AWorkbench.getActivePage();
               HistoryView historyView =
                  (HistoryView) page.showView(VIEW_ID, artifact.getGuid() + artifact.getBranch().getGuid(),
                     IWorkbenchPage.VIEW_ACTIVATE);

               historyView.explore(artifact, loadHistory);
            } catch (Exception ex) {
               OseeLog.log(Activator.class, OseeLevel.SEVERE_POPUP, ex);
            }
            return Status.OK_STATUS;
         }
      };
      Jobs.startJob(job);
   }
}

Back to the top