1 package org.simantics.workbench.internal.dialogs;
3 import java.util.ArrayList;
4 import java.util.Arrays;
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;
30 * Preference page for editing the list of recent workspaces and whether or not
31 * the user is prompted at startup.
35 public class RecentWorkspacesPreferencePage extends PreferencePage
36 implements IWorkbenchPreferencePage {
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;
42 private ChooseWorkspaceData workspacesData;
44 private Button promptOption;
45 private Spinner maxWorkspacesField;
46 private List workspacesList;
47 private Button removeButton;
51 public void init(IWorkbench workbench) {
52 workspacesData = new ChooseWorkspaceData(Platform.getInstanceLocation().getURL());
56 public Control createContents(Composite parent) {
57 PlatformUI.getWorkbench().getHelpSystem().setHelp(parent,
58 IWorkbenchHelpContextIds.WORKSPACES_PREFERENCE_PAGE);
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);
67 createPromptOption(container);
68 createMaxWorkspacesField(container);
69 createWorkspacesList(container);
71 Dialog.applyDialogFont(container);
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));
82 promptOption.setSelection(workspacesData.getShowDialog());
83 promptOption.addSelectionListener(new SelectionAdapter(){
85 public void widgetSelected(SelectionEvent event) {
86 workspacesData.toggleShowDialog();
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);
100 maxWorkspacesField.setSelection(workspacesData.getRecentWorkspaces().length);
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);
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);
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);
122 removeButton.addSelectionListener(new SelectionAdapter(){
124 public void widgetSelected(SelectionEvent event) {
125 removeSelectedWorkspaces();
126 updateRemoveButton();
130 workspacesList.addSelectionListener(new SelectionAdapter() {
132 public void widgetSelected(SelectionEvent event) {
133 updateRemoveButton();
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));
147 protected void removeSelectedWorkspaces() {
148 // This would be a lot less code if we could use Jakarta CollectionUtils and/or ArrayUtils
150 int[] selected = workspacesList.getSelectionIndices();
151 java.util.List<String> workspaces = new ArrayList<String>(Arrays.asList(workspacesList.getItems()));
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]);
158 String[] newItems = new String[workspaces.size()];
159 workspaces.toArray(newItems);
160 workspacesList.setItems(newItems);
165 protected void performDefaults() {
166 promptOption.setSelection(true);
167 super.performDefaults();
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];
178 for (int i = 0; i < tmpListItem.length; i++){
179 listItems[i] = TextProcessor.deprocess(tmpListItem[i]);
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);
186 System.arraycopy(listItems, 0, workspaces, 0, listItems.length);
189 workspacesData.setRecentWorkspaces(workspaces);
190 workspacesData.writePersistedData();
195 protected void updateRemoveButton() {
196 removeButton.setEnabled(workspacesList.getSelectionCount() > 0);