001: /*
002: *
003: * Copyright (c) 2007, Sun Microsystems, Inc.
004: *
005: * All rights reserved.
006: *
007: * Redistribution and use in source and binary forms, with or without
008: * modification, are permitted provided that the following conditions
009: * are met:
010: *
011: * * Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: * * Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in the
015: * documentation and/or other materials provided with the distribution.
016: * * Neither the name of Sun Microsystems nor the names of its contributors
017: * may be used to endorse or promote products derived from this software
018: * without specific prior written permission.
019: *
020: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
021: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
022: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
023: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
024: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
025: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
026: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
027: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
028: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
029: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
030: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
031: */
032: package example.mms;
033:
034: import java.io.InputStream;
035:
036: import javax.microedition.lcdui.*;
037:
038: import javax.wireless.messaging.*;
039:
040: public class PartsDialog implements CommandListener {
041: private static final Command CMD_BACK = new Command("Back",
042: Command.BACK, 1);
043: private static final Command CMD_NEXT = new Command("Next",
044: Command.OK, 1);
045: private static final Command CMD_OK = new Command("OK", Command.OK,
046: 1);
047: private static final Command CMD_CANCEL = new Command("Cancel",
048: Command.CANCEL, 1);
049:
050: /** current display. */
051: private MMSSend mmsSend;
052: private List typeList;
053: public int counter = 0;
054:
055: /** Creates a new instance of PartsDialog */
056: public PartsDialog(MMSSend mmsSend) {
057: this .mmsSend = mmsSend;
058:
059: String[] stringArray = { "Text", "Image" };
060:
061: typeList = new List("Add Part: Type", Choice.EXCLUSIVE,
062: stringArray, null);
063: typeList.addCommand(CMD_BACK);
064: typeList.addCommand(CMD_NEXT);
065: typeList.setCommandListener(this );
066: }
067:
068: public void show() {
069: mmsSend.getDisplay().setCurrent(typeList);
070: }
071:
072: /**
073: * Respond to commands, including exit
074: * @param c user interface command requested
075: * @param s screen object initiating the request
076: */
077: public void commandAction(Command c, Displayable s) {
078: try {
079: if (c == CMD_BACK) {
080: mmsSend.show();
081: } else if (c == CMD_NEXT) {
082: if (typeList.getSelectedIndex() == 0) {
083: mmsSend.getDisplay().setCurrent(new TextDialog());
084: } else {
085: mmsSend.getDisplay().setCurrent(new ImageDialog());
086: }
087: }
088: } catch (Exception ex) {
089: ex.printStackTrace();
090: }
091: }
092:
093: private class TextDialog extends Form implements CommandListener {
094: private Displayable mainForm;
095: private TextField text;
096: private String mimeType = "text/plain";
097:
098: public TextDialog() {
099: super ("Add Text");
100:
101: text = new TextField("Text: ", null, 256, TextField.ANY);
102: append(text);
103: append("MIME-Type: " + mimeType);
104:
105: addCommand(CMD_OK);
106: addCommand(CMD_CANCEL);
107: setCommandListener(this );
108: }
109:
110: public void commandAction(Command c, Displayable s) {
111: try {
112: if (c == CMD_OK) {
113: String encoding = "UTF-8";
114: byte[] contents = text.getString().getBytes(
115: encoding);
116: mmsSend.getMessage().addPart(
117: new MessagePart(contents, 0,
118: contents.length, mimeType, "id"
119: + counter,
120: "contentLocation", encoding));
121: counter++;
122: mmsSend.show();
123: } else if (c == CMD_CANCEL) {
124: mmsSend.show();
125: }
126: } catch (Exception ex) {
127: ex.printStackTrace();
128: }
129: }
130: }
131:
132: private class ImageDialog extends Form implements CommandListener {
133: private Displayable mainForm;
134: private ChoiceGroup cg;
135: private String mimeType = "image/png";
136: private String[] resouces = { "/Duke1.png", "/Duke2.png" };
137: private String[] imagesNames = { "Duke 1", "Duke 2" };
138:
139: public ImageDialog() {
140: super ("Add Image");
141:
142: cg = new ChoiceGroup("Select Image", Choice.EXCLUSIVE,
143: imagesNames, null);
144: append(cg);
145:
146: append("MIME-Type: " + mimeType);
147:
148: addCommand(CMD_OK);
149: addCommand(CMD_CANCEL);
150: setCommandListener(this );
151: }
152:
153: public void commandAction(Command c, Displayable s) {
154: try {
155: if (c == CMD_OK) {
156: int index = cg.getSelectedIndex();
157: String resouce = resouces[index];
158: InputStream is = getClass().getResourceAsStream(
159: resouce);
160: byte[] contents = new byte[is.available()];
161: is.read(contents);
162:
163: String contentLocation = imagesNames[index];
164: mmsSend.getMessage().addPart(
165: new MessagePart(contents, 0,
166: contents.length, mimeType, "id"
167: + counter, contentLocation,
168: null));
169: counter++;
170: mmsSend.show();
171: } else if (c == CMD_CANCEL) {
172: mmsSend.show();
173: }
174: } catch (Exception ex) {
175: ex.printStackTrace();
176: }
177: }
178: }
179: }
|