// ------------------------------------------------------------------------
/**
- * Checkbox state, true means checked.
+ * CheckBox state, true means checked.
*/
private boolean checked = false;
* @param mouse mouse event
* @return true if the mouse is currently on the checkbox
*/
- private boolean mouseOnCheckbox(final TMouseEvent mouse) {
+ private boolean mouseOnCheckBox(final TMouseEvent mouse) {
if ((mouse.getY() == 0)
&& (mouse.getX() >= 0)
&& (mouse.getX() <= 2)
*/
@Override
public void onMouseDown(final TMouseEvent mouse) {
- if ((mouseOnCheckbox(mouse)) && (mouse.isMouse1())) {
+ if ((mouseOnCheckBox(mouse)) && (mouse.isMouse1())) {
// Switch state
checked = !checked;
}
*/
public class TDesktop extends TWindow {
+ // ------------------------------------------------------------------------
+ // Constructors -----------------------------------------------------------
+ // ------------------------------------------------------------------------
+
/**
* Public constructor.
*
setActive(false);
}
+ // ------------------------------------------------------------------------
+ // TWindow ----------------------------------------------------------------
+ // ------------------------------------------------------------------------
+
/**
* The default TDesktop draws a hatch character across everything.
*/
import java.io.File;
import java.util.ArrayList;
+import java.util.Collections;
import java.util.List;
/**
*/
public class TDirectoryList extends TList {
+ // ------------------------------------------------------------------------
+ // Variables --------------------------------------------------------------
+ // ------------------------------------------------------------------------
+
/**
* Files in the directory.
*/
*/
private File path;
+ // ------------------------------------------------------------------------
+ // Constructors -----------------------------------------------------------
+ // ------------------------------------------------------------------------
+
+ /**
+ * Public constructor.
+ *
+ * @param parent parent widget
+ * @param path directory path, must be a directory
+ * @param x column relative to parent
+ * @param y row relative to parent
+ * @param width width of text area
+ * @param height height of text area
+ */
+ public TDirectoryList(final TWidget parent, final String path, final int x,
+ final int y, final int width, final int height) {
+
+ this(parent, path, x, y, width, height, null);
+ }
+
+ /**
+ * Public constructor.
+ *
+ * @param parent parent widget
+ * @param path directory path, must be a directory
+ * @param x column relative to parent
+ * @param y row relative to parent
+ * @param width width of text area
+ * @param height height of text area
+ * @param action action to perform when an item is selected
+ */
+ public TDirectoryList(final TWidget parent, final String path, final int x,
+ final int y, final int width, final int height, final TAction action) {
+
+ super(parent, null, x, y, width, height, action);
+ files = new ArrayList<File>();
+ setPath(path);
+ }
+
+ // ------------------------------------------------------------------------
+ // TList ------------------------------------------------------------------
+ // ------------------------------------------------------------------------
+
+ // ------------------------------------------------------------------------
+ // TDirectoryList ---------------------------------------------------------
+ // ------------------------------------------------------------------------
+
/**
* Set the new path to display.
*
newStrings.add(renderFile(files.size() - 1));
}
}
+ Collections.sort(newStrings);
setList(newStrings);
// Select the first entry
return String.format("%-20s %5dk", name, (file.length() / 1024));
}
- /**
- * Public constructor.
- *
- * @param parent parent widget
- * @param path directory path, must be a directory
- * @param x column relative to parent
- * @param y row relative to parent
- * @param width width of text area
- * @param height height of text area
- */
- public TDirectoryList(final TWidget parent, final String path, final int x,
- final int y, final int width, final int height) {
-
- this(parent, path, x, y, width, height, null);
- }
-
- /**
- * Public constructor.
- *
- * @param parent parent widget
- * @param path directory path, must be a directory
- * @param x column relative to parent
- * @param y row relative to parent
- * @param width width of text area
- * @param height height of text area
- * @param action action to perform when an item is selected
- */
- public TDirectoryList(final TWidget parent, final String path, final int x,
- final int y, final int width, final int height, final TAction action) {
-
- super(parent, null, x, y, width, height, action);
- files = new ArrayList<File>();
- setPath(path);
- }
-
}
*/
public class TEditorWidget extends TWidget {
+ // ------------------------------------------------------------------------
+ // Constants --------------------------------------------------------------
+ // ------------------------------------------------------------------------
+
/**
* The number of lines to scroll on mouse wheel up/down.
*/
private static final int wheelScrollSize = 3;
+ // ------------------------------------------------------------------------
+ // Variables --------------------------------------------------------------
+ // ------------------------------------------------------------------------
+
/**
* The document being edited.
*/
*/
private int leftColumn = 0;
+ // ------------------------------------------------------------------------
+ // Constructors -----------------------------------------------------------
+ // ------------------------------------------------------------------------
+
/**
* Public constructor.
*
document = new Document(text, defaultColor);
}
+ // ------------------------------------------------------------------------
+ // TWidget ----------------------------------------------------------------
+ // ------------------------------------------------------------------------
+
/**
* Draw the text box.
*/
super.onMouseDown(mouse);
}
- /**
- * Align visible area with document current line.
- *
- * @param topLineIsTop if true, make the top visible line the document
- * current line if it was off-screen. If false, make the bottom visible
- * line the document current line.
- */
- private void alignTopLine(final boolean topLineIsTop) {
- int line = document.getLineNumber();
-
- if ((line < topLine) || (line > topLine + getHeight() - 1)) {
- // Need to move topLine to bring document back into view.
- if (topLineIsTop) {
- topLine = line - (getHeight() - 1);
- if (topLine < 0) {
- topLine = 0;
- }
- assert (topLine >= 0);
- } else {
- topLine = line;
- assert (topLine >= 0);
- }
- }
-
- /*
- System.err.println("line " + line + " topLine " + topLine);
- */
-
- // Document is in view, let's set cursorY
- assert (line >= topLine);
- setCursorY(line - topLine);
- alignCursor();
- }
-
- /**
- * Align document current line with visible area.
- *
- * @param topLineIsTop if true, make the top visible line the document
- * current line if it was off-screen. If false, make the bottom visible
- * line the document current line.
- */
- private void alignDocument(final boolean topLineIsTop) {
- int line = document.getLineNumber();
- int cursor = document.getCursor();
-
- if ((line < topLine) || (line > topLine + getHeight() - 1)) {
- // Need to move document to ensure it fits view.
- if (topLineIsTop) {
- document.setLineNumber(topLine);
- } else {
- document.setLineNumber(topLine + (getHeight() - 1));
- }
- if (cursor < document.getCurrentLine().getDisplayLength()) {
- document.setCursor(cursor);
- }
- }
-
- /*
- System.err.println("getLineNumber() " + document.getLineNumber() +
- " topLine " + topLine);
- */
-
- // Document is in view, let's set cursorY
- setCursorY(document.getLineNumber() - topLine);
- alignCursor();
- }
-
- /**
- * Align visible cursor with document cursor.
- */
- private void alignCursor() {
- int width = getWidth();
-
- int desiredX = document.getCursor() - leftColumn;
- if (desiredX < 0) {
- // We need to push the screen to the left.
- leftColumn = document.getCursor();
- } else if (desiredX > width - 1) {
- // We need to push the screen to the right.
- leftColumn = document.getCursor() - (width - 1);
- }
-
- /*
- System.err.println("document cursor " + document.getCursor() +
- " leftColumn " + leftColumn);
- */
-
-
- setCursorX(document.getCursor() - leftColumn);
- }
-
/**
* Handle keystrokes.
*
}
}
+ // ------------------------------------------------------------------------
+ // TEditorWidget ----------------------------------------------------------
+ // ------------------------------------------------------------------------
+
+ /**
+ * Align visible area with document current line.
+ *
+ * @param topLineIsTop if true, make the top visible line the document
+ * current line if it was off-screen. If false, make the bottom visible
+ * line the document current line.
+ */
+ private void alignTopLine(final boolean topLineIsTop) {
+ int line = document.getLineNumber();
+
+ if ((line < topLine) || (line > topLine + getHeight() - 1)) {
+ // Need to move topLine to bring document back into view.
+ if (topLineIsTop) {
+ topLine = line - (getHeight() - 1);
+ if (topLine < 0) {
+ topLine = 0;
+ }
+ assert (topLine >= 0);
+ } else {
+ topLine = line;
+ assert (topLine >= 0);
+ }
+ }
+
+ /*
+ System.err.println("line " + line + " topLine " + topLine);
+ */
+
+ // Document is in view, let's set cursorY
+ assert (line >= topLine);
+ setCursorY(line - topLine);
+ alignCursor();
+ }
+
+ /**
+ * Align document current line with visible area.
+ *
+ * @param topLineIsTop if true, make the top visible line the document
+ * current line if it was off-screen. If false, make the bottom visible
+ * line the document current line.
+ */
+ private void alignDocument(final boolean topLineIsTop) {
+ int line = document.getLineNumber();
+ int cursor = document.getCursor();
+
+ if ((line < topLine) || (line > topLine + getHeight() - 1)) {
+ // Need to move document to ensure it fits view.
+ if (topLineIsTop) {
+ document.setLineNumber(topLine);
+ } else {
+ document.setLineNumber(topLine + (getHeight() - 1));
+ }
+ if (cursor < document.getCurrentLine().getDisplayLength()) {
+ document.setCursor(cursor);
+ }
+ }
+
+ /*
+ System.err.println("getLineNumber() " + document.getLineNumber() +
+ " topLine " + topLine);
+ */
+
+ // Document is in view, let's set cursorY
+ setCursorY(document.getLineNumber() - topLine);
+ alignCursor();
+ }
+
+ /**
+ * Align visible cursor with document cursor.
+ */
+ private void alignCursor() {
+ int width = getWidth();
+
+ int desiredX = document.getCursor() - leftColumn;
+ if (desiredX < 0) {
+ // We need to push the screen to the left.
+ leftColumn = document.getCursor();
+ } else if (desiredX > width - 1) {
+ // We need to push the screen to the right.
+ leftColumn = document.getCursor() - (width - 1);
+ }
+
+ /*
+ System.err.println("document cursor " + document.getCursor() +
+ " leftColumn " + leftColumn);
+ */
+
+
+ setCursorX(document.getCursor() - leftColumn);
+ }
+
/**
* Get the number of lines in the underlying Document.
*
*/
private static final ResourceBundle i18n = ResourceBundle.getBundle(TEditorWindow.class.getName());
+ // ------------------------------------------------------------------------
+ // Variables --------------------------------------------------------------
+ // ------------------------------------------------------------------------
+
/**
* Hang onto my TEditor so I can resize it with the window.
*/
*/
private String filename = "";
- /**
- * Setup other fields after the editor is created.
- */
- private void setupAfterEditor() {
- hScroller = new THScroller(this, 17, getHeight() - 2, getWidth() - 20);
- vScroller = new TVScroller(this, getWidth() - 2, 0, getHeight() - 2);
- setMinimumWindowWidth(25);
- setMinimumWindowHeight(10);
- setTopValue(1);
- setBottomValue(editField.getMaximumRowNumber());
- setLeftValue(1);
- setRightValue(editField.getMaximumColumnNumber());
-
- statusBar = newStatusBar(i18n.getString("statusBar"));
- statusBar.addShortcutKeypress(kbF1, cmHelp,
- i18n.getString("statusBarHelp"));
- statusBar.addShortcutKeypress(kbF2, cmSave,
- i18n.getString("statusBarSave"));
- statusBar.addShortcutKeypress(kbF3, cmOpen,
- i18n.getString("statusBarOpen"));
- statusBar.addShortcutKeypress(kbF10, cmMenu,
- i18n.getString("statusBarMenu"));
- }
+ // ------------------------------------------------------------------------
+ // Constructors -----------------------------------------------------------
+ // ------------------------------------------------------------------------
/**
* Public constructor sets window title.
this(parent, i18n.getString("newTextDocument"));
}
- /**
- * Read file data into a string.
- *
- * @param file the file to open
- * @return the file contents
- * @throws IOException if a java.io operation throws
- */
- private String readFileData(final File file) throws IOException {
- StringBuilder fileContents = new StringBuilder();
- Scanner scanner = new Scanner(file);
- String EOL = System.getProperty("line.separator");
-
- try {
- while (scanner.hasNextLine()) {
- fileContents.append(scanner.nextLine() + EOL);
- }
- return fileContents.toString();
- } finally {
- scanner.close();
- }
- }
-
- /**
- * Read file data into a string.
- *
- * @param filename the file to open
- * @return the file contents
- * @throws IOException if a java.io operation throws
- */
- private String readFileData(final String filename) throws IOException {
- return readFileData(new File(filename));
- }
+ // ------------------------------------------------------------------------
+ // TWindow ----------------------------------------------------------------
+ // ------------------------------------------------------------------------
/**
* Draw the window.
}
}
- /**
- * Check if a mouse press/release/motion event coordinate is over the
- * editor.
- *
- * @param mouse a mouse-based event
- * @return whether or not the mouse is on the editor
- */
- private final boolean mouseOnEditor(final TMouseEvent mouse) {
- if ((mouse.getAbsoluteX() >= getAbsoluteX() + 1)
- && (mouse.getAbsoluteX() < getAbsoluteX() + getWidth() - 1)
- && (mouse.getAbsoluteY() >= getAbsoluteY() + 1)
- && (mouse.getAbsoluteY() < getAbsoluteY() + getHeight() - 1)
- ) {
- return true;
- }
- return false;
- }
-
/**
* Handle mouse press events.
*
super.onCommand(command);
}
+ // ------------------------------------------------------------------------
+ // TEditorWindow ----------------------------------------------------------
+ // ------------------------------------------------------------------------
+
+ /**
+ * Setup other fields after the editor is created.
+ */
+ private void setupAfterEditor() {
+ hScroller = new THScroller(this, 17, getHeight() - 2, getWidth() - 20);
+ vScroller = new TVScroller(this, getWidth() - 2, 0, getHeight() - 2);
+ setMinimumWindowWidth(25);
+ setMinimumWindowHeight(10);
+ setTopValue(1);
+ setBottomValue(editField.getMaximumRowNumber());
+ setLeftValue(1);
+ setRightValue(editField.getMaximumColumnNumber());
+
+ statusBar = newStatusBar(i18n.getString("statusBar"));
+ statusBar.addShortcutKeypress(kbF1, cmHelp,
+ i18n.getString("statusBarHelp"));
+ statusBar.addShortcutKeypress(kbF2, cmSave,
+ i18n.getString("statusBarSave"));
+ statusBar.addShortcutKeypress(kbF3, cmOpen,
+ i18n.getString("statusBarOpen"));
+ statusBar.addShortcutKeypress(kbF10, cmMenu,
+ i18n.getString("statusBarMenu"));
+ }
+
+ /**
+ * Read file data into a string.
+ *
+ * @param file the file to open
+ * @return the file contents
+ * @throws IOException if a java.io operation throws
+ */
+ private String readFileData(final File file) throws IOException {
+ StringBuilder fileContents = new StringBuilder();
+ Scanner scanner = new Scanner(file);
+ String EOL = System.getProperty("line.separator");
+
+ try {
+ while (scanner.hasNextLine()) {
+ fileContents.append(scanner.nextLine() + EOL);
+ }
+ return fileContents.toString();
+ } finally {
+ scanner.close();
+ }
+ }
+
+ /**
+ * Read file data into a string.
+ *
+ * @param filename the file to open
+ * @return the file contents
+ * @throws IOException if a java.io operation throws
+ */
+ private String readFileData(final String filename) throws IOException {
+ return readFileData(new File(filename));
+ }
+
+ /**
+ * Check if a mouse press/release/motion event coordinate is over the
+ * editor.
+ *
+ * @param mouse a mouse-based event
+ * @return whether or not the mouse is on the editor
+ */
+ private final boolean mouseOnEditor(final TMouseEvent mouse) {
+ if ((mouse.getAbsoluteX() >= getAbsoluteX() + 1)
+ && (mouse.getAbsoluteX() < getAbsoluteX() + getWidth() - 1)
+ && (mouse.getAbsoluteY() >= getAbsoluteY() + 1)
+ && (mouse.getAbsoluteY() < getAbsoluteY() + getHeight() - 1)
+ ) {
+ return true;
+ }
+ return false;
+ }
+
}
*/
public class TInputBox extends TMessageBox {
+ // ------------------------------------------------------------------------
+ // Variables --------------------------------------------------------------
+ // ------------------------------------------------------------------------
+
/**
* The input field.
*/
private TField field;
- /**
- * Retrieve the answer text.
- *
- * @return the answer text
- */
- public String getText() {
- return field.getText();
- }
+ // ------------------------------------------------------------------------
+ // Constructors -----------------------------------------------------------
+ // ------------------------------------------------------------------------
/**
* Public constructor. The input box will be centered on screen.
getApplication().yield();
}
+ // ------------------------------------------------------------------------
+ // TMessageBox ------------------------------------------------------------
+ // ------------------------------------------------------------------------
+
+ // ------------------------------------------------------------------------
+ // TInputBox --------------------------------------------------------------
+ // ------------------------------------------------------------------------
+
+ /**
+ * Retrieve the answer text.
+ *
+ * @return the answer text
+ */
+ public String getText() {
+ return field.getText();
+ }
+
}
*/
public static final int ESC = 43;
-
// Special "no-key" keypress, used to ignore undefined keystrokes
public static final TKeypress kbNoKey = new TKeypress(true,
TKeypress.NONE, ' ', false, false, false);
*/
private static final ResourceBundle i18n = ResourceBundle.getBundle(TMessageBox.class.getName());
+ // ------------------------------------------------------------------------
+ // Constants --------------------------------------------------------------
+ // ------------------------------------------------------------------------
+
/**
* Message boxes have these supported types.
*/
YESNOCANCEL
};
- /**
- * The type of this message box.
- */
- private Type type;
-
- /**
- * My buttons.
- */
- private List<TButton> buttons;
-
/**
* Message boxes have these possible results.
*/
NO
};
+ // ------------------------------------------------------------------------
+ // Variables --------------------------------------------------------------
+ // ------------------------------------------------------------------------
+
+ /**
+ * The type of this message box.
+ */
+ private Type type;
+
+ /**
+ * My buttons.
+ */
+ private List<TButton> buttons;
+
/**
* Which button was clicked: OK, CANCEL, YES, or NO.
*/
return result;
}
+ // ------------------------------------------------------------------------
+ // Constructors -----------------------------------------------------------
+ // ------------------------------------------------------------------------
+
/**
* Public constructor. The message box will be centered on screen.
*
}
}
+ // ------------------------------------------------------------------------
+ // TWindow ----------------------------------------------------------------
+ // ------------------------------------------------------------------------
+
/**
* Handle keystrokes.
*
break;
default:
- throw new IllegalArgumentException("Invalid message box type: " + type);
+ throw new IllegalArgumentException("Invalid message box type: " +
+ type);
}
super.onKeypress(keypress);
*/
public class TPasswordField extends TField {
+ // ------------------------------------------------------------------------
+ // Constructors -----------------------------------------------------------
+ // ------------------------------------------------------------------------
+
/**
* Public constructor.
*
super(parent, x, y, width, fixed, text, enterAction, updateAction);
}
+ // ------------------------------------------------------------------------
+ // TField -----------------------------------------------------------------
+ // ------------------------------------------------------------------------
+
/**
* Draw the text field.
*/
*/
public class TRadioGroup extends TWidget {
+ // ------------------------------------------------------------------------
+ // Variables --------------------------------------------------------------
+ // ------------------------------------------------------------------------
+
/**
* Label for this radio button group.
*/
*/
private TRadioButton selectedButton = null;
- /**
- * Get the radio button ID that was selected.
- *
- * @return ID of the selected button, or 0 if no button is selected
- */
- public int getSelected() {
- if (selectedButton == null) {
- return 0;
- }
- return selectedButton.getId();
- }
-
- /**
- * Set the new selected radio button. Note package private access.
- *
- * @param button new button that became selected
- */
- void setSelected(final TRadioButton button) {
- assert (button.isSelected());
- if (selectedButton != null) {
- selectedButton.setSelected(false);
- }
- selectedButton = button;
- }
+ // ------------------------------------------------------------------------
+ // Constructors -----------------------------------------------------------
+ // ------------------------------------------------------------------------
/**
* Public constructor.
this.label = label;
}
+ // ------------------------------------------------------------------------
+ // TWidget ----------------------------------------------------------------
+ // ------------------------------------------------------------------------
+
/**
* Draw a radio button with label.
*/
getScreen().putStringXY(2, 0, label, radioGroupColor);
}
+ // ------------------------------------------------------------------------
+ // TRadioGroup ------------------------------------------------------------
+ // ------------------------------------------------------------------------
+
+ /**
+ * Get the radio button ID that was selected.
+ *
+ * @return ID of the selected button, or 0 if no button is selected
+ */
+ public int getSelected() {
+ if (selectedButton == null) {
+ return 0;
+ }
+ return selectedButton.getId();
+ }
+
+ /**
+ * Set the new selected radio button. Note package private access.
+ *
+ * @param button new button that became selected
+ */
+ void setSelected(final TRadioButton button) {
+ assert (button.isSelected());
+ if (selectedButton != null) {
+ selectedButton.setSelected(false);
+ }
+ selectedButton = button;
+ }
+
/**
* Convenience function to add a radio button to this group.
*
*/
public class TScrollableWidget extends TWidget implements Scrollable {
+ // ------------------------------------------------------------------------
+ // Variables --------------------------------------------------------------
+ // ------------------------------------------------------------------------
+
/**
* The horizontal scrollbar.
*/
*/
protected TVScroller vScroller = null;
- /**
- * Place the scrollbars on the edge of this widget, and adjust bigChange
- * to match the new size. This is called by onResize().
- */
- protected void placeScrollbars() {
- if (hScroller != null) {
- hScroller.setY(getHeight() - 1);
- hScroller.setWidth(getWidth() - 1);
- hScroller.setBigChange(getWidth() - 1);
- }
- if (vScroller != null) {
- vScroller.setX(getWidth() - 1);
- vScroller.setHeight(getHeight() - 1);
- vScroller.setBigChange(getHeight() - 1);
- }
- }
-
- /**
- * Recompute whatever data is displayed by this widget.
- */
- public void reflowData() {
- // Default: nothing to do
- }
-
- /**
- * Handle window/screen resize events.
- *
- * @param event resize event
- */
- @Override
- public void onResize(final TResizeEvent event) {
- if (event.getType() == TResizeEvent.Type.WIDGET) {
- setWidth(event.getWidth());
- setHeight(event.getHeight());
-
- reflowData();
- placeScrollbars();
- return;
- } else {
- super.onResize(event);
- }
- }
+ // ------------------------------------------------------------------------
+ // Constructors -----------------------------------------------------------
+ // ------------------------------------------------------------------------
/**
* Protected constructor.
super(parent, enabled, x, y, width, height);
}
+ // ------------------------------------------------------------------------
+ // TWidget ----------------------------------------------------------------
+ // ------------------------------------------------------------------------
+
+ /**
+ * Handle window/screen resize events.
+ *
+ * @param event resize event
+ */
+ @Override
+ public void onResize(final TResizeEvent event) {
+ if (event.getType() == TResizeEvent.Type.WIDGET) {
+ setWidth(event.getWidth());
+ setHeight(event.getHeight());
+
+ reflowData();
+ placeScrollbars();
+ return;
+ } else {
+ super.onResize(event);
+ }
+ }
+
+ // ------------------------------------------------------------------------
+ // TScrollableWidget ------------------------------------------------------
+ // ------------------------------------------------------------------------
+
+ /**
+ * Place the scrollbars on the edge of this widget, and adjust bigChange
+ * to match the new size. This is called by onResize().
+ */
+ protected void placeScrollbars() {
+ if (hScroller != null) {
+ hScroller.setY(getHeight() - 1);
+ hScroller.setWidth(getWidth() - 1);
+ hScroller.setBigChange(getWidth() - 1);
+ }
+ if (vScroller != null) {
+ vScroller.setX(getWidth() - 1);
+ vScroller.setHeight(getHeight() - 1);
+ vScroller.setBigChange(getHeight() - 1);
+ }
+ }
+
+ /**
+ * Recompute whatever data is displayed by this widget.
+ */
+ public void reflowData() {
+ // Default: nothing to do
+ }
+
/**
* Get the horizontal scrollbar, or null if this Viewport does not
* support horizontal scrolling.
*/
public class TScrollableWindow extends TWindow implements Scrollable {
+ // ------------------------------------------------------------------------
+ // Variables --------------------------------------------------------------
+ // ------------------------------------------------------------------------
+
/**
* The horizontal scrollbar.
*/
*/
protected TVScroller vScroller = null;
- /**
- * Place the scrollbars on the edge of this widget, and adjust bigChange
- * to match the new size. This is called by onResize().
- */
- protected void placeScrollbars() {
- if (hScroller != null) {
- hScroller.setY(getHeight() - 2);
- hScroller.setWidth(getWidth() - hScroller.getX() - 3);
- hScroller.setBigChange(getWidth() - hScroller.getX() - 3);
- }
- if (vScroller != null) {
- vScroller.setX(getWidth() - 2);
- vScroller.setHeight(getHeight() - 2);
- vScroller.setBigChange(getHeight() - 2);
- }
- }
-
- /**
- * Recompute whatever data is displayed by this widget.
- */
- public void reflowData() {
- // Default: nothing to do
- }
-
- /**
- * Handle window/screen resize events.
- *
- * @param event resize event
- */
- @Override
- public void onResize(final TResizeEvent event) {
- if (event.getType() == TResizeEvent.Type.WIDGET) {
- reflowData();
- placeScrollbars();
- return;
- } else {
- super.onResize(event);
- }
- }
+ // ------------------------------------------------------------------------
+ // Constructors -----------------------------------------------------------
+ // ------------------------------------------------------------------------
/**
* Public constructor. Window will be located at (0, 0).
super(application, title, x, y, width, height, flags);
}
+ // ------------------------------------------------------------------------
+ // TWindow ----------------------------------------------------------------
+ // ------------------------------------------------------------------------
+
+ /**
+ * Handle window/screen resize events.
+ *
+ * @param event resize event
+ */
+ @Override
+ public void onResize(final TResizeEvent event) {
+ if (event.getType() == TResizeEvent.Type.WIDGET) {
+ reflowData();
+ placeScrollbars();
+ return;
+ } else {
+ super.onResize(event);
+ }
+ }
+
+ /**
+ * Maximize window.
+ */
+ @Override
+ public void maximize() {
+ super.maximize();
+ placeScrollbars();
+ }
+
+ /**
+ * Restore (unmaximize) window.
+ */
+ @Override
+ public void restore() {
+ super.restore();
+ placeScrollbars();
+ }
+
+ // ------------------------------------------------------------------------
+ // TScrollableWindow ------------------------------------------------------
+ // ------------------------------------------------------------------------
+
+ /**
+ * Place the scrollbars on the edge of this widget, and adjust bigChange
+ * to match the new size. This is called by onResize().
+ */
+ protected void placeScrollbars() {
+ if (hScroller != null) {
+ hScroller.setY(getHeight() - 2);
+ hScroller.setWidth(getWidth() - hScroller.getX() - 3);
+ hScroller.setBigChange(getWidth() - hScroller.getX() - 3);
+ }
+ if (vScroller != null) {
+ vScroller.setX(getWidth() - 2);
+ vScroller.setHeight(getHeight() - 2);
+ vScroller.setBigChange(getHeight() - 2);
+ }
+ }
+
+ /**
+ * Recompute whatever data is displayed by this widget.
+ */
+ public void reflowData() {
+ // Default: nothing to do
+ }
+
/**
* Get the horizontal scrollbar, or null if this Viewport does not
* support horizontal scrolling.
*/
private static final ResourceBundle i18n = ResourceBundle.getBundle(TTerminalWindow.class.getName());
+ // ------------------------------------------------------------------------
+ // Variables --------------------------------------------------------------
+ // ------------------------------------------------------------------------
+
/**
* The emulator.
*/
*/
private boolean ptypipe = false;
- /**
- * Claim the keystrokes the emulator will need.
- */
- private void addShortcutKeys() {
- addShortcutKeypress(kbCtrlA);
- addShortcutKeypress(kbCtrlB);
- addShortcutKeypress(kbCtrlC);
- addShortcutKeypress(kbCtrlD);
- addShortcutKeypress(kbCtrlE);
- addShortcutKeypress(kbCtrlF);
- addShortcutKeypress(kbCtrlG);
- addShortcutKeypress(kbCtrlH);
- addShortcutKeypress(kbCtrlU);
- addShortcutKeypress(kbCtrlJ);
- addShortcutKeypress(kbCtrlK);
- addShortcutKeypress(kbCtrlL);
- addShortcutKeypress(kbCtrlM);
- addShortcutKeypress(kbCtrlN);
- addShortcutKeypress(kbCtrlO);
- addShortcutKeypress(kbCtrlP);
- addShortcutKeypress(kbCtrlQ);
- addShortcutKeypress(kbCtrlR);
- addShortcutKeypress(kbCtrlS);
- addShortcutKeypress(kbCtrlT);
- addShortcutKeypress(kbCtrlU);
- addShortcutKeypress(kbCtrlV);
- addShortcutKeypress(kbCtrlW);
- addShortcutKeypress(kbCtrlX);
- addShortcutKeypress(kbCtrlY);
- addShortcutKeypress(kbCtrlZ);
- addShortcutKeypress(kbF1);
- addShortcutKeypress(kbF2);
- addShortcutKeypress(kbF3);
- addShortcutKeypress(kbF4);
- addShortcutKeypress(kbF5);
- addShortcutKeypress(kbF6);
- addShortcutKeypress(kbF7);
- addShortcutKeypress(kbF8);
- addShortcutKeypress(kbF9);
- addShortcutKeypress(kbF10);
- addShortcutKeypress(kbF11);
- addShortcutKeypress(kbF12);
- addShortcutKeypress(kbAltA);
- addShortcutKeypress(kbAltB);
- addShortcutKeypress(kbAltC);
- addShortcutKeypress(kbAltD);
- addShortcutKeypress(kbAltE);
- addShortcutKeypress(kbAltF);
- addShortcutKeypress(kbAltG);
- addShortcutKeypress(kbAltH);
- addShortcutKeypress(kbAltU);
- addShortcutKeypress(kbAltJ);
- addShortcutKeypress(kbAltK);
- addShortcutKeypress(kbAltL);
- addShortcutKeypress(kbAltM);
- addShortcutKeypress(kbAltN);
- addShortcutKeypress(kbAltO);
- addShortcutKeypress(kbAltP);
- addShortcutKeypress(kbAltQ);
- addShortcutKeypress(kbAltR);
- addShortcutKeypress(kbAltS);
- addShortcutKeypress(kbAltT);
- addShortcutKeypress(kbAltU);
- addShortcutKeypress(kbAltV);
- addShortcutKeypress(kbAltW);
- addShortcutKeypress(kbAltX);
- addShortcutKeypress(kbAltY);
- addShortcutKeypress(kbAltZ);
- }
-
- /**
- * Convert a string array to a whitespace-separated string.
- *
- * @param array the string array
- * @return a single string
- */
- private String stringArrayToString(final String [] array) {
- StringBuilder sb = new StringBuilder(array[0].length());
- for (int i = 0; i < array.length; i++) {
- sb.append(array[i]);
- if (i < array.length - 1) {
- sb.append(' ');
- }
- }
- return sb.toString();
- }
-
- /**
- * Spawn the shell.
- *
- * @param command the command line to execute
- */
- private void spawnShell(final String [] command) {
-
- /*
- System.err.printf("spawnShell(): '%s'\n",
- stringArrayToString(command));
- */
-
- vScroller = new TVScroller(this, getWidth() - 2, 0, getHeight() - 2);
- setBottomValue(0);
-
- // Assume XTERM
- ECMA48.DeviceType deviceType = ECMA48.DeviceType.XTERM;
-
- try {
- ProcessBuilder pb = new ProcessBuilder(command);
- Map<String, String> env = pb.environment();
- env.put("TERM", ECMA48.deviceTypeTerm(deviceType));
- env.put("LANG", ECMA48.deviceTypeLang(deviceType, "en"));
- env.put("COLUMNS", "80");
- env.put("LINES", "24");
- pb.redirectErrorStream(true);
- shell = pb.start();
- emulator = new ECMA48(deviceType, shell.getInputStream(),
- shell.getOutputStream(), this);
- } catch (IOException e) {
- messageBox(i18n.getString("errorLaunchingShellTitle"),
- MessageFormat.format(i18n.getString("errorLaunchingShellText"),
- e.getMessage()));
- }
-
- // Setup the scroll bars
- onResize(new TResizeEvent(TResizeEvent.Type.WIDGET, getWidth(),
- getHeight()));
-
- // Claim the keystrokes the emulator will need.
- addShortcutKeys();
-
- // Add shortcut text
- newStatusBar(i18n.getString("statusBarRunning"));
- }
+ // ------------------------------------------------------------------------
+ // Constructors -----------------------------------------------------------
+ // ------------------------------------------------------------------------
/**
* Public constructor spawns a custom command line.
}
}
- /**
- * Terminate the child of the 'script' process used on POSIX. This may
- * or may not work.
- */
- private void terminateShellChildProcess() {
- int pid = -1;
- if (shell.getClass().getName().equals("java.lang.UNIXProcess")) {
- /* get the PID on unix/linux systems */
- try {
- Field field = shell.getClass().getDeclaredField("pid");
- field.setAccessible(true);
- pid = field.getInt(shell);
- } catch (Throwable e) {
- // SQUASH, this didn't work. Just bail out quietly.
- return;
- }
- }
- if (pid != -1) {
- // shell.destroy() works successfully at killing this side of
- // 'script'. But we need to make sure the other side (child
- // process) is also killed.
- String [] cmdKillIt = {
- "pkill", "-P", Integer.toString(pid)
- };
- try {
- Runtime.getRuntime().exec(cmdKillIt);
- } catch (Throwable e) {
- // SQUASH, this didn't work. Just bail out quietly.
- return;
- }
- }
- }
+ // ------------------------------------------------------------------------
+ // TScrollableWindow ------------------------------------------------------
+ // ------------------------------------------------------------------------
/**
* Draw the display buffer.
}
/**
- * Called by emulator when fresh data has come in.
- */
- public void displayChanged() {
- getApplication().postEvent(new TMenuEvent(TMenu.MID_REPAINT));
- }
-
- /**
- * Function to call to obtain the display width.
+ * Handle window/screen resize events.
*
- * @return the number of columns in the display
+ * @param resize resize event
*/
- public int getDisplayWidth() {
- if (ptypipe) {
- return getWidth() - 2;
- }
- return 80;
+ @Override
+ public void onResize(final TResizeEvent resize) {
+
+ // Synchronize against the emulator so we don't stomp on its reader
+ // thread.
+ synchronized (emulator) {
+
+ if (resize.getType() == TResizeEvent.Type.WIDGET) {
+ // Resize the scroll bars
+ reflowData();
+ placeScrollbars();
+
+ // Get out of scrollback
+ setVerticalValue(0);
+
+ if (ptypipe) {
+ emulator.setWidth(getWidth() - 2);
+ emulator.setHeight(getHeight() - 2);
+
+ emulator.writeRemote("\033[8;" + (getHeight() - 2) + ";" +
+ (getWidth() - 2) + "t");
+ }
+ }
+ return;
+
+ } // synchronized (emulator)
}
/**
- * Function to call to obtain the display height.
- *
- * @return the number of rows in the display
+ * Resize scrollbars for a new width/height.
*/
- public int getDisplayHeight() {
- if (ptypipe) {
- return getHeight() - 2;
- }
- return 24;
+ @Override
+ public void reflowData() {
+
+ // Synchronize against the emulator so we don't stomp on its reader
+ // thread.
+ synchronized (emulator) {
+
+ // Pull cursor information
+ readEmulatorState();
+
+ // Vertical scrollbar
+ setTopValue(getHeight() - 2
+ - (emulator.getScrollbackBuffer().size()
+ + emulator.getDisplayBuffer().size()));
+ setVerticalBigChange(getHeight() - 2);
+
+ } // synchronized (emulator)
}
/**
}
/**
- * Hook for subclasses to be notified of the shell termination.
- */
- public void onShellExit() {
- getApplication().postEvent(new TMenuEvent(TMenu.MID_REPAINT));
- }
-
- /**
- * Copy out variables from the emulator that TTerminal has to expose on
- * screen.
+ * Handle keystrokes.
+ *
+ * @param keypress keystroke event
*/
- private void readEmulatorState() {
- // Synchronize against the emulator so we don't stomp on its reader
- // thread.
- synchronized (emulator) {
-
- setCursorX(emulator.getCursorX() + 1);
- setCursorY(emulator.getCursorY() + 1
- + (getHeight() - 2 - emulator.getHeight())
- - getVerticalValue());
- setCursorVisible(emulator.isCursorVisible());
- if (getCursorX() > getWidth() - 2) {
- setCursorVisible(false);
- }
- if ((getCursorY() > getHeight() - 2) || (getCursorY() < 0)) {
- setCursorVisible(false);
- }
- if (emulator.getScreenTitle().length() > 0) {
- // Only update the title if the shell is still alive
- if (shell != null) {
- setTitle(emulator.getScreenTitle());
- }
- }
-
- // Check to see if the shell has died.
- if (!emulator.isReading() && (shell != null)) {
- try {
- int rc = shell.exitValue();
- // The emulator exited on its own, all is fine
- setTitle(MessageFormat.format(i18n.
- getString("windowTitleCompleted"), getTitle(), rc));
- shell = null;
- emulator.close();
- clearShortcutKeypresses();
- statusBar.setText(MessageFormat.format(i18n.
- getString("statusBarCompleted"), rc));
- onShellExit();
- } catch (IllegalThreadStateException e) {
- // The emulator thread has exited, but the shell Process
- // hasn't figured that out yet. Do nothing, we will see
- // this in a future tick.
- }
- } else if (emulator.isReading() && (shell != null)) {
- // The shell might be dead, let's check
- try {
- int rc = shell.exitValue();
- // If we got here, the shell died.
- setTitle(MessageFormat.format(i18n.
- getString("windowTitleCompleted"), getTitle(), rc));
- shell = null;
- emulator.close();
- clearShortcutKeypresses();
- statusBar.setText(MessageFormat.format(i18n.
- getString("statusBarCompleted"), rc));
- onShellExit();
- } catch (IllegalThreadStateException e) {
- // The shell is still running, do nothing.
- }
- }
-
- } // synchronized (emulator)
- }
-
- /**
- * Handle window/screen resize events.
- *
- * @param resize resize event
- */
- @Override
- public void onResize(final TResizeEvent resize) {
-
- // Synchronize against the emulator so we don't stomp on its reader
- // thread.
- synchronized (emulator) {
-
- if (resize.getType() == TResizeEvent.Type.WIDGET) {
- // Resize the scroll bars
- reflowData();
- placeScrollbars();
-
- // Get out of scrollback
- setVerticalValue(0);
-
- if (ptypipe) {
- emulator.setWidth(getWidth() - 2);
- emulator.setHeight(getHeight() - 2);
-
- emulator.writeRemote("\033[8;" + (getHeight() - 2) + ";" +
- (getWidth() - 2) + "t");
- }
- }
- return;
-
- } // synchronized (emulator)
- }
-
- /**
- * Resize scrollbars for a new width/height.
- */
- @Override
- public void reflowData() {
-
- // Synchronize against the emulator so we don't stomp on its reader
- // thread.
- synchronized (emulator) {
-
- // Pull cursor information
- readEmulatorState();
-
- // Vertical scrollbar
- setTopValue(getHeight() - 2
- - (emulator.getScrollbackBuffer().size()
- + emulator.getDisplayBuffer().size()));
- setVerticalBigChange(getHeight() - 2);
-
- } // synchronized (emulator)
- }
-
- /**
- * Check if a mouse press/release/motion event coordinate is over the
- * emulator.
- *
- * @param mouse a mouse-based event
- * @return whether or not the mouse is on the emulator
- */
- private final boolean mouseOnEmulator(final TMouseEvent mouse) {
-
- synchronized (emulator) {
- if (!emulator.isReading()) {
- return false;
- }
- }
-
- if ((mouse.getAbsoluteX() >= getAbsoluteX() + 1)
- && (mouse.getAbsoluteX() < getAbsoluteX() + getWidth() - 1)
- && (mouse.getAbsoluteY() >= getAbsoluteY() + 1)
- && (mouse.getAbsoluteY() < getAbsoluteY() + getHeight() - 1)
- ) {
- return true;
- }
- return false;
- }
-
- /**
- * Handle keystrokes.
- *
- * @param keypress keystroke event
- */
- @Override
- public void onKeypress(final TKeypressEvent keypress) {
+ @Override
+ public void onKeypress(final TKeypressEvent keypress) {
// Scrollback up/down
if (keypress.equals(kbShiftPgUp)
super.onMouseMotion(mouse);
}
+ // ------------------------------------------------------------------------
+ // TTerminalWindow --------------------------------------------------------
+ // ------------------------------------------------------------------------
+
+ /**
+ * Claim the keystrokes the emulator will need.
+ */
+ private void addShortcutKeys() {
+ addShortcutKeypress(kbCtrlA);
+ addShortcutKeypress(kbCtrlB);
+ addShortcutKeypress(kbCtrlC);
+ addShortcutKeypress(kbCtrlD);
+ addShortcutKeypress(kbCtrlE);
+ addShortcutKeypress(kbCtrlF);
+ addShortcutKeypress(kbCtrlG);
+ addShortcutKeypress(kbCtrlH);
+ addShortcutKeypress(kbCtrlU);
+ addShortcutKeypress(kbCtrlJ);
+ addShortcutKeypress(kbCtrlK);
+ addShortcutKeypress(kbCtrlL);
+ addShortcutKeypress(kbCtrlM);
+ addShortcutKeypress(kbCtrlN);
+ addShortcutKeypress(kbCtrlO);
+ addShortcutKeypress(kbCtrlP);
+ addShortcutKeypress(kbCtrlQ);
+ addShortcutKeypress(kbCtrlR);
+ addShortcutKeypress(kbCtrlS);
+ addShortcutKeypress(kbCtrlT);
+ addShortcutKeypress(kbCtrlU);
+ addShortcutKeypress(kbCtrlV);
+ addShortcutKeypress(kbCtrlW);
+ addShortcutKeypress(kbCtrlX);
+ addShortcutKeypress(kbCtrlY);
+ addShortcutKeypress(kbCtrlZ);
+ addShortcutKeypress(kbF1);
+ addShortcutKeypress(kbF2);
+ addShortcutKeypress(kbF3);
+ addShortcutKeypress(kbF4);
+ addShortcutKeypress(kbF5);
+ addShortcutKeypress(kbF6);
+ addShortcutKeypress(kbF7);
+ addShortcutKeypress(kbF8);
+ addShortcutKeypress(kbF9);
+ addShortcutKeypress(kbF10);
+ addShortcutKeypress(kbF11);
+ addShortcutKeypress(kbF12);
+ addShortcutKeypress(kbAltA);
+ addShortcutKeypress(kbAltB);
+ addShortcutKeypress(kbAltC);
+ addShortcutKeypress(kbAltD);
+ addShortcutKeypress(kbAltE);
+ addShortcutKeypress(kbAltF);
+ addShortcutKeypress(kbAltG);
+ addShortcutKeypress(kbAltH);
+ addShortcutKeypress(kbAltU);
+ addShortcutKeypress(kbAltJ);
+ addShortcutKeypress(kbAltK);
+ addShortcutKeypress(kbAltL);
+ addShortcutKeypress(kbAltM);
+ addShortcutKeypress(kbAltN);
+ addShortcutKeypress(kbAltO);
+ addShortcutKeypress(kbAltP);
+ addShortcutKeypress(kbAltQ);
+ addShortcutKeypress(kbAltR);
+ addShortcutKeypress(kbAltS);
+ addShortcutKeypress(kbAltT);
+ addShortcutKeypress(kbAltU);
+ addShortcutKeypress(kbAltV);
+ addShortcutKeypress(kbAltW);
+ addShortcutKeypress(kbAltX);
+ addShortcutKeypress(kbAltY);
+ addShortcutKeypress(kbAltZ);
+ }
+
+ /**
+ * Convert a string array to a whitespace-separated string.
+ *
+ * @param array the string array
+ * @return a single string
+ */
+ private String stringArrayToString(final String [] array) {
+ StringBuilder sb = new StringBuilder(array[0].length());
+ for (int i = 0; i < array.length; i++) {
+ sb.append(array[i]);
+ if (i < array.length - 1) {
+ sb.append(' ');
+ }
+ }
+ return sb.toString();
+ }
+
+ /**
+ * Spawn the shell.
+ *
+ * @param command the command line to execute
+ */
+ private void spawnShell(final String [] command) {
+
+ /*
+ System.err.printf("spawnShell(): '%s'\n",
+ stringArrayToString(command));
+ */
+
+ vScroller = new TVScroller(this, getWidth() - 2, 0, getHeight() - 2);
+ setBottomValue(0);
+
+ // Assume XTERM
+ ECMA48.DeviceType deviceType = ECMA48.DeviceType.XTERM;
+
+ try {
+ ProcessBuilder pb = new ProcessBuilder(command);
+ Map<String, String> env = pb.environment();
+ env.put("TERM", ECMA48.deviceTypeTerm(deviceType));
+ env.put("LANG", ECMA48.deviceTypeLang(deviceType, "en"));
+ env.put("COLUMNS", "80");
+ env.put("LINES", "24");
+ pb.redirectErrorStream(true);
+ shell = pb.start();
+ emulator = new ECMA48(deviceType, shell.getInputStream(),
+ shell.getOutputStream(), this);
+ } catch (IOException e) {
+ messageBox(i18n.getString("errorLaunchingShellTitle"),
+ MessageFormat.format(i18n.getString("errorLaunchingShellText"),
+ e.getMessage()));
+ }
+
+ // Setup the scroll bars
+ onResize(new TResizeEvent(TResizeEvent.Type.WIDGET, getWidth(),
+ getHeight()));
+
+ // Claim the keystrokes the emulator will need.
+ addShortcutKeys();
+
+ // Add shortcut text
+ newStatusBar(i18n.getString("statusBarRunning"));
+ }
+
+ /**
+ * Terminate the child of the 'script' process used on POSIX. This may
+ * or may not work.
+ */
+ private void terminateShellChildProcess() {
+ int pid = -1;
+ if (shell.getClass().getName().equals("java.lang.UNIXProcess")) {
+ /* get the PID on unix/linux systems */
+ try {
+ Field field = shell.getClass().getDeclaredField("pid");
+ field.setAccessible(true);
+ pid = field.getInt(shell);
+ } catch (Throwable e) {
+ // SQUASH, this didn't work. Just bail out quietly.
+ return;
+ }
+ }
+ if (pid != -1) {
+ // shell.destroy() works successfully at killing this side of
+ // 'script'. But we need to make sure the other side (child
+ // process) is also killed.
+ String [] cmdKillIt = {
+ "pkill", "-P", Integer.toString(pid)
+ };
+ try {
+ Runtime.getRuntime().exec(cmdKillIt);
+ } catch (Throwable e) {
+ // SQUASH, this didn't work. Just bail out quietly.
+ return;
+ }
+ }
+ }
+
+ /**
+ * Called by emulator when fresh data has come in.
+ */
+ public void displayChanged() {
+ getApplication().postEvent(new TMenuEvent(TMenu.MID_REPAINT));
+ }
+
+ /**
+ * Function to call to obtain the display width.
+ *
+ * @return the number of columns in the display
+ */
+ public int getDisplayWidth() {
+ if (ptypipe) {
+ return getWidth() - 2;
+ }
+ return 80;
+ }
+
+ /**
+ * Function to call to obtain the display height.
+ *
+ * @return the number of rows in the display
+ */
+ public int getDisplayHeight() {
+ if (ptypipe) {
+ return getHeight() - 2;
+ }
+ return 24;
+ }
+
+ /**
+ * Hook for subclasses to be notified of the shell termination.
+ */
+ public void onShellExit() {
+ getApplication().postEvent(new TMenuEvent(TMenu.MID_REPAINT));
+ }
+
+ /**
+ * Copy out variables from the emulator that TTerminal has to expose on
+ * screen.
+ */
+ private void readEmulatorState() {
+ // Synchronize against the emulator so we don't stomp on its reader
+ // thread.
+ synchronized (emulator) {
+
+ setCursorX(emulator.getCursorX() + 1);
+ setCursorY(emulator.getCursorY() + 1
+ + (getHeight() - 2 - emulator.getHeight())
+ - getVerticalValue());
+ setCursorVisible(emulator.isCursorVisible());
+ if (getCursorX() > getWidth() - 2) {
+ setCursorVisible(false);
+ }
+ if ((getCursorY() > getHeight() - 2) || (getCursorY() < 0)) {
+ setCursorVisible(false);
+ }
+ if (emulator.getScreenTitle().length() > 0) {
+ // Only update the title if the shell is still alive
+ if (shell != null) {
+ setTitle(emulator.getScreenTitle());
+ }
+ }
+
+ // Check to see if the shell has died.
+ if (!emulator.isReading() && (shell != null)) {
+ try {
+ int rc = shell.exitValue();
+ // The emulator exited on its own, all is fine
+ setTitle(MessageFormat.format(i18n.
+ getString("windowTitleCompleted"), getTitle(), rc));
+ shell = null;
+ emulator.close();
+ clearShortcutKeypresses();
+ statusBar.setText(MessageFormat.format(i18n.
+ getString("statusBarCompleted"), rc));
+ onShellExit();
+ } catch (IllegalThreadStateException e) {
+ // The emulator thread has exited, but the shell Process
+ // hasn't figured that out yet. Do nothing, we will see
+ // this in a future tick.
+ }
+ } else if (emulator.isReading() && (shell != null)) {
+ // The shell might be dead, let's check
+ try {
+ int rc = shell.exitValue();
+ // If we got here, the shell died.
+ setTitle(MessageFormat.format(i18n.
+ getString("windowTitleCompleted"), getTitle(), rc));
+ shell = null;
+ emulator.close();
+ clearShortcutKeypresses();
+ statusBar.setText(MessageFormat.format(i18n.
+ getString("statusBarCompleted"), rc));
+ onShellExit();
+ } catch (IllegalThreadStateException e) {
+ // The shell is still running, do nothing.
+ }
+ }
+
+ } // synchronized (emulator)
+ }
+
+ /**
+ * Check if a mouse press/release/motion event coordinate is over the
+ * emulator.
+ *
+ * @param mouse a mouse-based event
+ * @return whether or not the mouse is on the emulator
+ */
+ private final boolean mouseOnEmulator(final TMouseEvent mouse) {
+
+ synchronized (emulator) {
+ if (!emulator.isReading()) {
+ return false;
+ }
+ }
+
+ if ((mouse.getAbsoluteX() >= getAbsoluteX() + 1)
+ && (mouse.getAbsoluteX() < getAbsoluteX() + getWidth() - 1)
+ && (mouse.getAbsoluteY() >= getAbsoluteY() + 1)
+ && (mouse.getAbsoluteY() < getAbsoluteY() + getHeight() - 1)
+ ) {
+ return true;
+ }
+ return false;
+ }
+
}
import jexer.bits.CellAttributes;
import jexer.event.TKeypressEvent;
import jexer.event.TMouseEvent;
-import static jexer.TKeypress.*;
+import static jexer.TKeypress.kbDown;
+import static jexer.TKeypress.kbEnd;
+import static jexer.TKeypress.kbHome;
+import static jexer.TKeypress.kbLeft;
+import static jexer.TKeypress.kbPgDn;
+import static jexer.TKeypress.kbPgUp;
+import static jexer.TKeypress.kbRight;
+import static jexer.TKeypress.kbUp;
/**
* TText implements a simple scrollable text area. It reflows automatically on
*/
public class TText extends TScrollableWidget {
+ // ------------------------------------------------------------------------
+ // Constants --------------------------------------------------------------
+ // ------------------------------------------------------------------------
+
/**
* Available text justifications.
*/
FULL,
}
+ // ------------------------------------------------------------------------
+ // Variables --------------------------------------------------------------
+ // ------------------------------------------------------------------------
+
/**
* How to justify the text.
*/
*/
private int lineSpacing = 1;
- /**
- * Set the text.
- *
- * @param text new text to display
- */
- public void setText(final String text) {
- this.text = text;
- reflowData();
- }
-
- /**
- * Get the text.
- *
- * @return the text
- */
- public String getText() {
- return text;
- }
-
- /**
- * Convenience method used by TWindowLoggerOutput.
- *
- * @param line new line to add
- */
- public void addLine(final String line) {
- if (text.length() == 0) {
- text = line;
- } else {
- text += "\n\n";
- text += line;
- }
- reflowData();
- }
-
- /**
- * Recompute the bounds for the scrollbars.
- */
- private void computeBounds() {
- maxLineWidth = 0;
- for (String line : lines) {
- if (line.length() > maxLineWidth) {
- maxLineWidth = line.length();
- }
- }
-
- vScroller.setTopValue(0);
- vScroller.setBottomValue((lines.size() - getHeight()) + 1);
- if (vScroller.getBottomValue() < 0) {
- vScroller.setBottomValue(0);
- }
- if (vScroller.getValue() > vScroller.getBottomValue()) {
- vScroller.setValue(vScroller.getBottomValue());
- }
-
- hScroller.setLeftValue(0);
- hScroller.setRightValue((maxLineWidth - getWidth()) + 1);
- if (hScroller.getRightValue() < 0) {
- hScroller.setRightValue(0);
- }
- if (hScroller.getValue() > hScroller.getRightValue()) {
- hScroller.setValue(hScroller.getRightValue());
- }
- }
-
- /**
- * Set justification.
- *
- * @param justification LEFT, CENTER, RIGHT, or FULL
- */
- public void setJustification(final Justification justification) {
- this.justification = justification;
- reflowData();
- }
-
- /**
- * Left-justify the text.
- */
- public void leftJustify() {
- justification = Justification.LEFT;
- reflowData();
- }
-
- /**
- * Center-justify the text.
- */
- public void centerJustify() {
- justification = Justification.CENTER;
- reflowData();
- }
-
- /**
- * Right-justify the text.
- */
- public void rightJustify() {
- justification = Justification.RIGHT;
- reflowData();
- }
-
- /**
- * Fully-justify the text.
- */
- public void fullJustify() {
- justification = Justification.FULL;
- reflowData();
- }
-
- /**
- * Resize text and scrollbars for a new width/height.
- */
- @Override
- public void reflowData() {
- // Reset the lines
- lines.clear();
-
- // Break up text into paragraphs
- String[] paragraphs = text.split("\n\n");
- for (String p : paragraphs) {
- switch (justification) {
- case LEFT:
- lines.addAll(jexer.bits.StringUtils.left(p,
- getWidth() - 1));
- break;
- case CENTER:
- lines.addAll(jexer.bits.StringUtils.center(p,
- getWidth() - 1));
- break;
- case RIGHT:
- lines.addAll(jexer.bits.StringUtils.right(p,
- getWidth() - 1));
- break;
- case FULL:
- lines.addAll(jexer.bits.StringUtils.full(p,
- getWidth() - 1));
- break;
- }
-
- for (int i = 0; i < lineSpacing; i++) {
- lines.add("");
- }
- }
- computeBounds();
- }
+ // ------------------------------------------------------------------------
+ // Constructors -----------------------------------------------------------
+ // ------------------------------------------------------------------------
/**
* Public constructor.
reflowData();
}
+ // ------------------------------------------------------------------------
+ // TScrollableWidget ------------------------------------------------------
+ // ------------------------------------------------------------------------
+
/**
* Draw the text box.
*/
}
}
+ /**
+ * Resize text and scrollbars for a new width/height.
+ */
+ @Override
+ public void reflowData() {
+ // Reset the lines
+ lines.clear();
+
+ // Break up text into paragraphs
+ String[] paragraphs = text.split("\n\n");
+ for (String p : paragraphs) {
+ switch (justification) {
+ case LEFT:
+ lines.addAll(jexer.bits.StringUtils.left(p,
+ getWidth() - 1));
+ break;
+ case CENTER:
+ lines.addAll(jexer.bits.StringUtils.center(p,
+ getWidth() - 1));
+ break;
+ case RIGHT:
+ lines.addAll(jexer.bits.StringUtils.right(p,
+ getWidth() - 1));
+ break;
+ case FULL:
+ lines.addAll(jexer.bits.StringUtils.full(p,
+ getWidth() - 1));
+ break;
+ }
+
+ for (int i = 0; i < lineSpacing; i++) {
+ lines.add("");
+ }
+ }
+ computeBounds();
+ }
+
+ // ------------------------------------------------------------------------
+ // TText ------------------------------------------------------------------
+ // ------------------------------------------------------------------------
+
+ /**
+ * Set the text.
+ *
+ * @param text new text to display
+ */
+ public void setText(final String text) {
+ this.text = text;
+ reflowData();
+ }
+
+ /**
+ * Get the text.
+ *
+ * @return the text
+ */
+ public String getText() {
+ return text;
+ }
+
+ /**
+ * Convenience method used by TWindowLoggerOutput.
+ *
+ * @param line new line to add
+ */
+ public void addLine(final String line) {
+ if (text.length() == 0) {
+ text = line;
+ } else {
+ text += "\n\n";
+ text += line;
+ }
+ reflowData();
+ }
+
+ /**
+ * Recompute the bounds for the scrollbars.
+ */
+ private void computeBounds() {
+ maxLineWidth = 0;
+ for (String line : lines) {
+ if (line.length() > maxLineWidth) {
+ maxLineWidth = line.length();
+ }
+ }
+
+ vScroller.setTopValue(0);
+ vScroller.setBottomValue((lines.size() - getHeight()) + 1);
+ if (vScroller.getBottomValue() < 0) {
+ vScroller.setBottomValue(0);
+ }
+ if (vScroller.getValue() > vScroller.getBottomValue()) {
+ vScroller.setValue(vScroller.getBottomValue());
+ }
+
+ hScroller.setLeftValue(0);
+ hScroller.setRightValue((maxLineWidth - getWidth()) + 1);
+ if (hScroller.getRightValue() < 0) {
+ hScroller.setRightValue(0);
+ }
+ if (hScroller.getValue() > hScroller.getRightValue()) {
+ hScroller.setValue(hScroller.getRightValue());
+ }
+ }
+
+ /**
+ * Set justification.
+ *
+ * @param justification LEFT, CENTER, RIGHT, or FULL
+ */
+ public void setJustification(final Justification justification) {
+ this.justification = justification;
+ reflowData();
+ }
+
+ /**
+ * Left-justify the text.
+ */
+ public void leftJustify() {
+ justification = Justification.LEFT;
+ reflowData();
+ }
+
+ /**
+ * Center-justify the text.
+ */
+ public void centerJustify() {
+ justification = Justification.CENTER;
+ reflowData();
+ }
+
+ /**
+ * Right-justify the text.
+ */
+ public void rightJustify() {
+ justification = Justification.RIGHT;
+ reflowData();
+ }
+
+ /**
+ * Fully-justify the text.
+ */
+ public void fullJustify() {
+ justification = Justification.FULL;
+ reflowData();
+ }
+
}
"This one has both OK and Cancel buttons.\n",
"some input text", TInputBox.Type.OKCANCEL);
getApplication().messageBox("Your InputBox Answer",
- "You entered: " + in.getText());
+ "You entered: " + in.getText() + " and pressed " +
+ in.getResult());
}
}
);
*/
public class TCommandEvent extends TInputEvent {
+ // ------------------------------------------------------------------------
+ // Variables --------------------------------------------------------------
+ // ------------------------------------------------------------------------
+
/**
* Command dispatched.
*/
private TCommand cmd;
- /**
- * Get TCommand.
- *
- * @return the TCommand
- */
- public TCommand getCmd() {
- return cmd;
- }
+ // ------------------------------------------------------------------------
+ // Constructors -----------------------------------------------------------
+ // ------------------------------------------------------------------------
/**
* Public contructor.
this.cmd = cmd;
}
+ // ------------------------------------------------------------------------
+ // TInputEvent ------------------------------------------------------------
+ // ------------------------------------------------------------------------
+
/**
* Comparison check. All fields must match to return true.
*
public String toString() {
return String.format("CommandEvent: %s", cmd.toString());
}
+
+ // ------------------------------------------------------------------------
+ // TCommandEvent ----------------------------------------------------------
+ // ------------------------------------------------------------------------
+
+ /**
+ * Get TCommand.
+ *
+ * @return the TCommand
+ */
+ public TCommand getCmd() {
+ return cmd;
+ }
+
}
*/
public abstract class TInputEvent {
+ // ------------------------------------------------------------------------
+ // Variables --------------------------------------------------------------
+ // ------------------------------------------------------------------------
+
/**
* Time at which event was generated.
*/
private Date time;
+ // ------------------------------------------------------------------------
+ // Constructors -----------------------------------------------------------
+ // ------------------------------------------------------------------------
+
+ /**
+ * Protected contructor.
+ */
+ protected TInputEvent() {
+ // Save the current time
+ time = new Date();
+ }
+
+ // ------------------------------------------------------------------------
+ // TInputEvent ------------------------------------------------------------
+ // ------------------------------------------------------------------------
+
/**
* Get time.
*
return time;
}
- /**
- * Protected contructor.
- */
- protected TInputEvent() {
- // Save the current time
- time = new Date();
- }
}
*/
public class TKeypressEvent extends TInputEvent {
+ // ------------------------------------------------------------------------
+ // Variables --------------------------------------------------------------
+ // ------------------------------------------------------------------------
+
/**
* Keystroke received.
*/
private TKeypress key;
- /**
- * Get keystroke.
- *
- * @return keystroke
- */
- public TKeypress getKey() {
- return key;
- }
+ // ------------------------------------------------------------------------
+ // Constructors -----------------------------------------------------------
+ // ------------------------------------------------------------------------
/**
* Public contructor.
alt, ctrl, shift);
}
- /**
- * Create a duplicate instance.
- *
- * @return duplicate intance
- */
- public TKeypressEvent dup() {
- TKeypressEvent keypress = new TKeypressEvent(key.dup());
- return keypress;
- }
+ // ------------------------------------------------------------------------
+ // TInputEvent ------------------------------------------------------------
+ // ------------------------------------------------------------------------
/**
* Comparison check. All fields must match to return true.
public String toString() {
return String.format("Keypress: %s", key.toString());
}
+
+ // ------------------------------------------------------------------------
+ // TKeypressEvent ---------------------------------------------------------
+ // ------------------------------------------------------------------------
+
+ /**
+ * Get keystroke.
+ *
+ * @return keystroke
+ */
+ public TKeypress getKey() {
+ return key;
+ }
+
+ /**
+ * Create a duplicate instance.
+ *
+ * @return duplicate intance
+ */
+ public TKeypressEvent dup() {
+ TKeypressEvent keypress = new TKeypressEvent(key.dup());
+ return keypress;
+ }
+
}
*/
public class TMenuEvent extends TInputEvent {
+ // ------------------------------------------------------------------------
+ // Variables --------------------------------------------------------------
+ // ------------------------------------------------------------------------
+
/**
* MenuItem ID.
*/
private int id;
- /**
- * Get the MenuItem ID.
- *
- * @return the ID
- */
- public int getId() {
- return id;
- }
+ // ------------------------------------------------------------------------
+ // Constructors -----------------------------------------------------------
+ // ------------------------------------------------------------------------
/**
* Public contructor.
this.id = id;
}
+ // ------------------------------------------------------------------------
+ // TInputEvent ------------------------------------------------------------
+ // ------------------------------------------------------------------------
+
/**
* Make human-readable description of this TMenuEvent.
*
public String toString() {
return String.format("MenuEvent: %d", id);
}
+
+ // ------------------------------------------------------------------------
+ // TMenuEvent -------------------------------------------------------------
+ // ------------------------------------------------------------------------
+
+ /**
+ * Get the MenuItem ID.
+ *
+ * @return the ID
+ */
+ public int getId() {
+ return id;
+ }
+
}
*/
public class TimeoutInputStream extends InputStream {
+ // ------------------------------------------------------------------------
+ // Variables --------------------------------------------------------------
+ // ------------------------------------------------------------------------
+
/**
* The wrapped stream.
*/
*/
private volatile boolean cancel = false;
- /**
- * Request that the current read() operation timeout immediately.
- */
- public synchronized void cancelRead() {
- cancel = true;
- }
+ // ------------------------------------------------------------------------
+ // Constructors -----------------------------------------------------------
+ // ------------------------------------------------------------------------
/**
* Public constructor, at the default timeout of 10000 millis (10
this.timeoutMillis = timeoutMillis;
}
+ // ------------------------------------------------------------------------
+ // InputStream ------------------------------------------------------------
+ // ------------------------------------------------------------------------
+
/**
* Reads the next byte of data from the input stream.
*
"on?");
}
remaining -= rc;
- return rc;
+ if (remaining == 0) {
+ return b.length;
+ }
}
}
"available, but read() returned -1. What is going " +
"on?");
}
- return rc;
+ remaining -= rc;
+ if (remaining == 0) {
+ return len;
+ }
}
}
return stream.skip(n);
}
+ // ------------------------------------------------------------------------
+ // TimeoutInputStream -----------------------------------------------------
+ // ------------------------------------------------------------------------
+
+ /**
+ * Request that the current read() operation timeout immediately.
+ */
+ public synchronized void cancelRead() {
+ cancel = true;
+ }
+
}
*/
public class Document {
+ // ------------------------------------------------------------------------
+ // Variables --------------------------------------------------------------
+ // ------------------------------------------------------------------------
+
/**
* The list of lines.
*/
*/
private Highlighter highlighter = new Highlighter();
+ // ------------------------------------------------------------------------
+ // Constructors -----------------------------------------------------------
+ // ------------------------------------------------------------------------
+
+ /**
+ * Construct a new Document from an existing text string.
+ *
+ * @param str the text string
+ * @param defaultColor the color for unhighlighted text
+ */
+ public Document(final String str, final CellAttributes defaultColor) {
+ this.defaultColor = defaultColor;
+
+ // TODO: set different colors based on file extension
+ highlighter.setJavaColors();
+
+ String [] rawLines = str.split("\n");
+ for (int i = 0; i < rawLines.length; i++) {
+ lines.add(new Line(rawLines[i], this.defaultColor, highlighter));
+ }
+ }
+
+ // ------------------------------------------------------------------------
+ // Document ---------------------------------------------------------------
+ // ------------------------------------------------------------------------
+
/**
* Get the overwrite flag.
*
}
}
- /**
- * Construct a new Document from an existing text string.
- *
- * @param str the text string
- * @param defaultColor the color for unhighlighted text
- */
- public Document(final String str, final CellAttributes defaultColor) {
- this.defaultColor = defaultColor;
-
- // TODO: set different colors based on file extension
- highlighter.setJavaColors();
-
- String [] rawLines = str.split("\n");
- for (int i = 0; i < rawLines.length; i++) {
- lines.add(new Line(rawLines[i], this.defaultColor, highlighter));
- }
- }
-
/**
* Increment the line number by one. If at the last line, do nothing.
*
*/
public class Highlighter {
+ // ------------------------------------------------------------------------
+ // Variables --------------------------------------------------------------
+ // ------------------------------------------------------------------------
+
/**
* The highlighter colors.
*/
private SortedMap<String, CellAttributes> colors;
+ // ------------------------------------------------------------------------
+ // Constructors -----------------------------------------------------------
+ // ------------------------------------------------------------------------
+
/**
* Public constructor sets the theme to the default.
*/
colors = new TreeMap<String, CellAttributes>();
}
+ // ------------------------------------------------------------------------
+ // Highlighter ------------------------------------------------------------
+ // ------------------------------------------------------------------------
+
/**
* See if this is a character that should split a word.
*
}
-
}
*/
public class Line {
+ // ------------------------------------------------------------------------
+ // Variables --------------------------------------------------------------
+ // ------------------------------------------------------------------------
+
/**
* The list of words.
*/
*/
private StringBuilder rawText;
+ // ------------------------------------------------------------------------
+ // Constructors -----------------------------------------------------------
+ // ------------------------------------------------------------------------
+
+ /**
+ * Construct a new Line from an existing text string, and highlight
+ * certain strings.
+ *
+ * @param str the text string
+ * @param defaultColor the color for unhighlighted text
+ * @param highlighter the highlighter to use
+ */
+ public Line(final String str, final CellAttributes defaultColor,
+ final Highlighter highlighter) {
+
+ this.defaultColor = defaultColor;
+ this.highlighter = highlighter;
+ this.rawText = new StringBuilder(str);
+
+ scanLine();
+ }
+
+ /**
+ * Construct a new Line from an existing text string.
+ *
+ * @param str the text string
+ * @param defaultColor the color for unhighlighted text
+ */
+ public Line(final String str, final CellAttributes defaultColor) {
+ this(str, defaultColor, null);
+ }
+
+ // ------------------------------------------------------------------------
+ // Line -------------------------------------------------------------------
+ // ------------------------------------------------------------------------
+
/**
* Get a (shallow) copy of the words in this line.
*
}
}
- /**
- * Construct a new Line from an existing text string, and highlight
- * certain strings.
- *
- * @param str the text string
- * @param defaultColor the color for unhighlighted text
- * @param highlighter the highlighter to use
- */
- public Line(final String str, final CellAttributes defaultColor,
- final Highlighter highlighter) {
-
- this.defaultColor = defaultColor;
- this.highlighter = highlighter;
- this.rawText = new StringBuilder(str);
-
- scanLine();
- }
-
- /**
- * Construct a new Line from an existing text string.
- *
- * @param str the text string
- * @param defaultColor the color for unhighlighted text
- */
- public Line(final String str, final CellAttributes defaultColor) {
- this(str, defaultColor, null);
- }
-
/**
* Decrement the cursor by one. If at the first column, do nothing.
*
*/
public class Word {
+ // ------------------------------------------------------------------------
+ // Variables --------------------------------------------------------------
+ // ------------------------------------------------------------------------
+
/**
* The color to render this word as on screen.
*/
*/
private StringBuilder text = new StringBuilder(3);
+ // ------------------------------------------------------------------------
+ // Constructors -----------------------------------------------------------
+ // ------------------------------------------------------------------------
+
+ /**
+ * Construct a word with one character.
+ *
+ * @param ch the first character of the word
+ * @param defaultColor the color for unhighlighted text
+ * @param highlighter the highlighter to use
+ */
+ public Word(final char ch, final CellAttributes defaultColor,
+ final Highlighter highlighter) {
+
+ this.defaultColor = defaultColor;
+ this.highlighter = highlighter;
+ text.append(ch);
+ }
+
+ /**
+ * Construct a word with an empty string.
+ *
+ * @param defaultColor the color for unhighlighted text
+ * @param highlighter the highlighter to use
+ */
+ public Word(final CellAttributes defaultColor,
+ final Highlighter highlighter) {
+
+ this.defaultColor = defaultColor;
+ this.highlighter = highlighter;
+ }
+
+ // ------------------------------------------------------------------------
+ // Word -------------------------------------------------------------------
+ // ------------------------------------------------------------------------
+
/**
* Get the color used to display this word on screen.
*
return false;
}
- /**
- * Construct a word with one character.
- *
- * @param ch the first character of the word
- * @param defaultColor the color for unhighlighted text
- * @param highlighter the highlighter to use
- */
- public Word(final char ch, final CellAttributes defaultColor,
- final Highlighter highlighter) {
-
- this.defaultColor = defaultColor;
- this.highlighter = highlighter;
- text.append(ch);
- }
-
- /**
- * Construct a word with an empty string.
- *
- * @param defaultColor the color for unhighlighted text
- * @param highlighter the highlighter to use
- */
- public Word(final CellAttributes defaultColor,
- final Highlighter highlighter) {
-
- this.defaultColor = defaultColor;
- this.highlighter = highlighter;
- }
-
/**
* Perform highlighting.
*/