]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.workbench/src/org/simantics/workbench/internal/dialogs/RecentWorkspacesPreferencePage.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.workbench / src / org / simantics / workbench / internal / dialogs / RecentWorkspacesPreferencePage.java
1 package org.simantics.workbench.internal.dialogs;
2
3 import java.util.ArrayList;
4 import java.util.Arrays;
5
6 import org.eclipse.core.runtime.Platform;
7 import org.eclipse.jface.dialogs.Dialog;
8 import org.eclipse.jface.preference.PreferencePage;
9 import org.eclipse.osgi.util.TextProcessor;
10 import org.eclipse.swt.SWT;
11 import org.eclipse.swt.events.SelectionAdapter;
12 import org.eclipse.swt.events.SelectionEvent;
13 import org.eclipse.swt.layout.GridData;
14 import org.eclipse.swt.layout.GridLayout;
15 import org.eclipse.swt.widgets.Button;
16 import org.eclipse.swt.widgets.Composite;
17 import org.eclipse.swt.widgets.Control;
18 import org.eclipse.swt.widgets.Group;
19 import org.eclipse.swt.widgets.Label;
20 import org.eclipse.swt.widgets.List;
21 import org.eclipse.swt.widgets.Spinner;
22 import org.eclipse.ui.IWorkbench;
23 import org.eclipse.ui.IWorkbenchPreferencePage;
24 import org.eclipse.ui.PlatformUI;
25 import org.eclipse.ui.internal.IWorkbenchHelpContextIds;
26 import org.eclipse.ui.internal.ide.ChooseWorkspaceData;
27 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
28
29 /**
30  * Preference page for editing the list of recent workspaces and whether or not
31  * the user is prompted at startup.
32  * 
33  * @since 3.5
34  */
35 public class RecentWorkspacesPreferencePage extends PreferencePage
36 implements IWorkbenchPreferencePage {
37
38     private static final int MIN_WORKSPACS = 5;
39     private static final int MAX_WORKSPACES = 99;
40     private static final int MAX_WORKSPACES_DIGIT_COUNT = 2;
41
42     private ChooseWorkspaceData workspacesData;
43
44     private Button promptOption;
45     private Spinner maxWorkspacesField;
46     private List workspacesList;
47     private Button removeButton;
48
49
50     @Override
51     public void init(IWorkbench workbench) {
52         workspacesData = new ChooseWorkspaceData(Platform.getInstanceLocation().getURL());
53     }
54
55     @Override
56     public Control createContents(Composite parent) {
57         PlatformUI.getWorkbench().getHelpSystem().setHelp(parent,
58                 IWorkbenchHelpContextIds.WORKSPACES_PREFERENCE_PAGE);
59
60         Composite container = new Composite(parent, SWT.NULL);
61         final GridLayout gridLayout = new GridLayout();
62         gridLayout.numColumns = 2;
63         gridLayout.marginHeight = 0;
64         gridLayout.marginWidth = 0;
65         container.setLayout(gridLayout);
66
67         createPromptOption(container);
68         createMaxWorkspacesField(container);
69         createWorkspacesList(container);
70
71         Dialog.applyDialogFont(container);
72
73         return container;
74     }
75
76
77     protected void createPromptOption(Composite parent) {
78         promptOption = new Button(parent, SWT.CHECK);
79         promptOption.setText(IDEWorkbenchMessages.RecentWorkspacesPreferencePage_PromptAtStartup_label);
80         promptOption.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));
81
82         promptOption.setSelection(workspacesData.getShowDialog());
83         promptOption.addSelectionListener(new SelectionAdapter(){
84             @Override
85             public void widgetSelected(SelectionEvent event) {
86                 workspacesData.toggleShowDialog();
87             }
88         });
89     }
90
91
92     protected void createMaxWorkspacesField(Composite parent) {
93         final Label maxWorkspacesLabel = new Label(parent, SWT.NONE);
94         maxWorkspacesLabel.setText(IDEWorkbenchMessages.RecentWorkspacesPreferencePage_NumberOfWorkspaces_label);
95         maxWorkspacesField = new Spinner(parent, SWT.BORDER);
96         maxWorkspacesField.setTextLimit(MAX_WORKSPACES_DIGIT_COUNT);
97         maxWorkspacesField.setMinimum(MIN_WORKSPACS);
98         maxWorkspacesField.setMaximum(MAX_WORKSPACES);
99
100         maxWorkspacesField.setSelection(workspacesData.getRecentWorkspaces().length);
101     }
102
103
104     protected void createWorkspacesList(Composite parent) {
105         final Group recentWorkspacesGroup = new Group(parent, SWT.NONE);
106         recentWorkspacesGroup.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1));
107         recentWorkspacesGroup.setText(IDEWorkbenchMessages.RecentWorkspacesPreferencePage_RecentWorkspacesList_label);
108         final GridLayout gridLayout_1 = new GridLayout();
109         gridLayout_1.numColumns = 2;
110         recentWorkspacesGroup.setLayout(gridLayout_1);
111
112         workspacesList = new List(recentWorkspacesGroup, SWT.BORDER | SWT.MULTI);
113         final GridData gd_workspacesList = new GridData(SWT.FILL, SWT.FILL, true, true);
114         workspacesList.setLayoutData(gd_workspacesList);
115
116         removeButton = new Button(recentWorkspacesGroup, SWT.NONE);
117         final GridData gd_removeButton = new GridData(SWT.CENTER, SWT.TOP, false, false);
118         removeButton.setLayoutData(gd_removeButton);
119         removeButton.setText(IDEWorkbenchMessages.RecentWorkspacesPreferencePage_RemoveButton_label);
120         removeButton.setEnabled(false);
121
122         removeButton.addSelectionListener(new SelectionAdapter(){
123             @Override
124             public void widgetSelected(SelectionEvent event) {
125                 removeSelectedWorkspaces();
126                 updateRemoveButton();
127             }
128         });
129
130         workspacesList.addSelectionListener(new SelectionAdapter() {
131             @Override
132             public void widgetSelected(SelectionEvent event) {
133                 updateRemoveButton();
134             }
135         });
136
137         String[] recentWorkspaces = workspacesData.getRecentWorkspaces();
138         for (int i = 0; i < recentWorkspaces.length; i++) {
139             String aWorkspace = recentWorkspaces[i];
140             if (aWorkspace != null) {
141                 workspacesList.add(TextProcessor.process(aWorkspace));
142             }
143         }
144     }
145
146
147     protected void removeSelectedWorkspaces() {
148         // This would be a lot less code if we could use Jakarta CollectionUtils and/or ArrayUtils
149
150         int[] selected = workspacesList.getSelectionIndices();
151         java.util.List<String> workspaces = new ArrayList<String>(Arrays.asList(workspacesList.getItems()));
152
153         // Iterate bottom-up because removal changes indices in the list
154         for (int i = selected.length-1; i >= 0; i--) {
155             workspaces.remove(selected[i]);
156         }
157
158         String[] newItems = new String[workspaces.size()];
159         workspaces.toArray(newItems);
160         workspacesList.setItems(newItems);
161     }
162
163
164     @Override
165     protected void performDefaults() {
166         promptOption.setSelection(true);
167         super.performDefaults();
168     }
169
170
171     @Override
172     public boolean performOk() {
173         int maxWorkspaces = maxWorkspacesField.getSelection();
174         String[] workspaces = new String[maxWorkspaces];
175         String[] tmpListItem = workspacesList.getItems();
176         String[] listItems = new String[tmpListItem.length];
177
178         for (int i = 0; i < tmpListItem.length; i++){
179             listItems[i] = TextProcessor.deprocess(tmpListItem[i]);
180         }
181
182         if (maxWorkspaces < listItems.length) {
183             // TODO: maybe alert the user that the list will be truncated?
184             System.arraycopy(listItems, 0, workspaces, 0, maxWorkspaces);
185         } else  {
186             System.arraycopy(listItems, 0, workspaces, 0, listItems.length);
187         }
188
189         workspacesData.setRecentWorkspaces(workspaces);
190         workspacesData.writePersistedData();
191         return true;
192     }
193
194
195     protected void updateRemoveButton() {
196         removeButton.setEnabled(workspacesList.getSelectionCount() > 0);
197     }
198
199 }