summaryrefslogtreecommitdiffstats
path: root/dozentenmodul/src/main/java/org/openslx/dozmod/gui/wizard/page/ImageCustomPermissionPage.java
blob: 106a7299fe7a67cb41c4bd82196f455f120fefd5 (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
package org.openslx.dozmod.gui.wizard.page;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import javax.swing.JOptionPane;

import org.apache.log4j.Logger;
import org.openslx.bwlp.thrift.iface.ImagePermissions;
import org.openslx.bwlp.thrift.iface.UserInfo;
import org.openslx.dozmod.gui.control.table.ImagePermissionTable.UserImagePermissions;
import org.openslx.dozmod.gui.window.UserListWindow;
import org.openslx.dozmod.gui.window.layout.UserListWindowLayout.UserAddedCallback;
import org.openslx.dozmod.gui.wizard.Wizard;
import org.openslx.dozmod.gui.wizard.layout.ImageCustomPermissionPageLayout;
import org.openslx.dozmod.state.UploadWizardState;

@SuppressWarnings("serial")
public class ImageCustomPermissionPage extends ImageCustomPermissionPageLayout {

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

	private final ImageCustomPermissionPage me = this;

	private UploadWizardState state = null;

	private ArrayList<UserImagePermissions> permissionList = new ArrayList<>();

	/**
	 * wizard page for setting custom permissions
	 * 
	 * @param wizard
	 */
	public ImageCustomPermissionPage(Wizard wizard, UploadWizardState uploadWizardState) {
		super(wizard);
		setPageComplete(true);
		this.state = uploadWizardState;

		// add user button adapter
		addUser.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO again: which frame to giev? JOptionPane.getFrameForComponent(me) sounds cool at least :)
				UserListWindow.open(JOptionPane.getFrameForComponent(me), new UserAddedCallback() {
					@Override
					public void userAdded(final UserInfo newUser) {

						// looks if we have this user already
						Iterator<UserImagePermissions> it = permissionList.iterator();
						while (it.hasNext()) {
							UserImagePermissions current = it.next();
							if (current.userId == newUser.userId) {
								// user already in the list, skip it
								LOGGER.debug("User already present in the list, skipping!");
								return;
							}
						}
						// add it to the list with either default permissions if set, or none
						ImagePermissions newUserPerms = null;
						if (state.permissions == null) {
							newUserPerms = new ImagePermissions(false, false, false, false);
						} else {
							newUserPerms = state.permissions;
						}
						permissionList.add(new UserImagePermissions(newUser.userId, newUserPerms));
						LOGGER.debug("User added: " + newUser);
						permissionTable.setData(permissionList, false);
					}
				});
			}
		});
		// delete user button adapter
		removeUser.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				final UserImagePermissions selected = permissionTable.getSelectedItem();
				LOGGER.debug("Removing: " + selected);
				if (!permissionList.remove(selected)) {
					LOGGER.debug("Could not remove: " + selected);
				}
				permissionTable.setData(permissionList, false);
			}
		});
	}

	@Override
	protected void onPageLeave() {
		LOGGER.debug("Saving permissions to state ...");
		// save the table stuff to our upload wizard state
		if (permissionList == null || permissionList.isEmpty())
			return;
		// stuff in our list, clear old list in state before continuing
		if (state.permissionList == null) {
			state.permissionList = new HashMap<String, ImagePermissions>();
		} else {
			state.permissionList.clear();
		}

		// add them one by one
		for (UserImagePermissions perm : permissionList) {
			// for now just overwrite the saved permission list of the state
			state.permissionList.put(perm.userId, perm.permissions);
		}
	}
}