/*
* Copyright (c) 1995, 1996 Sun Microsystems, Inc. All Rights Reserved.
*
* Permission to use, copy, modify, and distribute this software
* and its documentation for NON-COMMERCIAL purposes and without
* fee is hereby granted provided that this copyright notice
* appears in all copies. Please refer to the file "copyright.html"
* for further important copyright and licensing information.
*
* SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
* THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
* TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
* ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
* DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
*/
import java.applet.*;
import java.awt.*;
import java.util.Enumeration;

public class Sender extends Applet {
private String myName;
private TextField nameField;
private TextArea mesg;
private TextArea status;
public String s;

public void init() {

setBackground(new java.awt.Color(0,128,128));
setForeground(java.awt.Color.white);

GridBagLayout gridBagS = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();

setLayout(gridBagS);

Label receiverLabel = new Label("宛先", Label.RIGHT);
gridBagS.setConstraints(receiverLabel, c);
add(receiverLabel);

nameField = new TextField(getParameter("RECEIVERNAME"), 10);
c.fill = GridBagConstraints.HORIZONTAL;
gridBagS.setConstraints(nameField, c);
add(nameField);

Button button = new Button("送信");
c.gridwidth = GridBagConstraints.REMAINDER; //end row
c.anchor = GridBagConstraints.WEST; //stick to the text field
c.fill = GridBagConstraints.NONE; //keep the button small
gridBagS.setConstraints(button, c);
add(button);

mesg = new TextArea("",1, 40, 1);
mesg.setEditable(true);
c.anchor = GridBagConstraints.CENTER; //reset to the default
c.fill = GridBagConstraints.BOTH; //make this big
// c.weightx = 1.0;
c.weightx = 0;
c.weighty = 0;
gridBagS.setConstraints(mesg, c);
add(mesg);

status = new TextArea("", 2, 40, 1);
status.setEditable(false);
c.anchor = GridBagConstraints.CENTER; //reset to the default
c.fill = GridBagConstraints.BOTH; //make this big
c.weightx = 1.0;
c.weighty = 1.0;
gridBagS.setConstraints(status, c);
add(status);

myName = getParameter("NAME");
Label senderLabel = new Label("(私は " + myName + ")", Label.CENTER);
c.weightx = 0.0;
c.weighty = 0.0;
gridBagS.setConstraints(senderLabel, c);
add(senderLabel);

validate();
}

public boolean action(Event event, Object o) {
Applet receiver = null;
String receiverName = nameField.getText(); //Get name to search for.
receiver = getAppletContext().getApplet(receiverName);
if (receiver != null) {
//Use the instanceof operator to make sure the applet
//we found is a Receiver object.
if (!(receiver instanceof Receiver)) {
status.appendText(receiverName + ", "
+ " という宛先がみつかりましたが Receiver オブジェクトではありません。\n");
} else {
status.appendText(receiverName + " を見つけました。\n"
+ " メッセージを送っています。\n");
//Cast the receiver to be a Receiver object
//(instead of just an Applet object) so that the
//compiler will let us call a Receiver method.
s = mesg.getText();
((Receiver)receiver).processRequestFrom(myName, s);
}
} else {
status.appendText(receiverName + " という宛先は見つかりませんでした。\n");
}
return false;

}

public Insets insets() {
return new Insets(3,3,3,3);
}

public void paint(Graphics g) {
g.drawRect(0, 0, size().width - 1, size().height - 1);
}

public String getAppletInfo() {
return "Sender by Kathy Walrath";
}
//{{DECLARE_CONTROLS
//}}
// static java.util.ResourceBundle senderBundle = java.util.ResourceBundle.getBundle("SenderBundle");
}

/*
* Copyright (c) 1995, 1996 Sun Microsystems, Inc. All Rights Reserved.
*
* Permission to use, copy, modify, and distribute this software
* and its documentation for NON-COMMERCIAL purposes and without
* fee is hereby granted provided that this copyright notice
* appears in all copies. Please refer to the file "copyright.html"
* for further important copyright and licensing information.
*
* SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
* THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
* TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
* ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
* DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
*/
import java.applet.*;
import java.awt.*;

public class Receiver extends Applet {
private final String waitingMessage = "";
private Label label = new Label(waitingMessage, Label.LEFT);
private TextArea ta1 = new TextArea("", 2,40, 1);

public void init() {
setBackground(new java.awt.Color(0,128,128));
setForeground(java.awt.Color.white);

// add(new Button("消去"));
add(new Label("(私は " + getParameter("name") + ")", Label.LEFT));
add(label);
add(ta1);

validate();
}

public boolean action(Event event, Object o) {
label.setText(waitingMessage);
repaint();
return false;
}

public void processRequestFrom(String senderName, String s) {
// ta1.setText(senderName + " からメッセージが届きました!");
// ta1.setText(s);
ta1.setText(senderName + " からメッセージが届きました!" + "\n" + (s));
repaint();
}

public void paint(Graphics g) {
g.drawRect(0, 0, size().width - 1, size().height - 1);
}

public String getAppletInfo() {
return "Receiver (named " + getParameter("name") + ") by Kathy Walrath";
}
//{{DECLARE_CONTROLS
//}}
// static java.util.ResourceBundle receiverBundle = java.util.ResourceBundle.getBundle("ReceiverBundle");
}


/*
* Copyright (c) 1995, 1996 Sun Microsystems, Inc. All Rights Reserved.
*
* Permission to use, copy, modify, and distribute this software
* and its documentation for NON-COMMERCIAL purposes and without
* fee is hereby granted provided that this copyright notice
* appears in all copies. Please refer to the file "copyright.html"
* for further important copyright and licensing information.
*
* SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
* THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
* TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
* ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
* DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
*/
import java.applet.*;
import java.awt.*;
import java.util.Enumeration;

public class GetApplets extends Applet {
private TextArea textArea;

public void init() {
setBackground(java.awt.Color.yellow);
setForeground(java.awt.Color.black);

setLayout(new BorderLayout());

add("North", new Button("このページ内のアプレットを検索"));

textArea = new TextArea(5, 40);
textArea.setEditable(false);
add("Center", textArea);

validate();
}

public boolean action(Event event, Object o) {
printApplets();
return false;
}

public String getAppletInfo() {
return "GetApplets by Kathy Walrath";
}

public void printApplets() {
//Enumeration will contain all applets on this page (including
//this one) that we can send messages to.
Enumeration e = getAppletContext().getApplets();

textArea.appendText("アプレット検索結果\n");

while (e.hasMoreElements()) {
Applet applet = (Applet)e.nextElement();
String info = ((Applet)applet).getAppletInfo();
if (info != null) {
textArea.appendText("- " + info + "\n");
} else {
textArea.appendText("- " + applet.getClass().getName() + "\n");
}
}
textArea.appendText("________________________ 以上\n\n");
textArea.appendText("* Copyright (c) 1995, 1996 Sun Microsystems, Inc. All Rights Reserved.\n");
textArea.appendText("* Permission to use, copy, modify, and distribute this software\n");
textArea.appendText("* and its documentation for NON-COMMERCIAL purposes and without\n");
textArea.appendText("* fee is hereby granted provided that this copyright notice\n");
textArea.appendText("* appears in all copies. Please refer to the file copyright.html\n");
textArea.appendText("* for further important copyright and licensing information.\n");
textArea.appendText("* SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF\n");
textArea.appendText("* THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED\n");
textArea.appendText("* TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A\n");
textArea.appendText("* PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR\n");
textArea.appendText("* ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR\n");
textArea.appendText("* DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.\n\n");

}
//{{DECLARE_CONTROLS
//}}
// static java.util.ResourceBundle getAppletsBundle = java.util.ResourceBundle.getBundle("GetAppletsBundle");
}