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

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

Moved the static field USER_NOBODY to AuthenticatorBrowser from Browser as it makes more sense there.
Added functionality to log out, equiv to closing Expeditee and starting it again.

File size: 9.6 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 _forUser = 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 public static void disconnect() {
83 if (_forUser != UserSettings.UserName.get()) {
84 _creator = null;
85 _forUser = UserSettings.UserName.get();
86 }
87 }
88
89 /**
90 * Adds a message to the MailBay.
91 * @param message The basic text of the message, what is displayed in the bay window down the bottom.
92 * @options A map describing the buttons to be provided as reply options (Button Text, Action to run)
93 * @return
94 */
95 public synchronized static Text addMessage(String timestamp, String message, String message2, Map<String, String> options) {
96 // Invalidate whole area
97 DisplayController.invalidateArea(DisplayController.getMessageBayPaintArea());
98
99 // Ensure frame creator
100 if (_creator == null || _forUser != UserSettings.UserName.get()) {
101 _forUser = UserSettings.UserName.get();
102 _creator = new FrameCreator(EXPEDITEE_MAIL_FRAMESET_NAME, FrameIO.MAIL_PATH, EXPEDITEE_MAIL_FRAMESET_NAME, FrameCreator.ExistingFramesetOptions.AppendAfterLastItem, false);
103 }
104
105 // We have enough space for the header + 2 preview messages
106 if (_previewMessages.size() >= 2) {
107 _previewMessages.remove(0);
108 for (Item i : _previewMessages) {
109 i.setY(i.getY() - SPACING);
110 }
111 }
112
113 // Add new message
114 Mail mail = new Mail(message, message2, options);
115 Text t = mail.getPreviewMessage(true);
116 _messages.add(t);
117 _creator.addText(timestamp, Colour.BLACK, null, null, false);
118 for (Text line: mail.getMessage()) {
119 _creator.addItem(line.copy(), false);
120 }
121 t.setLink(_creator.getCurrentFrame().getName());
122 _previewMessages.add(t);
123 _creator.addSpace(SPACING);
124 _creator.save();
125
126 // Make sure the link points to the latest frame
127 _mailLink.setLink(_creator.getCurrent());
128
129 DisplayController.requestRefresh(true);
130
131 return t;
132 }
133
134 /**
135 * Obtains the image item that is drawn to display the mail bay.
136 * @param clip
137 * @param size
138 * @return
139 */
140 public static Image getImage(Clip clip, Dimension size) {
141 // Can't get an image with an invalid size
142 if (size == null || size.width <= 0 || size.height <= 0) {
143 return null;
144 }
145
146 // Update the buffer
147 updateBuffer(Item.DEFAULT_BACKGROUND, clip, size);
148
149 // Return the image buffer
150 return _mailBuffer;
151 }
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. Two latest below:");
242 t.setPosition(OFFSET_X, HEADER_OFFSET_Y);
243 t.setOffset(0, -DisplayController.getFramePaintAreaHeight());
244 t.setColor(fontColor);
245 t.setFont(_messageFont.clone());
246 return t;
247 }
248
249 private static String getMessagePrefix() {
250 _messageCount++;
251 return "@" + _messageCount + ": ";
252 }
253
254 private static class Mail {
255 private String message;
256 private Map<String, String> options;
257 private String message2;
258
259 private Mail(String message, String message2, Map<String, String> options) {
260 this.message = message;
261 this.message2 = message2;
262 this.options = options;
263 }
264
265 private Text getPreviewMessage(boolean usePrefix) {
266 Text t = usePrefix ? new Text(getMessagePrefix() + message) : new Text(message);
267 int y = MESSAGE_OFFSET_Y + _previewMessages.size() * SPACING;
268 t.setPosition(OFFSET_X, y);
269 t.setColor(Colour.BLACK);
270 t.setFont(_messageFont.clone());
271 return t;
272 }
273
274 private Text getDetailLine() {
275 if (message2 == null || message2.isEmpty()) {
276 return null;
277 }
278 Text t = new Text(message2);
279 int y = MESSAGE_OFFSET_Y + _previewMessages.size() * SPACING;
280 t.setPosition(OFFSET_X, y);
281 t.setColor(Colour.BLACK);
282 t.setFont(_messageFont.clone());
283 return t;
284 }
285
286 private Text[] getMessage() {
287 List<Text> items = new LinkedList<Text>();
288 items.add(getPreviewMessage(false));
289 Text detail = getDetailLine();
290 if (detail != null) {
291 items.add(detail);
292 }
293
294 int i = items.size();
295 for (String content: options.keySet()) {
296 String action = options.get(content);
297 Text t = new Text(content);
298 int y = MESSAGE_OFFSET_Y + (_previewMessages.size() + i) * SPACING;
299 t.setPosition(OFFSET_X, y);
300 t.setColor(Colour.BLUE);
301 t.setFont(_messageFont.clone());
302 t.setAction(action);
303 items.add(t);
304 i++;
305 }
306
307 return items.toArray(new Text[] {});
308 }
309 }
310}
Note: See TracBrowser for help on using the repository browser.