*
* The MIT License (MIT)
*
- * Copyright (C) 2016 Kevin Lamonte
+ * Copyright (C) 2017 Kevin Lamonte
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
}
/**
- * Update the cursor position.
+ * Update the visible cursor position to match the location of position
+ * and windowStart.
*/
protected void updateCursor() {
if ((position > getWidth()) && fixed) {
}
}
+ /**
+ * Normalize windowStart such that most of the field data if visible.
+ */
+ protected void normalizeWindowStart() {
+ if (fixed) {
+ // windowStart had better be zero, there is nothing to do here.
+ assert (windowStart == 0);
+ return;
+ }
+ windowStart = position - (getWidth() - 1);
+ if (windowStart < 0) {
+ windowStart = 0;
+ }
+
+ updateCursor();
+ }
+
/**
* Handle mouse button presses.
*
windowStart--;
}
}
+ normalizeWindowStart();
return;
}
return;
}
if (keypress.equals(kbHome)) {
- position = 0;
- windowStart = 0;
+ home();
return;
}
if (keypress.equals(kbEnd)) {
- position = text.length();
- if (fixed == true) {
- if (position >= getWidth()) {
- position = text.length() - 1;
- }
- } else {
- windowStart = text.length() - getWidth() + 1;
- if (windowStart < 0) {
- windowStart = 0;
- }
- }
+ end();
return;
}
text = text.substring(0, position)
+ text.substring(position + 1);
}
+ dispatch(false);
return;
}
}
}
dispatch(false);
+ normalizeWindowStart();
return;
}
}
}
+ /**
+ * Position the cursor at the first column. The field may adjust the
+ * window start to show as much of the field as possible.
+ */
+ public void home() {
+ position = 0;
+ windowStart = 0;
+ }
+
+ /**
+ * Set the editing position to the last filled character. The field may
+ * adjust the window start to show as much of the field as possible.
+ */
+ public void end() {
+ position = text.length();
+ if (fixed == true) {
+ if (position >= getWidth()) {
+ position = text.length() - 1;
+ }
+ } else {
+ windowStart = text.length() - getWidth() + 1;
+ if (windowStart < 0) {
+ windowStart = 0;
+ }
+ }
+ }
+
+ /**
+ * Set the editing position. The field may adjust the window start to
+ * show as much of the field as possible.
+ *
+ * @param position the new position
+ * @throws IndexOutOfBoundsException if position is outside the range of
+ * the available text
+ */
+ public void setPosition(final int position) {
+ if ((position < 0) || (position >= text.length())) {
+ throw new IndexOutOfBoundsException("Max length is " +
+ text.length() + ", requested position " + position);
+ }
+ this.position = position;
+ normalizeWindowStart();
+ }
+
}