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

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

It is now possible to complete the process of recovering access to a Expeditee account. Further work, in the form of frames in the authentication frameset, are to follow.
A refactoring/tidy up has also been completed.

File size: 9.9 KB
Line 
1package org.expeditee.auth.mail.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 public static void clear() {
155 getPreviewMessages().clear();
156 _messageCount = 0;
157 }
158
159 public static Item getMailLink() {
160 return _mailLink;
161 }
162
163 public static void ensureLink() {
164 if (_mailLink.getLink() == null && FrameIO.canAccessFrame(MailBay.EXPEDITEE_MAIL_FRAMESET_NAME + 1)) {
165 _mailLink.setLink(MailBay.EXPEDITEE_MAIL_FRAMESET_NAME + 1);
166 }
167 }
168
169 /** Updates the image buffer to reflect the current state of the mail bay. */
170 private synchronized static void updateBuffer(Colour background, Clip clip, Dimension size) {
171 // If the buffer doesn't exist or is the wrong size, recreate it
172 if (_mailBuffer == null || !_mailBuffer.getSize().equals(size)) {
173 _mailBuffer = Image.createImage(size, true);
174 clip = null; // Need to recreate the entire image;
175 updateSize();
176 }
177
178 // Prepare graphics
179 GraphicsManager g = EcosystemManager.getGraphicsManager();
180 g.pushDrawingSurface(_mailBuffer);
181 if (clip != null) {
182 g.pushClip(clip);
183 }
184 g.setAntialiasing(true);
185 g.clear(background);
186 g.setFont(_messageFont);
187
188 // Paint header
189 FrameGraphics.PaintItem(getHeader(Colour.BLACK));
190
191 // Paint the mail messages to the screen (preview)
192 for (Item message : _previewMessages) {
193 if (message != null) {
194 if (clip == null || clip.isNotClipped() || message.isInDrawingArea(clip.getBounds())) {
195 FrameGraphics.PaintItem(message);
196 }
197 }
198 }
199
200 // Paint the status from the MessageBay
201 Item status = MessageBay.getStatus();
202 if (status != null) {
203 FrameGraphics.PaintItem(status);
204 }
205
206 // Paint the link to the mail frame
207 if (clip == null || clip.isNotClipped() || _mailLink.isInDrawingArea(clip.getBounds())) {
208 FrameGraphics.PaintItem(_mailLink);
209 }
210
211 g.popDrawingSurface();
212 }
213
214 /** Syncs message bay size according to FrameGraphics max size. */
215 private static void updateSize() {
216 for (Item i : _previewMessages) {
217 if (i != null) {
218 i.setOffset(0, -DisplayController.getMessageBayPaintArea().getMinY());
219 }
220 }
221
222 _mailLink.setOffset(0, -DisplayController.getMessageBayPaintArea().getMinY());
223 updateLink();
224 }
225
226 private static void updateLink() {
227 if (!isLinkInitialized && DisplayController.getFramePaintArea() != null
228 && DisplayController.getFramePaintAreaWidth() > 0) {
229 // set up 'Messages' link on the right hand side
230 _mailLink.setPosition(DisplayController.getMessageBayPaintArea().getWidth() - MAIL_LINK_Y_OFFSET,
231 MAIL_LINK_X);
232 _mailLink.setOffset(0, -DisplayController.getMessageBayPaintArea().getMinY());
233 isLinkInitialized = true;
234 } else {
235 _mailLink.setPosition(DisplayController.getMessageBayPaintArea().getWidth() - MAIL_LINK_Y_OFFSET,
236 MAIL_LINK_X);
237 }
238 }
239
240 private static Text getHeader(Colour fontColor) {
241 Text t = new Text("You have [" + _messages.size() + "] unprocessed messages waiting.");
242 if (_messages.size() >= 2) {
243 t.setText(t.getText() + " Two latest below:");
244 }
245 t.setPosition(OFFSET_X, HEADER_OFFSET_Y);
246 t.setOffset(0, -DisplayController.getFramePaintAreaHeight());
247 t.setColor(fontColor);
248 t.setFont(_messageFont.clone());
249 return t;
250 }
251
252 private static String getMessagePrefix() {
253 _messageCount++;
254 return "@" + _messageCount + ": ";
255 }
256
257 private static class Mail {
258 private String message;
259 private Map<String, String> options;
260 private String message2;
261
262 private Mail(String message, String message2, Map<String, String> options) {
263 this.message = message;
264 this.message2 = message2;
265 this.options = options;
266 }
267
268 private Text getPreviewMessage(boolean usePrefix) {
269 Text t = usePrefix ? new Text(getMessagePrefix() + message) : new Text(message);
270 int y = MESSAGE_OFFSET_Y + _previewMessages.size() * SPACING;
271 t.setPosition(OFFSET_X, y);
272 t.setColor(Colour.BLACK);
273 t.setFont(_messageFont.clone());
274 return t;
275 }
276
277 private Text getDetailLine() {
278 if (message2 == null || message2.isEmpty()) {
279 return null;
280 }
281 Text t = new Text(message2);
282 int y = MESSAGE_OFFSET_Y + _previewMessages.size() * SPACING;
283 t.setPosition(OFFSET_X, y);
284 t.setColor(Colour.BLACK);
285 t.setFont(_messageFont.clone());
286 return t;
287 }
288
289 private Text[] getMessage() {
290 List<Text> items = new LinkedList<Text>();
291 items.add(getPreviewMessage(false));
292 Text detail = getDetailLine();
293 if (detail != null) {
294 items.add(detail);
295 }
296
297 int i = items.size();
298 for (String content: options.keySet()) {
299 String[] split = content.split(":::");
300 String action = options.get(content);
301 Text t = new Text(split[0]);
302 for (int o = 1; o < split.length; o++) {
303 t.addToData(split[o]);
304 }
305 int y = MESSAGE_OFFSET_Y + (_previewMessages.size() + i) * SPACING;
306 t.setPosition(OFFSET_X, y);
307 t.setColor(Colour.BLUE);
308 t.setFont(_messageFont.clone());
309 t.setAction(action);
310 items.add(t);
311 i++;
312 }
313
314 return items.toArray(new Text[] {});
315 }
316 }
317}
Note: See TracBrowser for help on using the repository browser.