Skip to main content
summaryrefslogtreecommitdiffstats
blob: 6a7c78d99c3846018d3eadcce4da4744cc46ea60 (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
/*******************************************************************************
 * 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.define.viewers;

import java.net.URI;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IAdaptable;
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.util.LocalSelectionTransfer;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TreeSelection;
import org.eclipse.osee.framework.core.enums.CoreArtifactTypes;
import org.eclipse.osee.framework.core.model.Branch;
import org.eclipse.osee.framework.jdk.core.type.OseeCoreException;
import org.eclipse.osee.framework.logging.OseeLog;
import org.eclipse.osee.framework.skynet.core.artifact.Artifact;
import org.eclipse.osee.framework.skynet.core.artifact.ArtifactData;
import org.eclipse.osee.framework.ui.skynet.artifact.ArtifactTransfer;
import org.eclipse.osee.ote.define.jobs.OutfileToArtifactJob;
import org.eclipse.osee.ote.define.utilities.OutfileParserExtensionManager;
import org.eclipse.osee.ote.ui.define.OteUiDefinePlugin;
import org.eclipse.osee.ote.ui.define.dialogs.BranchComboDialog;
import org.eclipse.osee.ote.ui.define.jobs.AddArtifactsToViewerJob;
import org.eclipse.osee.ote.ui.define.jobs.ReportErrorsJob;
import org.eclipse.osee.ote.ui.define.views.TestRunView;
import org.eclipse.swt.dnd.DND;
import org.eclipse.swt.dnd.DragSource;
import org.eclipse.swt.dnd.DragSourceEvent;
import org.eclipse.swt.dnd.DragSourceListener;
import org.eclipse.swt.dnd.DropTarget;
import org.eclipse.swt.dnd.DropTargetAdapter;
import org.eclipse.swt.dnd.DropTargetEvent;
import org.eclipse.swt.dnd.Transfer;

/**
 * @author Roberto E. Escobar
 */
public class DragDropHandler {

   private final XViewerDataManager viewerDataManager;

   public DragDropHandler(XViewerDataManager viewerDataManager) {
      this.viewerDataManager = viewerDataManager;
      setupDropSupport();
      setupDragSupport();
   }

   private void setupDropSupport() {
      DropTarget dropTarget = new DropTarget(viewerDataManager.getControl(), DND.DROP_COPY);
      dropTarget.setTransfer(new Transfer[] {LocalSelectionTransfer.getTransfer(), ArtifactTransfer.getInstance(),});
      dropTarget.addDropListener(new DropTargetAdapter() {

         @Override
         public void drop(DropTargetEvent event) {
            try {
               performDrop(event);
            } catch (OseeCoreException ex) {
               OseeLog.log(OteUiDefinePlugin.class, Level.SEVERE, ex);
            }
         }

         @Override
         public void dragOver(DropTargetEvent event) {
            event.detail = DND.DROP_COPY;
         }
      });
   }

   private void setupDragSupport() {
      DragSource dragSource = new DragSource(viewerDataManager.getControl(), DND.DROP_COPY);
      dragSource.setTransfer(new Transfer[] {ArtifactTransfer.getInstance()});
      dragSource.addDragListener(new DragSourceListener() {

         @Override
         public void dragFinished(DragSourceEvent event) {
         }

         @Override
         public void dragSetData(DragSourceEvent event) {
            List<Artifact> artifacts = viewerDataManager.getSelectedArtifacts();
            if (artifacts.size() > 0) {
               event.data =
                  new ArtifactData(artifacts.toArray(new Artifact[artifacts.size()]), "", TestRunView.VIEW_ID);
            }
         }

         @Override
         public void dragStart(DragSourceEvent event) {
            event.doit = false;
            List<Artifact> artifacts = viewerDataManager.getSelectedArtifacts();
            if (artifacts.size() > 0) {
               event.doit = true;
            }
         }
      });

   }

   private void performDrop(DropTargetEvent e) throws OseeCoreException {
      Object object = e.data;
      if (object instanceof ArtifactData) {
         handleArtifactDrops((ArtifactData) object);
      } else if (object instanceof TreeSelection) {
         StructuredSelection selection = (StructuredSelection) object;
         if (selection.size() > 0) {
            URI[] iFiles = toResourceArray(selection.toArray());
            if (iFiles.length > 0) {
               try {
                  handleResourceDrops(iFiles);
               } catch (OseeCoreException ex) {
                  OseeLog.log(OteUiDefinePlugin.class, Level.SEVERE, ex);
               }
            } else {
               OseeLog.log(OteUiDefinePlugin.class, Level.WARNING, "No valid files dropped");
            }
         }
      }
   }

   private URI[] toResourceArray(Object[] objects) {
      List<URI> toReturn = new ArrayList<>();
      for (Object object : objects) {
         if (object instanceof IAdaptable) {
            Object resource = ((IAdaptable) object).getAdapter(IResource.class);
            if (resource != null) {
               IResource iResource = (IResource) resource;
               if (isResourceAllowed(iResource)) {
                  toReturn.add(iResource.getLocationURI());
               }
            }
         }
      }
      return toReturn.toArray(new URI[toReturn.size()]);
   }

   private boolean isResourceAllowed(IResource resource) {
      if (resource.getType() == IResource.FILE && resource.isAccessible()) {
         String toCheck = resource.getFileExtension();
         try {
            for (String extension : OutfileParserExtensionManager.getInstance().getSupportedExtensions()) {
               if (toCheck.equalsIgnoreCase(extension)) {
                  return true;
               }
            }
         } catch (OseeCoreException ex) {
            // Do Nothing
         }
      }
      return false;

   }

   private void handleArtifactDrops(ArtifactData artifactData) throws OseeCoreException {
      Artifact[] artifactsDropped = artifactData.getArtifacts();
      Set<Artifact> artifactsToAdd = new HashSet<>();
      for (Artifact artifact : artifactsDropped) {
         if (artifact.isOfType(CoreArtifactTypes.TestRun)) {
            artifactsToAdd.add(artifact);
         }
      }
      addArtifactsToTable(new ArrayList<Artifact>(artifactsToAdd));
   }

   private void handleResourceDrops(URI[] iFiles) throws OseeCoreException {
      Branch branch = BranchComboDialog.getBranchFromUser();
      if (branch != null) {
         OutfileToArtifactJob artifactJob = new OutfileToArtifactJob(branch, iFiles);
         artifactJob.addJobChangeListener(new JobChangeAdapter() {

            @Override
            public void done(IJobChangeEvent event) {
               IStatus status = event.getResult();
               if (status.equals(Status.OK_STATUS)) {
                  OutfileToArtifactJob job = (OutfileToArtifactJob) event.getJob();
                  Artifact[] results = job.getResults();
                  URI[] unparseable = job.getUnparseableFiles();
                  reportUnparseableItems(unparseable);
                  addArtifactsToTable(Arrays.asList(results));
               }
            }
         });
         artifactJob.schedule();
      }
   }

   private void addArtifactsToTable(final List<Artifact> artifacts) {
      Job job = new AddArtifactsToViewerJob(viewerDataManager, artifacts);
      job.schedule();
   }

   private void reportUnparseableItems(final URI[] unparseable) {
      if (unparseable.length > 0) {
         String title = "Artifact Drop Error";
         String message = "The following file(s) had errors during the parsing operation: ";
         ReportErrorsJob.openError(title, message, (Object[]) unparseable);
      }
   }
}

Back to the top