View Javadoc

1   /*
2    * $HeadURL: https://mindtreeinsight.svn.sourceforge.net/svnroot/mindtreeinsight/insight/insight-ui/trunk/src/main/java/com/mindtree/techworks/insight/gui/util/AboutFrame.java $
3    * $Date: 2007-12-16 05:20:57 -0700 (Sun, 16 Dec 2007) $
4    * $Revision: 30 $
5    * $Author: bindul $
6    * 
7    * Copyright (c) 2005 MindTree Consulting Ltd. 
8    * 
9    * This file is part of Insight.
10   * 
11   * Insight is free software: you can redistribute it and/or modify it under the 
12   * terms of the GNU General Public License as published by the Free Software 
13   * Foundation, either version 3 of the License, or (at your option) any later 
14   * version.
15   * 
16   * Insight is distributed in the hope that it will be useful, but 
17   * WITHOUT ANY WARRANTY; without even the implied warranty of 
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General 
19   * Public License for more details.
20   * 
21   * You should have received a copy of the GNU General Public License along with 
22   * Insight.  If not, see <http://www.gnu.org/licenses/>.
23   */
24  package com.mindtree.techworks.insight.gui.util;
25  
26  import java.awt.BorderLayout;
27  import java.awt.Component;
28  import java.awt.Dimension;
29  import java.awt.GridBagConstraints;
30  import java.awt.GridBagLayout;
31  import java.awt.Insets;
32  import java.awt.event.ActionEvent;
33  import java.awt.event.ActionListener;
34  import java.io.IOException;
35  import java.text.MessageFormat;
36  
37  import javax.swing.JButton;
38  import javax.swing.JDialog;
39  import javax.swing.JLabel;
40  import javax.swing.JOptionPane;
41  import javax.swing.JPanel;
42  import javax.swing.JScrollPane;
43  import javax.swing.JTextPane;
44  import javax.swing.event.HyperlinkEvent;
45  import javax.swing.event.HyperlinkListener;
46  
47  import com.mindtree.techworks.insight.InsightConstants;
48  import com.mindtree.techworks.insight.ResourceManager;
49  import com.mindtree.techworks.insight.VersionUtil;
50  import com.mindtree.techworks.insight.gui.Insight;
51  
52  /**
53  *
54  * The <code>AboutFrame</code> class displays details about Insight
55  * such as CopyRight information, contact information e.t.c.
56  *
57  * @author  Regunath B
58  * @version 1.0, 05/02/24
59  */
60  
61  public class AboutFrame extends JDialog {
62  	
63  	/**
64  	 * UID for the serialized form
65  	 */
66  	private static final long serialVersionUID = 220882995949861144L;
67  
68  	/**
69  	 * Constants that identifies the Hyperlink event type
70  	 */
71  	private static final String ACTIVATED = "ACTIVATED";
72  	
73  	/**
74  	 * The MessageFormat instance used for formatting the event details display
75  	 */
76      private static final MessageFormat FORMATTER = new MessageFormat(
77              "<code>Version {0} (Build Id: {1})</code>" +
78      		"<br><code>CopyRight © MindTree Consulting Pvt. Ltd.</code>" +
79  			"<br><code>All Rights reserved.</code>" +
80  			"<br><br><code>Support:</code>"	+
81  			"<br><code>Email - <a href = 'mailto:mindtreeinsight-users@lists.sourceforge.net'>mindtreeinsight-users@lists.sourceforge.net</a></code>" + 		
82  			"<br><code>Intranet - <a href = 'http://mindtreeinsight.sourceforge.net/'>http://mindtreeinsight.sourceforge.net/</a></code>"  		
83  			);
84  		
85  	/**
86  	 * The Width of the frame 
87  	 */
88  	private static final int FRAME_WIDTH = 300;
89  	
90  	/**
91  	 * The Height of the frame
92  	 */
93  	private static final int FRAME_HEIGHT = 200;
94  	
95  	/**
96  	 * Layout definition.
97  	 */
98  	private GridBagLayout gl;
99  	
100 	/**
101 	 * Layout constants
102 	 */
103 	private GridBagConstraints gc;
104 	
105 	/**
106 	 * The JEditorPane instance used to render the details
107 	 */
108 	private JTextPane detailsPane;	
109 	
110 	/**
111 	 * Buttons in the display
112 	 */
113 	protected JButton closeButton;
114 	
115 	/**
116 	 * The Insight instance that created this frame
117 	 */
118 	private Insight insight;
119 	
120 	/**
121 	 * Constructor for this class
122 	 * @param insight the Insight instance for this class
123 	 */
124 	public AboutFrame(Insight insight) {
125 		super(JOptionPane.getFrameForComponent(insight),InsightConstants.getLiteral("ABOUT_TITLE"),true);
126 		this.setResizable(false);
127 		this.insight = insight;
128 		gl = new GridBagLayout();
129 		gc = new GridBagConstraints();
130 		getContentPane().setLayout(gl);
131 
132         detailsPane = new JTextPane();
133         detailsPane.setEditable(false);
134         detailsPane.setContentType("text/html");
135         
136         JPanel containerPanel = new JPanel();
137         containerPanel.setLayout(new BorderLayout());      
138         containerPanel.add(new JLabel(ResourceManager.getInstance().loadImageIcon("INSIGHT_IMAGE")), BorderLayout.NORTH);
139         containerPanel.add(detailsPane, BorderLayout.SOUTH);
140         
141         JScrollPane scrollPane = new JScrollPane(containerPanel);
142         
143         detailsPane.setPreferredSize(new Dimension(FRAME_WIDTH, FRAME_HEIGHT));
144 		addComponent(scrollPane,0,0,1,1,2,1);
145 		
146 		closeButton = new JButton(InsightConstants.getLiteral("OK"));
147 		addComponent(closeButton,0,1,0,0,1,1);
148 
149 		ButtionActionProcessor actionProcessor = new ButtionActionProcessor();
150 		closeButton.addActionListener(actionProcessor);
151 		// add a hyperlink listener to process click on hyperlinks in the text
152 		detailsPane.addHyperlinkListener(actionProcessor);
153 		
154         final Object[] args =         {
155         		VersionUtil.getInsightVersion(),
156         		VersionUtil.getInsightBuild(),
157             };
158         detailsPane.setText(FORMATTER.format(args));		
159 		
160 		// display this frame
161 		this.pack();
162 		// Center this window in Insight
163 		int insightX = insight.getX();
164 		int insightY = insight.getY();
165 		int thisX = insightX + ((this.insight.getWidth() - this.getWidth())/ 2);
166 		int thisY = insightY + ((this.insight.getHeight() - this.getHeight())/ 2);
167 		this.setLocation(thisX, thisY);
168 		this.show();	
169 	}
170 	
171 	/**
172 	 * Adds the specified component to this panel using the specified
173 	 * constraints.
174 	 * 
175 	 * @param c
176 	 *            The component to layout
177 	 * @param gridx
178 	 *            X location in the Grid
179 	 * @param gridy
180 	 *            Y location in the Grid
181 	 * @param weightx
182 	 *            Weight in X direction
183 	 * @param weighty
184 	 *            Weight in Y direction
185 	 * @param width
186 	 *            Width
187 	 * @param height
188 	 *            Height
189 	 */	
190 	private final void addComponent(Component c, int gridx, int gridy, int weightx,
191 		int weighty, int width, int height) {
192 		gc.fill = GridBagConstraints.BOTH;
193 		gc.anchor = GridBagConstraints.NORTHWEST;
194 		gc.insets = new Insets(5,5,5,5);		
195 		gc.gridx = gridx;
196 		gc.gridy = gridy;
197 		gc.weightx = weightx;
198 		gc.weighty = weighty;
199 		gc.gridwidth = width;
200 		gc.gridheight = height;		
201 		gl.setConstraints( c, gc);
202 		getContentPane().add(c);
203 	}
204 	
205 	/**
206 	 * Helper class to handle action events 
207 	 */	
208 	private final class ButtionActionProcessor implements ActionListener, HyperlinkListener {
209 		/** 
210 		 * Interface method implementation
211 		 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
212 		 */
213 		public void actionPerformed(ActionEvent e) {
214 			Object source = e.getSource();
215 			if (source == closeButton) {
216 				hide();
217 				dispose();
218 			}
219 		}
220 
221 		/**
222 		 * Interface method implementation
223 		 * @see javax.swing.event.HyperlinkListener#hyperlinkUpdate(javax.swing.event.HyperlinkEvent)
224 		 */
225 		public void hyperlinkUpdate(HyperlinkEvent event) {
226 			// Open the browser for only 'ACTIVATED' event types
227 			if (event.getEventType().toString() == ACTIVATED) {
228 				new BrowserControl().displayURL(event.getURL().toString());
229 			}
230 		}
231 	}
232 
233 	/**
234 	 * Helper class that opens the default platform browser in order to display
235 	 * the specified URL. 
236 	 */
237 	private class BrowserControl {
238 		
239 	    /**
240 	     * Used to identify the windows platform.
241 	     */
242 	    private static final String WIN_ID = "Windows";
243 	    
244 	    /**
245 	     * The default system browser under windows.
246 	     */ 
247 	    private static final String WIN_PATH = "rundll32";
248 	    
249 	    /**
250 	     * The flag to display a url.
251 	     */
252 	    private static final String WIN_FLAG = "url.dll,FileProtocolHandler";
253 	    
254 	    /**
255 	     * The default browser under unix.
256 	     */
257 	    private static final String UNIX_PATH = "netscape";
258 	    
259 	    /**
260 	     * The flag to display a url.
261 	     */
262 	    private static final String UNIX_FLAG = "-remote openURL";
263 	    
264 	    /**
265 	     * Display a file in the system browser. Include the absolute path name
266 	     * in order to open a file on the file system
267 	     * @param url the file's url (the url must start with either "http://" or "file://").
268 	     */
269 	    public void displayURL(String url) {
270 	        boolean windows = isWindowsPlatform();
271 	        String cmd = null;
272 	        try {
273 	            if (windows) {
274 	                cmd = WIN_PATH + " " + WIN_FLAG + " " + url;
275 	                Runtime.getRuntime().exec(cmd);
276 	            } else {
277 	                // Under Unix, Netscape has to be running for the "-remote"
278 	                // command to work.  So, we try sending the command and
279 	                // check for an exit value.  If the exit command is 0,
280 	                // it worked, otherwise we need to start the browser.
281 	                // cmd = 'netscape -remote openURL(http://www.javaworld.com)'
282 	                cmd = UNIX_PATH + " " + UNIX_FLAG + "(" + url + ")";
283 	                Process p = Runtime.getRuntime().exec(cmd);
284 	                try {
285 	                    // wait for exit code -- if it's 0, command worked,
286 	                    // otherwise we need to start the browser up.
287 	                    int exitCode = p.waitFor();
288 	                    if (exitCode != 0) {
289 	                        // Command failed, start up the browser
290 	                        // cmd = 'netscape http://www.javaworld.com'
291 	                        cmd = UNIX_PATH + " "  + url;
292 	                        p = Runtime.getRuntime().exec(cmd);
293 	                    }
294 	                } catch(InterruptedException e) {
295 	                	e.printStackTrace();
296 	                }
297 	            }
298 	        } catch(IOException e) {
299             	e.printStackTrace();
300 	        }
301 	    }
302 	    /**
303 	     * Try to determine whether this application is running under Windows
304 	     * or some other platform by examing the "os.name" property.
305 	     *
306 	     * @return true if this application is running under a Windows OS
307 	     */
308 	    private boolean isWindowsPlatform() {
309 	        String os = System.getProperty("os.name");
310 	        if ( os != null && os.startsWith(WIN_ID)) {
311 	            return true;
312 	        }
313 			return false;
314 	    }
315 	}	
316 }