|
1 /* |
|
2 * Copyright (c) 2008-2009 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: Definitions for the class DropDownMenu |
|
15 * |
|
16 */ |
|
17 |
|
18 package com.nokia.s60tools.analyzetool.ui.actions; |
|
19 |
|
20 import java.util.AbstractList; |
|
21 import java.util.ArrayList; |
|
22 import java.util.Iterator; |
|
23 |
|
24 import org.eclipse.core.resources.IProject; |
|
25 import org.eclipse.jface.action.Action; |
|
26 import org.eclipse.jface.action.ActionContributionItem; |
|
27 import org.eclipse.jface.action.IAction; |
|
28 import org.eclipse.jface.action.IMenuCreator; |
|
29 import org.eclipse.swt.SWT; |
|
30 import org.eclipse.swt.widgets.Control; |
|
31 import org.eclipse.swt.widgets.Menu; |
|
32 import org.eclipse.swt.widgets.MenuItem; |
|
33 |
|
34 import com.nokia.s60tools.analyzetool.global.Constants; |
|
35 import com.nokia.s60tools.analyzetool.global.Util; |
|
36 import com.nokia.s60tools.analyzetool.ui.MainView; |
|
37 |
|
38 /** |
|
39 * Creates new drop down menu by given IActions. |
|
40 * |
|
41 * @author kihe |
|
42 * |
|
43 */ |
|
44 public class DropDownMenu extends Action implements IMenuCreator { |
|
45 |
|
46 /** Menu where to add actions. */ |
|
47 private Menu ddMenu; |
|
48 |
|
49 /** Contains actions of current menu. */ |
|
50 private final java.util.AbstractList<IAction> actions; |
|
51 |
|
52 /** this class parent class reference. */ |
|
53 private final MainView parentClass; |
|
54 |
|
55 /** Need to check actions. */ |
|
56 private final boolean checkActions; |
|
57 |
|
58 /** Is menu for file open actions. */ |
|
59 private final boolean isFileMenu; |
|
60 |
|
61 /** |
|
62 * Constructor. |
|
63 * |
|
64 * @param title |
|
65 * Drop down menu title |
|
66 * @param parentClassRef |
|
67 * Parent class reference |
|
68 * @param check |
|
69 * Is it necessary to check each action validity |
|
70 * @param fileMenu |
|
71 * Is current reference file menu |
|
72 * |
|
73 */ |
|
74 public DropDownMenu(final String title, final MainView parentClassRef, |
|
75 final boolean check, final boolean fileMenu) { |
|
76 setText(title); |
|
77 setMenuCreator(this); |
|
78 actions = new ArrayList<IAction>(); |
|
79 parentClass = parentClassRef; |
|
80 checkActions = check; |
|
81 isFileMenu = fileMenu; |
|
82 } |
|
83 |
|
84 /** |
|
85 * Adds actions. |
|
86 * |
|
87 * @param action |
|
88 * Action to add menu |
|
89 */ |
|
90 public void addAction(IAction action) { |
|
91 actions.add(action); |
|
92 } |
|
93 |
|
94 /** |
|
95 * Adds new action to drop down menu. |
|
96 * |
|
97 * @param parent |
|
98 * Current drop down menu |
|
99 * @param action |
|
100 * Action to add drop down menu |
|
101 */ |
|
102 protected void addActionToMenu(Menu parent, IAction action) { |
|
103 // create new contribution item |
|
104 ActionContributionItem item = new ActionContributionItem(action); |
|
105 |
|
106 item.fill(parent, -1); |
|
107 } |
|
108 |
|
109 /** |
|
110 * Creates file open menu. |
|
111 */ |
|
112 public final void createFileOpenMenu() { |
|
113 |
|
114 // add default item to menu |
|
115 // analyze results |
|
116 IAction analyzeResults = new Action() { |
|
117 @Override |
|
118 public void run() { |
|
119 parentClass.analyzeDataFile(Constants.ANALYZE_ASK_FOR_USER, |
|
120 null, true); |
|
121 } |
|
122 }; |
|
123 |
|
124 analyzeResults.setText(Constants.ACTION_OPEN_DOTS); |
|
125 analyzeResults.setToolTipText(Constants.ACTION_OPEN_DOTS); |
|
126 addActionToMenu(ddMenu, analyzeResults); |
|
127 |
|
128 // add separator to menu |
|
129 new MenuItem(ddMenu, SWT.SEPARATOR); |
|
130 |
|
131 // get history of opened files |
|
132 AbstractList<String> openedFiles = parentClass.fileOpenHistory |
|
133 .getItems(); |
|
134 |
|
135 if (openedFiles.isEmpty()) { |
|
136 Action tmpAction = new Action() { |
|
137 @Override |
|
138 public void run() { |
|
139 // no nothing by design |
|
140 } |
|
141 }; |
|
142 |
|
143 tmpAction.setText(Constants.NO_OPENED_FILES); |
|
144 tmpAction.setEnabled(false); |
|
145 addActionToMenu(ddMenu, tmpAction); |
|
146 } else { |
|
147 // thru opened files |
|
148 final Iterator<String> iterOpenedFiles = openedFiles.iterator(); |
|
149 while (iterOpenedFiles.hasNext()) { |
|
150 // get one history file |
|
151 String oneFile = iterOpenedFiles.next(); |
|
152 |
|
153 // create new action |
|
154 FileAction tmpAction = new FileAction() { |
|
155 @Override |
|
156 public void run() { |
|
157 parentClass.analyzeDataFile( |
|
158 Constants.ANALYZE_USE_DATA_FILE, super |
|
159 .getFileLocation(), true); |
|
160 } |
|
161 }; |
|
162 tmpAction.setFileLocation(oneFile); |
|
163 |
|
164 addActionToMenu(ddMenu, tmpAction); |
|
165 tmpAction.setToolTipText(oneFile); |
|
166 tmpAction.setText(parseTextName(oneFile)); |
|
167 } |
|
168 } |
|
169 } |
|
170 |
|
171 /** |
|
172 * Disposes current menu |
|
173 * |
|
174 * @see org.eclipse.jface.action.IMenuCreator#dispose() |
|
175 */ |
|
176 public void dispose() { |
|
177 if (ddMenu != null) { |
|
178 ddMenu.dispose(); |
|
179 ddMenu = null; |
|
180 } |
|
181 } |
|
182 |
|
183 /** |
|
184 * Gets menu contents. |
|
185 * |
|
186 * @param parent Parent of this menu |
|
187 * |
|
188 * @return Reference of menu |
|
189 */ |
|
190 public Menu getMenu(Control parent) { |
|
191 // if menu is open => close it |
|
192 if (ddMenu != null) { |
|
193 ddMenu.dispose(); |
|
194 } |
|
195 |
|
196 // create new menu |
|
197 ddMenu = new Menu(parent); |
|
198 |
|
199 // if this menu is file open menu |
|
200 if (isFileMenu) { |
|
201 createFileOpenMenu(); |
|
202 } else { |
|
203 // get iterator of menu actions |
|
204 java.util.Iterator<IAction> iterActions = actions.iterator(); |
|
205 |
|
206 // thru action |
|
207 while (iterActions.hasNext()) { |
|
208 |
|
209 // get one action |
|
210 IAction oneAction = iterActions.next(); |
|
211 |
|
212 // if no need to check actions => just add it to menu |
|
213 if (!checkActions) { |
|
214 addActionToMenu(ddMenu, oneAction); |
|
215 continue; |
|
216 } |
|
217 |
|
218 // file location |
|
219 String targetPath = ""; |
|
220 |
|
221 // get project reference |
|
222 IProject project = parentClass.project; |
|
223 |
|
224 // if action text equals to save data file and project exists |
|
225 if (Constants.ACTION_SAVE_DATA.equals(oneAction.getText()) |
|
226 && project != null && project.isOpen()) { |
|
227 // get data file location |
|
228 targetPath = Util.getBldInfFolder(project, false) |
|
229 + Constants.FILENAME; |
|
230 java.io.File file = new java.io.File(targetPath); |
|
231 |
|
232 // check file exists |
|
233 if (!file.exists()) { |
|
234 // is data file opened by user |
|
235 String openedDataFile = parentClass.usedDataFileName; |
|
236 |
|
237 // if no data file opened => set data file name to "" |
|
238 if (openedDataFile == null) { |
|
239 openedDataFile = ""; |
|
240 } |
|
241 |
|
242 // create file and if it exists set targetpath |
|
243 java.io.File s60File = new java.io.File(openedDataFile); |
|
244 if (s60File.exists()) { |
|
245 targetPath = openedDataFile; |
|
246 } |
|
247 } |
|
248 } else if (Constants.ACTION_SAVE_REPORT.equals(oneAction |
|
249 .getText()) |
|
250 && project != null && project.isOpen()) { |
|
251 targetPath = Util.getBldInfFolder(project, false) |
|
252 + Constants.FILENAME_CARBIDE; |
|
253 |
|
254 } |
|
255 |
|
256 // if file exisits enable this action otherwise disable |
|
257 java.io.File file = new java.io.File(targetPath); |
|
258 if (file.exists()) { |
|
259 oneAction.setEnabled(true); |
|
260 } else { |
|
261 oneAction.setEnabled(false); |
|
262 } |
|
263 // add action to menu |
|
264 addActionToMenu(ddMenu, oneAction); |
|
265 |
|
266 } |
|
267 |
|
268 } |
|
269 |
|
270 return ddMenu; |
|
271 } |
|
272 |
|
273 /** |
|
274 * Returns menu. |
|
275 * |
|
276 * @return Returns reference of this menu |
|
277 */ |
|
278 public Menu getMenu(Menu parent) { |
|
279 return null; |
|
280 } |
|
281 |
|
282 /** |
|
283 * Parsing given name if it is too long. |
|
284 * |
|
285 * @param name |
|
286 * File name |
|
287 * @return Parsed file name if it is too long otherwise the actual file name |
|
288 */ |
|
289 public final String parseTextName(String name) { |
|
290 int howManyChar = 100; |
|
291 if (name.length() > howManyChar) { |
|
292 StringBuffer tmpBuff = new StringBuffer(); |
|
293 tmpBuff.append("..."); |
|
294 tmpBuff.append(name.substring(name.length() - howManyChar, name |
|
295 .length())); |
|
296 return tmpBuff.toString(); |
|
297 } |
|
298 return name; |
|
299 } |
|
300 |
|
301 @Override |
|
302 public void run() { |
|
303 |
|
304 // get menu |
|
305 getMenu(parentClass.runView.getControl()); |
|
306 |
|
307 // set menu visible |
|
308 ddMenu.setVisible(true); |
|
309 |
|
310 } |
|
311 } |