AI2 Component  (Version nb184)
ElementsUtil.java
Go to the documentation of this file.
1 // -*- Mode: java; c-basic-offset: 2; -*-
2 // Copyright 2009-2011 Google, All Rights reserved
3 // Copyright 2011-2014 MIT, All rights reserved
4 // Released under the Apache License, Version 2.0
5 // http://www.apache.org/licenses/LICENSE-2.0
6 
7 package com.google.appinventor.components.runtime.util;
8 
10 
15 public class ElementsUtil {
16 
17  public static YailList elementsFromString(String itemString){
18  YailList items = new YailList();
19  if (itemString.length() > 0) {
20  items = YailList.makeList((Object[]) itemString.split(" *, *"));
21  }
22 
23  return items;
24  }
25 
33  public static YailList elements(YailList itemList, String componentName){
34  Object[] objects = itemList.toStringArray();
35  for (int i = 0; i < objects.length; i++) {
36  if (!(objects[i] instanceof String)) {
37  throw new YailRuntimeError("Items passed to " + componentName + " must be Strings",
38  "Error");
39  }
40  }
41  // this is not changing itemlist. it's just checking that the items are strings
42  return itemList;
43  }
44 
45  public static int selectionIndex(int index, YailList items){
46  if (index <= 0 || index > items.size()) {
47  return 0;
48  } else {
49  return index;
50  }
51  }
52 
53  public static String setSelectionFromIndex(int index, YailList items){
54  if (index == 0)
55  return "";
56  // YailLists are 0-based, but we want to be 1-based.
57  return items.getString(index - 1);
58  }
59 
60  public static int setSelectedIndexFromValue(String value, YailList items){
61  // Now, we need to change SelectionIndex to correspond to Selection.
62  // If multiple Selections have the same SelectionIndex, use the first.
63  // If none do, arbitrarily set the SelectionIndex to its default value
64  // of 0.
65  for (int i = 0; i < items.size(); i++) {
66  // The comparison is case-sensitive to be consistent with yail-equal?.
67  if (items.getString(i).equals(value)) {
68  return i + 1;
69  }
70  }
71  return 0;
72  }
73 
74 }
com.google.appinventor.components.runtime.util.YailList
Definition: YailList.java:26
com.google.appinventor.components
com.google.appinventor.components.runtime.util.YailList.makeList
static YailList makeList(Object[] objects)
Definition: YailList.java:59
com.google.appinventor.components.runtime.util.ElementsUtil.selectionIndex
static int selectionIndex(int index, YailList items)
Definition: ElementsUtil.java:45
com.google.appinventor.components.runtime.util.ElementsUtil.setSelectedIndexFromValue
static int setSelectedIndexFromValue(String value, YailList items)
Definition: ElementsUtil.java:60
com.google.appinventor.components.runtime.util.ElementsUtil.elements
static YailList elements(YailList itemList, String componentName)
Definition: ElementsUtil.java:33
com.google.appinventor.components.runtime.util.YailList.getString
String getString(int index)
Definition: YailList.java:193
com.google.appinventor.components.runtime.util.YailList.toStringArray
String[] toStringArray()
Definition: YailList.java:114
com.google.appinventor.components.runtime.errors.YailRuntimeError
Definition: YailRuntimeError.java:14
com.google.appinventor.components.runtime.util.ElementsUtil.elementsFromString
static YailList elementsFromString(String itemString)
Definition: ElementsUtil.java:17
com.google.appinventor.components.runtime
Copyright 2009-2011 Google, All Rights reserved.
Definition: AccelerometerSensor.java:8
com.google.appinventor.components.runtime.util.ElementsUtil.setSelectionFromIndex
static String setSelectionFromIndex(int index, YailList items)
Definition: ElementsUtil.java:53
com.google
com
com.google.appinventor.components.runtime.errors
Definition: ArrayIndexOutOfBoundsError.java:7
com.google.appinventor.components.runtime.util.ElementsUtil
Definition: ElementsUtil.java:15
com.google.appinventor.components.runtime.util.YailList.size
int size()
Definition: YailList.java:172
com.google.appinventor