001: /*
002: * (C) Copyright IBM Corp. 1998-2005. 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.swingui;
014:
015: import java.awt.Container;
016: import java.awt.FlowLayout;
017: import java.awt.Frame;
018:
019: import java.text.NumberFormat;
020: import java.text.ParseException;
021:
022: import javax.swing.JDialog;
023: import javax.swing.JPanel;
024: import javax.swing.JTextField;
025: import javax.swing.JButton;
026: import javax.swing.JLabel;
027:
028: import java.awt.event.ActionEvent;
029: import java.awt.event.ActionListener;
030: import java.awt.event.WindowEvent;
031: import java.awt.event.WindowAdapter;
032:
033: import com.ibm.richtext.uiimpl.resources.FrameResources;
034: import com.ibm.richtext.uiimpl.MenuItemSet;
035: import com.ibm.richtext.uiimpl.ResourceUtils;
036:
037: import com.ibm.richtext.styledtext.StyleModifier;
038: import com.ibm.richtext.textpanel.MTextPanel;
039:
040: /**
041: * Simple dialog which gets a number, and sends an appropriate command
042: */
043: final class JNumberDialog extends JDialog implements ActionListener {
044: static final String COPYRIGHT = "(C) Copyright IBM Corp. 1998-1999 - All Rights Reserved";
045: private MTextPanel fTextPanel;
046: private JTextField fInput = null;
047:
048: private JButton fOKButton = null;
049: private JButton fCancelButton = null;
050: private boolean fCharacter;
051: private Object fKey;
052: private float fMultiplier;
053:
054: /**
055: * @param multiplier the factor by which to multiply the user's
056: * selection before creating the attribute value. This
057: * is useful for subscripting.
058: */
059: JNumberDialog(Frame parent, String title, String message,
060: MTextPanel textPanel, Object key, boolean character,
061: float multiplier) {
062:
063: super (parent, title, false);
064: fTextPanel = textPanel;
065: fKey = key;
066: fCharacter = character;
067: fMultiplier = multiplier;
068:
069: Container content = getContentPane();
070:
071: content.setLayout(new java.awt.GridLayout(2, 1));
072:
073: JPanel panel = new JPanel();
074: panel.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 15));
075: fInput = new JTextField(5);
076:
077: panel.add(new JLabel(message));
078: panel.add(fInput);
079: content.add("Center", panel);
080:
081: fCancelButton = new JButton(ResourceUtils
082: .getResourceString(FrameResources.CANCEL));
083: fOKButton = new JButton(ResourceUtils
084: .getResourceString(FrameResources.OK));
085: JPanel p = new JPanel();
086: p.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
087: p.add(fCancelButton);
088: p.add(fOKButton);
089: content.add("South", p);
090:
091: pack();
092:
093: addWindowListener(new WindowAdapter() {
094: public void windowClosing(WindowEvent e) {
095: closeWindow(false);
096: }
097: });
098:
099: fOKButton.addActionListener(this );
100: fCancelButton.addActionListener(this );
101: }
102:
103: private void closeWindow(boolean sendAction) {
104:
105: setVisible(false);
106:
107: int num = 0;
108: if (sendAction) {
109: try {
110: String text = fInput.getText();
111: num = NumberFormat.getInstance().parse(text).intValue();
112: } catch (ParseException exception) {
113: sendAction = false;
114: }
115: }
116:
117: if (sendAction) {
118: sendAction(num);
119: }
120:
121: dispose();
122: }
123:
124: public void actionPerformed(ActionEvent e) {
125:
126: Object source = e.getSource();
127:
128: if (source == fOKButton) {
129: closeWindow(true);
130: } else if (source == fCancelButton) {
131: closeWindow(false);
132: } else {
133: throw new IllegalArgumentException("Invalid ActionEvent!");
134: }
135: }
136:
137: /**
138: * Handle the user input
139: * @param number The number the user typed in
140: */
141: private void sendAction(int number) {
142: float num = number * fMultiplier;
143: StyleModifier modifier = StyleModifier.createAddModifier(fKey,
144: new Float(num));
145: if (fCharacter == MenuItemSet.CHARACTER) {
146: fTextPanel.modifyCharacterStyleOnSelection(modifier);
147: } else {
148: fTextPanel.modifyParagraphStyleOnSelection(modifier);
149: }
150: }
151: }
|