Android Open Source - wolPi Text View Interactor






From Project

Back to project page wolPi.

License

The source code is released under:

Apache License

If you think the Android project wolPi listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package de.matthesrieke.wolpi.ui;
//from  w ww. java 2 s.  c o  m
import org.slf4j.helpers.FormattingTuple;
import org.slf4j.helpers.MessageFormatter;

import android.app.Activity;
import android.graphics.Color;
import android.text.Spannable;
import android.text.style.ForegroundColorSpan;
import android.widget.TextView;

import de.matthesrieke.wolpi.Interactor;

/**
 * {@link Interactor} implementation writing
 * content to a {@link TextView}, emulating consolish style.
 * 
 * @author matthes rieke
 *
 */
public class TextViewInteractor implements Interactor {
  
  private Activity parentActivity;
  private TextView targetTextView;

  public TextViewInteractor(Activity parentActivity, TextView targetTextView) {
    this.parentActivity = parentActivity;
    this.targetTextView = targetTextView;
  }

  @Override
  public boolean requestConfirmation(String request) {
    return ConfirmationDialog.issueConfirmationDialog(request, parentActivity);
  }
  
  @Override
  public void onOutput(final String output, final Located l, final Object... inlineStrings) {
    parentActivity.runOnUiThread(new Runnable() {
      
      @Override
      public void run() {
        targetTextView.append(System.getProperty("line.separator"));
        int color = resolveColor(l,  targetTextView.getTextColors().getDefaultColor());
        FormattingTuple result = MessageFormatter.arrayFormat(output, inlineStrings);
        appendColoredText(targetTextView, result.getMessage(), color);
      }
    });
  }
  
  protected int resolveColor(Located l, int defaultColor) {
    if (l == Located.LOCAL) {
      return defaultColor;
    }
    else {
      return Color.RED;
    }
  }

  @Override
  public void onOutput(String output, Object... inlineStrings) {
    onOutput(output, Located.LOCAL, inlineStrings);
  }
  
  @Override
  public void onError(String error, Located l, Object... inlineErrors) {
    onOutput(error, l, inlineErrors);
  }
  
  @Override
  public void onError(String error, Object... inlineErrors) {
    onError(error, Located.LOCAL, inlineErrors);
  }
  
  private void appendColoredText(TextView tv, String text, int color) {
      int start = tv.getText().length();
      tv.append(text);
      int end = tv.getText().length();

      Spannable spannableText = (Spannable) tv.getText();
      spannableText.setSpan(new ForegroundColorSpan(color), start, end, 0);
  }

}




Java Source Code List

de.matthesrieke.wolpi.CommandResult.java
de.matthesrieke.wolpi.Interactor.java
de.matthesrieke.wolpi.SysoutInteractor.java
de.matthesrieke.wolpi.UserInfoImpl.java
de.matthesrieke.wolpi.WolPiException.java
de.matthesrieke.wolpi.WolPi.java
de.matthesrieke.wolpi.dao.SQLiteSettingsProvider.java
de.matthesrieke.wolpi.settings.HostConfiguration.java
de.matthesrieke.wolpi.settings.SSHConnection.java
de.matthesrieke.wolpi.settings.SettingsProvider.java
de.matthesrieke.wolpi.settings.WolSettings.java
de.matthesrieke.wolpi.ui.ConfirmationDialog.java
de.matthesrieke.wolpi.ui.HostListActivity.java
de.matthesrieke.wolpi.ui.HostManagementActivity.java
de.matthesrieke.wolpi.ui.MainActivity.java
de.matthesrieke.wolpi.ui.TextViewInteractor.java
de.matthesrieke.wolpi.util.AndroidServiceLoader.java