View Javadoc

1   package helloagain.kanday;
2   
3   import java.awt.Color;
4   import java.awt.Container;
5   import java.awt.Dimension;
6   import java.awt.Font;
7   import java.awt.Frame;
8   import java.awt.event.ActionEvent;
9   import java.awt.event.ActionListener;
10  import java.util.logging.Level;
11  import java.util.logging.Logger;
12  import javax.swing.JButton;
13  import javax.swing.JColorChooser;
14  import javax.swing.JDialog;
15  import javax.swing.JLabel;
16  import javax.swing.JTextField;
17  import javax.swing.SpringLayout;
18  
19  /***
20   * プログラムの設定を変更するダイアログ。
21   * メッセージ表示ペーンの前景色、背景色を変更する。
22   * レイアウトには、<code>SpringLayout</code>を使用する。
23   * <pre>
24   * 画面デザイン概要
25   * +--------------------------------+
26   * |Preferences                     |  <-- タイトル
27   * +--------------------------------+
28   * |Foreground Color: ■  [Change]  |  <-- JLabel + JTextField + JButton
29   * |Background Color: □  [Change]  |  <-- 同上
30   * |                                |
31   * |           [Apply]  [Cancel]    |  <-- JButton + JButton
32   * +--------------------------------+
33   * </pre>
34   * 前景色と背景色の現在の色を表示するのに、空のJTextFieldを使用し、
35   * backgroundに設定して実現している。
36   * 表示する文字は、<code>KandayPreferences</code>から取得している。
37   * <pre>
38   * 配置位置の設定(SpringLayout)
39   * +------------------------------------------+
40   * |ダイアログのタイトル                    |
41   * +------------------------------------------+
42   * | 1     |2     3     |4       5     |6     |
43   * |<->[ JLabel ]<->[JTextField]<->[JButton]  |
44   * |            ^^^^^^^^|^^^^^^^^^^^^^^|      |
45   * | 7     |8   : 9     |10      11    |12    |
46   * |<->[JLabel] :<->[JTextField]<->[JButton]  |
47   * |          :                               |
48   * |          :13         14                  |
49   * |          :<->[Apply]<->[Cancel]          |
50   * |                 |15       |16            |
51   * +------------------------------------------+
52   * [ 1]  10 pixel    [ 6]  25 pixel    [11]  10 pixel    [16] -10 pixel
53   * [ 2]  25 pixel    [ 7]  10 pixel    [12]  20 pixel
54   * [ 3]  10 pixel    [ 8]  20 pixel    [13]  10 pixel
55   * [ 4]  25 pixel    [ 9]  10 pixel    [14]  25 pixel
56   * [ 5]  10 pixel    [10]  20 pixel    [15] -10 pixel
57   * </pre>
58   * <ul>
59   * <li>[9]は、左隣のJLabelではなく、上の行のJLabelとの間隔で指定している。
60   * <li>[10]と[12]は、JLabelとの間隔で指定している。
61   * <li>[15]と[16]は、間隔が負の値となっている。これはcontentPaneのSOUTHから
62   * 内側に位置するためである。
63   * </ul>
64   * <p>
65   * Created: Thu Jun 10 05:45:53 2004
66   *
67   * @author <a href="mailto:torutk@02.246.ne.jp">TAKAHASHI,Toru</a>
68   * @version 1.0
69   * @see javax.swing.SpringLayout
70   */
71  public class KandayPreferencesDialog extends JDialog {
72  
73      /***
74       * プログラムの設定を変更するダイアログを生成する。
75       * @param anOwner このダイアログの親となるフレーム
76       * @param aPreferences 設定情報を保持するインスタンス
77       */
78      public KandayPreferencesDialog(
79          final Frame anOwner, final KandayPreferences aPreferences
80      ) {
81          super(anOwner, aPreferences.getPreferencesDialogTitle(), true);
82          if (logger.isLoggable(Level.FINER)) {
83              logger.entering(
84                  MYCLASS, "KandayPreferencesDialog",
85                  new Object[] {anOwner, aPreferences}
86              );
87          }
88          foregroundColor = aPreferences.getMessageForegroundColor();
89          backgroundColor = aPreferences.getMessageBackgroundColor();
90          SpringLayout layout = new SpringLayout();
91          Container contentPane = getContentPane();
92          contentPane.setLayout(layout);
93  
94          // Foreground Colorラベルの生成
95          JLabel foregroundLabel = new JLabel(
96              aPreferences.getPreferencesDialogFglabelText()
97          );
98          // 前景色表示フィールドの生成
99          final JTextField foregroundField = new JTextField(8);
100         foregroundField.setEditable(false);
101         foregroundField.setBackground(foregroundColor);
102         // 前景色変更ボタンの生成
103         JButton foregroundChangeButton = new JButton(
104             aPreferences.getPreferencesDialogChangebuttonText()
105         );
106         // JLabel/JTextFieldの高さとJButtonの高さがデフォルトでは不釣合
107         // のため、JButtonの高さをJTextFieldの高さに揃える
108         foregroundChangeButton.setPreferredSize(
109             new Dimension(foregroundChangeButton.getPreferredSize().width,
110                           foregroundField.getPreferredSize().height)
111         );
112         // 変更ボタンが押されたら、色選択ダイアログを起動
113         foregroundChangeButton.addActionListener(new ActionListener() {
114                 public void actionPerformed(ActionEvent ev) {
115                     Color newColor = JColorChooser.showDialog(
116                         KandayPreferencesDialog.this,
117                         aPreferences.getPreferencesDialogFgcolorchooserTitle(),
118                         foregroundColor
119                     );
120                     if (newColor != null) {
121                         foregroundColor = newColor;
122                         foregroundField.setBackground(foregroundColor);
123                     }
124                 }
125             }
126         );
127 
128         // Background Colorラベルの生成
129         JLabel backgroundLabel = new JLabel(
130             aPreferences.getPreferencesDialogBglabelText()
131         );
132         // 背景色表示フィールドの生成
133         final JTextField backgroundField = new JTextField(8);
134         backgroundField.setEditable(false);
135         backgroundField.setBackground(backgroundColor);
136         // 背景色変更ボタンの生成
137         JButton backgroundChangeButton = new JButton(
138             aPreferences.getPreferencesDialogChangebuttonText()
139         );
140         backgroundChangeButton.setPreferredSize(
141             new Dimension(backgroundChangeButton.getPreferredSize().width,
142                           backgroundField.getPreferredSize().height)
143         );
144         backgroundChangeButton.addActionListener(new ActionListener() {
145                 public void actionPerformed(ActionEvent ev) {
146                     Color newColor = JColorChooser.showDialog(
147                         KandayPreferencesDialog.this,
148                         aPreferences.getPreferencesDialogBgcolorchooserTitle(),
149                         backgroundColor
150                     );
151                     if (newColor != null) {
152                         backgroundColor = newColor;
153                         backgroundField.setBackground(backgroundColor);
154                     }
155                 }
156             }
157         );
158 
159         // Applyボタンの生成
160         JButton applyButton = new JButton(
161             aPreferences.getPreferencesDialogApplybuttonText()
162         );
163         // Applyボタンが押されたら、KandayPreferencesを変更して
164         // KandayFrameに変更通知してからダイアログ破棄
165         applyButton.addActionListener(new ActionListener() {
166                 public void actionPerformed(ActionEvent ev) {
167                     boolean viewUpdate = false;
168                     if (!aPreferences.getMessageForegroundColor().equals(
169                             foregroundColor)
170                     ) {
171                         aPreferences.setMessageForegroundColor(foregroundColor);
172                         viewUpdate = true;
173                         logger.finer("apply fgcolor=" + foregroundColor);
174                     }
175                     if (!aPreferences.getMessageBackgroundColor().equals(
176                             backgroundColor)
177                     ) {
178                         aPreferences.setMessageBackgroundColor(backgroundColor);
179                         viewUpdate =true;
180                         logger.finer("apply bgcolor=" + backgroundColor);
181                     }
182                     if (viewUpdate == true) {
183                         ((KandayFrame)anOwner).updatePreferences();
184                     }
185                     KandayPreferencesDialog.this.dispose();
186                 }
187             }
188         );
189         // Cancelボタンの生成
190         JButton cancelButton = new JButton(
191             aPreferences.getPreferencesDialogCancelbuttonText()
192         );
193         // Cancelボタンが押されたら、ダイアログを破棄
194         cancelButton.addActionListener(new ActionListener() {
195                 public void actionPerformed(ActionEvent ev) {
196                     KandayPreferencesDialog.this.dispose();
197                 }
198             });
199         
200         contentPane.add(foregroundLabel);
201         contentPane.add(foregroundField);
202         contentPane.add(foregroundChangeButton);
203         contentPane.add(backgroundLabel);
204         contentPane.add(backgroundField);
205         contentPane.add(backgroundChangeButton);
206         contentPane.add(applyButton);
207         contentPane.add(cancelButton);
208 
209         layout.putConstraint(SpringLayout.WEST, foregroundLabel, 10, 
210                               SpringLayout.WEST, contentPane);
211         layout.putConstraint(SpringLayout.NORTH, foregroundLabel, 25,
212                               SpringLayout.NORTH, contentPane);
213         layout.putConstraint(SpringLayout.WEST, foregroundField, 10,
214                               SpringLayout.EAST, foregroundLabel);
215         layout.putConstraint(SpringLayout.NORTH, foregroundField, 25,
216                               SpringLayout.NORTH, contentPane);
217         layout.putConstraint(SpringLayout.WEST, foregroundChangeButton, 10,
218                               SpringLayout.EAST, foregroundField);
219         layout.putConstraint(SpringLayout.NORTH, foregroundChangeButton, 25,
220                               SpringLayout.NORTH, contentPane);
221 
222         layout.putConstraint(SpringLayout.WEST, backgroundLabel, 10, 
223                               SpringLayout.WEST, contentPane);
224         layout.putConstraint(SpringLayout.NORTH, backgroundLabel, 20,
225                               SpringLayout.SOUTH, foregroundLabel);
226         layout.putConstraint(SpringLayout.WEST, backgroundField, 10,
227                               SpringLayout.EAST, foregroundLabel);
228         layout.putConstraint(SpringLayout.NORTH, backgroundField, 20,
229                               SpringLayout.SOUTH, foregroundLabel);
230         layout.putConstraint(SpringLayout.WEST, backgroundChangeButton, 10,
231                               SpringLayout.EAST, foregroundField);
232         layout.putConstraint(SpringLayout.NORTH, backgroundChangeButton, 20,
233                               SpringLayout.SOUTH, foregroundLabel);
234 
235         layout.putConstraint(SpringLayout.WEST, applyButton, 10,
236                              SpringLayout.EAST, backgroundLabel);
237         layout.putConstraint(SpringLayout.SOUTH, applyButton, -10,
238                              SpringLayout.SOUTH, contentPane);
239         layout.putConstraint(SpringLayout.WEST, cancelButton, 25,
240                              SpringLayout.EAST, applyButton);
241         layout.putConstraint(SpringLayout.SOUTH, cancelButton, -10,
242                              SpringLayout.SOUTH, contentPane);
243 
244         // SpringLayoutの場合、pack()では大きさが定まらない
245         setSize(320, 200);
246 
247         setResizable(false);
248         setLocationRelativeTo(anOwner);
249         logger.exiting(MYCLASS, "KandayPreferencesDialog");
250     }
251 
252     private Color foregroundColor;
253     private Color backgroundColor;
254 
255     private static final String MYCLASS =
256         KandayPreferencesDialog.class.getName();
257     private static Logger logger = Logger.getLogger(MYCLASS);
258 
259 }