summaryrefslogtreecommitdiffstats
path: root/dozentenmodul/src/main/java/org/openslx/dozmod/gui/wizard/page/ImageCustomPermissionPage.java
blob: 1b9a92cb8b40c478adf24e5fe006ec5835d5ddb0 (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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package org.openslx.dozmod.gui.wizard.page;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ColumnLabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.ViewerCell;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.TableEditor;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;
import org.openslx.bwlp.thrift.iface.ImagePermissions;
import org.openslx.bwlp.thrift.iface.UserInfo;
import org.openslx.dozmod.gui.helper.TableHelper;
import org.openslx.dozmod.gui.helper.UserToPermissionLink;
import org.openslx.dozmod.gui.wizard.layout.ImageCustomPermissionPageLayout;
import org.openslx.dozmod.state.UploadWizardState;
import org.openslx.dozmod.thrift.UserCache;

public class ImageCustomPermissionPage extends ImageCustomPermissionPageLayout {

	private final static Logger LOGGER = Logger
			.getLogger(ImageCustomPermissionPage.class);

	private UploadWizardState uploadWizardState = null;
	private ArrayList<UserToPermissionLink> permissionList = null;
	private Map<String, ImagePermissions> imagePermissionsMap = null;

	protected TableViewer permissionTableViewer;

	/**
	 * wizard page for setting custom permissions
	 */
	@SuppressWarnings("serial")
	public ImageCustomPermissionPage(UploadWizardState uploadWizardState) {
		super();
		this.uploadWizardState = uploadWizardState;
		// data "model"
		imagePermissionsMap = new HashMap<String, ImagePermissions>() {
			{
				for (UserInfo user : UserCache.getAll()) {
					put(user.getUserId(), new ImagePermissions(true, true, true, false));
				}
			}
		};
		permissionList = new ArrayList<UserToPermissionLink>();
	}

	@Override
	public void createControl(Composite parent) {
		super.createControl(parent);

		List<String> mapKeys = new ArrayList<String>(imagePermissionsMap.keySet());
		if (imagePermissionsMap != null) {
			for (String key : mapKeys) {
				permissionList.add(new UserToPermissionLink(key, imagePermissionsMap
						.get(key)));
			}
		}
		
		// Initialize the table viewer now that everything is ready
		permissionTableViewer = new TableViewer(permissionTable);
		permissionTableViewer.setContentProvider(ArrayContentProvider.getInstance());

		// create table columns through TableHelper
		TableHelper.createImagePermissionTableColumns(permissionTableViewer,
				new ColumnLabelProvider() {
			@Override
			public void update(ViewerCell cell) {
				TableItem item = (TableItem) cell.getItem();
				Object cellElement = cell.getElement();
				UserToPermissionLink userPermLink = null;
				if (cellElement instanceof UserToPermissionLink) {
					userPermLink = (UserToPermissionLink) cellElement;
				}
				if (userPermLink == null) {
					// TODO handle properly
					LOGGER.debug("Could not cast '" + cellElement
							+ "' to UserToPermissionLink!");
					return;
				}
				int cellIndex = cell.getColumnIndex();
				// CASE there are 5 columns, therefore we need to subtract 1
				// (for the user label) when using cellIndex as index for the buttons array!!!
				if (userPermLink.buttons[cellIndex - 1] == null) {
					userPermLink.buttons[cellIndex - 1] = createButton((Composite) cell
							.getViewerRow().getControl(), SWT.CHECK);
					userPermLink.buttons[cellIndex - 1].setData("userId", userPermLink.getUserId());
					userPermLink.buttons[cellIndex - 1].setData("index", cellIndex - 1);
				}
				// finally set the button's state according to the perms
				boolean savedPerm = false;
				switch (cellIndex) {
					case 1:
						savedPerm = userPermLink.getPermission().link;
						break;
					case 2:
						savedPerm = userPermLink.getPermission().download;
						break;
					case 3:
						savedPerm = userPermLink.getPermission().edit;
						break;
					case 4:
						savedPerm = userPermLink.getPermission().admin;
					default:
						break;
				}
				userPermLink.buttons[cellIndex -1].setSelection(savedPerm);
				TableEditor editor = new TableEditor(item.getParent());
				editor.grabHorizontal = true;
				editor.grabVertical = true;
				editor.setEditor(userPermLink.buttons[cellIndex - 1], item, cell.getColumnIndex());
				editor.layout();
			}
		});

		permissionTableViewer.setInput(permissionList);
		permissionTableViewer.refresh();

		// add user button adapter
		addUser.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				LOGGER.debug("addUser called");
			}
		});
	}
	/**
	 * @param parent Parent to this button
	 * @param style Style
	 * @return the button
	 */
	private Button createButton(Composite parent, int style) {
		Button newButton = new Button(parent, style);
		newButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				Button o = (Button) e.getSource();
				callback(o);
			}
		});
		return newButton;
	}
	/**
	 * @param button
	 */
	public void callback(final Button button) {
		int index = (int)button.getData("index");
		String userId = (String) button.getData("userId");
		ImagePermissions perm = null;
		if (imagePermissionsMap.containsKey(userId)) {
			perm = imagePermissionsMap.get(userId);
		} else {
			LOGGER.error(userId + " is not in the permission map!");
			return;
		}
		LOGGER.debug("Before: " + perm);
		switch(index) {
		case 0:
			perm.link = button.getSelection();
			break;
		case 1:
			perm.download = button.getSelection();
			break;
		case 2:
			perm.edit = button.getSelection();
			break;
		case 3:
			perm.admin = button.getSelection();
			break;
		default:
			break;
		}
		uploadWizardState.permissionList = imagePermissionsMap;
		LOGGER.debug("After: " + perm);
	}
	/**
	 * @return permission list table object
	 */
	public Table getTable() {
		return permissionTable;
	}
}