001 /* 002 * YesNoDialog 003 * 004 * Copyright (c) 2003 Marco Schmidt. 005 * All rights reserved. 006 */ 007 008 package net.sourceforge.jiu.gui.awt.dialogs; 009 010 import java.awt.BorderLayout; 011 import java.awt.Button; 012 import java.awt.Dialog; 013 import java.awt.Frame; 014 import java.awt.Label; 015 import java.awt.Panel; 016 import java.awt.event.ActionEvent; 017 import java.awt.event.ActionListener; 018 import net.sourceforge.jiu.apps.Strings; 019 020 /** 021 * A dialog that asks a question and offers a Yes and a No button 022 * (and optionally a Cancel button). 023 * @author Marco Schmidt 024 * @since 0.11.0 025 */ 026 public class YesNoDialog extends Dialog implements ActionListener 027 { 028 /** 029 * Will be returned in {@link #getResult} if the YES button was chosen. 030 */ 031 public static final int RESULT_YES = 0; 032 033 /** 034 * Will be returned in {@link #getResult} if the NO button was chosen. 035 */ 036 public static final int RESULT_NO = 1; 037 038 /** 039 * Will be returned in {@link #getResult} if the CANCEL button was chosen. 040 */ 041 public static final int RESULT_CANCEL = 2; 042 private Button yes; 043 private Button no; 044 private Button cancel; 045 private int result; 046 047 /** 048 * Creates a new YesNoDialog object and shows it centered on the screen. 049 * @param owner the frame that owns this modal dialog 050 * @param strings the String resources 051 * @param titleIndex the index into the String resource of the title text 052 * @param questionIndex the index into the String resource of the question text 053 * @param includeCancel determines whether a third button 'Cancel' will be included 054 */ 055 public YesNoDialog(Frame owner, Strings strings, int titleIndex, int questionIndex, boolean includeCancel) 056 { 057 super(owner, strings.get(titleIndex), true); 058 059 add(new Label(strings.get(questionIndex)), BorderLayout.CENTER); 060 061 yes = new Button(strings.get(Strings.YES)); 062 yes.addActionListener(this); 063 no = new Button(strings.get(Strings.NO)); 064 no.addActionListener(this); 065 cancel = new Button(strings.get(Strings.CANCEL)); 066 cancel.addActionListener(this); 067 068 Panel panel = new Panel(); 069 panel.add(yes); 070 panel.add(no); 071 if (includeCancel) 072 { 073 panel.add(cancel); 074 } 075 add(panel, BorderLayout.SOUTH); 076 077 pack(); 078 Dialogs.center(this); 079 } 080 081 /** 082 * Hides (closes) this dialog if the OK button was source of the action event 083 * (e.g. if the button was pressed). 084 */ 085 public void actionPerformed(ActionEvent e) 086 { 087 if (e.getSource() == yes) 088 { 089 result = RESULT_YES; 090 setVisible(false); 091 } 092 else 093 if (e.getSource() == no) 094 { 095 result = RESULT_NO; 096 setVisible(false); 097 } 098 else 099 if (e.getSource() == cancel) 100 { 101 result = RESULT_CANCEL; 102 setVisible(false); 103 } 104 } 105 106 /** 107 * Returns one of the RESULT_xyz constants of this class. 108 * @return the RESULT constant of the button which the user has chosen 109 */ 110 public int getResult() 111 { 112 return result; 113 } 114 }