Skip to main content
summaryrefslogtreecommitdiffstats
blob: a858ab77a508947858999cf8425336d0b81a89d2 (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
/*******************************************************************************
 * 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;

import java.io.File;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.osee.framework.core.enums.CoreRelationTypes;
import org.eclipse.osee.framework.core.enums.RelationOrderBaseTypes;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
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.artifact.ArtifactData;
import org.eclipse.osee.framework.skynet.core.transaction.SkynetTransaction;
import org.eclipse.osee.framework.ui.plugin.util.Wizards;
import org.eclipse.osee.framework.ui.skynet.Import.ArtifactImportWizard;
import org.eclipse.osee.framework.ui.skynet.artifact.ArtifactTransfer;
import org.eclipse.osee.framework.ui.skynet.update.InterArtifactExplorerDropHandler;
import org.eclipse.osee.framework.ui.skynet.util.SkynetDragAndDrop;
import org.eclipse.swt.dnd.DND;
import org.eclipse.swt.dnd.DropTargetEvent;
import org.eclipse.swt.dnd.FileTransfer;
import org.eclipse.swt.widgets.TreeItem;
import org.eclipse.ui.IViewPart;

/**
 * @author Jeff C. Phillips
 */
public class ArtifactExplorerDragAndDrop extends SkynetDragAndDrop {
   private final TreeViewer treeViewer;
   private final String viewId;
   private final IViewPart viewPart;
   private final InterArtifactExplorerDropHandler interArtifactExplorerHandler;

   public ArtifactExplorerDragAndDrop(TreeViewer treeViewer, String viewId, IViewPart viewPart) {
      super(treeViewer.getTree(), treeViewer.getTree(), viewId);

      this.treeViewer = treeViewer;
      this.viewId = viewId;
      this.viewPart = viewPart;
      this.interArtifactExplorerHandler = new InterArtifactExplorerDropHandler();
   }

   @Override
   public Artifact[] getArtifacts() {
      IStructuredSelection selection = (IStructuredSelection) treeViewer.getSelection();
      Object[] objects = selection.toArray();
      Artifact[] artifacts = new Artifact[objects.length];

      for (int index = 0; index < objects.length; index++) {
         artifacts[index] = (Artifact) objects[index];
      }

      return artifacts;
   }

   @Override
   public void performDragOver(DropTargetEvent event) {
      event.feedback = DND.FEEDBACK_SELECT | DND.FEEDBACK_SCROLL | DND.FEEDBACK_EXPAND;

      if (FileTransfer.getInstance().isSupportedType(event.currentDataType)) {
         event.detail = DND.DROP_COPY;
      } else if (isValidForArtifactDrop(event)) {
         event.detail = DND.DROP_MOVE;
      } else {
         event.detail = DND.DROP_NONE;
      }
   }

   private boolean isValidForArtifactDrop(DropTargetEvent event) {
      if (ArtifactTransfer.getInstance().isSupportedType(event.currentDataType)) {
         ArtifactData artData = ArtifactTransfer.getInstance().nativeToJava(event.currentDataType);

         if (artData != null) {

            Artifact parentArtifact = getSelectedArtifact(event);
            if (parentArtifact != null && artData.getSource().equals(viewId)) {
               Artifact[] artifactsToBeRelated = artData.getArtifacts();

               for (Artifact artifact : artifactsToBeRelated) {
                  if (parentArtifact.equals(artifact)) {
                     return false;
                  }
               }
               return true;
            }
         } else {
            // only occurs during the drag on some platforms
            return true;
         }
      }
      return false;
   }

   private Artifact getSelectedArtifact(DropTargetEvent event) {
      TreeItem selected = treeViewer.getTree().getItem(treeViewer.getTree().toControl(event.x, event.y));

      if (selected != null && selected.getData() instanceof Artifact) {
         return (Artifact) selected.getData();
      }
      return null;
   }

   @Override
   public void performDrop(final DropTargetEvent event) {
      final Artifact parentArtifact = getSelectedArtifact(event);

      if (parentArtifact != null) {
         if (ArtifactTransfer.getInstance().isSupportedType(event.currentDataType)) {
            ArtifactData artData = ArtifactTransfer.getInstance().nativeToJava(event.currentDataType);
            final Artifact[] artifactsToBeRelated = artData.getArtifacts();
            if (artifactsToBeRelated != null && artifactsToBeRelated.length > 0 && !artifactsToBeRelated[0].getBranch().equals(
               parentArtifact.getBranch())) {
               try {
                  interArtifactExplorerHandler.dropArtifactIntoDifferentBranch(parentArtifact, artifactsToBeRelated,
                     true);
               } catch (OseeCoreException ex) {
                  OseeLog.log(SkynetGuiPlugin.class, OseeLevel.SEVERE_POPUP, ex);
               }
            } else if (isValidForArtifactDrop(event) && MessageDialog.openQuestion(
               viewPart.getViewSite().getShell(),
               "Confirm Move",
               "Are you sure you want to make each of the selected artifacts a child of " + parentArtifact.getName() + "?")) {
               try {
                  SkynetTransaction transaction =
                     new SkynetTransaction(parentArtifact.getBranch(), "Artifact explorer drag & drop");
                  // Replace all of the parent relations
                  for (Artifact artifact : artifactsToBeRelated) {
                     Artifact currentParent = artifact.getParent();
                     if (currentParent != null) {
                        currentParent.deleteRelation(CoreRelationTypes.Default_Hierarchical__Child, artifact);
                        currentParent.persist(transaction);
                     }
                     parentArtifact.addChild(RelationOrderBaseTypes.USER_DEFINED, artifact);
                     parentArtifact.persist(transaction);
                  }
                  transaction.execute();
               } catch (OseeCoreException ex) {
                  OseeLog.log(getClass(), OseeLevel.SEVERE_POPUP, ex);
               }
            }
         } else if (FileTransfer.getInstance().isSupportedType(event.currentDataType)) {
            Object object = FileTransfer.getInstance().nativeToJava(event.currentDataType);
            if (object instanceof String[]) {
               String filename = ((String[]) object)[0];

               ArtifactImportWizard wizard = new ArtifactImportWizard();
               wizard.setImportResourceAndArtifactDestination(new File(filename), parentArtifact);

               Wizards.initAndOpen(wizard, viewPart);
            }
         }
      }
   }
}

Back to the top