Skip to main content
summaryrefslogtreecommitdiffstats
blob: 41c7e363e57e02b580bab9a1959ddbc6aa6c0895 (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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/*******************************************************************************
 * 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;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.logging.Level;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.osee.framework.db.connection.exception.OseeCoreException;
import org.eclipse.osee.framework.logging.OseeLog;
import org.eclipse.osee.framework.skynet.core.artifact.Artifact;
import org.eclipse.osee.framework.skynet.core.artifact.ArtifactPersistenceManager;
import org.eclipse.osee.framework.skynet.core.artifact.ArtifactType;
import org.eclipse.osee.framework.skynet.core.artifact.Branch;
import org.eclipse.osee.framework.skynet.core.artifact.BranchPersistenceManager;
import org.eclipse.osee.framework.skynet.core.attribute.TypeValidityManager;
import org.eclipse.osee.framework.ui.plugin.util.DirectoryOrFileSelector;
import org.eclipse.osee.framework.ui.skynet.SkynetGuiPlugin;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.dialogs.WizardDataTransferPage;

/**
 * @author Ryan D. Brooks
 */
public class ArtifactImportPage extends WizardDataTransferPage {
   public enum ImportMethod {
      EXCEL, GENERAL, WHOLE_WORD, WORD_OUTLINE
   };

   public static final String PAGE_NAME = "osee.define.wizardPage.artifactImportPage";
   private final Artifact destinationArtifact;
   private List typeList;
   private List branchList;

   private Button chkReuseArtifacts;
   private Button radImportUnderDhRoot;
   private Button radImportUnderNamedRootFolder;
   private Button radImportUnderSelection;
   private Text txtImportUnderFolderName;

   private DirectoryOrFileSelector directoryFileSelector;

   private Button radWordOutlineExtractor;
   private Button radExcelExtractor;
   private Button radWholeWordExtractor;
   private Button radGeneralExtractor;

   private File importResource;

   private boolean built;

   /**
    * Constructor used by the Skynet drag-n-drop
    */
   public ArtifactImportPage(File importResource, Artifact reuseRootArtifact) {
      super(PAGE_NAME);
      Assert.isNotNull(importResource, "importResource can not be null");
      Assert.isNotNull(reuseRootArtifact, "reuseRootArtifact can not be null");

      this.importResource = importResource;
      this.destinationArtifact = reuseRootArtifact;
      this.built = false;
   }

   /**
    * Constructor that is used by the Eclipse framework
    * 
    * @param pageName
    * @param selection
    */
   public ArtifactImportPage(IStructuredSelection selection) {
      super(PAGE_NAME);

      this.importResource = null;
      Artifact selectedArtifact = null;
      if (selection != null && selection.size() == 1) {
         Object firstElement = selection.getFirstElement();
         if (firstElement instanceof IAdaptable) {
            Object resource = ((IAdaptable) firstElement).getAdapter(IResource.class);
            if (resource instanceof IResource) {
               importResource = ((IResource) resource).getLocation().toFile();
            }
         } else if (firstElement instanceof Artifact) {
            selectedArtifact = (Artifact) firstElement;
         }
      }

      this.destinationArtifact = selectedArtifact;
      this.built = false;
   }

   /**
    * (non-Javadoc) Method declared on IDialogPage.
    */
   public void createControl(Composite parent) {

      initializeDialogUnits(parent);

      Composite composite = new Composite(parent, SWT.NULL);
      composite.setLayout(new GridLayout(1, false));
      composite.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_FILL | GridData.HORIZONTAL_ALIGN_FILL));
      composite.setFont(parent.getFont());

      createSourceGroup(composite);

      createOptionsGroup(composite);

      built = true;

      restoreWidgetValues();
      updateWidgetEnablements();
      setPageComplete(determinePageCompletion());

      setControl(composite);
   }

   /**
    * The <code>WizardResourceImportPage</code> implementation of this <code>Listener</code> method handles all events
    * and enablements for controls on this page. Subclasses may extend.
    * 
    * @param event Event
    */
   public void handleEvent(Event event) {
      setPageComplete(determinePageCompletion());

      updateWidgetEnablements();
   }

   private void createSourceGroup(Composite parent) {
      createImportSource(parent);
      createImportMethodSelection(parent);
      setPageComplete(determinePageCompletion());
   }

   private void createImportSource(Composite parent) {

      directoryFileSelector = new DirectoryOrFileSelector(parent, SWT.NONE, "Import Source", this);

      if (importResource == null) {
         // Select directory as the default
         directoryFileSelector.setDirectorySelected(true);
      } else {
         directoryFileSelector.setDirectorySelected(!importResource.isFile());
         directoryFileSelector.setText(importResource.getAbsolutePath());
      }

      Group composite = new Group(parent, SWT.NONE);
      GridLayout layout = new GridLayout(2, false);
      composite.setLayout(layout);
      composite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

      radImportUnderDhRoot = new Button(composite, SWT.RADIO);
      radImportUnderDhRoot.setLayoutData(new GridData(SWT.BEGINNING, SWT.BEGINNING, false, false, 2, 1));
      radImportUnderDhRoot.setText("Add to Default Hierarchy Root");
      radImportUnderDhRoot.setToolTipText("All the top level artifacts that are imported " + "will become root level children.");
      radImportUnderDhRoot.addListener(SWT.Selection, this);

      radImportUnderNamedRootFolder = new Button(composite, SWT.RADIO);
      radImportUnderNamedRootFolder.setText("Add to Root Level Folder:");
      radImportUnderNamedRootFolder.setToolTipText("All the top level artifacts that are imported " + "will become children of the named folder at the root level.");
      radImportUnderNamedRootFolder.addListener(SWT.Selection, this);
      txtImportUnderFolderName = new Text(composite, SWT.BORDER);
      txtImportUnderFolderName.setText("Recent Imports");
      txtImportUnderFolderName.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

      radImportUnderSelection = new Button(composite, SWT.RADIO);
      radImportUnderSelection.setLayoutData(new GridData(SWT.BEGINNING, SWT.BEGINNING, false, false, 2, 1));
      radImportUnderSelection.setText("Add to Selected Artifact:" + destinationArtifact);
      radImportUnderSelection.setToolTipText("All the top level artifacts that are imported " + "will become children of the selected artifact.");
      radImportUnderSelection.addListener(SWT.Selection, this);
      radImportUnderSelection.addSelectionListener(new SelectionAdapter() {
         public void widgetSelected(SelectionEvent e) {
            if (radImportUnderSelection.getSelection()) {
               Branch branch = destinationArtifact.getBranch();
               branchList.setSelection(new String[] {branch.getBranchName()});
               branchList.showSelection();
               populateTypeList(branch);
            }
         }
      });

      chkReuseArtifacts = new Button(composite, SWT.CHECK);
      chkReuseArtifacts.addListener(SWT.Selection, this);
      chkReuseArtifacts.setText("Re-use Artifacts");
      chkReuseArtifacts.setEnabled(true);
      chkReuseArtifacts.setToolTipText("All imported artifacts will be checked against the root\n" + "import artifact and the content will be placed on the artifact\n" + "that has the same identifying attributes and level from the root");

      chkReuseArtifacts.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, true, false, 2, 1));

      // Setup initial selections
      chkReuseArtifacts.setSelection(false);
      if (destinationArtifact == null) {
         radImportUnderDhRoot.setSelection(true);
      } else {
         radImportUnderSelection.setSelection(true);
      }
   }

   /*
    * @see WizardPage#becomesVisible
    */
   public void setVisible(boolean visible) {
      super.setVisible(visible);
      // policy: wizards are not allowed to come up with an error message
      if (visible) {
         setErrorMessage(null);
      }
   }

   @Override
   protected boolean validateSourceGroup() {
      return directoryFileSelector.validate(this);
   }

   protected void createOptionsGroup(Composite parent) {
      Group composite = new Group(parent, SWT.NONE);
      composite.setText("Options");
      composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
      composite.setLayout(new GridLayout(2, false));

      Label label = new Label(composite, SWT.NONE);
      label.setText("Branch:");
      label.setLayoutData(new GridData(SWT.BEGINNING, SWT.BEGINNING, false, false));

      label = new Label(composite, SWT.NONE);
      label.setText("Artifact Type:");
      label.setLayoutData(new GridData(SWT.BEGINNING, SWT.BEGINNING, false, false));

      branchList = new List(composite, SWT.BORDER | SWT.V_SCROLL);
      GridData gridData = new GridData(GridData.FILL_BOTH);
      gridData.heightHint = 300;
      branchList.setLayoutData(gridData);

      typeList = new List(composite, SWT.BORDER | SWT.V_SCROLL);
      gridData = new GridData(GridData.FILL_BOTH);
      gridData.heightHint = 300;
      typeList.setLayoutData(gridData);

      Branch defaultBranch;
      if (destinationArtifact == null) {
         defaultBranch = BranchPersistenceManager.getDefaultBranch();
      } else {
         defaultBranch = destinationArtifact.getBranch();
      }

      try {
         int defaultBranchIndex = 0;
         for (Branch branch : BranchPersistenceManager.getBranches()) {
            branchList.add(branch.getBranchName());
            branchList.setData(branch.getBranchName(), branch);
            if (branch.equals(defaultBranch)) {
               branchList.select(defaultBranchIndex);
            } else {
               defaultBranchIndex++;
            }
         }
      } catch (OseeCoreException ex) {
         OseeLog.log(SkynetGuiPlugin.class, Level.SEVERE, ex);
      }

      populateTypeList(defaultBranch);
      // Start out with an item selected
      typeList.setSelection(0);

      branchList.addSelectionListener(new SelectionAdapter() {
         public void widgetSelected(SelectionEvent event) {
            String itemName = branchList.getItem(branchList.getSelectionIndex());
            populateTypeList((Branch) branchList.getData(itemName));
         }
      });
   }

   private void populateTypeList(Branch branch) {
      try {
         String[] selection = typeList.getSelection();
         typeList.removeAll();
         ArrayList<ArtifactType> validArtifactTypes =
               new ArrayList<ArtifactType>(TypeValidityManager.getValidArtifactTypes(branch));
         Collections.sort(validArtifactTypes);
         for (ArtifactType descriptor : validArtifactTypes) {
            typeList.add(descriptor.getName());
            typeList.setData(descriptor.getName(), descriptor);
         }
         typeList.setSelection(selection);
         // If the item wasn't available in the new list
         if (typeList.getSelectionCount() == 0) {
            typeList.select(0);
         }
         typeList.showSelection();
      } catch (OseeCoreException ex) {
         OseeLog.log(SkynetGuiPlugin.class, Level.SEVERE, ex);
         typeList.add("error loading artifact types");
      }
   }

   private void createImportMethodSelection(Composite parent) {
      Group composite = new Group(parent, SWT.NONE);
      composite.setText("Import Method");
      composite.setToolTipText("Select the method to be used for importing the selected file or directory");
      GridLayout layout = new GridLayout(2, true);
      composite.setLayout(layout);
      composite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

      radWordOutlineExtractor = new Button(composite, SWT.RADIO);
      radWordOutlineExtractor.setText("Word Outline");
      radWordOutlineExtractor.setToolTipText(WordOutlineExtractor.getDescription());
      radWordOutlineExtractor.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, true, false, 1, 1));
      radWordOutlineExtractor.addListener(SWT.Selection, this);

      radWholeWordExtractor = new Button(composite, SWT.RADIO);
      radWholeWordExtractor.setText("Whole Word Document");
      radWholeWordExtractor.setToolTipText(WholeWordDocumentExtractor.getDescription());
      radWholeWordExtractor.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, true, false, 1, 1));
      radWholeWordExtractor.addListener(SWT.Selection, this);

      radExcelExtractor = new Button(composite, SWT.RADIO);
      radExcelExtractor.setText("Excel XML Artifacts");
      radExcelExtractor.setToolTipText(ExcelArtifactExtractor.getDescription());
      radExcelExtractor.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, true, false, 1, 1));
      radExcelExtractor.addListener(SWT.Selection, this);

      radGeneralExtractor = new Button(composite, SWT.RADIO);
      radGeneralExtractor.setText("General Documents (All Formats)");
      radGeneralExtractor.setToolTipText(NativeDocumentExtractor.getDescription());
      radGeneralExtractor.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, true, false, 1, 1));
      radGeneralExtractor.addListener(SWT.Selection, this);

      radWordOutlineExtractor.setSelection(true);
   }

   /*
    * (non-Javadoc)
    * 
    * @see org.eclipse.ui.dialogs.WizardResourceImportPage#allowNewContainerName()
    */
   @Override
   protected boolean allowNewContainerName() {
      return true;
   }

   public File getImportFile() {
      return directoryFileSelector.getFile();
   }

   public boolean isExcelExtractor() {
      return radExcelExtractor.getSelection();
   }

   public boolean isWholeWordExtractor() {
      return radWholeWordExtractor.getSelection();
   }

   public boolean isGeneralDocumentExtractor() {
      return radGeneralExtractor.getSelection();
   }

   public boolean isWordOutlineExtractor() {
      return radWordOutlineExtractor.getSelection();
   }

   public ArtifactType getSelectedType() {
      String itemName = typeList.getItem(typeList.getSelectionIndex());
      return (ArtifactType) typeList.getData(itemName);
   }

   public Branch getSelectedBranch() {
      if (chkReuseArtifacts.getSelection()) {
         return destinationArtifact.getBranch();
      } else {
         String itemName = branchList.getItem(branchList.getSelectionIndex());
         return (Branch) branchList.getData(itemName);
      }
   }

   public Artifact getImportRoot() throws OseeCoreException {
      Artifact importRoot;

      if (radImportUnderDhRoot.getSelection()) {
         importRoot = ArtifactPersistenceManager.getDefaultHierarchyRootArtifact(getSelectedBranch());
      } else if (radImportUnderNamedRootFolder.getSelection()) {
         importRoot =
               ArtifactPersistenceManager.getDefaultHierarchyRootArtifact(getSelectedBranch()).getChild(
                     txtImportUnderFolderName.getText());
      } else if (radImportUnderSelection.getSelection()) {
         importRoot = destinationArtifact;
      } else {
         throw new IllegalStateException("One of the 3 radio buttons should be selected.");
      }

      return importRoot;
   }

   public Artifact getReuseArtifactRoot() {
      if (chkReuseArtifacts.getSelection()) {
         return destinationArtifact;
      }
      return null;
   }

   /*
    * The page may not be init'd here
    */
   @Override
   protected void updateWidgetEnablements() {
      super.updateWidgetEnablements();

      if (built) {
         branchList.setEnabled(!radImportUnderSelection.getSelection());

         txtImportUnderFolderName.setEnabled(false);// TODO future development
         radImportUnderSelection.setEnabled(destinationArtifact != null);

         typeList.setEnabled(radGeneralExtractor.getSelection() || radWordOutlineExtractor.getSelection() || radWholeWordExtractor.getSelection());
      }

   }
}

Back to the top