Medical Imaging Interaction Toolkit  2018.4.99-08619e4f
Medical Imaging Interaction Toolkit
berryHandlerUtil.cpp
Go to the documentation of this file.
1 /*============================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
13 #include "berryHandlerUtil.h"
14 
16 #include <berryCommandExceptions.h>
17 
18 #include "berryISources.h"
20 #include "berryRadioState.h"
21 #include "berryObjects.h"
22 #include "berryObjectString.h"
23 
24 namespace berry
25 {
26 
27 void HandlerUtil::NoVariableFound(const ExecutionEvent::ConstPointer& event,
28  const QString& name)
29 {
30  throw ExecutionException("No " + name +
31  " found while executing " + event->GetCommand()->GetId());
32 }
33 
34 void HandlerUtil::IncorrectTypeFound(const ExecutionEvent::ConstPointer& event,
35  const QString& name, const QString& expectedType,
36  const QString& wrongType)
37 {
38  throw ExecutionException("Incorrect type for "
39  + name
40  + " found while executing "
41  + event->GetCommand()->GetId()
42  + ", expected " + expectedType
43  + " found " + wrongType);
44 }
45 
47  const ExecutionEvent::ConstPointer& event, const QString& name)
48 {
49  if (IEvaluationContext::ConstPointer evalContext = event->GetApplicationContext().Cast<const IEvaluationContext>())
50  {
51  Object::ConstPointer var = evalContext->GetVariable(name);
53  }
54  return Object::ConstPointer();
55 }
56 
58  const ExecutionEvent::ConstPointer& event, const QString& name)
59 {
61  if (o.IsNull())
62  {
63  HandlerUtil::NoVariableFound(event, name);
64  }
65  return o;
66 }
67 
69  Object::Pointer context, const QString& name)
70 {
72  if (eval.IsNotNull())
73  {
74  Object::ConstPointer var = eval->GetVariable(name);
76  }
77  return Object::ConstPointer(nullptr);
78 }
79 
81  const ExecutionEvent::ConstPointer& event)
82 {
84  return o.Cast<const StringVectorType>();
85 }
86 
88  const ExecutionEvent::ConstPointer& event)
89 {
92  if (o.Cast<const StringVectorType>().IsNull())
93  {
94  HandlerUtil::IncorrectTypeFound(event, ISources::ACTIVE_CONTEXT_NAME(),
95  "StringVectorType", o->GetClassName());
96  }
97  return o.Cast<const StringVectorType>();
98 }
99 
100 //IEditorPart::Pointer HandlerUtil::GetActiveEditor(const ExecutionEvent::ConstPointer& event)
101 //{
102 // Object::Pointer o(HandlerUtil::GetVariable(event,
103 // ISources::ACTIVE_EDITOR_NAME));
104 // return o.Cast<IEditorPart>();
105 //}
106 
107 //IEditorPart::Pointer HandlerUtil::GetActiveEditorChecked(
108 // const ExecutionEvent::ConstPointer& event)
109 //{
110 // Object::Pointer o = HandlerUtil::GetVariableChecked(event,
111 // ISources::ACTIVE_EDITOR_NAME);
112 // if (o.Cast<IEditorPart>().IsNull())
113 // {
114 // HandlerUtil::IncorrectTypeFound(event, ISources::ACTIVE_EDITOR_NAME,
115 // "IEditorPart", o->GetClassName());
116 // }
117 // return (IEditorPart) o;
118 //}
119 
121 {
124  return o.Cast<const ObjectString>();
125 }
126 
128  const ExecutionEvent::ConstPointer& event)
129 {
132  if (o.Cast<const ObjectString>().IsNull())
133  {
134  HandlerUtil::IncorrectTypeFound(event, ISources::ACTIVE_EDITOR_ID_NAME(),
135  "QString", o->GetClassName());
136  }
137  return o.Cast<const ObjectString>();
138 }
139 
141 {
144  return IWorkbenchPart::Pointer(const_cast<IWorkbenchPart*>(dynamic_cast<const IWorkbenchPart*>(o.GetPointer())));
145 }
146 
148  const ExecutionEvent::ConstPointer& event)
149 {
152  if (o.Cast<const IWorkbenchPart>().IsNull())
153  {
154  HandlerUtil::IncorrectTypeFound(event, ISources::ACTIVE_PART_NAME(),
155  "IWorkbenchPart", o->GetClassName());
156  }
157  return IWorkbenchPart::Pointer(const_cast<IWorkbenchPart*>(dynamic_cast<const IWorkbenchPart*>(o.GetPointer())));
158 }
159 
161 {
164  return o.Cast<const ObjectString>();
165 }
166 
168  const ExecutionEvent::ConstPointer& event)
169 {
172  if (o.Cast<const ObjectString>().IsNull())
173  {
174  HandlerUtil::IncorrectTypeFound(event, ISources::ACTIVE_PART_ID_NAME(),
175  "QString", o->GetClassName());
176  }
177  return o.Cast<const ObjectString>();
178 }
179 
181  const ExecutionEvent::ConstPointer& event)
182 {
185  return IWorkbenchPartSite::Pointer(const_cast<IWorkbenchPartSite*>(dynamic_cast<const IWorkbenchPartSite*>(o.GetPointer())));
186 }
187 
189  const ExecutionEvent::ConstPointer& event)
190 {
193  if (o.Cast<const IWorkbenchPartSite>().IsNull())
194  {
195  HandlerUtil::IncorrectTypeFound(event, ISources::ACTIVE_SITE_NAME(),
196  "IWorkbenchSitePart", o->GetClassName());
197  }
198  return IWorkbenchPartSite::Pointer(const_cast<IWorkbenchPartSite*>(dynamic_cast<const IWorkbenchPartSite*>(o.GetPointer())));
199 }
200 
202  const ExecutionEvent::ConstPointer& event)
203 {
206  return o.Cast<const ISelection>();
207 }
208 
210  const ExecutionEvent::ConstPointer& event)
211 {
214  if (o.Cast<const ISelection>().IsNull())
215  {
216  HandlerUtil::IncorrectTypeFound(event,
218  o->GetClassName());
219  }
220  return o.Cast<const ISelection>();
221 }
222 
224  const ExecutionEvent::ConstPointer& event)
225 {
228  return o.Cast<const StringVectorType>();
229 }
230 
232  const ExecutionEvent::ConstPointer& event)
233 {
236  if (o.Cast<const StringVectorType>().IsNull())
237  {
238  HandlerUtil::IncorrectTypeFound(event, ISources::ACTIVE_MENU_NAME(),
239  "StringVectorType", o->GetClassName());
240  }
241  return o.Cast<const StringVectorType>();
242 }
243 
245  const ExecutionEvent::ConstPointer& event)
246 {
249  return o.Cast<const ISelection>();
250 }
251 
253  const ExecutionEvent::ConstPointer& event)
254 {
257  if (o.Cast<const ISelection>().IsNull())
258  {
259  HandlerUtil::IncorrectTypeFound(event,
261  "ISelection", o->GetClassName());
262  }
263  return o.Cast<const ISelection>();
264 }
265 
267  const ExecutionEvent::ConstPointer& event)
268 {
271  return IWorkbenchWindow::Pointer(const_cast<IWorkbenchWindow*>(dynamic_cast<const IWorkbenchWindow*>(o.GetPointer())));
272 }
273 
275  const ExecutionEvent::ConstPointer& event)
276 {
279  if (o.Cast<const IWorkbenchWindow>().IsNull())
280  {
281  HandlerUtil::IncorrectTypeFound(event,
282  ISources::ACTIVE_WORKBENCH_WINDOW_NAME(), "IWorkbenchWindow",
283  o->GetClassName());
284  }
285  return IWorkbenchWindow::Pointer(const_cast<IWorkbenchWindow*>(dynamic_cast<const IWorkbenchWindow*>(o.GetPointer())));
286 }
287 
289  const ExecutionEvent::ConstPointer& event)
290 {
293  return o.Cast<const ISelection>();
294 }
295 
297  const ExecutionEvent::ConstPointer& event)
298 {
301  if (o.Cast<const ISelection>().IsNull())
302  {
303  HandlerUtil::IncorrectTypeFound(event,
305  o->GetClassName());
306  }
307  return o.Cast<const ISelection>();
308 }
309 
311  const ExecutionEvent::ConstPointer& event)
312 {
315  return o.Cast<const ISelection>();
316 }
317 
319  const ExecutionEvent::ConstPointer& event)
320 {
323  if (o.Cast<const ISelection>().IsNull())
324  {
325  HandlerUtil::IncorrectTypeFound(event, ISources::SHOW_IN_SELECTION(),
326  "ISelection", o->GetClassName());
327  }
328  return o.Cast<const ISelection>();
329 }
330 
332  const ExecutionEvent::ConstPointer& event)
333 {
336  // if (var == IEvaluationContext.UNDEFINED_VARIABLE) {
337  // return null;
338  // }
339  return var;
340 }
341 
343  const ExecutionEvent::ConstPointer& event)
344 {
347  // if (var == IEvaluationContext.UNDEFINED_VARIABLE) {
348  // HandlerUtil::IncorrectTypeFound(event, ISources::SHOW_IN_INPUT, Object.class, var
349  // .getClass());
350  // }
351  return var;
352 }
353 
355 {
356  State::Pointer state = command->GetState(RegistryToggleState::STATE_ID);
357  if(state.IsNull())
358  {
359  throw ExecutionException("The command does not have a toggle state");
360  }
361  if(ObjectBool::Pointer boolObj = state->GetValue().Cast<ObjectBool>())
362  {
363  bool oldValue = boolObj->GetValue();
364  Object::Pointer newValue(new ObjectBool(!oldValue));
365  state->SetValue(newValue);
366  return oldValue;
367  }
368  else
369  {
370  throw ExecutionException("The command's toggle state doesn't contain a boolean value");
371  }
372 }
373 
375 {
376  QString parameter = event->GetParameter(RadioState::PARAMETER_ID);
377  if (parameter.isNull())
378  {
379  throw ExecutionException("The event does not have the radio state parameter");
380  }
381 
382  Command::ConstPointer command = event->GetCommand();
383  State::Pointer state = command->GetState(RadioState::STATE_ID);
384  if (state.IsNull())
385  {
386  throw ExecutionException("The command does not have a radio state");
387  }
388  if (ObjectString::Pointer strObj = state->GetValue().Cast<ObjectString>())
389  {
390  return parameter == *strObj;
391  }
392  else
393  {
394  throw ExecutionException("The command's radio state doesn't contain a String value");
395  }
396 }
397 
398 void HandlerUtil::UpdateRadioState(const SmartPointer<Command>& command, const QString& newState)
399 {
400  State::Pointer state = command->GetState(RadioState::STATE_ID);
401  if (state.IsNull())
402  {
403  throw ExecutionException("The command does not have a radio state");
404  }
405  Object::Pointer newValue(new ObjectString(newState));
406  state->SetValue(newValue);
407 }
408 
409 }
berry::SmartPointer< const Self > ConstPointer
Definition: berryObject.h:83
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)
SmartPointer< Other > Cast() const
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:82
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 Object::ConstPointer UNDEFINED_VARIABLE
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)
static bool ToggleCommandState(const SmartPointer< Command > &command)
static ISelection::ConstPointer GetActiveMenuSelectionChecked(const ExecutionEvent::ConstPointer &event)
static StringVectorType::ConstPointer GetActiveMenus(const ExecutionEvent::ConstPointer &event)
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)
ObjectType * GetPointer() const