Example #1
0
  public void init(
      List<String> titles, int selected, String explanation, List<ExpanderControlTransObj> extras) {
    this.selected = selected;
    Err.pr(SdzNote.EXPANDER_IDX, "init on <" + getName() + ">, selected <" + selected + ">");
    if (SdzNote.EXPANDER_IDX.isVisible() && "chkIntoExisting".equals(getName()) && selected == 0) {
      Err.pr("Selecting 0 not happening on DB (solution was to write toString())");
    }
    removeAll();
    ModernTableLayout modernTableLayout = new ModernTableLayout();
    modernTableLayout.insertColumn(0, LEFT_EDGE_WIDTH);
    modernTableLayout.insertColumn(1, 25 - LEFT_EDGE_WIDTH);
    modernTableLayout.insertColumn(2, GAP_WIDTH);
    int count = NUM_LEAD_COLS;
    for (int i = 0; i < titles.size(); i++) {
      String title = titles.get(i);
      long labelWidth;
      if (sizeIncreasesWhenSelected) {
        labelWidth = WidgetUtils.calcPixelWidthOfStringOnComponent(title, TYPICAL_BIG_CONTAINER);
      } else {
        labelWidth = WidgetUtils.calcPixelWidthOfStringOnComponent(title, TYPICAL_SIZED_CONTAINER);
      }
      modernTableLayout.insertColumn(count, labelWidth);
      count++;
      IconLabel iconLabel = getIconLabelAt(i, extras);
      if (iconLabel != null) {
        Icon icon = iconLabel.getIcon();
        if (icon != null) {
          modernTableLayout.insertColumn(count, icon.getIconWidth() + 7);
          count++;
        } else {
          Err.error("Expect there to always be an icon in an IconLabel");
        }
      }
      modernTableLayout.insertColumn(count, GAP_WIDTH);
      count++;
    }
    modernTableLayout.insertRow(0, ModernTableLayout.FILL);
    setLayout(modernTableLayout);
    NoEdgedButton[] labels = new NoEdgedButton[titles.size()];
    lShiftRight = new NoEdgedButton();
    add(lShiftRight, "1,0");
    count = NUM_LEAD_COLS;
    for (int i = 0; i < titles.size(); i++) {
      NoEdgedButton item = new NoEdgedButton();
      addMouseListeners(item, mouseListeners);
      item.setActionListener(this);
      if (i == selected) {
        if (sizeIncreasesWhenSelected) {
          item.setFont(BIG_BOLD_FONT);
        } else {
          item.setFont(BOLD_FONT);
        }
        if (foregroundChangesWhenSelected) {
          item.setForeground(PURPLE);
        }
      } else {
        item.setFont(NORMAL_FONT);
        item.setForeground(null);
      }
      item.setName("lExpander control item number " + i);
      String title = titles.get(i);
      item.setText(title);
      item.setHorizontalAlignment(SwingConstants.LEADING);
      add(item, count + ", 0");
      count++;
      IconLabel iconLabel = getIconLabelAt(i, extras);
      if (iconLabel != null) {
        addMouseListeners(iconLabel, mouseListeners);
        add(iconLabel, count + ", 0");
        count++;
      }
      count++;
      labels[i] = item;
      if (extras != null && extras.size() > i) {
        if (extras.get(i).getBackground() != null) {
          labels[i].setOpaque(true);
          labels[i].setBackground(extras.get(i).getBackground());
        } else {
          // Err.error( "Do we just setBackground() to null and opaque to false?");
          labels[i].setOpaque(false);
          labels[i].setBackground(null);
        }
        if (selected == i && foregroundChangesWhenSelected) {
          /*
           * Never override the selected foreground being purple if
           * selected being purple is what has been specified.
           */
        } else {
          if (extras.get(i).getForeground() != null) {
            labels[i].setForeground(extras.get(i).getForeground());
          } else {
            labels[i].setForeground(null);
          }
        }
        if (extras.get(i).getToolTipText() != null) {
          labels[i].setToolTipText(extras.get(i).getToolTipText());
        } else {
          labels[i].setToolTipText(null);
        }
        if (iconLabel != null) {
          if (extras.get(i).getIconToolTipText() != null) {
            iconLabel.setToolTipText(extras.get(i).getIconToolTipText());
          } else {
            iconLabel.setToolTipText(null);
          }
        }
      }
    }

    lShiftRight.setName("lShiftRight");
    lShiftRight.setToolTipText(explanation);
    lShiftRight.setActionListener(this);
    PortableImageIcon portableImageIcon =
        PortableImageIcon.createImageIcon(
            "images/control_end_blue.png", "Change View", PortableImageIcon.STDERR_MSG);
    if (portableImageIcon != null) {
      lShiftRight.setIcon(portableImageIcon);
    }

    setLShiftRight(lShiftRight);
    setName("ExpanderControl");
    setLabels(labels);
    setExplanation(explanation);
    /*
    setBackgrounds( backgrounds);
    setForegrounds( foregrounds);
    setToolTipTexts( toolTipTexts);
    */
    setExtras(extras);
  }
  public GeoPointWidget(Context context, FormEntryPrompt prompt) {
    super(context, prompt);

    mWaitingForData = false;
    mUseMaps = false;
    String appearance = prompt.getAppearanceHint();
    if ("maps".equalsIgnoreCase(appearance)) {
      try {
        // use google maps it exists on the device
        Class.forName("com.google.android.maps.MapActivity");
        mUseMaps = true;
      } catch (ClassNotFoundException e) {
        mUseMaps = false;
      }
    }

    setOrientation(LinearLayout.VERTICAL);

    mStringAnswer = new TextView(getContext());
    mAnswerDisplay = new TextView(getContext());
    mAnswerDisplay.setTextSize(TypedValue.COMPLEX_UNIT_DIP, mAnswerFontsize);
    mAnswerDisplay.setGravity(Gravity.CENTER);

    Spannable locButtonText;
    boolean viewButtonEnabled;

    String s = prompt.getAnswerText();
    if (s != null && !("".equals(s))) {
      setBinaryData(s);

      locButtonText = StringUtils.getStringSpannableRobust(getContext(), R.string.replace_location);
      viewButtonEnabled = true;
    } else {
      locButtonText = StringUtils.getStringSpannableRobust(getContext(), R.string.get_location);
      viewButtonEnabled = false;
    }

    mGetLocationButton = new Button(getContext());
    WidgetUtils.setupButton(
        mGetLocationButton, locButtonText, mAnswerFontsize, !prompt.isReadOnly());

    mGetLocationButton.setOnClickListener(
        new View.OnClickListener() {
          @Override
          public void onClick(View v) {
            Intent i;
            if (mUseMaps) {
              i = new Intent(getContext(), GeoPointMapActivity.class);
            } else {
              i = new Intent(getContext(), GeoPointActivity.class);
            }
            ((Activity) getContext()).startActivityForResult(i, FormEntryActivity.LOCATION_CAPTURE);
            mWaitingForData = true;
          }
        });

    // setup 'view location' button
    mViewButton = new Button(getContext());
    WidgetUtils.setupButton(
        mViewButton,
        StringUtils.getStringSpannableRobust(getContext(), R.string.show_location),
        mAnswerFontsize,
        viewButtonEnabled);

    // launch appropriate map viewer
    mViewButton.setOnClickListener(
        new View.OnClickListener() {
          @Override
          public void onClick(View v) {
            String s = mStringAnswer.getText().toString();
            String[] sa = s.split(" ");
            double gp[] = new double[4];
            gp[0] = Double.valueOf(sa[0]);
            gp[1] = Double.valueOf(sa[1]);
            gp[2] = Double.valueOf(sa[2]);
            gp[3] = Double.valueOf(sa[3]);
            Intent i = new Intent(getContext(), GeoPointMapActivity.class);
            i.putExtra(LOCATION, gp);
            getContext().startActivity(i);
          }
        });

    addView(mGetLocationButton);
    if (mUseMaps) {
      addView(mViewButton);
    }
    addView(mAnswerDisplay);
  }