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

                                      
                      



                                     


                             

                               



                                                      
                                         
                                                 
                                                                     
                                                 







                                               
                           







                                                                                        

                                                      
 
















                                                                           
                                   
         
 


                                                                 
                                                        
 





















                                                                                                                                 
                                                                                                                         
                                                                                                      
                                                                  








                                                               


                                                                                 
                             
                                  
                               

                                                               

                                                                         

                                                                                    



                                                                







                                                                                    

                                                       

                                                                              
                                                               



                                                                                       
 




                                                             
                                                            
                                                        
                       
                                             
                                 

         




                                                                         
                                                     

                                                    
                                             

                                                
                                                   
                                                        
                                                  
         








                                                                        
                                                                             
                                          
         
 
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.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.apache.log4j.Logger;
import org.openslx.bwlp.thrift.iface.ImageDetailsRead;
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.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;

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

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

	/**
	 * @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.getCurrentVersionId());
		
		
		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);
		makeEditable(ImagePerms.canEdit(image));
		pack();
		MainWindow.centerShell(this);
		setVisible(true);
	}

	/**
	 * 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.setEnabled(editable);
		txtTags.setEnabled(editable);
		txtVersion.setEnabled(editable);
		txtId.setEnabled(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);
	}
}