From: Kevin Lamonte Date: Wed, 21 Aug 2019 01:00:12 +0000 (-0500) Subject: #51 wip X-Git-Url: https://git.nikiroo.be/?a=commitdiff_plain;h=a524aa2e24b0ddeb5aa8105f1bed1c1c7b75e1c7;p=nikiroo-utils.git #51 wip --- diff --git a/examples/JexerTilingWindowManager2.java b/examples/JexerTilingWindowManager2.java index bef3637..cb7f0d0 100644 --- a/examples/JexerTilingWindowManager2.java +++ b/examples/JexerTilingWindowManager2.java @@ -97,9 +97,16 @@ public class JexerTilingWindowManager2 extends TApplication { active.getY(), active.getWidth(), active.getHeight(), new TAction() { public void DO() { - // TODO + if (source.getParent() instanceof TSplitPane) { + ((TSplitPane) source.getParent()).removeSplit(source, true); + } else if (source == root) { + assert (root != null); + root.remove(); + root = null; + } } })); + if (active == root) { root = split; } @@ -116,7 +123,13 @@ public class JexerTilingWindowManager2 extends TApplication { active.getY(), active.getWidth(), active.getHeight(), new TAction() { public void DO() { - // TODO + if (source.getParent() instanceof TSplitPane) { + ((TSplitPane) source.getParent()).removeSplit(source, true); + } else if (source == root) { + assert (root != null); + root.remove(); + root = null; + } } })); return true; @@ -134,10 +147,9 @@ public class JexerTilingWindowManager2 extends TApplication { 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 { + if (source.getParent() instanceof TSplitPane) { + ((TSplitPane) source.getParent()).removeSplit(source, true); + } else if (source == root) { assert (root != null); root.remove(); root = null; diff --git a/src/jexer/TAction.java b/src/jexer/TAction.java index bce0064..5343143 100644 --- a/src/jexer/TAction.java +++ b/src/jexer/TAction.java @@ -36,11 +36,38 @@ package jexer; public abstract class TAction { /** - * An optional bit of data associated with this action. Widgets that use - * this field are responsible for setting it. + * The widget that called this action's DO() method. Note that this + * field could be null, for example if executed as a timer action. + */ + public TWidget source; + + /** + * An optional bit of data associated with this action. */ public Object data; + /** + * Call DO() with source widget set. + * + * @param source the source widget + */ + public final void DO(final TWidget source) { + this.source = source; + DO(); + } + + /** + * Call DO() with source widget and data set. + * + * @param source the source widget + * @param data the data + */ + public final void DO(final TWidget source, final Object data) { + this.source = source; + this.data = data; + DO(); + } + /** * Various classes will call DO() when they are clicked/selected. */ diff --git a/src/jexer/TButton.java b/src/jexer/TButton.java index 3938c73..d86fa44 100644 --- a/src/jexer/TButton.java +++ b/src/jexer/TButton.java @@ -308,7 +308,7 @@ public class TButton extends TWidget { */ public void dispatch() { if (action != null) { - action.DO(); + action.DO(this); inButtonPress = false; } } diff --git a/src/jexer/TCalendar.java b/src/jexer/TCalendar.java index 580fb9f..c2005cc 100644 --- a/src/jexer/TCalendar.java +++ b/src/jexer/TCalendar.java @@ -161,7 +161,7 @@ public class TCalendar extends TWidget { @Override public void onMouseDoubleClick(final TMouseEvent mouse) { if (updateAction != null) { - updateAction.DO(); + updateAction.DO(this); } } @@ -184,7 +184,7 @@ public class TCalendar extends TWidget { increment = 1; } else if (keypress.equals(kbEnter)) { if (updateAction != null) { - updateAction.DO(); + updateAction.DO(this); } return; } else { diff --git a/src/jexer/TComboBox.java b/src/jexer/TComboBox.java index 2d1b2c0..b64dbde 100644 --- a/src/jexer/TComboBox.java +++ b/src/jexer/TComboBox.java @@ -119,7 +119,7 @@ public class TComboBox extends TWidget { TComboBox.this.activate(field); } if (updateAction != null) { - updateAction.DO(); + updateAction.DO(TComboBox.this); } } } diff --git a/src/jexer/TField.java b/src/jexer/TField.java index 1a706b1..7c8b5bc 100644 --- a/src/jexer/TField.java +++ b/src/jexer/TField.java @@ -479,11 +479,11 @@ public class TField extends TWidget { protected void dispatch(final boolean enter) { if (enter) { if (enterAction != null) { - enterAction.DO(); + enterAction.DO(this); } } else { if (updateAction != null) { - updateAction.DO(); + updateAction.DO(this); } } } diff --git a/src/jexer/TImage.java b/src/jexer/TImage.java index 3aaca2f..cd0ce96 100644 --- a/src/jexer/TImage.java +++ b/src/jexer/TImage.java @@ -216,7 +216,7 @@ public class TImage extends TWidget { @Override public void onMouseDown(final TMouseEvent mouse) { if (clickAction != null) { - clickAction.DO(); + clickAction.DO(this); return; } } diff --git a/src/jexer/TLabel.java b/src/jexer/TLabel.java index 0c62250..f34a533 100644 --- a/src/jexer/TLabel.java +++ b/src/jexer/TLabel.java @@ -257,7 +257,7 @@ public class TLabel extends TWidget { */ public void dispatch() { if (action != null) { - action.DO(); + action.DO(this); } } diff --git a/src/jexer/TList.java b/src/jexer/TList.java index 253e409..38a994c 100644 --- a/src/jexer/TList.java +++ b/src/jexer/TList.java @@ -507,7 +507,7 @@ public class TList extends TScrollableWidget { assert (selectedString >= 0); assert (selectedString < strings.size()); if (enterAction != null) { - enterAction.DO(); + enterAction.DO(this); } } @@ -518,7 +518,7 @@ public class TList extends TScrollableWidget { assert (selectedString >= 0); assert (selectedString < strings.size()); if (moveAction != null) { - moveAction.DO(); + moveAction.DO(this); } } @@ -529,7 +529,7 @@ public class TList extends TScrollableWidget { assert (selectedString >= 0); assert (selectedString < strings.size()); if (singleClickAction != null) { - singleClickAction.DO(); + singleClickAction.DO(this); } } diff --git a/src/jexer/TSpinner.java b/src/jexer/TSpinner.java index 4748486..61fac65 100644 --- a/src/jexer/TSpinner.java +++ b/src/jexer/TSpinner.java @@ -175,7 +175,7 @@ public class TSpinner extends TWidget { */ private void up() { if (upAction != null) { - upAction.DO(); + upAction.DO(this); } } @@ -184,7 +184,7 @@ public class TSpinner extends TWidget { */ private void down() { if (downAction != null) { - downAction.DO(); + downAction.DO(this); } } diff --git a/src/jexer/TSplitPane.java b/src/jexer/TSplitPane.java index 277f080..b63ea1f 100644 --- a/src/jexer/TSplitPane.java +++ b/src/jexer/TSplitPane.java @@ -275,7 +275,9 @@ public class TSplitPane extends TWidget { "horizontal split pane"); } if (left == null) { - remove(this.left); + if (this.left != null) { + remove(this.left); + } this.left = null; return; } @@ -305,7 +307,9 @@ public class TSplitPane extends TWidget { "horizontal split pane"); } if (right == null) { - remove(this.right); + if (this.right != null) { + remove(this.right); + } this.right = null; return; } @@ -335,7 +339,9 @@ public class TSplitPane extends TWidget { "split pane"); } if (top == null) { - remove(this.top); + if (this.top != null) { + remove(this.top); + } this.top = null; return; } @@ -365,7 +371,9 @@ public class TSplitPane extends TWidget { "vertical split pane"); } if (bottom == null) { - remove(this.bottom); + if (this.bottom != null) { + remove(this.bottom); + } this.bottom = null; return; } @@ -455,7 +463,7 @@ public class TSplitPane extends TWidget { // Remove me from my parent widget. TWidget newParent = getParent(); - setParent(null, false); + remove(false); if (keep == null) { // Nothing is left of either pane. Remove me and bail out. diff --git a/src/jexer/TTerminalWidget.java b/src/jexer/TTerminalWidget.java index db405cf..7a93f4e 100644 --- a/src/jexer/TTerminalWidget.java +++ b/src/jexer/TTerminalWidget.java @@ -229,9 +229,6 @@ public class TTerminalWidget extends TScrollableWidget super(parent, x, y, width, height); this.closeAction = closeAction; - if (closeAction != null) { - this.closeAction.data = this; - } String [] fullCommand; @@ -310,9 +307,6 @@ public class TTerminalWidget extends TScrollableWidget super(parent, x, y, width, height); this.closeAction = closeAction; - if (closeAction != null) { - this.closeAction.data = this; - } if (System.getProperty("jexer.TTerminal.shell") != null) { String shell = System.getProperty("jexer.TTerminal.shell"); @@ -829,7 +823,7 @@ public class TTerminalWidget extends TScrollableWidget app.invokeLater(new Runnable() { public void run() { if (closeAction != null) { - closeAction.DO(); + closeAction.DO(TTerminalWidget.this); } if (getApplication() != null) { getApplication().postEvent(new TMenuEvent( diff --git a/src/jexer/TWidget.java b/src/jexer/TWidget.java index 034c1e9..6994e6f 100644 --- a/src/jexer/TWidget.java +++ b/src/jexer/TWidget.java @@ -1277,13 +1277,13 @@ public abstract class TWidget implements Comparable { int absoluteBottomEdge = window.getAbsoluteY() + window.getHeight(); if (!(this instanceof TWindow) && !(this instanceof TVScroller) - && !(parent instanceof TDesktop) + && !(window instanceof TDesktop) ) { absoluteRightEdge -= 1; } if (!(this instanceof TWindow) && !(this instanceof THScroller) - && !(parent instanceof TDesktop) + && !(window instanceof TDesktop) ) { absoluteBottomEdge -= 1; } @@ -1548,54 +1548,50 @@ public abstract class TWidget implements Comparable { } /** - * 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. + * Insert a vertical split between this widget and parent, and optionally + * put another widget in the other side of the split. * * @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 + * on the left pane, and this widget 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 widgets = new ArrayList(children); - for (TWidget w: widgets) { - w.setParent(panel, false); - } - children.clear(); - splitPane.setParent(parent, false); - parent = null; - window = null; + TWidget myParent = parent; + remove(false); + splitPane.setParent(myParent, false); if (newWidgetOnLeft) { splitPane.setLeft(newWidget); - splitPane.setRight(panel); + splitPane.setRight(this); } else { splitPane.setRight(newWidget); - splitPane.setLeft(panel); + splitPane.setLeft(this); } - activate(splitPane); + splitPane.activate(); + if (newWidget != null) { + newWidget.activate(); + } else { + activate(); + } + assert (parent != null); + assert (window != null); 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. + * Insert a horizontal split between this widget and parent, and + * optionally put another widget in the other side of the split. * * @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 @@ -1606,36 +1602,33 @@ public abstract class TWidget implements Comparable { 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 widgets = new ArrayList(children); - for (TWidget w: widgets) { - w.setParent(panel, false); - } - children.clear(); - splitPane.setParent(parent, false); - parent = null; - splitPane.setTop(panel); - parent = null; - window = null; + TWidget myParent = parent; + remove(false); + splitPane.setParent(myParent, false); if (newWidgetOnTop) { splitPane.setTop(newWidget); - splitPane.setBottom(panel); + splitPane.setBottom(this); } else { splitPane.setBottom(newWidget); - splitPane.setTop(panel); + splitPane.setTop(this); + } + splitPane.activate(); + if (newWidget != null) { + newWidget.activate(); + } else { + activate(); } - activate(splitPane); + assert (parent != null); + assert (window != null); 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; } diff --git a/src/jexer/ttree/TTreeView.java b/src/jexer/ttree/TTreeView.java index 88abd70..22f72ca 100644 --- a/src/jexer/ttree/TTreeView.java +++ b/src/jexer/ttree/TTreeView.java @@ -233,7 +233,7 @@ public class TTreeView extends TWidget { */ public void dispatch() { if (action != null) { - action.DO(); + action.DO(this); } }