| 1 | package org.eclipse.tracecompass.internal.provisional.tmf.ui.views.timegraph2.swtjfx; |
| 2 | |
| 3 | import java.util.List; |
| 4 | import java.util.stream.Collectors; |
| 5 | import java.util.stream.IntStream; |
| 6 | |
| 7 | import org.eclipse.jdt.annotation.Nullable; |
| 8 | import org.eclipse.jface.action.Action; |
| 9 | import org.eclipse.jface.action.ActionContributionItem; |
| 10 | import org.eclipse.jface.action.IAction; |
| 11 | import org.eclipse.jface.action.IMenuCreator; |
| 12 | import org.eclipse.swt.widgets.Control; |
| 13 | import org.eclipse.swt.widgets.Event; |
| 14 | import org.eclipse.swt.widgets.Menu; |
| 15 | import org.eclipse.tracecompass.internal.provisional.tmf.core.views.timegraph2.ITimeGraphModelRenderProvider; |
| 16 | import org.eclipse.tracecompass.internal.provisional.tmf.core.views.timegraph2.ITimeGraphModelRenderProvider.FilterMode; |
| 17 | import org.eclipse.tracecompass.internal.provisional.tmf.core.views.timegraph2.ITimeGraphModelRenderProvider.SortingMode; |
| 18 | import org.eclipse.tracecompass.internal.provisional.tmf.ui.views.timegraph2.TimeGraphModelViewer; |
| 19 | import org.eclipse.tracecompass.internal.tmf.ui.Activator; |
| 20 | import org.eclipse.tracecompass.internal.tmf.ui.ITmfImageConstants; |
| 21 | |
| 22 | final class ActionFactory { |
| 23 | |
| 24 | private ActionFactory() {} |
| 25 | |
| 26 | private static abstract class DrowDownMenuCreator implements IMenuCreator { |
| 27 | |
| 28 | private @Nullable Menu fMenu = null; |
| 29 | |
| 30 | @Override |
| 31 | public final @Nullable Menu getMenu(@Nullable Menu parent) { |
| 32 | return null; // Not used? |
| 33 | } |
| 34 | |
| 35 | @Override |
| 36 | public final Menu getMenu(@Nullable Control parent) { |
| 37 | if (fMenu != null) { |
| 38 | fMenu.dispose(); |
| 39 | } |
| 40 | Menu menu = new Menu(parent); |
| 41 | getMenuActions().forEach(action -> { |
| 42 | new ActionContributionItem(action).fill(menu, -1); |
| 43 | }); |
| 44 | fMenu = menu; |
| 45 | return menu; |
| 46 | } |
| 47 | |
| 48 | @Override |
| 49 | public final void dispose() { |
| 50 | if (fMenu != null) { |
| 51 | fMenu.dispose(); |
| 52 | fMenu = null; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | protected abstract List<Action> getMenuActions(); |
| 57 | } |
| 58 | |
| 59 | private static class SelectSortingModeAction extends Action { |
| 60 | |
| 61 | public SelectSortingModeAction(TimeGraphModelViewer viewer) { |
| 62 | super("Sorting Mode", IAction.AS_DROP_DOWN_MENU); |
| 63 | ITimeGraphModelRenderProvider provider = viewer.getModelRenderProvider(); |
| 64 | |
| 65 | setToolTipText("Select Sorting Mode"); |
| 66 | setImageDescriptor(Activator.getDefault().getImageDescripterFromPath(ITmfImageConstants.IMG_UI_SHOW_LEGEND)); |
| 67 | setMenuCreator(new DrowDownMenuCreator() { |
| 68 | @Override |
| 69 | protected List<Action> getMenuActions() { |
| 70 | return IntStream.range(0, provider.getSortingModes().size()) |
| 71 | .mapToObj(index -> { |
| 72 | SortingMode sortingMode = provider.getSortingModes().get(index); |
| 73 | Action action = new Action(sortingMode.getName(), IAction.AS_RADIO_BUTTON) { |
| 74 | @Override |
| 75 | public void runWithEvent(@Nullable Event event) { |
| 76 | if (isChecked()) { |
| 77 | provider.setCurrentSortingMode(index); |
| 78 | System.out.println("repainting viewer"); |
| 79 | viewer.repaintCurrentArea(); |
| 80 | } |
| 81 | } |
| 82 | }; |
| 83 | action.setEnabled(true); |
| 84 | action.setChecked(provider.getCurrentSortingMode() == sortingMode); |
| 85 | return action; |
| 86 | }) |
| 87 | .collect(Collectors.toList()); |
| 88 | } |
| 89 | }); |
| 90 | } |
| 91 | |
| 92 | @Override |
| 93 | public void runWithEvent(@Nullable Event event) { |
| 94 | // TODO Also open the menu, need to figure out how |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | private static class SelectFilterModesAction extends Action { |
| 99 | |
| 100 | public SelectFilterModesAction(TimeGraphModelViewer viewer) { |
| 101 | super("Filters", IAction.AS_DROP_DOWN_MENU); |
| 102 | ITimeGraphModelRenderProvider provider = viewer.getModelRenderProvider(); |
| 103 | |
| 104 | setToolTipText("Configure Filters"); |
| 105 | setImageDescriptor(Activator.getDefault().getImageDescripterFromPath(ITmfImageConstants.IMG_UI_FILTERS)); |
| 106 | setMenuCreator(new DrowDownMenuCreator() { |
| 107 | @Override |
| 108 | protected List<Action> getMenuActions() { |
| 109 | return IntStream.range(0, provider.getFilterModes().size()) |
| 110 | .mapToObj(index -> { |
| 111 | FilterMode filterMode = provider.getFilterModes().get(index); |
| 112 | Action action = new Action(filterMode.getName(), IAction.AS_CHECK_BOX) { |
| 113 | @Override |
| 114 | public void runWithEvent(@Nullable Event event) { |
| 115 | if (isChecked()) { |
| 116 | provider.enableFilterMode(index); |
| 117 | } else { |
| 118 | provider.disableFilterMode(index); |
| 119 | } |
| 120 | System.out.println("repainting viewer"); |
| 121 | viewer.repaintCurrentArea(); |
| 122 | } |
| 123 | }; |
| 124 | action.setEnabled(true); |
| 125 | action.setChecked(provider.getActiveFilterModes().contains(filterMode)); |
| 126 | return action; |
| 127 | }) |
| 128 | .collect(Collectors.toList()); |
| 129 | } |
| 130 | }); |
| 131 | } |
| 132 | |
| 133 | @Override |
| 134 | public void runWithEvent(@Nullable Event event) { |
| 135 | // TODO Also open the menu, need to figure out how |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | public static Action getSelectSortingModeAction(TimeGraphModelViewer viewer) { |
| 140 | return new SelectSortingModeAction(viewer); |
| 141 | } |
| 142 | |
| 143 | public static Action getSelectFilterModesAction(TimeGraphModelViewer viewer) { |
| 144 | return new SelectFilterModesAction(viewer); |
| 145 | } |
| 146 | |
| 147 | } |