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




                                      
                          

                             
                      


                                        
                               

                               
                                    

                                                         
                                                 
                                              
                                                      

                                         

                                                                      
                                                                       
                                                   
                                         
                                             













                                                                                          
                                              










                                                                    
 






                                                                         
                                                                      




                                                                                    















                                                                                                                     


           
                                                                 

                                                        


                                                    
                                                        



                                                                    



                                                                                                                                     
                                         
                                                        

                                                  
                                                           


                                                              
                                                                                                    

                                                                                                                    










                                                                  
                                                                                   

                             
                                                     


                                                                 
                                                           





                                                                                      

                                                      

                                                                            
                                                      
                                                              
                                                            


                                                                        

                                                                         

                                              

                                                                                            
                                                                                             

                         





                                                                                                                     

                                                                           

                                                                                                       
                                                                  
 

                                                                       

                                                                                                 
                                                              
 
                                   




                                             























                                                                                                                           





                                                                         
                                                                     
                                                
                                                     


                                                   
                                                                        
                                                    
                                                                                      



                                                       












                                                                                 






















                                                                                                                    
                                                                                  
         
 
package org.openslx.dozmod.gui.window;

import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;

import org.apache.log4j.Logger;
import org.apache.thrift.TException;
import org.openslx.bwlp.thrift.iface.ImageDetailsRead;
import org.openslx.bwlp.thrift.iface.ImageVersionDetails;
import org.openslx.bwlp.thrift.iface.LectureRead;
import org.openslx.bwlp.thrift.iface.UserInfo;
import org.openslx.dozmod.filetransfer.TransferHelper;
import org.openslx.dozmod.gui.Gui;
import org.openslx.dozmod.gui.MainWindow;
import org.openslx.dozmod.gui.helper.MessageType;
import org.openslx.dozmod.gui.window.UserListWindow.UserAddedCallback;
import org.openslx.dozmod.gui.window.layout.LectureDetailsWindowLayout;
import org.openslx.dozmod.permissions.LecturePerms;
import org.openslx.dozmod.thrift.Session;
import org.openslx.dozmod.thrift.ThriftError;
import org.openslx.dozmod.thrift.UserCache;
import org.openslx.dozmod.util.FormatHelper;
import org.openslx.thrifthelper.ThriftManager;
import org.openslx.util.QuickTimer;
import org.openslx.util.QuickTimer.Task;

@SuppressWarnings("serial")
public class LectureDetailsWindow extends LectureDetailsWindowLayout {

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

	private final LectureDetailsWindow me = this;

	private LectureRead lecture = null;
	private ImageDetailsRead image = null;

	public LectureDetailsWindow(Frame modalParent) {
		super(modalParent);

		// Close button closes window
		btnClose.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		});

		btnDownloadImage.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				performImageDownload();
			}
		});
		
		btnAutoUpdate.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				cboVersions.setEnabled(!btnAutoUpdate.isSelected());
			}
		});

		btnChangeOwner.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				UserListWindow.open(JOptionPane.getFrameForComponent(btnChangeOwner),
						new UserAddedCallback() {
							@Override
							public void userAdded(UserInfo user, UserListWindow window) {
								window.dispose();
								setLectureOwner(user);
							}

						}, "Besitzer festlegen");
			}
		});
		makeEditable(false);
	}

	/**
	 * @param lectureId the id of the lecture to be displayed
	 */
	public void setLecture(final String lectureId) {
		QuickTimer.scheduleOnce(new Task() {
			@Override
			public void fire() {
				TException error = null;
				try {
					synchronized (me) {
						if (lecture != null)
							return;
						lecture = ThriftManager.getSatClient().getLectureDetails(Session.getSatelliteToken(),
								lectureId);
						image = ThriftManager.getSatClient().getImageDetails(Session.getSatelliteToken(),
								lecture.getImageBaseId());
					}
				} catch (TException e) {
					error = e;
				}
				final TException e = error;
				Gui.asyncExec(new Runnable() {
					@Override
					public void run() {
						if (e != null || lecture == null || image == null) {
							ThriftError.showMessage(btnAutoUpdate, LOGGER, e,
									"Konnte Daten der Vorlesung nicht abrufen");
							dispose();
						} else {
							fill();
						}
					}
				});
			}
		});
	}

	/**
	 * callback function when we received the lecture's details from the server
	 */
	private void fill() {
		if (lecture == null || image == null)
			return;
		txtTitle.setText(lecture.getLectureName());
		txtDescription.setText(lecture.getDescription());
		txtImageName.setText(image.getImageName());
		lblOwner.setUser(UserCache.find(lecture.getOwnerId()));
		lblUpdater.setUser(UserCache.find(lecture.getUpdaterId()));
		lblCreateTime.setText(FormatHelper.longDate(lecture.getCreateTime()));
		lblUpdateTime.setText(FormatHelper.longDate(lecture.getUpdateTime()));
		lblStartTime.setText(FormatHelper.longDate(lecture.getStartTime()));
		lblEndTime.setText(FormatHelper.longDate(lecture.getEndTime()));
		txtId.setText(lecture.getLectureId());

		btnIsActive.setSelected(lecture.isEnabled);
		btnHasInternetAccess.setSelected(lecture.hasInternetAccess);
		btnIsExam.setSelected(lecture.isExam);
		btnAutoUpdate.setSelected(lecture.autoUpdate);
		cboVersions.setEnabled(!lecture.autoUpdate);

		lblUseCount.setText(Integer.toString(lecture.useCount));

		// version combo
		List<ImageVersionDetails> versions = image.getVersions();

		// sort versions by createtime
		Collections.sort(versions, new Comparator<ImageVersionDetails>() {
			public int compare(ImageVersionDetails o1, ImageVersionDetails o2) {
				return -Long.compare(o1.getCreateTime(), o2.getCreateTime());
			}
		});

		cboVersions.setModel(new DefaultComboBoxModel<ImageVersionDetails>(
				versions.toArray(new ImageVersionDetails[versions.size()])));
		cboVersions.setSelectedItem(new ImageVersionDetails(lecture.getImageVersionId(), 0, 0, 0, null, true,
				true, true, null));

		Calendar startCal = Calendar.getInstance();
		startCal.setTime(new Date(lecture.getStartTime() * 1000l));
		startDate.getModel().setDate(startCal.get(Calendar.YEAR), startCal.get(Calendar.MONTH),
				startCal.get(Calendar.DATE));
		startTime.getModel().setValue(startCal.getTime());

		Calendar endCal = Calendar.getInstance();
		endCal.setTime(new Date(lecture.getEndTime() * 1000l));
		endDate.getModel().setDate(endCal.get(Calendar.YEAR), endCal.get(Calendar.MONTH),
				endCal.get(Calendar.DATE));
		endTime.getModel().setValue(endCal.getTime());

		makeEditable(true);
		pack();
		MainWindow.centerShell(this);
		setVisible(true);
	}

	private void setLectureOwner(final UserInfo user) {
		QuickTimer.scheduleOnce(new Task() {
			@Override
			public void fire() {
				try {
					ThriftManager.getSatClient().setLectureOwner(Session.getSatelliteToken(),
							lecture.getLectureId(), user.getUserId());
				} catch (TException e) {
					ThriftError.showMessage(me, LOGGER, e, "Fehler beim Übertragen der Besitzrechte");
					return;
				}
				Gui.showMessageBox(me, "Besitzrechte übertragen an " + FormatHelper.userName(user),
						MessageType.INFO, null, null);
				makeEditable(false);
				String lectureId = lecture.getLectureId();
				synchronized (me) {
					image = null;
					lecture = null;
				}
				setLecture(lectureId);
			}
		});
	}

	/**
	 * Enables/disables the editable fields based on 'editable'
	 * 
	 * @param editable true to make fields editable, false otherwise.
	 */
	private void makeEditable(boolean editable) {
		editable = editable && LecturePerms.canEdit(lecture);
		//txtTitle.setEnabled(editable);
		txtDescription.setEditable(editable);
		btnLinkImage.setEnabled(editable);
		btnIsExam.setEnabled(editable);
		btnAutoUpdate.setEnabled(editable);
		cboVersions.setEnabled(editable && !lecture.autoUpdate);
		btnSaveChanges.setEnabled(editable);
		btnChangeOwner.setEnabled(editable && LecturePerms.canAdmin(lecture));
		startDate.setEnabled(editable); // TODO
		startTime.setEnabled(editable);
		endDate.setEnabled(editable); // TODO
		endTime.setEnabled(editable);
	}

	/**
	 * Opens a new LectureDetailsWindow showing the details of the
	 * lecture with ID = lectureId
	 * 
	 * @param modalParent parent of this window
	 * @param lectureId id of the lecture to set the details of
	 */
	public static void open(Frame modalParent, String lectureId) {
		LectureDetailsWindow win = new LectureDetailsWindow(modalParent);
		win.setLecture(lectureId);
	}
	
	/**
	 * Triggers the download of the currently used image version of the lecture
	 */
	private void performImageDownload() {
		if (image == null){
			Gui.showMessageBox(this, "Image ungültig.", MessageType.ERROR,
					null, null);
			return;
		}
		btnDownloadImage.setEnabled(false);
		long versionSize = 0;
		for (ImageVersionDetails version : image.versions) {
			if(version.versionId.equals(lecture.imageVersionId)){
				versionSize = version.fileSize;
			}
		}
		if (versionSize == 0){
			Gui.showMessageBox(this, "Fehler bei der Abfrag der Größe des Images.", MessageType.ERROR,
					null, null);
			return;
		}
		TransferHelper.initDownload(JOptionPane.getFrameForComponent(this), lecture.imageVersionId,
				image.imageName, image.virtId, versionSize, null);
	}
}