blob: 1eab3dc89bd5014ac64e88f60d5966e40d13e5e9 [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2004, 2011 Tasktop Technologies and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* https://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*
* Tasktop Technologies - initial API and implementation
* Leah Findlater - initial API and implementation
*******************************************************************************/
package org.eclipse.mylyn.internal.monitor.ui;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.action.ActionContributionItem;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.IContributionItem;
import org.eclipse.mylyn.commons.core.StatusHandler;
import org.eclipse.mylyn.monitor.core.InteractionEvent;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Item;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.ToolItem;
/**
* @author Mik Kersten
* @author Leah Findlater
*/
public class MenuCommandMonitor implements Listener {
public static final String MENU_ITEM_ID = "item.label."; //$NON-NLS-1$
public static final String MENU_ITEM_SELECTED = "menu"; //$NON-NLS-1$
public static final String TOOLBAR_ITEM_SELECTED = "toolbar"; //$NON-NLS-1$
public static final String MENU_PATH_DELIM = "/"; //$NON-NLS-1$
public void handleEvent(Event event) {
try {
if (!(event.widget instanceof Item)) {
return;
}
Item item = (Item) event.widget;
if (item.getData() == null) {
return;
}
Object target = event.widget.getData();
String id = null;
String delta = null;
if (target instanceof IContributionItem) {
id = ((IContributionItem) target).getId();
}
if (id == null && target instanceof ActionContributionItem) {
IAction action = ((ActionContributionItem) target).getAction();
if (action.getId() != null) {
id = action.getId();
} else {
id = action.getClass().getName();
}
} else if (id == null) {
id = target.getClass().getName();
}
if (item instanceof MenuItem) {
MenuItem menu = (MenuItem) item;
Menu parentMenu = menu.getParent();
String location = ""; //$NON-NLS-1$
if (parentMenu != null) {
while (parentMenu.getParentItem() != null) {
location = parentMenu.getParentItem().getText() + MENU_PATH_DELIM + location;
parentMenu = parentMenu.getParentMenu();
}
}
if (id == null) {
return;
// TODO: would be good to put back this info in some form
// but it can contain private data, bug 178604
// if (id == null)
// id = "null";
// String itemText = obfuscateItemText(item.getText());
// id = id + "$" + MENU_ITEM_ID + location + itemText;
}
delta = MENU_ITEM_SELECTED;
} else if (item instanceof ToolItem) {
// TODO: would be good to put back this info in some form
// but it can contain private data, bug 178604
// ToolItem tool = (ToolItem) item;
// if (id == null)
// id = "null";
// id = id + "$" + MENU_ITEM_ID + '.' + tool.getToolTipText();
delta = TOOLBAR_ITEM_SELECTED;
}
InteractionEvent interactionEvent = InteractionEvent.makeCommand(id, delta);
MonitorUiPlugin.getDefault().notifyInteractionObserved(interactionEvent);
} catch (Throwable t) {
StatusHandler.log(new Status(IStatus.ERROR, MonitorUiPlugin.ID_PLUGIN, "Could not log selection", t)); //$NON-NLS-1$
}
}
}