Skip to main content
summaryrefslogtreecommitdiffstats
blob: 2260f1a47bca4c85b4d8b971d60c4169d3d8401d (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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/*******************************************************************************
 * 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.relation.explorer;

import java.util.ArrayList;
import org.eclipse.jface.viewers.StructuredViewer;
import org.eclipse.osee.framework.core.data.IArtifactType;
import org.eclipse.osee.framework.core.data.BranchId;
import org.eclipse.osee.framework.core.enums.CoreArtifactTypes;
import org.eclipse.osee.framework.core.enums.CoreAttributeTypes;
import org.eclipse.osee.framework.jdk.core.type.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.ArtifactTypeManager;
import org.eclipse.osee.framework.skynet.core.relation.RelationTypeSideSorter;
import org.eclipse.osee.framework.ui.skynet.ArtifactImageManager;
import org.eclipse.osee.framework.ui.skynet.internal.Activator;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.ControlListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;

/**
 * @author Michael S. Rodgers
 */
public class RelationExplorerWindow {

   private RelationTableViewer relationTableViewer;
   private final RelationTypeSideSorter relationGroup;
   private final boolean persistOnOk;
   private boolean cancelled = false;

   // Private arrays for valid drops
   private final ArrayList<Artifact> validArtifacts;
   private final ArrayList<String> urls;
   private final ArrayList<String> names;
   private BranchId branch;

   // Private arrays for invalid drops
   private final ArrayList<String> invalidName;
   private final ArrayList<String> invalidReason;
   private final ArrayList<Artifact> invalidArtifacts;

   private boolean needWindow;

   private Shell shell;

   private IArtifactType descriptor = null;

   private final StructuredViewer viewer;

   public static final int ADD_NUM = 0;
   public static final int ARTIFACT_NAME_NUM = 1;
   public static final int ARTIFACT_TYPE_NUM = 2;
   public static final int RATIONALE_NUM = 3;

   public static final int NAME_NUM = 0;
   public static final int REASON_NUM = 1;

   public RelationExplorerWindow(StructuredViewer viewer, RelationTypeSideSorter group, boolean persistOnOk) {
      this.validArtifacts = new ArrayList<>();
      this.invalidArtifacts = new ArrayList<>();

      this.urls = new ArrayList<>();
      this.names = new ArrayList<>();

      this.invalidName = new ArrayList<>();
      this.invalidReason = new ArrayList<>();

      this.viewer = viewer;
      this.relationGroup = group;
      this.persistOnOk = persistOnOk;
      this.needWindow = false;

   }

   public RelationExplorerWindow(StructuredViewer viewer, RelationTypeSideSorter group) {
      this(viewer, group, false);
   }

   public void addValid(Artifact artifact) {
      if (artifact == null) {
         needWindow = true;
      } else {
         this.branch = artifact.getBranch();
         this.validArtifacts.add(artifact);
         this.names.add(artifact.getName());
      }
   }

   public void addInvalid(String name, String reason) {
      invalidName.add(name);
      invalidReason.add(reason);
      needWindow = true;
   }

   public void createArtifactInformationBox() {
      drawWindow();
   }

   private void drawWindow() {
      shell = new Shell(SWT.ON_TOP | SWT.APPLICATION_MODAL | SWT.TITLE | SWT.MIN | SWT.MAX | SWT.RESIZE);

      // Setup Title
      shell.setText("Artifact Information");

      // Setup Icon
      Image image = ArtifactImageManager.getImage(CoreArtifactTypes.Artifact);
      shell.setImage(image);

      // Setup Form Layout
      FormLayout layout = new FormLayout();
      layout.marginHeight = 5;
      layout.marginWidth = 5;
      shell.setLayout(layout);

      SashForm sashForm = new SashForm(shell, SWT.VERTICAL);

      // Create valid artifact fields
      FormLayout validLayout = new FormLayout();
      validLayout.spacing = 5;

      Composite validComposite = new Composite(sashForm, SWT.NONE);
      validComposite.setLayout(validLayout);

      Label validLabel = new Label(validComposite, SWT.LEFT);
      validLabel.setText("Valid artifacts - will be added");

      Table validTable = new Table(validComposite,
         SWT.MULTI | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.HIDE_SELECTION);
      validTable.setLinesVisible(true);
      validTable.setHeaderVisible(true);

      // Create invalid artifacts fields
      FormLayout invalidLayout = new FormLayout();
      invalidLayout.spacing = 5;

      Composite invalidComposite = new Composite(sashForm, SWT.NONE);
      invalidComposite.setLayout(invalidLayout);

      Label invalidLabel = new Label(invalidComposite, SWT.LEFT);
      invalidLabel.setText("Invalid artifacts - will not be added");

      Table invalidTable = new Table(invalidComposite,
         SWT.MULTI | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.HIDE_SELECTION);
      invalidTable.setLinesVisible(true);
      invalidTable.setHeaderVisible(true);

      // Create the Buttons
      Button okButton = new Button(invalidComposite, SWT.PUSH);
      okButton.setText("OK");

      Button cancelButton = new Button(invalidComposite, SWT.PUSH);
      cancelButton.setText("Cancel");

      // Attach validLabel to top-left corner
      FormData data = new FormData();
      data.top = new FormAttachment(0);
      data.left = new FormAttachment(0);
      validLabel.setLayoutData(data);

      // Attach validTable to bottom of validLabel
      data = new FormData();
      data.top = new FormAttachment(validLabel);
      data.bottom = new FormAttachment(100);
      data.left = new FormAttachment(0);
      data.right = new FormAttachment(100);
      data.height = validTable.getItemHeight() * 10;
      validTable.setLayoutData(data);

      // Attach invalidLabel to top-left corner
      data = new FormData();
      data.top = new FormAttachment(0);
      data.left = new FormAttachment(0);
      invalidLabel.setLayoutData(data);

      // Attach invalidTable to bottom of invalidLabel
      data = new FormData();
      data.top = new FormAttachment(invalidLabel);
      data.bottom = new FormAttachment(okButton);
      data.left = new FormAttachment(0);
      data.right = new FormAttachment(100);
      data.height = validTable.getItemHeight() * 10;
      invalidTable.setLayoutData(data);

      // Attach sashForm to top-left corner of shell
      data = new FormData();
      data.top = new FormAttachment(0);
      data.bottom = new FormAttachment(100);
      data.left = new FormAttachment(0);
      data.right = new FormAttachment(100);
      sashForm.setLayoutData(data);

      // Attach buttons to bottom of sashForm
      data = new FormData();
      data.bottom = new FormAttachment(100);
      data.right = new FormAttachment(100);
      cancelButton.setLayoutData(data);

      data = new FormData();
      data.bottom = new FormAttachment(100);
      data.right = new FormAttachment(cancelButton);
      okButton.setLayoutData(data);

      // Populate Tables
      relationTableViewer = new RelationTableViewer(validTable, invalidTable, branch);
      for (int i = 0; i < validArtifacts.size(); i++) {
         relationTableViewer.addValidItem(validArtifacts.get(i));
      }
      for (int i = 0; i < invalidName.size(); i++) {
         relationTableViewer.addInvalidItem(invalidName.get(i), invalidReason.get(i));
      }

      // Add Listeners to buttons
      okButton.addSelectionListener(new SelectionAdapter() {
         @Override
         public void widgetSelected(SelectionEvent e) {
            okSelected();
         }

      });

      cancelButton.addSelectionListener(new SelectionAdapter() {
         @Override
         public void widgetSelected(SelectionEvent e) {
            cancelSelected();
         }

      });

      // Add shell resize listener
      shell.addControlListener(new ControlListener() {

         @Override
         public void controlMoved(ControlEvent e) {
            // do nothing
         }

         @Override
         public void controlResized(ControlEvent e) {
            relationTableViewer.resizeTable(((Shell) e.widget).getClientArea().width);
            shell.layout();
         }
      });

      if (needWindow) {
         shell.pack();
         shell.open();
      } else {
         okSelected();
      }
   }

   /**
    * Create the TableViewer
    */

   private void okSelected() {
      ArrayList<ArtifactModel> artifactList = relationTableViewer.getArtifactList().getArtifactModel();

      for (int i = 0; i < artifactList.size(); i++) {
         ArtifactModel model = artifactList.get(i);

         if (model.isAdd()) {
            Artifact artifact = model.getArtifact();
            descriptor = model.getDescriptor();
            if (artifact == null) {
               if (descriptor != null) {
                  try {
                     artifact = ArtifactTypeManager.addArtifact(descriptor, branch);
                     artifact.setName(model.getName());
                     artifact.setSoleAttributeValue(CoreAttributeTypes.ContentUrl,
                        urls.get(names.indexOf(model.getName())));
                     artifact.persist(getClass().getSimpleName());
                  } catch (Exception ex) {
                     OseeLog.log(Activator.class, OseeLevel.SEVERE_POPUP, ex);
                  }
               }
            } else {
               artifact = model.getArtifact();
            }

            if (artifact != null) {
               try {
                  relationGroup.getArtifact().addRelation(relationGroup, artifact);
               } catch (OseeCoreException ex) {
                  OseeLog.log(Activator.class, OseeLevel.SEVERE_POPUP, ex);
               }
            }
         }
      }
      if (persistOnOk) {
         try {
            relationGroup.getArtifact().persist(getClass().getSimpleName());
         } catch (OseeCoreException ex) {
            OseeLog.log(Activator.class, OseeLevel.SEVERE_POPUP, ex);
         }
      }
      shell.dispose();
      viewer.refresh();

   }

   private void cancelSelected() {
      cancelled = true;
      shell.dispose();
   }

   public RelationTypeSideSorter getRelationGroup() {
      return relationGroup;
   }

   public boolean isCancelled() {
      return cancelled;
   }

   public ArrayList<Artifact> getInvalidArtifacts() {
      return invalidArtifacts;
   }

   public void addInvalidArtifact(Artifact invalidArtifact, String errorMessage) {
      invalidArtifacts.add(invalidArtifact);
      addInvalid(invalidArtifact.getName(), errorMessage);
   }
}

Back to the top