Skip to main content
summaryrefslogtreecommitdiffstats
blob: 8766d9c6569aa3fc1e0d2c524711d6fd4f63e31a (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
/*******************************************************************************
 * 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.widgets;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import org.eclipse.jface.dialogs.IMessageProvider;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.osee.framework.core.data.IOseeBranch;
import org.eclipse.osee.framework.core.operation.ClientLogger;
import org.eclipse.osee.framework.core.operation.Operations;
import org.eclipse.osee.framework.jdk.core.util.Collections;
import org.eclipse.osee.framework.skynet.core.artifact.Artifact;
import org.eclipse.osee.framework.ui.skynet.ArtifactLabelProvider;
import org.eclipse.osee.framework.ui.skynet.artifact.ArtifactTransfer;
import org.eclipse.osee.framework.ui.skynet.blam.operation.StringGuidsToArtifactListOperation;
import org.eclipse.osee.framework.ui.skynet.branch.BranchSelectionDialog;
import org.eclipse.osee.framework.ui.skynet.internal.Activator;
import org.eclipse.osee.framework.ui.skynet.util.SkynetDragAndDrop;
import org.eclipse.osee.framework.ui.swt.Displays;
import org.eclipse.swt.SWT;
import org.eclipse.swt.dnd.Clipboard;
import org.eclipse.swt.dnd.DND;
import org.eclipse.swt.dnd.DropTargetEvent;
import org.eclipse.swt.dnd.TextTransfer;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;

/**
 * @author Ryan D. Brooks
 */
public class XListDropViewer extends XListViewer implements IXWidgetInputAddable {
   private MenuItem removeFromMenuItem;
   private TableViewer myTableViewer;
   private ArrayContentProvider myArrayContentProvider = null;
   private ArtifactLabelProvider myArtifactLabelProvider = null;

   private Menu popupMenu;

   protected boolean singleItemMode;

   public XListDropViewer(String displayLabel) {
      super(displayLabel);
      this.myArrayContentProvider = new ArrayContentProvider();
      setContentProvider(this.myArrayContentProvider);
      this.myArtifactLabelProvider = new ArtifactLabelProvider();
      setLabelProvider(this.myArtifactLabelProvider);
   }

   @Override
   protected void createControls(Composite parent, int horizontalSpan) {
      popupMenu = new Menu(parent);
      setMultiSelect(true);
      super.setListMenu(popupMenu);
      super.createControls(parent, horizontalSpan);
      new XDragAndDrop();
      this.myTableViewer = super.getTableViewer();
      createRemoveFromMenuItem(popupMenu);
      myTableViewer.getTable().setMenu(popupMenu);
   }

   private void createRemoveFromMenuItem(final Menu popupMenu) {
      removeFromMenuItem = new MenuItem(popupMenu, SWT.PUSH);
      removeFromMenuItem.setText("Remove");
      removeFromMenuItem.addSelectionListener(new SelectionAdapter() {

         @Override
         public void widgetSelected(SelectionEvent event) {
            IStructuredSelection structuredSelection = (IStructuredSelection) myTableViewer.getSelection();
            Iterator<?> iterator = structuredSelection.iterator();

            Collection<Object> items = getCollectionInput();
            if (items != null && !items.isEmpty()) {
               List<Object> modList = new ArrayList<>();

               while (iterator.hasNext()) {
                  modList.remove(iterator.next());
               }

               Object orginalInput = getInput();
               myArrayContentProvider.inputChanged(myTableViewer, orginalInput, modList);
               setInput(modList);
               notifyXModifiedListeners();
               refresh();
            }
         }
      });

      MenuItem paste = new MenuItem(popupMenu, SWT.NONE);
      paste.setText("Paste GUIDs from Clipboard");
      paste.addSelectionListener(new SelectionAdapter() {

         @Override
         public void widgetSelected(SelectionEvent e) {
            Displays.ensureInDisplayThread(new Runnable() {

               @Override
               public void run() {
                  Clipboard cb = new Clipboard(popupMenu.getDisplay());
                  try {
                     TextTransfer transfer = TextTransfer.getInstance();
                     String data = (String) cb.getContents(transfer);
                     IOseeBranch branch = BranchSelectionDialog.getBranchFromUser();
                     Operations.executeAsJob(new StringGuidsToArtifactListOperation(new ClientLogger(Activator.class),
                        data, branch, XListDropViewer.this), true);
                  } finally {
                     cb.dispose();
                  }
               }
            });

         }
      });
   }

   public List<Artifact> getArtifacts() {
      return Collections.castAll(getData());
   }

   public List<Artifact> getSelectedArtifacts() {
      List<Object> selectedArtifacts = new ArrayList<>();
      Collections.flatten(getSelected(), selectedArtifacts);
      return Collections.castAll(Artifact.class, selectedArtifacts);
   }

   /**
    * Adds artifacts to the viewer's input.
    */
   public void addToInput(Artifact... artifacts) {
      List<Object> objects = new ArrayList<>();
      for (Artifact artifact : artifacts) {
         objects.add(artifact);
      }
      addToInput(objects);
   }

   @Override
   public void addToInput(final Collection<Object> objects) {
      if (!objects.isEmpty()) {
         Displays.ensureInDisplayThread(new Runnable() {
            @Override
            public void run() {
               if (getInput() == null) {
                  setInput(objects);
               } else {
                  add(objects);
                  updateListWidget();
               }
               notifyXModifiedListeners();
            }
         });
      }
   }

   @SuppressWarnings("unchecked")
   @Override
   public Collection<Object> getData() {
      return (Collection<Object>) getInput();
   }

   private class XDragAndDrop extends SkynetDragAndDrop {

      public XDragAndDrop() {
         super(null, getControl(), "viewId");
      }

      @Override
      public void performDragOver(DropTargetEvent event) {
         if (ArtifactTransfer.getInstance().isSupportedType(event.currentDataType)) {
            event.detail = DND.DROP_COPY;
         }
      }

      @Override
      public Artifact[] getArtifacts() {
         return null;
      }

      @Override
      public void performArtifactDrop(Artifact[] dropArtifacts) {
         addToInput(dropArtifacts);
         if (XListDropViewer.this.singleItemMode && dropArtifacts.length > 1) {
            setMessage(XListDropViewer.class.getSimpleName(), String.format("Only 1 [%s] can be present", getLabel()),
               IMessageProvider.ERROR);
         }
         setSelected(dropArtifacts);
         notifyXModifiedListeners();
         refresh();
      }
   }
}

Back to the top