source: trunk/hiptop/cognet++/org/twodot/cognet/LinkMenu.java@ 420

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

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

File size: 2.8 KB
Line 
1/*
2 * cognet chat app
3 *
4 * Copyright 2003, Daniel Grobe Scahs <sachs@uiuc.edu>
5 * See LICENSE for redistribution terms
6 *
7 */
8package org.twodot.cognet;
9
10import danger.ui.Menu;
11import danger.ui.MenuItem;
12
13class LinkQueue
14{
15 public static final int maxQueueLen = 9;
16
17 private String LinkURLs[] = new String[maxQueueLen];
18 private String LinkNames[] = new String[maxQueueLen];
19 private int queueLen = 0;
20
21 public void addLink(String URL, String Name)
22 {
23 System.err.println("Adding "+URL);
24 if( queueLen < maxQueueLen )
25 queueLen++;
26 else
27 {
28 int i;
29
30 for( i = 0; i < queueLen-1; i++ )
31 {
32 LinkURLs[i] = LinkURLs[i+1];
33 LinkNames[i] = LinkNames[i+1];
34 }
35 }
36
37 if( Name.length() == 0 )
38 Name = URL;
39
40 LinkURLs[queueLen-1] = URL;
41 LinkNames[queueLen-1] = Name;
42 }
43
44 public String[] getNames()
45 {
46 return LinkNames;
47 }
48
49 public String[] getURLs()
50 {
51 return LinkURLs;
52 }
53
54 public int getQueueLen()
55 {
56 return queueLen;
57 }
58}
59
60
61public class LinkMenu
62{
63 public static final int maxLinkMenus = 9;
64 public static final int maxLinkTargets = maxLinkMenus * LinkQueue.maxQueueLen;
65
66 int numMenus;
67
68 public String Categories[] = new String[maxLinkMenus];
69 public LinkQueue Queue[] = new LinkQueue[maxLinkMenus];
70
71 public String LinkTarget[] = new String[maxLinkTargets];
72
73 public void AddLink(String URL, String Category, String Name)
74 {
75 synchronized(this)
76 {
77 int catIndex = findCategory(Category);
78 Queue[catIndex].addLink(URL, Name);
79 }
80 }
81
82 public Menu makeLinkMenu(int base)
83 {
84 Menu Linkmenu;
85 int i,j;
86 int index = 0;
87
88 Menu links = new Menu("Links");
89 MenuItem mi;
90
91 synchronized(this)
92 {
93 if( numMenus == 0 )
94 return null;
95
96 for( i = 0; i < numMenus; i++ )
97 {
98 int size;
99 String Names[], URLs[];
100
101 Menu submenu = new Menu(Categories[i]);
102
103 size = Queue[i].getQueueLen();
104 Names = Queue[i].getNames();
105 URLs = Queue[i].getURLs();
106
107 for( j = size-1; j >= 0; j-- )
108 {
109 submenu.addItem(Names[j],index+base);
110 LinkTarget[index++] = URLs[j];
111 }
112
113 mi = links.addItem(Categories[i]);
114 mi.addSubMenu(submenu);
115 }
116 }
117
118 return links;
119 }
120
121 public void dispatchLink(int target, int base)
122 {
123 System.err.println("Dispatch to "+LinkTarget[target-base]);
124
125 try
126 {
127 danger.net.URL.gotoURL(LinkTarget[target-base]);
128 }
129 catch( Exception e )
130 {
131 }
132 }
133
134 private int findCategory(String Category)
135 {
136 int i;
137
138 for( i=0; i < numMenus; i++ )
139 {
140 if( 0 == Category.compareTo(Categories[i]) )
141 return i;
142 }
143
144 if( numMenus == maxLinkMenus )
145 {
146 for( i=0; i < numMenus-1; i++ )
147 {
148 Categories[i] = Categories[i+1];
149 Queue[i] = Queue[i+1];
150 }
151 }
152 else
153 numMenus ++;
154
155 Categories[numMenus - 1] = Category;
156 Queue[numMenus - 1] = new LinkQueue();
157
158 return numMenus - 1;
159 }
160}
Note: See TracBrowser for help on using the repository browser.