1 package org.simantics.g3d.wizard;
\r
4 import java.io.IOException;
\r
5 import java.lang.reflect.InvocationTargetException;
\r
6 import java.util.Deque;
\r
7 import java.util.Iterator;
\r
8 import java.util.LinkedList;
\r
9 import java.util.Set;
\r
10 import java.util.TreeSet;
\r
12 import org.eclipse.jface.dialogs.MessageDialog;
\r
13 import org.eclipse.jface.operation.IRunnableWithProgress;
\r
14 import org.eclipse.jface.preference.IPersistentPreferenceStore;
\r
15 import org.eclipse.jface.preference.IPreferenceStore;
\r
16 import org.eclipse.jface.viewers.IStructuredSelection;
\r
17 import org.eclipse.jface.wizard.Wizard;
\r
18 import org.eclipse.jface.wizard.WizardPage;
\r
19 import org.eclipse.ui.IExportWizard;
\r
20 import org.eclipse.ui.IMemento;
\r
21 import org.eclipse.ui.IWorkbench;
\r
22 import org.simantics.db.management.ISessionContext;
\r
23 import org.simantics.project.IProject;
\r
24 import org.simantics.project.ProjectKeys;
\r
25 import org.simantics.ui.SimanticsUI;
\r
26 import org.simantics.utils.ui.ErrorLogger;
\r
27 import org.simantics.utils.ui.ExceptionUtils;
\r
28 import org.simantics.utils.ui.workbench.StringMemento;
\r
30 public abstract class ModelExportWizard<T extends IExportModel> extends Wizard implements IExportWizard {
\r
32 private static final int MAX_RECENT_EXPORT_PATHS = 10;
\r
33 Deque<String> recentExportPaths;
\r
38 protected abstract T createExportModel(Deque<String> recentExportPaths);
\r
40 protected abstract ModelExportWizardPage<T> createExportPage(T exportModel);
\r
42 protected abstract IPersistentPreferenceStore getPreferenceStore();
\r
44 protected abstract IRunnableWithProgress createExportRunnable(T exportModel);
\r
46 protected abstract String getExportLocationId();
\r
47 protected abstract String getExportOverwriteId();
\r
50 public void init(IWorkbench workbench, IStructuredSelection selection) {
\r
53 ISessionContext ctx = SimanticsUI.getSessionContext();
\r
56 IProject project = ctx.getHint(ProjectKeys.KEY_PROJECT);
\r
57 if (project == null)
\r
60 exportModel = createExportModel(recentExportPaths);
\r
61 exportModel.setSelection(selection.getFirstElement());
\r
62 exportModel.setOverwrite(overwrite);
\r
66 public void addPages() {
\r
68 if (exportModel != null) {
\r
69 addPage(createExportPage(exportModel));
\r
76 public boolean performFinish() {
\r
78 recentExportPaths.addFirst(exportModel.getExportLocation().getAbsolutePath());
\r
79 removeDuplicates(recentExportPaths);
\r
80 if (recentExportPaths.size() > MAX_RECENT_EXPORT_PATHS)
\r
81 recentExportPaths.pollLast();
\r
84 } catch (IOException e) {
\r
85 ErrorLogger.defaultLogError("Failed to write preferences", e);
\r
88 if (exportModel.usesFile()) {
\r
90 File outputFile = exportModel.getExportLocation();
\r
92 if (outputFile.exists()) {
\r
93 if (!outputFile.isFile()) {
\r
94 MessageDialog.openError(getShell(), "File Problem", "Output target is not a file " + outputFile.getAbsolutePath());
\r
97 if (!exportModel.isOverwrite()) {
\r
98 boolean ok = MessageDialog.openConfirm(getShell(), "Overwrite", "A file by the name " + outputFile.getAbsolutePath() + " contains files.\n\nDo you want to overwrite the files?");
\r
102 if (!outputFile.delete()) {
\r
103 MessageDialog.openError(getShell(), "Delete Problem", "Could not overwrite previously existing file " + outputFile.getAbsolutePath());
\r
109 File outputFolder = exportModel.getExportLocation();
\r
111 if (outputFolder.exists()) {
\r
112 if (!outputFolder.isDirectory()) {
\r
113 MessageDialog.openError(getShell(), "Folder Problem", "Output target is not a folder " + outputFolder.getAbsolutePath());
\r
116 String files[] = outputFolder.list();
\r
117 if (files.length > 0) {
\r
118 if (!exportModel.isOverwrite()) {
\r
119 boolean ok = MessageDialog.openConfirm(getShell(), "Overwrite", "A folder by the name " + outputFolder.getAbsolutePath() + " contains files.\n\nDo you want to overwrite the files?");
\r
127 if (!outputFolder.mkdir()) {
\r
128 MessageDialog.openError(getShell(), "Folder Problem", "Could not create new folder " + outputFolder);
\r
135 getContainer().run(true, true,createExportRunnable(exportModel));
\r
136 } catch (InvocationTargetException e) {
\r
137 Throwable t = e.getTargetException();
\r
138 WizardPage cp = (WizardPage) getContainer().getCurrentPage();
\r
139 if (t instanceof IOException) {
\r
140 ErrorLogger.defaultLogError("An I/O problem occurred while exporting the model. See exception for details.", t);
\r
141 cp.setErrorMessage("An I/O problem occurred while exporting the model.\n\nMessage: " + e.getMessage());
\r
143 ErrorLogger.defaultLogError("Unexpected exception while exporting the model. See exception for details.", t);
\r
144 cp.setErrorMessage("Unexpected exception while exporting the model. See error log for details.\n\nMessage: " + e.getMessage());
\r
147 } catch (InterruptedException e) {
\r
148 ExceptionUtils.logAndShowError(e);
\r
158 private boolean readPreferences() {
\r
159 IPreferenceStore store = getPreferenceStore();
\r
161 String recentPathsPref = store.getString(getExportLocationId());
\r
162 recentExportPaths = decodePaths(recentPathsPref);
\r
163 overwrite = store.getBoolean(getExportOverwriteId());
\r
168 private void writePreferences() throws IOException {
\r
169 IPersistentPreferenceStore store = getPreferenceStore();
\r
171 store.putValue(getExportLocationId(), encodePaths(recentExportPaths));
\r
172 store.setValue(getExportOverwriteId(), exportModel.isOverwrite());
\r
174 if (store.needsSaving())
\r
178 private static final String TAG_PATH = "path";
\r
179 private static final String ATTR_NAME = "name";
\r
182 public static Deque<String> decodePaths(String recentPathsPref) {
\r
183 Deque<String> result = new LinkedList<String>();
\r
185 StringMemento sm = new StringMemento(recentPathsPref);
\r
186 for (IMemento m : sm.getChildren(TAG_PATH)) {
\r
187 String name = m.getString(ATTR_NAME);
\r
188 if (name != null && !name.isEmpty())
\r
191 } catch (IllegalArgumentException e) {
\r
196 public static String encodePaths(Deque<String> recentPaths) {
\r
197 StringMemento sm = new StringMemento();
\r
198 for (String path : recentPaths) {
\r
199 IMemento m = sm.createChild(TAG_PATH);
\r
200 m.putString(ATTR_NAME, path);
\r
202 return sm.toString();
\r
205 public static <T> void removeDuplicates(Iterable<String> iter) {
\r
206 // Remove duplicates
\r
207 Set<String> dups = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER);
\r
208 for (Iterator<String> it = iter.iterator(); it.hasNext();) {
\r
209 String path = it.next();
\r
210 if (!dups.add(path)) {
\r