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

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

Directed mail bay to create framesets in FrameIO.MAIL_PATH

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