/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* The abstract class XBaseMenuWindow is the superclass
* of all menu windows.
*/
/************************************************
*
* Data members
*
************************************************/
/*
* Colors are calculated using MotifColorUtilities class
* from backgroundColor and are contained in these vars.
*/
/**
* Array of items.
*/
/**
* Index of selected item in array of items
*/
/**
* Specifies currently showing submenu.
*/
/**
* Static synchronizational object.
* Following operations should be synchronized
* using this object:
* 1. Access to items vector
* 2. Access to selection
* 3. Access to showing menu window member
*
* This is lowest level lock,
* no other locks should be taken when
* thread own this lock.
*/
/************************************************
*
* Event processing
*
************************************************/
/**
* If mouse button is clicked on item showing submenu
* we have to hide its submenu.
* And if mouse button is pressed on such item and
* dragged to another, getShowingSubmenu() is changed.
* So this member saves the item that the user
* presses mouse button on _only_ if it's showing submenu.
*/
/**
* If the PopupMenu is invoked as a result of right button click
* first mouse event after grabInput would be MouseReleased.
* We need to check if the user has moved mouse after input grab.
* If yes - hide the PopupMenu. If no - do nothing
*/
protected boolean hasPointerMoved = false;
/************************************************
*
* Mapping data
*
************************************************/
/**
* Mapping data that is filled in getMappedItems function
* and reset in resetSize function. It contains array of
* items in order that they appear on screen and may contain
* additional data defined by descendants.
*/
/**
* Array of item in order that they appear on screen
*/
/**
* Constructs MappingData object with list
* of menu items
*/
}
/**
* Constructs MappingData without items
* This constructor should be used in case of errors
*/
MappingData() {
}
try {
return super.clone();
} catch (CloneNotSupportedException ex) {
throw new InternalError();
}
}
return this.items;
}
}
/************************************************
*
* Construction
*
************************************************/
XBaseMenuWindow() {
super(new XCreateWindowParams(new Object[] {
}
/************************************************
*
* Abstract methods
*
************************************************/
/**
* Returns parent menu window (not the X-heirarchy parent window)
*/
/**
* Performs mapping of items in window.
* This function creates and fills specific
* descendant of MappingData
* and sets mapping coordinates of items
* This function should return default menu data
* if errors occur
*/
/**
* Calculates placement of submenu window
* given bounds of item with submenu and
* size of submenu window. Returns suggested
* rectangle for submenu window in global coordinates
* @param itemBounds the bounding rectangle of item
* in local coordinates
* @param windowSize the desired size of submenu's window
*/
/**
* This function is to be called if it's likely that size
* of items was changed. It can be called from any thread
* in any locked state, so it should not take locks
*/
protected abstract void updateSize();
/************************************************
*
* Initialization
*
************************************************/
/**
* Overrides XBaseWindow.instantPreInit
*/
super.instantPreInit(params);
}
/************************************************
*
* General-purpose functions
*
************************************************/
/**
* Returns static lock used for menus
*/
return menuTreeLock;
}
/**
* This function is called to clear all saved
* size data.
*/
protected void resetMapping() {
mappingData = null;
}
/**
* Invokes repaint procedure on eventHandlerThread
*/
void postPaintEvent() {
if (isShowing()) {
}
}
/************************************************
*
* Utility functions for manipulating items
*
************************************************/
/**
* Thread-safely returns item at specified index
* @param index the position of the item to be returned.
*/
if (index >= 0) {
synchronized(getMenuTreeLock()) {
}
}
}
return null;
}
/**
* Thread-safely creates a copy of the items vector
*/
synchronized(getMenuTreeLock()) {
}
}
/**
* Thread-safely returns selected item
*/
synchronized(getMenuTreeLock()) {
if (selectedIndex >= 0) {
}
}
return null;
}
}
/**
* Returns showing submenu, if any
*/
synchronized(getMenuTreeLock()) {
return showingSubmenu;
}
}
/**
* Adds item to end of items vector.
* Note that this function does not perform
* check for adding duplicate items
* @param item item to add
*/
mp.setContainer(this);
synchronized(getMenuTreeLock()) {
}
} else {
}
}
updateSize();
}
/**
* Removes item at the specified index from items vector.
* @param index the position of the item to be removed
*/
synchronized(getMenuTreeLock()) {
if (selectedIndex == index) {
selectItem(null, false);
} else if (selectedIndex > index) {
}
} else {
log.fine("WARNING: Attempt to remove non-existing menu item, index : " + index + ", item count : " + items.size());
}
}
}
updateSize();
}
/**
* Clears items vector and loads specified vector
* @param items vector to be loaded
*/
synchronized(getMenuTreeLock()) {
for(int i = 0; i < itemCnt; i++) {
}
}
}
/**
* We can not select by index, so we need to select by ref.
* @param item the item to be selected, null to clear selection
* @param showWindowIfMenu if the item is XMenuPeer then its
*/
synchronized(getMenuTreeLock()) {
if (this.selectedIndex != newSelectedIndex) {
}
this.selectedIndex = newSelectedIndex;
}
final XMenuPeer submenuToShow = (showWindowIfMenu && (item instanceof XMenuPeer)) ? (XMenuPeer)item : null;
if (submenuToShow != showingSubmenu) {
public void run() {
}
});
}
}
}
/**
* Performs hiding of currently showing submenu
* and showing of submenuToShow.
* This function should be executed on eventHandlerThread
* @param submenuToShow submenu to be shown or null
* to hide currently showing submenu
*/
//ensureCreated can invoke XWindowPeer.init() ->
//XWindowPeer.initGraphicsConfiguration() ->
//Window.getGraphicsConfiguration()
//that tries to obtain Component.AWTTreeLock.
//So it should be called outside awtLock()
if (menuWindowToShow != null) {
}
try {
synchronized(getMenuTreeLock()) {
if (showingSubmenu != submenuToShow) {
}
if (showingSubmenu != null) {
if (showingSubmenuWindow != null) {
}
}
if (submenuToShow != null) {
}
}
}
} finally {
}
}
for (int i = 0; i < itemCnt; i++) {
}
}
/************************************************
*
* Utility functions for manipulating mapped items
*
************************************************/
/**
* Returns array of mapped items, null if error
* This function has to be not synchronized
* and we have to guarantee that we return
* some MappingData to user. It's OK if
* this.mappingData is replaced meanwhile
*/
if (mappingData == null) {
mappingData = map();
this.mappingData = mappingData;
}
}
/**
* returns item thats mapped coordinates contain
* specified point, null of none.
* @param pt the point in this window's coordinate system
*/
for (int i = 0; i < cnt; i++) {
return items[i];
}
}
return null;
}
/**
* Returns first item after currently selected
* item that can be selected according to mapping array.
* (no separators and no disabled items).
* Currently selected item if it's only selectable,
* null if no item can be selected
*/
//Find index of selected item
int selIdx = -1;
for (int i = 0; i < cnt; i++) {
if (mappedItems[i] == selectedItem) {
selIdx = i;
break;
}
}
//cycle through mappedItems to find selectable item
//beginning from the next item and moving to the
//beginning of array when end is reached.
//Cycle is finished on selected item itself
for (int i = 0; i < cnt; i++) {
return item;
}
idx++;
idx = 0;
}
}
//return null if no selectable item was found
return null;
}
/**
* Returns first item before currently selected
* see getNextSelectableItem() for comments
*/
//Find index of selected item
int selIdx = -1;
for (int i = 0; i < cnt; i++) {
if (mappedItems[i] == selectedItem) {
selIdx = i;
break;
}
}
//cycle through mappedItems to find selectable item
for (int i = 0; i < cnt; i++) {
return item;
}
idx--;
if (idx < 0) {
}
}
//return null if no selectable item was found
return null;
}
/**
* Returns first selectable item
* This function is intended for clearing selection
*/
for (int i = 0; i < cnt; i++) {
return item;
}
}
return null;
}
/************************************************
*
* Utility functions for manipulating
* hierarchy of windows
*
************************************************/
/**
* returns leaf menu window or
* this if no children are showing
*/
synchronized(getMenuTreeLock()) {
XBaseMenuWindow leaf = this;
}
return leaf;
}
}
/**
* returns root menu window
* or this if this window is topmost
*/
synchronized(getMenuTreeLock()) {
XBaseMenuWindow t = this;
t = tparent;
tparent = t.getParentMenuWindow();
}
return t;
}
}
/**
* Returns window that contains pt.
* search is started from leaf window
* to return first window in Z-order
* @param pt point in global coordinates
*/
synchronized(getMenuTreeLock()) {
XBaseMenuWindow t = getShowingLeaf();
while (t != null) {
return t;
}
t = t.getParentMenuWindow();
}
return null;
}
}
/************************************************
*
* Primitives for getSubmenuBounds
*
* These functions are invoked from getSubmenuBounds
* implementations in different order. They check if window
* of size windowSize fits to the specified edge of
* rectangle itemBounds on the screen of screenSize.
* Return rectangle that occupies the window if it fits or null.
*
************************************************/
/**
* Checks if window fits below specified item
* returns rectangle that the window fits to or null.
* @param itemBounds rectangle of item in global coordinates
* @param windowSize size of submenu window to fit
* @param screenSize size of screen
*/
//Fix for 6267162: PIT: Popup Menu gets hidden below the screen when opened
//near the periphery of the screen, XToolkit
//Window should be moved if it's outside top-left screen bounds
//move it to the left if needed
}
}
} else {
return null;
}
}
/**
* Checks if window fits above specified item
* returns rectangle that the window fits to or null.
* @param itemBounds rectangle of item in global coordinates
* @param windowSize size of submenu window to fit
* @param screenSize size of screen
*/
//Fix for 6267162: PIT: Popup Menu gets hidden below the screen when opened
//near the periphery of the screen, XToolkit
//Window should be moved if it's outside bottom-left screen bounds
if (y >= 0) {
//move it to the left if needed
}
}
} else {
return null;
}
}
/**
* Checks if window fits to the right specified item
* returns rectangle that the window fits to or null.
* @param itemBounds rectangle of item in global coordinates
* @param windowSize size of submenu window to fit
* @param screenSize size of screen
*/
//Fix for 6267162: PIT: Popup Menu gets hidden below the screen when opened
//near the periphery of the screen, XToolkit
//Window should be moved if it's outside top-left screen bounds
//move it to the top if needed
}
}
} else {
return null;
}
}
/**
* Checks if window fits to the left specified item
* returns rectangle that the window fits to or null.
* @param itemBounds rectangle of item in global coordinates
* @param windowSize size of submenu window to fit
* @param screenSize size of screen
*/
//Fix for 6267162: PIT: Popup Menu gets hidden below the screen when opened
//near the periphery of the screen, XToolkit
//Window should be moved if it's outside top-right screen bounds
if (x >= 0) {
//move it to the top if needed
}
}
} else {
return null;
}
}
/**
* The last thing we can do with the window
* to fit it on screen - move it to the
* top-left edge and cut by screen dimensions
* @param windowSize size of submenu window to fit
* @param screenSize size of screen
*/
}
/************************************************
*
* Utility functions for manipulating colors
*
************************************************/
/**
* This function is called before every painting.
* TODO:It would be better to add PropertyChangeListener
* to target component
* TODO:It would be better to access background color
* not invoking user-overridable function
*/
void resetColors() {
}
/**
* Calculates colors of various elements given
* background color. Uses MotifColorUtilities
* @param backgroundColor the color of menu window's
* background.
*/
if (backgroundColor != this.backgroundColor) {
this.backgroundColor = backgroundColor;
darkShadowColor = new Color(MotifColorUtilities.calculateBottomShadowFromBackground(red,green,blue));
disabledColor = (backgroundColor.equals(Color.BLACK)) ? foregroundColor.darker() : backgroundColor.darker();
}
}
return backgroundColor;
}
return foregroundColor;
}
return lightShadowColor;
}
return darkShadowColor;
}
return selectedColor;
}
return disabledColor;
}
/************************************************
*
* Painting utility functions
*
************************************************/
/**
* Draws raised or sunken rectangle on specified graphics
* @param g the graphics on which to draw
* @param x the coordinate of left edge in coordinates of graphics
* @param y the coordinate of top edge in coordinates of graphics
* @param width the width of rectangle
* @param height the height of rectangle
* @param raised true to draw raised rectangle, false to draw sunken
*/
return;
}
g.setColor(c);
}
/************************************************
*
* Overriden utility functions of XWindow
*
************************************************/
/**
* Filters X events
*/
switch (e.get_type()) {
case XConstants.Expose :
case XConstants.GraphicsExpose :
case XConstants.ButtonPress:
case XConstants.ButtonRelease:
case XConstants.MotionNotify:
case XConstants.KeyPress:
case XConstants.KeyRelease:
case XConstants.DestroyNotify:
return super.isEventDisabled(e);
default:
return true;
}
}
/**
* Invokes disposal procedure on eventHandlerThread
*/
public void dispose() {
setDisposed(true);
public void run() {
doDispose();
}
});
}
/**
* Performs disposal of menu window.
* Should be called only on eventHandlerThread
*/
protected void doDispose() {
xSetVisible(false);
surfaceData = null;
}
destroy();
}
/**
* Invokes event processing on eventHandlerThread
* This function needs to be overriden since
* XBaseMenuWindow has no corresponding component
* so events can not be processed using standart means
*/
public void run() {
}
});
}
/**
* The implementation of base window performs processing
* of paint events only. This behaviour is changed in
* descendants.
*/
case PaintEvent.PAINT:
break;
}
}
/**
* Save location of pointer for further use
* then invoke superclass
*/
public boolean grabInput() {
int rootX;
int rootY;
boolean res;
try {
getScreenNumber());
} finally {
}
if (res) {
//Mouse pointer is on the same display
this.hasPointerMoved = false;
} else {
this.grabInputPoint = null;
this.hasPointerMoved = true;
}
return res;
}
/************************************************
*
* Overridable event processing functions
*
************************************************/
/**
* Performs repainting
*/
}
/************************************************
*
* User input handling utility functions
*
************************************************/
/**
* Performs handling of java mouse event
* Note that this function should be invoked
* only from root of menu window's hierarchy
* that grabs input focus
*/
return;
}
//Window that owns input
//Point of mouse event in global coordinates
if (!hasPointerMoved) {
//Fix for 6301307: NullPointerException while dispatching mouse events, XToolkit
if (grabInputPoint == null ||
hasPointerMoved = true;
}
}
//Z-order first descendant of current menu window
//hierarchy that contain mouse point
//Item in wnd that contains mouse point, if any
//Currently showing leaf window
switch (mouseEvent.getID()) {
case MouseEvent.MOUSE_PRESSED:
//This line is to get rid of possible problems
//That may occur if mouse events are lost
//Menus grab input and the user
//presses mouse button outside
ungrabInput();
} else {
//Menus grab input OR mouse is pressed on menu window
grabInput();
//Button is pressed on enabled item
//Button is pressed on item that shows
//submenu. We have to hide its submenu
//if user clicks on it
}
} else {
//Button is pressed on disabled item or empty space
}
}
}
break;
case MouseEvent.MOUSE_RELEASED:
//Note that if item is not null, wnd has to be not null
if (showingMousePressedSubmenu == item) {
//User clicks on item that shows submenu.
//Hide the submenu
if (wnd instanceof XMenuBarPeer) {
ungrabInput();
} else {
}
}
} else {
//Invoke action event
ungrabInput();
}
} else {
//Mouse is released outside menu items
ungrabInput();
}
}
break;
case MouseEvent.MOUSE_DRAGGED:
//Mouse is dragged over menu window
//Move selection to item under cursor
if (grabWindow == this){
}
} else {
}
} else {
//Mouse is dragged outside menu windows
//clear selection in leaf to reflect it
}
}
break;
}
}
/**
* Performs handling of java keyboard event
* Note that this function should be invoked
* only from root of menu window's hierarchy
* that grabs input focus
*/
return;
}
switch(keyCode) {
if (!(cwnd instanceof XMenuBarPeer)) {
//If active window is not menu bar,
//move selection up
}
break;
case KeyEvent.VK_KP_DOWN:
if (cwnd instanceof XMenuBarPeer) {
//If active window is menu bar show current submenu
selectItem(getSelectedItem(), true);
} else {
//move selection down
}
break;
case KeyEvent.VK_KP_LEFT:
if (cwnd instanceof XMenuBarPeer) {
//leaf window is menu bar
//select previous item
selectItem(getPrevSelectableItem(), false);
//leaf window is direct child of menu bar
//select previous item of menu bar
//and show its submenu
selectItem(getPrevSelectableItem(), true);
} else {
//hide leaf moving focus to its parent
//(equvivalent of pressing ESC)
//Fix for 6272952: PIT: Pressing LEFT ARROW on a popup menu throws NullPointerException, XToolkit
}
}
break;
case KeyEvent.VK_KP_RIGHT:
if (cwnd instanceof XMenuBarPeer) {
//leaf window is menu bar
//select next item
selectItem(getNextSelectableItem(), false);
//current item is menu, show its window
//(equivalent of ENTER)
} else if (this instanceof XMenuBarPeer) {
//if this is menu bar (not popup menu)
//and the user presses RIGHT on item (not submenu)
//select next top-level menu
selectItem(getNextSelectableItem(), true);
}
break;
//If the current item has submenu show it
//Perform action otherwise
ungrabInput();
}
break;
//If current window is menu bar or its child - close it
//If current window is popup menu - close it
//go one level up otherwise
//Fixed 6266513: Incorrect key handling in XAWT popup menu
//Popup menu should be closed on 'ESC'
ungrabInput();
} else if (cwnd instanceof XPopupMenuPeer) {
ungrabInput();
} else {
}
break;
//Fixed 6266513: Incorrect key handling in XAWT popup menu
//All menus should be closed on 'F10'
ungrabInput();
break;
default:
break;
}
}
} //class XBaseMenuWindow