|
Added
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2004 - 2006 Mylar committers 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 |
|
| 9 |
package org.eclipse.mylar.internal.tasks.ui.workingset; |
| 10 |
|
| 11 |
import java.util.ArrayList; |
| 12 |
import java.util.List; |
| 13 |
import java.util.Set; |
| 14 |
|
| 15 |
import org.eclipse.core.runtime.IAdaptable; |
| 16 |
import org.eclipse.jface.dialogs.Dialog; |
| 17 |
import org.eclipse.jface.dialogs.IDialogConstants; |
| 18 |
import org.eclipse.jface.viewers.CheckStateChangedEvent; |
| 19 |
import org.eclipse.jface.viewers.CheckboxTreeViewer; |
| 20 |
import org.eclipse.jface.viewers.ICheckStateListener; |
| 21 |
import org.eclipse.jface.viewers.ITreeContentProvider; |
| 22 |
import org.eclipse.jface.viewers.Viewer; |
| 23 |
import org.eclipse.jface.wizard.WizardPage; |
| 24 |
import org.eclipse.mylar.tasks.core.AbstractTaskContainer; |
| 25 |
import org.eclipse.mylar.tasks.core.ITaskListElement; |
| 26 |
import org.eclipse.mylar.tasks.ui.TasksUiPlugin; |
| 27 |
import org.eclipse.swt.SWT; |
| 28 |
import org.eclipse.swt.custom.BusyIndicator; |
| 29 |
import org.eclipse.swt.events.ModifyEvent; |
| 30 |
import org.eclipse.swt.events.ModifyListener; |
| 31 |
import org.eclipse.swt.events.SelectionAdapter; |
| 32 |
import org.eclipse.swt.events.SelectionEvent; |
| 33 |
import org.eclipse.swt.layout.GridData; |
| 34 |
import org.eclipse.swt.layout.GridLayout; |
| 35 |
import org.eclipse.swt.widgets.Button; |
| 36 |
import org.eclipse.swt.widgets.Composite; |
| 37 |
import org.eclipse.swt.widgets.Label; |
| 38 |
import org.eclipse.swt.widgets.Text; |
| 39 |
import org.eclipse.ui.IWorkingSet; |
| 40 |
import org.eclipse.ui.IWorkingSetManager; |
| 41 |
import org.eclipse.ui.PlatformUI; |
| 42 |
import org.eclipse.ui.dialogs.IWorkingSetPage; |
| 43 |
import org.eclipse.ui.internal.ide.IDEWorkbenchMessages; |
| 44 |
|
| 45 |
/** |
| 46 |
* Adapted from org.eclipse.ui.internal.ide.dialogs.ResourceWorkingSetPage |
| 47 |
*/ |
| 48 |
public class TaskWorkingSetPage extends WizardPage implements IWorkingSetPage { |
| 49 |
|
| 50 |
private final static int SIZING_SELECTION_WIDGET_WIDTH = 50; |
| 51 |
|
| 52 |
private final static int SIZING_SELECTION_WIDGET_HEIGHT = 200; |
| 53 |
|
| 54 |
private Text text; |
| 55 |
|
| 56 |
private CheckboxTreeViewer tree; |
| 57 |
|
| 58 |
private IWorkingSet workingSet; |
| 59 |
|
| 60 |
private boolean firstCheck = false; |
| 61 |
|
| 62 |
|
| 63 |
public TaskWorkingSetPage() { |
| 64 |
super("taskWorkingSetPage", //$NON-NLS-1$ |
| 65 |
"Task Working Set", null); // the icon |
| 66 |
setDescription("Enter a working set name and select task categories/queries."); |
| 67 |
} |
| 68 |
|
| 69 |
@SuppressWarnings("unchecked") |
| 70 |
public void finish() { |
| 71 |
// List<AbstractTaskContainer> elements = getCheckedElements((List<AbstractTaskContainer>) tree.getInput()); |
| 72 |
Object[] elements = tree.getCheckedElements(); |
| 73 |
IAdaptable[] adaptables = new IAdaptable[elements.length]; |
| 74 |
for (int i = 0; i < adaptables.length; i++) { |
| 75 |
adaptables[i] = (IAdaptable) elements[i]; |
| 76 |
} |
| 77 |
|
| 78 |
if (workingSet == null) { |
| 79 |
IWorkingSetManager workingSetManager = PlatformUI.getWorkbench().getWorkingSetManager(); |
| 80 |
workingSet = workingSetManager.createWorkingSet(getWorkingSetName(), adaptables); |
| 81 |
} else { |
| 82 |
workingSet.setName(getWorkingSetName()); |
| 83 |
workingSet.setElements(adaptables); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
public IWorkingSet getSelection() { |
| 88 |
return workingSet; |
| 89 |
} |
| 90 |
|
| 91 |
public void setSelection(IWorkingSet workingSet) { |
| 92 |
this.workingSet = workingSet; |
| 93 |
if (getShell() != null && text != null) { |
| 94 |
firstCheck = true; |
| 95 |
initializeCheckedState(); |
| 96 |
text.setText(workingSet.getName()); |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
private String getWorkingSetName() { |
| 101 |
return text.getText(); |
| 102 |
} |
| 103 |
|
| 104 |
public void createControl(Composite parent) { |
| 105 |
initializeDialogUnits(parent); |
| 106 |
|
| 107 |
Composite composite = new Composite(parent, SWT.NULL); |
| 108 |
|
| 109 |
GridLayout layout = new GridLayout(); |
| 110 |
layout.marginWidth = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN); |
| 111 |
layout.marginHeight = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_MARGIN); |
| 112 |
layout.verticalSpacing = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING); |
| 113 |
composite.setLayout(layout); |
| 114 |
composite.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL)); |
| 115 |
setControl(composite); |
| 116 |
|
| 117 |
// PlatformUI.getWorkbench().getHelpSystem().setHelp(composite, IIDEHelpContextIds.WORKING_SET_RESOURCE_PAGE); |
| 118 |
Label label = new Label(composite, SWT.WRAP); |
| 119 |
label.setText(IDEWorkbenchMessages.ResourceWorkingSetPage_message); |
| 120 |
label.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL |
| 121 |
| GridData.HORIZONTAL_ALIGN_FILL |
| 122 |
| GridData.VERTICAL_ALIGN_CENTER)); |
| 123 |
|
| 124 |
text = new Text(composite, SWT.SINGLE | SWT.BORDER); |
| 125 |
text.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL |
| 126 |
| GridData.HORIZONTAL_ALIGN_FILL)); |
| 127 |
text.addModifyListener(new ModifyListener() { |
| 128 |
public void modifyText(ModifyEvent e) { |
| 129 |
validateInput(); |
| 130 |
} |
| 131 |
}); |
| 132 |
text.setFocus(); |
| 133 |
// text.setBackground(FieldAssistColors.getRequiredFieldBackgroundColor(text)); |
| 134 |
|
| 135 |
label = new Label(composite, SWT.WRAP); |
| 136 |
label.setText(IDEWorkbenchMessages.ResourceWorkingSetPage_label_tree); |
| 137 |
label.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL |
| 138 |
| GridData.HORIZONTAL_ALIGN_FILL |
| 139 |
| GridData.VERTICAL_ALIGN_CENTER)); |
| 140 |
|
| 141 |
tree = new CheckboxTreeViewer(composite); |
| 142 |
tree.setUseHashlookup(true); |
| 143 |
|
| 144 |
final ITreeContentProvider treeContentProvider = new ITreeContentProvider() { |
| 145 |
|
| 146 |
@SuppressWarnings("unchecked") |
| 147 |
public Object[] getChildren(Object parentElement) { |
| 148 |
if(parentElement instanceof List) { |
| 149 |
List containers = (List) parentElement; |
| 150 |
return containers.toArray(new Object[containers.size()]); |
| 151 |
} |
| 152 |
return new Object[0]; |
| 153 |
} |
| 154 |
|
| 155 |
@SuppressWarnings("unchecked") |
| 156 |
public boolean hasChildren(Object element) { |
| 157 |
return getChildren(element).length > 0; |
| 158 |
} |
| 159 |
|
| 160 |
public Object[] getElements(Object element) { |
| 161 |
return getChildren(element); |
| 162 |
} |
| 163 |
|
| 164 |
public Object getParent(Object element) { |
| 165 |
return null; |
| 166 |
} |
| 167 |
|
| 168 |
public void dispose() { |
| 169 |
} |
| 170 |
|
| 171 |
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { |
| 172 |
} |
| 173 |
}; |
| 174 |
|
| 175 |
tree.setContentProvider(treeContentProvider); |
| 176 |
|
| 177 |
// XXX get real label provider from the task list |
| 178 |
// tree.setLabelProvider(new TaskListLabelProvider()); |
| 179 |
|
| 180 |
ArrayList<Object> containers = new ArrayList<Object>(); |
| 181 |
for (ITaskListElement element : (Set<ITaskListElement>) TasksUiPlugin.getTaskListManager().getTaskList().getRootElements()) { |
| 182 |
if (element instanceof AbstractTaskContainer) { |
| 183 |
containers.add(element); |
| 184 |
} |
| 185 |
} |
| 186 |
tree.setInput(containers); |
| 187 |
|
| 188 |
// tree.setComparator(new ResourceComparator(ResourceComparator.NAME)); |
| 189 |
|
| 190 |
GridData data = new GridData(GridData.FILL_BOTH | GridData.GRAB_VERTICAL); |
| 191 |
data.heightHint = SIZING_SELECTION_WIDGET_HEIGHT; |
| 192 |
data.widthHint = SIZING_SELECTION_WIDGET_WIDTH; |
| 193 |
tree.getControl().setLayoutData(data); |
| 194 |
|
| 195 |
tree.addCheckStateListener(new ICheckStateListener() { |
| 196 |
public void checkStateChanged(CheckStateChangedEvent event) { |
| 197 |
handleCheckStateChange(event); |
| 198 |
} |
| 199 |
}); |
| 200 |
|
| 201 |
// tree.addTreeListener(new ITreeViewerListener() { |
| 202 |
// public void treeCollapsed(TreeExpansionEvent event) { |
| 203 |
// } |
| 204 |
// |
| 205 |
// public void treeExpanded(TreeExpansionEvent event) { |
| 206 |
// final Object element = event.getElement(); |
| 207 |
// if (tree.getGrayed(element) == false) { |
| 208 |
// BusyIndicator.showWhile(getShell().getDisplay(), |
| 209 |
// new Runnable() { |
| 210 |
// public void run() { |
| 211 |
// setSubtreeChecked((IContainer) element, |
| 212 |
// tree.getChecked(element), false); |
| 213 |
// } |
| 214 |
// }); |
| 215 |
// } |
| 216 |
// } |
| 217 |
// }); |
| 218 |
|
| 219 |
// Add select / deselect all buttons for bug 46669 |
| 220 |
Composite buttonComposite = new Composite(composite, SWT.NONE); |
| 221 |
layout = new GridLayout(2, false); |
| 222 |
layout.marginWidth = 0; |
| 223 |
layout.marginHeight = 0; |
| 224 |
layout.horizontalSpacing = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING); |
| 225 |
buttonComposite.setLayout(layout); |
| 226 |
buttonComposite.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL)); |
| 227 |
|
| 228 |
Button selectAllButton = new Button(buttonComposite, SWT.PUSH); |
| 229 |
selectAllButton.setText(IDEWorkbenchMessages.ResourceWorkingSetPage_selectAll_label); |
| 230 |
selectAllButton.setToolTipText(IDEWorkbenchMessages.ResourceWorkingSetPage_selectAll_toolTip); |
| 231 |
selectAllButton.addSelectionListener(new SelectionAdapter() { |
| 232 |
public void widgetSelected(SelectionEvent selectionEvent) { |
| 233 |
tree.setCheckedElements(treeContentProvider.getElements(tree.getInput())); |
| 234 |
validateInput(); |
| 235 |
} |
| 236 |
}); |
| 237 |
setButtonLayoutData(selectAllButton); |
| 238 |
|
| 239 |
Button deselectAllButton = new Button(buttonComposite, SWT.PUSH); |
| 240 |
deselectAllButton.setText(IDEWorkbenchMessages.ResourceWorkingSetPage_deselectAll_label); |
| 241 |
deselectAllButton.setToolTipText(IDEWorkbenchMessages.ResourceWorkingSetPage_deselectAll_toolTip); |
| 242 |
deselectAllButton.addSelectionListener(new SelectionAdapter() { |
| 243 |
public void widgetSelected(SelectionEvent selectionEvent) { |
| 244 |
tree.setCheckedElements(new Object[0]); |
| 245 |
validateInput(); |
| 246 |
} |
| 247 |
}); |
| 248 |
setButtonLayoutData(deselectAllButton); |
| 249 |
|
| 250 |
initializeCheckedState(); |
| 251 |
if (workingSet != null) { |
| 252 |
text.setText(workingSet.getName()); |
| 253 |
} |
| 254 |
setPageComplete(false); |
| 255 |
|
| 256 |
Dialog.applyDialogFont(composite); |
| 257 |
} |
| 258 |
|
| 259 |
private void initializeCheckedState() { |
| 260 |
BusyIndicator.showWhile(getShell().getDisplay(), new Runnable() { |
| 261 |
public void run() { |
| 262 |
Object[] items = null; |
| 263 |
if (workingSet != null) { |
| 264 |
items = workingSet.getElements(); |
| 265 |
if (items != null) { |
| 266 |
tree.setCheckedElements(items); |
| 267 |
} |
| 268 |
} |
| 269 |
} |
| 270 |
}); |
| 271 |
} |
| 272 |
|
| 273 |
protected void handleCheckStateChange(final CheckStateChangedEvent event) { |
| 274 |
BusyIndicator.showWhile(getShell().getDisplay(), new Runnable() { |
| 275 |
public void run() { |
| 276 |
AbstractTaskContainer element = (AbstractTaskContainer) event.getElement(); |
| 277 |
tree.setGrayed(element, false); |
| 278 |
|
| 279 |
// boolean state = event.getChecked(); |
| 280 |
// if (element instanceof AbstractTaskContainer) { |
| 281 |
// setSubtreeChecked((AbstractTaskContainer) element, state, true); |
| 282 |
// } |
| 283 |
// updateParentState(element); |
| 284 |
validateInput(); |
| 285 |
} |
| 286 |
}); |
| 287 |
} |
| 288 |
|
| 289 |
protected void validateInput() { |
| 290 |
String errorMessage = null; |
| 291 |
String infoMessage= null; |
| 292 |
String newText = text.getText(); |
| 293 |
|
| 294 |
if (!newText.equals(newText.trim())) { |
| 295 |
errorMessage = "The name must not have a leading or trailing whitespace."; |
| 296 |
} else if (firstCheck) { |
| 297 |
firstCheck = false; |
| 298 |
return; |
| 299 |
} |
| 300 |
if ("".equals(newText)) { //$NON-NLS-1$ |
| 301 |
errorMessage = "The name must not be empty."; |
| 302 |
} |
| 303 |
if (errorMessage == null |
| 304 |
&& (workingSet == null || !newText.equals(workingSet.getName()))) { |
| 305 |
IWorkingSet[] workingSets = PlatformUI.getWorkbench().getWorkingSetManager().getWorkingSets(); |
| 306 |
for (int i = 0; i < workingSets.length; i++) { |
| 307 |
if (newText.equals(workingSets[i].getName())) { |
| 308 |
errorMessage = "A working set with the same name already exists."; |
| 309 |
} |
| 310 |
} |
| 311 |
} |
| 312 |
if (infoMessage == null && tree.getCheckedElements().length == 0) { |
| 313 |
infoMessage = "No categories/queries selected."; |
| 314 |
} |
| 315 |
setMessage(infoMessage, INFORMATION); |
| 316 |
setErrorMessage(errorMessage); |
| 317 |
setPageComplete(errorMessage == null); |
| 318 |
} |
| 319 |
|
| 320 |
} |
| 321 |
|