]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_2_1-src/src/com/ibm/richtext/awtui/ObjectDialog.java
icu4jsrc
[Dictionary.git] / jars / icu4j-4_2_1-src / src / com / ibm / richtext / awtui / ObjectDialog.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.util.Hashtable;\r
16 \r
17 import com.ibm.richtext.textlayout.attributes.AttributeSet;\r
18 import com.ibm.richtext.styledtext.StyleModifier;\r
19 import com.ibm.richtext.textpanel.MTextPanel;\r
20 import com.ibm.richtext.uiimpl.MenuItemSet;\r
21 \r
22 import com.ibm.richtext.uiimpl.resources.FrameResources;\r
23 import com.ibm.richtext.uiimpl.ResourceUtils;\r
24 \r
25 import java.awt.Dialog;\r
26 import java.awt.Frame;\r
27 import java.awt.Button;\r
28 import java.awt.Choice;\r
29 import java.awt.Label;\r
30 import java.awt.FlowLayout;\r
31 import java.awt.Panel;\r
32 import java.awt.GridLayout;\r
33 \r
34 import java.awt.event.ActionEvent;\r
35 import java.awt.event.ActionListener;\r
36 import java.awt.event.WindowEvent;\r
37 import java.awt.event.WindowAdapter;\r
38 \r
39 /**\r
40 * Simple dialog which gets a color\r
41 */\r
42 final class ObjectDialog extends Dialog implements ActionListener\r
43 {\r
44     /**\r
45      * For serialization\r
46      */\r
47     private static final long serialVersionUID = 8667616278985420231L;\r
48     //static final String COPYRIGHT =\r
49     //            "(C) Copyright IBM Corp. 1998-1999 - All Rights Reserved";\r
50 \r
51     private final MTextPanel fTextPanel;\r
52     private final Object fKey;\r
53     private boolean fCharacter;\r
54 \r
55     private final Button fOKButton;\r
56     private final Button fCancelButton;\r
57     private final Choice fItems;\r
58     private final Hashtable fNameToValueMap;\r
59     /**\r
60     * Construct a new ColorDialog.\r
61     * @param parent the dialog's parent frame\r
62     * @param title the dialogs title\r
63     * @param message the message displayed next to the input box\r
64     */\r
65     ObjectDialog(Frame parent, \r
66                  String title, \r
67                  String message, \r
68                  MTextPanel textPanel,\r
69                  Object key,\r
70                  boolean character,\r
71                  String[] names,\r
72                  Object[] values) {\r
73                     \r
74         super(parent, title, false);\r
75         fTextPanel = textPanel;\r
76         fKey = key;\r
77         fCharacter = character;\r
78         \r
79         setLayout(new GridLayout(2, 1));\r
80 \r
81         Panel panel = new Panel();\r
82         panel.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10));\r
83         fItems = new Choice();\r
84 \r
85         if (names.length != values.length) {\r
86             throw new IllegalArgumentException("Must have same number of names and values.");\r
87         }\r
88 \r
89         fNameToValueMap = new Hashtable(names.length);\r
90         \r
91         for (int i=0; i < names.length; i++) {\r
92             fItems.add(names[i]);\r
93             if (values[i] != null) {\r
94                 fNameToValueMap.put(names[i], values[i]);\r
95             }\r
96         }\r
97 \r
98         panel.add(new Label(message));        \r
99         panel.add(fItems);\r
100         \r
101         add("North", panel);\r
102 \r
103         fCancelButton = new Button(ResourceUtils.getResourceString(FrameResources.CANCEL));\r
104         fOKButton = new Button(ResourceUtils.getResourceString(FrameResources.OK));\r
105         Panel p = new Panel();\r
106         p.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));\r
107         p.add(fCancelButton);\r
108         p.add(fOKButton);\r
109         add("South", p);\r
110 \r
111         pack();\r
112 \r
113         addWindowListener(new WindowAdapter() {\r
114             public void windowClosing(WindowEvent e) {\r
115                 closeWindow(false);\r
116             }\r
117         });\r
118         \r
119         fOKButton.addActionListener(this);\r
120         fCancelButton.addActionListener(this);\r
121     }\r
122 \r
123     private void closeWindow(boolean sendAction) {\r
124 \r
125         setVisible(false);\r
126         if (sendAction) {\r
127             Object value = fNameToValueMap.get(fItems.getSelectedItem());\r
128             sendAction(value);\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 obj the value object\r
151     */\r
152     private void sendAction(Object value) {\r
153 \r
154         StyleModifier modifier;\r
155         if (value != null) {\r
156             modifier = StyleModifier.createAddModifier(fKey, value);\r
157         }\r
158         else {\r
159             AttributeSet set = new AttributeSet(fKey);\r
160             modifier = StyleModifier.createRemoveModifier(set);\r
161         }\r
162         \r
163         if (fCharacter == MenuItemSet.CHARACTER) {\r
164             fTextPanel.modifyCharacterStyleOnSelection(modifier);\r
165         }\r
166         else {\r
167             fTextPanel.modifyParagraphStyleOnSelection(modifier);\r
168         }\r
169     }\r
170 }\r