RosTextView.java
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2011 Google Inc.
00003  * 
00004  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
00005  * use this file except in compliance with the License. You may obtain a copy of
00006  * the License at
00007  * 
00008  * http://www.apache.org/licenses/LICENSE-2.0
00009  * 
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00012  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
00013  * License for the specific language governing permissions and limitations under
00014  * the License.
00015  */
00016 
00017 package org.ros.android.view;
00018 
00019 import android.content.Context;
00020 import android.util.AttributeSet;
00021 import android.widget.TextView;
00022 import org.ros.android.MessageCallable;
00023 import org.ros.message.MessageListener;
00024 import org.ros.namespace.GraphName;
00025 import org.ros.node.ConnectedNode;
00026 import org.ros.node.Node;
00027 import org.ros.node.NodeMain;
00028 import org.ros.node.topic.Subscriber;
00029 
00033 public class RosTextView<T> extends TextView implements NodeMain {
00034 
00035   private String topicName;
00036   private String messageType;
00037   private MessageCallable<String, T> callable;
00038 
00039   public RosTextView(Context context) {
00040     super(context);
00041   }
00042 
00043   public RosTextView(Context context, AttributeSet attrs) {
00044     super(context, attrs);
00045   }
00046 
00047   public RosTextView(Context context, AttributeSet attrs, int defStyle) {
00048     super(context, attrs, defStyle);
00049   }
00050 
00051   public void setTopicName(String topicName) {
00052     this.topicName = topicName;
00053   }
00054 
00055   public void setMessageType(String messageType) {
00056     this.messageType = messageType;
00057   }
00058 
00059   public void setMessageToStringCallable(MessageCallable<String, T> callable) {
00060     this.callable = callable;
00061   }
00062 
00063   @Override
00064   public GraphName getDefaultNodeName() {
00065     return GraphName.of("android_gingerbread/ros_text_view");
00066   }
00067 
00068   @Override
00069   public void onStart(ConnectedNode connectedNode) {
00070     Subscriber<T> subscriber = connectedNode.newSubscriber(topicName, messageType);
00071     subscriber.addMessageListener(new MessageListener<T>() {
00072       @Override
00073       public void onNewMessage(final T message) {
00074         if (callable != null) {
00075           post(new Runnable() {
00076             @Override
00077             public void run() {
00078               setText(callable.call(message));
00079             }
00080           });
00081         } else {
00082           post(new Runnable() {
00083             @Override
00084             public void run() {
00085               setText(message.toString());
00086             }
00087           });
00088         }
00089         postInvalidate();
00090       }
00091     });
00092   }
00093 
00094   @Override
00095   public void onShutdown(Node node) {
00096   }
00097 
00098   @Override
00099   public void onShutdownComplete(Node node) {
00100   }
00101 
00102   @Override
00103   public void onError(Node node, Throwable throwable) {
00104   }
00105 }


android_core
Author(s): Damon Kohler
autogenerated on Thu Aug 27 2015 12:11:33