summaryrefslogblamecommitdiffstats
path: root/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/ImageDetailsWindow.java
blob: be2ff02c707139b0d357b18a0100f12915089eea (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.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import javax.swing.JMenuItem;

import org.apache.log4j.Logger;
import org.openslx.bwlp.thrift.iface.ImageDetailsRead;
import org.openslx.bwlp.thrift.iface.ImagePermissions;
import org.openslx.bwlp.thrift.iface.ImageVersionDetails;
import org.openslx.bwlp.thrift.iface.OperatingSystem;
import org.openslx.bwlp.thrift.iface.Virtualizer;
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.layout.ImageDetailsWindowLayout;
import org.openslx.dozmod.permissions.ImagePerms;
import org.openslx.dozmod.thrift.MetaDataCache;
import org.openslx.dozmod.thrift.Session;
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 ImageDetailsWindow extends ImageDetailsWindowLayout {

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

	private final ImageDetailsWindow me = this;

	private ImageDetailsRead image = null;
	
	private final JMenuItem[] popupMenuItems = new JMenuItem[2];

	public ImageDetailsWindow(Frame modalParent) {
		super(modalParent);
		setFocusable(true);

		// ESC closes this window
		addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
					dispose();
				}
			}
		});
		// Close button closes window
		btnClose.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		});

		// nothing to save at first since nothing changed
		btnSaveChanges.setEnabled(false);
		btnSaveChanges.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				LOGGER.debug(("Speichern clicked"));
				// TODO do save
			}
		});
		// Setup popup menu for the right panel
		final PopupMenu pop = new PopupMenu(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (e.getSource().equals(popupMenuItems[0])) {
					// TODO new lecture wizard with this image preset
					LOGGER.debug("New lecture clicked");
				}
				if (e.getSource().equals(popupMenuItems[1])) {
					// TODO delete that image
					LOGGER.debug("Delete image clicked");
				}

			}
		});
		// our menu items
		popupMenuItems[0] = new JMenuItem("Neue Veranstaltung");
		popupMenuItems[1] = new JMenuItem("Löschen");
		// add them to the popup menu
		pop.addMenuItem(popupMenuItems[0]);
		pop.addSeparator();
		pop.addMenuItem(popupMenuItems[1]);

		// mouse adapter to register clicks and react on the right panel
		final MouseAdapter ma = new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent e) {
				processClick(e);
			}
			@Override
			public void mouseReleased(MouseEvent e) {
				processClick(e);
			}
			private void processClick(MouseEvent e) {
				int r = versionTable.rowAtPoint(e.getPoint());
				if (r >= 0 && r < versionTable.getRowCount()) {
					// highlight the row and popup the menu
					versionTable.setRowSelectionInterval(r, r);
					if (e.isPopupTrigger()) {
						pop.show(e.getComponent(), e.getX(), e.getY());
					}
				} else {
					versionTable.clearSelection();
				}
			}
		};
		// register mouse adapter for the right panel
		versionTableScrollPane.addMouseListener(ma);
		versionTable.addMouseListener(ma);

	}


	/**
	 * @param imageBaseId the id of the image to be displayed
	 */
	public void setImage(final String imageBaseId) {

		QuickTimer.scheduleOnce(new Task() {
			@Override
			public void fire() {
				Exception error = null;
				try {
					synchronized (me) {
						if (image != null)
							return;
						image = ThriftManager.getSatClient().getImageDetails(Session.getSatelliteToken(),
								imageBaseId);
					}
				} catch (Exception e) {
					error = e;
				}
				// Just prime the cache...
				MetaDataCache.getOperatingSystems();
				MetaDataCache.getVirtualizers();
				final Exception e = error;
				Gui.asyncExec(new Runnable() {
					@Override
					public void run() {
						if (e != null || image == null) {
							Gui.showMessageBox(null, "Konnte Daten des Images nicht abrufen",
									MessageType.ERROR, LOGGER, e);
							dispose();
						} else {
							fill();
						}
					}
				});
			}
		});
	}

	/**
	 * callback function when we received the image's details from the server
	 */
	private void fill() {
		if (image == null)
			return;
		txtTitle.setText(image.getImageName());
		txtDescription.setText(image.getDescription());
		lblOwner.setUser(UserCache.find(image.getOwnerId()));
		lblUpdater.setUser(UserCache.find(image.getUpdaterId()));
		lblCreateTime.setText(FormatHelper.longDate(image.getCreateTime()));
		lblUpdateTime.setText(FormatHelper.longDate(image.getUpdateTime()));
		txtId.setText(image.getImageBaseId());
		txtVersion.setText(image.getLatestVersionId());


		List<OperatingSystem> osList = MetaDataCache.getOperatingSystems();
		// all fine, lets sort it
		Collections.sort(osList, new Comparator<OperatingSystem>() {
			public int compare(OperatingSystem o1, OperatingSystem o2) {
				return o1.getOsName().compareTo(o2.getOsName());
			}
		});
		for (OperatingSystem os : osList) {
			cboOperatingSystem.addItem(os);
		}
		OperatingSystem os = MetaDataCache.getOsById(image.getOsId());
		if (os != null) {
			cboOperatingSystem.setSelectedItem(os);
		}
		Virtualizer virt = MetaDataCache.getVirtualizerById(image.getVirtId());
		if (virt != null)
			lblVirtualizer.setText(virt.getVirtName());

		String tagsString = "";
		for (String tag : image.getTags()) {
			tagsString = tagsString + ", " + tag;
		}
		txtTags.setText(tagsString);
		btnIsTemplate.setSelected(image.isTemplate);

		versionTable.setData(image.getVersions(), true);

		makeEditable(ImagePerms.canEdit(image));
		pack();
		MainWindow.centerShell(this);
		setVisible(true);
		//		refreshList();
	}

	/**
	 * Enables/disables the editable fields based on 'editable'
	 * 
	 * @param editable true to make fields editable, false otherwise.
	 */
	private void makeEditable(boolean editable) {
		txtTitle.setEnabled(editable);
		txtDescription.setEditable(editable);
		txtTags.setEditable(editable);
		txtVersion.setEditable(editable);
		txtId.setEditable(editable);
		btnIsTemplate.setEnabled(editable);
		cboOperatingSystem.setEnabled(editable);
		cboShareMode.setEnabled(editable);
	}

	/**
	 * Opens a new ImageDetailsWindow showing the details of the
	 * image with ID = imageBaseId
	 * 
	 * @param modalParent parent of this window
	 * @param imageBaseId id of the image to set the details of
	 */
	public static void open(Frame modalParent, String imageBaseId) {
		ImageDetailsWindow win = new ImageDetailsWindow(modalParent);
		win.setImage(imageBaseId);
	}

}