001 /* 002 * IntegerDialog 003 * 004 * Copyright (c) 2001, 2002, 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.Scrollbar; 017 import java.awt.TextComponent; 018 import java.awt.TextField; 019 import java.awt.event.ActionEvent; 020 import java.awt.event.ActionListener; 021 import java.awt.event.AdjustmentEvent; 022 import java.awt.event.AdjustmentListener; 023 import java.awt.event.KeyEvent; 024 import java.awt.event.KeyListener; 025 026 /** 027 * An AWT dialog to select an <code>int</code> value from a given interval. 028 * 029 * @author Marco Schmidt 030 */ 031 public class IntegerDialog extends Dialog implements ActionListener, AdjustmentListener, KeyListener 032 { 033 private Button cancel; 034 private int maxValue; 035 private int minValue; 036 private Button ok; 037 private boolean pressedOk; 038 private Integer result; 039 private Scrollbar scrollbar; 040 private TextComponent valueTextField; 041 042 /** 043 * Creates an IntegerDialog, a modal dialog that lets the user select one int 044 * value from a given interval. 045 * 046 * @param owner the {@link java.awt.Frame} this dialog will belong to 047 * @param title the text that will be displayed in the title bar of the dialog 048 * @param message the message text that will be displayed in the upper part of the dialog 049 * @param minValue the minimum allowed int value to be selected by the user 050 * @param initialValue the int value that is selected when the dialog first pops up 051 * @param maxValue the maximum allowed int value to be selected by the user 052 * @param okText the value for OK (just <code>"OK"</code> in English 053 * programs, may be different for other natural languages 054 * @param cancelText the value for Cancel (just <code>"Cancel"</code> in English 055 * programs, may be different for other natural languages 056 */ 057 public IntegerDialog(Frame owner, String title, String message, 058 int minValue, int initialValue, int maxValue, 059 String okText, String cancelText) 060 { 061 super(owner, title, true); 062 pressedOk = false; 063 this.minValue = minValue; 064 this.maxValue = maxValue; 065 add(new Label(message), BorderLayout.NORTH); 066 067 Panel panel = new Panel(new BorderLayout()); 068 scrollbar = new Scrollbar(Scrollbar.HORIZONTAL, initialValue, 1, minValue, maxValue + 1); 069 scrollbar.addAdjustmentListener(this); 070 panel.add(scrollbar, BorderLayout.CENTER); 071 072 valueTextField = new TextField(Integer.toString(initialValue), 6); 073 valueTextField.addKeyListener(this); 074 panel.add(valueTextField, BorderLayout.EAST); 075 add(panel, BorderLayout.CENTER); 076 077 panel = new Panel(); 078 ok = new Button(okText); 079 ok.addActionListener(this); 080 cancel = new Button(cancelText); 081 cancel.addActionListener(this); 082 panel.add(ok); 083 panel.add(cancel); 084 add(panel, BorderLayout.SOUTH); 085 086 pack(); 087 Dialogs.center(this); 088 } 089 090 /** 091 * Hides (closes) this dialog if the OK button was source of the action event 092 * (e.g. if the button was pressed). 093 */ 094 public void actionPerformed(ActionEvent e) 095 { 096 if (e.getSource() == ok) 097 { 098 pressedOk = true; 099 result = new Integer(scrollbar.getValue()); 100 setVisible(false); 101 } 102 else 103 if (e.getSource() == cancel) 104 { 105 setVisible(false); 106 } 107 } 108 109 public void adjustmentValueChanged(AdjustmentEvent e) 110 { 111 valueTextField.setText(Integer.toString(scrollbar.getValue())); 112 } 113 114 public Integer getValue() 115 { 116 return result; 117 } 118 119 public void handleKeys(KeyEvent e) 120 { 121 setScrollbarFromTextField(); 122 } 123 124 public void keyPressed(KeyEvent e) 125 { 126 handleKeys(e); 127 } 128 129 public void keyReleased(KeyEvent e) 130 { 131 handleKeys(e); 132 } 133 134 public void keyTyped(KeyEvent e) 135 { 136 handleKeys(e); 137 } 138 139 private void setScrollbarFromTextField() 140 { 141 String text = valueTextField.getText().trim(); 142 //System.out.println("Got text " + text); 143 int number; 144 try 145 { 146 number = Integer.parseInt(text); 147 } 148 catch (NumberFormatException nfe) 149 { 150 return; 151 } 152 153 if (number < minValue || number > maxValue) 154 { 155 return; 156 } 157 //System.out.println("Set scrollbar value to " + number); 158 scrollbar.setValue(number); 159 } 160 }