Search in sources :

Example 96 with TextWatcher

use of android.text.TextWatcher in project Jota-Text-Editor-old by jiro-aqua.

the class Main method onCreate.

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    applyBootSetting();
    super.onCreate(savedInstanceState);
    setContentView(R.layout.textviewer);
    PreferenceManager.getDefaultSharedPreferences(this).registerOnSharedPreferenceChangeListener(mOnSharedPreferenceChangeListener);
    setVolumeControlStream(AudioManager.STREAM_MUSIC);
    mEditor = (jp.sblo.pandora.jota.text.EditText) findViewById(R.id.textedit);
    mEditor.setDocumentChangedListener(this);
    mEditor.setShortcutListener(this);
    mEditor.setChanged(false);
    mLlSearch = (LinearLayout) findViewById(R.id.search);
    mLlReplace = (LinearLayout) findViewById(R.id.replace);
    mEdtSearchWord = (jp.sblo.pandora.jota.text.EditText) findViewById(R.id.edtSearchWord);
    mEdtSearchWord.setShortcutListener(null);
    mBtnForward = (ImageButton) findViewById(R.id.btnForward);
    mBtnBackward = (ImageButton) findViewById(R.id.btnBackward);
    mChkReplace = (Button) findViewById(R.id.chkReplace);
    mBtnClose = (ImageButton) findViewById(R.id.btnClose);
    mEdtReplaceWord = (jp.sblo.pandora.jota.text.EditText) findViewById(R.id.edtReplaceWord);
    mEdtReplaceWord.setShortcutListener(null);
    mBtnReplace = (Button) findViewById(R.id.btnReplace);
    mBtnReplaceAll = (Button) findViewById(R.id.btnReplaceAll);
    applySetting();
    mEdtSearchWord.addTextChangedListener(new TextWatcher() {

        public void onTextChanged(CharSequence s, int start, int before, int count) {
            boolean enabled = (s.length() > 0);
            mBtnForward.setEnabled(enabled);
            mBtnBackward.setEnabled(enabled);
            mSearchResult = null;
        }

        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        public void afterTextChanged(Editable s) {
        }
    });
    mEdtSearchWord.setOnKeyListener(new OnKeyListener() {

        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if ((keyCode == KeyEvent.KEYCODE_ENTER || keyCode == KeyEvent.KEYCODE_DPAD_CENTER) && event.getAction() == KeyEvent.ACTION_UP) {
                if (mBtnForward.isEnabled()) {
                    mBtnForward.performClick();
                    return true;
                }
            }
            return false;
        }
    });
    mEdtSearchWord.setImeOptions(EditorInfo.IME_ACTION_DONE | EditorInfo.IME_FLAG_NO_FULLSCREEN | EditorInfo.IME_FLAG_NO_EXTRACT_UI);
    mEdtReplaceWord.addTextChangedListener(new TextWatcher() {

        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }

        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        public void afterTextChanged(Editable s) {
        }
    });
    mEdtReplaceWord.setOnKeyListener(new OnKeyListener() {

        public boolean onKey(View v, int keyCode, KeyEvent event) {
            // }
            return false;
        }
    });
    mEdtReplaceWord.setImeOptions(EditorInfo.IME_ACTION_DONE | EditorInfo.IME_FLAG_NO_FULLSCREEN | EditorInfo.IME_FLAG_NO_EXTRACT_UI);
    mBtnForward.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            String searchword = mEdtSearchWord.getText().toString();
            mSearchForward = true;
            doSearch(searchword);
        }
    });
    mBtnBackward.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            String searchword = mEdtSearchWord.getText().toString();
            mSearchForward = false;
            doSearch(searchword);
        }
    });
    mChkReplace.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            mLlReplace.setVisibility(View.VISIBLE);
            mChkReplace.setVisibility(View.GONE);
            mEdtReplaceWord.requestFocus();
            if (mSearchResult == null) {
                String searchword = mEdtSearchWord.getText().toString();
                doSearch(searchword);
            }
        }
    });
    mBtnClose.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            mSearchResult = null;
            mLlSearch.setVisibility(View.GONE);
            mLlReplace.setVisibility(View.GONE);
            mChkReplace.setVisibility(View.VISIBLE);
        }
    });
    // edtReplaceWord
    // btnSkip
    // btnReplaceAll
    mBtnReplace.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            String searchword = mEdtSearchWord.getText().toString();
            mReplaceWord = mEdtReplaceWord.getText().toString();
            doReplace(searchword);
        }
    });
    mBtnReplaceAll.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            String searchword = mEdtSearchWord.getText().toString();
            mReplaceWord = mEdtReplaceWord.getText().toString();
            doReplaceAll(searchword);
        }
    });
    mProcNew.run();
    if (savedInstanceState == null) {
        Intent it = getIntent();
        if (it != null && (Intent.ACTION_VIEW.equals(it.getAction()) || Intent.ACTION_EDIT.equals(it.getAction()))) {
            mLine = -1;
            Uri data = it.getData();
            String scheme = data.getScheme();
            String path = null;
            if (ContentResolver.SCHEME_FILE.equals(scheme)) {
                path = Uri.decode(data.getEncodedPath());
                String lineparam = data.getQueryParameter("line");
                if (lineparam != null) {
                    try {
                        mLine = Integer.parseInt(lineparam);
                    } catch (Exception e) {
                    }
                }
            } else if (ContentResolver.SCHEME_CONTENT.equals(scheme)) {
                ContentResolver cr = getContentResolver();
                Cursor cur = null;
                try {
                    cur = cr.query(data, null, null, null, null);
                } catch (Exception e) {
                }
                if (cur != null) {
                    cur.moveToFirst();
                    try {
                        path = cur.getString(cur.getColumnIndex("_data"));
                        if (path == null || !path.startsWith(Environment.getExternalStorageDirectory().getPath())) {
                            // from content provider
                            path = data.toString();
                        }
                    } catch (Exception e) {
                        path = data.toString();
                    }
                } else {
                    path = data.toString();
                }
            } else {
            }
            // }
            if (path != null) {
                mTask = new TextLoadTask(this, this, mLine);
                mTask.execute(path, mSettings.CharsetOpen);
            }
        } else if (it != null && Intent.ACTION_SEND.equals(it.getAction())) {
            Bundle extras = it.getExtras();
            String text = extras.getString(Intent.EXTRA_TEXT);
            if (text != null) {
                mEditor.setText(text);
            }
        } else if (it != null && ACTION_EDIT_SCRIPT.equals(it.getAction())) {
            Bundle extras = it.getExtras();
            String path = extras.getString(EXTRA_SCRIPT_PATH);
            String contents = getIntent().getStringExtra(EXTRA_SCRIPT_CONTENT);
            if (contents != null) {
                mEditor.setText(contents);
            } else {
                if (path != null) {
                    mTask = new TextLoadTask(this, this, mLine);
                    mTask.execute(path, mSettings.CharsetOpen);
                }
            }
        } else if (mSettings.rememberlastfile) {
            File[] fl = getHistory();
            if (fl != null) {
                mTask = new TextLoadTask(this, this, -1);
                mTask.execute(fl[0].getPath(), mSettings.CharsetOpen);
            }
        }
    } else {
        mInstanceState.filename = savedInstanceState.getString("filename");
        mInstanceState.charset = savedInstanceState.getString("charset");
        // mInstanceState.text = savedInstanceState.getString("text" );
        mInstanceState.linebreak = savedInstanceState.getInt("linebreak");
        // mInstanceState.selstart = savedInstanceState.getInt("selstart" );
        // mInstanceState.selend = savedInstanceState.getInt("selend" );
        mInstanceState.changed = savedInstanceState.getBoolean("changed");
        // mEditor.setText(mInstanceState.text);
        // mEditor.setSelection(mInstanceState.selstart,
        // mInstanceState.selend);
        mEditor.setChanged(mInstanceState.changed);
    }
}
Also used : Bundle(android.os.Bundle) Intent(android.content.Intent) Cursor(android.database.Cursor) View(android.view.View) TextView(android.widget.TextView) Uri(android.net.Uri) ActivityNotFoundException(android.content.ActivityNotFoundException) ContentResolver(android.content.ContentResolver) KeyEvent(android.view.KeyEvent) TextWatcher(android.text.TextWatcher) Editable(android.text.Editable) OnKeyListener(android.view.View.OnKeyListener) OnClickListener(android.view.View.OnClickListener) Context(android.content.Context)

Example 97 with TextWatcher

use of android.text.TextWatcher in project Jota-Text-Editor-old by jiro-aqua.

the class SpannableStringBuilder method sendTextWillChange.

private TextWatcher[] sendTextWillChange(int start, int before, int after) {
    TextWatcher[] recip = getSpans(start, start + before, TextWatcher.class);
    int n = recip.length;
    for (int i = 0; i < n; i++) {
        recip[i].beforeTextChanged(this, start, before, after);
    }
    return recip;
}
Also used : TextWatcher(android.text.TextWatcher) Paint(android.graphics.Paint)

Example 98 with TextWatcher

use of android.text.TextWatcher in project Jota-Text-Editor-old by jiro-aqua.

the class SpannableStringBuilder method change.

private int change(boolean notify, int start, int end, CharSequence tb, int tbstart, int tbend) {
    checkRange("replace", start, end);
    int ret = tbend - tbstart;
    TextWatcher[] recipients = null;
    if (notify)
        recipients = sendTextWillChange(start, end - start, tbend - tbstart);
    for (int i = mSpanCount - 1; i >= 0; i--) {
        if ((mSpanFlags[i] & SPAN_PARAGRAPH) == SPAN_PARAGRAPH) {
            int st = mSpanStarts[i];
            if (st > mGapStart)
                st -= mGapLength;
            int en = mSpanEnds[i];
            if (en > mGapStart)
                en -= mGapLength;
            int ost = st;
            int oen = en;
            int clen = length();
            if (st > start && st <= end) {
                for (st = end; st < clen; st++) if (st > end && charAt(st - 1) == '\n')
                    break;
            }
            if (en > start && en <= end) {
                for (en = end; en < clen; en++) if (en > end && charAt(en - 1) == '\n')
                    break;
            }
            if (st != ost || en != oen)
                setSpan(mSpans[i], st, en, mSpanFlags[i]);
        }
    }
    moveGapTo(end);
    if (tbend - tbstart >= mGapLength + (end - start))
        resizeFor(mText.length - mGapLength + tbend - tbstart - (end - start));
    mGapStart += tbend - tbstart - (end - start);
    mGapLength -= tbend - tbstart - (end - start);
    if (mGapLength < 1)
        new Exception("mGapLength < 1").printStackTrace();
    TextUtils.getChars(tb, tbstart, tbend, mText, start);
    if (tb instanceof Spanned) {
        Spanned sp = (Spanned) tb;
        Object[] spans = sp.getSpans(tbstart, tbend, Object.class);
        for (int i = 0; i < spans.length; i++) {
            int st = sp.getSpanStart(spans[i]);
            int en = sp.getSpanEnd(spans[i]);
            if (st < tbstart)
                st = tbstart;
            if (en > tbend)
                en = tbend;
            if (getSpanStart(spans[i]) < 0) {
                setSpan(false, spans[i], st - tbstart + start, en - tbstart + start, sp.getSpanFlags(spans[i]));
            }
        }
    }
    // no need for span fixup on pure insertion
    if (tbend > tbstart && end - start == 0) {
        if (notify) {
            sendTextChange(recipients, start, end - start, tbend - tbstart);
            sendTextHasChanged(recipients);
        }
        return ret;
    }
    boolean atend = (mGapStart + mGapLength == mText.length);
    for (int i = mSpanCount - 1; i >= 0; i--) {
        if (mSpanStarts[i] >= start && mSpanStarts[i] < mGapStart + mGapLength) {
            int flag = (mSpanFlags[i] & START_MASK) >> START_SHIFT;
            if (flag == POINT || (flag == PARAGRAPH && atend))
                mSpanStarts[i] = mGapStart + mGapLength;
            else
                mSpanStarts[i] = start;
        }
        if (mSpanEnds[i] >= start && mSpanEnds[i] < mGapStart + mGapLength) {
            int flag = (mSpanFlags[i] & END_MASK);
            if (flag == POINT || (flag == PARAGRAPH && atend))
                mSpanEnds[i] = mGapStart + mGapLength;
            else
                mSpanEnds[i] = start;
        }
        if (mSpanEnds[i] < mSpanStarts[i]) {
            System.arraycopy(mSpans, i + 1, mSpans, i, mSpanCount - (i + 1));
            System.arraycopy(mSpanStarts, i + 1, mSpanStarts, i, mSpanCount - (i + 1));
            System.arraycopy(mSpanEnds, i + 1, mSpanEnds, i, mSpanCount - (i + 1));
            System.arraycopy(mSpanFlags, i + 1, mSpanFlags, i, mSpanCount - (i + 1));
            mSpanCount--;
        }
    }
    if (notify) {
        sendTextChange(recipients, start, end - start, tbend - tbstart);
        sendTextHasChanged(recipients);
    }
    return ret;
}
Also used : TextWatcher(android.text.TextWatcher) Spanned(android.text.Spanned) Paint(android.graphics.Paint)

Example 99 with TextWatcher

use of android.text.TextWatcher in project Jota-Text-Editor-old by jiro-aqua.

the class FileSelectorActivity method onCreate.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.filelist);
    mBtnOK = (Button) findViewById(R.id.btnOK);
    mBtnCancel = (Button) findViewById(R.id.btnCancel);
    mTxtFilePath = (TextView) findViewById(R.id.txtFilePath);
    mEdtFileName = (EditText) findViewById(R.id.edtFileName);
    mCharsetSpinnerOpen = (Spinner) findViewById(R.id.spinner_charset_open);
    mCharsetSpinnerSave = (Spinner) findViewById(R.id.spinner_charset_save);
    mLinebreakSpinner = (Spinner) findViewById(R.id.spinner_linebreak);
    Intent intent = getIntent();
    setResult(RESULT_CANCELED, intent);
    Bundle extras = intent.getExtras();
    if (extras != null) {
        mMode = extras.getString(INTENT_MODE);
        mExtension = (String[]) extras.get(INTENT_EXTENSION);
        m_strDirPath = extras.getString(INTENT_INIT_PATH);
        String charset = extras.getString(INTENT_CHARSET);
        if (charset != null) {
            selectItemOfSpinner(mCharsetSpinnerOpen, charset);
            selectItemOfSpinner(mCharsetSpinnerSave, charset);
        }
        int linebreak = extras.getInt(INTENT_LINEBREAK, -1);
        mLinebreakSpinner.setSelection(linebreak + 1);
    }
    File file = new File(m_strDirPath);
    if (!file.isDirectory()) {
        m_strDirPath = file.getParent();
        m_strFileName = file.getName();
    }
    if (mMode == null) {
        Log.e("JotaFileSelector", "No MODE parameter specified");
        finish();
        return;
    }
    if (MODE_OPEN.equals(mMode)) {
        setTitle(R.string.fs_title_open);
        mBtnOK.setVisibility(View.GONE);
        mEdtFileName.setVisibility(View.GONE);
        mEdtFileName.setEnabled(false);
        mCharsetSpinnerOpen.setVisibility(View.VISIBLE);
        mCharsetSpinnerSave.setVisibility(View.GONE);
        mLinebreakSpinner.setVisibility(View.GONE);
    } else if (MODE_SAVE.equals(mMode)) {
        setTitle(R.string.fs_title_save);
        mEdtFileName.setEnabled(true);
        mEdtFileName.setText(m_strFileName);
        mCharsetSpinnerOpen.setVisibility(View.GONE);
        mCharsetSpinnerSave.setVisibility(View.VISIBLE);
        mLinebreakSpinner.setVisibility(View.VISIBLE);
    } else if (MODE_DIR.equals(mMode)) {
        setTitle(R.string.fs_title_dir);
        mEdtFileName.setEnabled(false);
        mEdtFileName.setVisibility(View.GONE);
        mCharsetSpinnerOpen.setVisibility(View.GONE);
        mCharsetSpinnerSave.setVisibility(View.GONE);
        mLinebreakSpinner.setVisibility(View.GONE);
    } else {
        Log.e("JotaFileSelector", "MODE parameter must be OPEN , SAVE or DIR.");
        finish();
        return;
    }
    mEdtFileName.addTextChangedListener(new TextWatcher() {

        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if (MODE_SAVE.equals(mMode)) {
                if (s.toString().indexOf('/') < 0) {
                    String strFilePath;
                    if (m_strDirPath.equals("/")) {
                        strFilePath = "/" + s;
                    } else {
                        strFilePath = m_strDirPath + "/" + s;
                    }
                    File f = new File(strFilePath);
                    mBtnOK.setEnabled(!f.isDirectory());
                } else {
                    mBtnOK.setEnabled(false);
                }
            }
        }

        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        public void afterTextChanged(Editable s) {
        }
    });
    mEdtFileName.setOnKeyListener(new View.OnKeyListener() {

        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if ((keyCode == KeyEvent.KEYCODE_ENTER || keyCode == KeyEvent.KEYCODE_DPAD_CENTER) && event.getAction() == KeyEvent.ACTION_UP) {
                if (mBtnOK.isEnabled()) {
                    mBtnOK.performClick();
                }
            }
            return false;
        }
    });
    //        mEdtFileName.setOnFocusChangeListener(new OnFocusChangeListener() {
    //
    //            public void onFocusChange(View v, boolean hasFocus) {
    //                if ( hasFocus && mEdtFileName.isEnabled() ){
    //                    String strFileName = mEdtFileName.getText().toString();
    //                    if ( strFileName.indexOf('/') >= 0 ){
    //                        mEdtFileName.setText("");
    //                    }
    //                }
    //            }
    //        });
    mBtnOK.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            if (MODE_SAVE.equals(mMode)) {
                String strFileName = mEdtFileName.getText().toString();
                String strFilePath;
                if (m_strDirPath.equals("/")) {
                    strFilePath = "/" + strFileName;
                } else {
                    strFilePath = m_strDirPath + "/" + strFileName;
                }
                if (new File(strFilePath).exists()) {
                    new AlertDialog.Builder(FileSelectorActivity.this).setTitle(R.string.confirmation).setPositiveButton(R.string.label_ok, new DialogInterface.OnClickListener() {

                        public void onClick(DialogInterface dialog, int which) {
                            returnSaveFilepath();
                        }
                    }).setNegativeButton(R.string.label_cancel, null).setCancelable(true).setMessage(getString(R.string.confirm_overwrite)).show();
                } else {
                    returnSaveFilepath();
                }
            } else {
                returnDirectory();
            }
        }
    });
    mBtnCancel.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            setResult(RESULT_CANCELED, null);
            finish();
        }
    });
    fillList();
}
Also used : AlertDialog(android.app.AlertDialog) DialogInterface(android.content.DialogInterface) Bundle(android.os.Bundle) Intent(android.content.Intent) TextView(android.widget.TextView) View(android.view.View) ListView(android.widget.ListView) KeyEvent(android.view.KeyEvent) TextWatcher(android.text.TextWatcher) Editable(android.text.Editable) File(java.io.File)

Example 100 with TextWatcher

use of android.text.TextWatcher in project Jota-Text-Editor-old by jiro-aqua.

the class TextView method sendBeforeTextChanged.

private void sendBeforeTextChanged(CharSequence text, int start, int before, int after) {
    if (mListeners != null) {
        final ArrayList<TextWatcher> list = mListeners;
        final int count = list.size();
        for (int i = 0; i < count; i++) {
            list.get(i).beforeTextChanged(text, start, before, after);
        }
    }
}
Also used : TextWatcher(android.text.TextWatcher) TextPaint(android.text.TextPaint) Paint(android.graphics.Paint)

Aggregations

TextWatcher (android.text.TextWatcher)177 Editable (android.text.Editable)143 View (android.view.View)86 TextView (android.widget.TextView)67 Paint (android.graphics.Paint)37 TextPaint (android.text.TextPaint)28 ImageView (android.widget.ImageView)27 EditText (android.widget.EditText)26 Intent (android.content.Intent)24 KeyEvent (android.view.KeyEvent)21 AdapterView (android.widget.AdapterView)18 ListView (android.widget.ListView)15 DialogInterface (android.content.DialogInterface)12 Button (android.widget.Button)12 RecyclerView (android.support.v7.widget.RecyclerView)11 AlertDialog (android.app.AlertDialog)10 OnClickListener (android.view.View.OnClickListener)10 InputMethodManager (android.view.inputmethod.InputMethodManager)10 LinearLayoutManager (android.support.v7.widget.LinearLayoutManager)9 SuppressLint (android.annotation.SuppressLint)8