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.util.Hashtable;
016:
017: import com.ibm.richtext.textlayout.attributes.AttributeSet;
018: import com.ibm.richtext.styledtext.StyleModifier;
019: import com.ibm.richtext.textpanel.MTextPanel;
020: import com.ibm.richtext.uiimpl.MenuItemSet;
021:
022: import com.ibm.richtext.uiimpl.resources.FrameResources;
023: import com.ibm.richtext.uiimpl.ResourceUtils;
024:
025: import java.awt.Dialog;
026: import java.awt.Frame;
027: import java.awt.Button;
028: import java.awt.Choice;
029: import java.awt.Label;
030: import java.awt.FlowLayout;
031: import java.awt.Panel;
032: import java.awt.GridLayout;
033:
034: import java.awt.event.ActionEvent;
035: import java.awt.event.ActionListener;
036: import java.awt.event.WindowEvent;
037: import java.awt.event.WindowAdapter;
038:
039: /**
040: * Simple dialog which gets a color
041: */
042: final class ObjectDialog extends Dialog implements ActionListener {
043: static final String COPYRIGHT = "(C) Copyright IBM Corp. 1998-1999 - All Rights Reserved";
044:
045: private final MTextPanel fTextPanel;
046: private final Object fKey;
047: private boolean fCharacter;
048:
049: private final Button fOKButton;
050: private final Button fCancelButton;
051: private final Choice fItems;
052: private final Hashtable fNameToValueMap;
053:
054: /**
055: * Construct a new ColorDialog.
056: * @param parent the dialog's parent frame
057: * @param title the dialogs title
058: * @param message the message displayed next to the input box
059: */
060: ObjectDialog(Frame parent, String title, String message,
061: MTextPanel textPanel, Object key, boolean character,
062: String[] names, Object[] values) {
063:
064: super (parent, title, false);
065: fTextPanel = textPanel;
066: fKey = key;
067: fCharacter = character;
068:
069: setLayout(new GridLayout(2, 1));
070:
071: Panel panel = new Panel();
072: panel.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10));
073: fItems = new Choice();
074:
075: if (names.length != values.length) {
076: throw new IllegalArgumentException(
077: "Must have same number of names and values.");
078: }
079:
080: fNameToValueMap = new Hashtable(names.length);
081:
082: for (int i = 0; i < names.length; i++) {
083: fItems.add(names[i]);
084: if (values[i] != null) {
085: fNameToValueMap.put(names[i], values[i]);
086: }
087: }
088:
089: panel.add(new Label(message));
090: panel.add(fItems);
091:
092: add("North", panel);
093:
094: fCancelButton = new Button(ResourceUtils
095: .getResourceString(FrameResources.CANCEL));
096: fOKButton = new Button(ResourceUtils
097: .getResourceString(FrameResources.OK));
098: Panel p = new Panel();
099: p.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
100: p.add(fCancelButton);
101: p.add(fOKButton);
102: add("South", p);
103:
104: pack();
105:
106: addWindowListener(new WindowAdapter() {
107: public void windowClosing(WindowEvent e) {
108: closeWindow(false);
109: }
110: });
111:
112: fOKButton.addActionListener(this );
113: fCancelButton.addActionListener(this );
114: }
115:
116: private void closeWindow(boolean sendAction) {
117:
118: setVisible(false);
119: if (sendAction) {
120: Object value = fNameToValueMap
121: .get(fItems.getSelectedItem());
122: sendAction(value);
123: }
124: dispose();
125: }
126:
127: public void actionPerformed(ActionEvent e) {
128:
129: Object source = e.getSource();
130:
131: if (source == fOKButton) {
132: closeWindow(true);
133: } else if (source == fCancelButton) {
134: closeWindow(false);
135: } else {
136: throw new IllegalArgumentException("Invalid ActionEvent!");
137: }
138: }
139:
140: /**
141: * Handle the user input
142: * @param obj the value object
143: */
144: private void sendAction(Object value) {
145:
146: StyleModifier modifier;
147: if (value != null) {
148: modifier = StyleModifier.createAddModifier(fKey, value);
149: } else {
150: AttributeSet set = new AttributeSet(fKey);
151: modifier = StyleModifier.createRemoveModifier(set);
152: }
153:
154: if (fCharacter == MenuItemSet.CHARACTER) {
155: fTextPanel.modifyCharacterStyleOnSelection(modifier);
156: } else {
157: fTextPanel.modifyParagraphStyleOnSelection(modifier);
158: }
159: }
160: }
|