View Javadoc

1   /*
2    * This file is a part of CAST project.
3    * (c) Copyright 2009, AGH University of Science & Technology
4    * https://caribou.iisg.agh.edu.pl/trac/cast
5    *
6    * Licensed under the Eclipse Public License, Version 1.0 (the "License").
7    * You may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    * http://www.eclipse.org/legal/epl-v10.html
10   */
11  /*
12   * File: OutlineMiniatureView.java
13   * Created: 2009-02-05
14   * Author: skalkow, kpietak
15   * $Id: OutlineMiniatureView.java 2309 2009-02-05 22:37:58Z skalkow $
16   */
17  
18  package pl.edu.agh.cast.ui.outline;
19  
20  import org.eclipse.draw2d.Label;
21  import org.eclipse.draw2d.LightweightSystem;
22  import org.eclipse.draw2d.parts.ScrollableThumbnail;
23  import org.eclipse.swt.SWT;
24  import org.eclipse.swt.widgets.Canvas;
25  import org.eclipse.swt.widgets.Composite;
26  import org.eclipse.ui.IEditorPart;
27  import org.eclipse.ui.IPartListener;
28  import org.eclipse.ui.IWorkbenchPart;
29  
30  import pl.edu.agh.cast.editor.AbstractEditor;
31  import pl.edu.agh.cast.editor.input.DiagramEditorInput;
32  import pl.edu.agh.cast.ui.AbstractConfigurableView;
33  import pl.edu.agh.cast.util.Messages;
34  
35  /**
36   * Miniature outline view.
37   * 
38   * @author AGH CAST Team
39   */
40  public class OutlineMiniatureView extends AbstractConfigurableView implements IPartListener {
41  
42  	/**
43  	 * Constructor.
44  	 */
45  	public OutlineMiniatureView() {
46  		// nothing to do
47  	}
48  
49  	/**
50  	 * View Id.
51  	 */
52  	public static final String ID = "pl.edu.agh.cast.view.miniature"; //$NON-NLS-1$
53  
54  	private LightweightSystem lightweightSystem;
55  
56  	private ScrollableThumbnail scrollableThumbnail;
57  
58  	private Object currentDiagram;
59  
60  	/**
61  	 * {@inheritDoc}
62  	 * 
63  	 * @see pl.edu.agh.cast.ui.AbstractConfigurableView#activate()
64  	 */
65  	@Override
66  	protected void activate() {
67  		getSite().getPage().addPartListener(this);
68  	}
69  
70  	/**
71  	 * {@inheritDoc}
72  	 * 
73  	 * @see pl.edu.agh.cast.ui.AbstractTreeView#dispose()
74  	 */
75  	@Override
76  	public void dispose() {
77  		super.dispose();
78  		getSite().getPage().removePartListener(this);
79  	}
80  
81  	/**
82  	 * 
83  	 * {@inheritDoc}
84  	 * 
85  	 * @see org.eclipse.ui.IPartListener#partActivated(org.eclipse.ui.IWorkbenchPart)
86  	 */
87  	public void partActivated(IWorkbenchPart part) {
88  		updateActiveEditor();
89  	}
90  
91  	/**
92  	 * 
93  	 * {@inheritDoc}
94  	 * 
95  	 * @see org.eclipse.ui.IPartListener#partBroughtToTop(org.eclipse.ui.IWorkbenchPart)
96  	 */
97  	public void partBroughtToTop(IWorkbenchPart part) {
98  		updateActiveEditor();
99  	}
100 
101 	/**
102 	 * 
103 	 * {@inheritDoc}
104 	 * 
105 	 * @see org.eclipse.ui.IPartListener#partClosed(org.eclipse.ui.IWorkbenchPart)
106 	 */
107 	public void partClosed(IWorkbenchPart part) {
108 		updateActiveEditor();
109 	}
110 
111 	/**
112 	 * 
113 	 * {@inheritDoc}
114 	 * 
115 	 * @see org.eclipse.ui.IPartListener#partDeactivated(org.eclipse.ui.IWorkbenchPart)
116 	 */
117 	public void partDeactivated(IWorkbenchPart part) {
118 		updateActiveEditor();
119 	}
120 
121 	/**
122 	 * 
123 	 * {@inheritDoc}
124 	 * 
125 	 * @see org.eclipse.ui.IPartListener#partOpened(org.eclipse.ui.IWorkbenchPart)
126 	 */
127 	public void partOpened(IWorkbenchPart part) {
128 		updateActiveEditor();
129 	}
130 
131 	/**
132 	 * 
133 	 * {@inheritDoc}
134 	 * 
135 	 * @see pl.edu.agh.cast.ui.AbstractConfigurableView#contributeToActionBars()
136 	 */
137 	@Override
138 	protected void contributeToActionBars() {
139 		// nothing to do
140 
141 	}
142 
143 	/**
144 	 * 
145 	 * {@inheritDoc}
146 	 * 
147 	 * @see pl.edu.agh.cast.ui.AbstractConfigurableView#hookContextMenu()
148 	 */
149 	@Override
150 	protected void hookContextMenu() {
151 		// nothing to do
152 
153 	}
154 
155 	/**
156 	 * 
157 	 * {@inheritDoc}
158 	 * 
159 	 * @see pl.edu.agh.cast.ui.AbstractConfigurableView#hookDoubleClickAction()
160 	 */
161 	@Override
162 	protected void hookDoubleClickAction() {
163 		// nothing to do
164 
165 	}
166 
167 	/**
168 	 * 
169 	 * {@inheritDoc}
170 	 * 
171 	 * @see pl.edu.agh.cast.ui.AbstractConfigurableView#makeActions()
172 	 */
173 	@Override
174 	protected void makeActions() {
175 		// nothing to do
176 
177 	}
178 
179 	/**
180 	 * 
181 	 * {@inheritDoc}
182 	 * 
183 	 * @see org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
184 	 */
185 	@Override
186 	public void createPartControl(Composite parent) {
187 		activate();
188 		// create canvas for diagram's miniature
189 		Canvas canvas = new Canvas(parent, SWT.BORDER);
190 		this.lightweightSystem = new LightweightSystem(canvas);
191 	}
192 
193 	/**
194 	 * 
195 	 * {@inheritDoc}
196 	 * 
197 	 * @see org.eclipse.ui.part.WorkbenchPart#setFocus()
198 	 */
199 	@Override
200 	public void setFocus() {
201 		// nothing to do
202 
203 	}
204 
205 	/**
206 	 * Checks if active editor is instance of {@link AbstractEditor} and updates the miniature view.
207 	 */
208 	@SuppressWarnings("unchecked")
209 	private void updateActiveEditor() {
210 		IEditorPart activeEditor = getSite().getPage().getActiveEditor();
211 		if (activeEditor instanceof AbstractEditor) {
212 			AbstractEditor editor = (AbstractEditor)activeEditor;
213 			DiagramEditorInput input = (DiagramEditorInput)editor.getEditorInput();
214 			Object diagram = input.getDiagram();
215 			if (!diagram.equals(currentDiagram)) {
216 				currentDiagram = diagram;
217 
218 				// create miniature of diagram
219 				scrollableThumbnail = new ScrollableThumbnail(editor.getEditorViewport());
220 				scrollableThumbnail.setSource(editor.getPrintableLayer());
221 				lightweightSystem.setContents(scrollableThumbnail);
222 			}
223 		} else {
224 			currentDiagram = null;
225 
226 			// set blank figure instead of miniature
227 			lightweightSystem.setContents(new Label(Messages.OutlineMiniatureView_0)); //$NON-NLS-1$
228 		}
229 	}
230 }