OSDN Git Service

from subversion repository
[jindolf/Jindolf.git] / src / main / java / jp / sourceforge / jindolf / WebButton.java
1 /*\r
2  * Web-browser invoke button\r
3  *\r
4  * Copyright(c) 2009 olyutorskii\r
5  * $Id: WebButton.java 955 2009-12-13 13:37:43Z olyutorskii $\r
6  */\r
7 \r
8 package jp.sourceforge.jindolf;\r
9 \r
10 import java.awt.Frame;\r
11 import java.awt.GridBagConstraints;\r
12 import java.awt.GridBagLayout;\r
13 import java.awt.Insets;\r
14 import java.awt.event.ActionEvent;\r
15 import java.awt.event.ActionListener;\r
16 import java.net.MalformedURLException;\r
17 import java.net.URI;\r
18 import java.net.URL;\r
19 import javax.swing.BorderFactory;\r
20 import javax.swing.JButton;\r
21 import javax.swing.JLabel;\r
22 import javax.swing.JPanel;\r
23 import javax.swing.SwingUtilities;\r
24 \r
25 /**\r
26  * Webブラウザ起動ボタン。\r
27  */\r
28 @SuppressWarnings("serial")\r
29 public class WebButton extends JPanel implements ActionListener{\r
30 \r
31     private static final String ACTION_SHOWWEB = "SHOWWEB";\r
32 \r
33     private final JLabel caption;\r
34     private final JButton button;\r
35     private String webUrlText;\r
36 \r
37     /**\r
38      * コンストラクタ。\r
39      */\r
40     public WebButton(){\r
41         super();\r
42 \r
43         this.caption = new JLabel();\r
44         this.button = new JButton("Web");\r
45 \r
46         Monodizer.monodize(this.caption);\r
47         this.button.setIcon(GUIUtils.getWWWIcon());\r
48         this.button.setMargin(new Insets(1, 1, 1, 1));\r
49         this.button.setActionCommand(ACTION_SHOWWEB);\r
50         this.button.addActionListener(this);\r
51         this.button.setToolTipText("Webブラウザで表示");\r
52 \r
53         design();\r
54 \r
55         return;\r
56     }\r
57 \r
58     /**\r
59      * レイアウトの定義。\r
60      */\r
61     private void design(){\r
62         setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));\r
63 \r
64         GridBagLayout layout = new GridBagLayout();\r
65         setLayout(layout);\r
66 \r
67         GridBagConstraints constraints = new GridBagConstraints();\r
68         constraints.fill      = GridBagConstraints.NONE;\r
69         constraints.gridwidth = GridBagConstraints.RELATIVE;\r
70         constraints.insets    = new Insets(0, 0, 0, 10);\r
71 \r
72         add(this.caption, constraints);\r
73         add(this.button,  constraints);\r
74 \r
75         return;\r
76     }\r
77 \r
78     /**\r
79      * {@inheritDoc}\r
80      * @param b {@inheritDoc}\r
81      */\r
82     @Override\r
83     public void setEnabled(boolean b){\r
84         super.setEnabled(b);\r
85         this.button.setEnabled(b);\r
86         return;\r
87     }\r
88 \r
89     /**\r
90      * キャプション文字列の変更。\r
91      * 起動URLには影響しない。\r
92      * @param seq キャプション文字列\r
93      */\r
94     public void setCaption(CharSequence seq){\r
95         this.caption.setText(seq.toString());\r
96         return;\r
97     }\r
98 \r
99     /**\r
100      * Webブラウザに表示させるURLを設定する。\r
101      * キャプション文字列も更新される。\r
102      * @param url URL\r
103      */\r
104     public void setURL(URL url){\r
105         setURLText(url.toString());\r
106         return;\r
107     }\r
108 \r
109     /**\r
110      * Webブラウザに表示させるURIを設定する。\r
111      * キャプション文字列も更新される。\r
112      * @param uri URI\r
113      */\r
114     public void setURI(URI uri){\r
115         setURLText(uri.toString());\r
116         return;\r
117     }\r
118 \r
119     /**\r
120      * Webブラウザに表示させるURL文字列を設定する。\r
121      * キャプション文字列も更新される。\r
122      * @param urlText URL文字列\r
123      */\r
124     public void setURLText(CharSequence urlText){\r
125         String str = urlText.toString();\r
126 \r
127         try{\r
128             new URL(str);\r
129             setEnabled(true);\r
130         }catch(MalformedURLException e){\r
131             setEnabled(false);\r
132         }\r
133 \r
134         this.webUrlText = str;\r
135         setCaption(this.webUrlText);\r
136 \r
137         return;\r
138     }\r
139 \r
140     /**\r
141      * WebブラウザにURLを表示させる。\r
142      */\r
143     public void showDialog(){\r
144         Frame frame =\r
145                 (Frame) SwingUtilities.getAncestorOfClass(Frame.class, this);\r
146         WebIPCDialog.showDialog(frame, this.webUrlText);\r
147         return;\r
148     }\r
149 \r
150     /**\r
151      * ボタン押下イベントの受信。\r
152      * @param event イベント\r
153      */\r
154     public void actionPerformed(ActionEvent event){\r
155         if(event.getSource() != this.button) return;\r
156 \r
157         String command = event.getActionCommand();\r
158         if(command.equals(ACTION_SHOWWEB)){\r
159             showDialog();\r
160         }\r
161 \r
162         return;\r
163     }\r
164 \r
165 }\r