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

import java.awt.Color;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JTable;
import javax.swing.RowFilter;
import javax.swing.UIManager;

import org.apache.log4j.Logger;
import org.openslx.bwlp.thrift.iface.UserInfo;
import org.openslx.dozmod.gui.Gui;
import org.openslx.dozmod.gui.helper.TextChangeListener;
import org.openslx.dozmod.gui.helper.UiFeedback;
import org.openslx.dozmod.gui.window.layout.UserListWindowLayout;
import org.openslx.dozmod.thrift.cache.UserCache;
import org.openslx.util.QuickTimer;
import org.openslx.util.QuickTimer.Task;

@SuppressWarnings("serial")
public class UserListWindow extends UserListWindowLayout implements UiFeedback {

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

	private final UserListWindow me = this;

	private String ownerId = null;

	public interface UserAddedCallback {
		public void userAdded(UserInfo user, UserListWindow window);
	}

	public UserListWindow(final Window modalParent, final UserAddedCallback callback, String buttonCaption) {
		super(modalParent, buttonCaption);

		addWindowListener(new WindowAdapter() {
			@Override
			public void windowOpened(WindowEvent e) {
				// load the user cache async
				QuickTimer.scheduleOnce(new Task() {
					@Override
					public void fire() {
						final List<UserInfo> cacheList = UserCache.getAll();
						final List<UserInfo> userList = new ArrayList<>(cacheList.size() - 1);
						for (UserInfo user : cacheList) {
							if (ownerId != null && user.getUserId().equals(ownerId))
								continue;

							userList.add(user);
						}

						Gui.asyncExec(new Runnable() {
							@Override
							public void run() {
								userTable.setData(userList, true);
								// Scale the window after getting the list of users.
								int calcHeight = 150 + userList.size() * 20;
								calcHeight = calcHeight > 350 ? calcHeight : 350;
								me.setPreferredSize(Gui.getScaledDimension(450,
										(calcHeight < 600) ? calcHeight : 600));
								me.pack();
								userTable.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
							}
						});
					}
				});

			}
		});

		// filter the objects in the table depending on the search field
		txtSearch.getDocument().addDocumentListener(new TextChangeListener() {
			@Override
			public void changed() {
				try {
					userTable.getRowSorter().setRowFilter(
							RowFilter.regexFilter("(?i)" + txtSearch.getText()));
					txtSearch.setForeground(UIManager.getColor("TextField.foreground"));
				} catch (IllegalArgumentException ex) {
					txtSearch.setForeground(Color.RED);
				}
			}
		});

		btnConfirm.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				final UserInfo user = userTable.getSelectedItem();
				if (user == null)
					return;
				callback.userAdded(user, me);
			}
		});

		btnCancel.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		});

		getRootPane().setDefaultButton(btnConfirm);

		userTable.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if (e.getClickCount() == 2) {
					btnConfirm.doClick();
				}
			}
		});

		Gui.centerShellOverShell(modalParent, this);
		//		this.setLocationRelativeTo(SwingUtilities.getWindowAncestor(modalParent));
	}

	/**
	 * Open a new UserListWindow
	 * 
	 * @param modalParent
	 * @param callback
	 * @param buttonCaption
	 * @param ownerId The Id of the owner of an image or lecture. Should not be
	 *            shown in the list. Can be null
	 */
	public static void open(Window modalParent, UserAddedCallback callback, String buttonCaption,
			String ownerId) {
		UserListWindow win = new UserListWindow(modalParent, callback, buttonCaption);
		win.ownerId = ownerId;
		win.setVisible(true);
	}

	@Override
	public boolean wantConfirmQuit() {
		return false;
	}

	@Override
	public void escapePressed() {
		dispose();
	}
}