KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ui > actions > OpenRevisionAction


1 /*******************************************************************************
2  * Copyright (c) 2006, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.team.internal.ui.actions;
13
14 import java.lang.reflect.InvocationTargetException JavaDoc;
15
16 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.core.runtime.IProgressMonitor;
18 import org.eclipse.jface.dialogs.MessageDialog;
19 import org.eclipse.jface.operation.IRunnableWithProgress;
20 import org.eclipse.jface.viewers.IStructuredSelection;
21 import org.eclipse.team.core.history.IFileRevision;
22 import org.eclipse.team.internal.ui.TeamUIMessages;
23 import org.eclipse.team.internal.ui.Utils;
24 import org.eclipse.team.internal.ui.history.AbstractHistoryCategory;
25 import org.eclipse.team.ui.history.HistoryPage;
26 import org.eclipse.ui.PlatformUI;
27 import org.eclipse.ui.actions.BaseSelectionListenerAction;
28 import org.eclipse.ui.progress.IProgressService;
29
30 public class OpenRevisionAction extends BaseSelectionListenerAction {
31
32     private IStructuredSelection selection;
33     private HistoryPage page;
34     
35     public OpenRevisionAction(String JavaDoc text) {
36         super(text);
37     }
38
39     public void run() {
40             IStructuredSelection structSel = selection;
41
42             Object JavaDoc[] objArray = structSel.toArray();
43
44             for (int i = 0; i < objArray.length; i++) {
45                 Object JavaDoc tempRevision = objArray[i];
46                 //If not a revision, don't try opening
47
if (tempRevision instanceof AbstractHistoryCategory)
48                     continue;
49                 
50                 final IFileRevision revision = (IFileRevision) tempRevision;
51                 if (revision == null || !revision.exists()) {
52                     MessageDialog.openError(page.getSite().getShell(), TeamUIMessages.OpenRevisionAction_DeletedRevTitle, TeamUIMessages.OpenRevisionAction_DeletedRevMessage);
53                 } else {
54                     IRunnableWithProgress runnable = new IRunnableWithProgress() {
55                         public void run(IProgressMonitor monitor) throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
56                             try {
57                                 Utils.openEditor(page.getSite().getPage(), revision, monitor);
58                             } catch (CoreException e) {
59                                 throw new InvocationTargetException JavaDoc(e);
60                             }
61                             
62                         }
63                     };
64                     
65                     IProgressService progressService = PlatformUI.getWorkbench().getProgressService();
66                     try {
67                         progressService.run(false, false, runnable);
68                     } catch (InvocationTargetException JavaDoc e) {
69                         Utils.handleError(page.getSite().getShell(), e, TeamUIMessages.OpenRevisionAction_ErrorTitle, TeamUIMessages.OpenRevisionAction_ErrorMessage);
70                     } catch (InterruptedException JavaDoc e) {
71                     }
72                 }
73
74             }
75     }
76     
77     protected boolean updateSelection(IStructuredSelection selection) {
78         this.selection = selection;
79         return shouldShow();
80     }
81     
82     public void setPage(HistoryPage page) {
83         this.page = page;
84     }
85     
86     private boolean shouldShow() {
87         IStructuredSelection structSel = selection;
88         Object JavaDoc[] objArray = structSel.toArray();
89         
90         if (objArray.length == 0)
91             return false;
92         
93         for (int i = 0; i < objArray.length; i++) {
94             //Don't bother showing if this a category
95
if (objArray[i] instanceof AbstractHistoryCategory)
96                 return false;
97             
98             IFileRevision revision = (IFileRevision) objArray[i];
99             //check to see if any of the selected revisions are deleted revisions
100
if (revision != null && !revision.exists())
101                 return false;
102         }
103         
104         return true;
105     }
106
107 }
108
Popular Tags