Skip to main content
summaryrefslogtreecommitdiffstats
blob: 780d16b41a5d8172e5573ab0397fe811bcce50b6 (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
/*******************************************************************************
 * 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 java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.logging.Level;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.jface.viewers.StructuredViewer;
import org.eclipse.jface.window.Window;
import org.eclipse.osee.framework.access.AccessControlManager;
import org.eclipse.osee.framework.core.data.IAttributeType;
import org.eclipse.osee.framework.core.data.BranchId;
import org.eclipse.osee.framework.jdk.core.type.OseeCoreException;
import org.eclipse.osee.framework.jdk.core.util.Collections;
import org.eclipse.osee.framework.jdk.core.util.Conditions;
import org.eclipse.osee.framework.jdk.core.util.Strings;
import org.eclipse.osee.framework.logging.OseeLevel;
import org.eclipse.osee.framework.logging.OseeLog;
import org.eclipse.osee.framework.plugin.core.util.Jobs;
import org.eclipse.osee.framework.skynet.core.UserManager;
import org.eclipse.osee.framework.skynet.core.artifact.Artifact;
import org.eclipse.osee.framework.skynet.core.artifact.IBranchProvider;
import org.eclipse.osee.framework.skynet.core.attribute.AttributeTypeManager;
import org.eclipse.osee.framework.ui.skynet.internal.Activator;
import org.eclipse.osee.framework.ui.skynet.widgets.dialog.FilteredCheckboxAttributeTypeDialog;
import org.eclipse.osee.framework.ui.swt.ImageManager;
import org.eclipse.osee.framework.ui.swt.KeyedImage;
import org.eclipse.ui.progress.UIJob;

/**
 * @author Roberto E. Escobar
 */
public class ArtifactDecorator implements IArtifactDecoratorPreferences {

   private static final Collection<WeakReference<ArtifactDecorator>> DECORATOR_INSTANCES =
      new CopyOnWriteArrayList<WeakReference<ArtifactDecorator>>();

   private DecoratorAction showArtIds;
   private DecoratorAction showArtType;
   private DecoratorAction showArtVersion;
   private DecoratorAction showArtBranch;
   private DecoratorAction showRelations;
   private ShowAttributeAction attributesAction;
   private SetSettingsAsDefault saveSettingsAction;
   private StructuredViewer viewer;

   private final String storageKey;

   public ArtifactDecorator(String storageKey) {
      this.viewer = null;
      this.storageKey = storageKey;
      addDecoratorInstance(this);
   }

   public void setViewer(StructuredViewer viewer) {
      this.viewer = viewer;
   }

   private String asKey(String prefix, String name) {
      return String.format("%s.%s", prefix, name);
   }

   private void storeState() {
      try {
         saveAction(showArtIds, "artifact.decorator.show.artId");
         saveAction(showArtType, "artifact.decorator.show.artType");
         saveAction(showArtBranch, "artifact.decorator.show.artBranch");
         saveAction(showArtVersion, "artifact.decorator.show.artVersion");
         saveAction(showRelations, "artifact.decorator.show.relations");
         if (attributesAction != null) {
            Collection<IAttributeType> items = attributesAction.getSelected();
            saveSetting("artifact.decorator.attrTypes", Collections.toString(",", items));
            saveAction(attributesAction, "artifact.decorator.show.attrTypes");
         }
      } catch (OseeCoreException ex) {
         OseeLog.log(Activator.class, Level.SEVERE, ex);
      }
   }

   private void loadState() {
      try {
         loadAction(showArtIds, "artifact.decorator.show.artId");
         loadAction(showArtType, "artifact.decorator.show.artType");
         loadAction(showArtBranch, "artifact.decorator.show.artBranch");
         loadAction(showArtVersion, "artifact.decorator.show.artVersion");
         loadAction(showRelations, "artifact.decorator.show.relations");
         if (attributesAction != null) {
            String value = getSetting("artifact.decorator.attrTypes");
            if (Strings.isValid(value)) {
               String[] entries = value.split(",");
               attributesAction.setSelected(Arrays.asList(entries));
            }
            loadAction(attributesAction, "artifact.decorator.show.attrTypes");
         }
      } catch (OseeCoreException ex) {
         OseeLog.log(Activator.class, Level.SEVERE, ex);
      }
   }

   private void loadAction(Action action, String key) throws OseeCoreException {
      if (action != null) {
         String setting = getSetting(key);
         if (Strings.isValid(setting)) {
            boolean isChecked = Boolean.parseBoolean(setting);
            action.setChecked(isChecked);
         }
      }
   }

   private void saveAction(Action action, String key) throws OseeCoreException {
      boolean isChecked = action != null && action.isChecked();
      saveSetting(key, String.valueOf(isChecked));
   }

   private void saveSetting(String key, String value) throws OseeCoreException {
      UserManager.setSetting(asKey(storageKey, key), value);
   }

   private String getSetting(String key) throws OseeCoreException {
      return UserManager.getSetting(asKey(storageKey, key));
   }

   private void checkActionsCreated(IBranchProvider branchProvider) {
      if (showArtType == null) {
         showArtType = new DecoratorAction("Artifact Type", FrameworkImage.FILTERS, false);
      }
      if (showArtBranch == null) {
         showArtBranch = new DecoratorAction("Artifact Branch", FrameworkImage.FILTERS, false);
      }
      if (showArtVersion == null) {
         showArtVersion = new DecoratorAction("Artifact Version", FrameworkImage.FILTERS, false);
      }

      if (showRelations == null) {
         showRelations = new DecoratorAction("Relations", FrameworkImage.FILTERS, false);
      }

      if (attributesAction == null && branchProvider != null) {
         attributesAction = new ShowAttributeAction(branchProvider, FrameworkImage.FILTERS);
      }

      if (showArtIds == null && isAdmin()) {
         showArtIds = new DecoratorAction("Artifact Ids", FrameworkImage.FILTERS, false);
      }
      if (saveSettingsAction == null) {
         saveSettingsAction = new SetSettingsAsDefault();
      }
   }

   private boolean isAdmin() {
      boolean result = false;
      try {
         if (AccessControlManager.isOseeAdmin()) {
            result = true;
         }
      } catch (Exception ex) {
         OseeLog.log(Activator.class, OseeLevel.SEVERE_POPUP, ex);
         result = false;
      }
      return result;
   }

   public void addActions(IMenuManager manager, IBranchProvider provider) {
      checkActionsCreated(provider);

      if (showArtIds != null && isAdmin()) {
         if (manager != null) {
            manager.add(showArtIds);
         }
         showArtIds.updateText();
      }

      if (manager != null) {
         manager.add(showArtVersion);
         manager.add(showArtType);
         manager.add(showArtBranch);
         manager.add(showRelations);
      }
      showArtType.updateText();
      showArtVersion.updateText();
      showArtBranch.updateText();
      showRelations.updateText();

      if (manager != null) {
         manager.add(attributesAction);
         manager.add(new Separator());
         manager.add(saveSettingsAction);
      }

      loadState();
   }

   @Override
   public String getSelectedAttributeData(Artifact artifact) throws OseeCoreException {
      String toReturn = null;
      if (attributesAction != null) {
         Conditions.checkNotNull(artifact, "artifact");
         Collection<IAttributeType> selectedItems = attributesAction.getSelected();

         List<String> info = new ArrayList<>();
         for (IAttributeType attributeType : artifact.getAttributeTypes()) {
            if (selectedItems.contains(attributeType)) {
               String value = artifact.getAttributesToString(attributeType);
               if (Strings.isValid(value)) {
                  info.add(value);
               } else {
                  info.add("?");
               }
            }
         }
         if (!info.isEmpty()) {
            toReturn = "[" + Collections.toString(" | ", info) + "]";
         }
      }
      return toReturn != null ? toReturn : "";
   }

   @Override
   public boolean showArtIds() {
      return showArtIds != null && showArtIds.isChecked();
   }

   @Override
   public boolean showArtType() {
      return showArtType != null && showArtType.isChecked();
   }

   @Override
   public boolean showArtBranch() {
      return showArtBranch != null && showArtBranch.isChecked();
   }

   @Override
   public boolean showArtVersion() {
      return showArtVersion != null && showArtVersion.isChecked();
   }

   @Override
   public boolean showRelations() {
      return showRelations != null && showRelations.isChecked();
   }

   public void setShowArtType(boolean set) {
      if (showArtType != null) {
         showArtType.setChecked(set);
      }
   }

   public void setShowArtBranch(boolean set) {
      if (showArtBranch != null) {
         showArtBranch.setChecked(set);
      }
   }

   public void setShowRelations(boolean set) {
      if (showRelations != null) {
         showRelations.setChecked(set);
      }
   }

   private void refreshView() {
      if (viewer != null) {
         viewer.refresh();
      }
   }

   private final class DecoratorAction extends Action {
      private final String name;
      private boolean isSelected;

      public DecoratorAction(String name, KeyedImage image, boolean defaultValue) {
         super(name, IAction.AS_PUSH_BUTTON);
         this.name = name;
         this.isSelected = defaultValue;
         if (image != null) {
            setImageDescriptor(ImageManager.getImageDescriptor(image));
         }
         updateText();
      }

      @Override
      public void run() {
         setChecked(!isChecked());
         refreshView();
      }

      @Override
      public boolean isChecked() {
         return isSelected;
      }

      @Override
      public void setChecked(boolean checked) {
         isSelected = checked;
         updateText();
      }

      public void updateText() {
         String message = String.format("%s %s", isChecked() ? "Hide" : "Show", name);
         setText(message);
      }
   }

   private final class ShowAttributeAction extends Action {

      private final Set<IAttributeType> selectedTypes;
      private final IBranchProvider branchProvider;

      public ShowAttributeAction(IBranchProvider branchProvider, KeyedImage image) {
         super("Show Attributes", IAction.AS_PUSH_BUTTON);
         this.branchProvider = branchProvider;
         this.selectedTypes = new HashSet<>();
         if (image != null) {
            setImageDescriptor(ImageManager.getImageDescriptor(image));
         }
      }

      @Override
      public void run() {
         if (branchProvider != null) {
            Job job = new UIJob("Select Attribute Types") {

               @Override
               public IStatus runInUIThread(IProgressMonitor monitor) {
                  IStatus status = Status.OK_STATUS;
                  try {
                     BranchId branch = branchProvider.getBranch(monitor);
                     if (branch == null) {
                        status = new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Branch not selected");
                     } else {
                        Collection<IAttributeType> selectableTypes =
                           AttributeTypeManager.getValidAttributeTypes(branch);
                        FilteredCheckboxAttributeTypeDialog dialog = new FilteredCheckboxAttributeTypeDialog(
                           "Select Attribute Types", "Select attribute types to display.");
                        dialog.setSelectable(selectableTypes);

                        List<IAttributeType> initSelection = new ArrayList<>();
                        for (IAttributeType entry : selectedTypes) {
                           for (IAttributeType type : selectableTypes) {
                              if (type.equals(entry)) {
                                 initSelection.add(type);
                              }
                           }
                        }
                        dialog.setInitialSelections(initSelection);

                        int result = dialog.open();
                        if (result == Window.OK) {
                           selectedTypes.clear();
                           for (Object object : dialog.getResult()) {
                              if (object instanceof IAttributeType) {
                                 selectedTypes.add((IAttributeType) object);
                              }
                           }
                           refreshView();
                        }
                     }
                  } catch (OseeCoreException ex) {
                     status =
                        new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Error opening attribute types dialog", ex);
                  }
                  return status;
               }
            };
            Jobs.startJob(job);
         }
      }

      public Collection<IAttributeType> getSelected() {
         return selectedTypes;
      }

      public void setSelected(Collection<String> selected) throws OseeCoreException {
         selectedTypes.clear();
         for (String name : selected) {
            selectedTypes.add(AttributeTypeManager.getType(name));
         }
      }
   }

   private final class SetSettingsAsDefault extends Action {

      public SetSettingsAsDefault() {
         super("Store Label Settings", IAction.AS_PUSH_BUTTON);
         setImageDescriptor(ImageManager.getImageDescriptor(FrameworkImage.SAVE));
      }

      @Override
      public void run() {
         storeState();
         notifySettingsChanged(ArtifactDecorator.this);
      }
   }

   private static void addDecoratorInstance(ArtifactDecorator source) {
      DECORATOR_INSTANCES.add(new WeakReference<ArtifactDecorator>(source));
   }

   private static void notifySettingsChanged(ArtifactDecorator source) {
      List<Object> toRemove = new ArrayList<>();

      for (WeakReference<ArtifactDecorator> ref : DECORATOR_INSTANCES) {
         ArtifactDecorator decorator = ref.get();
         if (decorator != null) {
            if (!source.equals(decorator)) {
               decorator.loadState();
               decorator.refreshView();
            }
         } else {
            toRemove.add(ref);
         }
      }
      DECORATOR_INSTANCES.removeAll(toRemove);
   }
}

Back to the top