|
1 /* |
|
2 * Copyright (c) 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: |
|
15 * |
|
16 */ |
|
17 package com.nokia.s60tools.imaker.internal.actions; |
|
18 |
|
19 import java.lang.reflect.InvocationTargetException; |
|
20 import java.util.List; |
|
21 |
|
22 import org.eclipse.core.resources.IFile; |
|
23 import org.eclipse.core.resources.IProject; |
|
24 import org.eclipse.core.resources.IResource; |
|
25 import org.eclipse.core.runtime.IStatus; |
|
26 import org.eclipse.core.runtime.Path; |
|
27 import org.eclipse.core.runtime.jobs.Job; |
|
28 import org.eclipse.jface.action.IAction; |
|
29 import org.eclipse.jface.dialogs.IDialogConstants; |
|
30 import org.eclipse.jface.dialogs.MessageDialog; |
|
31 import org.eclipse.jface.resource.ImageDescriptor; |
|
32 import org.eclipse.jface.viewers.ISelection; |
|
33 import org.eclipse.jface.viewers.IStructuredSelection; |
|
34 import org.eclipse.swt.SWT; |
|
35 import org.eclipse.swt.events.SelectionEvent; |
|
36 import org.eclipse.swt.events.SelectionListener; |
|
37 import org.eclipse.swt.widgets.Control; |
|
38 import org.eclipse.swt.widgets.Menu; |
|
39 import org.eclipse.swt.widgets.MenuItem; |
|
40 import org.eclipse.swt.widgets.Shell; |
|
41 import org.eclipse.ui.IWorkbenchWindow; |
|
42 import org.eclipse.ui.IWorkbenchWindowPulldownDelegate; |
|
43 |
|
44 import com.nokia.s60tools.imaker.IEnvironment; |
|
45 import com.nokia.s60tools.imaker.IEnvironmentManager; |
|
46 import com.nokia.s60tools.imaker.IIMakerWrapper; |
|
47 import com.nokia.s60tools.imaker.IMakerPlugin; |
|
48 import com.nokia.s60tools.imaker.IMakerUtils; |
|
49 import com.nokia.s60tools.imaker.Messages; |
|
50 import com.nokia.s60tools.imaker.StatusHandler; |
|
51 import com.nokia.s60tools.imaker.exceptions.IMakerCoreCancelledException; |
|
52 import com.nokia.s60tools.imaker.exceptions.IMakerCoreExecutionException; |
|
53 import com.nokia.s60tools.imaker.exceptions.IMakerCoreNotFoundException; |
|
54 import com.nokia.s60tools.imaker.internal.console.IMakerJob; |
|
55 import com.nokia.s60tools.imaker.internal.dialogs.LaunchIMakerDialog; |
|
56 import com.nokia.s60tools.imaker.internal.managers.EnvironmentManager; |
|
57 import com.nokia.s60tools.imaker.internal.managers.ProjectManager; |
|
58 import com.nokia.s60tools.imaker.internal.model.ImakerProperties; |
|
59 import com.nokia.s60tools.imaker.internal.wrapper.IMakerWrapper; |
|
60 |
|
61 /** |
|
62 * @version 0.1 |
|
63 */ |
|
64 public class Flashmenu implements IWorkbenchWindowPulldownDelegate { |
|
65 /** menus items */ |
|
66 private Menu menu; |
|
67 private final String MENU_ITEM_SETTINGS = Messages.getString("Flashmenu.17"); |
|
68 |
|
69 /** private data members */ |
|
70 private MenuItem openDialogItem = null; |
|
71 private Shell shell; |
|
72 private IProject selectedProject; |
|
73 |
|
74 /* (non-Javadoc) |
|
75 * @see org.eclipse.ui.IWorkbenchWindowPulldownDelegate#getMenu(org.eclipse.swt.widgets.Control) |
|
76 */ |
|
77 public Menu getMenu(Control parent) { |
|
78 /*dispose the previously used menu*/ |
|
79 if(menu!=null) { |
|
80 menu.dispose(); |
|
81 } |
|
82 /* Create new menu and listeners if first call */ |
|
83 MenuItem item = null; |
|
84 MenuSelectionListener msl = new MenuSelectionListener(); |
|
85 menu = new Menu(parent); |
|
86 |
|
87 ImageDescriptor descriptor = IMakerPlugin.getImageDescriptor("icons/imakermenu16.png"); |
|
88 ProjectManager pm = new ProjectManager(selectedProject); |
|
89 List<IResource> files = pm.getImakerFiles(); |
|
90 for(IResource file: files) { |
|
91 IFile f = (IFile) file; |
|
92 item = new MenuItem(menu, SWT.RADIO); |
|
93 item.setText(f.getName()); |
|
94 item.setData(file); |
|
95 item.setImage(descriptor.createImage()); |
|
96 item.addSelectionListener(msl); |
|
97 } |
|
98 |
|
99 item = new MenuItem(menu, SWT.SEPARATOR); |
|
100 |
|
101 descriptor = IMakerPlugin.getImageDescriptor("icons/imakerdialog.png"); |
|
102 openDialogItem = new MenuItem(menu, SWT.NULL); |
|
103 openDialogItem.setText(MENU_ITEM_SETTINGS); //$NON-NLS-1$ |
|
104 openDialogItem.addSelectionListener(msl); |
|
105 openDialogItem.setImage(descriptor.createImage()); |
|
106 return menu; |
|
107 } |
|
108 |
|
109 /* |
|
110 * (non-Javadoc) |
|
111 * |
|
112 * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#dispose() |
|
113 */ |
|
114 public void dispose() { |
|
115 if(menu!=null) { |
|
116 menu.dispose(); |
|
117 } |
|
118 } |
|
119 |
|
120 /* |
|
121 * (non-Javadoc) |
|
122 * |
|
123 * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#init(org.eclipse.ui.IWorkbenchWindow) |
|
124 */ |
|
125 public void init(IWorkbenchWindow window) { |
|
126 this.shell = window.getShell(); |
|
127 window.getWorkbench(); |
|
128 } |
|
129 |
|
130 /* |
|
131 * (non-Javadoc) |
|
132 * |
|
133 * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction) |
|
134 */ |
|
135 public void run(IAction action) { |
|
136 IEnvironmentManager manager = EnvironmentManager.getInstance(); |
|
137 IEnvironment env = manager.getEnvironmentByDrive(getSelectionRoot()); |
|
138 ImakerProperties run = env.getRunProperties(); |
|
139 |
|
140 if(env!=null&&!run.isUsed()) { |
|
141 MessageDialog.openInformation(shell, "Unable To Launch iMaker", "The selection cannot be launched, and there are no recent " + |
|
142 "launches.\n Create new lauch using Open iMaker Dialog... from the pulldown menu."); |
|
143 } else { |
|
144 executeProperties(run); |
|
145 } |
|
146 } |
|
147 |
|
148 public void selectionChanged(IAction action, ISelection selection) { |
|
149 if(selection instanceof IStructuredSelection) { |
|
150 IStructuredSelection ss = (IStructuredSelection) selection; |
|
151 Object elem = ss.getFirstElement(); |
|
152 if(elem instanceof IResource) { |
|
153 selectedProject = ((IResource)elem).getProject(); |
|
154 } |
|
155 } else { |
|
156 selectedProject = null; |
|
157 } |
|
158 } |
|
159 |
|
160 private void initializeAndRunImaker(List<String> command) { |
|
161 String root = getSelectionRoot(); |
|
162 List<String> imaker = IMakerUtils.getImakerTool(root); |
|
163 IIMakerWrapper wrapper = new IMakerWrapper(imaker); |
|
164 IMakerJob job = new IMakerJob("Creating image", command,wrapper); |
|
165 job.setPriority(Job.LONG); |
|
166 job.setRule(IMakerPlugin.getDefault().getImakerRule()); |
|
167 job.schedule(); |
|
168 } |
|
169 |
|
170 private String getSelectionRoot() { |
|
171 return IMakerUtils.getProjectRootLocation(selectedProject); |
|
172 } |
|
173 |
|
174 /** |
|
175 * internal selectionlistener for menu items. |
|
176 */ |
|
177 private class MenuSelectionListener implements SelectionListener { |
|
178 |
|
179 public void widgetDefaultSelected(SelectionEvent e) { |
|
180 widgetSelected(e); |
|
181 } |
|
182 |
|
183 public void widgetSelected(SelectionEvent e) { |
|
184 MenuItem selection = (MenuItem)e.widget; |
|
185 if(selection==openDialogItem) { |
|
186 try { |
|
187 ProjectManager projectManager = new ProjectManager(selectedProject); |
|
188 IEnvironmentManager manager = IMakerPlugin.getEnvironmentManager(); |
|
189 manager.setActiveEnvironment(getSelectionRoot()); |
|
190 |
|
191 LaunchIMakerDialog dialog = new LaunchIMakerDialog(shell, |
|
192 projectManager,manager); |
|
193 try { |
|
194 manager.getActiveEnvironment().load(); |
|
195 } catch (InvocationTargetException ite) { |
|
196 Throwable cause = ite.getCause(); |
|
197 if(cause instanceof IMakerCoreCancelledException) { |
|
198 return; |
|
199 } else if(cause instanceof IMakerCoreExecutionException) { |
|
200 StatusHandler.handle(IStatus.ERROR,Messages.getString("Error.1"),cause); |
|
201 } else if(cause instanceof IMakerCoreNotFoundException) { |
|
202 StatusHandler.handle(IStatus.ERROR,Messages.getString("Error.2"),cause); |
|
203 } else { |
|
204 StatusHandler.handle(IStatus.ERROR,cause.getMessage(),cause); |
|
205 } |
|
206 return; |
|
207 } |
|
208 int returnValue = dialog.open(); |
|
209 if(returnValue == IDialogConstants.OK_ID) { |
|
210 String path = dialog.getFilePath(); |
|
211 ImakerProperties run = manager.getActiveEnvironment().getRunProperties(); |
|
212 if(!path.equals(ProjectManager.NEW_ITEM)) { |
|
213 IFile file = (IFile)projectManager.getImakerFile(new Path(path)); |
|
214 ImakerProperties prop = ImakerProperties.createFromFile(file); |
|
215 run.clear(); |
|
216 run.putAll(prop); |
|
217 } |
|
218 executeProperties(run); |
|
219 run.setUsed(true); |
|
220 } |
|
221 } catch(NullPointerException ne) { |
|
222 ne.printStackTrace(); |
|
223 } |
|
224 } else { |
|
225 IFile file = (IFile) selection.getData(); |
|
226 if(file!=null) { |
|
227 ImakerProperties props = ImakerProperties.createFromFile(file); |
|
228 executeProperties(props); |
|
229 } |
|
230 } |
|
231 |
|
232 } |
|
233 } |
|
234 private void executeProperties(ImakerProperties prop) { |
|
235 String tempDirectory = System.getProperty("java.io.tmpdir"); |
|
236 String target = tempDirectory+ImakerProperties.GENERATED_FILES_FOLDER; |
|
237 List<String> command = prop.parseImakerCommand(selectedProject, new Path(target)); |
|
238 initializeAndRunImaker(command); |
|
239 } |
|
240 } |