use of jp.sblo.pandora.jota.text.UndoBuffer.TextChange in project Jota-Text-Editor-old by jiro-aqua.
the class TextView method onTextContextMenuItem.
/**
* Called when a context menu option for the text view is selected. Currently
* this will be one of: {@link android.R.id#selectAll},
* {@link android.R.id#startSelectingText},
* {@link android.R.id#cut}, {@link android.R.id#copy},
* {@link android.R.id#paste}, {@link android.R.id#copyUrl},
* or {@link android.R.id#switchInputMethod}.
*/
public boolean onTextContextMenuItem(int id) {
// Jota Text Editor
int min = 0;
int max = mText.length();
if (isFocused()) {
final int selStart = getSelectionStart();
final int selEnd = getSelectionEnd();
min = Math.max(0, Math.min(selStart, selEnd));
max = Math.max(0, Math.max(selStart, selEnd));
}
ClipboardManager clip = (ClipboardManager) getContext().getSystemService(Context.CLIPBOARD_SERVICE);
switch(id) {
case ID_SELECT_ALL:
Selection.setSelection((Spannable) mText, 0, mText.length());
startTextSelectionMode();
getSelectionController().show();
return true;
case ID_START_SELECTING_TEXT:
if (mHasNavigationDevice) {
MetaKeyKeyListener.startSelecting(this, (Spannable) mText);
} else {
int start = Selection.getSelectionStart((Spannable) mText);
ArrowKeyMovementMethod.selectWord((Spannable) mText, start);
startTextSelectionMode();
}
// getSelectionController().show();
return true;
case ID_STOP_SELECTING_TEXT:
MetaKeyKeyListener.stopSelecting(this, (Spannable) mText);
// Selection.setSelection((Spannable) mText, getSelectionEnd()); // Jota Text Editor
return true;
// Jota Text Editor
case ID_CANCEL_SELECTION:
Selection.setSelection((Spannable) mText, getSelectionEnd());
return true;
// Jota Text Editor
case ID_SHOW_IME:
{
showIme(true);
return true;
}
case ID_UNDO:
MetaKeyKeyListener.stopSelecting(this, (Spannable) mText);
// UNDO
{
TextChange textchange = mUndoBuffer.pop();
if (textchange != null) {
Editable text = (Editable) getText();
mUndoRedo = true;
text.replace(textchange.start, textchange.start + textchange.newtext.length(), textchange.oldtext);
Selection.setSelection(text, textchange.start + textchange.oldtext.length());
mRedoBuffer.push(textchange);
}
}
return true;
case ID_REDO:
MetaKeyKeyListener.stopSelecting(this, (Spannable) mText);
// REDO
{
TextChange textchange = mRedoBuffer.pop();
if (textchange != null) {
Editable text = (Editable) getText();
mUndoRedo = true;
text.replace(textchange.start, textchange.start + textchange.oldtext.length(), textchange.newtext);
Selection.setSelection(text, textchange.start + textchange.newtext.length());
mUndoBuffer.push(textchange);
}
}
return true;
case ID_CUT:
// Jota Text Editor
if (max - min > MAX_PARCELABLE) {
Toast.makeText(getContext(), R.string.toast_overflow_of_limit, Toast.LENGTH_LONG).show();
} else {
clip.setText(mTransformed.subSequence(min, max));
((Editable) mText).delete(min, max);
stopTextSelectionMode();
MetaKeyKeyListener.stopSelecting(this, (Spannable) mText);
}
return true;
case ID_COPY:
// Jota Text Editor
if (max - min > MAX_PARCELABLE) {
Toast.makeText(getContext(), R.string.toast_overflow_of_limit, Toast.LENGTH_LONG).show();
} else {
clip.setText(mTransformed.subSequence(min, max));
stopTextSelectionMode();
MetaKeyKeyListener.stopSelecting(this, (Spannable) mText);
}
return true;
case ID_PASTE:
CharSequence paste = clip.getText();
if (paste != null && paste.length() > 0) {
// long minMax = prepareSpacesAroundPaste(min, max, paste);
// min = extractRangeStartFromLong(minMax);
// max = extractRangeEndFromLong(minMax);
// Selection.setSelection((Spannable) mText, max);
((Editable) mText).replace(min, max, paste);
stopTextSelectionMode();
MetaKeyKeyListener.stopSelecting(this, (Spannable) mText);
}
return true;
case ID_COPY_URL:
URLSpan[] urls = ((Spanned) mText).getSpans(min, max, URLSpan.class);
if (urls.length == 1) {
clip.setText(urls[0].getURL());
}
return true;
case ID_SWITCH_INPUT_METHOD:
{
InputMethodManager imm = InputMethodManager.peekInstance();
if (imm != null) {
imm.showInputMethodPicker();
}
return true;
}
case ID_DIRECTINTENT:
MetaKeyKeyListener.stopSelecting(this, (Spannable) mText);
if (min != max) {
((EditText) this).doShortcut(KeyEvent.KEYCODE_D);
}
return true;
}
return false;
}
Aggregations