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

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

FrameCreator now has the option to extend an existing frameset rather than overwrite or ignore the previous frameset content. A boolean variable to the FrameCreator constructor has changed to a Enum with three options to facilitate this.

The MailBay uses this new FrameCreator functionality.

In the near future, 'extend' option will be updated to more precisely reobtain the exact state of the FrameCreator when last used (IE, what the FrameCreator thinks its Y position is).

File size: 9.2 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.ReobtainStateFromExistingFrames, 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 _previewMessages.add(t);
110 _messages.add(t);
111 _creator.addText(timestamp, Colour.BLACK, null, null, false);
112 for (Text line: mail.getMessage()) {
113 _creator.addItem(line.copy(), false);
114 }
115 _creator.addSpace(SPACING);
116 _creator.save();
117
118 // Make sure the link points to the latest frame
119 _mailLink.setLink(_creator.getCurrent());
120
121 DisplayController.requestRefresh(true);
122
123 return t;
124 }
125
126 /**
127 * Obtains the image item that is drawn to display the mail bay.
128 * @param clip
129 * @param size
130 * @return
131 */
132 public static Image getImage(Clip clip, Dimension size) {
133 // Can't get an image with an invalid size
134 if (size == null || size.width <= 0 || size.height <= 0) {
135 return null;
136 }
137
138 // Update the buffer
139 updateBuffer(Item.DEFAULT_BACKGROUND, clip, size);
140
141 // Return the image buffer
142 return _mailBuffer;
143 }
144
145
146 public static void clear() {
147 getPreviewMessages().clear();
148 _messageCount = 0;
149 }
150
151 public static Item getMailLink() {
152 return _mailLink;
153 }
154
155 /** Updates the image buffer to reflect the current state of the mail bay. */
156 private synchronized static void updateBuffer(Colour background, Clip clip, Dimension size) {
157 // If the buffer doesn't exist or is the wrong size, recreate it
158 if (_mailBuffer == null || !_mailBuffer.getSize().equals(size)) {
159 _mailBuffer = Image.createImage(size, true);
160 clip = null; // Need to recreate the entire image;
161 updateSize();
162 }
163
164 // Prepare graphics
165 GraphicsManager g = EcosystemManager.getGraphicsManager();
166 g.pushDrawingSurface(_mailBuffer);
167 if (clip != null) {
168 g.pushClip(clip);
169 }
170 g.setAntialiasing(true);
171 g.clear(background);
172 g.setFont(_messageFont);
173
174 // Paint header
175 FrameGraphics.PaintItem(getHeader(Colour.BLACK));
176
177 // Paint the mail messages to the screen (preview)
178 for (Item message : _previewMessages) {
179 if (message != null) {
180 if (clip == null || clip.isNotClipped() || message.isInDrawingArea(clip.getBounds())) {
181 FrameGraphics.PaintItem(message);
182 }
183 }
184 }
185
186 // Paint the status from the MessageBay
187 Item status = MessageBay.getStatus();
188 if (status != null) {
189 FrameGraphics.PaintItem(status);
190 }
191
192 // Paint the link to the mail frame
193 if (clip == null || clip.isNotClipped() || _mailLink.isInDrawingArea(clip.getBounds())) {
194 FrameGraphics.PaintItem(_mailLink);
195 }
196
197 g.popDrawingSurface();
198 }
199
200 /** Syncs message bay size according to FrameGraphics max size. */
201 private static void updateSize() {
202 for (Item i : _previewMessages) {
203 if (i != null) {
204 i.setOffset(0, -DisplayController.getMessageBayPaintArea().getMinY());
205 }
206 }
207
208 _mailLink.setOffset(0, -DisplayController.getMessageBayPaintArea().getMinY());
209 updateLink();
210 }
211
212 private static void updateLink() {
213 if (!isLinkInitialized && DisplayController.getFramePaintArea() != null
214 && DisplayController.getFramePaintAreaWidth() > 0) {
215 // set up 'Messages' link on the right hand side
216 _mailLink.setPosition(DisplayController.getMessageBayPaintArea().getWidth() - MAIL_LINK_Y_OFFSET,
217 MAIL_LINK_X);
218 _mailLink.setOffset(0, -DisplayController.getMessageBayPaintArea().getMinY());
219 isLinkInitialized = true;
220 } else {
221 _mailLink.setPosition(DisplayController.getMessageBayPaintArea().getWidth() - MAIL_LINK_Y_OFFSET,
222 MAIL_LINK_X);
223 }
224 }
225
226 private static Text getHeader(Colour fontColor) {
227 Text t = new Text("You have [" + _messages.size() + "] unprocessed messages waiting. Two latest below:");
228 t.setPosition(OFFSET_X, HEADER_OFFSET_Y);
229 t.setOffset(0, -DisplayController.getFramePaintAreaHeight());
230 t.setColor(fontColor);
231 t.setFont(_messageFont.clone());
232 return t;
233 }
234
235 private static String getMessagePrefix() {
236 _messageCount++;
237 return "@" + _messageCount + ": ";
238 }
239
240 private static class Mail {
241 private String message;
242 private Map<String, String> options;
243 private String message2;
244
245 private Mail(String message, String message2, Map<String, String> options) {
246 this.message = message;
247 this.message2 = message2;
248 this.options = options;
249 }
250
251 private Text getPreviewMessage(boolean usePrefix) {
252 Text t = usePrefix ? new Text(getMessagePrefix() + message) : new Text(message);
253 int y = MESSAGE_OFFSET_Y + _previewMessages.size() * SPACING;
254 t.setPosition(OFFSET_X, y);
255 t.setColor(Colour.BLACK);
256 t.setFont(_messageFont.clone());
257 return t;
258 }
259
260 private Text getDetailLine() {
261 if (message2 == null || message2.isEmpty()) {
262 return null;
263 }
264 Text t = new Text(message2);
265 int y = MESSAGE_OFFSET_Y + _previewMessages.size() * SPACING;
266 t.setPosition(OFFSET_X, y);
267 t.setColor(Colour.BLACK);
268 t.setFont(_messageFont.clone());
269 return t;
270 }
271
272 private Text[] getMessage() {
273 List<Text> items = new LinkedList<Text>();
274 items.add(getPreviewMessage(false));
275 Text detail = getDetailLine();
276 if (detail != null) {
277 items.add(detail);
278 }
279
280 int i = items.size();
281 for (String content: options.keySet()) {
282 String action = options.get(content);
283 Text t = new Text(content);
284 int y = MESSAGE_OFFSET_Y + (_previewMessages.size() + i) * SPACING;
285 t.setPosition(OFFSET_X, y);
286 t.setColor(Colour.BLUE);
287 t.setFont(_messageFont.clone());
288 t.setAction(action);
289 items.add(t);
290 i++;
291 }
292
293 return items.toArray(new Text[] {});
294 }
295 }
296}
Note: See TracBrowser for help on using the repository browser.