|
1 /* |
|
2 * Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 * Base class for all action objects of Trace Builder view |
|
17 * |
|
18 */ |
|
19 package com.nokia.tracebuilder.action; |
|
20 |
|
21 import org.eclipse.jface.action.Action; |
|
22 import org.eclipse.ui.PlatformUI; |
|
23 |
|
24 import com.nokia.tracebuilder.engine.LastKnownLocation; |
|
25 import com.nokia.tracebuilder.engine.LastKnownLocationList; |
|
26 import com.nokia.tracebuilder.engine.TraceBuilderGlobals; |
|
27 import com.nokia.tracebuilder.engine.TraceBuilderHelp; |
|
28 import com.nokia.tracebuilder.engine.TraceLocation; |
|
29 import com.nokia.tracebuilder.engine.TraceLocationList; |
|
30 import com.nokia.tracebuilder.model.TraceBuilderException; |
|
31 import com.nokia.tracebuilder.view.TraceViewPlugin; |
|
32 |
|
33 /** |
|
34 * Base class for all action objects of Trace Builder view |
|
35 * |
|
36 */ |
|
37 abstract class TraceBuilderAction extends Action { |
|
38 |
|
39 /** |
|
40 * Constructor |
|
41 */ |
|
42 protected TraceBuilderAction() { |
|
43 } |
|
44 |
|
45 /** |
|
46 * Constructor with action type |
|
47 * |
|
48 * @param type |
|
49 * the action type |
|
50 */ |
|
51 protected TraceBuilderAction(int type) { |
|
52 super("", type); //$NON-NLS-1$ |
|
53 } |
|
54 |
|
55 /* |
|
56 * (non-Javadoc) |
|
57 * |
|
58 * @see org.eclipse.jface.action.IAction#run() |
|
59 */ |
|
60 @Override |
|
61 public void run() { |
|
62 try { |
|
63 doRun(); |
|
64 } catch (TraceBuilderException e) { |
|
65 TraceBuilderGlobals.getEvents().postError(e); |
|
66 } |
|
67 } |
|
68 |
|
69 /** |
|
70 * Sets the properties of this action |
|
71 * |
|
72 * @param actionID |
|
73 * the action ID |
|
74 * @param helpID |
|
75 * the help ID |
|
76 */ |
|
77 protected void setDefaultProperties(String actionID, String helpID) { |
|
78 if (actionID != null) { |
|
79 setImageDescriptor(TraceViewPlugin |
|
80 .getImageDescriptor(ActionIDs.ICONS_DIRECTORY + actionID |
|
81 + ActionIDs.GIF)); |
|
82 setActionDefinitionId(ActionIDs.ACTION_ID_BASE + actionID); |
|
83 } |
|
84 if (helpID != null) { |
|
85 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, |
|
86 TraceBuilderHelp.HELP_CONTEXT_BASE + helpID); |
|
87 } |
|
88 } |
|
89 |
|
90 /** |
|
91 * Enables this action based on model validity and selected object |
|
92 * |
|
93 * @param modelValid |
|
94 * the model validity |
|
95 * @param selectedObject |
|
96 * the object |
|
97 */ |
|
98 protected void setEnabled(boolean modelValid, Object selectedObject) { |
|
99 if (!modelValid) { |
|
100 setEnabled(false); |
|
101 } else { |
|
102 setEnabled(getEnabledFlag(selectedObject)); |
|
103 } |
|
104 } |
|
105 |
|
106 /** |
|
107 * Checks if this action belongs to the menu |
|
108 * |
|
109 * @return true if in menu, false if not |
|
110 */ |
|
111 protected boolean isInMenu() { |
|
112 return true; |
|
113 } |
|
114 |
|
115 /** |
|
116 * Checks if this action belongs to the pop-up menu |
|
117 * |
|
118 * @param selectedObject |
|
119 * the selection associated to the pop-up |
|
120 * @return true if in pop-up, false if not |
|
121 */ |
|
122 protected boolean isInPopupMenu(Object selectedObject) { |
|
123 return isEnabled(); |
|
124 } |
|
125 |
|
126 /** |
|
127 * If given object is a location or location list, this returns the |
|
128 * associated trace. Otherwise returns the object passed as parameter |
|
129 * |
|
130 * @param selectedObject |
|
131 * the object |
|
132 * @return the trace related to the object |
|
133 */ |
|
134 protected Object locationToTrace(Object selectedObject) { |
|
135 Object retval = null; |
|
136 if (selectedObject instanceof TraceLocation) { |
|
137 retval = ((TraceLocation) selectedObject).getTrace(); |
|
138 } else if (selectedObject instanceof LastKnownLocation) { |
|
139 retval = ((LastKnownLocation) selectedObject).getTrace(); |
|
140 } else if (selectedObject instanceof TraceLocationList) { |
|
141 retval = ((TraceLocationList) selectedObject).getOwner(); |
|
142 } else if (selectedObject instanceof LastKnownLocationList) { |
|
143 retval = ((LastKnownLocationList) selectedObject).getOwner(); |
|
144 } |
|
145 if (retval == null) { |
|
146 retval = selectedObject; |
|
147 } |
|
148 return retval; |
|
149 } |
|
150 |
|
151 /** |
|
152 * Gets the enabled flag based on the object selected by user |
|
153 * |
|
154 * @param selectedObject |
|
155 * the object |
|
156 * @return true if enabled, false if not |
|
157 */ |
|
158 protected abstract boolean getEnabledFlag(Object selectedObject); |
|
159 |
|
160 /** |
|
161 * Runs this action |
|
162 * |
|
163 * @throws TraceBuilderException |
|
164 * if an exception occurs |
|
165 */ |
|
166 protected abstract void doRun() throws TraceBuilderException; |
|
167 |
|
168 } |