/*
Copyright (C) 2005 Board of Regents of the University of Wisconsin
System (Univ. of Wisconsin-Madison, Trace R&D Center)
This piece of the software package, developed by the Trace Center -
University of Wisconsin is released to the public domain with only
the following restrictions:
1) That the following acknowledgement be included in the source code and
documentation for the program or package that use this code:
Parts of this program were based on reference designs developed by the
Trace Center, University of Wisconsin-Madison under funding from the
National Institute on Disability and Rehabilitation Research US Dept
of Education.
2) That this program not be modified unless it is plainly marked as modified
from the original distributed by Trace.
(NOTE: This release applies only to the files that contain this notice, not
necessarily to any other code or libraries associated with this file. Please
check individual files and libraries for the rights to use each)
This reference design was developed under funding from the National Institute
on Disability and Rehabilitation Research US Dept of Education.
THIS PIECE OF THE SOFTWARE PACKAGE IS EXPERIMENTAL/DEMONSTRATION IN NATURE.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS INCLUDED IN THIS NOTICE BE
LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY
DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
package edu.wisc.trace.urcsamples.textclient;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import edu.wisc.trace.urcsdk.client.ClientSession;
import edu.wisc.trace.urcsdk.client.Widget;
import edu.wisc.trace.urcsdk.client.uisocket.UISocketMirror;
import edu.wisc.trace.urcsdk.client.GenericClient;
/**
*
* SessionPanel is responsible for the correct placement and
* maintenance of all TextWidgets created during the UIBuilding
* phase.
*
*
*
* Created on: April 19, 2004
* Known bugs: None
* Thread safe: Yes
*
*
* @author Hemanth Vijayan, Trace R&D Center
* @version $Revision: 1.21 $
*
*/
public class SessionPanel {
private static final long serialVersionUID = 7371864868891520898L;
private ClientSession session;
private String currentScreen = "";
private UISocketMirror socket;
private List widgets = new ArrayList();
private List widgetscopy = new ArrayList();
private GenericClient gc = null;
private TextModalDialog modalDialog = null;
/**
*
* Constructor
*
* @param socket
* @param session
* @param gc
* client object
*
*/
public SessionPanel(UISocketMirror socket, ClientSession session,
GenericClient gc) {
this.gc = gc;
this.session = session;
this.socket = socket;
}
/**
* Sets the modalDialog to tmd
* @param tmd
*/
public void setDialog(TextModalDialog tmd) {
modalDialog = tmd;
}
/**
*
* @return the modalDialog object if any
*/
public TextModalDialog getDialog() {
return modalDialog;
}
/**
* Adds the widget to the list.
*
* @param widget
*/
public void add(Widget widget) {
if (!widgets.contains(widget)) {
widgets.add(widget);
}
}
/**
* Returns the stored ClientSession object that was passed through the
* constructor.
*
* @return stored Clientsession object
*/
public ClientSession getSession() {
return session;
}
/**
* removes the stored session
*/
public void removeSession() {
this.session = null;
}
/**
* Returns the stored UISocketMirror object that was passed through the
* constructor.
*
* @return socket
*/
public UISocketMirror getSocket() {
if (socket == null)
throw new NullPointerException(
"SessionPanel has a null socket, this shouldn't ever happen!");
return socket;
}
/**
* Updates the widgets resources.
*
*/
public void updateResources() {
for (Widget w : widgets) {
w.updateResources();
}
}
/**
* Returns the list of Widgets present in the socket.
*
* @return List
*/
public List getWidgets() {
return widgets;
}
/**
* This method is called to create the Session Panel. It goes through the
* list of widgets present.If the widget is Writeable or executable(the case
* for triggers), then it will display the widget.
*
*/
public void paintSessionPanel() {
if (!currentScreen.equals("socket") && this.modalDialog == null) {
if (this.session.equals(((TextClient) gc).session)) {
if ((currentScreen.equals("") || currentScreen
.equals("sessionPanel"))
&& (((TextClient) gc).currentScreen.equals("session"))) {
int count = 0;
boolean write = false;
boolean read = false;
System.out.println();
System.out.println("b: Socket List Window");
widgetscopy.clear(); // a copy is created so that the
// index can
// easily matched to the widget.
// eg. if widgets 1, 3, 10 are the only ones visible. Then
// widgetscopy(1) will
// match widgets.get(3). Otherwise a map needs to be used.
for (int i = 0; i < widgets.size(); i++) {
if (((TextWidget) widgets.get(i)).getReadable()) {
read = true;
}
if (((TextWidget) widgets.get(i)).getWriteable()) {
write = true;
}
if (write || read) {
widgetscopy.add(widgets.get(i));
if (write)
System.out.println(++count + ": "
+ widgets.get(i).getUIComponent());
else
System.out.println(++count + ": "
+ widgets.get(i).getUIComponent()
+ " --Read Only");
read = false;
write = false;
}
}
System.out
.print("Enter Interactor choice (corresponding #): ");
}
if (!currentScreen.equals("sessionPanel")
&& (((TextClient) gc).currentScreen.equals("session"))
&& this.modalDialog == null) {
currentScreen = "sessionPanel";
getWidgetWindow();
}
}
}
}
/**
* Internal method that is called when the interactor choice needs to be
* selected using the InputStreamReader. The character set b is chosen to go
* back to the socket list window.
*
*/
private void getWidgetWindow() {
InputStreamReader isr = new InputStreamReader(System.in);
StreamTokenizer st = new StreamTokenizer(isr);
st.wordChars('?', '?');
st.wordChars('\n', '\n');
st.eolIsSignificant(true);
int selection = 0;
boolean read = true; // read is set to false when it should redraw
// the session panel.
boolean exit = false; // set to true so that the socket list needs be
// drawn.
boolean help = false;
try {
while (read && !exit) {
switch (st.nextToken()) {
case java.io.StreamTokenizer.TT_NUMBER:
if (this.modalDialog == null) {
if (session != null) {
selection = (int) st.nval;
if (selection > widgetscopy.size()
|| selection == 0) {
System.out.println("Invalid Entry, try again");
System.out
.print("Enter Interactor choice (corresponding #): ");
} else {
read = false;
currentScreen = "socket";
TextWidget w = (TextWidget) widgetscopy
.get(selection - 1);
w.doChange(); // The method that performs the
// widgets
// function.
// currentScreen = "sessionPanel";
}
} else {
exit = true;
}
} else {
this.modalDialog
.getWidgetWindow(((Integer) (int) st.nval)
.toString());
//read = false;
}
break;
case java.io.StreamTokenizer.TT_WORD:
if (this.modalDialog == null) {
if (session != null) {
if (st.sval.toLowerCase().equals("b")) {
exit = true;
} else if (st.sval.equals("?")
|| st.sval.toLowerCase().equals("h")
|| st.sval.toLowerCase().equals("help")) {
read = false;
help = true;
helpScreen();
} else {
System.out.println("Invalid Entry, try again");
System.out
.print("Enter Interactor choice (corresponding #): ");
}
} else {
exit = true;
}
} else {
this.modalDialog.getWidgetWindow(st.sval);
//read = false;
}
break;
case StreamTokenizer.TT_EOL:
if (this.getDialog() == null) {
//do nothing
} else {
this.getDialog().getWidgetWindow("");
read = false;
//doChange();
}
break;
default:
if (session != null) {
System.out.println("Invalid Entry, try again");
System.out
.print("Enter Interactor choice (corresponding #): ");
} else {
exit = true;
}
break;
}
}
} catch (IOException ioe) {
System.out.println(ioe.getMessage());
}
if (!read) {
if (!help) {
currentScreen = "";
paintSessionPanel();
}
} else if (exit) {
currentScreen = "";
((TextClient) gc).currentScreen = "socket";
((TextClient) gc).getSocketListWindow();
}
}
/**
* @return Returns a string with the socket label based on the clients
* preference.
*/
public String toString() {
return "SessionPanel(" + socket.getLabel(gc.getPreferences()) + ")";
}
/**
* Prints out the target help menu screen which helps to navigate through
* the targets interactors and find help on how to use them.
*
*/
public void helpScreen() {
System.out.println("------------------");
System.out.println(" Target Help Menu");
if (this.modalDialog == null) {
int count = 0;
boolean write = false;
boolean read = false;
System.out.println();
System.out.println("b: Socket List Window");
widgetscopy.clear();
for (int i = 0; i < widgets.size(); i++) {
if (((TextWidget) widgets.get(i)).getReadable()) {
read = true;
}
if (((TextWidget) widgets.get(i)).getWriteable()) {
write = true;
}
if (write || read) {
widgetscopy.add(widgets.get(i));
System.out.println(++count + ": "
+ widgets.get(i).getUIComponent());
read = false;
write = false;
}
}
System.out
.print("Enter interactor choice you would like help with: ");
}
InputStreamReader isr = new InputStreamReader(System.in);
StreamTokenizer st = new StreamTokenizer(isr);
int selection = 0;
boolean read = true; // read is set to false when it should redraw
// the session panel.
boolean exit = false; // set to true so that the socket list needs be
// drawn.
try {
while (read && !exit) {
switch (st.nextToken()) {
case java.io.StreamTokenizer.TT_NUMBER:
if (this.modalDialog == null) {
if (session != null) {
selection = (int) st.nval;
if (selection > widgetscopy.size()
|| selection == 0) {
System.out.println("Invalid Entry, try again");
System.out
.print("Enter interactor choice you would like help with: ");
} else {
read = false;
TextWidget w = (TextWidget) widgetscopy
.get(selection - 1);
getHelp(w);
}
} else {
exit = true;
}
} else {
this.modalDialog
.getWidgetWindow(((Integer) (int) st.nval)
.toString());
}
break;
case java.io.StreamTokenizer.TT_WORD:
if (this.modalDialog == null) {
if (session != null) {
if (st.sval.toLowerCase().equals("b")) {
exit = true;
} else {
System.out.println("Invalid Entry, try again");
System.out
.print("Enter interactor choice you would like help with: ");
}
} else {
exit = true;
}
} else {
this.modalDialog.getWidgetWindow(st.sval);
}
break;
default:
if (session != null) {
System.out.println("Invalid Entry, try again");
System.out
.print("Enter interactor choice you would like help with: ");
} else {
exit = true;
}
break;
}
}
} catch (IOException ioe) {
System.out.println(ioe.getMessage());
}
if (exit) {
currentScreen = "";
paintSessionPanel();
}
}
private void getHelp(TextWidget w) {
String text;
if (w instanceof TextGroup) {
if (!(text = w.getHelpText()).equals("")) {
System.out.println("Help for the group interactor");
System.out.println(w.getHelpText());
}
((TextGroup) w).getHelp();
} else if (!(text = w.getHelpText()).equals("")) {
System.out.println(text);
helpScreen();
} else {
System.out.println("No help associated with the chosen interactor");
helpScreen();
}
}
}