]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_2_1-src/src/com/ibm/richtext/awtui/NumberDialog.java
go
[Dictionary.git] / jars / icu4j-4_2_1-src / src / com / ibm / richtext / awtui / NumberDialog.java
1 /*\r
2  * (C) Copyright IBM Corp. 1998-2007.  All Rights Reserved.\r
3  *\r
4  * The program is provided "as is" without any warranty express or\r
5  * implied, including the warranty of non-infringement and the implied\r
6  * warranties of merchantibility and fitness for a particular purpose.\r
7  * IBM will not be liable for any damages suffered by you as a result\r
8  * of using the Program. In no event will IBM be liable for any\r
9  * special, indirect or consequential damages or lost profits even if\r
10  * IBM has been advised of the possibility of their occurrence. IBM\r
11  * will not be liable for any third party claims against you.\r
12  */\r
13 package com.ibm.richtext.awtui;\r
14 \r
15 import java.awt.FlowLayout;\r
16 import java.awt.Dialog;\r
17 import java.awt.TextField;\r
18 import java.awt.Button;\r
19 import java.awt.Frame;\r
20 import java.awt.Panel;\r
21 import java.awt.Label;\r
22 \r
23 import java.text.NumberFormat;\r
24 import java.text.ParseException;\r
25 \r
26 import java.awt.event.ActionEvent;\r
27 import java.awt.event.ActionListener;\r
28 import java.awt.event.WindowEvent;\r
29 import java.awt.event.WindowAdapter;\r
30 \r
31 import com.ibm.richtext.uiimpl.resources.FrameResources;\r
32 import com.ibm.richtext.uiimpl.MenuItemSet;\r
33 import com.ibm.richtext.uiimpl.ResourceUtils;\r
34 \r
35 import com.ibm.richtext.styledtext.StyleModifier;\r
36 import com.ibm.richtext.textpanel.MTextPanel;\r
37 \r
38 /**\r
39 * Simple dialog which gets a number, and sends an appropriate command\r
40 */\r
41 final class NumberDialog extends Dialog implements ActionListener\r
42 {\r
43     /**\r
44      * For serialization\r
45      */\r
46     private static final long serialVersionUID = -2181405364377952745L;\r
47     //static final String COPYRIGHT =\r
48     //            "(C) Copyright IBM Corp. 1998-1999 - All Rights Reserved";\r
49     private MTextPanel fTextPanel;\r
50     private TextField fInput = null;\r
51     private Button fOKButton = null;\r
52     private Button fCancelButton = null;\r
53     private boolean fCharacter;\r
54     private Object fKey;\r
55     private float fMultiplier; \r
56 \r
57     /**\r
58      * @param multiplier the factor by which to multiply the user's\r
59      *        selection before creating the attribute value.   This\r
60      *        is useful for subscripting.\r
61      */\r
62     NumberDialog(Frame parent,\r
63                  String title,\r
64                  String message,\r
65                  MTextPanel textPanel,\r
66                  Object key,\r
67                  boolean character,\r
68                  float multiplier) {\r
69 \r
70         super(parent, title, false);\r
71         fTextPanel = textPanel;\r
72         fKey = key;\r
73         fCharacter = character;\r
74         fMultiplier = multiplier;\r
75         setLayout(new java.awt.GridLayout(2,1));\r
76 \r
77         Panel panel = new Panel();\r
78         panel.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 15));\r
79         fInput = new TextField(5);\r
80         panel.add(new Label(message));\r
81         panel.add(fInput);\r
82         add("Center", panel);\r
83 \r
84         fCancelButton = new Button(ResourceUtils.getResourceString(FrameResources.CANCEL));\r
85         fOKButton = new Button(ResourceUtils.getResourceString(FrameResources.OK));\r
86         Panel p = new Panel();\r
87         p.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));\r
88         p.add(fCancelButton);\r
89         p.add(fOKButton);\r
90         add("South", p);\r
91 \r
92         pack();\r
93 \r
94         addWindowListener(new WindowAdapter() {\r
95             public void windowClosing(WindowEvent e) {\r
96                 closeWindow(false);\r
97             }\r
98         });\r
99         \r
100         fOKButton.addActionListener(this);\r
101         fCancelButton.addActionListener(this);\r
102     }\r
103 \r
104     private void closeWindow(boolean sendAction) {\r
105 \r
106         setVisible(false);\r
107 \r
108         int num = 0;\r
109         if (sendAction) {\r
110             try {\r
111                 String text = fInput.getText();\r
112                 num = NumberFormat.getInstance().parse(text).intValue();\r
113             }\r
114             catch (ParseException exception) {\r
115                 sendAction = false;\r
116             }\r
117         }\r
118 \r
119         if (sendAction) {\r
120             sendAction(num);\r
121         }\r
122 \r
123         dispose();\r
124     }\r
125 \r
126     public void actionPerformed(ActionEvent e) {\r
127 \r
128         Object source = e.getSource();\r
129 \r
130         if (source == fOKButton) {\r
131             closeWindow(true);\r
132         }\r
133         else if (source == fCancelButton) {\r
134             closeWindow(false);\r
135         }\r
136         else {\r
137             throw new IllegalArgumentException("Invalid ActionEvent!");\r
138         }\r
139     }\r
140 \r
141     /**\r
142     * Handle the user input\r
143     * @param the number the user typed in\r
144     */\r
145     private void sendAction(int number) {\r
146         float num = number * fMultiplier;\r
147         StyleModifier modifier = StyleModifier.createAddModifier(\r
148                                                 fKey,\r
149                                                 new Float(num));\r
150         if (fCharacter == MenuItemSet.CHARACTER) {\r
151             fTextPanel.modifyCharacterStyleOnSelection(modifier);\r
152         }\r
153         else {\r
154             fTextPanel.modifyParagraphStyleOnSelection(modifier);\r
155         }\r
156     }\r
157 }\r