summaryrefslogblamecommitdiffstats
path: root/dozentenmodul/src/main/java/org/openslx/dozmod/gui/control/ImageCustomPermissionManager.java
blob: 0c6d183799672e039dfafe700587c90652845fdb (plain) (tree)
1
2
3
4
5
6
7
8
9
10

                                       
                             






                                     

                                 
                               

                               



                                                      
                                                                 







                                                                                                  
                                                          
 




                                                

                                                
                                                                                                       

                                                               
                                                    






                                                                                                  









                                                                           
                
                                           
                                                                   





















                                                                                                                                                       
                   

                                              
                                                                      









                                                                                                        

















                                                                                                                        





















                                                                                
package org.openslx.dozmod.gui.control;

import java.awt.BorderLayout;
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.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

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;
import org.openslx.dozmod.gui.control.table.ImagePermissionTable.UserImagePermissions;
import org.openslx.dozmod.gui.window.UserListWindow;
import org.openslx.dozmod.gui.window.UserListWindow.UserAddedCallback;


/**
 * Panel including ImagePermissionTable and add/remove buttons for setting customImagePermissions.
 */
public class ImageCustomPermissionManager extends JPanel {

protected ImagePermissionTable permissionTable;	
	
	protected JButton btnAddUser;
	protected JButton btnRemoveUser;
	
	private ImageCustomPermissionManager me;

	private ArrayList<UserImagePermissions> permissionList = new ArrayList<UserImagePermissions>();
	private Map<String, ImagePermissions> newPermissionMap;

	private ImagePermissions defaultPermissions;

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


	public ImageCustomPermissionManager(){
		super();
		me = this;
		setLayout(new BorderLayout());
		setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
		permissionTable = new ImagePermissionTable();
		add(new JScrollPane(permissionTable), BorderLayout.CENTER);
		JPanel userButtonPane = new JPanel();
		btnAddUser = new JButton("Benutzer hinzufügern");
		userButtonPane.add(btnAddUser);
		btnRemoveUser = new JButton("Benutzer entfernen");
		userButtonPane.add(btnRemoveUser);
		add(userButtonPane, BorderLayout.SOUTH);
		
		// 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 (UserImagePermissions 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 UserImagePermissions(newUser.userId, new ImagePermissions(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 UserImagePermissions 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, ImagePermissions> permissionMap, final ImagePermissions defaultPermissions){
		this.newPermissionMap = permissionMap == null ? new HashMap<String, ImagePermissions>() : permissionMap;
		permissionList.clear();
		this.defaultPermissions = defaultPermissions;

		for (Entry<String, ImagePermissions> e : newPermissionMap.entrySet()) {
			permissionList.add(new UserImagePermissions(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, ImagePermissions> getMap(){
		// save the table stuff to our upload wizard state
		if (permissionList == null)
			return null;

		newPermissionMap.clear();

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

}