summaryrefslogtreecommitdiffstats
path: root/dozentenmodul/src/main/java/util/ListAllOtherUsers_GUI.java
blob: e09f0b0854955a6ded0a92c172098f8887ab2e4c (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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
package util;

import gui.intro.About_GUI;

import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.SystemColor;
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.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.swing.DefaultListSelectionModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;

import models.Links;
import models.SessionData;
import models.person;

import org.apache.log4j.Logger;
import org.apache.thrift.TException;
import org.openslx.sat.thrift.iface.Person;
import org.openslx.sat.thrift.iface.Server.Client;

import thrift.ThriftConnection;

@SuppressWarnings("serial")
public class ListAllOtherUsers_GUI extends JFrame {

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

	private final JPanel contentPanel = new JPanel();
	String[] result;
	JTable tableUsers;
	boolean activeSearch = false;

	String[] titles = { "userID", "Nachname", "Vorname", "Mail" };

	ThriftConnection con = new ThriftConnection();
	Client client = models.Client.clientcon.getClient();
	//final DefaultTableModel modelUsers = new DefaultTableModel(titles, 0);
	
	private JButton btnAdd;
	private List<Person> map = null; // List of people
	private JTextField textField;
	Component c = null;
	private JTable table;
	private int userIDPos;

	List<String> user = new ArrayList<String>();

	private static final String HELP_MESSAGE = "<html><div align=\"center\">"
			+ "In der Übersicht sehen Sie primär alle Veranstaltungen, die Sie erzeugt haben.<br />"
			+ "Sie können die Veranstaltungen hier löschen. Alternativ werden veraltete Einträge irgendwann automatisch gelöscht.<br />"
			+ "Veraltet bedeutet, dass Veranstaltungen, die drei Monate lang nicht augerufen wurden, vorerst deaktiviert werden."
			+ "</div></html>";
	
	
	//prevent table cells being clickable
	final DefaultTableModel modelUsers = new DefaultTableModel(titles, 0){
		public boolean isCellEditable(int rowIndex, int mColIndex) {
			return false;
		}
	};
	
	final TableRowSorter<TableModel> rowSorterAll = new TableRowSorter<TableModel>(modelUsers);

	/**
	 * 
	 * Constructor
	 */

	public ListAllOtherUsers_GUI(Component formerGUI, final JTable table,
			final int userIDPos) {

		// get table model to work with
		final DefaultTableModel model = (DefaultTableModel) table.getModel();
		
		//set incoming table
		this.table=table;
		this.userIDPos=userIDPos;

		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent arg0) {
				// Beendet die Anwendung nach klick auf X
				con.closeThriftConnection();
				dispose();
			}

			@Override
			public void windowOpened(WindowEvent arg0) {

				// auszublendende Angaben
				// 0=userID

				tableUsers.getColumnModel().getColumn(0).setWidth(0);
				tableUsers.getColumnModel().getColumn(0).setMinWidth(0);
				tableUsers.getColumnModel().getColumn(0).setMaxWidth(0);
			}
		});
		// Verhindert das Vergroessern Des Fensters
		setResizable(false);

		try {
			// Setzt das Look & Feel auf System
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (ClassNotFoundException | InstantiationException
				| IllegalAccessException | UnsupportedLookAndFeelException e) {

			e.printStackTrace();
		}
		// Setzt den Fenstertitel
		setTitle("bwLehrpool Suite - Benutzer hinzufügen");
		// Zentriert das Fenster in die Bildmitte
		setBounds(0, 0, 531, 673);
		setLocationRelativeTo(formerGUI);

		getContentPane().setLayout(null);
		{
			JPanel panel = new JPanel();
			panel.setBackground(SystemColor.menu);
			panel.setBounds(10, 11, 570, 57);
			getContentPane().add(panel);
			panel.setLayout(null);
			{
				JLabel lblNewLabel = new JLabel("Benutzer hinzufügen");
				lblNewLabel.setBounds(10, 11, 509, 22);
				panel.add(lblNewLabel);
				lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 18));
			}

			JTextPane txtpnBitteWhlenSie = new JTextPane();
			txtpnBitteWhlenSie.setEditable(false);
			txtpnBitteWhlenSie.setBackground(SystemColor.menu);
			txtpnBitteWhlenSie
					.setText("Wählen Sie bitte aus der unten stehenden Liste Benutzer aus, denen Sie Rechte vergeben möchten.");
			txtpnBitteWhlenSie.setBounds(10, 36, 509, 32);
			panel.add(txtpnBitteWhlenSie);
		}
		contentPanel.setBounds(10, 176, 498, 359);
		contentPanel.setBackground(SystemColor.menu);
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel);
		contentPanel.setLayout(null);
		JScrollPane scrollPaneMyImage = new JScrollPane();
		scrollPaneMyImage.setBounds(0, 0, 498, 359);
		contentPanel.add(scrollPaneMyImage);
		tableUsers = new JTable();
		tableUsers.setSelectionModel(new MyDefaultListSelectionModel());

		tableUsers.setModel(modelUsers);

		tableUsers.getColumnModel().getColumn(1).sizeWidthToFit();// Nachname
		tableUsers.getColumnModel().getColumn(2).sizeWidthToFit();// Vorname
		tableUsers.getColumnModel().getColumn(3).sizeWidthToFit();// email

		// initialize content
		initTableContent();

		tableUsers.setRowSorter(rowSorterAll);

		scrollPaneMyImage.setViewportView(tableUsers);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setBounds(0, 568, 508, 33);
			buttonPane.setBackground(SystemColor.menu);
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane);

			btnAdd = new JButton("Übernehmen und schließen");
			buttonPane.add(btnAdd);
			{
				JButton btnCancel = new JButton("Abbrechen");
				buttonPane.add(btnCancel);
				btnCancel.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						// only close window
						dispose();
					}
				});
				btnCancel.setActionCommand("OK");
				getRootPane().setDefaultButton(btnCancel);
			}
			btnAdd.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					// save the user list and go back to last GUI
					int pos = 0; // pointer for array
					int[] selectedRows = tableUsers.getSelectedRows(); // list

					// check if a row is selected
					if (selectedRows.length > 0) {
						// step through entries
						for (int i = 0; i < selectedRows.length; i++) {
							// get userID and write it to the table in former
							// GUI

							if (table.getColumnCount() == 6) { // is it an image
																// GUI?

								Object[] obj = {
										tableUsers.getValueAt(
												selectedRows[pos], 1)
												+ ", "
												+ tableUsers.getValueAt(
														selectedRows[pos], 2), // Nachname
																				// und
																				// Vorname
										false, // Read image
										false, // Write image
										false, // Link image
										false, // Admin image
										tableUsers.getValueAt(
												selectedRows[pos], 0) // userID
								};
								model.addRow(obj);

							} else if (table.getColumnCount() == 5) { // is it a
																		// lecture
																		// GUI?
								Object[] obj = {
										tableUsers.getValueAt(
												selectedRows[pos], 1)
												+ ", "
												+ tableUsers.getValueAt(
														selectedRows[pos], 2), // Nachname
																				// und
																				// Vorname
										false, // Read image
										false, // Write image
										false, // Admin image
										tableUsers.getValueAt(
												selectedRows[pos], 0) // userID
								};
								model.addRow(obj);
							}

							pos++;// next selected row
						}
					}// end if

					dispose();
				}// end actionPerformed
			});
		}

		JSeparator separator = new JSeparator();
		separator.setBounds(0, 79, 836, 1);
		getContentPane().add(separator);

		JSeparator separator_1 = new JSeparator();
		separator_1.setBounds(0, 552, 836, 1);
		getContentPane().add(separator_1);

		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);

		JMenu mnNewMenu_1 = new JMenu("Hilfe");
		menuBar.add(mnNewMenu_1);

		JMenuItem mntmFaq = new JMenuItem("FAQ");
		mntmFaq.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent arg0) {

				OpenLinks open = new OpenLinks();

				URI windows;
				try {
					windows = new URI(Links.getFAQ());
					open.openWebpage(windows);
				} catch (URISyntaxException e) {

					e.printStackTrace();
				}
			}
		});
		mnNewMenu_1.add(mntmFaq);

		JMenuItem mntmOtrs = new JMenuItem("OTRS");
		mntmOtrs.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent arg0) {

				OpenLinks open = new OpenLinks();
				URI windows;
				try {
					windows = new URI(Links.getOTRS());
					open.openWebpage(windows);
				} catch (URISyntaxException e) {
					e.printStackTrace();
				}
			}
		});
		mnNewMenu_1.add(mntmOtrs);

		JMenuItem mntmAbout = new JMenuItem("About");
		mntmAbout.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				About_GUI ab = new About_GUI();
				ab.setVisible(true);
			}
		});
		mnNewMenu_1.add(mntmAbout);

		JMenu mnNewMenu_Info = new JMenu("Info");
		mnNewMenu_Info.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				JOptionPane.showMessageDialog(c, HELP_MESSAGE,
						"Hilfe zu dieser Oberfläche",
						JOptionPane.INFORMATION_MESSAGE);
			}
		});
		menuBar.add(mnNewMenu_Info);

		c = this;

	}


	// get users from db which are not in listed in the permission yet
	public void initTableContent() {
		
		//LOGGER.info("Getting list of all other users from server");
		try {
			
			//set users which are NOT to be listed in table
			//logged on user
			user.add(0, person.verantwortlicher.getUserID());
			
			//users already in table
			for(int y=0; y<table.getRowCount(); y++){
				user.add(""+table.getValueAt(y, userIDPos));
			}			
			
			//get the info
			map = client.getAllOtherSatelliteUsers(user,SessionData.session.getAuthToken());
			Iterator<Person> i = map.iterator();

			int x = 0;
			while (i.hasNext()) {
				// erzeuge Objekte fuer die Tabelle
				Object[] obj = { map.get(x).getUserID(), // userID
						map.get(x).getNachname(), // Nachname
						map.get(x).getVorname(),// Vorname
						map.get(x).getMail() // EMail
				};
				// Fuege diese Objekte der Tabelle hinzu
				modelUsers.addRow(obj);
				i.next();
				x++;
			}// end while

		} catch (TException e2) {
			LOGGER.info("Failed to get List of users from server");
			e2.printStackTrace();
		}
	}// end initTableContent
}// end class ListAllOtherUsers

@SuppressWarnings("serial")
class MyDefaultListSelectionModel extends DefaultListSelectionModel {

	public MyDefaultListSelectionModel() {
		super();
	}

	public void setSelectionInterval(int index0, int index1) {
		if (isSelectedIndex(index0)) {
			super.removeSelectionInterval(index0, index1);
		} else {
			super.addSelectionInterval(index0, index1);
		}
	}
}// end class MyDefaultListSelectionModel