1 | package de.uka.ipd.sdq.sensorframework.dialogs.dataset; |
2 | |
3 | import org.eclipse.core.resources.ResourcesPlugin; |
4 | import org.eclipse.core.runtime.IPath; |
5 | import org.eclipse.core.runtime.IStatus; |
6 | import org.eclipse.jface.dialogs.MessageDialog; |
7 | import org.eclipse.jface.viewers.TreeSelection; |
8 | import org.eclipse.jface.wizard.IWizardPage; |
9 | import org.eclipse.jface.wizard.Wizard; |
10 | import org.eclipse.ui.PlatformUI; |
11 | import org.eclipse.ui.dialogs.WizardNewFileCreationPage; |
12 | import org.eclipse.ui.dialogs.WizardNewFolderMainPage; |
13 | |
14 | import de.uka.ipd.sdq.dialogs.error.ErrorDisplayDialog; |
15 | import de.uka.ipd.sdq.sensorframework.SensorFrameworkDataset; |
16 | import de.uka.ipd.sdq.sensorframework.dao.file.FileDAOFactory; |
17 | import de.uka.ipd.sdq.sensorframework.dao.memory.MemoryDAOFactory; |
18 | import de.uka.ipd.sdq.sensorframework.dialogs.SensorFrameworkDialogPlugin; |
19 | import de.uka.ipd.sdq.sensorframework.entities.dao.IDAOFactory; |
20 | |
21 | public class AddNewDatasourceWizard extends Wizard { |
22 | |
23 | private WizardNewFolderMainPage newFolderPage; |
24 | private WizardNewFileCreationPage newDBpage; |
25 | private String result; |
26 | private WizardSelectDatasourcePage selectTypePage; |
27 | |
28 | public AddNewDatasourceWizard() { |
29 | super(); |
30 | this.setWindowTitle("Select/create datastore..."); |
31 | } |
32 | |
33 | |
34 | /* (non-Javadoc) |
35 | * @see org.eclipse.jface.wizard.Wizard#addPages() |
36 | */ |
37 | @Override |
38 | public void addPages() { |
39 | super.addPages(); |
40 | |
41 | selectTypePage = new WizardSelectDatasourcePage( |
42 | "Select Type of Datasource to create", true, true, true); |
43 | this.addPage(selectTypePage); |
44 | |
45 | newDBpage = new WizardNewFileCreationPage("Create new database", |
46 | new TreeSelection()); |
47 | this.addPage(newDBpage); |
48 | |
49 | newFolderPage = new WizardNewFolderMainPage( |
50 | "Create new data directory", new TreeSelection()); |
51 | this.addPage(newFolderPage); |
52 | } |
53 | |
54 | |
55 | /* (non-Javadoc) |
56 | * @see org.eclipse.jface.wizard.Wizard#performFinish() |
57 | */ |
58 | @Override |
59 | public boolean performFinish() { |
60 | |
61 | if (selectTypePage.getResult().equals("Memory Datasource")) { |
62 | |
63 | SensorFrameworkDataset.singleton().addDataSource( |
64 | new MemoryDAOFactory("")); |
65 | |
66 | } else if (selectTypePage.getResult().equals("File Datasource")) { |
67 | |
68 | IPath selectedWorkspacePath = newFolderPage.createNewFolder().getFullPath(); |
69 | IPath targetDirectory = ResourcesPlugin.getWorkspace().getRoot().getFile(selectedWorkspacePath).getLocation(); |
70 | try { |
71 | IDAOFactory fileFactory = new FileDAOFactory(targetDirectory.toOSString()); |
72 | SensorFrameworkDataset.singleton().addDataSource( |
73 | fileFactory); |
74 | } catch (Exception ex) { |
75 | MessageDialog.openError(getShell(), "File DAO factory error.", |
76 | ex.getMessage()); |
77 | SensorFrameworkDialogPlugin.log(IStatus.ERROR, ex.getMessage() == null ? "" : ex.getMessage()); |
78 | return false; |
79 | } |
80 | } |
81 | return true; |
82 | } |
83 | |
84 | |
85 | /* (non-Javadoc) |
86 | * @see org.eclipse.jface.wizard.Wizard#canFinish() |
87 | */ |
88 | @Override |
89 | public boolean canFinish() { |
90 | if (selectTypePage.getResult().equals("Memory Datasource")) { |
91 | return true; |
92 | } else if (selectTypePage.getResult().equals("DB4O Datasource")) { |
93 | return newDBpage.isPageComplete(); |
94 | } else if (selectTypePage.getResult().equals("File Datasource")) { |
95 | return newFolderPage.isPageComplete(); |
96 | } |
97 | return false; |
98 | } |
99 | |
100 | |
101 | /* (non-Javadoc) |
102 | * @see org.eclipse.jface.wizard.Wizard#getNextPage(org.eclipse.jface.wizard.IWizardPage) |
103 | */ |
104 | @Override |
105 | public IWizardPage getNextPage(IWizardPage page) { |
106 | if (page instanceof WizardSelectDatasourcePage) { |
107 | |
108 | WizardSelectDatasourcePage data_type_page = (WizardSelectDatasourcePage) page; |
109 | |
110 | if (data_type_page.getResult().equals("DB4O Datasource")) { |
111 | return newDBpage; |
112 | } else if (data_type_page.getResult().equals("File Datasource")) { |
113 | return newFolderPage; |
114 | } |
115 | } |
116 | return null; |
117 | } |
118 | |
119 | public String getResult() { |
120 | return result; |
121 | } |
122 | |
123 | private class ErrorDisplayRunner implements Runnable { |
124 | private Throwable e; |
125 | |
126 | /** |
127 | * @param e the throwable to display in the error |
128 | * display dialog |
129 | */ |
130 | public ErrorDisplayRunner(Throwable e) { |
131 | super(); |
132 | this.e = e; |
133 | } |
134 | |
135 | public void run() { |
136 | new ErrorDisplayDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow() |
137 | .getShell(), e).open(); |
138 | } |
139 | } |
140 | |
141 | } |
142 | |