/* Copyright (C) 2014 A.Vontobel
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* The license is in LICENCE-GPL.txt.
* If not, see http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
*/
package net.sf.drawj2d.cmd;
import org.hecl.*;
import java.util.ArrayList;
import java.util.Arrays;
/**
* LabelCmd implements the "label" command, which
* just labels a point.
*/
public class LabelCmd implements Command {
API api;
boolean debug = false;
public LabelCmd (API api) {
this.api = api;
}
@Override
public Thing cmdCode(Interp interp, Thing[] argv) throws HeclException {
assert argv.length > 0;
if (debug) System.out.println(argv[0].toString());
if (argv.length < 2) throw new HeclException ("Illegal nb of arguments.");
String lb = StringThing.get(argv[1]);
switch (argv.length - 1) {
case 1: // label
api.label(lb);
break;
case 2: // label, position
case 3: // label, point P1, point P2
case 4: // label, point P1, point P2x, P2y or label, point P1x, P1y, point P2
case 5: // label, point P1x, P1y, point P2x, P2y
assert argv.length > 2;
Thing[] argv2 = new Thing[argv.length-2];
for (int i = 2; i < argv.length; i++) {
argv2[i-2] = argv[i];
}
ArrayList singleargv2 = splitArgv2(argv2);
switch (singleargv2.size()) {
case 1: // position
String dir = StringThing.get(argv[2]);
api.label(lb, dir);
break;
case 4: // point P1x, P1y, point P2x, P2y
// Convert the arguments from thing to double.
double[] argd = new double[4];
for (int i = 0; i < 4; i++) {
argd[i] = NumberThing.asNumber(singleargv2.get(i)).doubleValue();
}
api.labelline(argd[0], argd[1], argd[2], argd[3], lb);
break;
default:
throw new HeclException("Illegal nb of arguments.");
}
break;
default:
throw new HeclException ("Illegal nb of arguments.");
}
return null;
}
/** Make a list of arguments.
* Split List arguments to single arguments. */
private ArrayList splitArgv2(Thing[] argv) throws HeclException {
ArrayList singleargv = new ArrayList(argv.length);
for (int i = 0; i < argv.length; i++) {
Thing[] currentargv = ListThing.getArray(argv[i]);
singleargv.addAll(Arrays.asList(currentargv));
}
return singleargv;
}
}
Ads help maintain this website.