]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_2_1-src/src/com/ibm/richtext/swingui/JNumberDialog.java
icu4jsrc
[Dictionary.git] / jars / icu4j-4_2_1-src / src / com / ibm / richtext / swingui / JNumberDialog.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.swingui;\r
14 \r
15 import java.awt.Container;\r
16 import java.awt.FlowLayout;\r
17 import java.awt.Frame;\r
18 \r
19 import java.text.NumberFormat;\r
20 import java.text.ParseException;\r
21 \r
22 import javax.swing.JDialog;\r
23 import javax.swing.JPanel;\r
24 import javax.swing.JTextField;\r
25 import javax.swing.JButton;\r
26 import javax.swing.JLabel;\r
27 \r
28 import java.awt.event.ActionEvent;\r
29 import java.awt.event.ActionListener;\r
30 import java.awt.event.WindowEvent;\r
31 import java.awt.event.WindowAdapter;\r
32 \r
33 import com.ibm.richtext.uiimpl.resources.FrameResources;\r
34 import com.ibm.richtext.uiimpl.MenuItemSet;\r
35 import com.ibm.richtext.uiimpl.ResourceUtils;\r
36 \r
37 import com.ibm.richtext.styledtext.StyleModifier;\r
38 import com.ibm.richtext.textpanel.MTextPanel;\r
39 \r
40 /**\r
41 * Simple dialog which gets a number, and sends an appropriate command\r
42 */\r
43 final class JNumberDialog extends JDialog implements ActionListener\r
44 {\r
45     /**\r
46      * For serialization\r
47      */\r
48     private static final long serialVersionUID = 8948601172508147011L;\r
49     //static final String COPYRIGHT =\r
50     //            "(C) Copyright IBM Corp. 1998-1999 - All Rights Reserved";\r
51     private MTextPanel fTextPanel;\r
52     private JTextField fInput = null;\r
53 \r
54     private JButton fOKButton = null;\r
55     private JButton fCancelButton = null;\r
56     private boolean fCharacter;\r
57     private Object fKey;\r
58     private float fMultiplier; \r
59 \r
60     /**\r
61      * @param multiplier the factor by which to multiply the user's\r
62      *        selection before creating the attribute value.   This\r
63      *        is useful for subscripting.\r
64      */\r
65     JNumberDialog(Frame parent,\r
66                   String title,\r
67                   String message,\r
68                   MTextPanel textPanel,\r
69                   Object key,\r
70                   boolean character,\r
71                   float multiplier) {\r
72 \r
73         super(parent, title, false);\r
74         fTextPanel = textPanel;\r
75         fKey = key;\r
76         fCharacter = character;\r
77         fMultiplier = multiplier;\r
78         \r
79         Container content = getContentPane();\r
80         \r
81         content.setLayout(new java.awt.GridLayout(2,1));\r
82 \r
83         JPanel panel = new JPanel();\r
84         panel.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 15));\r
85         fInput = new JTextField(5);\r
86 \r
87         panel.add(new JLabel(message));\r
88         panel.add(fInput);\r
89         content.add("Center", panel);\r
90 \r
91         fCancelButton = new JButton(ResourceUtils.getResourceString(FrameResources.CANCEL));\r
92         fOKButton = new JButton(ResourceUtils.getResourceString(FrameResources.OK));\r
93         JPanel p = new JPanel();\r
94         p.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));\r
95         p.add(fCancelButton);\r
96         p.add(fOKButton);\r
97         content.add("South", p);\r
98 \r
99         pack();\r
100 \r
101         addWindowListener(new WindowAdapter() {\r
102             public void windowClosing(WindowEvent e) {\r
103                 closeWindow(false);\r
104             }\r
105         });\r
106         \r
107         fOKButton.addActionListener(this);\r
108         fCancelButton.addActionListener(this);\r
109     }\r
110 \r
111     private void closeWindow(boolean sendAction) {\r
112 \r
113         setVisible(false);\r
114 \r
115         int num = 0;\r
116         if (sendAction) {\r
117             try {\r
118                 String text = fInput.getText();\r
119                 num = NumberFormat.getInstance().parse(text).intValue();\r
120             }\r
121             catch (ParseException exception) {\r
122                 sendAction = false;\r
123             }\r
124         }\r
125 \r
126         if (sendAction) {\r
127             sendAction(num);\r
128         }\r
129 \r
130         dispose();\r
131     }\r
132 \r
133     public void actionPerformed(ActionEvent e) {\r
134 \r
135         Object source = e.getSource();\r
136 \r
137         if (source == fOKButton) {\r
138             closeWindow(true);\r
139         }\r
140         else if (source == fCancelButton) {\r
141             closeWindow(false);\r
142         }\r
143         else {\r
144             throw new IllegalArgumentException("Invalid ActionEvent!");\r
145         }\r
146     }\r
147 \r
148     /**\r
149     * Handle the user input\r
150     * @param number The number the user typed in\r
151     */\r
152     private void sendAction(int number) {\r
153         float num = number * fMultiplier;\r
154         StyleModifier modifier = StyleModifier.createAddModifier(\r
155                                                 fKey,\r
156                                                 new Float(num));\r
157         if (fCharacter == MenuItemSet.CHARACTER) {\r
158             fTextPanel.modifyCharacterStyleOnSelection(modifier);\r
159         }\r
160         else {\r
161             fTextPanel.modifyParagraphStyleOnSelection(modifier);\r
162         }\r
163     }\r
164 }\r