1 /*******************************************************************************
2  *  Copyright (c) 2000, 2018 IBM Corporation and others.
3  *
4  *  This program and the accompanying materials
5  *  are made available under the terms of the Eclipse Public License 2.0
6  *  which accompanies this distribution, and is available at
7  *  https://www.eclipse.org/legal/epl-2.0/
8  *
9  *  SPDX-License-Identifier: EPL-2.0
10  *
11  *  Contributors:
12  *     IBM Corporation - initial API and implementation
13  *******************************************************************************/
14 package org.eclipse.pde.internal.ui.wizards;
15 
16 import org.eclipse.jface.viewers.*;
17 import org.eclipse.jface.wizard.IWizardNode;
18 import org.eclipse.pde.internal.ui.elements.*;
19 import org.eclipse.swt.SWT;
20 import org.eclipse.swt.custom.BusyIndicator;
21 import org.eclipse.swt.custom.SashForm;
22 import org.eclipse.swt.layout.FillLayout;
23 import org.eclipse.swt.layout.GridLayout;
24 import org.eclipse.swt.widgets.*;
25 
26 public abstract class WizardTreeSelectionPage extends BaseWizardSelectionPage {
27 	private TreeViewer categoryTreeViewer;
28 	private String baseCategory;
29 	protected TableViewer wizardSelectionViewer;
30 
31 	private WizardCollectionElement wizardCategories;
32 
WizardTreeSelectionPage(WizardCollectionElement categories, String baseCategory, String message)33 	public WizardTreeSelectionPage(WizardCollectionElement categories, String baseCategory, String message) {
34 		super("NewExtension", message); //$NON-NLS-1$
35 		this.wizardCategories = categories;
36 		this.baseCategory = baseCategory;
37 	}
38 
advanceToNextPage()39 	public void advanceToNextPage() {
40 		getContainer().showPage(getNextPage());
41 	}
42 
43 	@Override
createControl(Composite parent)44 	public void createControl(Composite parent) {
45 		// top level group
46 		Composite container = new Composite(parent, SWT.NULL);
47 		FillLayout flayout = new FillLayout();
48 		flayout.marginWidth = 5;
49 		flayout.marginHeight = 5;
50 		container.setLayout(flayout);
51 		SashForm rootSash = new SashForm(container, SWT.VERTICAL);
52 		SashForm outerSash = new SashForm(rootSash, SWT.HORIZONTAL);
53 		GridLayout layout = new GridLayout();
54 		layout.numColumns = 2;
55 
56 		// tree pane
57 		Tree tree = new Tree(outerSash, SWT.BORDER);
58 		categoryTreeViewer = new TreeViewer(tree);
59 		categoryTreeViewer.setContentProvider(new TreeContentProvider());
60 		categoryTreeViewer.setLabelProvider(ElementLabelProvider.INSTANCE);
61 
62 		categoryTreeViewer.setComparator(new WizardCollectionComparator(baseCategory));
63 		categoryTreeViewer.addSelectionChangedListener(this);
64 
65 		// wizard actions pane
66 		Table table = new Table(outerSash, SWT.BORDER);
67 		new TableColumn(table, SWT.NONE);
68 		TableLayout tlayout = new TableLayout();
69 		tlayout.addColumnData(new ColumnWeightData(100));
70 		table.setLayout(tlayout);
71 
72 		wizardSelectionViewer = new TableViewer(table);
73 		wizardSelectionViewer.setContentProvider(new ListContentProvider());
74 		wizardSelectionViewer.setLabelProvider(ListUtil.TABLE_LABEL_PROVIDER);
75 		wizardSelectionViewer.setComparator(ListUtil.NAME_COMPARATOR);
76 		wizardSelectionViewer.addSelectionChangedListener(this);
77 		wizardSelectionViewer.addDoubleClickListener(event -> BusyIndicator.showWhile(wizardSelectionViewer.getControl().getDisplay(), () -> {
78 					selectionChanged(new SelectionChangedEvent(wizardSelectionViewer,
79 							wizardSelectionViewer.getStructuredSelection()));
80 			advanceToNextPage();
81 		}));
82 
83 		// the new composite below is needed in order to make the label span the two
84 		// defined columns of outerContainer
85 		Composite descriptionComposite = new Composite(rootSash, SWT.NONE);
86 		layout = new GridLayout();
87 		layout.marginHeight = 0;
88 		layout.marginWidth = 0;
89 		descriptionComposite.setLayout(layout);
90 		createDescriptionIn(descriptionComposite);
91 
92 		initializeViewers();
93 		rootSash.setWeights(new int[] {70, 30});
94 		setControl(container);
95 	}
96 
getSingleSelection(IStructuredSelection selection)97 	protected Object getSingleSelection(IStructuredSelection selection) {
98 		Object selectedObject = selection.getFirstElement();
99 		if (selection.size() > 1)
100 			selectedObject = null; // ie.- a multi-selection
101 		return selectedObject;
102 	}
103 
handleCategorySelection(SelectionChangedEvent selectionEvent)104 	private void handleCategorySelection(SelectionChangedEvent selectionEvent) {
105 		setErrorMessage(null);
106 		setDescriptionText(""); //$NON-NLS-1$
107 		setSelectedNode(null);
108 
109 		WizardCollectionElement selectedCategory = (WizardCollectionElement) getSingleSelection(
110 				selectionEvent.getStructuredSelection());
111 
112 		if (selectedCategory == null)
113 			wizardSelectionViewer.setInput(null);
114 		else
115 			wizardSelectionViewer.setInput(selectedCategory.getWizards());
116 	}
117 
handleWizardSelection(SelectionChangedEvent selectionEvent)118 	private void handleWizardSelection(SelectionChangedEvent selectionEvent) {
119 		setErrorMessage(null);
120 
121 		WizardElement currentSelection = (WizardElement) getSingleSelection(selectionEvent.getStructuredSelection());
122 
123 		// If no single selection, clear and return
124 		if (currentSelection == null) {
125 			setDescriptionText(""); //$NON-NLS-1$
126 			setSelectedNode(null);
127 			return;
128 		}
129 		final WizardElement finalSelection = currentSelection;
130 		setSelectedNode(createWizardNode(finalSelection));
131 		setDescriptionText(finalSelection.getDescription());
132 	}
133 
initializeViewers()134 	protected void initializeViewers() {
135 		categoryTreeViewer.setInput(wizardCategories);
136 		wizardSelectionViewer.addSelectionChangedListener(this);
137 		Object[] categories = wizardCategories.getChildren();
138 		if (categories.length > 0)
139 			categoryTreeViewer.setSelection(new StructuredSelection(categories[0]));
140 		categoryTreeViewer.getTree().setFocus();
141 	}
142 
143 	@Override
selectionChanged(SelectionChangedEvent selectionEvent)144 	public void selectionChanged(SelectionChangedEvent selectionEvent) {
145 		if (selectionEvent.getSelectionProvider().equals(categoryTreeViewer))
146 			handleCategorySelection(selectionEvent);
147 		else
148 			handleWizardSelection(selectionEvent);
149 	}
150 
151 	@Override
setSelectedNode(IWizardNode node)152 	public void setSelectedNode(IWizardNode node) {
153 		super.setSelectedNode(node);
154 	}
155 }
156