Skip to main content
summaryrefslogtreecommitdiffstats
blob: 208610ab27267d8fc4cfec912978f8412e6f4180 (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
/*******************************************************************************
 * 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.plugin;

import java.util.logging.Level;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.framework.jdk.core.util.Strings;
import org.eclipse.osee.framework.logging.OseeLog;
import org.eclipse.osee.framework.plugin.core.IActionReportingService;
import org.eclipse.osee.framework.plugin.core.IActionable;
import org.eclipse.osee.framework.ui.plugin.internal.OseePluginUiActivator;
import org.eclipse.osee.framework.ui.swt.ImageManager;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
import org.eclipse.ui.part.MultiPageEditorPart;
import org.eclipse.ui.part.ViewPart;
import org.osgi.framework.Version;

public final class OseeUiActions {
   private static final String BUG_TITLE = "Generate Action Against This Tool";

   private OseeUiActions() {
      super();
   }

   private static IActionReportingService getActionReportingService() throws OseeCoreException {
      return OseePluginUiActivator.getInstance().getActionReportingService();
   }

   private static boolean isActionReportingServiceAvailable() {
      boolean isAvailable = false;
      try {
         isAvailable = getActionReportingService() != null;
      } catch (OseeCoreException e) {
         // Do nothing;
      }
      return isAvailable;
   }

   private static void reportLogException(String item, String description) {
      try {
         getActionReportingService().report(item, description);
      } catch (Exception ex) {
         OseeLog.log(OseePluginUiActivator.class, Level.SEVERE, ex);
      }
   }

   public static void addButtonToEditorToolBar(final MultiPageEditorPart editorPart, IActionable actionableObject, final OseeUiActivator oseePlugin, IToolBarManager toolBar, final String editorId, final String actionableItem) {
      if (!isActionReportingServiceAvailable()) {
         return;
      }
      Action bugAction = new Action(BUG_TITLE, IAction.AS_PUSH_BUTTON) {
         @Override
         public void run() {
            String version = (String) oseePlugin.getBundle().getHeaders().get("Bundle-Version");
            String desc = String.format("Found in \"%s\" version %s.", editorId, version);
            if (editorPart instanceof IActionable) {
               String moreDesc = ((IActionable) editorPart).getActionDescription();
               if (Strings.isValid(moreDesc)) {
                  desc += "\n" + moreDesc;
               }
            }
            reportLogException(actionableItem, desc);
         }
      };
      bugAction.setToolTipText(BUG_TITLE);
      bugAction.setImageDescriptor(ImageManager.getImageDescriptor(PluginUiImage.BUG));
      toolBar.add(bugAction);
   }

   public static void addButtonToEditorToolBar(IActionable actionableObject, final OseeUiActivator oseePlugin, ToolBar toolBar, final String editorId, String actionableItem) {
      addButtonToEditorToolBar(actionableObject, oseePlugin, toolBar, null, editorId, actionableItem);
   }

   public static void addButtonToEditorToolBar(IActionable actionableObject, final OseeUiActivator oseePlugin, Composite comp, final String editorId, String actionableItem) {
      addButtonToEditorToolBar(actionableObject, oseePlugin, null, comp, editorId, actionableItem);
   }

   private static void addButtonToEditorToolBar(final IActionable actionableObject, final OseeUiActivator oseePlugin, ToolBar toolBar, Composite comp, final String editorId, final String actionableItem) {
      if (!isActionReportingServiceAvailable()) {
         return;
      }

      if (actionableObject == null) {
         throw new IllegalArgumentException(String.format("actionableObject can not be null"));
      }
      if (!Strings.isValid(editorId)) {
         throw new IllegalArgumentException(String.format("editorId can not be null or empty"));
      }
      if (actionableItem == null) {
         throw new IllegalArgumentException(String.format("aspect can not be null"));
      }

      if (toolBar != null) {
         ToolItem item = new ToolItem(toolBar, SWT.PUSH);
         item.setImage(ImageManager.getImage(PluginUiImage.BUG));
         item.setToolTipText(BUG_TITLE);
         item.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
               String version = (String) oseePlugin.getBundle().getHeaders().get("Bundle-Version");
               String desc = String.format("\n\nItem: %s\nVersion: %s", editorId, version);
               String moreDesc = actionableObject.getActionDescription();
               if (Strings.isValid(moreDesc)) {
                  desc += "\n" + moreDesc;
               }
               reportLogException(actionableItem, desc);
            }
         });
      } else if (comp != null) {
         Button bugButton = new Button(comp, SWT.PUSH);
         bugButton.setToolTipText(BUG_TITLE);
         bugButton.setImage(ImageManager.getImage(PluginUiImage.BUG));
         bugButton.addSelectionListener(new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent e) {
               String version = (String) oseePlugin.getBundle().getHeaders().get("Bundle-Version");
               String desc = String.format("\n\nItem: %s\nVersion: %s", editorId, version);
               String moreDesc = actionableObject.getActionDescription();
               if (Strings.isValid(moreDesc)) {
                  desc += "\n" + moreDesc;
               }
               reportLogException(actionableItem, desc);
            }

         });
      } else {
         throw new IllegalArgumentException("Can't determine bug target.");
      }
   }

   public static void addBugToViewToolbar(final ViewPart viewPart, final IActionable actionableObject, final OseeUiActivator oseePlugin, final String viewId, final String actionableItem) {
      if (!isActionReportingServiceAvailable()) {
         return;
      }
      if (!Strings.isValid(viewId)) {
         throw new IllegalArgumentException(String.format("viewId can not be null or empty"));
      }
      if (actionableItem == null) {
         throw new IllegalArgumentException("Aspect can not be null.");
      }
      Action bugAction = new Action("Generate Action Against This View") {
         @Override
         public void run() {
            String version = (String) oseePlugin.getBundle().getHeaders().get("Bundle-Version");
            String desc = String.format("\n\nItem: %s\nVersion: %s", viewId, version);
            String moreDesc = actionableObject.getActionDescription();
            if (!moreDesc.equals("")) {
               desc += "\n" + moreDesc;
            }
            reportLogException(actionableItem, desc);
         }
      };
      bugAction.setImageDescriptor(ImageManager.getImageDescriptor(PluginUiImage.BUG));
      bugAction.setToolTipText("Generate Action Against This View");

      IToolBarManager toolbarManager = viewPart.getViewSite().getActionBars().getToolBarManager();
      toolbarManager.add(bugAction);
   }

   public static Action createBugAction(OseeUiActivator oseePlugin, IAdaptable target, String itemId, String actionableItem) {
      return new BugAction(oseePlugin, target, itemId, actionableItem);
   }

   private static final class BugAction extends Action {
      private static String BUG_TITLE = "Generate Action Against This Tool";
      private final OseeUiActivator oseePlugin;
      private final String itemId;
      private final IAdaptable target;
      private final String actionableItem;

      public BugAction(OseeUiActivator oseePlugin, IAdaptable target, String itemId, String actionableItem) {
         super(BUG_TITLE, IAction.AS_PUSH_BUTTON);
         this.oseePlugin = oseePlugin;
         this.itemId = itemId;
         this.target = target;
         this.actionableItem = actionableItem;
         setToolTipText(BUG_TITLE);
         setImageDescriptor(ImageManager.getImageDescriptor(PluginUiImage.BUG));
      }

      @Override
      public void run() {
         Version version =
            new Version((String) oseePlugin.getBundle().getHeaders().get(org.osgi.framework.Constants.BUNDLE_VERSION));
         String desc = String.format("Found in \"%s\" version %s.", itemId, version);
         IActionable actionable = (IActionable) target.getAdapter(IActionable.class);
         if (actionable != null) {
            String moreDesc = actionable.getActionDescription();
            if (Strings.isValid(moreDesc)) {
               desc += "\n" + moreDesc;
            }
         }
         reportLogException(actionableItem, desc);
      }
   }
}

Back to the top