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

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

Followup: credentials frame is now programmatically calculated rather than hard coded.

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