Go up menu item tree to look for parents to perform updates with, this supports multi...
[SquirrelJME.git] / modules / midp-lcdui / src / main / java / cc / squirreljme / runtime / lcdui / scritchui / MenuLayoutBar.java
blob3379ddb996ab7e23fc57962ec7e95c8b5ae2b93e
1 // -*- Mode: Java; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
3 // Multi-Phasic Applications: SquirrelJME
4 // Copyright (C) Stephanie Gawroriski <xer@multiphasicapps.net>
5 // ---------------------------------------------------------------------------
6 // SquirrelJME is under the Mozilla Public License Version 2.0.
7 // See license.mkd for licensing and copyright information.
8 // ---------------------------------------------------------------------------
10 package cc.squirreljme.runtime.lcdui.scritchui;
12 import cc.squirreljme.jvm.mle.scritchui.ScritchInterface;
13 import cc.squirreljme.jvm.mle.scritchui.brackets.ScritchMenuBarBracket;
14 import cc.squirreljme.runtime.cldc.annotation.SquirrelJMEVendorApi;
15 import javax.microedition.lcdui.Displayable;
16 import javax.microedition.lcdui.Menu;
17 import org.jetbrains.annotations.Async;
19 /**
20 * Represents the layout state for a menu bar.
22 * It might be awkward that menu bars are bound to {@link Displayable}s,
23 * however {@link Displayable}s always have just a single menu bar.
25 * There is a single pinned menu which is used as the default menu for
26 * commands that are added, since they need to go somewhere.
28 * @see ScritchMenuBarBracket
29 * @since 2024/07/18
31 @SquirrelJMEVendorApi
32 public final class MenuLayoutBar
33 extends MenuLayoutBindableSub<Displayable>
35 /** Root node which represents the menu bar. */
36 private volatile MenuLayoutBarNode _rootNode;
38 /** The pinned menu. */
39 private volatile Menu _pinned;
41 /**
42 * Initializes the bindable.
44 * @param __scritch The ScritchUI interface.
45 * @param __item The item to bind to.
46 * @throws NullPointerException On null arguments.
47 * @since 2024/07/18
49 public MenuLayoutBar(ScritchInterface __scritch, Displayable __item)
50 throws NullPointerException
52 super(__scritch, __item);
55 /**
56 * Returns the pinned menu, which generally is the default item.
58 * @return The pinned menu, or {@code null} if there is nothing pinned.
59 * @since 2024/07/18
61 @SquirrelJMEVendorApi
62 public Menu pin()
64 synchronized (this)
66 return this._pinned;
70 /**
71 * Pins the given menu as the default menu, or clears it.
73 * @param __menu The menu to pin as the default, {@code null} will clear
74 * the menu.
75 * @since 2024/07/18
77 @SquirrelJMEVendorApi
78 public void pin(Menu __menu)
80 synchronized (this)
82 this._pinned = __menu;
86 /**
87 * Performs recursive refreshing of this bindable.
89 * @throws IllegalStateException If this is called outside the event
90 * loop.
91 * @since 2024/07/18
93 @SquirrelJMEVendorApi
94 @Async.Execute
95 protected void refreshInLoop()
96 throws IllegalStateException
98 ScritchInterface scritchApi = this.scritchApi;
99 synchronized (this)
101 // Do we need to make the root node?
102 MenuLayoutBarNode rootNode = this._rootNode;
103 if (rootNode == null)
105 // Create native menu bar first
106 ScritchMenuBarBracket bar = scritchApi.menu().menuBarNew();
108 // Assign
109 rootNode = new MenuLayoutBarNode(this.getMidp(), bar);
110 this._rootNode = rootNode;
113 // Clear out entire tree, so we rebuild the menu
114 rootNode.__clear(scritchApi, false);
116 // Then rebuild in loop
117 rootNode.__build(scritchApi);