*/
package jexer;
+import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import jexer.bits.CellAttributes;
import jexer.event.TKeypressEvent;
import jexer.event.TMenuEvent;
+import jexer.event.TMouseEvent;
import jexer.event.TResizeEvent;
import jexer.menu.TMenu;
import static jexer.TKeypress.*;
// Event handlers -----------------------------------------------------
// --------------------------------------------------------------------
+ /**
+ * Handle mouse double-click events.
+ *
+ * @param mouse mouse double-click event
+ */
+ @Override
+ public void onMouseDoubleClick(final TMouseEvent mouse) {
+ // Use TWidget's code to pass the event to the children.
+ super.onMouseDown(mouse);
+
+ // Double-click means to start editing.
+ fieldText = field.getText();
+ isEditing = true;
+ field.setEnabled(true);
+ activate(field);
+
+ if (isActive()) {
+ // Let the table know that I was activated.
+ ((TTableWidget) getParent()).selectedRow = row;
+ ((TTableWidget) getParent()).selectedColumn = column;
+ ((TTableWidget) getParent()).alignGrid();
+ }
+ }
+
+ /**
+ * Handle mouse press events.
+ *
+ * @param mouse mouse button press event
+ */
+ @Override
+ public void onMouseDown(final TMouseEvent mouse) {
+ // Use TWidget's code to pass the event to the children.
+ super.onMouseDown(mouse);
+
+ if (isActive()) {
+ // Let the table know that I was activated.
+ ((TTableWidget) getParent()).selectedRow = row;
+ ((TTableWidget) getParent()).selectedColumn = column;
+ ((TTableWidget) getParent()).alignGrid();
+ }
+ }
+
+ /**
+ * Handle mouse release events.
+ *
+ * @param mouse mouse button release event
+ */
+ @Override
+ public void onMouseUp(final TMouseEvent mouse) {
+ // Use TWidget's code to pass the event to the children.
+ super.onMouseDown(mouse);
+
+ if (isActive()) {
+ // Let the table know that I was activated.
+ ((TTableWidget) getParent()).selectedRow = row;
+ ((TTableWidget) getParent()).selectedColumn = column;
+ ((TTableWidget) getParent()).alignGrid();
+ }
+ }
+
/**
* Handle keystrokes.
*
// Event handlers ---------------------------------------------------------
// ------------------------------------------------------------------------
+ /**
+ * Handle mouse press events.
+ *
+ * @param mouse mouse button press event
+ */
+ @Override
+ public void onMouseDown(final TMouseEvent mouse) {
+ if (mouse.isMouseWheelUp() || mouse.isMouseWheelDown()) {
+ // Treat wheel up/down as 3 up/down
+ TKeypressEvent keyEvent;
+ if (mouse.isMouseWheelUp()) {
+ keyEvent = new TKeypressEvent(kbUp);
+ } else {
+ keyEvent = new TKeypressEvent(kbDown);
+ }
+ for (int i = 0; i < 3; i++) {
+ onKeypress(keyEvent);
+ }
+ return;
+ }
+
+ // Use TWidget's code to pass the event to the children.
+ super.onMouseDown(mouse);
+ }
+
/**
* Handle keystrokes.
*
}
if (keypress.equals(kbLeft)) {
+ // Left
if (selectedColumn > 0) {
selectedColumn--;
}
activate(columns.get(selectedColumn).get(selectedRow));
} else if (keypress.equals(kbRight)) {
+ // Right
if (selectedColumn < columns.size() - 1) {
selectedColumn++;
}
activate(columns.get(selectedColumn).get(selectedRow));
} else if (keypress.equals(kbUp)) {
+ // Up
if (selectedRow > 0) {
selectedRow--;
}
activate(columns.get(selectedColumn).get(selectedRow));
} else if (keypress.equals(kbDown)) {
+ // Down
if (selectedRow < rows.size() - 1) {
selectedRow++;
}
activate(columns.get(selectedColumn).get(selectedRow));
} else if (keypress.equals(kbHome)) {
+ // Home - leftmost column
selectedColumn = 0;
activate(columns.get(selectedColumn).get(selectedRow));
} else if (keypress.equals(kbEnd)) {
+ // End - rightmost column
selectedColumn = columns.size() - 1;
activate(columns.get(selectedColumn).get(selectedRow));
} else if (keypress.equals(kbPgUp)) {
- // TODO
+ // PgUp - Treat like multiple up
+ for (int i = 0; i < getHeight() - 2; i++) {
+ if (selectedRow > 0) {
+ selectedRow--;
+ }
+ }
+ activate(columns.get(selectedColumn).get(selectedRow));
} else if (keypress.equals(kbPgDn)) {
- // TODO
+ // PgDn - Treat like multiple up
+ for (int i = 0; i < getHeight() - 2; i++) {
+ if (selectedRow < rows.size() - 1) {
+ selectedRow++;
+ }
+ }
+ activate(columns.get(selectedColumn).get(selectedRow));
} else if (keypress.equals(kbCtrlHome)) {
- // TODO
+ // Ctrl-Home - go to top-left
+ selectedRow = 0;
+ selectedColumn = 0;
+ activate(columns.get(selectedColumn).get(selectedRow));
+ activate(columns.get(selectedColumn).get(selectedRow));
} else if (keypress.equals(kbCtrlEnd)) {
- // TODO
+ // Ctrl-End - go to bottom-right
+ selectedRow = rows.size() - 1;
+ selectedColumn = columns.size() - 1;
+ activate(columns.get(selectedColumn).get(selectedRow));
+ activate(columns.get(selectedColumn).get(selectedRow));
} else {
// Pass to the Cell.
super.onKeypress(keypress);
columns.get(selectedColumn).width--;
for (Cell cell: getSelectedColumn().cells) {
cell.setWidth(columns.get(selectedColumn).width);
- cell.field.setWidth(columns.get(selectedColumn).width - 1);
+ cell.field.setWidth(columns.get(selectedColumn).width);
}
for (int i = selectedColumn + 1; i < columns.size(); i++) {
for (Cell cell: columns.get(i).cells) {
columns.get(selectedColumn).width++;
for (Cell cell: getSelectedColumn().cells) {
cell.setWidth(columns.get(selectedColumn).width);
- cell.field.setWidth(columns.get(selectedColumn).width - 1);
+ cell.field.setWidth(columns.get(selectedColumn).width);
}
for (int i = selectedColumn + 1; i < columns.size(); i++) {
for (Cell cell: columns.get(i).cells) {
alignGrid();
break;
case TMenu.MID_TABLE_FILE_SAVE_CSV:
+ // TODO
+ break;
case TMenu.MID_TABLE_FILE_SAVE_TEXT:
+ // TODO
break;
default:
super.onMenu(menu);
break;
}
putStringXY(columns.get(i).get(top).getX(), 0,
- String.format(" %-6s ", columns.get(i).label),
+ String.format(" %-" +
+ (columns.get(i).get(top).getWidth() - 2)
+ + "s ", columns.get(i).label),
(i == selectedColumn ? labelColorSelected : labelColor));
}
}
return rows.get(selectedRow);
}
+ /**
+ * Get the currently-selected column number. 0 is the left-most column.
+ *
+ * @return the selected column number
+ */
+ public int getSelectedColumnNumber() {
+ return selectedColumn;
+ }
+
+ /**
+ * Set the currently-selected column number. 0 is the left-most column.
+ *
+ * @param column the column number to select
+ */
+ public void setSelectedColumnNumber(final int column) {
+ if ((column < 0) || (column > columns.size() - 1)) {
+ throw new IndexOutOfBoundsException("Column count is " +
+ columns.size() + ", requested index " + column);
+ }
+ selectedColumn = column;
+ activate(columns.get(selectedColumn).get(selectedRow));
+ alignGrid();
+ }
+
+ /**
+ * Get the currently-selected row number. 0 is the top-most row.
+ *
+ * @return the selected row number
+ */
+ public int getSelectedRowNumber() {
+ return selectedRow;
+ }
+
+ /**
+ * Set the currently-selected row number. 0 is the left-most column.
+ *
+ * @param row the row number to select
+ */
+ public void setSelectedRowNumber(final int row) {
+ if ((row < 0) || (row > rows.size() - 1)) {
+ throw new IndexOutOfBoundsException("Row count is " +
+ rows.size() + ", requested index " + row);
+ }
+ selectedRow = row;
+ activate(columns.get(selectedColumn).get(selectedRow));
+ alignGrid();
+ }
+
+ /**
+ * Get the number of columns.
+ *
+ * @return the number of columns
+ */
+ public int getColumnCount() {
+ return columns.size();
+ }
+
+ /**
+ * Get the number of rows.
+ *
+ * @return the number of rows
+ */
+ public int getRowCount() {
+ return rows.size();
+ }
+
/**
* Get the full horizontal width of this table.
*
* @return the width required to render the entire table
*/
- public int getMaximumWidth() {
+ private int getMaximumWidth() {
int totalWidth = 0;
if (showRowLabels == true) {
// For now, all row labels are 8 cells wide. TODO: make this
*
* @return the height required to render the entire table
*/
- public int getMaximumHeight() {
+ private int getMaximumHeight() {
int totalHeight = 0;
if (showColumnLabels == true) {
// For now, all column labels are 1 cell tall. TODO: make this
*/
for (int x = 0; x < columns.size(); x++) {
for (int y = 0; y < rows.size(); y++) {
- rows.get(y).cells.get(x).setVisible(true);
+ Cell cell = rows.get(y).cells.get(x);
+ cell.setVisible(true);
+
+ // Special case: mouse double-clicks can lead to multiple
+ // cells in editing mode. Only allow a cell to remain
+ // editing if it is fact the active widget.
+ if (cell.isEditing && !cell.isActive()) {
+ cell.fieldText = cell.field.getText();
+ cell.isEditing = false;
+ cell.field.setEnabled(false);
+ }
}
}
left = selectedColumn;
for (int x = selectedColumn - 1; x >= 0; x--) {
newCellX -= rows.get(y).cells.get(x).getWidth() + 1;
- if (newCellX - rows.get(y).cells.get(x).getWidth() + 1 >= 0) {
+ if (newCellX >= (showRowLabels ? 8 : 0)) {
rows.get(y).cells.get(x).setVisible(true);
rows.get(y).cells.get(x).setX(newCellX);
left--;
}
+ /**
+ * Save contents to file.
+ *
+ * @param filename file to save to
+ * @throws IOException if a java.io operation throws
+ */
+ public void saveToFilename(final String filename) throws IOException {
+ // TODO
+ }
+
}
*/
package jexer;
+import java.io.IOException;
+import java.text.MessageFormat;
import java.util.ResourceBundle;
import jexer.event.TCommandEvent;
// TWindow ----------------------------------------------------------------
// ------------------------------------------------------------------------
- /**
- * Draw the window.
- */
- @Override
- public void draw() {
- // Draw as normal.
- super.draw();
-
- // Add borders on rows and columns.
- // TODO
- }
-
/**
* Handle mouse press events.
*
if (mouseOnTable(mouse)) {
// The table might have changed, update the scollbars.
- // TODO
- /*
- setBottomValue(editField.getMaximumRowNumber());
- setVerticalValue(editField.getVisibleRowNumber());
- setRightValue(editField.getMaximumColumnNumber());
- setHorizontalValue(editField.getEditingColumnNumber());
- */
- } else {
- if (mouse.isMouseWheelUp() || mouse.isMouseWheelDown()) {
- // Vertical scrollbar actions
- // TODO
- // editField.setVisibleRowNumber(getVerticalValue());
- }
+ setBottomValue(tableField.getRowCount() - 1);
+ setVerticalValue(tableField.getSelectedRowNumber());
+ setRightValue(tableField.getColumnCount() - 1);
+ setHorizontalValue(tableField.getSelectedColumnNumber());
}
}
super.onMouseUp(mouse);
if (mouse.isMouse1() && mouseOnVerticalScroller(mouse)) {
- // Clicked on vertical scrollbar
- // TODO
- // editField.setVisibleRowNumber(getVerticalValue());
+ // Clicked/dragged on vertical scrollbar.
+ tableField.setSelectedRowNumber(getVerticalValue());
+ }
+ if (mouse.isMouse1() && mouseOnHorizontalScroller(mouse)) {
+ // Clicked/dragged on horizontal scrollbar.
+ tableField.setSelectedColumnNumber(getHorizontalValue());
}
-
- // TODO: horizontal scrolling
}
/**
super.onMouseMotion(mouse);
if (mouseOnTable(mouse) && mouse.isMouse1()) {
- // The editor might have changed, update the scollbars.
- // TODO
- /*
- setBottomValue(editField.getMaximumRowNumber());
- setVerticalValue(editField.getVisibleRowNumber());
- setRightValue(editField.getMaximumColumnNumber());
- setHorizontalValue(editField.getEditingColumnNumber());
- */
+ // The table might have changed, update the scollbars.
+ setBottomValue(tableField.getRowCount() - 1);
+ setVerticalValue(tableField.getSelectedRowNumber());
+ setRightValue(tableField.getColumnCount() - 1);
+ setHorizontalValue(tableField.getSelectedColumnNumber());
} else {
if (mouse.isMouse1() && mouseOnVerticalScroller(mouse)) {
- // Clicked/dragged on vertical scrollbar
- // TODO
- // editField.setVisibleRowNumber(getVerticalValue());
+ // Clicked/dragged on vertical scrollbar.
+ tableField.setSelectedRowNumber(getVerticalValue());
+ }
+ if (mouse.isMouse1() && mouseOnHorizontalScroller(mouse)) {
+ // Clicked/dragged on horizontal scrollbar.
+ tableField.setSelectedColumnNumber(getHorizontalValue());
}
-
- // TODO: horizontal scrolling
}
}
// Use TWidget's code to pass the event to the children.
super.onKeypress(keypress);
- // The editor might have changed, update the scollbars.
- // TODO
- /*
- setBottomValue(editField.getMaximumRowNumber());
- setVerticalValue(editField.getVisibleRowNumber());
- setRightValue(editField.getMaximumColumnNumber());
- setHorizontalValue(editField.getEditingColumnNumber());
- */
+ // The table might have changed, update the scollbars.
+ setBottomValue(tableField.getRowCount() - 1);
+ setVerticalValue(tableField.getSelectedRowNumber());
+ setRightValue(tableField.getColumnCount() - 1);
+ setHorizontalValue(tableField.getSelectedColumnNumber());
}
/**
}
}
+ /**
+ * Method that subclasses can override to handle posted command events.
+ *
+ * @param command command event
+ */
+ @Override
+ public void onCommand(final TCommandEvent command) {
+ if (command.equals(cmOpen)) {
+ try {
+ String filename = fileOpenBox(".");
+ if (filename != null) {
+ try {
+ // TODO
+ if (false) {
+ tableField.saveToFilename(filename);
+ }
+ } catch (IOException e) {
+ messageBox(i18n.getString("errorDialogTitle"),
+ MessageFormat.format(i18n.
+ getString("errorReadingFile"), e.getMessage()));
+ }
+ }
+ } catch (IOException e) {
+ messageBox(i18n.getString("errorDialogTitle"),
+ MessageFormat.format(i18n.
+ getString("errorOpeningFileDialog"), e.getMessage()));
+ }
+ return;
+ }
+
+ if (command.equals(cmSave)) {
+ try {
+ String filename = fileSaveBox(".");
+ if (filename != null) {
+ tableField.saveToFilename(filename);
+ }
+ } catch (IOException e) {
+ messageBox(i18n.getString("errorDialogTitle"),
+ MessageFormat.format(i18n.
+ getString("errorSavingFile"), e.getMessage()));
+ }
+ return;
+ }
+
+ // Didn't handle it, let children get it instead
+ super.onCommand(command);
+ }
+
// ------------------------------------------------------------------------
// TTableWindow -----------------------------------------------------------
// ------------------------------------------------------------------------
vScroller = new TVScroller(this, getWidth() - 2, 0, getHeight() - 2);
setMinimumWindowWidth(25);
setMinimumWindowHeight(10);
- setTopValue(1);
- // setBottomValue(editField.getMaximumRowNumber());
- setLeftValue(1);
- setRightValue(tableField.getMaximumWidth());
+ setTopValue(tableField.getSelectedRowNumber());
+ setBottomValue(tableField.getRowCount() - 1);
+ setLeftValue(tableField.getSelectedColumnNumber());
+ setRightValue(tableField.getColumnCount() - 1);
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"));
}