Home | History | Annotate | Download | only in editor
      1 /*
      2  * Copyright 2012 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *     http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.notificationstudio.editor;
     18 
     19 import android.text.Editable;
     20 import android.text.InputType;
     21 import android.text.TextWatcher;
     22 import android.view.View;
     23 import android.widget.EditText;
     24 
     25 import com.android.notificationstudio.R;
     26 import com.android.notificationstudio.editor.Editors.Editor;
     27 import com.android.notificationstudio.model.EditableItem;
     28 
     29 public class TextEditor implements Editor {
     30 
     31     public Runnable bindEditor(View v, final EditableItem item, final Runnable afterChange) {
     32         final EditText textEditor = (EditText) v.findViewById(R.id.text_editor);
     33         textEditor.setVisibility(View.VISIBLE);
     34         textEditor.setInputType(getInputType());
     35         Runnable updateEditText = new Runnable() {
     36             public void run() {
     37                 textEditor.setText(item.getValue() == null ? "" : item.getValue().toString());
     38             }};
     39         if (item.hasValue())
     40             updateEditText.run();
     41         textEditor.addTextChangedListener(new TextWatcher() {
     42             public void afterTextChanged(Editable s) {
     43                 Object newVal = parseValue(s.length() == 0 ? null : s.toString());
     44                 if (equal(newVal, item.getValue()))
     45                     return;
     46                 item.setValue(newVal);
     47                 afterChange.run();
     48             }
     49 
     50             public void beforeTextChanged(CharSequence s, int start, int count, int after) {
     51                 // noop
     52             }
     53 
     54             public void onTextChanged(CharSequence s, int start, int before, int count) {
     55                 // noop
     56             }
     57         });
     58         return updateEditText;
     59     }
     60 
     61     protected int getInputType() {
     62         return InputType.TYPE_CLASS_TEXT;
     63     }
     64 
     65     protected Object parseValue(String str) {
     66         return str;
     67     }
     68 
     69     private static boolean equal(Object a,  Object b) {
     70         return a == b || (a != null && a.equals(b));
     71     }
     72 
     73 }