|
| 1 | +/* |
| 2 | + * Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * Copyright (c) 2020, Red Hat Inc. All rights reserved. |
| 4 | + * |
| 5 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 6 | + * |
| 7 | + * The contents of this file are subject to the terms of either the Universal Permissive License |
| 8 | + * v 1.0 as shown at http://oss.oracle.com/licenses/upl |
| 9 | + * |
| 10 | + * or the following license: |
| 11 | + * |
| 12 | + * Redistribution and use in source and binary forms, with or without modification, are permitted |
| 13 | + * provided that the following conditions are met: |
| 14 | + * |
| 15 | + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions |
| 16 | + * and the following disclaimer. |
| 17 | + * |
| 18 | + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of |
| 19 | + * conditions and the following disclaimer in the documentation and/or other materials provided with |
| 20 | + * the distribution. |
| 21 | + * |
| 22 | + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to |
| 23 | + * endorse or promote products derived from this software without specific prior written permission. |
| 24 | + * |
| 25 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR |
| 26 | + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 27 | + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR |
| 28 | + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 29 | + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 30 | + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 31 | + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY |
| 32 | + * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 33 | + */ |
| 34 | +package org.openjdk.jmc.flightrecorder.ui.common; |
| 35 | + |
| 36 | +import org.eclipse.swt.SWT; |
| 37 | +import org.eclipse.swt.events.ShellAdapter; |
| 38 | +import org.eclipse.swt.events.ShellEvent; |
| 39 | +import org.eclipse.swt.graphics.Point; |
| 40 | +import org.eclipse.swt.graphics.Rectangle; |
| 41 | +import org.eclipse.swt.layout.GridData; |
| 42 | +import org.eclipse.swt.layout.GridLayout; |
| 43 | +import org.eclipse.swt.widgets.Button; |
| 44 | +import org.eclipse.swt.widgets.Composite; |
| 45 | +import org.eclipse.swt.widgets.Display; |
| 46 | +import org.eclipse.swt.widgets.Event; |
| 47 | +import org.eclipse.swt.widgets.Listener; |
| 48 | +import org.eclipse.swt.widgets.Shell; |
| 49 | + |
| 50 | +import org.openjdk.jmc.flightrecorder.ui.common.LaneEditor.EditLanesContainer; |
| 51 | +import org.openjdk.jmc.flightrecorder.ui.messages.internal.Messages; |
| 52 | +import org.openjdk.jmc.ui.common.util.Environment; |
| 53 | +import org.openjdk.jmc.ui.common.util.Environment.OSType; |
| 54 | +import org.openjdk.jmc.ui.handlers.MCContextMenuManager; |
| 55 | + |
| 56 | +public class DropdownLaneFilter extends Composite { |
| 57 | + private static final int EXTRA_SHELL_WIDTH = 300; |
| 58 | + private static final int SHELL_HEIGHT = 500; |
| 59 | + private Button dropdownButton; |
| 60 | + private GridLayout layout; |
| 61 | + private MCContextMenuManager[] mms; |
| 62 | + private Shell shell; |
| 63 | + private ThreadGraphLanes lanes; |
| 64 | + private EditLanesContainer container; |
| 65 | + |
| 66 | + public DropdownLaneFilter(Composite parent, ThreadGraphLanes lanes, MCContextMenuManager[] mms) { |
| 67 | + super(parent, SWT.NONE); |
| 68 | + this.lanes = lanes; |
| 69 | + this.mms = mms; |
| 70 | + this.layout = new GridLayout(); |
| 71 | + layout.marginHeight = 0; |
| 72 | + layout.marginWidth = 0; |
| 73 | + setLayout(layout); |
| 74 | + dropdownButton = new Button(this, SWT.TOGGLE); |
| 75 | + dropdownButton.setLayoutData(new GridData(GridData.FILL_BOTH)); |
| 76 | + dropdownButton.setText(Messages.DropdownLaneFilter_THREAD_STATE_SELECTION); |
| 77 | + dropdownButton.addListener(SWT.MouseUp, new Listener() { |
| 78 | + @Override |
| 79 | + public void handleEvent(Event e) { |
| 80 | + /* |
| 81 | + * Windows: works IF the menu item that is toggled is also highlight. e.g, if the |
| 82 | + * user wanted to toggle the Java Compiler lanes, then it's not enough to just click |
| 83 | + * the checkbox - the Java Compiler menu item must be highlighted at the time of |
| 84 | + * toggling |
| 85 | + * |
| 86 | + * MacOS: There are currently issues with paint timings with Mac OS at the moment |
| 87 | + * where toggling an activity lane from the dropdown does not redraw the chart. This |
| 88 | + * may be related to the Windows issue, and may be a SWT limitation. |
| 89 | + */ |
| 90 | + if (Environment.getOSType() != OSType.LINUX) { |
| 91 | + lanes.openEditLanesDialog(mms, false); |
| 92 | + dropdownButton.setSelection(false); |
| 93 | + } else { |
| 94 | + if (dropdownButton.getSelection()) { |
| 95 | + displayDropdown(); |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + }); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Creates a new shell which is positioned below the dropdown button. This new shell creates the |
| 104 | + * appearance of a dropdown component, and it's contents will be the TypeFilterBuilder as found |
| 105 | + * in the Edit Thread Lanes dialog. |
| 106 | + */ |
| 107 | + private void displayDropdown() { |
| 108 | + Point p = dropdownButton.getParent().toDisplay(dropdownButton.getLocation()); |
| 109 | + Point size = dropdownButton.getSize(); |
| 110 | + Rectangle shellRect = new Rectangle(p.x, p.y + size.y, size.x, 0); |
| 111 | + |
| 112 | + shell = new Shell(DropdownLaneFilter.this.getShell(), SWT.BORDER); |
| 113 | + shell.addShellListener(new ShellAdapter() { |
| 114 | + |
| 115 | + public void shellDeactivated(ShellEvent e) { |
| 116 | + if (!isCursorOnTopOfButton()) { |
| 117 | + // If the shell is closed without clicking the button (i.e., not forcing |
| 118 | + // a toggle), then the button must be toggled programmatically. |
| 119 | + dropdownButton.setSelection(false); |
| 120 | + } |
| 121 | + disposeDropdown(); |
| 122 | + } |
| 123 | + }); |
| 124 | + |
| 125 | + shell.setLayout(this.layout); |
| 126 | + shell.setSize(shellRect.width + EXTRA_SHELL_WIDTH, SHELL_HEIGHT); |
| 127 | + shell.setLocation(shellRect.x, shellRect.y); |
| 128 | + |
| 129 | + container = new EditLanesContainer(shell, lanes.getTypeTree(), lanes.getLaneDefinitions(), () -> updateChart()); |
| 130 | + container.setLayoutData(new GridData(GridData.FILL_BOTH)); |
| 131 | + shell.open(); |
| 132 | + } |
| 133 | + |
| 134 | + private void disposeDropdown() { |
| 135 | + if (shell != null && !shell.isDisposed()) { |
| 136 | + shell.close(); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Determine whether or not the mouse cursor is overlapping the dropdown button. An open |
| 142 | + * dropdown shell should close when the user clicks the button. In Linux, the MouseListener on |
| 143 | + * the button will fire. In Windows, the shell has priority and the MouseListener doesn't get |
| 144 | + * activated. This function is to be used in the ShellAdapter to determine if the user closed |
| 145 | + * the shell by trying to click the button, or by clicking away from the dropdown shell. |
| 146 | + * |
| 147 | + * @return true if the mouse cursor is on top of the button |
| 148 | + */ |
| 149 | + private boolean isCursorOnTopOfButton() { |
| 150 | + Point cursor = Display.getCurrent().getCursorLocation(); |
| 151 | + Point buttonLoc = dropdownButton.toDisplay(1, 1); |
| 152 | + Rectangle buttonRect = new Rectangle(buttonLoc.x, buttonLoc.y, dropdownButton.getSize().x, |
| 153 | + dropdownButton.getSize().y); |
| 154 | + return buttonRect.contains(cursor); |
| 155 | + } |
| 156 | + |
| 157 | + private void updateChart() { |
| 158 | + lanes.buildChart(); |
| 159 | + lanes.updateContextMenus(mms, false); |
| 160 | + } |
| 161 | +} |
0 commit comments