|
Added
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2009 Eric Rizzo and others. |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* http://www.eclipse.org/legal/epl-v10.html |
| 7 |
* |
| 8 |
* Contributors: |
| 9 |
* Eric Rizzo - initial API and implementation |
| 10 |
*******************************************************************************/ |
| 11 |
package org.eclipse.ui.internal.ide.application.dialogs; |
| 12 |
|
| 13 |
import java.util.ArrayList; |
| 14 |
import java.util.Arrays; |
| 15 |
|
| 16 |
import org.eclipse.core.runtime.Platform; |
| 17 |
import org.eclipse.jface.preference.PreferencePage; |
| 18 |
import org.eclipse.swt.SWT; |
| 19 |
import org.eclipse.swt.events.SelectionAdapter; |
| 20 |
import org.eclipse.swt.events.SelectionEvent; |
| 21 |
import org.eclipse.swt.layout.GridData; |
| 22 |
import org.eclipse.swt.layout.GridLayout; |
| 23 |
import org.eclipse.swt.widgets.Button; |
| 24 |
import org.eclipse.swt.widgets.Composite; |
| 25 |
import org.eclipse.swt.widgets.Control; |
| 26 |
import org.eclipse.swt.widgets.Group; |
| 27 |
import org.eclipse.swt.widgets.Label; |
| 28 |
import org.eclipse.swt.widgets.List; |
| 29 |
import org.eclipse.swt.widgets.Spinner; |
| 30 |
import org.eclipse.ui.IWorkbench; |
| 31 |
import org.eclipse.ui.IWorkbenchPreferencePage; |
| 32 |
import org.eclipse.ui.internal.ide.ChooseWorkspaceData; |
| 33 |
|
| 34 |
|
| 35 |
/** |
| 36 |
* Preference page for editing the list of recent workspaces and whether or not |
| 37 |
* the user is prompted at startup. |
| 38 |
* |
| 39 |
* @since 3.5 |
| 40 |
*/ |
| 41 |
public class RecentWorkspacesPreferencePage extends PreferencePage |
| 42 |
implements IWorkbenchPreferencePage { |
| 43 |
|
| 44 |
private static final int MIN_WORKSPACS = 5; |
| 45 |
private static final int MAX_WORKSPACES = 99; |
| 46 |
private static final int MAX_WORKSPACES_DIGIT_COUNT = 2; |
| 47 |
|
| 48 |
private ChooseWorkspaceData workspacesData; |
| 49 |
|
| 50 |
private Button promptOption; |
| 51 |
private Spinner maxWorkspacesField; |
| 52 |
private List workspacesList; |
| 53 |
private Button removeButton; |
| 54 |
|
| 55 |
|
| 56 |
public void init(IWorkbench workbench) { |
| 57 |
workspacesData = new ChooseWorkspaceData(Platform.getInstanceLocation().getURL()); |
| 58 |
} |
| 59 |
|
| 60 |
public Control createContents(Composite parent) { |
| 61 |
Composite container = new Composite(parent, SWT.NULL); |
| 62 |
final GridLayout gridLayout = new GridLayout(); |
| 63 |
gridLayout.numColumns = 2; |
| 64 |
gridLayout.marginHeight = 0; |
| 65 |
gridLayout.marginWidth = 0; |
| 66 |
container.setLayout(gridLayout); |
| 67 |
|
| 68 |
createPromptOption(container); |
| 69 |
createMaxWorkspacesField(container); |
| 70 |
createWorkspacesList(container); |
| 71 |
|
| 72 |
return container; |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
protected void createPromptOption(Composite parent) { |
| 77 |
promptOption = new Button(parent, SWT.CHECK); |
| 78 |
promptOption.setText("Prompt for &workspace on startup"); //$NON-NLS-1$ |
| 79 |
promptOption.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1)); |
| 80 |
|
| 81 |
promptOption.setSelection(workspacesData.getShowDialog()); |
| 82 |
promptOption.addSelectionListener(new SelectionAdapter(){ |
| 83 |
public void widgetSelected(SelectionEvent event) { |
| 84 |
workspacesData.toggleShowDialog(); |
| 85 |
} |
| 86 |
}); |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
protected void createMaxWorkspacesField(Composite parent) { |
| 91 |
final Label maxWorkspacesLabel = new Label(parent, SWT.NONE); |
| 92 |
maxWorkspacesLabel.setText("&Number of recent workspaces to remember:"); //$NON-NLS-1$ |
| 93 |
maxWorkspacesField = new Spinner(parent, SWT.BORDER); |
| 94 |
maxWorkspacesField.setTextLimit(MAX_WORKSPACES_DIGIT_COUNT); |
| 95 |
maxWorkspacesField.setMinimum(MIN_WORKSPACS); |
| 96 |
maxWorkspacesField.setMaximum(MAX_WORKSPACES); |
| 97 |
|
| 98 |
maxWorkspacesField.setSelection(workspacesData.getRecentWorkspaces().length); |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
protected void createWorkspacesList(Composite parent) { |
| 103 |
final Group recentWorkspacesGroup = new Group(parent, SWT.NONE); |
| 104 |
recentWorkspacesGroup.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1)); |
| 105 |
recentWorkspacesGroup.setText("Recent workspaces"); //$NON-NLS-1$ |
| 106 |
final GridLayout gridLayout_1 = new GridLayout(); |
| 107 |
gridLayout_1.numColumns = 2; |
| 108 |
recentWorkspacesGroup.setLayout(gridLayout_1); |
| 109 |
|
| 110 |
workspacesList = new List(recentWorkspacesGroup, SWT.BORDER | SWT.MULTI); |
| 111 |
final GridData gd_workspacesList = new GridData(SWT.FILL, SWT.FILL, true, true); |
| 112 |
workspacesList.setLayoutData(gd_workspacesList); |
| 113 |
|
| 114 |
removeButton = new Button(recentWorkspacesGroup, SWT.NONE); |
| 115 |
final GridData gd_removeButton = new GridData(SWT.CENTER, SWT.TOP, false, false); |
| 116 |
removeButton.setLayoutData(gd_removeButton); |
| 117 |
removeButton.setText("&Remove"); //$NON-NLS-1$ |
| 118 |
removeButton.setEnabled(false); |
| 119 |
|
| 120 |
removeButton.addSelectionListener(new SelectionAdapter(){ |
| 121 |
public void widgetSelected(SelectionEvent event) { |
| 122 |
removeSelectedWorkspaces(); |
| 123 |
} |
| 124 |
}); |
| 125 |
|
| 126 |
workspacesList.addSelectionListener(new SelectionAdapter() { |
| 127 |
public void widgetSelected(SelectionEvent event) { |
| 128 |
removeButton.setEnabled(workspacesList.getSelectionCount() > 0); |
| 129 |
} |
| 130 |
}); |
| 131 |
|
| 132 |
String[] recentWorkspaces = workspacesData.getRecentWorkspaces(); |
| 133 |
for (int i = 0; i < recentWorkspaces.length; i++) { |
| 134 |
String aWorkspace = recentWorkspaces[i]; |
| 135 |
if (aWorkspace != null) { |
| 136 |
workspacesList.add(aWorkspace); |
| 137 |
} |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
protected void removeSelectedWorkspaces() { |
| 143 |
// This would be a lot less code if we could use Jakarta CollectionUtils and/or ArrayUtils |
| 144 |
|
| 145 |
int[] selected = workspacesList.getSelectionIndices(); |
| 146 |
java.util.List workspaces = new ArrayList(Arrays.asList(workspacesList.getItems())); |
| 147 |
|
| 148 |
// Iterate bottom-up because removal changes indices in the list |
| 149 |
for (int i = selected.length-1; i >= 0; i--) { |
| 150 |
workspaces.remove(selected[i]); |
| 151 |
} |
| 152 |
|
| 153 |
String[] newItems = new String[workspaces.size()]; |
| 154 |
workspaces.toArray(newItems); |
| 155 |
workspacesList.setItems(newItems); |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
protected void performDefaults() { |
| 160 |
promptOption.setSelection(true); |
| 161 |
super.performDefaults(); |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
public boolean performOk() { |
| 166 |
int maxWorkspaces = maxWorkspacesField.getSelection(); |
| 167 |
String[] workspaces = new String[maxWorkspaces]; |
| 168 |
String[] listItems = workspacesList.getItems(); |
| 169 |
|
| 170 |
if (maxWorkspaces < listItems.length) { |
| 171 |
// TODO: maybe alert the user that the list will be truncated? |
| 172 |
System.arraycopy(listItems, 0, workspaces, 0, maxWorkspaces); |
| 173 |
} else { |
| 174 |
System.arraycopy(listItems, 0, workspaces, 0, listItems.length); |
| 175 |
} |
| 176 |
|
| 177 |
workspacesData.setRecentWorkspaces(workspaces); |
| 178 |
workspacesData.writePersistedData(); |
| 179 |
return true; |
| 180 |
} |
| 181 |
|
| 182 |
} |