summaryrefslogtreecommitdiffstats
path: root/dozentenmodul/src/main/java/org/openslx/dozmod/gui/control/LectureCustomPermissionManager.java
blob: e081c050b35376e69594f9fd1492fe9620e1de24 (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
package org.openslx.dozmod.gui.control;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import org.apache.log4j.Logger;
import org.openslx.bwlp.thrift.iface.LecturePermissions;
import org.openslx.bwlp.thrift.iface.UserInfo;
import org.openslx.dozmod.gui.control.table.LecturePermissionTable;
import org.openslx.dozmod.gui.control.table.LecturePermissionTable.UserLecturePermissions;
import org.openslx.dozmod.gui.helper.GridManager;
import org.openslx.dozmod.gui.window.UserListWindow;
import org.openslx.dozmod.gui.window.UserListWindow.UserAddedCallback;


/**
 * Panel including LecturePermissionTable and add/remove buttons for setting customLecturePermissions.
 */
public class LectureCustomPermissionManager extends JPanel{

	/**
	 * Self reference
	 */
	private LectureCustomPermissionManager me;

	protected LecturePermissionTable permissionTable;	

	protected JButton btnAddUser;
	protected JButton btnRemoveUser;

	private ArrayList<UserLecturePermissions> permissionList  = new ArrayList<UserLecturePermissions>();
	private Map<String, LecturePermissions> newPermissionMap;

	private LecturePermissions defaultPermissions;

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


	public LectureCustomPermissionManager(){
		super();
		me = this;

		GridManager grid = new GridManager(this, 1);


		permissionTable = new LecturePermissionTable();

		// Panel for the add- and remove buttons
		JPanel userButtonPane = new JPanel();
		userButtonPane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
		userButtonPane.setLayout(new BoxLayout(userButtonPane, BoxLayout.LINE_AXIS));

		btnAddUser = new JButton("Benutzer hinzufügern");
		userButtonPane.add(btnAddUser);
		btnRemoveUser = new JButton("Benutzer entfernen");
		userButtonPane.add(btnRemoveUser);
		userButtonPane.add(Box.createGlue());	

		// Put everything into the grid
		grid.add(new JScrollPane(permissionTable)).fill(true, true).expand(true, true);
		grid.nextRow();
		grid.add(userButtonPane).fill(true, false).expand(true, false);
		grid.nextRow();
		grid.finish(false);


		// add user button listener
		btnAddUser.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO again: which frame to give? JOptionPane.getFrameForComponent(me) sounds cool at least :)
				UserListWindow.open(JOptionPane.getFrameForComponent(me), new UserAddedCallback() {

					@Override
					public void userAdded(final UserInfo newUser, UserListWindow window) {

						// check if we have this user already
						for (UserLecturePermissions current : permissionList) {
							if (current.userId.equals(newUser.userId)) {
								LOGGER.debug("User already present in the list, skipping!");
								return;
							}
						}
						// add it to the list with default permissions
						permissionList.add(new UserLecturePermissions(newUser.userId, new LecturePermissions(defaultPermissions)));
						LOGGER.debug("User added: " + newUser);
						permissionTable.setData(permissionList, false);
					}
				}, "Hinzufügen");
			}
		});

		// delete user button listener
		btnRemoveUser.addActionListener(new ActionListener() {

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

	/**
	 * Initialise the PermissionManager
	 * @param permissionMap the old permission, to initialise the table with, null creates empty table.
	 * @param defaultPermissions the permissions for a newly added user
	 */
	public void initPanel(Map<String, LecturePermissions> permissionMap, final LecturePermissions defaultPermissions){
		this.newPermissionMap = permissionMap == null ? new HashMap<String, LecturePermissions>() : permissionMap;
		permissionList.clear();
		this.defaultPermissions = defaultPermissions;
		for (Entry<String, LecturePermissions> e : newPermissionMap.entrySet()) {
			permissionList.add(new UserLecturePermissions(e.getKey(), e.getValue()));
		}
		permissionTable.setData(permissionList, false);
	}


	/**
	 * Get map with the permissions set in the table of the manager.
	 * @return Map with new custom permissions, null if something went wrong
	 */
	public Map<String, LecturePermissions> getMap(){
		if (permissionList == null)
			return null;

		newPermissionMap.clear();

		// put permissions of the list into the map
		for (UserLecturePermissions perm : permissionList) {
			newPermissionMap.put(perm.userId, perm.permissions);
		}
		return newPermissionMap;
	}
}