/* 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 edu.wisc.trace.urcsdk.base.Constants; import edu.wisc.trace.urcsdk.support.UserPreferences; import edu.wisc.trace.urcsdk.client.IInteractor; import edu.wisc.trace.urcsdk.client.resources.IResource; import edu.wisc.trace.urcsdk.client.resources.EmptyResource; import edu.wisc.trace.urcsdk.client.resources.ResourceManager; import edu.wisc.trace.urcsdk.client.uisocket.ElementRef; import edu.wisc.trace.urcsdk.client.uisocket.UISocketMirror; /** *

* TextBoolean is the text implementation of the Boolean * interactor. The boolean interactor like a checkbox, power button is * represented as it's label(value) and when selected the value is flipped. *

*

* All interactors extend the TextWidget class. *

*

* Created on:April 19, 2006
* Known bugs: None
* Thread safe: Yes
*

* * @author Hemanth Vijayan, Trace R&D Center * @version $Revision 1.0 $ * */ public class TextBoolean extends TextWidget { String label = null; private SessionPanel sp; boolean updated = false; boolean read; boolean write = false; // widget is Writeable based on the value of write. /** * The label for the text boolean is set. * * @param b * @param i */ public TextBoolean(TextUIBuilder b, IInteractor i) { super(b, i); this.label = getLabel(); } /** * This method adds the session panel to the textBoolean 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; } private void print() { if (Boolean.parseBoolean(interactor.getSocketElement().getValue() .toString())) { System.out.print("Enter 0 to turn " + label + " off or b to go back: "); } else { System.out.print("Enter 1 to turn " + label + " on or b to go back: "); } } /** * This method is called to flip the value of the boolean being represented * doChange() is the overridden method defined in the * TextWidget to perform the necessary functions for an interactor. */ public void doChange() { print(); 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; 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 > 1 || selection < 0) { System.out.println("Invalid Entry, try again"); print(); } else { read = false; try { if (getWriteable()) { boolean oldValue = Boolean .parseBoolean(interactor .getSocketElement() .getValue().toString()); boolean newValue = false; if (selection == 1) newValue = true; if (oldValue == newValue) { } else { interactor .getSocketElement() .setValueRequest( Boolean .toString(!oldValue)); } } } catch (Exception e2) { } } } else { this.sp.getDialog().getWidgetWindow( ((Integer) (int) st.nval).toString()); read = false; //doChange(); } 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.inputBoolean.equals(null)) { helpString = HelpParser.inputBoolean; } System.out.println(helpString); print(); } else { System.out.println("Invalid Entry, try again"); print(); } } 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; } } } catch (IOException ioe) { System.out.println(ioe.getMessage()); } if (!exit) doChange(); } /** * This method gets the label for the interactor based on the clients * preferences (language, format etc) * * @return String */ public String getLabel() { return getText(Constants.ResourceRole.Label.toString()); } /** * This method is called to display the interactor in the session panel. The * textboolean displays its name/label and the current value(On or Off). * * @return String Returns a string of the form label(value) */ public String getUIComponent() { String val; if (Boolean.parseBoolean(interactor.getSocketElement().getValue() .toString())) val = "On"; else val = "Off"; return (this.label + " (" + val + ") "); } /* * (non-Javadoc) * * @see edu.wisc.trace.urcsdk.client.Widget#updateValue() */ public void updateValue() { // Have to change the values the urc is showing. if (sp != null) sp.paintSessionPanel(); } /* * (non-Javadoc) * @see edu.wisc.trace.urcsdk.client.Widget#setReadable(boolean) */ public void setReadable(boolean read) { this.read = read; } /* * (non-Javadoc) * @see edu.wisc.trace.urcsamples.textclient.TextWidget#getReadable() */ public boolean getReadable() { return this.read; } /** * This method sets the write attribute for the TextBoolean which is used to * decide if the interactor should be displayed. * * @param write - * boolean to set internal property write */ public void setWriteable(boolean write) { this.write = write; } /* * (non-Javadoc) * @see edu.wisc.trace.urcsdk.client.Widget#setExecutable(boolean) */ public void setExecutable(boolean execute) { // nothing } /** * @return boolean write */ public boolean getWriteable() { return write; } /* * (non-Javadoc) * @see edu.wisc.trace.urcsdk.client.Widget#updateResources() */ public void updateResources() { } /** * This method gets the HelpPurpose defined in a resource * sheet for the boolean interactor and returns the text in the following * format. Help-Purpose : (string text) If * it is empty, it's not included. * * @return help text. */ public String getHelpText() { String tmp, text = ""; if (!(tmp = getText(Constants.ResourceRole.HelpPurpose.toString())) .equals("")) { text += '\n' + "Help-Purpose: " + tmp; } return text; } /** * 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(); UISocketMirror uis = interactor.getSocketElement().getSocket(); ResourceManager rm = uis.getTarget().getResourceManager(); ElementRef elementRef = interactor.getSocketElement().getRef(); String valueRef = interactor.getSocketElement().getValue().toString(); valueRef = (valueRef.equals("")) ? null : valueRef; // for InputBoolean interactors there is no opRef (so we pass null) IResource label = rm.getResource(elementRef, valueRef, true, null, role, uPrefs); if (label == null) return ""; else if (label instanceof EmptyResource) return ""; else return label.getValue().toString(); } }