summaryrefslogblamecommitdiffstats
path: root/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/ConfigWindow.java
blob: 3003624c43fcb4ced42084360a15e942ebd925cb (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.Enumeration;

import javax.swing.AbstractButton;
import javax.swing.JRadioButton;
import javax.swing.UIManager;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.thrift.TException;
import org.openslx.bwlp.thrift.iface.SatelliteUserConfig;
import org.openslx.dozmod.Config;
import org.openslx.dozmod.Config.ProxyMode;
import org.openslx.dozmod.gui.Gui;
import org.openslx.dozmod.gui.MainWindow;
import org.openslx.dozmod.gui.helper.I18n;
import org.openslx.dozmod.gui.helper.Language;
import org.openslx.dozmod.gui.helper.MessageType;
import org.openslx.dozmod.gui.helper.UiFeedback;
import org.openslx.dozmod.gui.window.layout.ConfigWindowLayout;
import org.openslx.dozmod.thrift.Session;
import org.openslx.dozmod.thrift.ThriftError;
import org.openslx.thrifthelper.ThriftManager;

public class ConfigWindow extends ConfigWindowLayout implements UiFeedback, ActionListener {

	/**
	 * Version for serialization.
	 */
	private static final long serialVersionUID = 1528773221604107054L;

	private final static Logger LOGGER = LogManager.getLogger(ConfigWindow.class);
	private SatelliteUserConfig userConfig = null;
	private JRadioButton selectedLookAndFeel = null;
	private JRadioButton selectedTransferEnc = null;

	public ConfigWindow(Window modalParent) {
		super(modalParent);
		btnSave.setEnabled(false);

		// -- PROXY --
		// set proxy mode as saved in config
		switch (Config.getProxyMode()) {
		case NONE:
			btnProxyNone.setSelected(true);
			break;
		case AUTO:
			btnProxyAuto.setSelected(true);
			break;
		case HTTP_CONNECT:
		case SOCKS:
		default:
			break;
		}
		btnProxyNone.addActionListener(this);
		btnProxyAuto.addActionListener(this);
		// -- END PROXY --

		// -- SAT USER CONFIG --
		// get the satellite user config and set the checkbox accordingly
		if (Session.getSatelliteToken() == null) {
			chkSendMeMail.setEnabled(false);
		} else {
			userConfig = getSatUserConfig();
			if (userConfig != null) {
				chkSendMeMail.setSelected(userConfig.isEmailNotifications());
			} else {
				chkSendMeMail.setSelected(false);
			}
			chkSendMeMail.addActionListener(this);
			lblYourAddress.setText(I18n.WINDOW.getString("Config.Label.yourAddress.text", Session.getEMail()));
		}
		// -- END USER CONFIG --

		ChangeListener changeListener = new ChangeListener() {
			@Override
			public void stateChanged(ChangeEvent e) {
				reactToInput();
			}
		};

		// -- FONT CONFIG --
		// set font scaling as saved in config
		sldFontSize.setValue(Config.getFontScaling());
		sldFontSize.addChangeListener(changeListener);
		// -- Look And Feel --
		for (Enumeration<AbstractButton> btn = btnGroupLookAndFeel.getElements(); btn.hasMoreElements();) {
			final JRadioButton b = (JRadioButton) btn.nextElement();
			if (Config.getLookAndFeel() != null && !Config.getLookAndFeel().isEmpty()) {
				if (b.getToolTipText().equals(Config.getLookAndFeel())) {
					selectedLookAndFeel = b;
					b.setSelected(true);
				}
			}
			// check if we set from the config, if not fall back to system look and feel
			if (selectedLookAndFeel == null && b.getToolTipText().equals(UIManager.getSystemLookAndFeelClassName())) {
				selectedLookAndFeel = b;
				b.setSelected(true);
			}
			b.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					selectedLookAndFeel = b;
					reactToInput();
				}
			});
		}
		if (selectedLookAndFeel == null) {
			// non-critical but log it anyways
			LOGGER.error("Failed to detect the current look & feel theme.");
		}
		
		// -- Transfer connection encryption --
		for (Enumeration<AbstractButton> btn = btnGroupTransferEnc.getElements(); btn.hasMoreElements();) {
			final JRadioButton b = (JRadioButton) btn.nextElement();
			if (Config.getFileTransferMode().name().equals(b.getToolTipText())) {
				selectedTransferEnc = b;
				b.setSelected(true);
			}
			b.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					selectedTransferEnc = b;
					reactToInput();
				}
			});
		}
		if (selectedTransferEnc == null) {
			selectedTransferEnc = (JRadioButton) btnGroupTransferEnc.getElements().nextElement();
			selectedTransferEnc.setSelected(true);
		}

		// Transfer connection count
		sldConnections.setValue(Config.getTransferConnectionCount());
		sldConnections.addChangeListener(changeListener);

		// Language
		for (Language language : Language.values()) {
			if (language.value.equals(Config.getPreferredLanguage())) {
				cboLanguage.setSelectedItem(language);
				break;
			}
		}
		cboLanguage.addActionListener(this);

		// -- BOTTOM BUTTONS --
		btnSave.addActionListener(this);
		btnClose.addActionListener(this);
		// -- END BOTTOM BUTTONS --
		pack();
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		Object source = e.getSource();
		if (source == btnProxyAuto || source == btnProxyHttp || source == btnProxyNone
				|| source == btnProxySocks || source == chkSendMeMail || source == cboLanguage) {
			reactToInput();
		}
		if (source == btnSave) {
			saveOptions();
		}
		if (source == btnClose) {
			dispose();
		}
	}

	private void reactToInput() {
		// our flag to en/disable the save button
		boolean changed = false;
		// first check our initial state
		ProxyMode initMode = Config.getProxyMode();
		int fontScaling = Config.getFontScaling();
		Language newLanguage = (Language)cboLanguage.getSelectedItem();

		// now check if the buttons represent another state
		if (btnProxyNone.isSelected() && initMode != ProxyMode.NONE) {
			changed = true;
		} else if (btnProxyAuto.isSelected() && initMode != ProxyMode.AUTO) {
			changed = true;
		} else if (chkSendMeMail.isEnabled() && chkSendMeMail.isSelected() != userConfig.emailNotifications) {
			changed = true;
		} else if ((int) sldFontSize.getValue() != fontScaling) {
			changed = true;
		} else if ((int) sldConnections.getValue() != Config.getTransferConnectionCount()) {
			changed = true;
		} else if (!selectedLookAndFeel.getToolTipText().equals(Config.getLookAndFeel())) {
			changed = true;
		} else if (!selectedTransferEnc.getToolTipText().equals(Config.getFileTransferMode().name())) {
			changed = true;
		} else if (!newLanguage.value.equals(Config.getPreferredLanguage())) {
			changed = true;
		}

		// en/disable the save button
		btnSave.setEnabled(changed);
	}

	private SatelliteUserConfig getSatUserConfig() {
		SatelliteUserConfig userConfig = null;
		// set mail me stuff as saved on the sat
		try {
			userConfig = ThriftManager.getSatClient().getUserConfig(Session.getSatelliteToken());
		} catch (TException e) {
			ThriftError.showMessage(this, LOGGER, e,
					I18n.WINDOW.getString("Config.Message.error.couldNotGetUserConfFromSat"));
		}
		return userConfig;
	}

	private boolean setSatUserConfig(SatelliteUserConfig config) {
		if (config == null)
			return false;
		// set mail me stuff as saved on the sat
		try {
			ThriftManager.getSatClient().setUserConfig(Session.getSatelliteToken(), config);
			userConfig = config;
		} catch (TException e) {
			ThriftError.showMessage(this, LOGGER, e,
					I18n.WINDOW.getString("Config.Message.error.couldNotSaveUserConfOnSat"));
			return false;
		}
		return true;
	}

	private void saveOptions() {
		boolean restartRequired = false;
		btnSave.setEnabled(false);
		// save proxy mode to config
		ProxyMode selectedMode = null;
		if (btnProxyAuto.isSelected())
			selectedMode = ProxyMode.AUTO;
		if (btnProxyNone.isSelected())
			selectedMode = ProxyMode.NONE;
		if (selectedMode != null) {
			restartRequired = restartRequired || Config.getProxyMode() != selectedMode;
			Config.setProxyMode(selectedMode);
		}

		// save font scaling to config
		int newFontScaling = (int) sldFontSize.getValue();
		restartRequired = restartRequired || Config.getFontScaling() != newFontScaling;
		Config.setFontScaling(newFontScaling);
		// transfers
		Config.setTransferConnectionCount((int) sldConnections.getValue());

		// save mail notification stuff to sat
		if (Session.getSatelliteToken() != null
				&& !setSatUserConfig(new SatelliteUserConfig(chkSendMeMail.isSelected()))) {
			// If saving failed, re-enable button
			btnSave.setEnabled(true);
		}

		// save LookAndFeel
		if (selectedLookAndFeel != null) {
			restartRequired = restartRequired || !selectedLookAndFeel.getToolTipText().equals(Config.getLookAndFeel());
			Config.setLookAndFeel(selectedLookAndFeel.getToolTipText());
		}
		// save TransferEnc
		if (selectedTransferEnc != null) {
			Config.setFileTransferMode(selectedTransferEnc.getToolTipText());
		}

		// save language
		Language newLanguage = (Language)cboLanguage.getSelectedItem();
		restartRequired = restartRequired || !newLanguage.value.equals(Config.getPreferredLanguage());
		Config.setPreferredLanguage(newLanguage.value);

		if (restartRequired) {
			// let the user know he needs to restart for the changes to apply
			Gui.showMessageBox(this, I18n.WINDOW.getString("Config.Message.info.restartNeededToApplyChanges"),
					MessageType.INFO, null, null);
		}
	}

	public static boolean shouldBeShown() {
		return true;
	}

	public static void open(Window modalParent) {
		ConfigWindow win = new ConfigWindow(modalParent);
		MainWindow.centerShell(win);
		win.setVisible(true);
	}

	/* 
	 * UIFeedback Implementation
	 */
	@Override
	public boolean wantConfirmQuit() {
		return false;
	}

	@Override
	public void escapePressed() {
		dispose();
	}
}