Skip to main content
summaryrefslogtreecommitdiffstats
blob: c6bb56e7a2f00810908f56805e173c6f23e93557 (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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
/*******************************************************************************
 * 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.skywalker;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.logging.Level;
import org.eclipse.osee.framework.core.data.AttributeTypeId;
import org.eclipse.osee.framework.core.data.AttributeTypeToken;
import org.eclipse.osee.framework.core.data.BranchId;
import org.eclipse.osee.framework.core.data.IArtifactType;
import org.eclipse.osee.framework.core.data.RelationTypeSide;
import org.eclipse.osee.framework.core.enums.RelationSide;
import org.eclipse.osee.framework.core.model.type.RelationType;
import org.eclipse.osee.framework.jdk.core.type.OseeCoreException;
import org.eclipse.osee.framework.jdk.core.util.AXml;
import org.eclipse.osee.framework.jdk.core.util.Strings;
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.artifact.search.ArtifactQuery;
import org.eclipse.osee.framework.skynet.core.attribute.AttributeTypeManager;
import org.eclipse.osee.framework.skynet.core.relation.RelationTypeManager;
import org.eclipse.osee.framework.ui.skynet.internal.Activator;
import org.eclipse.osee.framework.ui.skynet.skywalker.ISkyWalkerOptionsChangeListener.ModType;
import org.eclipse.zest.layouts.LayoutStyles;
import org.eclipse.zest.layouts.algorithms.AbstractLayoutAlgorithm;
import org.eclipse.zest.layouts.algorithms.GridLayoutAlgorithm;
import org.eclipse.zest.layouts.algorithms.RadialLayoutAlgorithm;
import org.eclipse.zest.layouts.algorithms.SpringLayoutAlgorithm;
import org.eclipse.zest.layouts.algorithms.TreeLayoutAlgorithm;
import org.eclipse.zest.layouts.algorithms.VerticalLayoutAlgorithm;

/**
 * @author Donald G. Dunne
 */
public final class SkyWalkerOptions {

   private Artifact artifact;
   private int levels = 1;
   private static Map<AbstractLayoutAlgorithm, String> layouts;
   private AbstractLayoutAlgorithm layout;
   protected AbstractLayoutAlgorithm defaultLayout;
   private Map<IArtifactType, Boolean> artTypes;
   private Map<AttributeTypeToken, Boolean> showAttributes;
   // RelationLinkDescriptor and RelationLinkDescriptorSide
   private Map<Object, Boolean> relTypes;
   private boolean filterEnabled = true;
   private final Set<ISkyWalkerOptionsChangeListener> listeners = new HashSet<>();
   public static final String RADIAL_DOWN_LAYOUT = "Radial - Down";
   public static final String RADIAL_RIGHT_LAYOUT = "Radial - Right";
   public static final String SPRING_LAYOUT = "Spring";
   public static enum LinkName {
      None,
      Link_Name,
      Full_Link_Name,
      Phrasing_A_to_B,
      Phrasing_B_to_A,
      Other_Side_Name
   };
   private LinkName linkName = LinkName.Link_Name;

   public SkyWalkerOptions() {
      loadLayouts();
      layout = defaultLayout;
   }

   public void addSkyWalkerOptionsChangeListener(ISkyWalkerOptionsChangeListener skyWalkerOptionsChangeListener) {
      listeners.add(skyWalkerOptionsChangeListener);
   }

   public String getExtendedName(Artifact artifact) throws OseeCoreException {
      if (getSelectedShowAttributeTypes().isEmpty()) {
         return "";
      } else {
         StringBuffer sb = new StringBuffer();
         for (AttributeTypeId attributeType : getSelectedShowAttributeTypes()) {
            if (artifact.getAttributeCount(attributeType) > 0) {
               sb.append("\n");
               sb.append(artifact.getAttributesToString(attributeType));
            }
         }
         return sb.toString();
      }
   }

   private void loadArtTypes() {
      if (artTypes == null) {
         artTypes = new HashMap<>();
         try {
            for (IArtifactType descriptor : ArtifactTypeManager.getValidArtifactTypes(artifact.getBranch())) {
               artTypes.put(descriptor, true);
            }
         } catch (Exception ex) {
            OseeLog.log(Activator.class, Level.SEVERE, ex);
         }
      }
   }

   private void loadAttributeTypes() {
      if (showAttributes == null) {
         showAttributes = new HashMap<>();
         try {
            for (AttributeTypeToken descriptor : AttributeTypeManager.getValidAttributeTypes(artifact.getBranch())) {
               showAttributes.put(descriptor, false);
            }
         } catch (OseeCoreException ex) {
            OseeLog.log(Activator.class, Level.SEVERE, ex);
         }
      }
   }

   private void loadRelTypes() {
      if (relTypes == null) {
         relTypes = new HashMap<>();
         try {
            for (RelationType relationType : RelationTypeManager.getValidTypes(artifact.getBranch())) {
               relTypes.put(relationType, true);
               relTypes.put(new RelationTypeSide(relationType, RelationSide.SIDE_A), true);
               relTypes.put(new RelationTypeSide(relationType, RelationSide.SIDE_B), true);
            }
         } catch (Exception ex) {
            OseeLog.log(Activator.class, Level.SEVERE, ex);
         }
      }
   }

   public String toXml() throws OseeCoreException {
      StringBuffer sb = new StringBuffer();
      sb.append(AXml.addTagData("guid", artifact.getGuid()));
      sb.append(AXml.addTagData("branchUuid", artifact.getBranchId() + ""));
      sb.append(AXml.addTagData("artTypes",
         org.eclipse.osee.framework.jdk.core.util.Collections.toString(",", getSelectedArtTypes())));
      sb.append(AXml.addTagData("relTypes",
         org.eclipse.osee.framework.jdk.core.util.Collections.toString(",", getSelectedRelTypes())));
      sb.append(AXml.addTagData("showAttributes",
         org.eclipse.osee.framework.jdk.core.util.Collections.toString(",", getSelectedShowAttributeTypes())));
      sb.append(AXml.addTagData("layout", getLayoutName(getLayout())));
      sb.append(AXml.addTagData("levels", getLevels() + ""));
      sb.append(AXml.addTagData("linkName", getLinkName() + ""));
      return sb.toString();
   }

   public void fromXml(String xml) {
      try {
         String guid = AXml.getTagData(xml, "guid");
         if (Strings.isValid(guid)) {
            String branchUuid = AXml.getTagData(xml, "branchUuid");
            BranchId branch = BranchId.valueOf(branchUuid);
            Artifact art = ArtifactQuery.getArtifactFromId(guid, branch);
            if (art != null) {
               setArtifact(art);
            }
         }
      } catch (Exception ex) {
         OseeLog.log(Activator.class, Level.WARNING, "SkyWalker couldn't find stored artifact via guid", ex);
      }
      String artTypeStr = AXml.getTagData(xml, "artTypes");
      if (Strings.isValid(artTypeStr)) {
         for (Entry<IArtifactType, Boolean> desc : artTypes.entrySet()) {
            desc.setValue(false);
         }
         for (String name : artTypeStr.split(",")) {
            for (Entry<IArtifactType, Boolean> desc : artTypes.entrySet()) {
               if (desc.getKey().getName().equals(name)) {
                  desc.setValue(true);
                  break;
               }
            }
         }
      }
      String relTypeStr = AXml.getTagData(xml, "relTypes");
      if (Strings.isValid(relTypeStr)) {
         for (Entry<Object, Boolean> desc : relTypes.entrySet()) {
            desc.setValue(false);
         }
         for (String name : relTypeStr.split(",")) {
            for (Entry<Object, Boolean> desc : relTypes.entrySet()) {
               if (desc.getKey().toString().equals(name)) {
                  desc.setValue(true);
                  break;
               }
            }
         }
      }
      String showAttrString = AXml.getTagData(xml, "showAttributes");
      if (Strings.isValid(showAttrString)) {
         for (Entry<AttributeTypeToken, Boolean> desc : showAttributes.entrySet()) {
            desc.setValue(false);
         }
         for (String name : showAttrString.split(",")) {
            for (Entry<AttributeTypeToken, Boolean> desc : showAttributes.entrySet()) {
               if (desc.getKey().getName().equals(name)) {
                  desc.setValue(true);
                  break;
               }
            }
         }
      }
      String layoutStr = AXml.getTagData(xml, "layout");
      if (Strings.isValid(layoutStr)) {
         for (AbstractLayoutAlgorithm layout : getLayouts()) {
            if (getLayoutName(layout).equals(layoutStr)) {
               setLayout(layout);
               break;
            }
         }
      }
      String levelStr = AXml.getTagData(xml, "levels");
      if (Strings.isValid(levelStr)) {
         setLevels(Integer.parseInt(levelStr));
      }

      String linkNameStr = AXml.getTagData(xml, "linkName");
      if (Strings.isValid(linkNameStr)) {
         setLinkName(LinkName.valueOf(linkNameStr));
      }

      notifyListeners(ModType.ArtType, ModType.RelType, ModType.Level, ModType.Layout, ModType.Link_Name,
         ModType.Show_Attribute);
   }

   /**
    * @return the artifact
    */
   public Artifact getArtifact() {
      return artifact;
   }

   private Map<AbstractLayoutAlgorithm, String> loadLayouts() {
      if (layouts == null) {
         layouts = new HashMap<>();

         RadialLayoutAlgorithm radLayout = new RadialLayoutAlgorithm(LayoutStyles.NO_LAYOUT_NODE_RESIZING);
         radLayout.setRangeToLayout(-90 * Math.PI / 360, 90 * Math.PI / 360);
         defaultLayout = radLayout;
         layouts.put(radLayout, "Radial - Right (default)");

         radLayout = new RadialLayoutAlgorithm(LayoutStyles.NO_LAYOUT_NODE_RESIZING);
         radLayout.setRangeToLayout(0, 180 * Math.PI / 360);
         layouts.put(radLayout, RADIAL_DOWN_LAYOUT);

         layouts.put(new RadialLayoutAlgorithm(LayoutStyles.NO_LAYOUT_NODE_RESIZING), "Radial - Full");
         layouts.put(new SpringLayoutAlgorithm(), SPRING_LAYOUT);
         layouts.put(new TreeLayoutAlgorithm(LayoutStyles.NO_LAYOUT_NODE_RESIZING), "Tree");
         layouts.put(new VerticalLayoutAlgorithm(LayoutStyles.NO_LAYOUT_NODE_RESIZING), "Vertical");
         layouts.put(new GridLayoutAlgorithm(LayoutStyles.NO_LAYOUT_NODE_RESIZING), "Grid");
      }
      return layouts;
   }

   /**
    * @return the defaultLayout
    */
   public AbstractLayoutAlgorithm getLayout() {
      return layout;
   }

   public AbstractLayoutAlgorithm getLayout(String layoutName) {
      for (Entry<AbstractLayoutAlgorithm, String> entry : layouts.entrySet()) {
         if (entry.getValue().equals(layoutName)) {
            return entry.getKey();
         }
      }
      return defaultLayout;
   }

   public Set<AbstractLayoutAlgorithm> getLayouts() {
      return layouts.keySet();
   }

   public String getLayoutName(AbstractLayoutAlgorithm layout) {
      return layouts.get(layout);
   }

   /**
    * @param artifact the artifact to set
    */
   public void setArtifact(Artifact artifact) {
      if (artifact.equals(this.artifact)) {
         return;
      }
      this.artifact = artifact;
      loadArtTypes();
      loadRelTypes();
      loadAttributeTypes();
      notifyListeners(ModType.Artifact);
   }

   public boolean isValidArtifactType(IArtifactType type) {
      if (!isFilterEnabled()) {
         return true;
      }
      return getSelectedArtTypes().contains(type);
   }

   public boolean isValidRelationType(RelationType type) {
      if (!isFilterEnabled()) {
         return true;
      }
      return getSelectedRelTypes().contains(type);
   }

   public boolean isValidRelationLinkDescriptorSide(RelationTypeSide side) {
      if (!isFilterEnabled()) {
         return true;
      }
      return getSelectedRelTypes().contains(side);
   }

   /**
    * @return the levels
    */
   public int getLevels() {
      return levels;
   }

   /**
    * @param levels the levels to set
    */
   public void setLevels(int levels) {
      if (this.levels == levels) {
         return;
      }
      this.levels = levels;
      notifyListeners(ModType.Level);
   }

   /**
    * @return the filterByArtType
    */
   public boolean isFilterEnabled() {
      return filterEnabled;
   }

   /**
    * @param filterByArtType the filterByArtType to set
    */
   public void setFilterEnabled(boolean enable) {
      if (this.filterEnabled == enable) {
         return;
      }
      this.filterEnabled = enable;
      notifyListeners(ModType.FilterEnabled);
   }

   private void notifyListeners(ModType... modType) {
      for (ISkyWalkerOptionsChangeListener listener : listeners) {
         listener.modified(modType);
      }
   }

   public void setSelectedRelTypes(Object[] selected) {
      if (relTypes == null) {
         loadRelTypes();
      }
      List<Object> selList = new ArrayList<>();
      for (Object obj : selected) {
         selList.add(obj);
      }
      for (Entry<Object, Boolean> entry : relTypes.entrySet()) {
         entry.setValue(selList.contains(entry.getKey()));
      }
      notifyListeners(ModType.RelType);
   }

   public void setSelectedShowAttributes(Object[] selected) {
      List<Object> selList = new ArrayList<>();
      for (Object obj : selected) {
         selList.add(obj);
      }
      for (Entry<AttributeTypeToken, Boolean> entry : showAttributes.entrySet()) {
         entry.setValue(selList.contains(entry.getKey()));
      }
      notifyListeners(ModType.Show_Attribute);
   }

   public void setSelectedArtTypes(Collection<IArtifactType> selected) {
      if (artTypes == null) {
         loadArtTypes();
      }
      for (Entry<IArtifactType, Boolean> entry : artTypes.entrySet()) {
         entry.setValue(selected.contains(entry.getKey()));
      }
      notifyListeners(ModType.ArtType);
   }

   public Set<IArtifactType> getSelectedArtTypes() {
      Set<IArtifactType> selected = new HashSet<>();
      if (artTypes == null) {
         return selected;
      }
      for (IArtifactType desc : artTypes.keySet()) {
         if (artTypes.get(desc)) {
            selected.add(desc);
         }
      }
      return selected;
   }

   public Set<Object> getSelectedRelTypes() {
      Set<Object> selected = new HashSet<>();
      if (relTypes == null) {
         return selected;
      }
      for (Object desc : relTypes.keySet()) {
         if (relTypes.get(desc)) {
            selected.add(desc);
         }
      }
      return selected;
   }

   public Set<AttributeTypeToken> getSelectedShowAttributeTypes() {
      Set<AttributeTypeToken> selected = new HashSet<>();
      if (showAttributes == null) {
         return selected;
      }
      for (AttributeTypeToken desc : showAttributes.keySet()) {
         if (showAttributes.get(desc)) {
            selected.add(desc);
         }
      }
      return selected;
   }

   public Set<IArtifactType> getAllArtTypes() {
      if (artTypes == null) {
         return new HashSet<IArtifactType>();
      }
      return artTypes.keySet();
   }

   public Set<Object> getAllRelTypes() {
      if (relTypes == null) {
         return new HashSet<Object>();
      }
      return relTypes.keySet();
   }

   public Set<AttributeTypeToken> getAllShowAttributes() {
      if (showAttributes == null) {
         return new HashSet<AttributeTypeToken>();
      }
      return showAttributes.keySet();
   }

   public Set<RelationType> getAllRelationLinkDescriptorTypes() {
      if (relTypes == null) {
         return new HashSet<RelationType>();
      }
      Set<RelationType> descs = new HashSet<>();
      for (Object obj : relTypes.keySet()) {
         if (obj instanceof RelationType) {
            descs.add((RelationType) obj);
         }
      }
      return descs;
   }

   /**
    * @param layout the layout to set
    */
   public void setLayout(AbstractLayoutAlgorithm layout) {
      if (this.layout == layout) {
         return;
      }
      this.layout = layout;
      notifyListeners(ModType.Layout);
   }

   /**
    * @return the linkName
    */
   public LinkName getLinkName() {
      return linkName;
   }

   /**
    * @param linkName the linkName to set
    */
   public void setLinkName(LinkName linkName) {
      if (this.linkName == linkName) {
         return;
      }
      this.linkName = linkName;
      notifyListeners(ModType.Link_Name);
   }

   /**
    * @return the defaultLayout
    */
   public AbstractLayoutAlgorithm getDefaultLayout() {
      return defaultLayout;
   }

   /**
    * @param defaultLayout the defaultLayout to set
    */
   public void setDefaultLayout(AbstractLayoutAlgorithm defaultLayout) {
      this.defaultLayout = defaultLayout;
   }

}

Back to the top