Skip to main content
summaryrefslogtreecommitdiffstats
blob: 169e489d7a97859948493405284a0b7c5b1f0b1a (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
/*******************************************************************************
 * 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.ats.task;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
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.viewers.ISelectionProvider;
import org.eclipse.osee.ats.core.task.TaskArtifact;
import org.eclipse.osee.ats.core.type.AtsArtifactTypes;
import org.eclipse.osee.ats.core.util.AtsUtilCore;
import org.eclipse.osee.ats.core.workflow.AbstractWorkflowArtifact;
import org.eclipse.osee.ats.internal.Activator;
import org.eclipse.osee.ats.util.AtsUtil;
import org.eclipse.osee.ats.world.AtsMetricsComposite;
import org.eclipse.osee.ats.world.IAtsMetricsProvider;
import org.eclipse.osee.ats.world.WorldEditor;
import org.eclipse.osee.ats.world.WorldEditorParameterSearchItemProvider;
import org.eclipse.osee.ats.world.search.WorldSearchItem.SearchType;
import org.eclipse.osee.framework.core.exception.OseeArgumentException;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.framework.core.util.IWorkPage;
import org.eclipse.osee.framework.core.util.Result;
import org.eclipse.osee.framework.logging.OseeLevel;
import org.eclipse.osee.framework.logging.OseeLog;
import org.eclipse.osee.framework.skynet.core.artifact.Artifact;
import org.eclipse.osee.framework.skynet.core.transaction.SkynetTransaction;
import org.eclipse.osee.framework.ui.plugin.util.AWorkbench;
import org.eclipse.osee.framework.ui.plugin.xnavigate.XNavigateComposite.TableLoadOption;
import org.eclipse.osee.framework.ui.skynet.OseeStatusContributionItemFactory;
import org.eclipse.osee.framework.ui.skynet.artifact.editor.AbstractArtifactEditor;
import org.eclipse.osee.framework.ui.swt.CursorManager;
import org.eclipse.osee.framework.ui.swt.Displays;
import org.eclipse.osee.framework.ui.swt.IDirtiableEditor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorReference;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;

/**
 * @author Donald G. Dunne
 */
public class TaskEditor extends AbstractArtifactEditor implements IAtsMetricsProvider, IXTaskViewer {
   public static final String EDITOR_ID = "org.eclipse.osee.ats.editor.TaskEditor";
   private int mainPageIndex, metricsPageIndex;
   private TaskEditorXWidgetActionPage taskActionPage;
   private final Collection<TaskArtifact> tasks = new HashSet<TaskArtifact>();
   private boolean loading = false;
   public final static int TITLE_MAX_LENGTH = WorldEditor.TITLE_MAX_LENGTH;

   @Override
   public void doSave(IProgressMonitor monitor) {
      try {
         SkynetTransaction transaction = new SkynetTransaction(AtsUtil.getAtsBranch(), "Task Editor Save");
         for (TaskArtifact taskArt : tasks) {
            taskArt.saveSMA(transaction);
         }
         transaction.execute();
      } catch (Exception ex) {
         OseeLog.log(Activator.class, OseeLevel.SEVERE_POPUP, ex);
      }
      onDirtied();
   }

   public List<Artifact> getLoadedArtifacts() {
      return taskActionPage.getTaskComposite().getTaskXViewer().getLoadedArtifacts();
   }

   @Override
   public boolean isSaveOnCloseNeeded() {
      return isDirty();
   }

   public static Collection<TaskEditor> getEditors() {
      final List<TaskEditor> editors = new ArrayList<TaskEditor>();
      Displays.pendInDisplayThread(new Runnable() {
         @Override
         public void run() {
            for (IEditorReference editor : AWorkbench.getEditors(EDITOR_ID)) {
               editors.add((TaskEditor) editor.getEditor(false));
            }
         }
      });
      return editors;
   }

   public static void closeAll() {
      Displays.ensureInDisplayThread(new Runnable() {
         @Override
         public void run() {
            for (IEditorReference editor : AWorkbench.getEditors(EDITOR_ID)) {
               AWorkbench.getActivePage().closeEditor(editor.getEditor(false), false);
            }
         }
      });
   }

   public void setTableTitle(final String title, final boolean warning) {
      taskActionPage.setTableTitle(title, warning);
   }

   @Override
   public void dispose() {
      for (TaskArtifact taskArt : tasks) {
         if (taskArt != null && !taskArt.isDeleted() && taskArt.isSMAEditorDirty().isTrue()) {
            taskArt.revertSMA();
         }
      }
      if (taskActionPage != null && taskActionPage.getTaskComposite() != null) {
         taskActionPage.getTaskComposite().disposeComposite();
      }
      super.dispose();
   }

   @Override
   public boolean isDirty() {
      for (TaskArtifact taskArt : new CopyOnWriteArrayList<TaskArtifact>(tasks)) {
         if (taskArt.isDeleted()) {
            continue;
         } else if (taskArt.isSMAEditorDirty().isTrue()) {
            return true;
         }
      }
      return false;
   }

   @Override
   public String toString() {
      return "TaskEditor";
   }

   /**
    * @return the taskActionPage
    */
   public TaskEditorXWidgetActionPage getTaskActionPage() {
      return taskActionPage;
   }

   @Override
   protected void addPages() {

      try {
         OseeStatusContributionItemFactory.addTo(this, true);

         IEditorInput editorInput = getEditorInput();
         if (!(editorInput instanceof TaskEditorInput)) {
            throw new OseeArgumentException("Editor Input not TaskEditorInput");
         }

         createMainTab();
         createMetricsTab();

         setActivePage(mainPageIndex);
         loadTable();

         getSite().setSelectionProvider(getTaskActionPage().getTaskComposite().getTaskXViewer());

      } catch (OseeCoreException ex) {
         OseeLog.log(Activator.class, Level.SEVERE, ex);
      } catch (PartInitException ex) {
         OseeLog.log(Activator.class, Level.SEVERE, ex);
      }
   }

   private void createMainTab() throws PartInitException {
      taskActionPage = new TaskEditorXWidgetActionPage(this);
      mainPageIndex = addPage(taskActionPage);
   }

   private void createMetricsTab() {
      Composite comp = AtsUtil.createCommonPageComposite(getContainer());
      AtsUtil.createCommonToolBar(comp);
      new AtsMetricsComposite(this, comp, SWT.NONE);
      metricsPageIndex = addPage(comp);
      setPageText(metricsPageIndex, "Metrics");
   }

   public ITaskEditorProvider getTaskEditorProvider() {
      TaskEditorInput aei = (TaskEditorInput) getEditorInput();
      return aei.getItaskEditorProvider();
   }

   private void loadTable() throws OseeCoreException {
      ITaskEditorProvider provider = getTaskEditorProvider();
      setPartName(provider.getTaskEditorLabel(SearchType.Search));

      if (provider instanceof TaskEditorParameterSearchItemProvider && ((TaskEditorParameterSearchItemProvider) provider).isFirstTime()) {
         setPartName(provider.getName());
         setTableTitle(WorldEditorParameterSearchItemProvider.ENTER_OPTIONS_AND_SELECT_SEARCH, false);
         return;
      }
      if (provider instanceof TaskEditorParameterSearchItemProvider) {
         Result result =
            ((TaskEditorParameterSearchItemProvider) provider).getWorldSearchItem().isParameterSelectionValid();
         if (result.isFalse()) {
            AWorkbench.popup(result);
            return;
         }
      }
      if (loading) {
         AWorkbench.popup("Already Loading, Please Wait");
         return;
      }
      LoadTableJob job = null;
      job = new LoadTableJob(provider, SearchType.ReSearch, this);
      job.setUser(false);
      job.setPriority(Job.LONG);
      job.schedule();
      if (provider.getTableLoadOptions() != null && provider.getTableLoadOptions().contains(TableLoadOption.ForcePend)) {
         try {
            job.join();
         } catch (InterruptedException ex) {
            OseeLog.log(Activator.class, Level.SEVERE, ex);
         }
      }
   }

   @Override
   public void onDirtied() {
      Displays.ensureInDisplayThread(new Runnable() {
         @Override
         public void run() {
            firePropertyChange(PROP_DIRTY);
         }
      });
   }

   public static void open(final ITaskEditorProvider provider) throws OseeCoreException {
      Displays.ensureInDisplayThread(new Runnable() {
         @Override
         public void run() {
            IWorkbenchPage page = AWorkbench.getActivePage();
            try {
               page.openEditor(new TaskEditorInput(provider), EDITOR_ID);
            } catch (PartInitException ex) {
               OseeLog.log(Activator.class, OseeLevel.SEVERE_POPUP, ex);
            }
         }
      }, (provider.getTableLoadOptions() != null && provider.getTableLoadOptions().contains(TableLoadOption.ForcePend)));
   }

   private static class LoadTableJob extends Job {

      private final ITaskEditorProvider itaskEditorProvider;
      private final TaskEditor taskEditor;
      private final SearchType searchType;

      public LoadTableJob(ITaskEditorProvider itaskEditorProvider, SearchType searchType, TaskEditor taskEditor) throws OseeCoreException {
         super("Loading \"" + itaskEditorProvider.getTaskEditorLabel(searchType) + "\"");
         this.searchType = searchType;
         this.taskEditor = taskEditor;
         taskEditor.setPartName(itaskEditorProvider.getTaskEditorLabel(searchType));
         taskEditor.setTableTitle("Loading \"" + itaskEditorProvider.getTaskEditorLabel(searchType) + "\"", false);
         this.itaskEditorProvider = itaskEditorProvider;
      }

      @Override
      protected IStatus run(IProgressMonitor monitor) {
         if (taskEditor.isLoading()) {
            return new Status(IStatus.ERROR, Activator.PLUGIN_ID, -1, "Already Loading, Please Wait", null);
         }
         try {
            taskEditor.setLoading(true);
            taskEditor.getTaskActionPage().getTaskComposite().getTaskXViewer().clear(true);
            final List<TaskArtifact> taskArts = new ArrayList<TaskArtifact>();
            for (Artifact artifact : itaskEditorProvider.getTaskEditorTaskArtifacts()) {
               if (artifact.isOfType(AtsArtifactTypes.Task)) {
                  taskArts.add((TaskArtifact) artifact);
               }
            }
            taskEditor.tasks.clear();
            taskEditor.tasks.addAll(taskArts);
            Displays.pendInDisplayThread(new Runnable() {
               @Override
               public void run() {
                  try {
                     taskEditor.setPartName(itaskEditorProvider.getTaskEditorLabel(searchType));
                     if (taskArts.isEmpty()) {
                        taskEditor.setTableTitle(
                           "No Results Found - " + itaskEditorProvider.getTaskEditorLabel(searchType), true);
                     } else {
                        taskEditor.setTableTitle(itaskEditorProvider.getTaskEditorLabel(searchType), false);
                     }
                     taskEditor.getTaskActionPage().getTaskComposite().loadTable();
                  } catch (OseeCoreException ex) {
                     OseeLog.log(Activator.class, Level.SEVERE, ex);
                  }
               }
            });
         } catch (final Exception ex) {
            monitor.done();
            return new Status(IStatus.ERROR, Activator.PLUGIN_ID, -1, "Can't load tasks", ex);
         } finally {
            taskEditor.setLoading(false);
         }
         monitor.done();
         return Status.OK_STATUS;
      }
   }

   @Override
   public Collection<? extends Artifact> getMetricsArtifacts() {
      return tasks;
   }

   @Override
   public Artifact getMetricsVersionArtifact() throws OseeCoreException {
      for (TaskArtifact taskArt : tasks) {
         if (taskArt.getTargetedVersion() != null) {
            return taskArt.getTargetedVersion();
         }
      }
      return null;
   }

   @Override
   public String getCurrentStateName() {
      return "";
   }

   @Override
   public IDirtiableEditor getEditor() {
      return this;
   }

   @Override
   public AbstractWorkflowArtifact getAwa() {
      return null;
   }

   @Override
   public String getTabName() {
      return "Tasks";
   }

   @Override
   public Collection<TaskArtifact> getTaskArtifacts(IWorkPage state) {
      return tasks;
   }

   @Override
   public Collection<TaskArtifact> getTaskArtifacts() {
      return tasks;
   }

   @Override
   public boolean isTaskable() {
      return false;
   }

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

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

   @Override
   public void handleRefreshAction() throws OseeCoreException {
      loadTable();
   }

   @Override
   public double getManHoursPerDayPreference() throws OseeCoreException {
      if (tasks.isEmpty()) {
         return AtsUtilCore.DEFAULT_HOURS_PER_WORK_DAY;
      }
      return tasks.iterator().next().getManHrsPerDayPreference();
   }

   public boolean isLoading() {
      return loading;
   }

   public void setLoading(final boolean loading) {
      this.loading = loading;
      Displays.ensureInDisplayThread(new Runnable() {
         @Override
         public void run() {
            if (loading) {
               taskActionPage.getTaskComposite().setCursor(CursorManager.getCursor(SWT.CURSOR_WAIT));
            } else {
               taskActionPage.getTaskComposite().setCursor(null);
            }
         }
      });
   }

   @Override
   protected void pageChange(int newPageIndex) {
      super.pageChange(newPageIndex);
      if (newPageIndex != -1 && pages.size() > newPageIndex) {
         Object page = pages.get(newPageIndex);
         if (page != null) {
            ISelectionProvider provider = taskActionPage.getTaskComposite().getTaskXViewer();
            String title = getPageText(newPageIndex);
            if (title.equalsIgnoreCase("metrics")) {
               provider = null;
            }
            getSite().setSelectionProvider(provider);
         }
      }
   }
}

Back to the top