source: trunk/hiptop/cognet++/org/twodot/cognet/ChatWindow.java@ 210

Last change on this file since 210 was 210, checked in by Nicholas Riley, 18 years ago

cognet++: Brian Swetland et al.'s generic proxied hiptop chat client.
Requires cognetd to function.

File size: 5.1 KB
Line 
1/*
2 * cognet chat app
3 *
4 * Copyright 2003, Brian Swetland <swetland@frotz.net>
5 * See LICENSE for redistribution terms
6 *
7 */
8package org.twodot.cognet;
9
10import danger.ui.ScreenWindow;
11import danger.ui.EditText;
12import danger.ui.Layout;
13import danger.app.Event;
14import danger.ui.Font;
15import java.util.Date;
16import danger.util.format.DateFormat;
17
18import danger.ui.MarqueeAlert;
19import danger.ui.NotificationManager;
20import danger.ui.Shortcut;
21
22public class ChatWindow extends ScreenWindow
23{
24 public ChatWindow(Engine e, String ctxt, String key) {
25 super(ctxt);
26
27 history = new ChatHistory(8192, e);
28 history.setSize(getWidth() - 1, getHeight() - 20);
29 history.setPosition(1, 0);
30 this.addChild(history);
31 history.show();
32
33 line = new EditText(true, true);
34 Layout.positionBelow(line, history, 0, 2);
35 line.setSize(getWidth(), getHeight() - line.getTop());
36 line.show();
37 addChild(line);
38
39 setFocusedChild(line);
40 context = ctxt;
41 this.key = key;
42 engine = e;
43 app = e.app;
44
45 if(key.charAt(0) == '%') special = true;
46 if(key.charAt(0) == '#') channel = true;
47 if(key.charAt(0) == '&') channel = true;
48 }
49
50 public void show() {
51 engine.MakeActive(this);
52 super.show();
53 }
54
55 public void adjustActionMenuState() {
56 app.MakeMenu(getActionMenu(),this);
57 }
58
59 public boolean eventKeyUp(char key, Event event) {
60 if((key == '\n') || (key == '\r')){
61 engine.Command(this.key,line.toString());
62 line.clear();
63 return true;
64 } else {
65 return super.eventKeyUp(key, event);
66 }
67 }
68
69 public void ScrollList(int count) {
70 if(count < 0) {
71 count = -count;
72 for(int i = 0; i < count; i++) history.ScrollUp();
73 } else {
74 for(int i = 0; i < count; i++) history.ScrollDown();
75 }
76 history.invalidate();
77
78 }
79
80 // no autorepeat
81 public boolean eventWidgetUp(int widget, Event event) {
82 switch(widget) {
83 case Event.DEVICE_WHEEL:
84 ScrollList(-1);
85 return true;
86 case Event.DEVICE_WHEEL_PAGE_UP:
87 ScrollList(-8);
88 return true;
89 case Event.DEVICE_WHEEL_PAGE_DOWN:
90 ScrollList(8);
91 return true;
92 case Event.DEVICE_BUTTON_BACK:
93 getApplication().returnToLauncher();
94 return true;
95 default:
96 return super.eventWidgetUp(widget,event);
97 }
98 }
99
100 // autorepeat
101 public boolean eventWidgetDown(int widget, Event event) {
102 switch(widget) {
103 case Event.DEVICE_WHEEL:
104 ScrollList(1);
105 return true;
106 case Event.DEVICE_WHEEL_PAGE_DOWN:
107 ScrollList(8);
108 return true;
109 default:
110 return super.eventWidgetDown(widget,event);
111 }
112 }
113
114 public boolean receiveEvent(Event e) {
115 //e.DebugPrint();
116 switch(e.type){
117 case kMessage:
118 String s = (String) e.argument;
119 history.AddChannel(s);
120 history.invalidate();
121 if(!active) {
122 if(!mailbox) {
123 mailbox = true;
124 engine.UpdateAlerts();
125 }
126 //if(!channel && !special){
127 //}
128 }
129
130 if (!app.fgapp) {
131 long timeNow = System.currentTimeMillis();
132 if ((app.doMarquee) && (timeNow >= (lastMarquee + (1000 * marqueeTimeout)))) {
133 NotificationManager.marqueeAlertNotify(new MarqueeAlert(s, app.bm_notify,1));
134 lastMarquee = timeNow;
135 }
136 app.Notify();
137 }
138
139 Date date = new Date();
140 setSubTitle("Last Message " + DateFormat.withFormat("h:mma", date));
141
142 return true;
143 case kClear:
144 history.Clear();
145 history.invalidate();
146 return true;
147 }
148
149 return super.receiveEvent(e);
150 }
151
152 void MSG(String s) {
153 Event e = new Event(this, kMessage);
154 e.argument = s;
155 getListener().sendEvent(e);
156 }
157
158 void CLR() {
159 getListener().sendEvent(new Event(this, kClear));
160 }
161
162
163 void URL(String URL, String Category, String Name)
164 {
165 links.AddLink(URL, Category, Name);
166 app.MakeMenu(getActionMenu(),this);
167 }
168
169 public boolean eventShortcut(char c, Event e) {
170 if ((e.modifiers & Event.EVENT_MODIFIER_CAPS) != 0) {
171 if (engine.saveQuickKey(key, (char)e.data))
172 return true;
173 }
174 switch (c) {
175 case '@':
176 engine.login.show();
177 return true;
178 case ',':
179 ChatWindow cw = engine.NextMailbox(this);
180 if(cw != null) cw.show();
181 return true;
182 case Shortcut.ARROW_LEFT:
183 prev.show();
184 return true;
185 case Shortcut.ARROW_RIGHT:
186 next.show();
187 return true;
188 default:
189 System.err.println("Got shortcut " + c);
190 return super.eventShortcut(c, e);
191 }
192 }
193
194 static final int kMessage = 1;
195 static final int kClear = 2;
196
197 Cognet app;
198 ChatHistory history;
199 EditText line;
200 Engine engine;
201
202 String context;
203 String key;
204
205 void Remove() {
206 next.prev = prev;
207 prev.next = next;
208 next = this;
209 prev = this;
210 hide();
211 }
212
213 void SetQuickKey(char quickkey) {
214 this.quickkey = quickkey;
215 if(quickkey != '\0'){
216 setTitle(Shortcut.makeShortcutLabel(quickkey, true) + ": " + context);
217 } else {
218 setTitle(context);
219 }
220 }
221
222 void setFont(Font f) {
223 if (f != null) {
224 history.setPlainFont(f);
225 history.invalidate();
226 }
227 }
228
229 void setEmoticons(boolean onOff) {
230 history.setEmoticons(onOff);
231 }
232
233 char quickkey;
234 boolean active;
235 boolean mailbox;
236 boolean special;
237 boolean channel;
238 long lastMarquee;
239 public final int marqueeTimeout = 60; // one minute
240
241 public LinkMenu links = new LinkMenu();
242
243 ChatWindow next, prev;
244}
Note: See TracBrowser for help on using the repository browser.