Skip to main content
summaryrefslogtreecommitdiffstats
blob: db77a74f58739b582b11714b1f089dd8106b1199 (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
/*******************************************************************************
 * 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.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.osee.framework.core.enums.PermissionEnum;
import org.eclipse.osee.framework.jdk.core.type.OseeCoreException;
import org.eclipse.osee.framework.jdk.core.util.Collections;
import org.eclipse.osee.framework.skynet.core.AccessPolicy;
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.swt.Displays;
import org.eclipse.swt.dnd.Clipboard;
import org.eclipse.swt.dnd.HTMLTransfer;
import org.eclipse.swt.dnd.TextTransfer;
import org.eclipse.swt.dnd.Transfer;

/**
 * @author Jeff C. Phillips
 */
public class ArtifactClipboard {
   private static final String STATUS = "work";
   private Clipboard clipboard;
   private final String viewId;

   public ArtifactClipboard(String viewId) {
      this.clipboard = new Clipboard(null);
      this.viewId = viewId;
   }

   private List<Artifact> getArtifactsWithPermission(AccessPolicy accessService, PermissionEnum permission, List<Artifact> artifacts) throws OseeCoreException {
      ArrayList<Artifact> toReturn = new ArrayList<Artifact>(artifacts);
      Iterator<Artifact> artIterator = toReturn.iterator();

      // Remove Artifact that do not have write permission.
      while (artIterator.hasNext()) {
         Artifact cur = artIterator.next();
         if (!accessService.hasArtifactPermission(java.util.Collections.singleton(cur), permission, Level.WARNING).matched()) {
            artIterator.remove();
         }
      }
      return toReturn;
   }

   public void dispose() {
      if (clipboard != null) {
         clipboard.dispose();
      }
   }

   public void setArtifactsToClipboard(AccessPolicy policyHandlerService, List<Artifact> artifactTransferData) throws OseeCoreException {
      if (artifactTransferData == null) {
         throw new IllegalArgumentException("Artifacts can not be null for artifact copy.");
      }
      if (artifactTransferData.isEmpty()) {
         throw new IllegalArgumentException("Artifacts can not be empty.");
      }
      if (clipboard.isDisposed()) {
         this.clipboard = new Clipboard(null);
      }

      List<Artifact> authFailedList = new ArrayList<Artifact>(artifactTransferData);
      List<Artifact> authorizedArtifacts =
         getArtifactsWithPermission(policyHandlerService, PermissionEnum.READ, artifactTransferData);

      authFailedList.removeAll(authorizedArtifacts);

      if (authorizedArtifacts.size() > 0) {
         ArrayList<String> textTransferData = new ArrayList<String>();
         for (Artifact cur : authorizedArtifacts) {
            textTransferData.add(cur.getName());
         }
         Artifact[] artifacts = authorizedArtifacts.toArray(new Artifact[authorizedArtifacts.size()]);
         clipboard.setContents(
            new Object[] {
               new ArtifactData(artifacts, STATUS, viewId),
               HTMLTransferFormatter.getHtml(artifacts),
               Collections.toString(textTransferData, null, ", ", null)},
            new Transfer[] {ArtifactTransfer.getInstance(), HTMLTransfer.getInstance(), TextTransfer.getInstance()});
      }
      if (authFailedList.size() > 0) {
         String failed = Collections.toString(", ", authFailedList) + ".";
         MessageDialog.openError(
            Displays.getActiveShell(),
            "Copy Error",
            "Access control has restricted this action. The following artifacts were not copied to the clipboard: " + failed);
      }

   }

   public void setTextToClipboard(Collection<String> textTransferData) {
      if (textTransferData == null) {
         throw new IllegalArgumentException("Artifacts can not be null for artifact copy.");
      }
      if (textTransferData.isEmpty()) {
         throw new IllegalArgumentException("Artifacts can not be empty.");
      }
      if (clipboard.isDisposed()) {
         this.clipboard = new Clipboard(null);
      }

      clipboard.setContents(new Object[] {Collections.toString(textTransferData, null, ", ", null)},
         new Transfer[] {TextTransfer.getInstance()});
   }

   public boolean isEmpty() {
      boolean theReturn = true;
      if (clipboard.isDisposed()) {
         theReturn = true;
      } else {
         theReturn = clipboard.getContents(ArtifactTransfer.getInstance()) == null;
      }
      return theReturn;
   }

   public List<Artifact> getCopiedContents() {
      Object object = clipboard.getContents(ArtifactTransfer.getInstance());
      List<Artifact> copiedItems;
      if (object instanceof ArtifactData) {
         ArtifactData data = (ArtifactData) object;
         copiedItems = Arrays.asList(data.getArtifacts());
      } else {
         copiedItems = java.util.Collections.emptyList();
      }
      return copiedItems;
   }

}

Back to the top