Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
berryHandlerUtil.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 
17 #include "berryHandlerUtil.h"
18 
20 #include <berryCommandExceptions.h>
21 
22 #include "berryISources.h"
24 #include "berryRadioState.h"
25 #include "berryObjects.h"
26 #include "berryObjectString.h"
27 
28 namespace berry
29 {
30 
31 void HandlerUtil::NoVariableFound(const ExecutionEvent::ConstPointer& event,
32  const QString& name)
33 {
34  throw ExecutionException("No " + name +
35  " found while executing " + event->GetCommand()->GetId());
36 }
37 
38 void HandlerUtil::IncorrectTypeFound(const ExecutionEvent::ConstPointer& event,
39  const QString& name, const QString& expectedType,
40  const QString& wrongType)
41 {
42  throw ExecutionException("Incorrect type for "
43  + name
44  + " found while executing "
45  + event->GetCommand()->GetId()
46  + ", expected " + expectedType
47  + " found " + wrongType);
48 }
49 
51  const ExecutionEvent::ConstPointer& event, const QString& name)
52 {
53  if (IEvaluationContext::ConstPointer evalContext = event->GetApplicationContext().Cast<const IEvaluationContext>())
54  {
55  Object::ConstPointer var = evalContext->GetVariable(name);
56  return var == IEvaluationContext::UNDEFINED_VARIABLE ? Object::ConstPointer() : var;
57  }
58  return Object::ConstPointer();
59 }
60 
62  const ExecutionEvent::ConstPointer& event, const QString& name)
63 {
65  if (o.IsNull())
66  {
67  HandlerUtil::NoVariableFound(event, name);
68  }
69  return o;
70 }
71 
73  Object::Pointer context, const QString& name)
74 {
76  if (eval.IsNotNull())
77  {
78  Object::ConstPointer var = eval->GetVariable(name);
79  return var == IEvaluationContext::UNDEFINED_VARIABLE ? Object::ConstPointer() : var;
80  }
81  return Object::ConstPointer(nullptr);
82 }
83 
85  const ExecutionEvent::ConstPointer& event)
86 {
88  return o.Cast<const StringVectorType>();
89 }
90 
92  const ExecutionEvent::ConstPointer& event)
93 {
96  if (o.Cast<const StringVectorType>().IsNull())
97  {
98  HandlerUtil::IncorrectTypeFound(event, ISources::ACTIVE_CONTEXT_NAME(),
99  "StringVectorType", o->GetClassName());
100  }
101  return o.Cast<const StringVectorType>();
102 }
103 
104 //IEditorPart::Pointer HandlerUtil::GetActiveEditor(const ExecutionEvent::ConstPointer& event)
105 //{
106 // Object::Pointer o(HandlerUtil::GetVariable(event,
107 // ISources::ACTIVE_EDITOR_NAME));
108 // return o.Cast<IEditorPart>();
109 //}
110 
111 //IEditorPart::Pointer HandlerUtil::GetActiveEditorChecked(
112 // const ExecutionEvent::ConstPointer& event)
113 //{
114 // Object::Pointer o = HandlerUtil::GetVariableChecked(event,
115 // ISources::ACTIVE_EDITOR_NAME);
116 // if (o.Cast<IEditorPart>().IsNull())
117 // {
118 // HandlerUtil::IncorrectTypeFound(event, ISources::ACTIVE_EDITOR_NAME,
119 // "IEditorPart", o->GetClassName());
120 // }
121 // return (IEditorPart) o;
122 //}
123 
125 {
128  return o.Cast<const ObjectString>();
129 }
130 
132  const ExecutionEvent::ConstPointer& event)
133 {
136  if (o.Cast<const ObjectString>().IsNull())
137  {
138  HandlerUtil::IncorrectTypeFound(event, ISources::ACTIVE_EDITOR_ID_NAME(),
139  "QString", o->GetClassName());
140  }
141  return o.Cast<const ObjectString>();
142 }
143 
145 {
148  return IWorkbenchPart::Pointer(const_cast<IWorkbenchPart*>(dynamic_cast<const IWorkbenchPart*>(o.GetPointer())));
149 }
150 
152  const ExecutionEvent::ConstPointer& event)
153 {
156  if (o.Cast<const IWorkbenchPart>().IsNull())
157  {
158  HandlerUtil::IncorrectTypeFound(event, ISources::ACTIVE_PART_NAME(),
159  "IWorkbenchPart", o->GetClassName());
160  }
161  return IWorkbenchPart::Pointer(const_cast<IWorkbenchPart*>(dynamic_cast<const IWorkbenchPart*>(o.GetPointer())));
162 }
163 
165 {
168  return o.Cast<const ObjectString>();
169 }
170 
172  const ExecutionEvent::ConstPointer& event)
173 {
176  if (o.Cast<const ObjectString>().IsNull())
177  {
178  HandlerUtil::IncorrectTypeFound(event, ISources::ACTIVE_PART_ID_NAME(),
179  "QString", o->GetClassName());
180  }
181  return o.Cast<const ObjectString>();
182 }
183 
185  const ExecutionEvent::ConstPointer& event)
186 {
189  return IWorkbenchPartSite::Pointer(const_cast<IWorkbenchPartSite*>(dynamic_cast<const IWorkbenchPartSite*>(o.GetPointer())));
190 }
191 
193  const ExecutionEvent::ConstPointer& event)
194 {
197  if (o.Cast<const IWorkbenchPartSite>().IsNull())
198  {
199  HandlerUtil::IncorrectTypeFound(event, ISources::ACTIVE_SITE_NAME(),
200  "IWorkbenchSitePart", o->GetClassName());
201  }
202  return IWorkbenchPartSite::Pointer(const_cast<IWorkbenchPartSite*>(dynamic_cast<const IWorkbenchPartSite*>(o.GetPointer())));
203 }
204 
206  const ExecutionEvent::ConstPointer& event)
207 {
210  return o.Cast<const ISelection>();
211 }
212 
214  const ExecutionEvent::ConstPointer& event)
215 {
218  if (o.Cast<const ISelection>().IsNull())
219  {
220  HandlerUtil::IncorrectTypeFound(event,
222  o->GetClassName());
223  }
224  return o.Cast<const ISelection>();
225 }
226 
228  const ExecutionEvent::ConstPointer& event)
229 {
232  return o.Cast<const StringVectorType>();
233 }
234 
236  const ExecutionEvent::ConstPointer& event)
237 {
240  if (o.Cast<const StringVectorType>().IsNull())
241  {
242  HandlerUtil::IncorrectTypeFound(event, ISources::ACTIVE_MENU_NAME(),
243  "StringVectorType", o->GetClassName());
244  }
245  return o.Cast<const StringVectorType>();
246 }
247 
249  const ExecutionEvent::ConstPointer& event)
250 {
253  return o.Cast<const ISelection>();
254 }
255 
257  const ExecutionEvent::ConstPointer& event)
258 {
261  if (o.Cast<const ISelection>().IsNull())
262  {
263  HandlerUtil::IncorrectTypeFound(event,
265  "ISelection", o->GetClassName());
266  }
267  return o.Cast<const ISelection>();
268 }
269 
271  const ExecutionEvent::ConstPointer& event)
272 {
275  return IWorkbenchWindow::Pointer(const_cast<IWorkbenchWindow*>(dynamic_cast<const IWorkbenchWindow*>(o.GetPointer())));
276 }
277 
279  const ExecutionEvent::ConstPointer& event)
280 {
283  if (o.Cast<const IWorkbenchWindow>().IsNull())
284  {
285  HandlerUtil::IncorrectTypeFound(event,
286  ISources::ACTIVE_WORKBENCH_WINDOW_NAME(), "IWorkbenchWindow",
287  o->GetClassName());
288  }
289  return IWorkbenchWindow::Pointer(const_cast<IWorkbenchWindow*>(dynamic_cast<const IWorkbenchWindow*>(o.GetPointer())));
290 }
291 
293  const ExecutionEvent::ConstPointer& event)
294 {
297  return o.Cast<const ISelection>();
298 }
299 
301  const ExecutionEvent::ConstPointer& event)
302 {
305  if (o.Cast<const ISelection>().IsNull())
306  {
307  HandlerUtil::IncorrectTypeFound(event,
309  o->GetClassName());
310  }
311  return o.Cast<const ISelection>();
312 }
313 
315  const ExecutionEvent::ConstPointer& event)
316 {
319  return o.Cast<const ISelection>();
320 }
321 
323  const ExecutionEvent::ConstPointer& event)
324 {
327  if (o.Cast<const ISelection>().IsNull())
328  {
329  HandlerUtil::IncorrectTypeFound(event, ISources::SHOW_IN_SELECTION(),
330  "ISelection", o->GetClassName());
331  }
332  return o.Cast<const ISelection>();
333 }
334 
336  const ExecutionEvent::ConstPointer& event)
337 {
340  // if (var == IEvaluationContext.UNDEFINED_VARIABLE) {
341  // return null;
342  // }
343  return var;
344 }
345 
347  const ExecutionEvent::ConstPointer& event)
348 {
351  // if (var == IEvaluationContext.UNDEFINED_VARIABLE) {
352  // HandlerUtil::IncorrectTypeFound(event, ISources::SHOW_IN_INPUT, Object.class, var
353  // .getClass());
354  // }
355  return var;
356 }
357 
359 {
360  State::Pointer state = command->GetState(RegistryToggleState::STATE_ID);
361  if(state.IsNull())
362  {
363  throw ExecutionException("The command does not have a toggle state");
364  }
365  if(ObjectBool::Pointer boolObj = state->GetValue().Cast<ObjectBool>())
366  {
367  bool oldValue = boolObj->GetValue();
368  Object::Pointer newValue(new ObjectBool(!oldValue));
369  state->SetValue(newValue);
370  return oldValue;
371  }
372  else
373  {
374  throw ExecutionException("The command's toggle state doesn't contain a boolean value");
375  }
376 }
377 
379 {
380  QString parameter = event->GetParameter(RadioState::PARAMETER_ID);
381  if (parameter.isNull())
382  {
383  throw ExecutionException("The event does not have the radio state parameter");
384  }
385 
386  Command::ConstPointer command = event->GetCommand();
387  State::Pointer state = command->GetState(RadioState::STATE_ID);
388  if (state.IsNull())
389  {
390  throw ExecutionException("The command does not have a radio state");
391  }
392  if (ObjectString::Pointer strObj = state->GetValue().Cast<ObjectString>())
393  {
394  return parameter == *strObj;
395  }
396  else
397  {
398  throw ExecutionException("The command's radio state doesn't contain a String value");
399  }
400 }
401 
402 void HandlerUtil::UpdateRadioState(const SmartPointer<Command>& command, const QString& newState)
403 {
404  State::Pointer state = command->GetState(RadioState::STATE_ID);
405  if (state.IsNull())
406  {
407  throw ExecutionException("The command does not have a radio state");
408  }
409  Object::Pointer newValue(new ObjectString(newState));
410  state->SetValue(newValue);
411 }
412 
413 }
berry::SmartPointer< const Self > ConstPointer
Definition: berryObject.h:89
static IWorkbenchPart::Pointer GetActivePart(const ExecutionEvent::ConstPointer &event)
static Object::ConstPointer GetShowInInputChecked(const ExecutionEvent::ConstPointer &event)
static Object::ConstPointer GetVariable(const ExecutionEvent::ConstPointer &event, const QString &name)
static void UpdateRadioState(const SmartPointer< Command > &command, const QString &newState)
static const QString ACTIVE_PART_NAME()
static StringVectorType::ConstPointer GetActiveContexts(const ExecutionEvent::ConstPointer &event)
static const QString SHOW_IN_INPUT()
static IWorkbenchWindow::Pointer GetActiveWorkbenchWindowChecked(const ExecutionEvent::ConstPointer &event)
static ISelection::ConstPointer GetActiveMenuEditorInput(const ExecutionEvent::ConstPointer &event)
static ObjectString::ConstPointer GetActivePartIdChecked(const ExecutionEvent::ConstPointer &event)
static IWorkbenchPartSite::Pointer GetActiveSite(const ExecutionEvent::ConstPointer &event)
static bool MatchesRadioState(const SmartPointer< ExecutionEvent > &event)
static const QString ACTIVE_CONTEXT_NAME()
static ObjectString::ConstPointer GetActivePartId(const ExecutionEvent::ConstPointer &event)
static ISelection::ConstPointer GetActiveMenuSelection(const ExecutionEvent::ConstPointer &event)
static IWorkbenchWindow::Pointer GetActiveWorkbenchWindow(const ExecutionEvent::ConstPointer &event)
static const QString STATE_ID
static const QString ACTIVE_CURRENT_SELECTION_NAME()
static const QString ACTIVE_MENU_SELECTION_NAME()
static StringVectorType::ConstPointer GetActiveMenusChecked(const ExecutionEvent::ConstPointer &event)
static ISelection::ConstPointer GetActiveMenuEditorInputChecked(const ExecutionEvent::ConstPointer &event)
static Object::ConstPointer GetVariableChecked(const ExecutionEvent::ConstPointer &event, const QString &name)
static const QString ACTIVE_MENU_NAME()
berry::SmartPointer< Self > Pointer
Definition: berryObject.h:88
static const QString PARAMETER_ID
static IWorkbenchPart::Pointer GetActivePartChecked(const ExecutionEvent::ConstPointer &event)
static const QString ACTIVE_MENU_EDITOR_INPUT_NAME()
static const QString ACTIVE_EDITOR_ID_NAME()
static const QString ACTIVE_WORKBENCH_WINDOW_NAME()
static const QString ACTIVE_PART_ID_NAME()
static ISelection::ConstPointer GetShowInSelectionChecked(const ExecutionEvent::ConstPointer &event)
static ObjectString::ConstPointer GetActiveEditorIdChecked(const ExecutionEvent::ConstPointer &event)
static ISelection::ConstPointer GetCurrentSelection(const ExecutionEvent::ConstPointer &event)
static ISelection::ConstPointer GetCurrentSelectionChecked(const ExecutionEvent::ConstPointer &event)
static ObjectString::ConstPointer GetActiveEditorId(const ExecutionEvent::ConstPointer &event)
ObjectType * GetPointer() const
static bool ToggleCommandState(const SmartPointer< Command > &command)
static ISelection::ConstPointer GetActiveMenuSelectionChecked(const ExecutionEvent::ConstPointer &event)
static StringVectorType::ConstPointer GetActiveMenus(const ExecutionEvent::ConstPointer &event)
SmartPointer< Other > Cast() const
static const QString SHOW_IN_SELECTION()
static const QString ACTIVE_SITE_NAME()
static IWorkbenchPartSite::Pointer GetActiveSiteChecked(const ExecutionEvent::ConstPointer &event)
static Object::ConstPointer GetShowInInput(const ExecutionEvent::ConstPointer &event)
static StringVectorType::ConstPointer GetActiveContextsChecked(const ExecutionEvent::ConstPointer &event)
static ISelection::ConstPointer GetShowInSelection(const ExecutionEvent::ConstPointer &event)