Commit | Line | Data |
---|---|---|
cfcfd964 PT |
1 | /******************************************************************************* |
2 | * Copyright (c) 2015 Ericsson | |
3 | * | |
4 | * All rights reserved. This program and the accompanying materials are | |
5 | * made available under the terms of the Eclipse Public License v1.0 which | |
6 | * accompanies this distribution, and is available at | |
7 | * http://www.eclipse.org/legal/epl-v10.html | |
8 | * | |
9 | * Contributors: | |
10 | * Patrick Tasse - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
13 | package org.eclipse.tracecompass.tmf.ui.widgets.timegraph.dialogs; | |
14 | ||
15 | import java.util.ArrayList; | |
16 | import java.util.Arrays; | |
17 | import java.util.List; | |
18 | ||
19 | import org.eclipse.jface.action.Action; | |
20 | import org.eclipse.jface.viewers.Viewer; | |
21 | import org.eclipse.jface.viewers.ViewerFilter; | |
22 | import org.eclipse.tracecompass.internal.tmf.ui.Activator; | |
23 | import org.eclipse.tracecompass.internal.tmf.ui.ITmfImageConstants; | |
24 | import org.eclipse.tracecompass.internal.tmf.ui.Messages; | |
25 | import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.TimeGraphViewer; | |
26 | import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.ITimeGraphEntry; | |
27 | ||
28 | /** | |
29 | * Action to show the time graph filter dialog and add a filter to hide the | |
30 | * unselected elements in the time graph viewer. The filter is removed if all | |
31 | * elements are selected. | |
32 | * | |
33 | * @since 2.0 | |
34 | */ | |
35 | public class ShowFilterDialogAction extends Action { | |
36 | ||
37 | /** The filter dialog */ | |
38 | private final TimeGraphFilterDialog fFilterDialog; | |
39 | ||
40 | /** The time graph viewer */ | |
41 | private final TimeGraphViewer fTimeGraphViewer; | |
42 | ||
43 | /** | |
44 | * This filter simply keeps a list of elements that should be filtered out. | |
45 | * All the other elements will be shown. | |
46 | * By default and when the list is set to null, all elements are shown. | |
47 | */ | |
48 | private class RawViewerFilter extends ViewerFilter { | |
49 | ||
50 | private List<? extends ITimeGraphEntry> fFiltered = null; | |
51 | ||
52 | public RawViewerFilter(List<? extends ITimeGraphEntry> filteredElements) { | |
53 | fFiltered = filteredElements; | |
54 | } | |
55 | ||
56 | public void setFiltered(List<? extends ITimeGraphEntry> objects) { | |
57 | fFiltered = objects; | |
58 | } | |
59 | ||
60 | public List<? extends ITimeGraphEntry> getFiltered() { | |
61 | return fFiltered; | |
62 | } | |
63 | ||
64 | @Override | |
65 | public boolean select(Viewer viewer, Object parentElement, Object element) { | |
66 | if (fFiltered == null) { | |
67 | return true; | |
68 | } | |
69 | return !fFiltered.contains(element); | |
70 | } | |
71 | } | |
72 | ||
73 | /** | |
74 | * Constructor | |
75 | * | |
76 | * @param timeGraphViewer | |
77 | * the time graph viewer | |
78 | */ | |
79 | public ShowFilterDialogAction(TimeGraphViewer timeGraphViewer) { | |
80 | fFilterDialog = new TimeGraphFilterDialog(timeGraphViewer.getControl().getShell()); | |
81 | fTimeGraphViewer = timeGraphViewer; | |
82 | setText(Messages.ShowFilterDialogAction_FilterActionNameText); | |
83 | setToolTipText(Messages.ShowFilterDialogAction_FilterActionToolTipText); | |
84 | setImageDescriptor(Activator.getDefault().getImageDescripterFromPath(ITmfImageConstants.IMG_UI_FILTERS)); | |
85 | } | |
86 | ||
87 | @Override | |
88 | public void run() { | |
89 | ITimeGraphEntry[] topInput = fTimeGraphViewer.getTimeGraphContentProvider().getElements(fTimeGraphViewer.getInput()); | |
90 | if (topInput != null) { | |
91 | List<? extends ITimeGraphEntry> allElements = listAllInputs(Arrays.asList(topInput)); | |
92 | fFilterDialog.setInput(fTimeGraphViewer.getInput()); | |
93 | fFilterDialog.setTitle(Messages.TmfTimeFilterDialog_WINDOW_TITLE); | |
94 | fFilterDialog.setMessage(Messages.TmfTimeFilterDialog_MESSAGE); | |
95 | fFilterDialog.setExpandedElements(allElements.toArray()); | |
96 | RawViewerFilter rawViewerFilter = null; | |
97 | for (ViewerFilter filter : fTimeGraphViewer.getFilters()) { | |
98 | if (filter instanceof RawViewerFilter) { | |
99 | rawViewerFilter = (RawViewerFilter) filter; | |
100 | } | |
101 | } | |
102 | if (rawViewerFilter != null && rawViewerFilter.getFiltered() != null) { | |
103 | ArrayList<? extends ITimeGraphEntry> nonFilteredElements = new ArrayList<>(allElements); | |
104 | nonFilteredElements.removeAll(rawViewerFilter.getFiltered()); | |
105 | fFilterDialog.setInitialElementSelections(nonFilteredElements); | |
106 | } else { | |
107 | fFilterDialog.setInitialElementSelections(allElements); | |
108 | } | |
109 | fFilterDialog.open(); | |
110 | if (fFilterDialog.getResult() != null) { | |
111 | if (fFilterDialog.getResult().length != allElements.size()) { | |
112 | List<? extends ITimeGraphEntry> filteredElements = new ArrayList<>(allElements); | |
113 | filteredElements.removeAll(Arrays.asList(fFilterDialog.getResult())); | |
114 | if (rawViewerFilter == null) { | |
115 | rawViewerFilter = new RawViewerFilter(filteredElements); | |
116 | addFilter(rawViewerFilter); | |
117 | } else { | |
118 | rawViewerFilter.setFiltered(filteredElements); | |
119 | refresh(); | |
120 | } | |
121 | } else if (rawViewerFilter != null) { | |
122 | removeFilter(rawViewerFilter); | |
123 | } | |
124 | } | |
125 | } | |
126 | } | |
127 | ||
128 | /** | |
129 | * Get the filter dialog. | |
130 | * | |
131 | * @return the filter dialog | |
132 | */ | |
133 | public TimeGraphFilterDialog getFilterDialog() { | |
134 | return fFilterDialog; | |
135 | } | |
136 | ||
137 | /** | |
138 | * Add a viewer filter. | |
139 | * | |
140 | * @param filter | |
141 | * The filter object to be added to the viewer | |
142 | */ | |
143 | protected void addFilter(ViewerFilter filter) { | |
144 | fTimeGraphViewer.addFilter(filter); | |
145 | } | |
146 | ||
147 | /** | |
148 | * Remove a viewer filter. | |
149 | * | |
150 | * @param filter | |
151 | * The filter object to be removed from the viewer | |
152 | */ | |
153 | protected void removeFilter(ViewerFilter filter) { | |
154 | fTimeGraphViewer.removeFilter(filter); | |
155 | } | |
156 | ||
157 | /** | |
158 | * Refresh the viewer. | |
159 | */ | |
160 | protected void refresh() { | |
161 | fTimeGraphViewer.refresh(); | |
162 | } | |
163 | ||
164 | /** | |
165 | * Explores the list of top-level inputs and returns all the inputs. | |
166 | * | |
167 | * @param inputs | |
168 | * The top-level inputs | |
169 | * @return All the inputs | |
170 | */ | |
171 | private List<? extends ITimeGraphEntry> listAllInputs(List<? extends ITimeGraphEntry> inputs) { | |
172 | ArrayList<ITimeGraphEntry> items = new ArrayList<>(); | |
173 | for (ITimeGraphEntry entry : inputs) { | |
174 | items.add(entry); | |
175 | if (entry.hasChildren()) { | |
176 | items.addAll(listAllInputs(entry.getChildren())); | |
177 | } | |
178 | } | |
179 | return items; | |
180 | } | |
181 | } |