2 * Jexer - Java Text User Interface
4 * The MIT License (MIT)
6 * Copyright (C) 2019 Kevin Lamonte
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
26 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
31 import jexer
.bits
.CellAttributes
;
32 import jexer
.bits
.GraphicsChars
;
33 import jexer
.event
.TMenuEvent
;
34 import jexer
.event
.TMouseEvent
;
35 import jexer
.event
.TResizeEvent
;
36 import jexer
.menu
.TMenu
;
39 * TSplitPane contains two widgets with a draggable horizontal or vertical
42 public class TSplitPane
extends TWidget
{
44 // ------------------------------------------------------------------------
45 // Variables --------------------------------------------------------------
46 // ------------------------------------------------------------------------
49 * If true, split vertically. If false, split horizontally.
51 private boolean vertical
= true;
54 * The location of the split bar, either as a column number for vertical
55 * split or a row number for horizontal split.
57 private int split
= 0;
60 * The widget on the left side.
65 * The widget on the right side.
67 private TWidget right
;
70 * The widget on the top side.
75 * The widget on the bottom side.
77 private TWidget bottom
;
80 * If true, we are in the middle of a split move.
82 private boolean inSplitMove
= false;
85 * The last seen mouse position.
87 private TMouseEvent mouse
;
89 // ------------------------------------------------------------------------
90 // Constructors -----------------------------------------------------------
91 // ------------------------------------------------------------------------
96 * @param parent parent widget
97 * @param x column relative to parent
98 * @param y row relative to parent
99 * @param width width of widget
100 * @param height height of widget
101 * @param vertical if true, split vertically
103 public TSplitPane(final TWidget parent
, final int x
, final int y
,
104 final int width
, final int height
, final boolean vertical
) {
106 super(parent
, x
, y
, width
, height
);
108 this.vertical
= vertical
;
112 // ------------------------------------------------------------------------
113 // Event handlers ---------------------------------------------------------
114 // ------------------------------------------------------------------------
117 * Handle window/screen resize events.
119 * @param event resize event
122 public void onResize(final TResizeEvent event
) {
123 if (event
.getType() == TResizeEvent
.Type
.WIDGET
) {
125 super.onResize(event
);
127 if (vertical
&& (split
>= getWidth() - 2)) {
129 } else if (!vertical
&& (split
>= getHeight() - 2)) {
138 * Handle mouse button presses.
140 * @param mouse mouse button event
143 public void onMouseDown(final TMouseEvent mouse
) {
148 if (mouse
.isMouse1()) {
150 inSplitMove
= (mouse
.getAbsoluteX() - getAbsoluteX() == split
);
152 inSplitMove
= (mouse
.getAbsoluteY() - getAbsoluteY() == split
);
159 // I didn't take it, pass it on to my children
160 super.onMouseDown(mouse
);
164 * Handle mouse button releases.
166 * @param mouse mouse button release event
169 public void onMouseUp(final TMouseEvent mouse
) {
172 if (inSplitMove
&& mouse
.isMouse1()) {
178 // I didn't take it, pass it on to my children
179 super.onMouseUp(mouse
);
183 * Handle mouse movements.
185 * @param mouse mouse motion event
188 public void onMouseMotion(final TMouseEvent mouse
) {
191 if ((mouse
.getAbsoluteX() - getAbsoluteX() < 0)
192 || (mouse
.getAbsoluteX() - getAbsoluteX() >= getWidth())
193 || (mouse
.getAbsoluteY() - getAbsoluteY() < 0)
194 || (mouse
.getAbsoluteY() - getAbsoluteY() >= getHeight())
196 // Mouse has travelled out of my window.
202 split
= mouse
.getAbsoluteX() - getAbsoluteX();
203 split
= Math
.min(Math
.max(1, split
), getWidth() - 2);
205 split
= mouse
.getAbsoluteY() - getAbsoluteY();
206 split
= Math
.min(Math
.max(1, split
), getHeight() - 2);
212 // I didn't take it, pass it on to my children
213 super.onMouseMotion(mouse
);
216 // ------------------------------------------------------------------------
217 // TWidget ----------------------------------------------------------------
218 // ------------------------------------------------------------------------
225 CellAttributes attr
= getTheme().getColor("tsplitpane");
227 vLineXY(split
, 0, getHeight(), GraphicsChars
.WINDOW_SIDE
, attr
);
228 // TODO: draw intersections of children
231 && (mouse
.getAbsoluteX() == getAbsoluteX() + split
)
232 && (mouse
.getAbsoluteY() >= getAbsoluteY()) &&
233 (mouse
.getAbsoluteY() < getAbsoluteY() + getHeight())
235 putCharXY(split
, mouse
.getAbsoluteY() - getAbsoluteY(),
239 hLineXY(0, split
, getWidth(), GraphicsChars
.SINGLE_BAR
, attr
);
240 // TODO: draw intersections of children
243 && (mouse
.getAbsoluteY() == getAbsoluteY() + split
)
244 && (mouse
.getAbsoluteX() >= getAbsoluteX()) &&
245 (mouse
.getAbsoluteX() < getAbsoluteX() + getWidth())
247 putCharXY(mouse
.getAbsoluteX() - getAbsoluteX(), split
,
255 * Generate a human-readable string for this widget.
257 * @return a human-readable string
260 public String
toString() {
261 return String
.format("%s(%8x) %s position (%d, %d) geometry %dx%d " +
262 "split %d left %s(%8x) right %s(%8x) top %s(%8x) bottom %s(%8x) " +
263 "active %s enabled %s visible %s", getClass().getName(),
264 hashCode(), (vertical ?
"VERTICAL" : "HORIZONTAL"),
265 getX(), getY(), getWidth(), getHeight(), split
,
266 (left
== null ?
"null" : left
.getClass().getName()),
267 (left
== null ?
0 : left
.hashCode()),
268 (right
== null ?
"null" : right
.getClass().getName()),
269 (right
== null ?
0 : right
.hashCode()),
270 (top
== null ?
"null" : top
.getClass().getName()),
271 (top
== null ?
0 : top
.hashCode()),
272 (bottom
== null ?
"null" : bottom
.getClass().getName()),
273 (bottom
== null ?
0 : bottom
.hashCode()),
274 isActive(), isEnabled(), isVisible());
277 // ------------------------------------------------------------------------
278 // TSplitPane -------------------------------------------------------------
279 // ------------------------------------------------------------------------
282 * Get the widget on the left side.
284 * @return the widget on the left, or null if not set
286 public TWidget
getLeft() {
291 * Set the widget on the left side.
293 * @param left the widget to set, or null to remove
295 public void setLeft(final TWidget left
) {
297 throw new IllegalArgumentException("cannot set left on " +
298 "horizontal split pane");
301 if (this.left
!= null) {
308 left
.setParent(this, false);
309 onResize(new TResizeEvent(TResizeEvent
.Type
.WIDGET
, getWidth(),
314 * Get the widget on the right side.
316 * @return the widget on the right, or null if not set
318 public TWidget
getRight() {
323 * Set the widget on the right side.
325 * @param right the widget to set, or null to remove
327 public void setRight(final TWidget right
) {
329 throw new IllegalArgumentException("cannot set right on " +
330 "horizontal split pane");
333 if (this.right
!= null) {
340 right
.setParent(this, false);
341 onResize(new TResizeEvent(TResizeEvent
.Type
.WIDGET
, getWidth(),
346 * Get the widget on the top side.
348 * @return the widget on the top, or null if not set
350 public TWidget
getTop() {
355 * Set the widget on the top side.
357 * @param top the widget to set, or null to remove
359 public void setTop(final TWidget top
) {
361 throw new IllegalArgumentException("cannot set top on vertical " +
365 if (this.top
!= null) {
372 top
.setParent(this, false);
373 onResize(new TResizeEvent(TResizeEvent
.Type
.WIDGET
, getWidth(),
378 * Get the widget on the bottom side.
380 * @return the widget on the bottom, or null if not set
382 public TWidget
getBottom() {
387 * Set the widget on the bottom side.
389 * @param bottom the widget to set, or null to remove
391 public void setBottom(final TWidget bottom
) {
393 throw new IllegalArgumentException("cannot set bottom on " +
394 "vertical split pane");
396 if (bottom
== null) {
397 if (this.bottom
!= null) {
403 this.bottom
= bottom
;
404 bottom
.setParent(this, false);
405 onResize(new TResizeEvent(TResizeEvent
.Type
.WIDGET
, getWidth(),
410 * Remove a widget, regardless of what pane it is on.
412 * @param widget the widget to remove
414 public void removeWidget(final TWidget widget
) {
415 if (widget
== null) {
416 throw new IllegalArgumentException("cannot remove null widget");
418 if (left
== widget
) {
420 assert(right
!= widget
);
421 assert(top
!= widget
);
422 assert(bottom
!= widget
);
425 if (right
== widget
) {
427 assert(left
!= widget
);
428 assert(top
!= widget
);
429 assert(bottom
!= widget
);
434 assert(left
!= widget
);
435 assert(right
!= widget
);
436 assert(bottom
!= widget
);
439 if (bottom
== widget
) {
441 assert(left
!= widget
);
442 assert(right
!= widget
);
443 assert(top
!= widget
);
446 throw new IllegalArgumentException("widget " + widget
+
447 " not in this split");
451 * Replace a widget, regardless of what pane it is on, with another
454 * @param oldWidget the widget to remove
455 * @param newWidget the widget to replace it with
457 public void replaceWidget(final TWidget oldWidget
,
458 final TWidget newWidget
) {
460 if (oldWidget
== null) {
461 throw new IllegalArgumentException("cannot remove null oldWidget");
463 if (left
== oldWidget
) {
465 assert(right
!= newWidget
);
466 assert(top
!= newWidget
);
467 assert(bottom
!= newWidget
);
470 if (right
== oldWidget
) {
472 assert(left
!= newWidget
);
473 assert(top
!= newWidget
);
474 assert(bottom
!= newWidget
);
477 if (top
== oldWidget
) {
479 assert(left
!= newWidget
);
480 assert(right
!= newWidget
);
481 assert(bottom
!= newWidget
);
484 if (bottom
== oldWidget
) {
485 setBottom(newWidget
);
486 assert(left
!= newWidget
);
487 assert(right
!= newWidget
);
488 assert(top
!= newWidget
);
491 throw new IllegalArgumentException("oldWidget " + oldWidget
+
492 " not in this split");
496 * Layout the two child widgets.
498 private void layoutChildren() {
501 left
.setDimensions(0, 0, split
, getHeight());
502 left
.onResize(new TResizeEvent(TResizeEvent
.Type
.WIDGET
,
503 left
.getWidth(), left
.getHeight()));
506 right
.setDimensions(split
+ 1, 0, getWidth() - split
- 1,
508 right
.onResize(new TResizeEvent(TResizeEvent
.Type
.WIDGET
,
509 right
.getWidth(), right
.getHeight()));
513 top
.setDimensions(0, 0, getWidth(), split
);
514 top
.onResize(new TResizeEvent(TResizeEvent
.Type
.WIDGET
,
515 top
.getWidth(), top
.getHeight()));
517 if (bottom
!= null) {
518 bottom
.setDimensions(0, split
+ 1, getWidth(),
519 getHeight() - split
- 1);
520 bottom
.onResize(new TResizeEvent(TResizeEvent
.Type
.WIDGET
,
521 bottom
.getWidth(), bottom
.getHeight()));
527 * Recenter the split to the middle of this split pane.
529 public void center() {
531 split
= getWidth() / 2;
533 split
= getHeight() / 2;
539 * Remove this split, removing the widget specified.
541 * @param widgetToRemove the widget to remove
542 * @param doClose if true, call the close() method before removing the
544 * @return the pane that remains, or null if nothing is retained
546 public TWidget
removeSplit(final TWidget widgetToRemove
,
547 final boolean doClose
) {
551 if ((widgetToRemove
!= left
) && (widgetToRemove
!= right
)) {
552 throw new IllegalArgumentException("widget to remove is not " +
553 "either of the panes in this splitpane");
555 if (widgetToRemove
== left
) {
562 if ((widgetToRemove
!= top
) && (widgetToRemove
!= bottom
)) {
563 throw new IllegalArgumentException("widget to remove is not " +
564 "either of the panes in this splitpane");
566 if (widgetToRemove
== top
) {
573 // Remove me from my parent widget.
574 TWidget myParent
= getParent();
578 if (myParent
instanceof TSplitPane
) {
579 // TSplitPane has a left/right/top/bottom link to me
580 // somewhere, remove it.
581 ((TSplitPane
) myParent
).removeWidget(this);
584 // Nothing is left of either pane. Remove me and bail out.
588 if (myParent
instanceof TSplitPane
) {
589 // TSplitPane has a left/right/top/bottom link to me
590 // somewhere, replace me with keep.
591 ((TSplitPane
) myParent
).replaceWidget(this, keep
);
593 keep
.setParent(myParent
, false);
594 keep
.setDimensions(getX(), getY(), getWidth(), getHeight());
595 keep
.onResize(new TResizeEvent(TResizeEvent
.Type
.WIDGET
, getWidth(),