Skip to main content
summaryrefslogtreecommitdiffstats
blob: 5172f4a5f26e7860cca11de5c625c571689b79d7 (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
/*******************************************************************************
 * 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.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.logging.Level;
import org.eclipse.osee.framework.jdk.core.type.OseeCoreException;
import org.eclipse.osee.framework.jdk.core.util.Collections;
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.HTMLTransferFormatter;
import org.eclipse.osee.framework.ui.skynet.artifact.ArtifactTransfer;
import org.eclipse.osee.framework.ui.skynet.internal.Activator;
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.FileTransfer;
import org.eclipse.swt.dnd.HTMLTransfer;
import org.eclipse.swt.dnd.TextTransfer;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.swt.widgets.Control;

/**
 * @author Jeff C. Phillips
 */
public abstract class SkynetDragAndDrop {
   private final String viewId;
   private DragSource source;
   private DropTarget target;

   public SkynetDragAndDrop(Control dragAndDropControl, String viewId) {
      this(dragAndDropControl, dragAndDropControl, viewId);
   }

   /**
    * Caller may optionally pass null for either the dragSource or dropTarget when both are not needed
    */
   public SkynetDragAndDrop(Control dragSource, Control dropTarget, String viewId) {
      this.viewId = viewId;
      if (dragSource != null) {
         source = new DragSource(dragSource, DND.DROP_MOVE | DND.DROP_COPY);
         setupDragSupport();
      }
      if (dropTarget != null) {
         target = new DropTarget(dropTarget, DND.DROP_MOVE | DND.DROP_COPY);
         setupDropSupport();
      }
   }

   private void setupDragSupport() {
      source.setTransfer(new Transfer[] {
         HTMLTransfer.getInstance(),
         ArtifactTransfer.getInstance(),
         TextTransfer.getInstance()});
      source.addDragListener(new DragSourceListener() {

         @Override
         public void dragFinished(DragSourceEvent event) {
            // do nothing
         }

         @Override
         public void dragSetData(DragSourceEvent event) {
            performDataTransafer(event);
         }

         @Override
         public void dragStart(DragSourceEvent event) {
            // do nothing
         }
      });
   }

   private void performDataTransafer(DragSourceEvent event) {
      if (HTMLTransfer.getInstance().isSupportedType(event.dataType)) {
         htmlTransferDragSetData(event);
      } else if (ArtifactTransfer.getInstance().isSupportedType(event.dataType)) {
         artifactTransferDragSetData(event);
      } else if (TextTransfer.getInstance().isSupportedType(event.dataType)) {
         textTransferDragSetData(event);
      }
   }

   private void setupDropSupport() {
      target.setTransfer(new Transfer[] {
         FileTransfer.getInstance(),
         TextTransfer.getInstance(),
         ArtifactTransfer.getInstance()});
      target.addDropListener(new DropTargetAdapter() {

         @Override
         public void dragOperationChanged(DropTargetEvent event) {
            operationChanged(event);
         }

         @Override
         public void drop(DropTargetEvent event) {
            performDrop(event);
         }

         @Override
         public void dragOver(DropTargetEvent event) {
            try {
               performDragOver(event);
            } catch (Exception ex) {
               OseeLog.log(Activator.class, Level.SEVERE, ex);
            }
         }

         @Override
         public void dropAccept(DropTargetEvent event) {
            // do nothing
         }
      });
   }

   public void performDragOver(DropTargetEvent event) throws OseeCoreException {
      // provided for subclass implementation
   }

   public void artifactTransferDragSetData(DragSourceEvent event) {
      try {
         if (getArtifacts() != null && getArtifacts().length > 0) {
            event.data = new ArtifactData(getArtifacts(), "work", viewId);
         }
      } catch (Exception ex) {
         //         OSEELog.logException(ChangeReportView.class, ex, true);
      }
   }

   public void htmlTransferDragSetData(DragSourceEvent event) {
      try {
         if (getArtifacts() != null && getArtifacts().length > 0) {
            event.data = HTMLTransferFormatter.getHtml(getArtifacts());
         }
      } catch (Exception ex) {
         //         OSEELog.logException(ChangeReportView.class, ex, true);
      }
   }

   public void textTransferDragSetData(DragSourceEvent event) {
      try {
         if (getArtifacts() != null && getArtifacts().length > 0) {
            Artifact[] artifacts = getArtifacts();
            Collection<String> names = new ArrayList<String>(artifacts.length);

            for (Artifact artifact : artifacts) {
               names.add(artifact.getName());
            }

            event.data = Collections.toString(names, null, ", ", null);
         }
      } catch (Exception ex) {
         //         OSEELog.logException(ChangeReportView.class, ex, true);
      }
   }

   /**
    * Override this method to supply the base class with artifacts to be used for drag and drop.
    */
   public abstract Artifact[] getArtifacts() throws Exception;

   /**
    * Override this method to implement the drop operation.
    */
   public void performDrop(DropTargetEvent event) {
      if (event.data instanceof ArtifactData) {
         performArtifactDrop(((ArtifactData) event.data).getArtifacts());
      } else if (event.data instanceof String[]) {
         performFileDrop((String[]) event.data);
      } else if (event.data instanceof String) {
         performTextDrop((String) event.data);
      }
   }

   public void operationChanged(DropTargetEvent event) {
      // provided for subclass implementation
   }

   /**
    * override this method and its cousins rather than performDrop in order to have the drop data preprocessed and
    * passed in the desired form
    */
   public void performTextDrop(String text) {
      // provided for subclass implementation
   }

   /**
    * override this method and its cousins rather than performDrop in order to have the drop data preprocessed and
    * passed in the desired form
    */
   public void performArtifactDrop(Artifact[] dropArtifacts) {
      // provided for subclass implementation
   }

   /**
    * override this method and its cousins rather than performDrop in order to have the drop data preprocessed and
    * passed in the desired form
    */
   public void performFileDrop(String[] fileNames) {
      // provided for subclass implementation
   }
}

Back to the top