Skip to main content
summaryrefslogtreecommitdiffstats
blob: 7646a3b4dbbd12297d422808ebd6e7f7006ed75d (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
/*******************************************************************************
 * Copyright (c) 2015 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.change.view;

import java.util.Collections;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
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.action.IToolBarManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.nebula.widgets.xviewer.XViewer;
import org.eclipse.osee.framework.core.data.IOseeBranch;
import org.eclipse.osee.framework.core.model.TransactionRecord;
import org.eclipse.osee.framework.core.operation.OperationBuilder;
import org.eclipse.osee.framework.core.operation.Operations;
import org.eclipse.osee.framework.jdk.core.util.Lib;
import org.eclipse.osee.framework.jdk.core.util.Strings;
import org.eclipse.osee.framework.logging.OseeLevel;
import org.eclipse.osee.framework.logging.OseeLog;
import org.eclipse.osee.framework.skynet.core.SystemGroup;
import org.eclipse.osee.framework.skynet.core.artifact.BranchManager;
import org.eclipse.osee.framework.skynet.core.event.OseeEventManager;
import org.eclipse.osee.framework.skynet.core.event.listener.ITransactionEventListener;
import org.eclipse.osee.framework.skynet.core.event.model.Sender;
import org.eclipse.osee.framework.skynet.core.event.model.TransactionEvent;
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.change.BranchTransactionUiData;
import org.eclipse.osee.framework.ui.skynet.change.ChangeReportEditorInput;
import org.eclipse.osee.framework.ui.skynet.change.actions.ShowChangeReportAction;
import org.eclipse.osee.framework.ui.skynet.change.actions.ShowChangeReportSinceAction;
import org.eclipse.osee.framework.ui.skynet.change.operations.LoadAssociatedArtifactOperation;
import org.eclipse.osee.framework.ui.skynet.change.operations.LoadBranchTransactionsOperation;
import org.eclipse.osee.framework.ui.skynet.internal.Activator;
import org.eclipse.osee.framework.ui.skynet.util.DbConnectionExceptionComposite;
import org.eclipse.osee.framework.ui.skynet.widgets.xBranch.XBranchContentProvider;
import org.eclipse.osee.framework.ui.skynet.widgets.xBranch.XBranchWidget;
import org.eclipse.osee.framework.ui.skynet.widgets.xBranch.XBranchWidget.IBranchWidgetMenuListener;
import org.eclipse.osee.framework.ui.swt.Displays;
import org.eclipse.osee.framework.ui.swt.Widgets;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IWorkbenchActionConstants;
import org.eclipse.ui.forms.IManagedForm;
import org.eclipse.ui.forms.editor.FormPage;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.ScrolledForm;
import org.eclipse.ui.progress.UIJob;

/**
 * @author Donald G. Dunne
 */
public class BranchTransactionPage extends FormPage implements IBranchWidgetMenuListener, ITransactionEventListener, ITransactionRecordSelectionProvider {

   private XBranchWidget xBranchWidget;
   private final IOseeBranch branch;
   private final AtomicBoolean refreshNeeded = new AtomicBoolean(false);
   private final AtomicBoolean processEvents = new AtomicBoolean(false);
   private BranchTransactionUiData branchTransactionData;

   public BranchTransactionPage(ChangeReportEditor editor, IOseeBranch branch) {
      super(editor, "transaction", "Transactions");
      this.branch = branch;
   }

   @Override
   public void showBusy(boolean busy) {
      super.showBusy(busy);
      if (Widgets.isAccessible(getManagedForm().getForm())) {
         getManagedForm().getForm().getForm().setBusy(busy);
      }
   }

   @Override
   protected void createFormContent(IManagedForm managedForm) {
      super.createFormContent(managedForm);

      final ScrolledForm form = managedForm.getForm();
      final FormToolkit toolkit = managedForm.getToolkit();

      Composite parent = form.getBody();

      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));

      if (DbConnectionExceptionComposite.dbConnectionIsOk(parent)) {

         xBranchWidget = new XBranchWidget(new BranchTransactionXViewerFactory(), this);
         xBranchWidget.setDisplayLabel(false);
         xBranchWidget.createWidgets(parent, 1);
         xBranchWidget.adaptControls(toolkit);
         xBranchWidget.getXViewer().setColumnMultiEditEnabled(false);

         XBranchContentProvider contentProvider =
            (XBranchContentProvider) xBranchWidget.getXViewer().getContentProvider();
         contentProvider.setShowTransactions(true);
         contentProvider.setMaxPerList(500);
         getSite().setSelectionProvider(xBranchWidget.getXViewer());
         OseeEventManager.addListener(this);
      }

      updateTitle(form);
      updateImage(form);

      managedForm.getMessageManager().setAutoUpdate(false);

      addToolBar(toolkit, form, true);
      form.reflow(true);

      if (getEditorInput().isNotLoaded()) {
         xBranchWidget.setExtraInfoLabel("Cleared on shut down. Press refresh to reload.");
      } else {
         recomputeBranchTransactions(true);
      }

   }

   @Override
   public void updateMenuActionsForTable(MenuManager mm) {
      mm.insertBefore(XViewer.MENU_GROUP_PRE, new ShowChangeReportSinceAction(getEditorInput().getBranch(), this));
      mm.insertBefore(XViewer.MENU_GROUP_PRE, new ShowChangeReportAction(this));
      if (SystemGroup.OseeAdmin.isCurrentUserMember()) {
         mm.insertBefore(XViewer.MENU_GROUP_PRE, new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
         mm.insertBefore(XViewer.MENU_GROUP_PRE, new EditTransactionComment(this));
      }
   }

   public void recomputeBranchTransactions(boolean isReloadAllowed) {
      OperationBuilder builder = Operations.createBuilder("Load Branch Transactions");
      branchTransactionData = new BranchTransactionUiData(BranchManager.getBranch(getEditorInput().getBranch()),
         (XBranchContentProvider) xBranchWidget.getXViewer().getContentProvider());
      builder.addOp(new LoadBranchTransactionsOperation(branchTransactionData));
      builder.addOp(new LoadAssociatedArtifactOperation(getEditor().getChanges()));
      Operations.executeAsJob(builder.build(), true, Job.LONG, new ReloadJobChangeAdapter());
   }

   private final class ReloadJobChangeAdapter extends JobChangeAdapter {
      private long startTime = 0;

      @Override
      public void scheduled(IJobChangeEvent event) {
         super.scheduled(event);
         getEditor().getBranchTransactionActionBarContributor().getReloadAction().setEnabled(false);
         showBusy(true);
      }

      @Override
      public void aboutToRun(IJobChangeEvent event) {
         super.aboutToRun(event);
         startTime = System.currentTimeMillis();
      }

      @Override
      public void done(IJobChangeEvent event) {
         super.done(event);
         String message = String.format("Branch Transactions Load completed in [%s]", Lib.getElapseString(startTime));
         final XBranchWidget fXBranchWidget = xBranchWidget;
         OseeLog.log(Activator.class, Level.INFO, message);
         Job job = new UIJob("Reload Branch Transactions") {

            @Override
            public IStatus runInUIThread(IProgressMonitor monitor) {
               if (Widgets.isAccessible(getManagedForm().getForm())) {
                  fXBranchWidget.loadData(branchTransactionData);
                  getEditor().getBranchTransactionActionBarContributor().getReloadAction().setEnabled(true);
                  getEditor().getBranchTransactionActionBarContributor().getOpenAssociatedArtifactAction().updateEnablement();
                  getManagedForm().getForm().getBody().layout(true, true);
                  getManagedForm().getForm().reflow(true);
                  getManagedForm().refresh();
                  showBusy(false);
               }
               return Status.OK_STATUS;
            }
         };
         Operations.scheduleJob(job, false, Job.SHORT, null);
      }
   }

   private void updateTitle(ScrolledForm form) {
      form.setText(Strings.escapeAmpersands(getEditorInput().getBranchTransactionName()));
   }

   private void updateImage(ScrolledForm form) {
      form.setImage(getEditor().getEditorInput().getBranchTransactionImage());
   }

   private void addToolBar(FormToolkit toolkit, ScrolledForm form, boolean add) {
      IToolBarManager manager = form.getToolBarManager();
      if (add) {
         getEditor().getBranchTransactionActionBarContributor().contributeToToolBar(manager);
         manager.add(xBranchWidget.getXViewer().getCustomizeAction());
         manager.update(true);
      } else {
         manager.removeAll();
      }
      form.reflow(true);
   }

   @Override
   public ChangeReportEditor getEditor() {
      return (ChangeReportEditor) super.getEditor();
   }

   @Override
   public ChangeReportEditorInput getEditorInput() {
      return (ChangeReportEditorInput) super.getEditorInput();
   }

   private boolean isInitialized() {
      return xBranchWidget != null;
   }

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

   @Override
   public void refreshUI(List<TransactionRecord> records) {
      if (isInitialized()) {
         xBranchWidget.loadData(branch);
      }
   }

   @Override
   public void handleTransactionEvent(Sender sender, TransactionEvent transEvent) {
      refreshNeeded.set(true);
      if (isInitialized() && processEvents.get()) {
         refreshIfNeeded();
      }
   }

   private void refreshIfNeeded() {
      if (refreshNeeded.compareAndSet(true, false)) {
         Displays.ensureInDisplayThread(new Runnable() {
            @Override
            public void run() {
               try {
                  xBranchWidget.loadData(branch);
               } catch (Exception ex) {
                  OseeLog.log(Activator.class, OseeLevel.SEVERE_POPUP, ex);
               }
            }
         });
      }
   }

   public void setSelectionProvider() {
      getSite().setSelectionProvider(xBranchWidget.getXViewer());
   }

}

Back to the top