|
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 the License "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 |
|
18 package com.nokia.carbide.cpp.internal.pi.save; |
|
19 |
|
20 import java.util.ArrayList; |
|
21 |
|
22 import org.eclipse.core.resources.IContainer; |
|
23 import org.eclipse.core.resources.IFolder; |
|
24 import org.eclipse.core.resources.IProject; |
|
25 import org.eclipse.core.resources.IResource; |
|
26 import org.eclipse.core.resources.IWorkspaceRoot; |
|
27 import org.eclipse.core.resources.ResourcesPlugin; |
|
28 import org.eclipse.core.runtime.IPath; |
|
29 import org.eclipse.core.runtime.Path; |
|
30 import org.eclipse.jface.dialogs.IDialogPage; |
|
31 import org.eclipse.jface.viewers.DecoratingLabelProvider; |
|
32 import org.eclipse.jface.viewers.ISelection; |
|
33 import org.eclipse.jface.viewers.IStructuredSelection; |
|
34 import org.eclipse.jface.viewers.ITreeContentProvider; |
|
35 import org.eclipse.jface.viewers.TreeSelection; |
|
36 import org.eclipse.jface.viewers.TreeViewer; |
|
37 import org.eclipse.jface.viewers.Viewer; |
|
38 import org.eclipse.jface.wizard.WizardDialog; |
|
39 import org.eclipse.jface.wizard.WizardPage; |
|
40 import org.eclipse.swt.SWT; |
|
41 import org.eclipse.swt.events.ModifyEvent; |
|
42 import org.eclipse.swt.events.ModifyListener; |
|
43 import org.eclipse.swt.events.SelectionEvent; |
|
44 import org.eclipse.swt.events.SelectionListener; |
|
45 import org.eclipse.swt.layout.GridData; |
|
46 import org.eclipse.swt.layout.GridLayout; |
|
47 import org.eclipse.swt.widgets.Button; |
|
48 import org.eclipse.swt.widgets.Composite; |
|
49 import org.eclipse.swt.widgets.Label; |
|
50 import org.eclipse.swt.widgets.Text; |
|
51 import org.eclipse.ui.IViewSite; |
|
52 import org.eclipse.ui.IWorkbenchWizard; |
|
53 import org.eclipse.ui.PlatformUI; |
|
54 import org.eclipse.ui.model.WorkbenchContentProvider; |
|
55 import org.eclipse.ui.model.WorkbenchLabelProvider; |
|
56 import org.eclipse.ui.wizards.newresource.BasicNewProjectResourceWizard; |
|
57 |
|
58 import com.nokia.carbide.cpp.pi.ComNokiaCarbidePiHelpIDs; |
|
59 |
|
60 /** |
|
61 * The SaveTableWizardPage wizard page allows setting the containing workspace folder |
|
62 * for the saved table data as well as the file name. The page will only accept file |
|
63 * name without the extension OR with the extension .csv. |
|
64 */ |
|
65 |
|
66 public class SaveTableWizardPage extends WizardPage { |
|
67 |
|
68 // Provides all folders in the workspace |
|
69 private class ProjectContentProvider |
|
70 extends WorkbenchContentProvider |
|
71 implements ITreeContentProvider |
|
72 { |
|
73 // local version just to prevent getChildren/hadChild getting stuck |
|
74 // calling each other |
|
75 private Object[] localGetChildren(Object arg0) { |
|
76 ArrayList<Object> returnList = new ArrayList<Object>(); |
|
77 Object[] children = super.getChildren(arg0); |
|
78 for (Object child: children) { |
|
79 if (child instanceof IViewSite || |
|
80 child instanceof IWorkspaceRoot || |
|
81 child instanceof IFolder) |
|
82 { |
|
83 returnList.add(child); |
|
84 } else if (child instanceof IProject) { |
|
85 if (((IProject)child).isOpen()) { |
|
86 returnList.add(child); |
|
87 } |
|
88 } |
|
89 } |
|
90 return returnList.toArray(new Object[returnList.size()]); |
|
91 } |
|
92 |
|
93 public Object[] getChildren(Object arg0) { |
|
94 return localGetChildren(arg0); |
|
95 } |
|
96 |
|
97 public Object getParent(Object arg0) { |
|
98 return super.getParent(arg0); |
|
99 } |
|
100 |
|
101 public boolean hasChildren(Object arg0) { |
|
102 if (localGetChildren(arg0).length > 0) { |
|
103 return true; |
|
104 } |
|
105 return false; |
|
106 } |
|
107 |
|
108 public Object[] getElements(Object arg0) { |
|
109 return super.getElements(arg0); |
|
110 } |
|
111 |
|
112 public void dispose() { |
|
113 super.dispose(); |
|
114 } |
|
115 |
|
116 public void inputChanged(Viewer arg0, Object arg1, Object arg2) { |
|
117 super.inputChanged(arg0, arg1, arg2); |
|
118 } |
|
119 } |
|
120 |
|
121 // private Text containerText; |
|
122 |
|
123 private Text fileText; |
|
124 |
|
125 private ISelection selection; |
|
126 |
|
127 private TreeViewer outputChooserTreeViewer; |
|
128 |
|
129 /** |
|
130 * Constructor for SampleNewWizardPage. |
|
131 * |
|
132 * @param pageName |
|
133 */ |
|
134 public SaveTableWizardPage(ISelection selection) { |
|
135 super("wizardPage"); //$NON-NLS-1$ |
|
136 setTitle(Messages.getString("SaveTableWizardPage.CVSFileForTableData")); //$NON-NLS-1$ |
|
137 setDescription(Messages.getString("SaveTableWizardPage.SpecifyFileWithCSVExtension")); //$NON-NLS-1$ |
|
138 this.selection = selection; |
|
139 } |
|
140 |
|
141 /** |
|
142 * @see IDialogPage#createControl(Composite) |
|
143 */ |
|
144 public void createControl(Composite parent) { |
|
145 Composite container = new Composite(parent, SWT.NULL); |
|
146 GridLayout layout = new GridLayout(); |
|
147 container.setLayout(layout); |
|
148 layout.numColumns = 1; |
|
149 // layout.verticalSpacing = 9; |
|
150 |
|
151 PlatformUI.getWorkbench().getHelpSystem().setHelp(container, ComNokiaCarbidePiHelpIDs.PI_SAVE_TABLE_WIZARD_PAGE); |
|
152 createProjectComposite(container); |
|
153 GridData gd; |
|
154 |
|
155 Label fileLabel = new Label(container, SWT.NONE); |
|
156 fileLabel.setText(Messages.getString("SaveTableWizardPage.OutputFileName")); //$NON-NLS-1$ |
|
157 fileText = new Text(container, SWT.BORDER | SWT.SINGLE); |
|
158 gd = new GridData(GridData.FILL_HORIZONTAL); |
|
159 fileText.setLayoutData(gd); |
|
160 fileText.addModifyListener(new ModifyListener() { |
|
161 public void modifyText(ModifyEvent e) { |
|
162 dialogChanged(); |
|
163 } |
|
164 }); |
|
165 |
|
166 initialize(); |
|
167 dialogChanged(); |
|
168 setControl(container); |
|
169 } |
|
170 |
|
171 /** |
|
172 * Tests if the current workbench selection is a suitable container to use. |
|
173 */ |
|
174 |
|
175 private void initialize() { |
|
176 if (selection != null && selection.isEmpty() == false |
|
177 && selection instanceof IStructuredSelection) { |
|
178 IStructuredSelection ssel = (IStructuredSelection) selection; |
|
179 if (ssel.size() > 1) |
|
180 return; |
|
181 Object obj = ssel.getFirstElement(); |
|
182 if (obj instanceof IResource) { |
|
183 IContainer container; |
|
184 if (obj instanceof IContainer) |
|
185 container = (IContainer) obj; |
|
186 else |
|
187 container = ((IResource) obj).getParent(); |
|
188 // containerText.setText(container.getFullPath().toString()); |
|
189 } |
|
190 } |
|
191 fileText.setText("new_file.csv"); //$NON-NLS-1$ |
|
192 } |
|
193 |
|
194 /** |
|
195 * Ensures that both text fields are set. |
|
196 */ |
|
197 |
|
198 private void dialogChanged() { |
|
199 // IResource container = ResourcesPlugin.getWorkspace().getRoot() |
|
200 // .findMember(new Path(getContainerName())); |
|
201 String fileName = getFileName(); |
|
202 |
|
203 if (outputChooserTreeViewer.getTree().getSelection().length < 1) { |
|
204 // if (getContainerName().length() == 0) { |
|
205 updateStatus(ERROR, Messages.getString("SaveTableWizardPage.ProjectFolderRequired")); //$NON-NLS-1$ |
|
206 return; |
|
207 } |
|
208 // if (container == null |
|
209 // || (container.getType() & (IResource.PROJECT | IResource.FOLDER)) == 0) { |
|
210 // updateStatus("File container must exist"); |
|
211 // return; |
|
212 // } |
|
213 // if (!container.isAccessible()) { |
|
214 // updateStatus("Project must be writable"); |
|
215 // return; |
|
216 // } |
|
217 if (fileName.length() == 0) { |
|
218 updateStatus(ERROR, Messages.getString("SaveTableWizardPage.FileNameRequired")); //$NON-NLS-1$ |
|
219 return; |
|
220 } |
|
221 if (fileName.replace('\\', '/').indexOf('/', 1) > 0) { |
|
222 updateStatus(ERROR, Messages.getString("SaveTableWizardPage.FileNameMustBeValid")); //$NON-NLS-1$ |
|
223 return; |
|
224 } |
|
225 int dotLoc = fileName.lastIndexOf('.'); |
|
226 if (dotLoc != -1) { |
|
227 String ext = fileName.substring(dotLoc + 1); |
|
228 if (ext.equalsIgnoreCase("csv") == false) { //$NON-NLS-1$ |
|
229 updateStatus(ERROR, Messages.getString("SaveTableWizardPage.FileExtensionMustBeCSV")); //$NON-NLS-1$ |
|
230 return; |
|
231 } |
|
232 } |
|
233 |
|
234 TreeSelection selection = (TreeSelection) outputChooserTreeViewer.getSelection(); |
|
235 if ( (selection != null) |
|
236 && (selection.getFirstElement() != null) |
|
237 && (selection.getFirstElement() instanceof IContainer)) { |
|
238 IContainer container = (IContainer) selection.getFirstElement(); |
|
239 String file = fileName; |
|
240 |
|
241 if (dotLoc == -1) |
|
242 { |
|
243 file += ".csv"; //$NON-NLS-1$ |
|
244 } |
|
245 if (container.getFile(new Path(file)).exists()) { |
|
246 updateStatus(WARNING, Messages.getString("SaveTableWizardPage.WarningFileExists")); //$NON-NLS-1$ |
|
247 return; |
|
248 } |
|
249 } |
|
250 |
|
251 updateStatus(WARNING, null); |
|
252 } |
|
253 |
|
254 private void updateStatus(int messageType, String message) { |
|
255 setMessage(message, messageType); |
|
256 setPageComplete(messageType != ERROR); |
|
257 } |
|
258 |
|
259 public IPath getContainerName() { |
|
260 |
|
261 TreeSelection selection = (TreeSelection) outputChooserTreeViewer.getSelection(); |
|
262 if ( (selection != null) |
|
263 && (selection.getFirstElement() != null) |
|
264 && (selection.getFirstElement() instanceof IContainer)) { |
|
265 IContainer container = (IContainer) selection.getFirstElement(); |
|
266 return container.getFullPath(); |
|
267 } |
|
268 return null; |
|
269 } |
|
270 |
|
271 public String getFileName() { |
|
272 return fileText.getText(); |
|
273 } |
|
274 |
|
275 // /** |
|
276 // * Uses the standard container selection dialog to choose the new value for |
|
277 // * the container field. |
|
278 // */ |
|
279 // |
|
280 // private void handleBrowse() { |
|
281 // ContainerSelectionDialog dialog = new ContainerSelectionDialog( |
|
282 // getShell(), ResourcesPlugin.getWorkspace().getRoot(), false, |
|
283 // "Select new file container"); |
|
284 // if (dialog.open() == ContainerSelectionDialog.OK) { |
|
285 // Object[] result = dialog.getResult(); |
|
286 // if (result.length == 1) { |
|
287 // containerText.setText(((Path) result[0]).toString()); |
|
288 // } |
|
289 // } |
|
290 // } |
|
291 |
|
292 // private Composite composite = null; |
|
293 // private Composite projectComposite = null; |
|
294 // private Composite buttonComposite = null; |
|
295 // private TreeViewer outputChooserTreeViewer = null; |
|
296 // private Button createButton = null; |
|
297 // private Label fileLabel = null; |
|
298 // private Text outputText = null; |
|
299 // |
|
300 // public boolean validatePage() { |
|
301 // // need to enable finish, setPageComplete eventually check global states |
|
302 //// if (outputText.getText().length() < 1 || outputChooserTreeViewer.getTree().getSelection().length < 1) { |
|
303 //// setErrorMessage(Messages.getString("NewPIWizardPageOutputTask.choose.output.project")); //$NON-NLS-1$ |
|
304 //// setPageComplete(false); |
|
305 //// return true; |
|
306 //// } |
|
307 //// setErrorMessage(null); |
|
308 //// setPageComplete(true); |
|
309 // return true; |
|
310 // } |
|
311 |
|
312 /** |
|
313 * This method initializes projectComposite |
|
314 * |
|
315 */ |
|
316 private void createProjectComposite(Composite container) { |
|
317 GridLayout gridLayout2 = new GridLayout(); |
|
318 gridLayout2.numColumns = 2; |
|
319 Composite projectComposite = new Composite(container, SWT.NONE); |
|
320 projectComposite.setLayout(gridLayout2); |
|
321 projectComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); |
|
322 outputChooserTreeViewer = new TreeViewer(projectComposite, SWT.BORDER); |
|
323 outputChooserTreeViewer.getControl().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); |
|
324 outputChooserTreeViewer.setContentProvider(new ProjectContentProvider()); |
|
325 outputChooserTreeViewer.setLabelProvider(new DecoratingLabelProvider( |
|
326 new WorkbenchLabelProvider(), |
|
327 PlatformUI.getWorkbench().getDecoratorManager().getLabelDecorator())); |
|
328 outputChooserTreeViewer.setInput(ResourcesPlugin.getWorkspace().getRoot()); |
|
329 outputChooserTreeViewer.getTree().addSelectionListener(new SelectionListener() { |
|
330 |
|
331 public void widgetDefaultSelected(SelectionEvent arg0) { |
|
332 } |
|
333 |
|
334 public void widgetSelected(SelectionEvent arg0) { |
|
335 dialogChanged(); |
|
336 } |
|
337 }); |
|
338 |
|
339 createButtonComposite(projectComposite); |
|
340 } |
|
341 |
|
342 private void createButtonComposite(Composite projectComposite) { |
|
343 GridLayout gridLayout = new GridLayout(); |
|
344 gridLayout.numColumns = 1; |
|
345 Composite buttonComposite = new Composite(projectComposite, SWT.NONE); |
|
346 buttonComposite.setLayout(gridLayout); |
|
347 buttonComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, true)); |
|
348 Button createButton = new Button(buttonComposite, SWT.PUSH); |
|
349 createButton.setText(Messages.getString("SaveTableWizardPage.CreateEmptyProject")); //$NON-NLS-1$ |
|
350 createButton.addSelectionListener(new SelectionListener() { |
|
351 |
|
352 public void widgetDefaultSelected(SelectionEvent arg0) { |
|
353 } |
|
354 |
|
355 public void widgetSelected(SelectionEvent arg0) { |
|
356 // get a standard Eclipse Wizard for creating folder |
|
357 IWorkbenchWizard wizard = new BasicNewProjectResourceWizard(); |
|
358 wizard.init(PlatformUI.getWorkbench(), new TreeSelection()); |
|
359 WizardDialog dialog = new WizardDialog(getShell(), wizard); |
|
360 dialog.open(); |
|
361 if (outputChooserTreeViewer.getTree().getItemCount() == 1) { |
|
362 dialogChanged(); |
|
363 } |
|
364 } |
|
365 }); |
|
366 } |
|
367 // |
|
368 // private String generateNpiFileName() { |
|
369 // // prepare the container folder to look into |
|
370 // TreeSelection selection = (TreeSelection) outputChooserTreeViewer.getSelection(); |
|
371 // if (selection == null) |
|
372 // return ""; |
|
373 // if (selection.getFirstElement() == null) |
|
374 // return ""; |
|
375 // if ((selection.getFirstElement() instanceof IContainer) == false) |
|
376 // return ""; |
|
377 // IContainer container = (IContainer) selection.getFirstElement(); |
|
378 // |
|
379 // String sampleFilename = null;//new java.io.File(NewPIWizardSettings.getInstance().sampleFileName).getName(); |
|
380 // String initialFilename; |
|
381 // |
|
382 // initialFilename = sampleFilename; |
|
383 // |
|
384 // int dot = initialFilename.lastIndexOf("."); //$NON-NLS-1$ |
|
385 // String noExtension; |
|
386 // String baseName; |
|
387 // String finalName; |
|
388 // if (dot > 1) { |
|
389 // noExtension = initialFilename.substring(0, initialFilename.lastIndexOf(".")); //$NON-NLS-1$ |
|
390 // } else { |
|
391 // noExtension = initialFilename; |
|
392 // } |
|
393 // |
|
394 // Long suffixNumber = new Long(0); |
|
395 // |
|
396 // // just suffix _<number> if the name was derived from input sample name |
|
397 // if (noExtension.lastIndexOf("_") > 1 && //$NON-NLS-1$ |
|
398 // initialFilename.equalsIgnoreCase(sampleFilename) == false |
|
399 // //&& |
|
400 //// isPositiveLong(noExtension.substring(noExtension.lastIndexOf("_") + 1)) |
|
401 // ) { //$NON-NLS-1$ |
|
402 // baseName = noExtension.substring(0, noExtension.lastIndexOf("_")); //$NON-NLS-1$ |
|
403 //// if (isPositiveLong(noExtension.substring(noExtension.lastIndexOf("_") + 1))) { //$NON-NLS-1$ |
|
404 //// suffixNumber = Long.parseLong(noExtension.substring(noExtension.lastIndexOf("_") + 1)); //$NON-NLS-1$ |
|
405 //// } |
|
406 // } else { |
|
407 // baseName = noExtension; |
|
408 // } |
|
409 // |
|
410 // // check existing npi and bump number |
|
411 // while (container.getFile(new Path(baseName + "_" + suffixNumber.toString() + DOT_CSV)).exists()) { //$NON-NLS-1$ |
|
412 // suffixNumber++; |
|
413 // } |
|
414 // |
|
415 // finalName = baseName + "_" + suffixNumber.toString() + DOT_CSV; //$NON-NLS-1$ |
|
416 // |
|
417 // return finalName; |
|
418 // } |
|
419 // |
|
420 // public void setupPageFromFromNewPIWizardSettings() { |
|
421 // outputText.setText(SaveTableWizardPage.DOT_CSV); |
|
422 // outputChooserTreeViewer.getTree().deselectAll(); |
|
423 // IContainer outputContainer = null; |
|
424 // if (outputChooserTreeViewer.getSelection() == null || |
|
425 // outputChooserTreeViewer.getSelection().isEmpty()) { |
|
426 // // stupid eclipse guideline for default container when we found out how to do it |
|
427 // outputContainer = findDefaultContainer(); |
|
428 // } |
|
429 // if (outputContainer == null ) { |
|
430 // // select it if it's the only one output project |
|
431 // if (outputChooserTreeViewer.getTree().getItemCount() == 1) { |
|
432 // outputChooserTreeViewer.setSelection(new StructuredSelection(outputChooserTreeViewer.getTree().getItem(0))); |
|
433 // } |
|
434 // } else { |
|
435 // // highlight all items select by expanding to that level |
|
436 // // restoring from file system persistent data could use this |
|
437 // outputChooserTreeViewer.expandToLevel(outputContainer, 0); |
|
438 // outputChooserTreeViewer.setSelection(new StructuredSelection(outputContainer)); |
|
439 // } |
|
440 // } |
|
441 // |
|
442 // private IContainer findDefaultContainer() { |
|
443 // IContainer result = null; |
|
444 // ISelection selection = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService().getSelection(); |
|
445 // if (selection instanceof IStructuredSelection) { |
|
446 // IStructuredSelection structuredSelection = (IStructuredSelection) selection; |
|
447 // Object firstElement = structuredSelection.getFirstElement(); |
|
448 // if (firstElement instanceof IFile) { |
|
449 // result = ((IFile)firstElement).getParent(); |
|
450 // } else if (firstElement instanceof IContainer) { |
|
451 // result = (IContainer) firstElement; |
|
452 // } |
|
453 // } |
|
454 // if (result == null) { |
|
455 // try { |
|
456 // IResource[] resources = ResourcesPlugin.getWorkspace().getRoot().members(); |
|
457 // for (IResource resource : resources) { |
|
458 // if (resource instanceof IContainer) { |
|
459 // result = (IContainer) resource; |
|
460 // break; |
|
461 // } |
|
462 // } |
|
463 // } catch (CoreException e) { |
|
464 // e.printStackTrace(); |
|
465 // } |
|
466 // } |
|
467 // return result; |
|
468 // } |
|
469 // |
|
470 // public void writePageDataToNewPIWizardSettings() { |
|
471 // } |
|
472 // |
|
473 // public void setVisible(boolean visable) { |
|
474 // super.setVisible(visable); |
|
475 // if (visable) { |
|
476 // outputText.setText(SaveTableWizardPage.DOT_CSV); |
|
477 // validatePage(); |
|
478 // } else { |
|
479 // } |
|
480 // } |
|
481 } |