]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.team.ui/src/org/simantics/team/ui/StageInitWizard.java
Removed contact application support prints
[simantics/platform.git] / bundles / org.simantics.team.ui / src / org / simantics / team / ui / StageInitWizard.java
1 package org.simantics.team.ui;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.util.Deque;
6 import java.util.LinkedList;
7
8 import org.eclipse.core.runtime.preferences.InstanceScope;
9 import org.eclipse.jface.dialogs.Dialog;
10 import org.eclipse.jface.preference.IPersistentPreferenceStore;
11 import org.eclipse.jface.preference.IPreferenceStore;
12 import org.eclipse.jface.viewers.IStructuredSelection;
13 import org.eclipse.jface.wizard.Wizard;
14 import org.eclipse.jface.wizard.WizardDialog;
15 import org.eclipse.swt.widgets.Display;
16 import org.eclipse.ui.IImportWizard;
17 import org.eclipse.ui.IWorkbench;
18 import org.eclipse.ui.PlatformUI;
19 import org.eclipse.ui.preferences.ScopedPreferenceStore;
20 import org.simantics.db.common.utils.Logger;
21 import org.simantics.db.exception.DatabaseException;
22 import org.simantics.team.Activator;
23 import org.simantics.team.Utils;
24 import org.simantics.utils.ui.ErrorLogger;
25
26 public class StageInitWizard extends Wizard implements IImportWizard {
27     static public class Data {
28         public Boolean ok = false;
29         public Boolean requireExisting = false;
30         public String comment; // Initial comment.
31         Deque<String> recentLocations = new LinkedList<String>();;
32         final File stagingFolder;
33         public File teamFolder;
34         public Data(File stagingFolder, File teamFolder) {
35             this.stagingFolder = stagingFolder;
36             this.teamFolder = teamFolder;
37         }
38     }
39     public static boolean openInitWizard(final Data data)
40     throws DatabaseException {
41         final Display display = PlatformUI.getWorkbench().getDisplay();
42         display.syncExec(new Runnable() {
43             @Override
44             public void run() {
45                 StageInitWizard stageInitWizard = new StageInitWizard(data);
46                 WizardDialog dialog = new WizardDialog(display.getActiveShell(), stageInitWizard);
47                 if (Dialog.OK != dialog.open())
48                     data.ok = false;
49                 else
50                     data.ok = true;
51             }});
52         return data.ok;
53     }
54     private final Data data;
55     private static final int MAX_RECENT_IMPORT_PATHS = 10;
56     private boolean readPreferences(IStructuredSelection selection) {
57         IPreferenceStore store = new ScopedPreferenceStore(InstanceScope.INSTANCE, Activator.PLUGIN_ID);
58         try {
59             data.teamFolder = Utils.getTeamFolder();
60             if (null == data.teamFolder)
61                 data.teamFolder = new File("Default Team Folder");
62         } catch (DatabaseException e) {
63             Logger.defaultLogError("Failed to get team folder.", e);
64             data.teamFolder = new File("Default Team Folder");
65         }
66         String recentPathsPref = store.getString(Preferences.RECENT_TEAM_FOLDERS);
67         data.recentLocations = Preferences.decodePaths(recentPathsPref);
68         return true;
69     }
70     private void writePreferences() throws IOException {
71         IPersistentPreferenceStore store = new ScopedPreferenceStore(InstanceScope.INSTANCE, Activator.PLUGIN_ID);
72         store.putValue(Preferences.CURRENT_TEAM_FOLDER, data.teamFolder.getAbsolutePath());
73         store.putValue(Preferences.RECENT_TEAM_FOLDERS, Preferences.encodePaths(data.recentLocations));
74         if (store.needsSaving())
75             store.save();
76     }
77     public StageInitWizard(Data data) {
78         this.data = data;
79         setWindowTitle("Staging Init");
80         setNeedsProgressMonitor(true);
81     }
82     @Override
83     public void init(IWorkbench workbench, IStructuredSelection selection) {
84         readPreferences(selection);
85     }
86     @Override
87     public void addPages() {
88         super.addPages();
89         addPage(new StageInitPage(data));
90     }
91     @Override
92     public boolean performFinish() {
93         try {
94             data.recentLocations.addFirst(data.teamFolder.getAbsolutePath());
95             Preferences.removeDuplicates(data.recentLocations);
96             if (data.recentLocations.size() > MAX_RECENT_IMPORT_PATHS)
97                 data.recentLocations.pollLast();
98             writePreferences();
99         } catch (IOException e) {
100             ErrorLogger.defaultLogError("Failed to write preferences", e);
101         }
102         return true;
103     }
104 }