Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ab3ec19d96fe3f851a10bc1c7ef83eb22f4ec802 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171

/*****************************************************************************
 * Copyright (c) 2013 CEA LIST.
 *
 *    
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *  Patrick Tessier (CEA LIST) patrick.tessier@cea.fr - Initial API and implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.adltool.designer.wizard;

import java.util.ArrayList;
import java.util.Iterator;

import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.papyrus.adltool.designer.bundle.BundleLabelProvider;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;

/**
 * aGUI in order to select a set of bundle from a list 
 *
 */
public class BundleSelectionPage extends WizardPage {
	/**
	 * the graphical tree 
	 */
	protected Tree elementTree;
	protected ArrayList<?> bundleList;
	protected ArrayList<Object> selectedBundleList;
	protected BundleLabelProvider bundleLabelProvider= new BundleLabelProvider();

	private Composite comp;

	/**
	 * 
	 * Constructor.
	 *
	 * @param bundleList the list of bundle to display
	 */
	public BundleSelectionPage(ArrayList<?> bundleList) {
		super("BundleSelection");
		setTitle("Bundle Selection");
		setDescription("Select bundles to create the architecture model.");
		this.bundleList=bundleList;
		this.selectedBundleList= new ArrayList<Object>();
	}

	public void createControl(Composite parent) {
		comp = new Composite(parent, SWT.NULL);
		FillLayout layout = new FillLayout();
		layout.type = SWT.VERTICAL;
		comp.setLayout(layout);
		elementTree = new Tree(comp, SWT.CHECK | SWT.BORDER | SWT.V_SCROLL);
		Composite buttons = new Composite(comp, SWT.NONE);
		buttons.setLayout(new RowLayout());

		Button selectAll = new Button(buttons, SWT.PUSH);
		selectAll.setText("Select All");
		selectAll.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				selectAll();
			}
		});

		Button deselectAll = new Button(buttons, SWT.PUSH);
		deselectAll.setText("Deselect All");
		deselectAll.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				deselectAll();
			}

			
		});

		
		
		if(bundleList != null) {

			Iterator<?> it = bundleList.iterator();
			while(it.hasNext()) {
				Object current = it.next();
				createTreeItem(current);
			}
		}

		elementTree.addListener(SWT.Selection, new Listener() {

			public void handleEvent(Event event) {
				if(event.detail == SWT.CHECK) {
					if(selectedBundleList.contains(event.item.getData())) {
						selectedBundleList.remove(event.item.getData());

					} else {
						selectedBundleList.add(event.item.getData());
					}
					if(selectedBundleList.size()>0){
						setPageComplete(true);
					}
					else{
						setPageComplete(false);
					}
				}
			}
		});
		comp.pack();
		setControl(comp);
		setPageComplete(false);


	}


	protected void selectAll() {
		selectedBundleList.clear();
		for(int i = 0; i < elementTree.getItems().length; i++) {
			elementTree.getItems()[i].setChecked(true);
			selectedBundleList.add(elementTree.getItems()[i].getData());
			
		}
		setPageComplete(true);
		
	}
	protected void deselectAll() {
		for(int i = 0; i < elementTree.getItems().length; i++) {
			elementTree.getItems()[i].setChecked(false);
		}
		
			selectedBundleList.clear();
			setPageComplete(false);
	}

	/**
	 * create an item that represent the bundle
	 * @param _package
	 */
	private void createTreeItem(Object bundle) {
		TreeItem item = new TreeItem(elementTree, SWT.NONE);
		item.setText(bundleLabelProvider.getText(bundle));
		item.setImage(bundleLabelProvider.getImage(bundle));
		item.setData(bundle);
	}

	/**
	 * Returns the elements to import.
	 * 
	 * @return the set of selected bundle
	 */
	public ArrayList<Object> getResult() {
		return selectedBundleList;
	}

} 

Back to the top