source: trunk/src/org/expeditee/auth/gui/MailBay.java@ 1313

Last change on this file since 1313 was 1313, checked in by bln4, 5 years ago

Feedback for test functions.
MailBay only specifies that it is previewing two if there are at least two messages.

File size: 9.8 KB
Line 
1package org.expeditee.auth.gui;
2
3import java.util.LinkedList;
4import java.util.List;
5import java.util.Map;
6
7import org.expeditee.auth.AuthenticatorBrowser;
8import org.expeditee.core.Clip;
9import org.expeditee.core.Colour;
10import org.expeditee.core.Dimension;
11import org.expeditee.core.Font;
12import org.expeditee.core.Image;
13import org.expeditee.gio.EcosystemManager;
14import org.expeditee.gio.GraphicsManager;
15import org.expeditee.gui.DisplayController;
16import org.expeditee.gui.FrameCreator;
17import org.expeditee.gui.FrameGraphics;
18import org.expeditee.gui.FrameIO;
19import org.expeditee.gui.MessageBay;
20import org.expeditee.items.Item;
21import org.expeditee.items.Text;
22import org.expeditee.settings.UserSettings;
23
24public class MailBay {
25 public static final String EXPEDITEE_MAIL_FRAMESET_NAME = "expediteemail";
26
27 /** The y position of the header to the mail bay. */
28 private static final int HEADER_OFFSET_Y = 15;
29
30 /** Space between mail messages */
31 private static final int SPACING = 25;
32
33 /** The (x,y) of the top message. */
34 private static final int MESSAGE_OFFSET_Y = 15 + SPACING;
35 private static final int OFFSET_X = 20;
36
37 /** Buffer image of the mail window. */
38 private static Image _mailBuffer;
39
40 /** The list of messages shown in the mail bay. */
41 private static List<Item> _previewMessages = new LinkedList<Item>();
42 private static List<Item> _messages = new LinkedList<Item>();
43
44 /** Font used for mail messages. */
45 private static Font _messageFont = new Font("Serif-Plain-16");
46
47 /** Used to number messages. */
48 private static int _messageCount;
49
50 /** Creator for creating the mail frames. */
51 private static FrameCreator _creator = null;
52
53 /** The currently logged in user, consulted when deciding if a new FrameCreator is needed. */
54 private static String _forUser = UserSettings.UserName.get();
55
56 /** The link that the preview pane displays pointing towards unprocessed messages. */
57 private static Text _mailLink = new Text(-2, "@Mail", Colour.BLACK, Colour.WHITE);
58
59 /** Wether the link has been drawn before. */
60 private static boolean isLinkInitialized = false;
61
62 /** The position of the mail link. */
63 private static int MAIL_LINK_Y_OFFSET = 100;
64 private static int MAIL_LINK_X = 50;
65
66 /**
67 * Obtain the two messages shown at the bottom of the screen as a preview to the entire collection of mail.
68 * @return
69 */
70 public static List<Item> getPreviewMessages() {
71 return _previewMessages;
72 }
73
74 /**
75 * An item is a 'preview mail item' if it is currently being previewed at the bottom of the screen.
76 * @param i
77 * @return
78 */
79 public static boolean isPreviewMailItem(Item i) {
80 return _previewMessages.contains(i) || i == _mailLink;
81 }
82
83 public static void disconnect() {
84 if (_forUser != UserSettings.UserName.get()) {
85 _creator = null;
86 _forUser = UserSettings.UserName.get();
87 }
88 }
89
90 /**
91 * Adds a message to the MailBay.
92 * @param message The basic text of the message, what is displayed in the bay window down the bottom.
93 * @options A map describing the buttons to be provided as reply options (Button Text, Action to run)
94 * @return
95 */
96 public synchronized static Text addMessage(String timestamp, String message, String message2, Map<String, String> options) {
97 // Invalidate whole area
98 DisplayController.invalidateArea(DisplayController.getMessageBayPaintArea());
99
100 // Ensure frame creator
101 if (_creator == null || _forUser != UserSettings.UserName.get()) {
102 _forUser = UserSettings.UserName.get();
103 _creator = new FrameCreator(EXPEDITEE_MAIL_FRAMESET_NAME, FrameIO.MAIL_PATH, EXPEDITEE_MAIL_FRAMESET_NAME, FrameCreator.ExistingFramesetOptions.AppendAfterLastItem, false, AuthenticatorBrowser.PROFILEENCRYPTIONLABEL);
104 }
105
106 // We have enough space for the header + 2 preview messages
107 if (_previewMessages.size() >= 2) {
108 _previewMessages.remove(0);
109 for (Item i : _previewMessages) {
110 i.setY(i.getY() - SPACING);
111 }
112 }
113
114 // Add new message
115 Mail mail = new Mail(message, message2, options);
116 Text t = mail.getPreviewMessage(true);
117 _messages.add(t);
118 _creator.addText(timestamp, Colour.BLACK, null, null, false);
119 for (Text line: mail.getMessage()) {
120 _creator.addItem(line.copy(), false);
121 }
122 t.setLink(_creator.getCurrentFrame().getName());
123 _previewMessages.add(t);
124 _creator.addSpace(SPACING);
125 _creator.save();
126
127 // Make sure the link points to the latest frame
128 _mailLink.setLink(_creator.getCurrent());
129
130 DisplayController.requestRefresh(true);
131
132 return t;
133 }
134
135 /**
136 * Obtains the image item that is drawn to display the mail bay.
137 * @param clip
138 * @param size
139 * @return
140 */
141 public static Image getImage(Clip clip, Dimension size) {
142 // Can't get an image with an invalid size
143 if (size == null || size.width <= 0 || size.height <= 0) {
144 return null;
145 }
146
147 // Update the buffer
148 updateBuffer(Item.DEFAULT_BACKGROUND, clip, size);
149
150 // Return the image buffer
151 return _mailBuffer;
152 }
153
154
155 public static void clear() {
156 getPreviewMessages().clear();
157 _messageCount = 0;
158 }
159
160 public static Item getMailLink() {
161 return _mailLink;
162 }
163
164 public static void ensureLink() {
165 if (_mailLink.getLink() == null && FrameIO.canAccessFrame(MailBay.EXPEDITEE_MAIL_FRAMESET_NAME + 1)) {
166 _mailLink.setLink(MailBay.EXPEDITEE_MAIL_FRAMESET_NAME + 1);
167 }
168 }
169
170 /** Updates the image buffer to reflect the current state of the mail bay. */
171 private synchronized static void updateBuffer(Colour background, Clip clip, Dimension size) {
172 // If the buffer doesn't exist or is the wrong size, recreate it
173 if (_mailBuffer == null || !_mailBuffer.getSize().equals(size)) {
174 _mailBuffer = Image.createImage(size, true);
175 clip = null; // Need to recreate the entire image;
176 updateSize();
177 }
178
179 // Prepare graphics
180 GraphicsManager g = EcosystemManager.getGraphicsManager();
181 g.pushDrawingSurface(_mailBuffer);
182 if (clip != null) {
183 g.pushClip(clip);
184 }
185 g.setAntialiasing(true);
186 g.clear(background);
187 g.setFont(_messageFont);
188
189 // Paint header
190 FrameGraphics.PaintItem(getHeader(Colour.BLACK));
191
192 // Paint the mail messages to the screen (preview)
193 for (Item message : _previewMessages) {
194 if (message != null) {
195 if (clip == null || clip.isNotClipped() || message.isInDrawingArea(clip.getBounds())) {
196 FrameGraphics.PaintItem(message);
197 }
198 }
199 }
200
201 // Paint the status from the MessageBay
202 Item status = MessageBay.getStatus();
203 if (status != null) {
204 FrameGraphics.PaintItem(status);
205 }
206
207 // Paint the link to the mail frame
208 if (clip == null || clip.isNotClipped() || _mailLink.isInDrawingArea(clip.getBounds())) {
209 FrameGraphics.PaintItem(_mailLink);
210 }
211
212 g.popDrawingSurface();
213 }
214
215 /** Syncs message bay size according to FrameGraphics max size. */
216 private static void updateSize() {
217 for (Item i : _previewMessages) {
218 if (i != null) {
219 i.setOffset(0, -DisplayController.getMessageBayPaintArea().getMinY());
220 }
221 }
222
223 _mailLink.setOffset(0, -DisplayController.getMessageBayPaintArea().getMinY());
224 updateLink();
225 }
226
227 private static void updateLink() {
228 if (!isLinkInitialized && DisplayController.getFramePaintArea() != null
229 && DisplayController.getFramePaintAreaWidth() > 0) {
230 // set up 'Messages' link on the right hand side
231 _mailLink.setPosition(DisplayController.getMessageBayPaintArea().getWidth() - MAIL_LINK_Y_OFFSET,
232 MAIL_LINK_X);
233 _mailLink.setOffset(0, -DisplayController.getMessageBayPaintArea().getMinY());
234 isLinkInitialized = true;
235 } else {
236 _mailLink.setPosition(DisplayController.getMessageBayPaintArea().getWidth() - MAIL_LINK_Y_OFFSET,
237 MAIL_LINK_X);
238 }
239 }
240
241 private static Text getHeader(Colour fontColor) {
242 Text t = new Text("You have [" + _messages.size() + "] unprocessed messages waiting.");
243 if (_messages.size() >= 2) {
244 t.setText(t.getText() + " Two latest below:");
245 }
246 t.setPosition(OFFSET_X, HEADER_OFFSET_Y);
247 t.setOffset(0, -DisplayController.getFramePaintAreaHeight());
248 t.setColor(fontColor);
249 t.setFont(_messageFont.clone());
250 return t;
251 }
252
253 private static String getMessagePrefix() {
254 _messageCount++;
255 return "@" + _messageCount + ": ";
256 }
257
258 private static class Mail {
259 private String message;
260 private Map<String, String> options;
261 private String message2;
262
263 private Mail(String message, String message2, Map<String, String> options) {
264 this.message = message;
265 this.message2 = message2;
266 this.options = options;
267 }
268
269 private Text getPreviewMessage(boolean usePrefix) {
270 Text t = usePrefix ? new Text(getMessagePrefix() + message) : new Text(message);
271 int y = MESSAGE_OFFSET_Y + _previewMessages.size() * SPACING;
272 t.setPosition(OFFSET_X, y);
273 t.setColor(Colour.BLACK);
274 t.setFont(_messageFont.clone());
275 return t;
276 }
277
278 private Text getDetailLine() {
279 if (message2 == null || message2.isEmpty()) {
280 return null;
281 }
282 Text t = new Text(message2);
283 int y = MESSAGE_OFFSET_Y + _previewMessages.size() * SPACING;
284 t.setPosition(OFFSET_X, y);
285 t.setColor(Colour.BLACK);
286 t.setFont(_messageFont.clone());
287 return t;
288 }
289
290 private Text[] getMessage() {
291 List<Text> items = new LinkedList<Text>();
292 items.add(getPreviewMessage(false));
293 Text detail = getDetailLine();
294 if (detail != null) {
295 items.add(detail);
296 }
297
298 int i = items.size();
299 for (String content: options.keySet()) {
300 String action = options.get(content);
301 Text t = new Text(content);
302 int y = MESSAGE_OFFSET_Y + (_previewMessages.size() + i) * SPACING;
303 t.setPosition(OFFSET_X, y);
304 t.setColor(Colour.BLUE);
305 t.setFont(_messageFont.clone());
306 t.setAction(action);
307 items.add(t);
308 i++;
309 }
310
311 return items.toArray(new Text[] {});
312 }
313 }
314}
Note: See TracBrowser for help on using the repository browser.