/* * cognet chat app * * Copyright 2003, Brian Swetland * See LICENSE for redistribution terms * */ package org.twodot.cognet; import danger.ui.ScreenWindow; import danger.ui.EditText; import danger.ui.Layout; import danger.app.Event; import danger.ui.Font; import java.util.Date; import danger.util.format.DateFormat; import danger.ui.MarqueeAlert; import danger.ui.NotificationManager; import danger.ui.Shortcut; public class ChatWindow extends ScreenWindow { public ChatWindow(Engine e, String ctxt, String key) { super(ctxt); history = new ChatHistory(8192, e); history.setSize(getWidth() - 1, getHeight() - 20); history.setPosition(1, 0); this.addChild(history); history.show(); line = new EditText(true, true); Layout.positionBelow(line, history, 0, 2); line.setSize(getWidth(), getHeight() - line.getTop()); line.show(); addChild(line); setFocusedChild(line); context = ctxt; this.key = key; engine = e; app = e.app; if(key.charAt(0) == '%') special = true; if(key.charAt(0) == '#') channel = true; if(key.charAt(0) == '&') channel = true; } public void show() { engine.MakeActive(this); super.show(); } public void adjustActionMenuState() { app.MakeMenu(getActionMenu(),this); } public boolean eventKeyUp(char key, Event event) { if((key == '\n') || (key == '\r')){ engine.Command(this.key,line.toString()); line.clear(); return true; } else { return super.eventKeyUp(key, event); } } public void ScrollList(int count) { if(count < 0) { count = -count; for(int i = 0; i < count; i++) history.ScrollUp(); } else { for(int i = 0; i < count; i++) history.ScrollDown(); } history.invalidate(); } // no autorepeat public boolean eventWidgetUp(int widget, Event event) { switch(widget) { case Event.DEVICE_WHEEL: ScrollList(-1); return true; case Event.DEVICE_WHEEL_PAGE_UP: ScrollList(-8); return true; case Event.DEVICE_WHEEL_PAGE_DOWN: ScrollList(8); return true; case Event.DEVICE_BUTTON_BACK: getApplication().returnToLauncher(); return true; default: return super.eventWidgetUp(widget,event); } } // autorepeat public boolean eventWidgetDown(int widget, Event event) { switch(widget) { case Event.DEVICE_WHEEL: ScrollList(1); return true; case Event.DEVICE_WHEEL_PAGE_DOWN: ScrollList(8); return true; default: return super.eventWidgetDown(widget,event); } } public boolean receiveEvent(Event e) { //e.DebugPrint(); switch(e.type){ case kMessage: String s = (String) e.argument; history.AddChannel(s); history.invalidate(); if(!active) { if(!mailbox) { mailbox = true; engine.UpdateAlerts(); } //if(!channel && !special){ //} } if (!app.fgapp) { long timeNow = System.currentTimeMillis(); if ((app.doMarquee) && (timeNow >= (lastMarquee + (1000 * marqueeTimeout)))) { NotificationManager.marqueeAlertNotify(new MarqueeAlert(s, app.bm_notify,1)); lastMarquee = timeNow; } app.Notify(); } Date date = new Date(); setSubTitle("Last Message " + DateFormat.withFormat("h:mma", date)); return true; case kClear: history.Clear(); history.invalidate(); return true; } return super.receiveEvent(e); } void MSG(String s) { Event e = new Event(this, kMessage); e.argument = s; getListener().sendEvent(e); } void CLR() { getListener().sendEvent(new Event(this, kClear)); } void URL(String URL, String Category, String Name) { links.AddLink(URL, Category, Name); app.MakeMenu(getActionMenu(),this); } public boolean eventShortcut(char c, Event e) { if ((e.modifiers & Event.EVENT_MODIFIER_CAPS) != 0) { if (engine.saveQuickKey(key, (char)e.data)) return true; } switch (c) { case '@': engine.login.show(); return true; case ',': ChatWindow cw = engine.NextMailbox(this); if(cw != null) cw.show(); return true; case Shortcut.ARROW_LEFT: prev.show(); return true; case Shortcut.ARROW_RIGHT: next.show(); return true; default: System.err.println("Got shortcut " + c); return super.eventShortcut(c, e); } } static final int kMessage = 1; static final int kClear = 2; Cognet app; ChatHistory history; EditText line; Engine engine; String context; String key; void Remove() { next.prev = prev; prev.next = next; next = this; prev = this; hide(); } void SetQuickKey(char quickkey) { this.quickkey = quickkey; if(quickkey != '\0'){ setTitle(Shortcut.makeShortcutLabel(quickkey, true) + ": " + context); } else { setTitle(context); } } void setFont(Font f) { if (f != null) { history.setPlainFont(f); history.invalidate(); } } void setEmoticons(boolean onOff) { history.setEmoticons(onOff); } char quickkey; boolean active; boolean mailbox; boolean special; boolean channel; long lastMarquee; public final int marqueeTimeout = 60; // one minute public LinkMenu links = new LinkMenu(); ChatWindow next, prev; }