Skip to main content
summaryrefslogtreecommitdiffstats
blob: 8067072fbf63af47848632ba8bb95503b7e5890d (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
/*******************************************************************************
 * 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.ote.ui.define.reports;

import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtension;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.osee.framework.jdk.core.type.Pair;
import org.eclipse.osee.framework.jdk.core.util.Strings;
import org.eclipse.osee.framework.logging.OseeLog;
import org.eclipse.osee.framework.plugin.core.util.ExtensionPoints;
import org.eclipse.osee.framework.ui.swt.ImageManager;
import org.eclipse.osee.ote.ui.define.OteUiDefinePlugin;
import org.eclipse.swt.graphics.Image;
import org.osgi.framework.Bundle;

/**
 * @author Roberto E. Escobar
 */
public class ExtensionDefinedReports {
   private static final String EXTENSION_ID = "ITestRunReport";
   private final Map<String, ReportData> reportMap;
   private Map<String, Pair<String, String>> idsAndNameMap;

   private static ExtensionDefinedReports instance = null;

   private ExtensionDefinedReports() {
      this.reportMap = new HashMap<>();
      loadReports();
   }

   public static ExtensionDefinedReports getInstance() {
      if (instance == null) {
         instance = new ExtensionDefinedReports();
      }
      return instance;
   }

   @SuppressWarnings("unchecked")
   public Pair<String, String>[] getIdsAndNames() {
      if (idsAndNameMap == null) {
         this.idsAndNameMap = new HashMap<>();
         Set<String> ids = reportMap.keySet();
         for (String id : ids) {
            ReportData data = reportMap.get(id);
            this.idsAndNameMap.put(id, new Pair<String, String>(id, data.getName()));
         }
      }
      return idsAndNameMap.values().toArray(new Pair[idsAndNameMap.size()]);
   }

   public Image getImage(String key) {
      ReportData data = reportMap.get(key);
      Image toReturn = data.getIcon();
      return toReturn != null ? toReturn : ImageManager.getImage(ImageManager.MISSING);
   }

   public ITestRunReport getReportGenerator(String key) {
      ReportData data = reportMap.get(key);
      return data != null ? data.getTestRunReport() : null;
   }

   public Pair<String, String> getIdAndName(String id) {
      return idsAndNameMap.get(id);
   }

   private void loadReports() {
      List<IConfigurationElement> elements =
         ExtensionPoints.getExtensionElements(OteUiDefinePlugin.getInstance(), EXTENSION_ID, EXTENSION_ID);
      for (IConfigurationElement element : elements) {
         IExtension extension = (IExtension) element.getParent();
         String identifier = extension.getUniqueIdentifier();
         String name = extension.getLabel();
         String className = element.getAttribute("classname");
         String iconName = element.getAttribute("icon");
         String bundleName = element.getContributor().getName();

         if (Strings.isValid(bundleName) && Strings.isValid(className)) {
            try {
               Bundle bundle = Platform.getBundle(bundleName);
               Class<?> taskClass = bundle.loadClass(className);
               ITestRunReport object = (ITestRunReport) taskClass.newInstance();
               if (object != null) {
                  URL url = bundle.getEntry(iconName);
                  ImageDescriptor imageDescriptor = ImageDescriptor.createFromURL(url);
                  reportMap.put(identifier, new ReportData(identifier, name, object, imageDescriptor.createImage()));
               }
            } catch (Exception ex) {
               OseeLog.logf(OteUiDefinePlugin.class, Level.SEVERE,
                  ex, "Error loading report [%s]", className);
            }
         }
      }
   }

   private final class ReportData {
      private final String name;
      private final Image icon;
      private final ITestRunReport testRunReport;

      public ReportData(String id, String name, ITestRunReport testRunReport, Image icon) {
         super();
         this.name = name;
         this.icon = icon;
         this.testRunReport = testRunReport;
      }

      /**
       * @return the name
       */
      public String getName() {
         return name;
      }

      /**
       * @return the icon
       */
      public Image getIcon() {
         return icon;
      }

      /**
       * @return the testRunReport
       */
      public ITestRunReport getTestRunReport() {
         return testRunReport;
      }

   }
}

Back to the top