aboutsummaryrefslogtreecommitdiffstats
path: root/hierarchyviewer2/libs/hierarchyviewerlib/src/com/android/hierarchyviewerlib/ui/TreeViewOverview.java
blob: 3352df05ef2b82c92ca64ce4200bbd0775bfd953 (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
/*
 * Copyright (C) 2010 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.hierarchyviewerlib.ui;

import com.android.ddmuilib.ImageLoader;
import com.android.hierarchyviewerlib.models.TreeViewModel;
import com.android.hierarchyviewerlib.models.TreeViewModel.ITreeChangeListener;
import com.android.hierarchyviewerlib.ui.util.DrawableViewNode;
import com.android.hierarchyviewerlib.ui.util.DrawableViewNode.Point;
import com.android.hierarchyviewerlib.ui.util.DrawableViewNode.Rectangle;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Path;
import org.eclipse.swt.graphics.Transform;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;

public class TreeViewOverview extends Canvas implements ITreeChangeListener {

    private TreeViewModel mModel;

    private DrawableViewNode mTree;

    private Rectangle mViewport;

    private Transform mTransform;

    private Transform mInverse;

    private Rectangle mBounds = new Rectangle();

    private double mScale;

    private boolean mDragging = false;

    private DrawableViewNode mSelectedNode;

    private static Image sNotSelectedImage;

    private static Image sSelectedImage;

    private static Image sFilteredImage;

    private static Image sFilteredSelectedImage;

    public TreeViewOverview(Composite parent) {
        super(parent, SWT.NONE);

        mModel = TreeViewModel.getModel();
        mModel.addTreeChangeListener(this);

        loadResources();

        addPaintListener(mPaintListener);
        addMouseListener(mMouseListener);
        addMouseMoveListener(mMouseMoveListener);
        addListener(SWT.Resize, mResizeListener);
        addDisposeListener(mDisposeListener);

        mTransform = new Transform(Display.getDefault());
        mInverse = new Transform(Display.getDefault());

        loadAllData();
    }

    private void loadResources() {
        ImageLoader loader = ImageLoader.getLoader(this.getClass());
        sNotSelectedImage = loader.loadImage("not-selected.png", Display.getDefault()); //$NON-NLS-1$
        sSelectedImage = loader.loadImage("selected-small.png", Display.getDefault()); //$NON-NLS-1$
        sFilteredImage = loader.loadImage("filtered.png", Display.getDefault()); //$NON-NLS-1$
        sFilteredSelectedImage =
                loader.loadImage("selected-filtered-small.png", Display.getDefault()); //$NON-NLS-1$
    }

    private DisposeListener mDisposeListener = new DisposeListener() {
        @Override
        public void widgetDisposed(DisposeEvent e) {
            mModel.removeTreeChangeListener(TreeViewOverview.this);
            mTransform.dispose();
            mInverse.dispose();
        }
    };

    private MouseListener mMouseListener = new MouseListener() {

        @Override
        public void mouseDoubleClick(MouseEvent e) {
            // pass
        }

        @Override
        public void mouseDown(MouseEvent e) {
            boolean redraw = false;
            synchronized (TreeViewOverview.this) {
                if (mTree != null && mViewport != null) {
                    mDragging = true;
                    redraw = true;
                    handleMouseEvent(transformPoint(e.x, e.y));
                }
            }
            if (redraw) {
                mModel.removeTreeChangeListener(TreeViewOverview.this);
                mModel.setViewport(mViewport);
                mModel.addTreeChangeListener(TreeViewOverview.this);
                doRedraw();
            }
        }

        @Override
        public void mouseUp(MouseEvent e) {
            boolean redraw = false;
            synchronized (TreeViewOverview.this) {
                if (mTree != null && mViewport != null) {
                    mDragging = false;
                    redraw = true;
                    handleMouseEvent(transformPoint(e.x, e.y));

                    // Update bounds and transform only on mouse up. That way,
                    // you don't get confusing behaviour during mouse drag and
                    // it snaps neatly at the end
                    setBounds();
                    setTransform();
                }
            }
            if (redraw) {
                mModel.removeTreeChangeListener(TreeViewOverview.this);
                mModel.setViewport(mViewport);
                mModel.addTreeChangeListener(TreeViewOverview.this);
                doRedraw();
            }
        }

    };

    private MouseMoveListener mMouseMoveListener = new MouseMoveListener() {
        @Override
        public void mouseMove(MouseEvent e) {
            boolean moved = false;
            synchronized (TreeViewOverview.this) {
                if (mDragging) {
                    moved = true;
                    handleMouseEvent(transformPoint(e.x, e.y));
                }
            }
            if (moved) {
                mModel.removeTreeChangeListener(TreeViewOverview.this);
                mModel.setViewport(mViewport);
                mModel.addTreeChangeListener(TreeViewOverview.this);
                doRedraw();
            }
        }
    };

    private void handleMouseEvent(Point pt) {
        mViewport.x = pt.x - mViewport.width / 2;
        mViewport.y = pt.y - mViewport.height / 2;
        if (mViewport.x < mBounds.x) {
            mViewport.x = mBounds.x;
        }
        if (mViewport.y < mBounds.y) {
            mViewport.y = mBounds.y;
        }
        if (mViewport.x + mViewport.width > mBounds.x + mBounds.width) {
            mViewport.x = mBounds.x + mBounds.width - mViewport.width;
        }
        if (mViewport.y + mViewport.height > mBounds.y + mBounds.height) {
            mViewport.y = mBounds.y + mBounds.height - mViewport.height;
        }
    }

    private Point transformPoint(double x, double y) {
        float[] pt = {
                (float) x, (float) y
        };
        mInverse.transform(pt);
        return new Point(pt[0], pt[1]);
    }

    private Listener mResizeListener = new Listener() {
        @Override
        public void handleEvent(Event arg0) {
            synchronized (TreeViewOverview.this) {
                setTransform();
            }
            doRedraw();
        }
    };

    private PaintListener mPaintListener = new PaintListener() {
        @Override
        public void paintControl(PaintEvent e) {
            synchronized (TreeViewOverview.this) {
                if (mTree != null) {
                    e.gc.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_BLACK));
                    e.gc.setForeground(Display.getDefault().getSystemColor(SWT.COLOR_WHITE));
                    e.gc.fillRectangle(0, 0, getBounds().width, getBounds().height);
                    e.gc.setTransform(mTransform);
                    e.gc.setLineWidth((int) Math.ceil(0.7 / mScale));
                    Path connectionPath = new Path(Display.getDefault());
                    paintRecursive(e.gc, mTree, connectionPath);
                    e.gc.drawPath(connectionPath);
                    connectionPath.dispose();

                    if (mViewport != null) {
                        e.gc.setAlpha(50);
                        e.gc.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_WHITE));
                        e.gc.fillRectangle((int) mViewport.x, (int) mViewport.y, (int) Math
                                .ceil(mViewport.width), (int) Math.ceil(mViewport.height));

                        e.gc.setAlpha(255);
                        e.gc.setForeground(Display.getDefault().getSystemColor(
                                        SWT.COLOR_DARK_GRAY));
                        e.gc.setLineWidth((int) Math.ceil(2 / mScale));
                        e.gc.drawRectangle((int) mViewport.x, (int) mViewport.y, (int) Math
                                .ceil(mViewport.width), (int) Math.ceil(mViewport.height));
                    }
                }
            }
        }
    };

    private void paintRecursive(GC gc, DrawableViewNode node, Path connectionPath) {
        if (mSelectedNode == node && node.viewNode.filtered) {
            gc.drawImage(sFilteredSelectedImage, node.left, (int) Math.round(node.top));
        } else if (mSelectedNode == node) {
            gc.drawImage(sSelectedImage, node.left, (int) Math.round(node.top));
        } else if (node.viewNode.filtered) {
            gc.drawImage(sFilteredImage, node.left, (int) Math.round(node.top));
        } else {
            gc.drawImage(sNotSelectedImage, node.left, (int) Math.round(node.top));
        }
        int N = node.children.size();
        if (N == 0) {
            return;
        }
        float childSpacing =
                (1.0f * (DrawableViewNode.NODE_HEIGHT - 2 * TreeView.LINE_PADDING)) / N;
        for (int i = 0; i < N; i++) {
            DrawableViewNode child = node.children.get(i);
            paintRecursive(gc, child, connectionPath);
            float x1 = node.left + DrawableViewNode.NODE_WIDTH;
            float y1 =
                    (float) node.top + TreeView.LINE_PADDING + childSpacing * i + childSpacing / 2;
            float x2 = child.left;
            float y2 = (float) child.top + DrawableViewNode.NODE_HEIGHT / 2.0f;
            float cx1 = x1 + TreeView.BEZIER_FRACTION * DrawableViewNode.PARENT_CHILD_SPACING;
            float cy1 = y1;
            float cx2 = x2 - TreeView.BEZIER_FRACTION * DrawableViewNode.PARENT_CHILD_SPACING;
            float cy2 = y2;
            connectionPath.moveTo(x1, y1);
            connectionPath.cubicTo(cx1, cy1, cx2, cy2, x2, y2);
        }
    }

    private void doRedraw() {
        Display.getDefault().syncExec(new Runnable() {
            @Override
            public void run() {
                redraw();
            }
        });
    }

    public void loadAllData() {
        Display.getDefault().syncExec(new Runnable() {
            @Override
            public void run() {
                synchronized (this) {
                    mTree = mModel.getTree();
                    mSelectedNode = mModel.getSelection();
                    mViewport = mModel.getViewport();
                    setBounds();
                    setTransform();
                }
            }
        });
    }

    // Note the syncExec and then synchronized... It avoids deadlock
    @Override
    public void treeChanged() {
        Display.getDefault().syncExec(new Runnable() {
            @Override
            public void run() {
                synchronized (this) {
                    mTree = mModel.getTree();
                    mSelectedNode = mModel.getSelection();
                    mViewport = mModel.getViewport();
                    setBounds();
                    setTransform();
                }
            }
        });
        doRedraw();
    }

    private void setBounds() {
        if (mViewport != null && mTree != null) {
            mBounds.x = Math.min(mViewport.x, mTree.bounds.x);
            mBounds.y = Math.min(mViewport.y, mTree.bounds.y);
            mBounds.width =
                    Math.max(mViewport.x + mViewport.width, mTree.bounds.x + mTree.bounds.width)
                            - mBounds.x;
            mBounds.height =
                    Math.max(mViewport.y + mViewport.height, mTree.bounds.y + mTree.bounds.height)
                            - mBounds.y;
        } else if (mTree != null) {
            mBounds.x = mTree.bounds.x;
            mBounds.y = mTree.bounds.y;
            mBounds.width = mTree.bounds.x + mTree.bounds.width - mBounds.x;
            mBounds.height = mTree.bounds.y + mTree.bounds.height - mBounds.y;
        }
    }

    private void setTransform() {
        if (mTree != null) {

            mTransform.identity();
            mInverse.identity();
            final Point size = new Point();
            size.x = getBounds().width;
            size.y = getBounds().height;
            if (mBounds.width == 0 || mBounds.height == 0 || size.x == 0 || size.y == 0) {
                mScale = 1;
            } else {
                mScale = Math.min(size.x / mBounds.width, size.y / mBounds.height);
            }
            mTransform.scale((float) mScale, (float) mScale);
            mInverse.scale((float) mScale, (float) mScale);
            mTransform.translate((float) -mBounds.x, (float) -mBounds.y);
            mInverse.translate((float) -mBounds.x, (float) -mBounds.y);
            if (size.x / mBounds.width < size.y / mBounds.height) {
                mTransform.translate(0, (float) (size.y / mScale - mBounds.height) / 2);
                mInverse.translate(0, (float) (size.y / mScale - mBounds.height) / 2);
            } else {
                mTransform.translate((float) (size.x / mScale - mBounds.width) / 2, 0);
                mInverse.translate((float) (size.x / mScale - mBounds.width) / 2, 0);
            }
            mInverse.invert();
        }
    }

    @Override
    public void viewportChanged() {
        Display.getDefault().syncExec(new Runnable() {
            @Override
            public void run() {
                synchronized (this) {
                    mViewport = mModel.getViewport();
                    setBounds();
                    setTransform();
                }
            }
        });
        doRedraw();
    }

    @Override
    public void zoomChanged() {
        viewportChanged();
    }

    @Override
    public void selectionChanged() {
        synchronized (this) {
            mSelectedNode = mModel.getSelection();
        }
        doRedraw();
    }
}