1 /*
2  * Copyright (C) 2010 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.ide.eclipse.hierarchyviewer.views;
18 
19 import com.android.ddmuilib.ImageLoader;
20 import com.android.hierarchyviewerlib.HierarchyViewerDirector;
21 import com.android.hierarchyviewerlib.models.TreeViewModel;
22 import com.android.hierarchyviewerlib.models.TreeViewModel.ITreeChangeListener;
23 import com.android.hierarchyviewerlib.ui.LayoutViewer;
24 
25 import org.eclipse.jface.action.Action;
26 import org.eclipse.jface.action.IMenuManager;
27 import org.eclipse.jface.action.IToolBarManager;
28 import org.eclipse.jface.resource.ImageDescriptor;
29 import org.eclipse.swt.SWT;
30 import org.eclipse.swt.graphics.Image;
31 import org.eclipse.swt.layout.FillLayout;
32 import org.eclipse.swt.widgets.Composite;
33 import org.eclipse.swt.widgets.Display;
34 import org.eclipse.ui.IActionBars;
35 import org.eclipse.ui.part.ViewPart;
36 
37 public class LayoutView extends ViewPart implements ITreeChangeListener {
38 
39     public static final String ID = "com.android.ide.eclipse.hierarchyviewer.views.LayoutView"; //$NON-NLS-1$
40 
41     private LayoutViewer mLayoutViewer;
42 
43     private Image mOnBlack;
44 
45     private Image mOnWhite;
46 
47     private Action mShowExtrasAction = new Action("Show &Extras", Action.AS_CHECK_BOX) {
48         @Override
49         public void run() {
50             mLayoutViewer.setShowExtras(isChecked());
51         }
52     };
53 
54     private Action mLoadAllViewsAction = new Action("Load All &Views") {
55         @Override
56         public void run() {
57             HierarchyViewerDirector.getDirector().loadAllViews();
58             mShowExtrasAction.setChecked(true);
59             mLayoutViewer.setShowExtras(true);
60         }
61     };
62 
63     private Action mOnBlackWhiteAction = new Action("Change Background &Color") {
64         @Override
65         public void run() {
66             boolean newValue = !mLayoutViewer.getOnBlack();
67             mLayoutViewer.setOnBlack(newValue);
68             if (newValue) {
69                 setImageDescriptor(ImageDescriptor.createFromImage(mOnWhite));
70             } else {
71                 setImageDescriptor(ImageDescriptor.createFromImage(mOnBlack));
72             }
73         }
74     };
75 
76     @Override
createPartControl(Composite parent)77     public void createPartControl(Composite parent) {
78         mShowExtrasAction.setAccelerator(SWT.MOD1 + 'E');
79         ImageLoader imageLoader = ImageLoader.getLoader(HierarchyViewerDirector.class);
80         Image image = imageLoader.loadImage("show-extras.png", Display.getDefault()); //$NON-NLS-1$
81         mShowExtrasAction.setImageDescriptor(ImageDescriptor.createFromImage(image));
82         mShowExtrasAction.setToolTipText("Show images");
83         mShowExtrasAction.setEnabled(TreeViewModel.getModel().getTree() != null);
84 
85         mOnWhite = imageLoader.loadImage("on-white.png", Display.getDefault()); //$NON-NLS-1$
86         mOnBlack = imageLoader.loadImage("on-black.png", Display.getDefault()); //$NON-NLS-1$
87 
88         mOnBlackWhiteAction.setAccelerator(SWT.MOD1 + 'C');
89         mOnBlackWhiteAction.setImageDescriptor(ImageDescriptor.createFromImage(mOnWhite));
90         mOnBlackWhiteAction.setToolTipText("Change layout viewer background color");
91 
92         mLoadAllViewsAction.setAccelerator(SWT.MOD1 + 'V');
93         image = imageLoader.loadImage("load-all-views.png", Display.getDefault()); //$NON-NLS-1$
94         mLoadAllViewsAction.setImageDescriptor(ImageDescriptor.createFromImage(image));
95         mLoadAllViewsAction.setToolTipText("Load all view images");
96         mLoadAllViewsAction.setEnabled(TreeViewModel.getModel().getTree() != null);
97 
98         parent.setLayout(new FillLayout());
99 
100         mLayoutViewer = new LayoutViewer(parent);
101 
102         placeActions();
103 
104         TreeViewModel.getModel().addTreeChangeListener(this);
105     }
106 
placeActions()107     public void placeActions() {
108         IActionBars actionBars = getViewSite().getActionBars();
109 
110         IMenuManager mm = actionBars.getMenuManager();
111         mm.removeAll();
112         mm.add(mOnBlackWhiteAction);
113         mm.add(mShowExtrasAction);
114         mm.add(mLoadAllViewsAction);
115 
116         IToolBarManager tm = actionBars.getToolBarManager();
117         tm.removeAll();
118         tm.add(mOnBlackWhiteAction);
119         tm.add(mShowExtrasAction);
120         tm.add(mLoadAllViewsAction);
121     }
122 
123     @Override
dispose()124     public void dispose() {
125         super.dispose();
126         TreeViewModel.getModel().removeTreeChangeListener(this);
127     }
128 
129     @Override
setFocus()130     public void setFocus() {
131         mLayoutViewer.setFocus();
132     }
133 
134     @Override
selectionChanged()135     public void selectionChanged() {
136         // pass
137     }
138 
139     @Override
treeChanged()140     public void treeChanged() {
141         Display.getDefault().syncExec(new Runnable() {
142             @Override
143             public void run() {
144                 mLoadAllViewsAction.setEnabled(TreeViewModel.getModel().getTree() != null);
145                 mShowExtrasAction.setEnabled(TreeViewModel.getModel().getTree() != null);
146             }
147         });
148     }
149 
150     @Override
viewportChanged()151     public void viewportChanged() {
152         // pass
153     }
154 
155     @Override
zoomChanged()156     public void zoomChanged() {
157         // pass
158     }
159 
160 }
161