ModalDialog.java
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008, AIST, the University of Tokyo and General Robotix Inc.
3  * All rights reserved. This program is made available under the terms of the
4  * Eclipse Public License v1.0 which accompanies this distribution, and is
5  * available at http://www.eclipse.org/legal/epl-v10.html
6  * Contributors:
7  * General Robotix Inc.
8  * National Institute of Advanced Industrial Science and Technology (AIST)
9  */
17 package com.generalrobotix.ui.util;
18 
19 import java.awt.AWTEventMulticaster;
20 import java.awt.BorderLayout;
21 import java.awt.Component;
22 import java.awt.Dialog;
23 import java.awt.Frame;
24 import java.awt.GridBagConstraints;
25 import java.awt.GridBagLayout;
26 import java.awt.Window;
27 import java.awt.event.ActionEvent;
28 import java.awt.event.ActionListener;
29 import java.awt.event.WindowEvent;
30 
31 import javax.swing.Box;
32 import javax.swing.BoxLayout;
33 import javax.swing.JButton;
34 import javax.swing.JDialog;
35 import javax.swing.JLabel;
36 import javax.swing.JPanel;
37 import javax.swing.SwingUtilities;
38 import javax.swing.border.EmptyBorder;
39 
40 
44 @SuppressWarnings("serial") //$NON-NLS-1$
45 public class ModalDialog extends JDialog {
46  //--------------------------------------------------------------------
47  // 定数
48 
49  // ボタンタイプ
50  public static final int NONE_TYPE = 0;
51  public static final int OK_TYPE = 1;
52  public static final int OK_CANCEL_TYPE = 2;
53  public static final int YES_NO_TYPE = 3;
54  public static final int YES_NO_CANCEL_TYPE = 4;
55 
56  // キャプションの配置
57  public static final int INLINE_CAPTION = 1;
58  public static final int MULTILINE_CAPTION = 2;
59 
60  // ボタンの識別子
61  public static final int CUSTOM_BUTTON = 0;
62  public static final int OK_BUTTON = 1;
63  public static final int CANCEL_BUTTON = 2;
64  public static final int YES_BUTTON = 3;
65  public static final int NO_BUTTON = 4;
66  public static final int CLOSE_BUTTON = 5;
67 
68  // コンポーネント配置のための定数
69  private static final int BUTTON_GAP = 5;
70  private static final int CAPTION_GAP = 11;
71 
72  //--------------------------------------------------------------------
73  // インスタンス変数
74  private int width_;
75 
76  protected int buttonType_;
77  protected String buttonCommand_;
78  protected boolean closeButtonEnabled_ = true;
79 
80  protected ActionListener listener_;
81  protected Window owner_;
82  protected JPanel inputArea_;
83  protected JPanel buttonArea_;
84  protected JPanel mainPanel_;
85  protected GridBagConstraints gbc_;
86 
87  //--------------------------------------------------------------------
88  // コンストラクタ
89  public ModalDialog() {}
90 
91  public ModalDialog(
92  Frame owner,
93  String title,
94  String message,
95  int buttonType
96  ) {
97  super(owner, title, true);
98  owner_ = owner;
99  _init(title, message, buttonType);
100  }
101 
102  public ModalDialog(
103  Dialog owner,
104  String title,
105  String message,
106  int buttonType
107  ) {
108  super(owner, title, true);
109  owner_ = owner;
110  _init(title, message, buttonType);
111  }
112 
113  private void _init(String title, String message, int buttonType) {
114  JLabel messageLabel = new JLabel(message);
115  //messageLabel.setForeground(Color.black);
116 
117  inputArea_ = new JPanel();
118  inputArea_.setLayout(new GridBagLayout());
119  inputArea_.setBorder(new EmptyBorder(6, 12, 0, 0));
120  gbc_ = new GridBagConstraints();
121 
122  listener_ = new ActionListener() {
123  public void actionPerformed(ActionEvent evt) {
124  InnerButton button = (InnerButton)evt.getSource();
125  buttonType_ = button.getButtonType();
126  buttonCommand_ = evt.getActionCommand();
127  SwingUtilities.invokeLater(
128  new Runnable() {
129  public void run() {
130  ModalDialog.this.dispose();
131  }
132  }
133  );
134  }
135  };
136 
137  buttonArea_ = new JPanel();
138  buttonArea_.setBorder(new EmptyBorder(0, 0, 11, 11));
139  buttonArea_.setLayout(new BoxLayout(buttonArea_, BoxLayout.X_AXIS));
140  buttonArea_.setAlignmentX(Component.LEFT_ALIGNMENT);
141  buttonArea_.add(Box.createHorizontalGlue());
142 
143  InnerButton okButton;
144  switch (buttonType) {
145  case NONE_TYPE:
146  break;
147  case OK_TYPE:
148  okButton = new InnerButton(OK_BUTTON);
149  getRootPane().setDefaultButton(okButton);
150  buttonArea_.add(okButton);
151  break;
152  case OK_CANCEL_TYPE:
153  okButton = new InnerButton(OK_BUTTON);
154  getRootPane().setDefaultButton(okButton);
155  buttonArea_.add(okButton);
156  buttonArea_.add(Box.createHorizontalStrut(BUTTON_GAP));
157  buttonArea_.add(new InnerButton(CANCEL_BUTTON));
158  break;
159  case YES_NO_TYPE:
160  buttonArea_.add(new InnerButton(YES_BUTTON));
161  buttonArea_.add(Box.createHorizontalStrut(BUTTON_GAP));
162  buttonArea_.add(new InnerButton(NO_BUTTON));
163  break;
164  case YES_NO_CANCEL_TYPE:
165  buttonArea_.add(new InnerButton(YES_BUTTON));
166  buttonArea_.add(Box.createHorizontalStrut(BUTTON_GAP));
167  buttonArea_.add(new InnerButton(NO_BUTTON));
168  buttonArea_.add(Box.createHorizontalStrut(BUTTON_GAP));
169  buttonArea_.add(new InnerButton(CANCEL_BUTTON));
170  break;
171  }
172 
173  mainPanel_ = new JPanel();
174  mainPanel_.setLayout(new BorderLayout());
175  mainPanel_.setBorder(new EmptyBorder(12, 12, 17, 11));
176  mainPanel_.add(messageLabel, BorderLayout.NORTH);
177  mainPanel_.add(inputArea_, BorderLayout.CENTER);
178 
179  getContentPane().add(mainPanel_, BorderLayout.CENTER);
180  getContentPane().add(buttonArea_, BorderLayout.SOUTH);
181  setResizable(false);
182  }
183 
184  //--------------------------------------------------------------------
185  // 公開メソッド
186  public void setInputAreaWidth(int width) {
187  width_ = width;
188  gbc_.gridwidth = GridBagConstraints.REMAINDER;
189  gbc_.fill = GridBagConstraints.HORIZONTAL;
190  inputArea_.add(Box.createHorizontalStrut(width_), gbc_);
191 
192  }
193 
194  public int getInputAreaWidth() {
195  return width_;
196  }
197 
199  public void addInputComponent(
200  String caption,
201  Component component,
202  int location,
203  boolean fill
204  ) {
205  gbc_.gridwidth = GridBagConstraints.REMAINDER;
206  gbc_.fill = GridBagConstraints.HORIZONTAL;
207  inputArea_.add(Box.createVerticalStrut(11), gbc_);
208 
209  gbc_.anchor = GridBagConstraints.NORTHWEST;
210  gbc_.fill = GridBagConstraints.NONE;
211 
212  JLabel label;
213 
214  if (caption == null || caption.equals("")) { //$NON-NLS-1$
215  label = new JLabel(""); //$NON-NLS-1$
216  } else {
217  label = new JLabel(caption + " :"); //$NON-NLS-1$
218  }
219 
220  switch (location) {
221  case INLINE_CAPTION:
222  gbc_.gridwidth = 2;
223  inputArea_.add(label, gbc_);
224  gbc_.gridwidth = 1;
225  inputArea_.add(Box.createHorizontalStrut(CAPTION_GAP));
226  break;
227  case MULTILINE_CAPTION:
228  gbc_.gridwidth = GridBagConstraints.REMAINDER;
229  inputArea_.add(label, gbc_);
230 
231  gbc_.gridwidth = GridBagConstraints.REMAINDER;
232  gbc_.fill = GridBagConstraints.HORIZONTAL;
233  inputArea_.add(Box.createVerticalStrut(6), gbc_);
234 
235  gbc_.gridwidth = 1;
236  inputArea_.add(Box.createHorizontalStrut(12));
237  break;
238  default:
239  return;
240  }
241 
242  gbc_.anchor = GridBagConstraints.NORTHWEST;
243  gbc_.gridwidth = GridBagConstraints.REMAINDER;
244  if (fill) {
245  gbc_.fill = GridBagConstraints.HORIZONTAL;
246  } else {
247  gbc_.fill = GridBagConstraints.NONE;
248  }
249 
250  inputArea_.add(component, gbc_);
251  }
252 
253  public void addButton(String caption) {
254  buttonArea_.add(Box.createHorizontalStrut(BUTTON_GAP));
255  InnerButton button = new InnerButton(CUSTOM_BUTTON);
256  button.setCaption(caption);
257  buttonArea_.add(button);
258  }
259 
260  //戻り値として押されたボタンの識別子を返す。
261  public int showModalDialog() {
262  setModal(true);
263  pack();
264  setLocationRelativeTo(owner_);
265  setVisible(true);
266  return buttonType_;
267  }
268 
269  public void showNoWait() {
270  setModal(false);
271  owner_.setEnabled(false);
272  pack();
273  setLocationRelativeTo(owner_);
274  setVisible(true);
275  }
276 
277  public void dispose() {
278  super.dispose();
279  owner_.setEnabled(true);
280  }
281 
282  public void addActionListener(ActionListener listener) {
283  listener_ = AWTEventMulticaster.add(listener_, listener);
284  }
285 
286  public void removeActionListener(ActionListener listener) {
287  listener_ = AWTEventMulticaster.remove(listener_, listener);
288  }
289 
290  public void setCloseButtonEnabled(boolean enabled) {
291  closeButtonEnabled_ = enabled;
292  }
293 
294  public void processWindowEvent(WindowEvent evt) {
295  if (closeButtonEnabled_) {
296  super.processWindowEvent(evt);
297  }
298  }
299 
300  //--------------------------------------------------------------------
301  // インナークラス
302  protected class InnerButton extends JButton {
303  private int type_;
304 
305  public InnerButton(int type) {
306  type_ = type;
307  switch (type_) {
308  case CUSTOM_BUTTON:
309  break;
310  case OK_BUTTON:
311  setCaption(MessageBundle.get("dialog.okButton")); //$NON-NLS-1$
312  break;
313  case CANCEL_BUTTON:
314  setCaption(MessageBundle.get("dialog.cancelButton")); //$NON-NLS-1$
315  break;
316  case YES_BUTTON:
317  setCaption(MessageBundle.get("dialog.yesButton")); //$NON-NLS-1$
318  break;
319  case NO_BUTTON:
320  setCaption(MessageBundle.get("dialog.noButton")); //$NON-NLS-1$
321  break;
322  }
323 
324  this.addActionListener(ModalDialog.this.listener_);
325  }
326 
327  void setCaption(String caption) {
328  setText(caption);
329  setActionCommand(caption);
330  }
331 
332  public int getButtonType() { return type_; }
333  }
334 }
static final String get(String key)
png_infop png_charp png_int_32 png_int_32 int * type
Definition: png.h:2332
void _init(String title, String message, int buttonType)
#define null
our own NULL pointer
Definition: IceTypes.h:57
void addInputComponent(String caption, Component component, int location, boolean fill)
png_infop png_uint_32 * width
Definition: png.h:2309
ModalDialog(Frame owner, String title, String message, int buttonType)
def run(tree, args)
png_infop int int location
Definition: png.h:2487
void setCloseButtonEnabled(boolean enabled)
void addActionListener(ActionListener listener)
ModalDialog(Dialog owner, String title, String message, int buttonType)
void processWindowEvent(WindowEvent evt)
void removeActionListener(ActionListener listener)


openhrp3
Author(s): AIST, General Robotix Inc., Nakamura Lab of Dept. of Mechano Informatics at University of Tokyo
autogenerated on Sat May 8 2021 02:42:39