aboutsummaryrefslogtreecommitdiffstats
path: root/ddms/libs/ddmuilib/src/com/android/ddmuilib/log/event/EventValueSelector.java
blob: e7c5196fa7b61dafa475486e9b02c35788b0b1bb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
/*
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.ddmuilib.log.event;

import com.android.ddmlib.log.EventContainer.CompareMethod;
import com.android.ddmlib.log.EventContainer.EventValueType;
import com.android.ddmlib.log.EventLogParser;
import com.android.ddmlib.log.EventValueDescription;
import com.android.ddmuilib.log.event.EventDisplay.OccurrenceDisplayDescriptor;
import com.android.ddmuilib.log.event.EventDisplay.ValueDisplayDescriptor;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import java.util.ArrayList;
import java.util.Map;
import java.util.Set;

final class EventValueSelector extends Dialog {
    private static final int DLG_WIDTH = 400;
    private static final int DLG_HEIGHT = 300;

    private Shell mParent;
    private Shell mShell;
    private boolean mEditStatus;
    private Combo mEventCombo;
    private Combo mValueCombo;
    private Combo mSeriesCombo;
    private Button mDisplayPidCheckBox;
    private Combo mFilterCombo;
    private Combo mFilterMethodCombo;
    private Text mFilterValue;
    private Button mOkButton;

    private EventLogParser mLogParser;
    private OccurrenceDisplayDescriptor mDescriptor;

    /** list of event integer in the order of the combo. */
    private Integer[] mEventTags;

    /** list of indices in the {@link EventValueDescription} array of the current event
     * that are of type string. This lets us get back the {@link EventValueDescription} from the
     * index in the Series {@link Combo}.
     */
    private final ArrayList<Integer> mSeriesIndices = new ArrayList<Integer>();

    public EventValueSelector(Shell parent) {
        super(parent, SWT.DIALOG_TRIM | SWT.BORDER | SWT.APPLICATION_MODAL);
    }

    /**
     * Opens the display option dialog to edit a new descriptor.
     * @param decriptorClass the class of the object to instantiate. Must extend
     * {@link OccurrenceDisplayDescriptor}
     * @param logParser
     * @return true if the object is to be created, false if the creation was canceled.
     */
    boolean open(Class<? extends OccurrenceDisplayDescriptor> descriptorClass,
            EventLogParser logParser) {
        try {
            OccurrenceDisplayDescriptor descriptor = descriptorClass.newInstance();
            setModified();
            return open(descriptor, logParser);
        } catch (InstantiationException e) {
            return false;
        } catch (IllegalAccessException e) {
            return false;
        }
    }

    /**
     * Opens the display option dialog, to edit a {@link OccurrenceDisplayDescriptor} object or
     * a {@link ValueDisplayDescriptor} object.
     * @param descriptor The descriptor to edit.
     * @return true if the object was modified.
     */
    boolean open(OccurrenceDisplayDescriptor descriptor, EventLogParser logParser) {
        // make a copy of the descriptor as we'll use a working copy.
        if (descriptor instanceof ValueDisplayDescriptor) {
            mDescriptor = new ValueDisplayDescriptor((ValueDisplayDescriptor)descriptor);
        } else if (descriptor instanceof OccurrenceDisplayDescriptor) {
            mDescriptor = new OccurrenceDisplayDescriptor(descriptor);
        } else {
            return false;
        }

        mLogParser = logParser;

        createUI();

        if (mParent == null || mShell == null) {
            return false;
        }

        loadValueDescriptor();

        checkValidity();

        // Set the dialog size.
        try {
            mShell.setMinimumSize(DLG_WIDTH, DLG_HEIGHT);
            Rectangle r = mParent.getBounds();
            // get the center new top left.
            int cx = r.x + r.width/2;
            int x = cx - DLG_WIDTH / 2;
            int cy = r.y + r.height/2;
            int y = cy - DLG_HEIGHT / 2;
            mShell.setBounds(x, y, DLG_WIDTH, DLG_HEIGHT);
        } catch (Exception e) {
            e.printStackTrace();
        }

        mShell.layout();

        // actually open the dialog
        mShell.open();

        // event loop until the dialog is closed.
        Display display = mParent.getDisplay();
        while (!mShell.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }

        return mEditStatus;
    }

    OccurrenceDisplayDescriptor getDescriptor() {
        return mDescriptor;
    }

    private void createUI() {
        GridData gd;

        mParent = getParent();
        mShell = new Shell(mParent, getStyle());
        mShell.setText("Event Display Configuration");

        mShell.setLayout(new GridLayout(2, false));

        Label l = new Label(mShell, SWT.NONE);
        l.setText("Event:");

        mEventCombo = new Combo(mShell, SWT.DROP_DOWN | SWT.READ_ONLY);
        mEventCombo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

        // the event tag / event name map
        Map<Integer, String> eventTagMap = mLogParser.getTagMap();
        Map<Integer, EventValueDescription[]> eventInfoMap = mLogParser.getEventInfoMap();
        Set<Integer> keys = eventTagMap.keySet();
        ArrayList<Integer> list = new ArrayList<Integer>();
        for (Integer i : keys) {
            if (eventInfoMap.get(i) != null) {
                String eventName = eventTagMap.get(i);
                mEventCombo.add(eventName);

                list.add(i);
            }
        }
        mEventTags = list.toArray(new Integer[list.size()]);

        mEventCombo.addSelectionListener(new SelectionAdapter() {
            /* (non-Javadoc)
             * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
             */
            @Override
            public void widgetSelected(SelectionEvent e) {
                handleEventComboSelection();
                setModified();
            }
        });

        l = new Label(mShell, SWT.NONE);
        l.setText("Value:");

        mValueCombo = new Combo(mShell, SWT.DROP_DOWN | SWT.READ_ONLY);
        mValueCombo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
        mValueCombo.addSelectionListener(new SelectionAdapter() {
            /* (non-Javadoc)
             * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
             */
            @Override
            public void widgetSelected(SelectionEvent e) {
                handleValueComboSelection();
                setModified();
            }
        });

        l = new Label(mShell, SWT.NONE);
        l.setText("Series Name:");

        mSeriesCombo = new Combo(mShell, SWT.DROP_DOWN | SWT.READ_ONLY);
        mSeriesCombo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
        mSeriesCombo.addSelectionListener(new SelectionAdapter() {
            /* (non-Javadoc)
             * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
             */
            @Override
            public void widgetSelected(SelectionEvent e) {
                handleSeriesComboSelection();
                setModified();
            }
        });

        // empty comp
        new Composite(mShell, SWT.NONE).setLayoutData(gd = new GridData());
        gd.heightHint = gd.widthHint = 0;

        mDisplayPidCheckBox = new Button(mShell, SWT.CHECK);
        mDisplayPidCheckBox.setText("Also Show pid");
        mDisplayPidCheckBox.setEnabled(false);
        mDisplayPidCheckBox.addSelectionListener(new SelectionAdapter() {
            /* (non-Javadoc)
             * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
             */
            @Override
            public void widgetSelected(SelectionEvent e) {
                mDescriptor.includePid = mDisplayPidCheckBox.getSelection();
                setModified();
            }
        });

        l = new Label(mShell, SWT.NONE);
        l.setText("Filter By:");

        mFilterCombo = new Combo(mShell, SWT.DROP_DOWN | SWT.READ_ONLY);
        mFilterCombo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
        mFilterCombo.addSelectionListener(new SelectionAdapter() {
            /* (non-Javadoc)
             * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
             */
            @Override
            public void widgetSelected(SelectionEvent e) {
                handleFilterComboSelection();
                setModified();
            }
        });

        l = new Label(mShell, SWT.NONE);
        l.setText("Filter Method:");

        mFilterMethodCombo = new Combo(mShell, SWT.DROP_DOWN | SWT.READ_ONLY);
        mFilterMethodCombo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
        for (CompareMethod method : CompareMethod.values()) {
            mFilterMethodCombo.add(method.toString());
        }
        mFilterMethodCombo.select(0);
        mFilterMethodCombo.addSelectionListener(new SelectionAdapter() {
            /* (non-Javadoc)
             * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
             */
            @Override
            public void widgetSelected(SelectionEvent e) {
                handleFilterMethodComboSelection();
                setModified();
            }
        });

        l = new Label(mShell, SWT.NONE);
        l.setText("Filter Value:");

        mFilterValue = new Text(mShell, SWT.BORDER | SWT.SINGLE);
        mFilterValue.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
        mFilterValue.addModifyListener(new ModifyListener() {
            @Override
            public void modifyText(ModifyEvent e) {
                if (mDescriptor.filterValueIndex != -1) {
                    // get the current selection in the event combo
                    int index = mEventCombo.getSelectionIndex();

                    if (index != -1) {
                        // match it to an event
                        int eventTag = mEventTags[index];
                        mDescriptor.eventTag = eventTag;

                        // get the EventValueDescription for this tag
                        EventValueDescription valueDesc = mLogParser.getEventInfoMap()
                            .get(eventTag)[mDescriptor.filterValueIndex];

                        // let the EventValueDescription convert the String value into an object
                        // of the proper type.
                        mDescriptor.filterValue = valueDesc.getObjectFromString(
                                mFilterValue.getText().trim());
                        setModified();
                    }
                }
            }
        });

        // add a separator spanning the 2 columns

        l = new Label(mShell, SWT.SEPARATOR | SWT.HORIZONTAL);
        gd = new GridData(GridData.FILL_HORIZONTAL);
        gd.horizontalSpan = 2;
        l.setLayoutData(gd);

        // add a composite to hold the ok/cancel button, no matter what the columns size are.
        Composite buttonComp = new Composite(mShell, SWT.NONE);
        gd = new GridData(GridData.FILL_HORIZONTAL);
        gd.horizontalSpan = 2;
        buttonComp.setLayoutData(gd);
        GridLayout gl;
        buttonComp.setLayout(gl = new GridLayout(6, true));
        gl.marginHeight = gl.marginWidth = 0;

        Composite padding = new Composite(mShell, SWT.NONE);
        padding.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

        mOkButton = new Button(buttonComp, SWT.PUSH);
        mOkButton.setText("OK");
        mOkButton.setLayoutData(new GridData(GridData.CENTER));
        mOkButton.addSelectionListener(new SelectionAdapter() {
            /* (non-Javadoc)
             * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
             */
            @Override
            public void widgetSelected(SelectionEvent e) {
                mShell.close();
            }
        });

        padding = new Composite(mShell, SWT.NONE);
        padding.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

        padding = new Composite(mShell, SWT.NONE);
        padding.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

        Button cancelButton = new Button(buttonComp, SWT.PUSH);
        cancelButton.setText("Cancel");
        cancelButton.setLayoutData(new GridData(GridData.CENTER));
        cancelButton.addSelectionListener(new SelectionAdapter() {
            /* (non-Javadoc)
             * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
             */
            @Override
            public void widgetSelected(SelectionEvent e) {
                // cancel the edit
                mEditStatus = false;
                mShell.close();
            }
        });

        padding = new Composite(mShell, SWT.NONE);
        padding.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

        mShell.addListener(SWT.Close, new Listener() {
            @Override
            public void handleEvent(Event event) {
                event.doit = true;
            }
        });
    }

    private void setModified() {
        mEditStatus = true;
    }

    private void handleEventComboSelection() {
        // get the current selection in the event combo
        int index = mEventCombo.getSelectionIndex();

        if (index != -1) {
            // match it to an event
            int eventTag = mEventTags[index];
            mDescriptor.eventTag = eventTag;

            // get the EventValueDescription for this tag
            EventValueDescription[] values = mLogParser.getEventInfoMap().get(eventTag);

            // fill the combo for the values
            mValueCombo.removeAll();
            if (values != null) {
                if (mDescriptor instanceof ValueDisplayDescriptor) {
                    ValueDisplayDescriptor valueDescriptor = (ValueDisplayDescriptor)mDescriptor;

                    mValueCombo.setEnabled(true);
                    for (EventValueDescription value : values) {
                        mValueCombo.add(value.toString());
                    }

                    if (valueDescriptor.valueIndex != -1) {
                        mValueCombo.select(valueDescriptor.valueIndex);
                    } else {
                        mValueCombo.clearSelection();
                    }
                } else {
                    mValueCombo.setEnabled(false);
                }

                // fill the axis combo
                mSeriesCombo.removeAll();
                mSeriesCombo.setEnabled(false);
                mSeriesIndices.clear();
                int axisIndex = 0;
                int selectionIndex = -1;
                for (EventValueDescription value : values) {
                    if (value.getEventValueType() == EventValueType.STRING) {
                        mSeriesCombo.add(value.getName());
                        mSeriesCombo.setEnabled(true);
                        mSeriesIndices.add(axisIndex);

                        if (mDescriptor.seriesValueIndex != -1 &&
                                mDescriptor.seriesValueIndex == axisIndex) {
                            selectionIndex = axisIndex;
                        }
                    }
                    axisIndex++;
                }

                if (mSeriesCombo.isEnabled()) {
                    mSeriesCombo.add("default (pid)", 0 /* index */);
                    mSeriesIndices.add(0 /* index */, -1 /* value */);

                    // +1 because we added another item at index 0
                    mSeriesCombo.select(selectionIndex + 1);

                    if (selectionIndex >= 0) {
                        mDisplayPidCheckBox.setSelection(mDescriptor.includePid);
                        mDisplayPidCheckBox.setEnabled(true);
                    } else {
                        mDisplayPidCheckBox.setEnabled(false);
                        mDisplayPidCheckBox.setSelection(false);
                    }
                } else {
                    mDisplayPidCheckBox.setSelection(false);
                    mDisplayPidCheckBox.setEnabled(false);
                }

                // fill the filter combo
                mFilterCombo.setEnabled(true);
                mFilterCombo.removeAll();
                mFilterCombo.add("(no filter)");
                for (EventValueDescription value : values) {
                    mFilterCombo.add(value.toString());
                }

                // select the current filter
                mFilterCombo.select(mDescriptor.filterValueIndex + 1);
                mFilterMethodCombo.select(getFilterMethodIndex(mDescriptor.filterCompareMethod));

                // fill the current filter value
                if (mDescriptor.filterValueIndex != -1) {
                    EventValueDescription valueInfo = values[mDescriptor.filterValueIndex];
                    if (valueInfo.checkForType(mDescriptor.filterValue)) {
                        mFilterValue.setText(mDescriptor.filterValue.toString());
                    } else {
                        mFilterValue.setText("");
                    }
                } else {
                    mFilterValue.setText("");
                }
            } else {
                disableSubCombos();
            }
        } else {
            disableSubCombos();
        }

        checkValidity();
    }

    /**
     *
     */
    private void disableSubCombos() {
        mValueCombo.removeAll();
        mValueCombo.clearSelection();
        mValueCombo.setEnabled(false);

        mSeriesCombo.removeAll();
        mSeriesCombo.clearSelection();
        mSeriesCombo.setEnabled(false);

        mDisplayPidCheckBox.setEnabled(false);
        mDisplayPidCheckBox.setSelection(false);

        mFilterCombo.removeAll();
        mFilterCombo.clearSelection();
        mFilterCombo.setEnabled(false);

        mFilterValue.setEnabled(false);
        mFilterValue.setText("");
        mFilterMethodCombo.setEnabled(false);
    }

    private void handleValueComboSelection() {
        ValueDisplayDescriptor valueDescriptor = (ValueDisplayDescriptor)mDescriptor;

        // get the current selection in the value combo
        int index = mValueCombo.getSelectionIndex();
        valueDescriptor.valueIndex = index;

        // for now set the built-in name

        // get the current selection in the event combo
        int eventIndex = mEventCombo.getSelectionIndex();

        // match it to an event
        int eventTag = mEventTags[eventIndex];

        // get the EventValueDescription for this tag
        EventValueDescription[] values = mLogParser.getEventInfoMap().get(eventTag);

        valueDescriptor.valueName = values[index].getName();

        checkValidity();
    }

    private void handleSeriesComboSelection() {
        // get the current selection in the axis combo
        int index = mSeriesCombo.getSelectionIndex();

        // get the actual value index from the list.
        int valueIndex = mSeriesIndices.get(index);

        mDescriptor.seriesValueIndex = valueIndex;

        if (index > 0) {
            mDisplayPidCheckBox.setEnabled(true);
            mDisplayPidCheckBox.setSelection(mDescriptor.includePid);
        } else {
            mDisplayPidCheckBox.setSelection(false);
            mDisplayPidCheckBox.setEnabled(false);
        }
    }

    private void handleFilterComboSelection() {
        // get the current selection in the axis combo
        int index = mFilterCombo.getSelectionIndex();

        // decrement index by 1 since the item 0 means
        // no filter (index = -1), and the rest is offset by 1
        index--;

        mDescriptor.filterValueIndex = index;

        if (index != -1) {
            mFilterValue.setEnabled(true);
            mFilterMethodCombo.setEnabled(true);
            if (mDescriptor.filterValue instanceof String) {
                mFilterValue.setText((String)mDescriptor.filterValue);
            }
        } else {
            mFilterValue.setText("");
            mFilterValue.setEnabled(false);
            mFilterMethodCombo.setEnabled(false);
        }
    }

    private void handleFilterMethodComboSelection() {
        // get the current selection in the axis combo
        int index = mFilterMethodCombo.getSelectionIndex();
        CompareMethod method = CompareMethod.values()[index];

        mDescriptor.filterCompareMethod = method;
    }

    /**
     * Returns the index of the filter method
     * @param filterCompareMethod the {@link CompareMethod} enum.
     */
    private int getFilterMethodIndex(CompareMethod filterCompareMethod) {
        CompareMethod[] values = CompareMethod.values();
        for (int i = 0 ; i < values.length ; i++) {
            if (values[i] == filterCompareMethod) {
                return i;
            }
        }
        return -1;
    }


    private void loadValueDescriptor() {
        // get the index from the eventTag.
        int eventIndex = 0;
        int comboIndex = -1;
        for (int i : mEventTags) {
            if (i == mDescriptor.eventTag) {
                comboIndex = eventIndex;
                break;
            }
            eventIndex++;
        }

        if (comboIndex == -1) {
            mEventCombo.clearSelection();
        } else {
            mEventCombo.select(comboIndex);
        }

        // get the event from the descriptor
        handleEventComboSelection();
    }

    private void checkValidity() {
        mOkButton.setEnabled(mEventCombo.getSelectionIndex() != -1 &&
                (((mDescriptor instanceof ValueDisplayDescriptor) == false) ||
                        mValueCombo.getSelectionIndex() != -1));
    }
}