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

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

Alterations to how mail works to work better in a cloud environment.
General code tidying and updating to work with larger changes happening to Expeditee.

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