/* 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.client.TargetMirror; import edu.wisc.trace.urcsdk.client.pret.RangeInteractor; 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.client.uisocket.UISocketMirror; import edu.wisc.trace.urcsdk.client.uisocket.types.NumberVariable; import edu.wisc.trace.urcsdk.support.UserPreferences; /** *

* TextRange is the text implementation of the Range interactor. *

* *

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

* * @author Hemanth Vijayan, Trace R&D Center * @version $Revision: 1.20 $ */ public class TextRange extends TextWidget { private String label; private NumberVariable numVar; private SessionPanel sp; private boolean read; private int min, max; private boolean write = false; /** * Creates a text range object. * * @param UIbuilder * @param rangeNumber */ public TextRange(TextUIBuilder UIbuilder, RangeInteractor rangeNumber) { super(UIbuilder, rangeNumber); numVar = (NumberVariable) rangeNumber.getSocketElement(); label = getLabel(); min = (int) numVar.getMinInclusive(); max = (int) numVar.getMaxInclusive(); } /** * returns the label associated with the range interactor. If no label is * found by the resource manager, then the id is used as the label. * * @return String label */ public String getLabel() { String text; if ((text = getText(Constants.ResourceRole.Label.toString())) .equals("")) { text = numVar.getId(); } return text; } /* * (non-Javadoc) * * @see edu.wisc.trace.urcsdk.client.Widget#updateResources() */ public void updateResources() { } /** * 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; } /** * returns the label and the value in parenthesis. * * @return string */ public String getUIComponent() { String val = interactor.getSocketElement().getValue().toString(); if (val == "" || val == null) val = "0"; return label + " (" + val + ")"; } /** * Update the values displayed to the user. Redraws the session panel. */ public void updateValue() { 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; } private String getUpOpRef() { UserPreferences uPrefs = builder.getUrc().getPreferences(); UISocketMirror socket = interactor.getSocketElement().getSocket(); TargetMirror target = socket.getTarget(); ResourceManager rm = target.getResourceManager(); ElementRef elementRef = numVar.getRef(); IResource text = rm.getResource(elementRef, null, true, "http://www.incits.org/incits393-2005#up", "http://www.incits.org/incits393-2005#label", uPrefs); if (text == null) return ""; else if (text instanceof EmptyResource) return ""; else return text.getValue().toString().toLowerCase(); } private String getDownOpRef() { UserPreferences uPrefs = builder.getUrc().getPreferences(); UISocketMirror socket = interactor.getSocketElement().getSocket(); TargetMirror target = socket.getTarget(); ResourceManager rm = target.getResourceManager(); ElementRef elementRef = numVar.getRef(); IResource text = rm.getResource(elementRef, null, true, "http://www.incits.org/incits393-2005#down", "http://www.incits.org/incits393-2005#label", uPrefs); if (text == null) return ""; else if (text instanceof EmptyResource) return ""; else return text.getValue().toString().toLowerCase(); } /** * sets write based on the interactors dependency. */ public void setWriteable(boolean write) { this.write = write; } /* * (non-Javadoc) * * @see edu.wisc.trace.urcsdk.client.Widget#setExecutable(boolean) */ public void setExecutable(boolean execute) { } /** * called when the interactor is selected. It asks for the user input and * sets the range to the value entered between min and max value. */ public void doChange() { if (getWriteable()) { InputStreamReader isr = new InputStreamReader(System.in); StreamTokenizer st = new StreamTokenizer(isr); String down = getDownOpRef(); String up = getUpOpRef(); char[] temp = { ' ' }; if (down != null) { for (int a = 0; a < down.length(); a++) { temp[0] = down.charAt(a); if (temp[0] == 'b') { // loop on } else { down = new String(temp); } } } if (up != null) { for (int a = 0; a < up.length(); a++) { temp[0] = up.charAt(a); if (temp[0] == 'b') { // loop on } else { up = new String(temp); } } } if (up.equals("") && down.equals("")) { System.out.print("Please enter value between (" + min + " and " + max + ") or b to go back: "); } else if (!up.equals("") && down.equals("")) { System.out.print("Please enter value between (" + min + " and " + max + "), " + up + " to increment or b to go back: "); } else if (!down.equals("") && up.equals("")) { System.out.print("Please enter value between (" + min + " and " + max + "), " + down + " to decrement or b to go back: "); } else { System.out.print("Please enter value between (" + min + " and " + max + "), " + up + " to increment, " + down + " to decrement or b to go back: "); } int selection = 0; boolean read = true; boolean exit = false; st.wordChars('?', '?'); st.wordChars('\n', '\n'); st.eolIsSignificant(true); try { while (read && !exit) { switch (st.nextToken()) { case StreamTokenizer.TT_NUMBER: // number input if (this.sp.getDialog() == null) { selection = (int) st.nval; if (selection > max || selection < min) { System.out.println("Invalid Entry, try again"); if (up.equals("") && down.equals("")) { System.out .print("Please enter value between (" + min+ " and "+ max+ ") or b to go back: "); } else if (!up.equals("") && down.equals("")) { System.out .print("Please enter value between ("+ min + " and "+ max+ "), "+ up+ " to increment or b to go back: "); } else if (!down.equals("") && up.equals("")) { System.out .print("Please enter value between ("+ min + " and "+ max+ "), "+ down + " to decrement or b to go back: "); } else { System.out .print("Please enter value between (" + min+ " and "+ max+ "), "+ up + " to increment, " + down + " to decrement or b to go back: "); } } else { read = false; try { if (getWriteable()) numVar.setValueRequest(Integer .toString(selection)); } catch (Exception e2) { } } } else { this.sp.getDialog().getWidgetWindow( ((Integer) (int) st.nval).toString()); read = false; // doChange(); } break; case StreamTokenizer.TT_WORD: // word input = string // If there are no modal dialogs open. If they are, the // user input is // for that. So redirect the input to the modal dialog. 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.range.equals(null)) { helpString = HelpParser.range; } System.out.println(helpString); if (up.equals("") && down.equals("")) { System.out .print("Please enter value between (" + min+ " and "+ max+ ") or b to go back: "); } else if (!up.equals("") && down.equals("")) { System.out .print("Please enter value between (" + min+ " and "+ max+ "), " + up+ " to increment or b to go back: "); } else if (!down.equals("") && up.equals("")) { System.out .print("Please enter value between (" + min+ " and "+ max + "), "+ down + " to decrement or b to go back: "); } else { System.out .print("Please enter value between (" + min+ " and "+ max+ "), "+ up+ " to increment, " + down+ " to decrement or b to go back: "); } } else if (st.sval.toLowerCase().equals(up)) { read = false; try { if (getWriteable()) { int val = Integer.parseInt(interactor .getSocketElement().getValue() .toString()); val++; if (val > max) { // do nothing } else numVar.setValueRequest(Integer .toString(val)); } } catch (Exception e2) { } } else if (st.sval.toLowerCase().equals(down)) { read = false; try { if (getWriteable()) { int val = Integer.parseInt(interactor .getSocketElement().getValue() .toString()); val--; if (val < min) { // do nothing } else numVar.setValueRequest(Integer .toString(val)); } } catch (Exception e2) { } } else { System.out.println("Invalid Entry, try again"); if (up.equals("") && down.equals("")) { System.out .print("Please enter value between (" + min+ " and " + max+ ") or b to go back: "); } else if (!up.equals("") && down.equals("")) { System.out .print("Please enter value between (" + min+ " and "+ max+ "), "+ up + " to increment or b to go back: "); } else if (!down.equals("") && up.equals("")) { System.out .print("Please enter value between (" + min+ " and "+ max+ "), "+ down + " to decrement or b to go back: "); } else { System.out .print("Please enter value between (" + min+ " and "+ max+ "), "+ up+ " to increment, " + down+ " to decrement or b to go back: "); } } } 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(); } } /** * returns write attribute for the interactor. */ public boolean getWriteable() { return write; } /** * This method gets the HelpPurpose defined in a resource sheet for the * range interactor and returns the text in the following format. * Help-Purpose : (string text) * * @return help text. */ public String getHelpText() { String tmp, text = ""; if (!(tmp = getText(Constants.ResourceRole.HelpPurpose.toString())) .equals("")) { text = "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 socket = interactor.getSocketElement().getSocket(); TargetMirror target = socket.getTarget(); ResourceManager rm = target.getResourceManager(); ElementRef elementRef = numVar.getRef(); IResource text = rm.getResource(elementRef, null, true, null, role, uPrefs); if (text == null) return ""; else if (text instanceof EmptyResource) return ""; else return text.getValue().toString(); } }