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

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

The MailBay will now link to the expediteemail frameset if it exists and doesn't already link to it. (This would occur if you had not received a new message since last log in)

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