package org.openslx.dozmod.gui.window;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.RowFilter;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import org.apache.log4j.Logger;
import org.openslx.bwlp.thrift.iface.LectureSummary;
import org.openslx.dozmod.gui.Gui;
import org.openslx.dozmod.gui.MainWindow;
import org.openslx.dozmod.gui.helper.MessageType;
import org.openslx.dozmod.gui.helper.PopupMenu;
import org.openslx.dozmod.gui.window.LectureDetailsWindow.LectureUpdatedCallback;
import org.openslx.dozmod.gui.window.layout.LectureListWindowLayout;
import org.openslx.dozmod.gui.wizard.LectureWizard;
import org.openslx.dozmod.permissions.LecturePerms;
import org.openslx.dozmod.thrift.Session;
import org.openslx.dozmod.thrift.ThriftActions;
import org.openslx.dozmod.thrift.ThriftActions.DeleteLectureCallback;
import org.openslx.dozmod.thrift.cache.LectureCache;
import org.openslx.util.QuickTimer;
import org.openslx.util.QuickTimer.Task;
/**
* Window for displaying a table with the available lectures.
* Lectures can be created and deleted here.
*/
@SuppressWarnings("serial")
public class LectureListWindow extends LectureListWindowLayout {
private final static Logger LOGGER = Logger.getLogger(LectureListWindow.class);
public final LectureListWindow me = this;
/**
* Popup menu items
*/
private JMenuItem popupItemNew = new JMenuItem("Neu");
private JMenuItem popupItemEdit = new JMenuItem("Bearbeiten");
private JMenuItem popupItemDelete = new JMenuItem("Löschen");
public LectureListWindow() {
super();
// filter the objects in the table depending on the search field
searchTextField.getDocument().addDocumentListener(new DocumentListener() {
@Override
public void removeUpdate(DocumentEvent e) {
changedUpdate(e);
}
@Override
public void insertUpdate(DocumentEvent e) {
changedUpdate(e);
}
@Override
public void changedUpdate(DocumentEvent e) {
// stuff
applyFilterOnTable();
}
});
filterCbo.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
applyFilterOnTable();
}
});
newButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new LectureWizard(SwingUtilities.getWindowAncestor(me), null, null).setVisible(true);
}
});
editButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
openLectureDetails(lectureTable.getSelectedItem());
}
});
// delete lecture
deleteButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
deleteLecture(lectureTable.getSelectedItem());
}
});
// return to mainMenu
switchViewButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
MainWindow.showPage(ImageListWindow.class);
}
});
/**
* Popup menu for the version table on the right side
*/
final PopupMenu pop = new PopupMenu(new ActionListener() {
public void actionPerformed(ActionEvent e) {
LectureSummary lecture = lectureTable.getSelectedItem();
if (lecture == null)
return;
if (e.getSource().equals(popupItemNew)) {
if (Gui.showMessageBox(me, "Um eine Veranstaltung zu erstellen, müssen Sie ein Image auswählen. Zur Imageauswahl wechseln?",
MessageType.QUESTION_YESNO, LOGGER, null)) {
MainWindow.showPage(ImageListWindow.class);
}
}
if (e.getSource().equals(popupItemEdit)) {
openLectureDetails(lecture);
}
if (e.getSource().equals(popupItemDelete)) {
deleteLecture(lecture);
}
}
});
pop.addMenuItem(popupItemNew);
pop.addMenuItem(popupItemEdit);
pop.addSeparator();
pop.addMenuItem(popupItemDelete);
// finally the table listeners
lectureTable.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
processClick(e);
}
@Override
public void mouseReleased(MouseEvent e) {
processClick(e);
}
private void processClick(MouseEvent e) {
// left double click => open details
if (SwingUtilities.isLeftMouseButton(e) && e.getClickCount() == 2) {
openLectureDetails(lectureTable.getSelectedItem());
return;
}
// else, check if we are a popup trigger
int r = lectureTable.rowAtPoint(e.getPoint());
if (r >= 0 && r < lectureTable.getRowCount()) {
// highlight the row and popup the menu
lectureTable.setRowSelectionInterval(r, r);
if (e.isPopupTrigger()) {
pop.show(e.getComponent(), e.getX(), e.getY());
}
} else {
lectureTable.clearSelection();
}
}
});
lectureTable.addKeyListener(new KeyAdapter() {
@Override
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_F5) {
refreshList(true);
}
}
});
lectureTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
LectureSummary item = lectureTable.getSelectedItem();
updateAvailableOptions(item);
}
});
updateAvailableOptions(null);
}
/**
* Updates the buttons/popup menu items according to the user's permissions
*
* @param item the image to check the user's permissions for
*/
private void updateAvailableOptions(LectureSummary lecture) {
boolean edit = LecturePerms.canEdit(lecture);
boolean admin = LecturePerms.canAdmin(lecture);
editButton.setEnabled(edit);
deleteButton.setEnabled(admin);
popupItemEdit.setEnabled(edit);
popupItemDelete.setEnabled(admin);
}
/**
* Opens the given lecture's details as a popup window
*/
private void openLectureDetails(final LectureSummary lecture) {
if (lecture == null)
return;
LectureDetailsWindow.open((JFrame)SwingUtilities.getWindowAncestor(me), lecture.getLectureId(), new LectureUpdatedCallback() {
@Override
public void updated(boolean success) {
refreshList(success);
}
});
}
/**
* Deletes the given lecture
*/
private void deleteLecture(final LectureSummary lecture) {
if (lecture == null)
return;
ThriftActions.deleteLecture(JOptionPane.getFrameForComponent(me),
lecture.getLectureId(), new DeleteLectureCallback() {
@Override
public void deleted(boolean success) {
refreshList(success);
}
});
}
/**
* Applies the filter entered in the search field to the table
*/
private void applyFilterOnTable() {
try {
// List for filters
List<RowFilter<Object, Object>> filters = new ArrayList<RowFilter<Object, Object>>(3);
// string for text in filter text field
String filterField = searchTextField.getText();
filters.add(RowFilter.regexFilter("(?i)" + filterField, 0, 1, 2));
// filter for user (only show own)
switch (filterCbo.getSelectedIndex()) {
case 0:
// no additional filters
break;
case 1:
// filter for owner
String userFilter = Session.getUserId();
filters.add(RowFilter.regexFilter("(?i)" + userFilter, 1));
break;
case 2:
// filter for editable
RowFilter<Object, Object> editFilter = new RowFilter<Object, Object>() {
public boolean include(Entry entry) {
return lectureTable.getModelRow((Integer) entry.getIdentifier()).getDefaultPermissions().edit;
}
};
filters.add(editFilter);
break;
case 3:
// filter for soon ending lectures
RowFilter<Object, Object> expireFilter = new RowFilter<Object, Object>() {
public boolean include(Entry entry) {
long endTime = lectureTable.getModelRow((Integer) entry.getIdentifier()).getEndTime() * 1000l;
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
cal.add(Calendar.DATE, 7);
return cal.getTimeInMillis() > endTime ? true: false;
}
};
filters.add(expireFilter);
break;
default:
break;
}
// for using multiple filters
RowFilter<Object, Object> andFilters = RowFilter.andFilter(filters);
lectureTable.getRowSorter().setRowFilter(andFilters);
searchTextField.setForeground(UIManager.getColor("TextField.foreground"));
} catch (IllegalArgumentException ex) {
// TODO set background color of search field to something redish
searchTextField.setForeground(Color.RED);
}
}
private void refreshList(final boolean force) {
QuickTimer.scheduleOnce(new Task() {
@Override
public void fire() {
final List<LectureSummary> lectureList = LectureCache.get(force);
Gui.asyncExec(new Runnable() {
@Override
public void run() {
lectureTable.setData(lectureList, true);
}
});
}
});
}
@Override
public boolean requestHide() {
return true;
}
@Override
public void requestShow() {
refreshList(false);
}
}