import jexer.TAction;
import jexer.TApplication;
import jexer.TDesktop;
+import jexer.TPanel;
import jexer.TTerminalWidget;
+import jexer.TSplitPane;
import jexer.TWidget;
import jexer.event.TKeypressEvent;
import jexer.event.TMenuEvent;
-import jexer.event.TMouseEvent;
-import jexer.event.TResizeEvent;
import jexer.menu.TMenu;
/**
* Handle to the root widget.
*/
private TWidget root = null;
-
+
/**
* Main entry point.
*/
tileMenu.addDefaultItem(TMenu.MID_EXIT);
// Spin up the root terminal
- root = new TTerminalWidget(getDesktop(), 0, 0,
- getDesktop().getWidth(), getDesktop().getHeight(),
- new TAction() {
- public void DO() {
- // TODO: if root's parent is TSplitPane, call
- // TSplitPane.removeSplit(TWidget).
- if (root != null) {
- root.remove();
- }
- }
- });
+ createRootTerminal();
}
/**
@Override
protected boolean onMenu(TMenuEvent event) {
if (event.getId() == MENU_SPLIT_VERTICAL) {
- splitVertical();
+ if (root == null) {
+ createRootTerminal();
+ return true;
+ }
+ TWidget active = root.getActiveChild();
+ TSplitPane split = active.splitVertical(false,
+ new TTerminalWidget(getDesktop(), active.getX(),
+ active.getY(), active.getWidth(), active.getHeight(),
+ new TAction() {
+ public void DO() {
+ // TODO
+ }
+ }));
+ if (active == root) {
+ root = split;
+ }
return true;
}
if (event.getId() == MENU_SPLIT_HORIZONTAL) {
- splitHorizontal();
+ if (root == null) {
+ createRootTerminal();
+ return true;
+ }
+ TWidget active = root.getActiveChild();
+ TSplitPane split = active.splitHorizontal(false,
+ new TTerminalWidget(getDesktop(), active.getX(),
+ active.getY(), active.getWidth(), active.getHeight(),
+ new TAction() {
+ public void DO() {
+ // TODO
+ }
+ }));
return true;
}
}
/**
- * Perform the vertical split.
+ * Create the root terminal.
*/
- private void splitVertical() {
- // TODO
- }
-
- /**
- * Perform the horizontal split.
- */
- private void splitHorizontal() {
- // TODO
+ private void createRootTerminal() {
+ assert (root == null);
+ root = new TTerminalWidget(getDesktop(), 0, 0,
+ getDesktop().getWidth(), getDesktop().getHeight(),
+ new TAction() {
+ public void DO() {
+ TWidget target = (TWidget) data;
+ if (target.getParent() instanceof TPanel) {
+ ((TSplitPane) target.getParent().getParent()).removeSplit(target, true);
+ } else {
+ assert (root != null);
+ root.remove();
+ root = null;
+ }
+ }
+ });
}
}
layoutChildren();
}
+ /**
+ * Remove this split, removing the widget specified.
+ *
+ * @param widgetToRemove the widget to remove
+ * @param doClose if true, call the close() method before removing the
+ * child
+ * @return the pane that remains, or null if nothing is retained
+ */
+ public TWidget removeSplit(final TWidget widgetToRemove,
+ final boolean doClose) {
+
+ TWidget keep = null;
+ if (vertical) {
+ if ((widgetToRemove != left) && (widgetToRemove != right)) {
+ throw new IllegalArgumentException("widget to remove is not " +
+ "either of the panes in this splitpane");
+ }
+ if (widgetToRemove == left) {
+ keep = right;
+ } else {
+ keep = left;
+ }
+
+ } else {
+ if ((widgetToRemove != top) && (widgetToRemove != bottom)) {
+ throw new IllegalArgumentException("widget to remove is not " +
+ "either of the panes in this splitpane");
+ }
+ if (widgetToRemove == top) {
+ keep = bottom;
+ } else {
+ keep = top;
+ }
+ }
+
+ // Remove me from my parent widget.
+ TWidget newParent = getParent();
+ setParent(null, false);
+
+ if (keep == null) {
+ // Nothing is left of either pane. Remove me and bail out.
+ return null;
+ }
+
+ keep.setParent(newParent, false);
+ keep.setDimensions(getX(), getY(), getWidth(), getHeight());
+ keep.onResize(new TResizeEvent(TResizeEvent.Type.WIDGET, getWidth(),
+ getHeight()));
+
+ return keep;
+ }
+
}
* @param menu menu event
*/
public void onMenu(final TMenuEvent menu) {
-
- // Special case: if a split command comes in, insert a TPanel and
- // TSplitPane in the hierarchy here.
- TPanel panel = null;
- TSplitPane pane = null;
- List<TWidget> widgets = null;
- switch (menu.getId()) {
- case TMenu.MID_SPLIT_VERTICAL:
- if (children.size() == 0) {
- break;
- }
- panel = new TPanel(null, x, y, width, height);
- pane = new TSplitPane(null, x, y, width, height, true);
- widgets = new ArrayList<TWidget>(children);
- for (TWidget w: widgets) {
- w.setParent(panel, false);
- }
- children.clear();
- pane.setParent(this, false);
- pane.setLeft(panel);
- activate(pane);
- for (TWidget w: widgets) {
- assert (w.window != null);
- assert (w.parent != null);
- }
- assert (pane.getWindow() != null);
- assert (pane.getParent() != null);
- assert (panel.getWindow() != null);
- assert (panel.getParent() != null);
- assert (pane.isActive() == true);
- assert (panel.isActive() == true);
- return;
- case TMenu.MID_SPLIT_HORIZONTAL:
- if (children.size() == 0) {
- break;
- }
- panel = new TPanel(null, x, y, width, height);
- pane = new TSplitPane(null, x, y, width, height, false);
- widgets = new ArrayList<TWidget>(children);
- for (TWidget w: widgets) {
- w.setParent(panel, false);
- }
- children.clear();
- pane.setParent(this, false);
- pane.setTop(panel);
- activate(pane);
- for (TWidget w: widgets) {
- assert (w.window != null);
- assert (w.parent != null);
- }
- assert (pane.getWindow() != null);
- assert (pane.getParent() != null);
- assert (panel.getWindow() != null);
- assert (panel.getParent() != null);
- assert (pane.isActive() == true);
- assert (panel.isActive() == true);
- return;
- default:
- break;
- }
-
// Default: do nothing, pass to children instead
for (TWidget widget: children) {
widget.onMenu(menu);
return this;
}
+ /**
+ * Split this widget into two, putting all of this widget's children into
+ * a new TPanel, and returning a new TSplitPane with that panel on the
+ * left or right pane.
+ *
+ * @param newWidgetOnLeft if true, the new widget (if specified) will be
+ * on the left pane, and this widget's children will be placed on the
+ * right pane
+ * @param newWidget the new widget to add to the other pane, or null
+ * @return the new split pane widget
+ */
+ public TSplitPane splitVertical(final boolean newWidgetOnLeft,
+ final TWidget newWidget) {
+
+ TPanel panel = new TPanel(null, x, y, width, height);
+ TSplitPane splitPane = new TSplitPane(null, x, y, width, height, true);
+ List<TWidget> widgets = new ArrayList<TWidget>(children);
+ for (TWidget w: widgets) {
+ w.setParent(panel, false);
+ }
+ children.clear();
+ splitPane.setParent(parent, false);
+ parent = null;
+ window = null;
+ if (newWidgetOnLeft) {
+ splitPane.setLeft(newWidget);
+ splitPane.setRight(panel);
+ } else {
+ splitPane.setRight(newWidget);
+ splitPane.setLeft(panel);
+ }
+ activate(splitPane);
+ for (TWidget w: widgets) {
+ assert (w.window != null);
+ assert (w.parent != null);
+ }
+ assert (splitPane.getWindow() != null);
+ assert (splitPane.getParent() != null);
+ assert (panel.getWindow() != null);
+ assert (panel.getParent() != null);
+ assert (splitPane.isActive() == true);
+ assert (panel.isActive() == true);
+ return splitPane;
+ }
+
+ /**
+ * Split this widget into two, putting all of this widget's children into
+ * a new TPanel, and returning a new TSplitPane with that panel on the
+ * top or bottom pane.
+ *
+ * @param newWidgetOnTop if true, the new widget (if specified) will be
+ * on the top pane, and this widget's children will be placed on the
+ * bottom pane
+ * @param newWidget the new widget to add to the other pane, or null
+ * @return the new split pane widget
+ */
+ public TSplitPane splitHorizontal(final boolean newWidgetOnTop,
+ final TWidget newWidget) {
+
+ TPanel panel = new TPanel(null, x, y, width, height);
+ TSplitPane splitPane = new TSplitPane(null, x, y, width, height, false);
+ List<TWidget> widgets = new ArrayList<TWidget>(children);
+ for (TWidget w: widgets) {
+ w.setParent(panel, false);
+ }
+ children.clear();
+ splitPane.setParent(parent, false);
+ parent = null;
+ splitPane.setTop(panel);
+ parent = null;
+ window = null;
+ if (newWidgetOnTop) {
+ splitPane.setTop(newWidget);
+ splitPane.setBottom(panel);
+ } else {
+ splitPane.setBottom(newWidget);
+ splitPane.setTop(panel);
+ }
+ activate(splitPane);
+ for (TWidget w: widgets) {
+ assert (w.window != null);
+ assert (w.parent != null);
+ }
+ assert (splitPane.getWindow() != null);
+ assert (splitPane.getParent() != null);
+ assert (panel.getWindow() != null);
+ assert (panel.getParent() != null);
+ assert (splitPane.isActive() == true);
+ assert (panel.isActive() == true);
+ return splitPane;
+ }
+
// ------------------------------------------------------------------------
// Passthru for Screen functions ------------------------------------------
// ------------------------------------------------------------------------