001: /*
002: * (C) Copyright IBM Corp. 1998-2004. All Rights Reserved.
003: *
004: * The program is provided "as is" without any warranty express or
005: * implied, including the warranty of non-infringement and the implied
006: * warranties of merchantibility and fitness for a particular purpose.
007: * IBM will not be liable for any damages suffered by you as a result
008: * of using the Program. In no event will IBM be liable for any
009: * special, indirect or consequential damages or lost profits even if
010: * IBM has been advised of the possibility of their occurrence. IBM
011: * will not be liable for any third party claims against you.
012: */
013: package com.ibm.richtext.awtui;
014:
015: import java.awt.FlowLayout;
016: import java.awt.Dialog;
017: import java.awt.TextField;
018: import java.awt.Button;
019: import java.awt.Frame;
020: import java.awt.Panel;
021: import java.awt.Label;
022:
023: import java.text.NumberFormat;
024: import java.text.ParseException;
025:
026: import java.awt.event.ActionEvent;
027: import java.awt.event.ActionListener;
028: import java.awt.event.WindowEvent;
029: import java.awt.event.WindowAdapter;
030:
031: import com.ibm.richtext.uiimpl.resources.FrameResources;
032: import com.ibm.richtext.uiimpl.MenuItemSet;
033: import com.ibm.richtext.uiimpl.ResourceUtils;
034:
035: import com.ibm.richtext.styledtext.StyleModifier;
036: import com.ibm.richtext.textpanel.MTextPanel;
037:
038: /**
039: * Simple dialog which gets a number, and sends an appropriate command
040: */
041: final class NumberDialog extends Dialog implements ActionListener {
042: static final String COPYRIGHT = "(C) Copyright IBM Corp. 1998-1999 - All Rights Reserved";
043: private MTextPanel fTextPanel;
044: private TextField fInput = null;
045: private Button fOKButton = null;
046: private Button fCancelButton = null;
047: private boolean fCharacter;
048: private Object fKey;
049: private float fMultiplier;
050:
051: /**
052: * @param multiplier the factor by which to multiply the user's
053: * selection before creating the attribute value. This
054: * is useful for subscripting.
055: */
056: NumberDialog(Frame parent, String title, String message,
057: MTextPanel textPanel, Object key, boolean character,
058: float multiplier) {
059:
060: super (parent, title, false);
061: fTextPanel = textPanel;
062: fKey = key;
063: fCharacter = character;
064: fMultiplier = multiplier;
065: setLayout(new java.awt.GridLayout(2, 1));
066:
067: Panel panel = new Panel();
068: panel.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 15));
069: fInput = new TextField(5);
070: panel.add(new Label(message));
071: panel.add(fInput);
072: add("Center", panel);
073:
074: fCancelButton = new Button(ResourceUtils
075: .getResourceString(FrameResources.CANCEL));
076: fOKButton = new Button(ResourceUtils
077: .getResourceString(FrameResources.OK));
078: Panel p = new Panel();
079: p.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
080: p.add(fCancelButton);
081: p.add(fOKButton);
082: add("South", p);
083:
084: pack();
085:
086: addWindowListener(new WindowAdapter() {
087: public void windowClosing(WindowEvent e) {
088: closeWindow(false);
089: }
090: });
091:
092: fOKButton.addActionListener(this );
093: fCancelButton.addActionListener(this );
094: }
095:
096: private void closeWindow(boolean sendAction) {
097:
098: setVisible(false);
099:
100: int num = 0;
101: if (sendAction) {
102: try {
103: String text = fInput.getText();
104: num = NumberFormat.getInstance().parse(text).intValue();
105: } catch (ParseException exception) {
106: sendAction = false;
107: }
108: }
109:
110: if (sendAction) {
111: sendAction(num);
112: }
113:
114: dispose();
115: }
116:
117: public void actionPerformed(ActionEvent e) {
118:
119: Object source = e.getSource();
120:
121: if (source == fOKButton) {
122: closeWindow(true);
123: } else if (source == fCancelButton) {
124: closeWindow(false);
125: } else {
126: throw new IllegalArgumentException("Invalid ActionEvent!");
127: }
128: }
129:
130: /**
131: * Handle the user input
132: * @param the number the user typed in
133: */
134: private void sendAction(int number) {
135: float num = number * fMultiplier;
136: StyleModifier modifier = StyleModifier.createAddModifier(fKey,
137: new Float(num));
138: if (fCharacter == MenuItemSet.CHARACTER) {
139: fTextPanel.modifyCharacterStyleOnSelection(modifier);
140: } else {
141: fTextPanel.modifyParagraphStyleOnSelection(modifier);
142: }
143: }
144: }
|