/*
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.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
import java.util.ArrayList;
import java.util.List;
import edu.wisc.trace.urcsdk.base.Constants;
import edu.wisc.trace.urcsdk.base.UrcException;
import edu.wisc.trace.urcsdk.client.GenericClient;
import edu.wisc.trace.urcsdk.client.IInteractor;
import edu.wisc.trace.urcsdk.client.Widget;
import edu.wisc.trace.urcsdk.client.pret.Group;
import edu.wisc.trace.urcsdk.client.resources.EmptyResource;
import edu.wisc.trace.urcsdk.client.resources.IResource;
import edu.wisc.trace.urcsdk.client.resources.ResourceManager;
import edu.wisc.trace.urcsdk.client.uisocket.ElementRef;
import edu.wisc.trace.urcsdk.support.ErrorHandler;
import edu.wisc.trace.urcsdk.support.UserPreferences;
/**
*
* TextGroup is a pseudo interactor. It acts more like a
* container of interactors. This interactor doesn't deal with a write or read
* attribute but is Writeable/readable if the interactors in the group are
* Writeable/readable.
*
*
*
* Created on: April 19, 2006
* Known bugs: None
* Thread safe: Yes
*
*
* @author Hemanth Vijayan, Trace R&D Center
* @version $Revision: 1.21 $
*
*/
public class TextGroup extends TextWidget {
GenericClient gc = null;
SessionPanel sp;
List widgets = new ArrayList();
List widgetscopy = new ArrayList();
Group grp;
/**
* adds widget to the list
*
* @param w
*/
private void addWidget(Widget w) {
if (w != null) {
widgets.add(w);
}
}
/**
* This method adds the session panel to the textrange object. The reason
* for this is so that it will be able to access whether or not a modal
* dialog has been displayed to the user.
*
* @param sp
* SessionPanel object
*/
public void setSessionPanel(SessionPanel sp) {
this.sp = sp;
for (Widget w : widgets) {
((TextWidget) w).setSessionPanel(this.sp);
}
}
/**
* Creates the group widget.
*
* @param b
* @param i
* @param gc
* @throws UrcException
*/
public TextGroup(TextUIBuilder b, IInteractor i, GenericClient gc)
throws UrcException {
super(b, i);
this.gc = gc;
grp = (Group) i;
if (!(i instanceof Group)) {
throw ErrorHandler.newException(this,
"Interactor used for TextGroup widget was not a Group.");
}
// For the interactors grouped up, create the widget and add to list
for (IInteractor ints : ((Group) i).getInteractors()) {
Widget w = b.createWidget(ints);
addWidget(w);
}
}
/**
* getUIComponent returns the group label. It is called while creating the
* session panel.
*/
public String getUIComponent() {
return getLabel();
}
/**
* Updates the resouces for the widgets in the group.
*/
public void updateResources() {
for (Widget w : widgets) {
w.updateResources();
}
}
/**
* gets the list of widgets in the group.
*
* @return List
*/
public List getWidgets() {
return widgets;
}
/*
* (non-Javadoc)
*
* @see edu.wisc.trace.urcsdk.client.Widget#updateValue()
*/
public void updateValue() {
if (sp != null)
sp.paintSessionPanel();
}
/**
* This method is defined in the Widget Class. The group interactor doesn't
* have a read attribute, so this method doesn't do anything.
*
* @param read
*/
public void setReadable(boolean read) {
// no read
}
/*
* (non-Javadoc)
*
* @see edu.wisc.trace.urcsamples.textclient.TextWidget#getReadable()
*/
public boolean getReadable() {
for (Widget w : widgets) {
if (w instanceof TextWidget) {
if (((TextWidget) w).getReadable()) {
return true;
}
}
}
return false;
}
/**
* This method is defined in the Widget Class. The group interactor doesn't
* have a write attribute, so this method doesn't do anything.
*
* @param write
*/
public void setWriteable(boolean write) {
// no write
}
/**
* This method is defined in the Widget Class. The group interactor doesn't
* have an execute attribute, so this method doesn't do anything.
*
* @param execute
*/
public void setExecutable(boolean execute) {
// no execute
}
/**
* This method is called when the user selects the group iteractor. The
* available widgets are displayed.
*/
public void doChange() {
if (getWriteable()) {
int count = 0;
widgetscopy.clear();
System.out.println("b: Previous screen");
for (int i = 0; i < widgets.size(); i++) {
if (((TextWidget) widgets.get(i)).getWriteable()) {
widgetscopy.add(widgets.get(i));
System.out.println(++count + ": "
+ widgets.get(i).getUIComponent());
}
}
System.out.print("Enter Interactor choice (corresponding #): ");
getWidgetWindow();
}
}
/**
* This method waits for the users choice of the group interactors. It then
* calls the widgets action method when selected.
*
*/
private void getWidgetWindow() {
java.io.InputStreamReader isr = new java.io.InputStreamReader(System.in);
java.io.StreamTokenizer st = new java.io.StreamTokenizer(isr);
st.wordChars('\n', '\n');
st.eolIsSignificant(true);
int selection = 0;
boolean read = true;
boolean exit = false;
try {
while (read && !exit) {
switch (st.nextToken()) {
case java.io.StreamTokenizer.TT_NUMBER:
if (this.sp.getDialog() == 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;
TextWidget w = (TextWidget) widgetscopy
.get(selection - 1);
w.doChange();
// doChange();
}
} else {
this.sp.getDialog().getWidgetWindow(
((Integer) (int) st.nval).toString());
read = false;
// doChange();
}
break;
case java.io.StreamTokenizer.TT_WORD:
if (this.sp.getDialog() == null) {
if (st.sval.toLowerCase().equals("b")) {
exit = true;
} else if (st.sval.equals("?")
|| st.sval.toLowerCase().equals("h")
|| st.sval.toLowerCase().equals("help")) {
String helpString = "No help found";
if (!HelpParser.group.equals(null)) {
helpString = HelpParser.group;
}
System.out.println(helpString);
System.out
.print("Enter Interactor choice (corresponding #): ");
} else {
System.out.println("Invalid Entry, try again");
System.out
.print("Enter Interactor choice (corresponding #): ");
}
} else {
this.sp.getDialog().getWidgetWindow(st.sval.toString());
read = false;
// doChange();
}
break;
case StreamTokenizer.TT_EOL:
if (this.sp.getDialog() == null) {
// do nothing
} else {
this.sp.getDialog().getWidgetWindow("");
read = false;
// doChange();
}
break;
default:
break;
}
}
if (!exit)
doChange();
} catch (IOException ioe) {
System.out.println(ioe.getMessage());
}
}
/**
* If any of the widgets in the group is Writeable or executable(in the case
* of triggers), then the group is Writeable.
*
* @return Writeable
*/
public boolean getWriteable() {
for (Widget w : widgets) {
if (((TextWidget) w).getWriteable()) {
return true;
}
}
return false;
}
/**
* This method gets the label for the interactor based on the clients
* preferences (language, format etc)
*
* @return String
*/
public String getLabel() {
String label = getText(Constants.ResourceRole.Label.toString());
if (label == null) {
return grp.getID();
} else if (label.equals(""))
return grp.getID();
else
return label;
}
/**
* This method gets the HelpPurpose defined in a resource sheet for the
* group interactor and returns the text in the following format.
* Help-purpose : (string text) If it is empty, it isn't included.
*
* @return help text.
*/
public String getHelpText() {
String tmp, text = "";
if (!(tmp = getText(Constants.ResourceRole.HelpPurpose.toString()))
.equals("")) {
text = "Help-Purpose: " + tmp;
}
return text;
}
/**
* Prints the help text for the interactor.
*
*/
public void getHelp() {
int count = 0;
widgetscopy.clear();
System.out
.println('\n' + "Select the following choices for help about these interactors.");
System.out.println("b: Previous screen");
for (int i = 0; i < widgets.size(); i++) {
widgetscopy.add(widgets.get(i));
System.out
.println(++count + ": " + widgets.get(i).getUIComponent());
}
System.out
.print("Enter the interactor choice you want help with (corresponding #): ");
InputStreamReader isr = new InputStreamReader(System.in);
StreamTokenizer st = new StreamTokenizer(isr);
st.wordChars('?', '?');
int selection = 0;
boolean read = true;
boolean exit = false;
try {
while (read && !exit) {
switch (st.nextToken()) {
case StreamTokenizer.TT_NUMBER:
if (this.sp.getDialog() == null) {
selection = (int) st.nval;
if (selection > widgetscopy.size() || selection == 0) {
System.out.println("Invalid Entry, try again");
System.out
.print("Enter the interactor choice you want help with (corresponding #): ");
} else {
read = false;
TextWidget w = (TextWidget) widgetscopy
.get(selection - 1);
String text;
if (!(text = w.getHelpText()).equals("")) {
System.out.println(text);
} else {
System.out
.println("No help associated with the chosen interactor");
}
getHelp();
}
} else {
this.sp.getDialog().getWidgetWindow(
((Integer) (int) st.nval).toString());
read = false;
getHelp();
}
break;
case StreamTokenizer.TT_WORD:
if (this.sp.getDialog() == null) {
if (st.sval.toLowerCase().equals("b")) {
exit = true;
} else if (st.sval.equals("?")
|| st.sval.toLowerCase().equals("h")
|| st.sval.toLowerCase().equals("help")) {
String helpString = "No help found";
if (!HelpParser.group.equals(null)) {
helpString = HelpParser.group;
}
System.out.println(helpString);
System.out
.print("Enter the interactor choice you want help with (corresponding #): ");
} else {
System.out.println("Invalid Entry, try again");
System.out
.print("Enter the interactor choice you want help with (corresponding #): ");
}
} else {
this.sp.getDialog().getWidgetWindow(st.sval.toString());
read = false;
getHelp();
}
break;
default:
break;
}
}
if (exit) {
this.sp.helpScreen();
}
} catch (IOException ioe) {
System.out.println(ioe.getMessage());
}
}
/**
* This method is the base that the getHelpText() and getLabel() call. It
* has a role parameter which passes the desired role to the resource
* manager.
*
* @param role
* @return label/help
*/
private String getText(String role) {
UserPreferences uPrefs = builder.getUrc().getPreferences();
ResourceManager rm = grp.getSession().getSocket().getTarget()
.getResourceManager();
ElementRef elementRef = grp.getSelfRef();
IResource label = rm.getResource(elementRef, null, true, null, role,
uPrefs);
if (label == null)
return "";
else if (label instanceof EmptyResource)
return "";
else
return label.getValue().toString().trim();
}
}