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




                                      

                           
                           
 
                                
 


                                               

                                                                      


                                                                    
                           
                                                                    
 
                                           




                                                                                 


                                                                
                                                                                       
                                            
                                                        
                                                            
                                                                                

                                                        


                                                        
                                                                                                         
                                                                
                                 

                                                                                     
                         
                  
                                                                


                                                          
 

                                                                   

                                                                    
                                                        
                                                                  
                                                                             

                                                                                                            

                                                                                     


                                                 

                                                                 







                                                                                           
                                                           
 

                                                            
 



                                          





                                                            
                                                                         

                                                                                   
                                                                                    

         
 
package org.openslx.dozmod.gui.window;

import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Map.Entry;

import javax.swing.JRadioButton;

import org.openslx.bwlp.thrift.iface.Satellite;
import org.openslx.bwlp.thrift.iface.UserInfo;
import org.openslx.dozmod.gui.Gui;
import org.openslx.dozmod.gui.window.layout.SatelliteListWindowLayout;

/**
 * Window for selecting an available satellite or setting custom ip.
 */
@SuppressWarnings("serial")
public class SatelliteListWindow extends SatelliteListWindowLayout {

	private Satellite satellite = null;

	public interface UserAddedCallback {
		public void userAdded(UserInfo user, SatelliteListWindow window);
	}

	/**
	 * Don't use this, use the static function open instead.
	 */
	public SatelliteListWindow(final Window modalParent, List<Satellite> satList) {
		super(modalParent, satList);
		// Check, whether we have any satellites
		if (satList != null && !satList.isEmpty()) {
			radioToSat.keySet().iterator().next().setSelected(true);
			customIpField.setEnabled(false);
		} else {
			radioCustomIp.setSelected(true);
		}

		// deactivate the custom field when selecting a satellite to make things clearer for user
		ActionListener listener = new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				customIpField.setEnabled(radioCustomIp.isSelected());
			}
		};
		for (JRadioButton radio : radioToSat.keySet()) {
			radio.addActionListener(listener);
		}
		radioCustomIp.addActionListener(listener);

		// Continue button
		nextButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				// satellite is selected
				if (!radioCustomIp.isSelected()) {
					// check, which satellite is selected
					for (Entry<JRadioButton, Satellite> entry : radioToSat.entrySet()) {
						if (entry.getKey().isSelected()) {
							satellite = entry.getValue();
							break;
						}
					}
				}
				// custom button selected
				if (radioCustomIp.isSelected()) {
					satellite = new Satellite();
					satellite.addressList = new ArrayList<String>();
					satellite.addressList.add(customIpField.getText());
				}
				dispose();
			}
		});

		getRootPane().setDefaultButton(nextButton);

		Gui.centerShellOverShell(modalParent, this);
	}

	private Satellite runAndReturn() {
		setVisible(true);
		return satellite;
	}

	/**
	 * Open a new SatelliteListWindow
	 * 
	 * @param modalParent
	 * @param satList The list of satellites to display.
	 * @return satellite with address to use, or null on error/cancel
	 */
	public static Satellite open(Window modalParent, List<Satellite> satList) {
		return new SatelliteListWindow(modalParent, satList).runAndReturn();
	}

}