1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.team.internal.ccvs.ui;
15 
16 import java.util.*;
17 
18 import org.eclipse.jface.dialogs.Dialog;
19 import org.eclipse.jface.viewers.*;
20 import org.eclipse.swt.SWT;
21 import org.eclipse.swt.events.*;
22 import org.eclipse.swt.layout.GridData;
23 import org.eclipse.swt.layout.GridLayout;
24 import org.eclipse.swt.widgets.Button;
25 import org.eclipse.swt.widgets.Composite;
26 import org.eclipse.team.internal.ui.dialogs.DialogArea;
27 
28 /**
29  * Reusable area that provides a list to select from and a select all and
30  * deselect all button.
31  */
32 public class ListSelectionArea extends DialogArea {
33 	private Object inputElement;
34 	private IStructuredContentProvider contentProvider;
35 	private ILabelProvider labelProvider;
36 	private String message;
37 	private List initialSelections;
38 
39 	// the visual selection widget group
40 	private CheckboxTableViewer listViewer;
41 
42 	private Object[] previousCheckedElements;
43 
44 	public static final String LIST_SELECTION = "ListSelection"; //$NON-NLS-1$
45 
46 	/**
47 	 * Constructor for ListSelectionArea.
48 	 * @param parentDialog
49 	 * @param settings
50 	 */
ListSelectionArea( Object input, IStructuredContentProvider contentProvider, ILabelProvider labelProvider, String message)51 	public ListSelectionArea(
52 			Object input,
53 			IStructuredContentProvider contentProvider,
54 			ILabelProvider labelProvider,
55 			String message) {
56 		this.inputElement = input;
57 		this.contentProvider = contentProvider;
58 		this.labelProvider = labelProvider;
59 		this.message = message;
60 		this.initialSelections = new ArrayList();
61 	}
62 
63 	@Override
createArea(Composite parent)64 	public void createArea(Composite parent) {
65 
66 		Dialog.applyDialogFont(parent);
67 
68 		final Composite composite = createComposite(parent, 1, true);
69 
70 		initializeDialogUnits(composite);
71 
72 		if (message != null)
73 			createWrappingLabel(composite, message, 1);
74 
75 		listViewer = CheckboxTableViewer.newCheckList(composite, SWT.BORDER);
76 		GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
77 		data.heightHint = 0; // It will expand to the size of the wizard page!
78 		data.widthHint = 0;
79 		listViewer.getTable().setLayoutData(data);
80 
81 		listViewer.setLabelProvider(labelProvider);
82 		listViewer.setContentProvider(contentProvider);
83 
84 		listViewer.addCheckStateListener(new ICheckStateListener() {
85 			@Override
86 			public void checkStateChanged(CheckStateChangedEvent event) {
87 				Object[] checkedElements = getViewer().getCheckedElements();
88 				firePropertyChangeChange(LIST_SELECTION, previousCheckedElements, checkedElements);
89 				previousCheckedElements = checkedElements;
90 			}
91 		});
92 
93 		addSelectionButtons(composite);
94 
95 		initializeViewer();
96 
97 		// initialize page
98 		if (!getInitialElementSelections().isEmpty())
99 			checkInitialSelections();
100 	}
101 
102 	/**
103 	 * Initializes this dialog's viewer after it has been laid out.
104 	 */
initializeViewer()105 	private void initializeViewer() {
106 		listViewer.setInput(inputElement);
107 	}
108 
109 	/**
110 	 * Visually checks the previously-specified elements in this dialog's list
111 	 * viewer.
112 	 */
checkInitialSelections()113 	private void checkInitialSelections() {
114 		Iterator itemsToCheck = getInitialElementSelections().iterator();
115 
116 		while (itemsToCheck.hasNext())
117 			listViewer.setChecked(itemsToCheck.next(),true);
118 	}
119 
120 	/**
121 	 * Add the selection and deselection buttons to the dialog.
122 	 * @param composite org.eclipse.swt.widgets.Composite
123 	 */
addSelectionButtons(Composite composite)124 	private void addSelectionButtons(Composite composite) {
125 		Composite buttonComposite = new Composite(composite, SWT.RIGHT);
126 		buttonComposite.setLayout(new GridLayout(2, false));
127 		buttonComposite.setData(new GridData(SWT.END, SWT.BEGINNING, true, false));
128 
129 		Button selectButton = createButton(buttonComposite, CVSUIMessages.ListSelectionArea_selectAll, GridData.HORIZONTAL_ALIGN_FILL);
130 
131 		SelectionListener listener = new SelectionAdapter() {
132 			@Override
133 			public void widgetSelected(SelectionEvent e) {
134 				listViewer.setAllChecked(true);
135 			}
136 		};
137 		selectButton.addSelectionListener(listener);
138 
139 
140 		Button deselectButton = createButton(buttonComposite, CVSUIMessages.ListSelectionArea_deselectAll, GridData.HORIZONTAL_ALIGN_FILL);
141 
142 		listener = new SelectionAdapter() {
143 			@Override
144 			public void widgetSelected(SelectionEvent e) {
145 				listViewer.setAllChecked(false);
146 
147 			}
148 		};
149 		deselectButton.addSelectionListener(listener);
150 	}
151 
152 	/**
153 	 * Returns the list of initial element selections.
154 	 * @return List
155 	 */
getInitialElementSelections()156 	protected List getInitialElementSelections(){
157 		return initialSelections;
158 	}
159 
160 	/**
161 	 * Returns the listViewer.
162 	 * @return CheckboxTableViewer
163 	 */
getViewer()164 	public CheckboxTableViewer getViewer() {
165 		return listViewer;
166 	}
167 
168 }
169