View Javadoc

1   /*
2    * Created on Jan 7, 2005
3    * 
4    *   Magic-Project is a turn based strategy simulator
5    *   Copyright (C) 2003-2007 Fabrice Daugan
6    *
7    *   This program is free software; you can redistribute it and/or modify it 
8    * under the terms of the GNU General Public License as published by the Free 
9    * Software Foundation; either version 2 of the License, or (at your option) any
10   * later version.
11   *
12   *   This program is distributed in the hope that it will be useful, but WITHOUT 
13   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14   * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more 
15   * details.
16   *
17   *   You should have received a copy of the GNU General Public License along  
18   * with this program; if not, write to the Free Software Foundation, Inc., 
19   * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20   */
21  package net.sf.magicproject.ui.component;
22  
23  import java.awt.Cursor;
24  import java.awt.event.MouseEvent;
25  import java.awt.event.MouseListener;
26  
27  import javax.swing.Icon;
28  import javax.swing.JLabel;
29  import javax.swing.JOptionPane;
30  import javax.swing.SwingConstants;
31  
32  import net.sf.magicproject.tools.WebBrowser;
33  import net.sf.magicproject.ui.MagicUIComponents;
34  import net.sf.magicproject.ui.i18n.LanguageManager;
35  
36  /***
37   * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
38   * @since 0.82
39   */
40  public class JLink extends JLabel implements MouseListener {
41  
42  	/***
43  	 * Create a new instance of this class.
44  	 * 
45  	 * @param link
46  	 *          the url link.
47  	 * @param text
48  	 *          the displayed text.
49  	 * @param allign
50  	 *          the allignment.
51  	 */
52  	public JLink(String link, String text, int allign) {
53  		this(link, text, null, allign);
54  	}
55  
56  	/***
57  	 * Create a new instance of this class.
58  	 * 
59  	 * @param link
60  	 *          the url link.
61  	 * @param text
62  	 *          the displayed text.
63  	 */
64  	public JLink(String link, String text) {
65  		this(link, text, null, 0);
66  	}
67  
68  	/***
69  	 * Create a new instance of this class.
70  	 * 
71  	 * @param link
72  	 *          the url link.
73  	 * @param icon
74  	 *          the displayed icon.
75  	 * @param allign
76  	 *          the allignment.
77  	 */
78  	public JLink(String link, Icon icon, int allign) {
79  		this(link, null, icon, allign);
80  	}
81  
82  	/***
83  	 * Create a new instance of this class.
84  	 * 
85  	 * @param link
86  	 *          the url link.
87  	 * @param icon
88  	 *          the displayed icon.
89  	 */
90  	public JLink(String link, Icon icon) {
91  		this(link, null, icon, 0);
92  	}
93  
94  	/***
95  	 * Create a new instance of this class.
96  	 * 
97  	 * @param link
98  	 *          the url link.
99  	 * @param text
100 	 *          the displayed text.
101 	 * @param icon
102 	 *          the displayed icon.
103 	 */
104 	public JLink(String link, String text, Icon icon) {
105 		this(link, text, icon, 0);
106 	}
107 
108 	/***
109 	 * Create a new instance of this class.
110 	 * 
111 	 * @param link
112 	 *          the url link.
113 	 * @param text
114 	 *          the displayed text.
115 	 * @param icon
116 	 *          the displayed icon.
117 	 * @param allign
118 	 *          the allignment.
119 	 */
120 	public JLink(String link, String text, Icon icon, int allign) {
121 		super(text == null ? null : new StringBuilder("<html><a href=\"").append(
122 				link).append("\">").append(text).append("<a>").toString(), icon, allign);
123 		setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
124 		setHorizontalAlignment(SwingConstants.LEFT);
125 		this.link = link;
126 		addMouseListener(this);
127 	}
128 
129 	/*
130 	 * (non-Javadoc)
131 	 * 
132 	 * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
133 	 */
134 	public void mouseClicked(MouseEvent e) {
135 		try {
136 			WebBrowser.launchBrowser(link);
137 		} catch (Exception ex) {
138 			JOptionPane.showMessageDialog(MagicUIComponents.magicForm,
139 					LanguageManager.getString("error") + " : " + ex.getMessage(),
140 					LanguageManager.getString("web-pb"), JOptionPane.INFORMATION_MESSAGE,
141 					null);
142 		}
143 	}
144 
145 	public void mousePressed(MouseEvent e) {
146 		// Ignore this event
147 	}
148 
149 	public void mouseReleased(MouseEvent e) {
150 		// Ignore this event
151 	}
152 
153 	public void mouseEntered(MouseEvent e) {
154 		// Ignore this event
155 	}
156 
157 	public void mouseExited(MouseEvent e) {
158 		// Ignore this event
159 	}
160 
161 	/***
162 	 * The link
163 	 */
164 	private String link;
165 }