/* Copyright (c) 2008, University of Southampton All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the University of Southampton nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ import java.applet.Applet; import java.awt.*; import java.text.DecimalFormat; import java.text.NumberFormat; import java.util.Vector; public class rendersliderV2 extends Applet { public rendersliderV2() { fontname = "TimesRoman"; inset = 20; } void drawhorizstepscale(Graphics g, int i, int j, double f, String s) { byte byte0 = 15; byte byte1 = 10; double f1 = (double)(j - 2 * (byte0 + inset)) / (f+1); g.drawLine(byte0 + inset, (i / 5) * 3, byte0 + inset, (i / 5) * 3 + byte1); g.drawLine(j - byte0 - inset, (i / 5) * 3, j - byte0 - inset, (i / 5) * 3 + byte1); g.drawLine(byte0 + inset, (i / 5) * 3 + byte1, j - byte0 - inset, (i / 5) * 3 + byte1); for(int k = 1; (double)k <= f; k++) { double f2 = (double)(inset + byte0) + f1 * (double)k; g.drawLine((int)f2, (i / 5) * 3, (int)f2, (i / 5) * 3 + byte1); } for(int l = 0; (double)l < f + 1.0D; l++) { double f3 = ((double)(inset + byte0) - f1 / 2.0D) + f1 * (double)(l+1); Point point = new Point((int)f3, (i / 2 - 2) + byte1 / 2); String s1; if(textlabels) { s1 = labelvalues[l]; } else { double f4 = step * (double)l; s1 = myFormatter.format(f4); if(s1.indexOf('.') > 0) { int i1 = s1.length() - s1.indexOf('.'); if(i1 > decimals + 1) { int j1 = s1.indexOf('.') + decimals + 1; s1 = s1.substring(0, j1); } } } drawstringatpoint(s, i / 8, s1, g, point); } } void drawvertstepscale(Graphics g, int i, int j, double f, String s) { // 'i' is height, 'j' is width byte byte0 = 15; byte byte1 = 10; double f1 = (double)(i - 2 * (byte0 + inset)) / (f+1); // draw baseline and end-points g.drawLine((j / 5) * 3, byte0 + inset, (j / 5) * 3 + byte1, byte0 + inset); g.drawLine((j / 5) * 3, i - byte0 - inset, (j / 5) * 3 + byte1, i - byte0 - inset); g.drawLine((j / 5) * 3 + byte1, byte0 + inset, (j / 5) * 3 + byte1, i - byte0 - inset); // draw graduations for(int k = 0; (double)k <= f; k++) { double f2 = (double)(inset + byte0) + f1 * (double)k; g.drawLine((j / 5) * 3, (int)f2, (j / 5) * 3 + byte1, (int)f2); } // write graduation labels for(int l = 0; (double)l < f + 1.0D; l++) { double f3 = ((double)(inset + byte0) - f1 / 2.0D) + f1 * (double)(l+1); Point point = new Point((j / 2 - 2) + byte1 / 2, (int)f3); String s1; if(textlabels) { s1 = labelvalues[l]; } else { double f4 = step * (double)l; s1 = myFormatter.format(f4); if(s1.indexOf('.') > 0) { int i1 = s1.length() - s1.indexOf('.'); if(i1 > decimals + 1) { int j1 = s1.indexOf('.') + decimals + 1; s1 = s1.substring(0, j1); } } } drawstringatpoint(s, j / 8, s1, g, point); } } void drawstringatpoint(String s, int i, String s1, Graphics g, Point point) { Font font = new Font(s, 1, i); FontMetrics fontmetrics = getFontMetrics(font); int j = fontmetrics.getHeight(); int k = fontmetrics.getDescent(); int l = fontmetrics.getAscent(); String s2 = s1; int i1 = fontmetrics.stringWidth(s1); g.setFont(font); g.drawString(s1, point.x - i1 / 2, point.y + l / 2); } public Vector getIdentifiers() { Vector identifiers = new Vector(); // only expecting one identifer - add it here identifiers.add(identifier); return identifiers; } public Vector getValues(String name) { if(name == identifier) { Vector value = new Vector(); // append label data if appropriate if(textlabels) output = labelidents[ScrollbarValue] + "," + labelvalues[ScrollbarValue]; // if there was no initial value and the slider has not yet been // moved, return a value of "null" if(isNullStart == true) { System.out.println("getValue(): Still a null start"); return null; } // else, append the appropriate output else { System.out.println("getValue(): No longer a null start"); value.add(output); } return value; } else return null; } public boolean handleEvent(Event event) { if(event.target instanceof Scrollbar) { ScrollbarValue = ((Scrollbar)event.target).getValue(); // an interaction has happened - unflag the empty starting value isNullStart = false; shownvalue = (double)ScrollbarValue / factor; repaint(); } return true; } public void init() { this.setLayout(null); setBackground(Color.white); String s = getParameter("feedbackstate"); String s1 = getParameter("baseType"); String s2 = getParameter("lowerBound"); String s3 = getParameter("upperBound"); String s4 = getParameter("step"); identifier = getParameter("identifier"); // if no start value is provided, flag it and set the value to zero if(getParameter("startVal") == null || getParameter("startVal").equals("null") || getParameter("startVal").equals("")) { initvalstr = "0"; isNullStart = true; } else { initvalstr = getParameter("startval"); isNullStart = false; } String s5 = getParameter("orientation"); orientation = s5; steplabel = getParameter("stepLabel"); int l = Integer.parseInt(getParameter("NoOfChoices")); String as[] = new String[l + 1]; labelidents = new String[l + 1]; labelvalues = new String[l + 1]; if(s1.equals("identifier")) textlabels = true; else textlabels = false; if(l > 1) { for(int i1 = 1; i1 < l + 1; i1++) { String s7 = "ChoiceNo" + String.valueOf(i1); as[i1] = getParameter(s7); if(textlabels) { labelvalues[i1] = as[i1].substring(as[i1].indexOf(":") + 1); labelidents[i1] = as[i1].substring(0, as[i1].indexOf(":")); String s8 = new String(labelvalues[i1]); s8 = labelidents[i1]; } } } step = Double.valueOf(s4).doubleValue(); factor = 1.0D / step; double f = Double.valueOf(initvalstr).doubleValue(); double f1 = Double.valueOf(s3).doubleValue(); double f2 = Double.valueOf(s2).doubleValue(); factor = 1.0D / step; steps = f1 / step; if(s.equals("No")) { Scrollbarinitval = (int) Math.round(f * factor); } else { s = s.substring(s.indexOf(":") + 1); s = s.trim(); if(s1.equals("identifier")) { for(int j1 = 1; j1 < l + 1; j1++) { if(!labelidents[j1].equals(s)) continue; Scrollbarinitval = j1; break; } } else { Scrollbarinitval = (int) Math.round((double)Integer.parseInt(s) * factor); } } int i = 0; int j = (int) Math.round(f2 * factor); int k = (int) Math.round(f1 * factor) + 1; if(s4.indexOf('.') > 0) decimals = s4.length() - s4.indexOf('.') - 1; else decimals = 0; String s6 = "#."; if(decimals > 0) { for(int k1 = 0; k1 < decimals; k1++) s6 = s6 + '#'; } else { s6 = "#"; } myFormatter = new DecimalFormat(s6); int orient; if(orientation.equals("Scrollbar.HORIZONTAL")) { orient = Scrollbar.HORIZONTAL; } else { orient = Scrollbar.VERTICAL; } Thescrollbar = new Scrollbar(orient, Scrollbarinitval, i, j, k); ScrollbarValue = Scrollbarinitval; shownvalue = (double)ScrollbarValue / factor; if(shownvalue == 0.0D) shownvalue = Scrollbarinitval; if(orient == Scrollbar.VERTICAL) Thescrollbar.setBounds(inset, inset, (this.getWidth()-(2*inset))/3, this.getHeight()-(2*inset)); else Thescrollbar.setBounds(inset, inset, this.getWidth()-(2*inset), (this.getHeight()-(2*inset))/3); add(Thescrollbar); } public Insets insets() { return new Insets(inset, inset, inset, inset); } public void paint(Graphics g) { int i = getSize().width; int j = getSize().height; g.drawRect(0, 0, getSize().width - 1, getSize().height - 1); showvalue(shownvalue, fontname, g); if(steplabel.equals("Yes")) { if(orientation.equals("Scrollbar.HORIZONTAL")) drawhorizstepscale(g, j, i, steps, fontname); else drawvertstepscale(g, j, i, steps, fontname); } //Vector temp = getValues((String)getIdentifiers().elementAt(0)); //String tmp = (String) temp.get(0); //System.out.println("Scrollbar value = " + tmp); } public void reset() { Thescrollbar.setValue(Scrollbarinitval); shownvalue = (double)Scrollbarinitval / factor; repaint(); } void showvalue(double f, String s, Graphics g) { int i = getSize().width; int j = getSize().height; int k,l; if(orientation.equals("Scrollbar.VERTICAL")) { k = (i * 6) / 7; l = j / 2; } else { k = i / 2; l = (j * 6) / 7; } Point point = new Point(k, l); int i1 = ScrollbarValue; if(textlabels) { output = labelvalues[ScrollbarValue]; } else { output = myFormatter.format(f); if(f == 0.0D) output = initvalstr; if(output.indexOf('.') > 0) { int j1 = output.length() - output.indexOf('.'); if(j1 > decimals + 1) { int k1 = output.indexOf('.') + decimals + 1; output = output.substring(0, k1); } } } if(orientation.equals("Scrollbar.HORIZONTAL")) { drawstringatpoint(s, j / 6, output, g, point); } else drawstringatpoint(s, i / 6, output, g, point); } Scrollbar Thescrollbar; int ScrollbarValue; int Scrollbarinitval; double factor; double steps; double step; double shownvalue; int decimals; String identifier; String orientation; String pattern; String initvalstr; String steplabel; String feedbackinfo; String output; String fontname; DecimalFormat myFormatter; String labelidents[]; String labelvalues[]; int inset; boolean textlabels; boolean isNullStart; }