View Javadoc

1   package es.inteco.eni.swing;
2   
3   import java.awt.BorderLayout;
4   import java.awt.Dimension;
5   import java.awt.Frame;
6   import java.awt.Point;
7   import java.awt.event.ActionEvent;
8   import java.awt.event.ActionListener;
9   import java.awt.event.WindowAdapter;
10  import java.awt.event.WindowEvent;
11  import java.io.IOException;
12  import java.net.URL;
13  
14  import javax.swing.JEditorPane;
15  import javax.swing.JFrame;
16  import javax.swing.JScrollPane;
17  
18  import org.apache.log4j.Logger;
19  
20  import es.inteco.eni.commons.MessagesHelper;
21  import es.inteco.eni.utils.ExceptionUtils;
22  
23  /**
24   * Action listener para gestionar el click en el menĂº "Acerca de"
25   * 
26   * 
27   *
28   */
29  public class AboutActionListener implements ActionListener{
30  	/**
31  	 * Log
32  	 */
33  	private static Logger log = Logger.getLogger(AboutActionListener.class);
34  
35  	/**
36  	 * Ventana del validador
37  	 */
38  	private ENIValidator eniValidator;
39  	/**
40  	 * Ventana del "Acerca de"
41  	 */
42  	private JFrame aboutWindow;
43  
44  	/**
45  	 * Constructor
46  	 * @param eniValidator
47  	 */
48  	public AboutActionListener(ENIValidator eniValidator){
49  		this.eniValidator = eniValidator;
50  	}
51  
52  	/**
53  	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
54  	 */
55  	@Override
56  	public void actionPerformed(ActionEvent e) {
57  		aboutWindow = new JFrame();
58  		aboutWindow.setTitle(MessagesHelper.getText("eni.windows.frame.about.title"));
59  		URL helpURL = ENIValidator.class.getResource("/html/about.html");
60  
61  		//Cargamos el html del "Acerca de"
62  		JEditorPane aboutPane = null;
63  		try {
64  			aboutPane = new JEditorPane(helpURL);
65  		} catch (IOException ioe) {
66  			log.error(ExceptionUtils.stackTraceToString(ioe));
67  		}
68  		aboutPane.setEditable(false);
69  		JScrollPane scroller = new JScrollPane();
70  		scroller.getViewport().add(aboutPane);
71  		aboutWindow.getContentPane().add(BorderLayout.CENTER, scroller);
72  
73  
74  		aboutWindow.setVisible(true);
75  		aboutWindow.setResizable(false);
76  		aboutWindow.setSize(500,300);
77  		aboutWindow.setIconImage(ENIValidator.eniIco);
78  		//Centramos la ventana
79  		centerWindow();
80  		//Desactivamos la ventana principal
81  		eniValidator.setEnabled(false);
82  
83  		//Al cerrar la ventana, activamos la ventana principal
84  		aboutWindow.addWindowListener(new WindowAdapter() {
85  			/**
86  			 * @see java.awt.event.WindowAdapter#windowClosing(java.awt.event.WindowEvent)
87  			 */
88  			public void windowClosing(WindowEvent e) {
89  				aboutWindow.setVisible(false);
90  				aboutWindow.dispose();
91  				eniValidator.setEnabled(true);
92  				eniValidator.toFront();
93  				eniValidator.setState(Frame.NORMAL);
94  
95  			}
96  		});
97  	}
98  
99  	/**
100 	 * Centra la ventana del "Acerca de "
101 	 */
102 	private void centerWindow(){
103 		int x=0;
104 		int y=0;
105 		Point topLeft = eniValidator.getLocationOnScreen();
106 		Dimension parentSize = eniValidator.getSize();
107 
108 		Dimension mySize = aboutWindow.getSize();
109 
110 		if (parentSize.width > mySize.width){ 
111 			x = ((parentSize.width - mySize.width)/2) + topLeft.x;
112 		}else{ 
113 			x = topLeft.x;
114 		}
115 
116 		if (parentSize.height > mySize.height){
117 			y = ((parentSize.height - mySize.height)/2) + topLeft.y;
118 		}else{ 
119 			y = topLeft.y;
120 		}
121 		aboutWindow.setLocation (x, y);
122 	}
123 }