Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
berryAbstractUICTKPlugin.cpp
Go to the documentation of this file.
1 /*===================================================================
2 
3 BlueBerry Platform
4 
5 Copyright (c) German Cancer Research Center,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
18 
19 #include "internal/berryBundleUtility.h"
20 #include "internal/berryWorkbenchPlugin.h"
21 
22 #include "berryPlatformUI.h"
24 #include "berryIPreferences.h"
25 
26 #include <QIcon>
27 #include <QImage>
28 
29 namespace berry
30 {
31 
32 const QString AbstractUICTKPlugin::FN_DIALOG_SETTINGS = "dialog_settings.xml";
33 
35  : preferencesService(nullptr)
36 {
37 }
38 
39 // IDialogSettings getDialogSettings() {
40 // if (dialogSettings == null) {
41 // loadDialogSettings();
42 // }
43 // return dialogSettings;
44 // }
45 
46 
47 // ImageRegistry getImageRegistry() {
48 // if (imageRegistry == null) {
49 // imageRegistry = createImageRegistry();
50 // initializeImageRegistry(imageRegistry);
51 // }
52 // return imageRegistry;
53 // }
54 
55 
57 {
58  // Create the preference store lazily.
59  if (preferencesService == nullptr)
60  {
61  ctkServiceReference serviceRef = m_Context->getServiceReference<IPreferencesService>();
62  if (!serviceRef)
63  {
64  BERRY_ERROR << "Preferences service not available";
65  }
66  preferencesService = m_Context->getService<IPreferencesService>(serviceRef);
67  }
68  return preferencesService;
69 }
70 
72 {
73  IPreferencesService* prefService = this->GetPreferencesService();
74  if (prefService == nullptr) return IPreferences::Pointer(nullptr);
75 
76  return prefService->GetSystemPreferences();
77 }
78 
80 {
81  return PlatformUI::GetWorkbench();
82 }
83 
84 // ImageRegistry createImageRegistry()
85 // {
86 //
87 // //If we are in the UI Thread use that
88 // if (Display.getCurrent() != null)
89 // {
90 // return new ImageRegistry(Display.getCurrent());
91 // }
92 //
93 // if (PlatformUI.isWorkbenchRunning())
94 // {
95 // return new ImageRegistry(PlatformUI.getWorkbench().getDisplay());
96 // }
97 //
98 // //Invalid thread access if it is not the UI Thread
99 // //and the workbench is not created.
100 // throw new SWTError(SWT.ERROR_THREAD_INVALID_ACCESS);
101 // }
102 
103 
104 // void initializeImageRegistry(ImageRegistry reg) {
105 // // spec'ed to do nothing
106 // }
107 
108 
109 // void loadDialogSettings() {
110 // dialogSettings = new DialogSettings("Workbench"); //$NON-NLS-1$
111 //
112 // // bug 69387: The instance area should not be created (in the call to
113 // // #getStateLocation) if -data @none or -data @noDefault was used
114 // IPath dataLocation = getStateLocationOrNull();
115 // if (dataLocation != null) {
116 // // try r/w state area in the local file system
117 // String readWritePath = dataLocation.append(FN_DIALOG_SETTINGS)
118 // .toOSString();
119 // File settingsFile = new File(readWritePath);
120 // if (settingsFile.exists()) {
121 // try {
122 // dialogSettings.load(readWritePath);
123 // } catch (IOException e) {
124 // // load failed so ensure we have an empty settings
125 // dialogSettings = new DialogSettings("Workbench"); //$NON-NLS-1$
126 // }
127 //
128 // return;
129 // }
130 // }
131 //
132 // // otherwise look for bundle specific dialog settings
133 // URL dsURL = BundleUtility.find(getBundle(), FN_DIALOG_SETTINGS);
134 // if (dsURL == null) {
135 // return;
136 // }
137 //
138 // InputStream is = null;
139 // try {
140 // is = dsURL.openStream();
141 // BufferedReader reader = new BufferedReader(
142 // new InputStreamReader(is, "utf-8")); //$NON-NLS-1$
143 // dialogSettings.load(reader);
144 // } catch (IOException e) {
145 // // load failed so ensure we have an empty settings
146 // dialogSettings = new DialogSettings("Workbench"); //$NON-NLS-1$
147 // } finally {
148 // try {
149 // if (is != null) {
150 // is.close();
151 // }
152 // } catch (IOException e) {
153 // // do nothing
154 // }
155 // }
156 // }
157 
158 
159 // void refreshPluginActions() {
160 // // If the workbench is not started yet, or is no longer running, do nothing.
161 // if (!PlatformUI.isWorkbenchRunning()) {
162 // return;
163 // }
164 //
165 // // startup() is not guaranteed to be called in the UI thread,
166 // // but refreshPluginActions must run in the UI thread,
167 // // so use asyncExec. See bug 6623 for more details.
168 // Display.getDefault().asyncExec(new Runnable() {
169 // public void run() {
170 // WWinPluginAction.refreshActionList();
171 // }
172 // });
173 // }
174 
175 
176 // void saveDialogSettings() {
177 // if (dialogSettings == null) {
178 // return;
179 // }
180 //
181 // try {
182 // IPath path = getStateLocationOrNull();
183 // if(path == null) {
184 // return;
185 // }
186 // String readWritePath = path
187 // .append(FN_DIALOG_SETTINGS).toOSString();
188 // dialogSettings.save(readWritePath);
189 // } catch (IOException e) {
190 // // spec'ed to ignore problems
191 // } catch (IllegalStateException e) {
192 // // spec'ed to ignore problems
193 // }
194 // }
195 
196 
197 void AbstractUICTKPlugin::start(ctkPluginContext* context)
198 {
199  Plugin::start(context);
200 
201  // Should only attempt refreshPluginActions() once the bundle
202  // has been fully started. Otherwise, action delegates
203  // can be created while in the process of creating
204  // a triggering action delegate (if UI events are processed during startup).
205  // Also, if the start throws an exception, the bundle will be shut down.
206  // We don't want to have created any delegates if this happens.
207  // See bug 63324 for more details.
208 
209  // bundleListener = new BundleListener()
210  // {
211  // public void bundleChanged(BundleEvent event)
212  // {
213  // if (event.getBundle() == getBundle())
214  // {
215  // if (event.getType() == BundleEvent.STARTED)
216  // {
217  // // We're getting notified that the bundle has been started.
218  // // Make sure it's still active. It may have been shut down between
219  // // the time this event was queued and now.
220  // if (getBundle().getState() == Bundle.ACTIVE)
221  // {
222  // refreshPluginActions();
223  // }
224  // fc.removeBundleListener(this);
225  // }
226  // }
227  // }
228  // };
229  // context.addBundleListener(bundleListener);
230 
231  // bundleListener is removed in stop(BundleContext)
232 }
233 
234 void AbstractUICTKPlugin::stop(ctkPluginContext* context)
235 {
236  Q_UNUSED(context)
237 
238  // try
239  // {
240  // if (bundleListener != null)
241  // {
242  // context.removeBundleListener(bundleListener);
243  // }
244  // saveDialogSettings();
245  // savePreferenceStore();
246  // preferenceStore = null;
247  // if (imageRegistry != null)
248  // imageRegistry.dispose();
249  // imageRegistry = null;
250  //}
251 
252  Plugin::stop(context);
253 }
254 
256  const QString& pluginId, const QString& imageFilePath)
257 {
258  if (pluginId.isEmpty() || imageFilePath.isEmpty())
259  {
260  throw ctkInvalidArgumentException("argument cannot be empty");
261  }
262 
263  // if the plug-in is not ready then there is no image
264  QSharedPointer<ctkPlugin> plugin = BundleUtility::FindPlugin(pluginId);
265  if (!BundleUtility::IsReady(plugin.data()))
266  {
267  return QIcon();
268  }
269 
270  QByteArray imgContent = plugin->getResource(imageFilePath);
271  QImage image = QImage::fromData(imgContent);
272  QPixmap pixmap = QPixmap::fromImage(image);
273  return QIcon(pixmap);
274 }
275 
277 {
278  return QIcon(":/org.blueberry.ui.qt/icon_missing.png");
279 }
280 
281 }
void stop(ctkPluginContext *context) override
Definition: berryPlugin.cpp:32
void start(ctkPluginContext *context) override
Definition: berryPlugin.cpp:27
virtual SmartPointer< IPreferences > GetSystemPreferences()=0
Implements transparent reference counting.
#define BERRY_ERROR
Definition: berryLog.h:26
void start(ctkPluginContext *context) override
static IWorkbench * GetWorkbench()
void stop(ctkPluginContext *context) override
IPreferencesService * GetPreferencesService() const
ctkPluginContext * m_Context
Definition: berryPlugin.h:71
SmartPointer< IPreferences > GetPreferences() const
static QIcon ImageDescriptorFromPlugin(const QString &pluginId, const QString &imageFilePath)
berry::SmartPointer< Self > Pointer