summaryrefslogtreecommitdiff
path: root/java/apps/NexusII/src/RoomFrame.java
blob: e9da19b31d60062b6c3be84e5b9174cb302b3ce4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*
   $RCSfile$
   $Author$

   Last Update: $Date$
   $Revision$

   REVISION HISTORY:
   $Log$
   Revision 1.1  1997/01/31 01:11:02  sumedh
   Added the Nexus II source code files.

# Revision 1.2  1996/12/07  06:25:18  rajeev
# backup
#
# Revision 1.1  1996/12/07  06:15:12  rajeev
# Initial revision
#


*/
import java.awt.*;
import awtCommand.*;
import java.util.* ;

//import NexusII.client.* ;
//import NexusII.util.* ;
//import NexusII.networking.*;

class RoomFrame extends CFrame implements consts {
  private static String rcsId = new String("$Id$");

  // Graphics Objects
  private CTextField tfInput ; 
  private CTextArea taOutput ;
  private CButton bLeave ;
  private ImageCanvas icOutput;
  private Font normalFont = new Font("Helvetica", Font.PLAIN, 14);
  private Font boldFont = new Font("Helvetica", Font.BOLD, 14);
  private Font italicFont = new Font("Helvetica", Font.ITALIC, 14);
  private static final int LINE_LENGTH = 70;

  // Other required objects
  private MT_Bounded_Queue write_q_ ; 
  private NexusClientApplet applet_ ; 
  private String myName_ ; 

  public RoomFrame(MT_Bounded_Queue write_q,
		   NexusClientApplet applet,
		   String name) {
    super(name);
    write_q_ = write_q ; 
    applet_  = applet ; 
    myName_  = name ; 
    SetUpGraphics();
    this.pack();
    this.show();
  }

  void SetUpGraphics() {
    // Initialize the graphics objects
    // The input text line
    tfInput = new CTextField(LINE_LENGTH);
    textHandler handlerT = new textHandler(write_q_,applet_,myName_);
    tfInput.setActionCommand(handlerT);
    // The leave button
    bLeave = new CButton(LEAVE_STR);
    bLeave.setActionCommand(applet_.nexusJoiner);
	
    icOutput = new ImageCanvas(myName_);
    taOutput = new CTextArea(10,LINE_LENGTH);

    GridBagLayout gbl = new GridBagLayout();
    GridBagConstraints gbc = new GridBagConstraints();
    setFont(normalFont);
    setLayout (gbl);

    gbc.insets = new Insets(5,5,5,5);
    gbc.ipadx  = 5;
    gbc.ipady  = 5;
    gbc.weightx = 1.0;
    gbc.weighty = 1.0;

    // First the Image so that sizes are fixed
    gbc.gridx = 1;
    gbc.gridy = 0;
    gbc.anchor = GridBagConstraints.CENTER;
    gbc.fill = GridBagConstraints.BOTH;
    gbl.setConstraints(icOutput, gbc);
    add(icOutput);

    // The Text Output Area
    gbc.gridx = 0;
    gbc.gridy = 0;
    gbc.weighty = 0.0;
    gbc.anchor = GridBagConstraints.SOUTH;
    gbc.fill = GridBagConstraints.BOTH;
    gbl.setConstraints(taOutput,gbc);
    taOutput.setEditable(false);
    add(taOutput);

    // The Text Input Field
    gbc.gridx = 0;
    gbc.gridy = 1;
    gbc.weighty = 0.0;
    gbc.anchor = GridBagConstraints.NORTH;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbl.setConstraints(tfInput,gbc);
    add(tfInput);

    // The Leave Button
    gbc.gridx = 1;
    gbc.gridy = 1;
    gbc.anchor = GridBagConstraints.NORTH;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbl.setConstraints(bLeave,gbc);
    add(bLeave);

    resize(800,280);
    setResizable(true);	// Allow the user to resize
    validate();
  }

  public boolean handleEvent(Event event) {
    if (event.id == Event.WINDOW_DESTROY)
      dispose();
    return super.handleEvent(event);
  }

  // This function will be called by the RoomHandler when it receives
  // anything for this room and will add the data 
  public  void addText(String tobeadded) {
    if (false) {
      String speaker = null;
      String msg = null;
      int pos = tobeadded.indexOf(':');
      if (pos >= 0)
        speaker = tobeadded.substring(0,pos+1);
      else
        speaker = new String("");
      msg = tobeadded.substring(pos+1);
      Font oldf = taOutput.getFont();
      taOutput.setFont(boldFont);
      taOutput.appendText(speaker);
      taOutput.setFont(italicFont);
      taOutput.appendText(msg+"\n");
      taOutput.setFont(oldf);
    }
    else {
      taOutput.appendText(tobeadded+"\n");
    }
    return;
  }

  // This function will also be called by the RoomHandler whenit receives
  // Image data for this room. 
  public void 	updateImage(Image im) { 
    if(DEBUG) { 
      System.out.println("Room : I got an image");
    }
    icOutput.setImage(im);
  } 
  
  
}



/**
 * The ImageCanvas Class -- for the image in the class
 */
class ImageCanvas extends Canvas implements consts {

  Image image_ = null;
  int defWidth_ = 128;
  int defHeight_ = 128;
  int margin = 5;

  String name_ = new String("Nexus Room");
  Font nameFont = new Font("Helvetica",0,14);

  public ImageCanvas(String s) 
  {
    name_ = s;
  }

  public Dimension preferredSize() {
    return minimumSize();
  }
  public Dimension minimumSize() {
    return new Dimension(defWidth_, defHeight_);
  }

  public void setImage(Image newIm) {
    image_ = newIm;
    repaint();
  }
  public Image getImage() {
    return image_;
  }

  public void name(String n) {
    name_ = n;
  }
  public String name() {
    return name_;
  }

  public void paint(Graphics g) {
    Dimension d = size();
    int width = d.width;
    int height = d.height;

    if (DEBUG)
      System.out.println("ImageCanvas:: width = "+width+ " height="+height);

    g.setColor(Color.black);
    g.fillRect(0,0,width-1,height-1);
    g.setColor(Color.white);
    g.fillRect(margin,margin,width-2*margin,height-2*margin);
    // Create image if reqd
    if (image_ == null)
      image_ = createImage(width-2*margin,height-2*margin);

    g.drawImage(image_, margin, margin,
		width-2*margin, height-2*margin, Color.white, this);
    g.setColor(Color.blue);
    g.setFont(nameFont);
    FontMetrics fm = g.getFontMetrics();
    g.drawString(name_,(width - fm.stringWidth(name_))/2,
		 (int)(height*0.9 - fm.getMaxDescent()));
    validate();
    return;
  }

  public void update(Graphics g) {
    paint(g);
  }

} // End of the Image Canvas Class



// this is the event handler for the textfield -- whenever anything is typed 
class textHandler implements Command,consts,Runnable { 
  private MT_Bounded_Queue write_q_ ; 
  private NexusClientApplet applet_ ; 
  private String name_ ; 
  Object what_ ; 

  public textHandler(MT_Bounded_Queue write_q,
		     NexusClientApplet applet,
		     String name) { 
    write_q_ = write_q ;
    applet_   = applet ; 
    name_ = name ; 
  }

  public void execute(Object target, Event evt, Object what) { 
    // get the string and send it across in a different thread 
    what_ = what ; 
    // clear the field in the gui 
    ((TextField)target).setText("");
    // send it off 
    new Thread(this).start(); 
  }

  // send off the string in a different thread 
  public void run() {
    String data = (String)what_; 
    // check if data begins with / and is followed by url 
    if(!data.startsWith("/url")) { 
      
      String user = NexusClientApplet.myName ;
      String command = user + ":" + data ; 
      String len = Integer.toString(command.length());
      dataPacket packet  = new dataPacket(NexusClientApplet.myName,name_,
					  "TEXT",len,command);
      write_q_.nq(packet);
    }
    else { 
      // it is /url 
      StringTokenizer t  = new StringTokenizer(data); 
      // take out the /url from here 
      String ur = t.nextToken(); 
      // data now 
      String command = t.nextToken(); 
      String len = Integer.toString(command.length());
      dataPacket packet  = new dataPacket(NexusClientApplet.myName,name_,
					  "URL",len,command);
      write_q_.nq(packet);
    }
  }
} // end of class