OSDN Git Service

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