1 11 package org.eclipse.ui.internal.dialogs; 12 13 import java.util.ArrayList ; 14 import java.util.List ; 15 16 import org.eclipse.core.runtime.CoreException; 17 import org.eclipse.core.runtime.Path; 18 import org.eclipse.jface.dialogs.Dialog; 19 import org.eclipse.jface.viewers.DoubleClickEvent; 20 import org.eclipse.jface.viewers.ISelection; 21 import org.eclipse.jface.viewers.IStructuredSelection; 22 import org.eclipse.jface.viewers.StructuredSelection; 23 import org.eclipse.jface.viewers.TreeViewer; 24 import org.eclipse.jface.wizard.IWizardNode; 25 import org.eclipse.jface.wizard.IWizardPage; 26 import org.eclipse.swt.SWT; 27 import org.eclipse.swt.graphics.Font; 28 import org.eclipse.swt.layout.GridData; 29 import org.eclipse.swt.layout.GridLayout; 30 import org.eclipse.swt.widgets.Composite; 31 import org.eclipse.swt.widgets.Control; 32 import org.eclipse.swt.widgets.Label; 33 import org.eclipse.ui.IWorkbench; 34 import org.eclipse.ui.IWorkbenchWizard; 35 import org.eclipse.ui.activities.ITriggerPoint; 36 import org.eclipse.ui.activities.WorkbenchActivityHelper; 37 import org.eclipse.ui.dialogs.FilteredTree; 38 import org.eclipse.ui.internal.WorkbenchMessages; 39 import org.eclipse.ui.model.AdaptableList; 40 import org.eclipse.ui.model.WorkbenchLabelProvider; 41 import org.eclipse.ui.wizards.IWizardCategory; 42 import org.eclipse.ui.wizards.IWizardDescriptor; 43 44 50 public abstract class ImportExportPage extends WorkbenchWizardSelectionPage{ 51 protected static final String DIALOG_SETTING_SECTION_NAME = "ImportExportPage."; 53 private TreeViewer treeViewer; 55 56 59 protected class CategorizedWizardSelectionTree { 60 private final static int SIZING_LISTS_HEIGHT = 200; 61 62 private IWizardCategory wizardCategories; 63 private String message; 64 private TreeViewer viewer; 65 66 72 protected CategorizedWizardSelectionTree(IWizardCategory categories, String msg){ 73 this.wizardCategories = categories; 74 this.message = msg; 75 } 76 77 84 protected Composite createControl(Composite parent){ 85 Font font = parent.getFont(); 86 87 Composite outerContainer = new Composite(parent, SWT.NONE); 89 outerContainer.setLayout(new GridLayout()); 90 outerContainer.setLayoutData(new GridData(GridData.FILL_BOTH)); 91 outerContainer.setFont(font); 92 93 Label messageLabel = new Label(outerContainer, SWT.NONE); 94 if (message != null) { 95 messageLabel.setText(message); 96 } 97 messageLabel.setFont(font); 98 99 createFilteredTree(outerContainer); 100 layoutTopControl(viewer.getControl()); 101 102 return outerContainer; 103 } 104 105 110 private void createFilteredTree(Composite parent){ 111 FilteredTree filteredTree = new FilteredTree(parent, SWT.SINGLE | SWT.H_SCROLL 113 | SWT.V_SCROLL | SWT.BORDER, new WizardPatternFilter()); 114 viewer = filteredTree.getViewer(); 115 filteredTree.setFont(parent.getFont()); 116 117 viewer.setContentProvider(new WizardContentProvider()); 118 viewer.setLabelProvider(new WorkbenchLabelProvider()); 119 viewer.setComparator(DataTransferWizardCollectionComparator.INSTANCE); 120 121 ArrayList inputArray = new ArrayList (); 122 boolean expandTop = false; 123 124 if (wizardCategories != null) { 125 if (wizardCategories.getParent() == null) { 126 IWizardCategory [] children = wizardCategories.getCategories(); 127 for (int i = 0; i < children.length; i++) { 128 inputArray.add(children[i]); 129 } 130 } else { 131 expandTop = true; 132 inputArray.add(wizardCategories); 133 } 134 } 135 136 if (expandTop) { 138 viewer.setAutoExpandLevel(2); 139 } 140 141 AdaptableList input = new AdaptableList(inputArray); 142 143 viewer.addFilter(new WizardActivityFilter()); 145 146 viewer.setInput(input); 147 } 148 149 153 protected TreeViewer getViewer(){ 154 return viewer; 155 } 156 157 162 private void layoutTopControl(Control control) { 163 GridData data = new GridData(GridData.FILL_BOTH); 164 165 int availableRows = DialogUtil.availableRows(control.getParent()); 166 167 if (availableRows > 50) { 169 data.heightHint = SIZING_LISTS_HEIGHT; 170 } else { 171 data.heightHint = availableRows * 3; 172 } 173 174 control.setLayoutData(data); 175 } 176 } 177 178 184 protected ImportExportPage(IWorkbench aWorkbench, IStructuredSelection currentSelection){ 185 super("importExportPage", aWorkbench, currentSelection, null, null); setTitle(WorkbenchMessages.Select); 187 } 188 189 193 public void createControl(Composite parent) { 194 Font font = parent.getFont(); 195 196 Composite outerContainer = new Composite(parent, SWT.NONE); 198 outerContainer.setLayout(new GridLayout()); 199 outerContainer.setLayoutData(new GridData(GridData.FILL_BOTH)); 200 outerContainer.setFont(font); 201 202 Composite comp = createTreeViewer(outerContainer); 203 204 Dialog.applyDialogFont(comp); 205 206 restoreWidgetValues(); 207 208 setControl(outerContainer); 209 210 initialize(); 211 } 212 213 216 protected abstract Composite createTreeViewer(Composite parent); 217 218 224 protected void treeDoubleClicked(DoubleClickEvent event){ 225 ISelection selection = event.getViewer().getSelection(); 226 IStructuredSelection ss = (IStructuredSelection) selection; 227 listSelectionChanged(ss); 228 229 Object element = ss.getFirstElement(); 230 TreeViewer v = (TreeViewer)event.getViewer(); 231 if (v.isExpandable(element)) { 232 v.setExpandedState(element, !v.getExpandedState(element)); 233 } else if (element instanceof WorkbenchWizardElement) { 234 if (canFlipToNextPage()) { 235 getContainer().showPage(getNextPage()); 236 } 237 } 238 getContainer().showPage(getNextPage()); 239 } 240 241 244 private void updateSelectedNode(WorkbenchWizardElement wizardElement){ 245 setErrorMessage(null); 246 if (wizardElement == null) { 247 updateMessage(); 248 setSelectedNode(null); 249 return; 250 } 251 252 setSelectedNode(createWizardNode(wizardElement)); 253 setMessage(wizardElement.getDescription()); 254 } 255 256 260 protected void updateMessage(){ 261 TreeViewer viewer = getTreeViewer(); 262 if (viewer != null){ 263 ISelection selection = viewer.getSelection(); 264 IStructuredSelection ss = (IStructuredSelection) selection; 265 Object sel = ss.getFirstElement(); 266 if (sel instanceof WorkbenchWizardElement){ 267 updateSelectedNode((WorkbenchWizardElement)sel); 268 } 269 else{ 270 setSelectedNode(null); 271 } 272 } else { 273 setMessage(null); 274 } 275 } 276 277 282 protected void listSelectionChanged(ISelection selection){ 283 setErrorMessage(null); 284 IStructuredSelection ss = (IStructuredSelection) selection; 285 Object sel = ss.getFirstElement(); 286 if (sel instanceof WorkbenchWizardElement){ 287 WorkbenchWizardElement currentWizardSelection = (WorkbenchWizardElement) sel; 288 updateSelectedNode(currentWizardSelection); 289 } else { 290 updateSelectedNode(null); 291 } 292 } 293 294 297 private IWizardNode createWizardNode(IWizardDescriptor element) { 298 return new WorkbenchWizardNode(this, element) { 299 public IWorkbenchWizard createWizard() throws CoreException { 300 return wizardElement.createWizard(); 301 } 302 }; 303 } 304 305 309 protected void restoreWidgetValues() { 310 updateMessage(); 311 } 312 313 318 protected void expandPreviouslyExpandedCategories(String setting, IWizardCategory wizardCategories, TreeViewer viewer) { 319 String [] expandedCategoryPaths = getDialogSettings() 320 .getArray(setting); 321 if (expandedCategoryPaths == null || expandedCategoryPaths.length == 0) { 322 return; 323 } 324 325 List categoriesToExpand = new ArrayList (expandedCategoryPaths.length); 326 327 if (wizardCategories != null) { 328 for (int i = 0; i < expandedCategoryPaths.length; i++) { 329 IWizardCategory category = wizardCategories 330 .findCategory(new Path(expandedCategoryPaths[i])); 331 if (category != null) { 332 categoriesToExpand.add(category); 333 } 334 } 335 } 336 337 if (!categoriesToExpand.isEmpty()) { 338 viewer.setExpandedElements(categoriesToExpand.toArray()); 339 } 340 341 } 342 343 348 protected void selectPreviouslySelected(String setting, IWizardCategory wizardCategories, final TreeViewer viewer) { 349 String selectedId = getDialogSettings().get(setting); 350 if (selectedId == null) { 351 return; 352 } 353 354 if (wizardCategories == null) { 355 return; 356 } 357 358 Object selected = wizardCategories.findCategory(new Path( 359 selectedId)); 360 361 if (selected == null) { 362 selected = wizardCategories.findWizard(selectedId); 363 364 if (selected == null) { 365 return; 367 } 368 } 369 370 viewer.setSelection(new StructuredSelection(selected), true); 371 } 372 373 378 protected void storeExpandedCategories(String setting, TreeViewer viewer) { 379 Object [] expandedElements = viewer.getExpandedElements(); 380 List expandedElementPaths = new ArrayList (expandedElements.length); 381 for (int i = 0; i < expandedElements.length; ++i) { 382 if (expandedElements[i] instanceof IWizardCategory) { 383 expandedElementPaths 384 .add(((IWizardCategory) expandedElements[i]) 385 .getPath().toString()); 386 } 387 } 388 getDialogSettings().put(setting, 389 (String []) expandedElementPaths 390 .toArray(new String [expandedElementPaths.size()])); 391 } 392 393 397 protected void storeSelectedCategoryAndWizard(String setting, TreeViewer viewer) { 398 Object selected = ((IStructuredSelection) viewer 399 .getSelection()).getFirstElement(); 400 401 if (selected != null) { 402 if (selected instanceof IWizardCategory) { 403 getDialogSettings().put(setting, 404 ((IWizardCategory) selected).getPath() 405 .toString()); 406 } else { 407 getDialogSettings().put(setting, 409 ((IWizardDescriptor) selected).getId()); 410 } 411 } 412 } 413 414 419 public void saveWidgetValues(){ 420 } 422 423 427 public IWizardPage getNextPage() { 428 ITriggerPoint triggerPoint = getTriggerPoint(); 429 430 if (triggerPoint == null || WorkbenchActivityHelper.allowUseOf(triggerPoint, getSelectedNode())) { 431 return super.getNextPage(); 432 } 433 return null; 434 } 435 436 441 protected ITriggerPoint getTriggerPoint(){ 442 return null; } 444 445 450 protected void setTreeViewer(TreeViewer viewer){ 451 treeViewer = viewer; 452 } 453 454 459 protected TreeViewer getTreeViewer(){ 460 return treeViewer; 461 } 462 463 467 protected void initialize(){ 468 } 470 } 471 | Popular Tags |