summaryrefslogblamecommitdiffstats
path: root/dozentenmodul/src/main/java/org/openslx/dozmod/gui/configurator/LecturePermissionConfigurator.java
blob: 5e4c1649eec95a9d7b50db9994316d263cc3b6e0 (plain) (tree)
1
2
3
4
5
6
7
8
9
                                            
 

                                     
                               

                                   

                           
                      


                           
                                  
                                 

                             
                           
                              
                          
                             
                                  
                             
                                        



                                                        
                                                                 

                                                                                          
                                                        
                                                 


                                                                      
   

                                                                            
   



              
                           
                                                                                                                            
 


                         
                                                 
 
                                                         



                                        
                                                                                                                 

                                                      
 
                                      
 
                                                                                                   
 
                                                


                          

                                                            
                                                               

                                                        
                                                     


                                                                                             
                                                                 


                                                                  
                                                     

                                               
                                                                   

                                                                          




                                                                               



                                                                    
                                                                                                                   

                                                                                                              







                                                                                                                            

                                                                                                                                     
                                                                                               
                                                                      
                                         
                                                           




                                                                      


                                                                                                          
                                                                                           


                                                                                      
                                                      

                         









                                                                                               







                                                                                             
         


                                           


                                                                                 
                                                                           
                                                                                   
           

                                                                                      
                                       
                                                             
                                       



                                                                                                         
                 


                                                               

                                                                        
           

                                                                                

                                                                                                        

                                                                    
                                                                                                    


                                        
 

                                     
           
 


                                                     


                 


                                                           
         

                                                                          
 


                                                         

         
 
package org.openslx.dozmod.gui.configurator;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.event.ChangeListener;

import org.apache.log4j.Logger;
import org.openslx.bwlp.thrift.iface.LecturePermissions;
import org.openslx.bwlp.thrift.iface.UserInfo;
import org.openslx.dozmod.gui.changemonitor.GenericControlWindow;
import org.openslx.dozmod.gui.control.table.LecturePermissionTable;
import org.openslx.dozmod.gui.control.table.LecturePermissionTable.UserLecturePermissions;
import org.openslx.dozmod.gui.control.table.QScrollPane;
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.
 */
/**
 * @author joe
 *
 */
@SuppressWarnings("serial")
public class LecturePermissionConfigurator extends JPanel implements GenericControlWindow<Map<String, LecturePermissions>> {

	/**
	 * Self reference
	 */
	private LecturePermissionConfigurator me;

	protected LecturePermissionTable permissionTable;

	protected JButton btnAddUser;
	protected JButton btnRemoveUser;

	private final ArrayList<UserLecturePermissions> permissionList = new ArrayList<UserLecturePermissions>();

	private LecturePermissions defaultPermissions;

	private String ownerId = null;

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

	public LecturePermissionConfigurator() {
		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ügen");
		userButtonPane.add(btnAddUser);
		btnRemoveUser = new JButton("Benutzer entfernen");
		userButtonPane.add(btnRemoveUser);
		userButtonPane.add(Box.createGlue());

		// Put everything into the grid
		QScrollPane jsp = new QScrollPane(permissionTable);
		jsp.setBackground(UIManager.getColor("Table.background"));
		grid.add(jsp).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) {
				UserListWindow.open(SwingUtilities.getWindowAncestor(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)));
						permissionTable.setData(permissionList, false);
						fireUserChangeEvent();
					}
				}, "Hinzufügen", ownerId);
			}
		});

		// delete user button listener
		btnRemoveUser.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				final UserLecturePermissions selected = permissionTable.getSelectedItem();
				if (selected != null && !permissionList.remove(selected)) {
					LOGGER.debug("Could not remove: " + selected);
				}
				permissionTable.setData(permissionList, false);
				fireUserChangeEvent();
			}
		});

		// keyboard shortcut
		permissionTable.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(
				KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0), "delete");
		permissionTable.getActionMap().put("delete", new AbstractAction() {
			@Override
			public void actionPerformed(ActionEvent ae) {
				btnRemoveUser.doClick();
			}
		});
		permissionTable.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				// TODO: This is stupid; permissionTable should fire an event
				// if one of the check boxes changes.
				fireUserChangeEvent();
			}
		});
	}

	/**
	 * 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
	 * @param ownerId The user to exclude in the list do add user. May be null.
	 */
	public void initPanel(Map<String, LecturePermissions> permissionMap,
			final LecturePermissions defaultPermissions, String ownerId) {
		this.ownerId = ownerId;
		this.defaultPermissions = defaultPermissions;
		permissionList.clear();
		if (permissionMap != null) {
			for (Entry<String, LecturePermissions> e : permissionMap.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> getPermissions() {
		Map<String, LecturePermissions> newPermissionMap = new HashMap<>(permissionList.size());
		// put permissions of the list into the map
		for (UserLecturePermissions perm : permissionList) {
			newPermissionMap.put(perm.userId, new LecturePermissions(perm.permissions));
		}
		return newPermissionMap;
	}

	/*
	 * For dialog change monitor 
	 */

	void fireUserChangeEvent() {
		for (ChangeListener cl : listeners) {
			cl.stateChanged(null);
		}
	}

	@Override
	public Map<String, LecturePermissions> getState() {
		return getPermissions();
	}
	
	private final List<ChangeListener> listeners = new ArrayList<>(1);

	@Override
	public void addChangeListener(ChangeListener l) {
		listeners.add(l);
	}

}