1 /* ====================================================================
2 * The Apache Software License, Version 1.1
3 *
4 * Copyright (c) 2000 The Apache Software Foundation. All rights
5 * reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
17 * distribution.
18 *
19 * 3. The end-user documentation included with the redistribution,
20 * if any, must include the following acknowledgment:
21 * "This product includes software developed by the
22 * Apache Software Foundation (http://www.apache.org/)."
23 * Alternately, this acknowledgment may appear in the software itself,
24 * if and wherever such third-party acknowledgments normally appear.
25 *
26 * 4. The names "Apache" and "Apache Software Foundation" must
27 * not be used to endorse or promote products derived from this
28 * software without prior written permission. For written
29 * permission, please contact apache@apache.org.
30 *
31 * 5. Products derived from this software may not be called "Apache",
32 * nor may "Apache" appear in their name, without prior written
33 * permission of the Apache Software Foundation.
34 *
35 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46 * SUCH DAMAGE.
47 * ====================================================================
48 *
49 * This software consists of voluntary contributions made by many
50 * individuals on behalf of the Apache Software Foundation. For more
51 * information on the Apache Software Foundation, please see
52 * <http://www.apache.org/>.
53 *
54 * Portions of this software are based upon public domain software
55 * originally written at the National Center for Supercomputing Applications,
56 * University of Illinois, Urbana-Champaign.
57 */
58 package com.bonevich.eclipse.cvsgrapher.editors;
59
60 import com.bonevich.eclipse.cvsgrapher.CvsGraphPluginMessages;
61 import com.bonevich.eclipse.cvsgrapher.CvsGraphPluginResources;
62 import com.bonevich.eclipse.cvsgrapher.model.CvsGraphModel;
63
64 import org.apache.commons.logging.Log;
65 import org.apache.commons.logging.LogFactory;
66 import org.eclipse.core.resources.IFile;
67 import org.eclipse.core.resources.IMarker;
68 import org.eclipse.core.resources.IResource;
69 import org.eclipse.core.runtime.IProgressMonitor;
70 import org.eclipse.draw2d.FigureCanvas;
71 import org.eclipse.draw2d.geometry.Dimension;
72 import org.eclipse.gef.DefaultEditDomain;
73 import org.eclipse.gef.EditPartViewer;
74 import org.eclipse.gef.GraphicalViewer;
75 import org.eclipse.gef.editparts.ScalableFreeformRootEditPart;
76 import org.eclipse.gef.ui.parts.GraphicalEditor;
77 import org.eclipse.gef.ui.parts.GraphicalViewerKeyHandler;
78 import org.eclipse.gef.ui.parts.ScrollingGraphicalViewer;
79 import org.eclipse.jface.dialogs.ProgressMonitorDialog;
80 import org.eclipse.jface.operation.IRunnableWithProgress;
81 import org.eclipse.swt.SWT;
82 import org.eclipse.swt.events.KeyEvent;
83 import org.eclipse.swt.graphics.Rectangle;
84 import org.eclipse.team.core.RepositoryProvider;
85 import org.eclipse.team.core.TeamException;
86 import org.eclipse.team.internal.ccvs.core.CVSProviderPlugin;
87 import org.eclipse.team.internal.ccvs.core.ICVSRemoteFile;
88 import org.eclipse.team.internal.ccvs.core.ILogEntry;
89 import org.eclipse.team.internal.ccvs.core.resources.CVSWorkspaceRoot;
90 import org.eclipse.team.internal.ccvs.ui.CVSUIPlugin;
91 import org.eclipse.ui.IEditorInput;
92 import org.eclipse.ui.IEditorSite;
93 import org.eclipse.ui.PartInitException;
94
95 import java.lang.reflect.InvocationTargetException;
96
97 /***
98 * The CVS Graph GEF editor.
99 * @author jbonevic
100 * @todo add tooltip to editor tab (class level todo)
101 */
102 public final class CvsGraphEditor
103 extends GraphicalEditor
104 implements CvsGraphPluginResources
105 {
106 private static final Log _logger = LogFactory.getLog(CvsGraphEditor.class);
107
108 public static final String EDITOR_ID = "com.bonevich.eclipse.cvsgrapher.editor";
109
110 private ICVSRemoteFile _remoteFile = null;
111 private ILogEntry[] _entries = null;
112
113 public CvsGraphEditor()
114 {
115 setEditDomain(new DefaultEditDomain(this));
116 }
117
118 /***
119 * @see org.eclipse.ui.IEditorPart#init(IEditorSite, IEditorInput)
120 * @todo add tooltip to editor tab
121 */
122 public void init(IEditorSite iSite, IEditorInput iInput) throws PartInitException
123 {
124 setSite(iSite);
125 setInput(iInput);
126 initializeCvsRemoteFile();
127 }
128
129 /***
130 * @see org.eclipse.gef.ui.parts.GraphicalEditor#initializeGraphicalViewer()
131 */
132 protected void initializeGraphicalViewer()
133 {
134 if (_logger.isDebugEnabled())
135 {
136 _logger.debug("Opening editor for " + _remoteFile.getRepositoryRelativePath());
137 _logger.debug("Viewer = " + getGraphicalViewer());
138 }
139 EditPartViewer viewer = getGraphicalViewer();
140 viewer.setContents(new CvsGraphModel(_entries));
141 }
142
143 /***
144 * @see org.eclipse.ui.IEditorPart#isSaveAsAllowed()
145 */
146 public boolean isSaveAsAllowed()
147 {
148 return false;
149 }
150
151 /***
152 * @see org.eclipse.ui.IEditorPart#doSave(IProgressMonitor)
153 */
154 public void doSave(IProgressMonitor iMonitor)
155 {
156 }
157
158 /***
159 * @see org.eclipse.ui.IEditorPart#doSaveAs()
160 */
161 public void doSaveAs()
162 {
163 }
164
165 /***
166 * @see org.eclipse.ui.IEditorPart#gotoMarker(IMarker)
167 */
168 public void gotoMarker(IMarker iMarker)
169 {
170 }
171
172 /***
173 * @see org.eclipse.ui.IEditorPart#isDirty()
174 */
175 public boolean isDirty()
176 {
177 return false;
178 }
179
180 /***
181 * @see org.eclipse.gef.ui.parts.GraphicalEditor#configureGraphicalViewer()
182 */
183 protected void configureGraphicalViewer()
184 {
185 super.configureGraphicalViewer();
186 GraphicalViewer viewer = getGraphicalViewer();
187
188 ScalableFreeformRootEditPart rootPart = new ScalableFreeformRootEditPart();
189 viewer.setRootEditPart(rootPart);
190 viewer.setEditPartFactory(new CvsGraphEditPartFactory());
191
192 ((FigureCanvas) viewer.getControl()).setScrollBarVisibility(FigureCanvas.ALWAYS);
193 viewer.setContextMenu(new CvsGraphContextMenu(viewer, rootPart.getZoomManager()));
194 viewer.setKeyHandler(new CvsGraphViewerKeyHandler(viewer));
195 }
196
197 private void initializeCvsRemoteFile()
198 {
199 IResource resource = (IResource) getEditorInput().getAdapter(IResource.class);
200 if (resource instanceof IFile)
201 {
202 IFile file = (IFile) resource;
203
204 if (_logger.isDebugEnabled())
205 {
206 _logger.debug("resource = " + file);
207 }
208
209 RepositoryProvider teamProvider =
210 RepositoryProvider.getProvider(
211 file.getProject(),
212 CVSProviderPlugin.getTypeId()
213 );
214 if (teamProvider != null)
215 {
216 try
217 {
218 _remoteFile = (ICVSRemoteFile) CVSWorkspaceRoot.getRemoteResourceFor(file);
219 setTitle(CvsGraphPluginMessages.bind(CVSGRAPHER_EDITOR_TITLE_WITHARG, _remoteFile.getName()));
220 initializeLogEntries();
221 }
222 catch (TeamException e)
223 {
224 CVSUIPlugin.openError(getSite().getShell(), null, null, e);
225 }
226 }
227 return;
228 }
229 _remoteFile = null;
230 setTitle(CvsGraphPluginMessages.bind(CVSGRAPHER_EDITOR_TITLE));
231 }
232
233 private void initializeLogEntries()
234 {
235 try
236 {
237 new ProgressMonitorDialog(
238 getSite().getShell()
239 ).run(
240 true,
241 true,
242 new IRunnableWithProgress()
243 {
244 public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException
245 {
246 try
247 {
248 _entries = _remoteFile.getLogEntries(monitor);
249 }
250 catch (TeamException e)
251 {
252 throw new InvocationTargetException(e);
253 }
254 }
255 }
256 );
257 }
258 catch (InterruptedException ignore)
259 {
260 // ignore cancellation
261 }
262 catch (InvocationTargetException e)
263 {
264 CVSUIPlugin.openError(getSite().getShell(), null, null, e);
265 }
266 }
267
268 ///////////////////////////////////////////
269 // Inner classes
270
271 /***
272 * Extends the basic key handler to add appropriate scrolling behavior for PageDown,
273 * PageUp, Home, and End key events.
274 *
275 * @author jbonevic
276 */
277 final class CvsGraphViewerKeyHandler extends GraphicalViewerKeyHandler
278 {
279 public CvsGraphViewerKeyHandler(GraphicalViewer viewer)
280 {
281 super(viewer);
282 }
283
284 public boolean keyPressed(KeyEvent event)
285 {
286 if (event.keyCode == SWT.PAGE_DOWN || event.keyCode == SWT.PAGE_UP ||
287 event.keyCode == SWT.HOME || event.keyCode == SWT.END )
288 {
289 ScrollingGraphicalViewer viewer = (ScrollingGraphicalViewer) getGraphicalViewer();
290 FigureCanvas canvas = (FigureCanvas) viewer.getControl();
291
292 int x = canvas.getViewport().getViewLocation().x;
293 int y = canvas.getViewport().getViewLocation().y;
294 int dx = 0;
295 int dy = 0;
296
297 Dimension viewingArea = canvas.getViewport().getClientArea().getSize();
298 Rectangle totalArea = canvas.getBounds();
299
300 switch (event.keyCode)
301 {
302 case SWT.PAGE_DOWN : dx = x; dy = y + viewingArea.height; break;
303 case SWT.PAGE_UP : dx = x; dy = y - viewingArea.height; break;
304 case SWT.HOME : dx = 0; dy = y; break;
305 case SWT.END : dx = totalArea.width; dy = y; break;
306 default: dx = x; dy = y; break;
307 }
308
309 canvas.scrollSmoothTo(dx, dy);
310 return true;
311 }
312 return super.keyPressed(event);
313 }
314 }
315 }
This page was automatically generated by Maven