Skip to main content
summaryrefslogtreecommitdiffstats
blob: 95d4488cb9eb0fa7a3740a208f075364ae0b38b2 (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
/*******************************************************************************
 * 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.blam;

import java.io.IOException;
import java.io.InputStream;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.xml.parsers.ParserConfigurationException;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.jobs.IJobChangeListener;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.osee.framework.core.data.ArtifactId;
import org.eclipse.osee.framework.core.operation.IOperation;
import org.eclipse.osee.framework.core.operation.OperationLogger;
import org.eclipse.osee.framework.core.operation.Operations;
import org.eclipse.osee.framework.jdk.core.type.OseeCoreException;
import org.eclipse.osee.framework.jdk.core.type.OseeStateException;
import org.eclipse.osee.framework.jdk.core.util.Lib;
import org.eclipse.osee.framework.jdk.core.util.Strings;
import org.eclipse.osee.framework.skynet.core.artifact.Artifact;
import org.eclipse.osee.framework.ui.skynet.FrameworkImage;
import org.eclipse.osee.framework.ui.skynet.XWidgetParser;
import org.eclipse.osee.framework.ui.skynet.internal.Activator;
import org.eclipse.osee.framework.ui.skynet.widgets.XModifiedListener;
import org.eclipse.osee.framework.ui.skynet.widgets.XWidget;
import org.eclipse.osee.framework.ui.skynet.widgets.util.IDynamicWidgetLayoutListener;
import org.eclipse.osee.framework.ui.skynet.widgets.util.SwtXWidgetRenderer;
import org.eclipse.osee.framework.ui.skynet.widgets.util.XWidgetRendererItem;
import org.eclipse.osee.framework.ui.swt.ImageManager;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.forms.IManagedForm;
import org.eclipse.ui.forms.editor.FormEditor;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.osgi.framework.Bundle;
import org.xml.sax.SAXException;

/**
 * @author Ryan D. Brooks
 */
public abstract class AbstractBlam implements IDynamicWidgetLayoutListener {

   private static final String DEFAULT_DESCRIPTION =
      "Select parameters below and click the play button at the top right.";
   private final static String titleEnd = " BLAM";
   private final Pattern capitalLetter = Pattern.compile("[A-Z]+[a-z]*");

   protected Set<ArtifactId> excludedArtifactIdMap = new HashSet<>();
   protected Map<Long, String> branchViews;
   protected ArtifactId viewId = ArtifactId.SENTINEL;

   public enum BlamUiSource {
      DEFAULT,
      FILE
   }

   public static final String BRANCH_VIEW = "Branch View";
   public static final String branchXWidgetXml =
      "<xWidgets><XWidget xwidgetType=\"XBranchSelectWidget\" displayName=\"Branch\" /></xWidgets>";
   public static final String BRANCH_VIEW_WIDGET =
      "<XWidget xwidgetType=\"XCombo()\" displayName=\"Branch View\" horizontalLabel=\"true\"/>";
   public static final String emptyXWidgetsXml = "<xWidgets/>";
   private OperationLogger logger;

   private final String description;
   private final BlamUiSource source;
   private final String name;

   public AbstractBlam() {
      this(null, DEFAULT_DESCRIPTION, BlamUiSource.DEFAULT);
   }

   public AbstractBlam(String name, String usageDescription, BlamUiSource source) {
      this.name = Strings.isValid(name) ? name : generateNameFromClass();
      this.description = Strings.isValid(usageDescription) ? usageDescription : DEFAULT_DESCRIPTION;
      this.source = source != null ? source : BlamUiSource.DEFAULT;
   }

   private String generateNameFromClass() {
      String className = getClass().getSimpleName();
      StringBuilder generatedName = new StringBuilder(className.length() + 7);

      Matcher capMatch = capitalLetter.matcher(className);
      for (boolean found = capMatch.find(); found || !capMatch.hitEnd(); found = capMatch.find()) {
         generatedName.append(capMatch.start() > 0 ? " " + capMatch.group() : capMatch.group());
      }
      return generatedName.toString();
   }

   public void runOperation(VariableMap variableMap, IProgressMonitor monitor) throws Exception {
      throw new OseeStateException(
         "either runOperation or createOperation but be overriden by subclesses of AbstractBlam");
   }

   public IOperation createOperation(VariableMap variableMap, OperationLogger logger) throws Exception {
      return new ExecuteBlamOperation(this, variableMap, logger);
   }

   /**
    * Return collection of categories that blam belongs to. These will be used to create categories that blams are put
    * into in UI navigators. BLAM can belong in multiple categories.
    */
   public abstract Collection<String> getCategories();

   public String getXWidgetsXml() throws OseeCoreException {
      switch (source) {
         case FILE:
            return getXWidgetsXmlFromUiFile(getClass().getSimpleName(), Activator.PLUGIN_ID);
         case DEFAULT:
         default:
            StringBuilder sb = new StringBuilder();
            sb.append("<xWidgets>");
            sb.append(branchXWidgetXml);
            sb.append(BRANCH_VIEW_WIDGET);
            sb.append("</xWidgets>");
            return sb.toString();
      }
   }

   /**
    * Expects the {@code <className>} of blam. Gets {@code /bundleName/ui/<className>Ui.xml } and returns its contents.
    *
    * @param className class name of blam
    * @param nameOfBundle name of bundle i.e. org.eclipse.rcp.xyz
    * @return contents of the {@code /bundleName/ui/<className>Ui.xml }
    * @throws OseeCoreException usually {@link IOException} or {@link NullPointerException} wrapped in
    * {@link OseeCoreException}
    */
   public String getXWidgetsXmlFromUiFile(String className, String nameOfBundle) throws OseeCoreException {
      String file = String.format("ui/%sUi.xml", className);
      Bundle bundle = Platform.getBundle(nameOfBundle);

      String contents = null;
      InputStream inStream = null;
      try {
         inStream = bundle.getEntry(file).openStream();
         contents = Lib.inputStreamToString(inStream);
      } catch (IOException ex) {
         OseeCoreException.wrapAndThrow(ex);
      } finally {
         Lib.close(inStream);
      }

      return contents;
   }

   public String getDescriptionUsage() {
      return this.description;
   }

   public String getName() {
      return name;
   }

   public void log(String... row) {
      if (logger != null) {
         logger.log(row);
      }
   }

   public void log(Throwable th) {
      if (logger != null) {
         logger.log(th);
      }
   }

   public void logf(String format, Object... args) {
      if (logger != null) {
         logger.logf(format, args);
      }
   }

   public void execute(OperationLogger logger, VariableMap variableMap, IJobChangeListener jobChangeListener) {
      try {
         this.logger = logger;
         IOperation blamOperation = createOperation(variableMap, logger);
         Operations.executeAsJob(blamOperation, true, Job.LONG, jobChangeListener);
      } catch (Exception ex) {
         log(ex);
      }
   }

   @SuppressWarnings("unused")
   public List<XWidgetRendererItem> getLayoutDatas() throws IllegalArgumentException, ParserConfigurationException, SAXException, IOException, CoreException, OseeCoreException {
      return XWidgetParser.extractWorkAttributes(new SwtXWidgetRenderer(), getXWidgetsXml());
   }

   public String getRunText() {
      return "Run BLAM in Job";
   }

   public Image getImage() {
      return ImageManager.getImage(FrameworkImage.BLAM);
   }

   public ImageDescriptor getImageDescriptor() {
      return ImageManager.getImageDescriptor(FrameworkImage.BLAM);
   }

   public String getTitle() {
      return getName().toLowerCase().contains(titleEnd.toLowerCase().trim()) ? getName() : getName() + titleEnd;
   }

   public void addWidgets(IManagedForm managedForm, FormEditor editor, Composite sectionBody) throws OseeCoreException {
      // provided for subclass implementation
   }

   public boolean showUsageSection() {
      return true;
   }

   public boolean showExecuteSection() {
      return true;
   }

   public String getTabTitle() {
      return "BLAM Workflow";
   }

   public void excludeArtifacts(Iterator<Artifact> iter) {
      while (iter.hasNext()) {
         Artifact artifact = iter.next();
         if (excludedArtifactIdMap.contains(ArtifactId.valueOf(artifact.getId()))) {
            iter.remove();
         }
      }
   }

   public void setViewId(Object view) {
      if (branchViews != null) {
         for (Entry<Long, String> entry : branchViews.entrySet()) {
            if (entry.getValue().equals(view)) {
               viewId = ArtifactId.valueOf(entry.getKey());
            }
         }
      }
   }

}

Back to the top