KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ui > history > OpenLocalFileAction


1 /*******************************************************************************
2  * Copyright (c) 2006 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 package org.eclipse.team.internal.ui.history;
12
13 import java.io.IOException JavaDoc;
14 import java.io.InputStream JavaDoc;
15
16 import org.eclipse.core.resources.IFileState;
17 import org.eclipse.core.runtime.Platform;
18 import org.eclipse.core.runtime.content.IContentType;
19 import org.eclipse.jface.dialogs.MessageDialog;
20 import org.eclipse.jface.viewers.IStructuredSelection;
21 import org.eclipse.team.internal.ui.TeamUIMessages;
22 import org.eclipse.team.internal.ui.TeamUIPlugin;
23 import org.eclipse.ui.*;
24 import org.eclipse.ui.actions.BaseSelectionListenerAction;
25
26 public class OpenLocalFileAction extends BaseSelectionListenerAction {
27
28     protected OpenLocalFileAction(String JavaDoc text) {
29         super(text);
30     }
31
32     public void run() {
33         try {
34             IStructuredSelection structSel = getStructuredSelection();
35
36             Object JavaDoc[] objArray = structSel.toArray();
37
38             for (int i = 0; i < objArray.length; i++) {
39                 IFileState state = (IFileState) objArray[i];
40                 if (!state.exists()) {
41                     MessageDialog.openError(TeamUIPlugin.getActivePage().getActivePart().getSite().getShell(), TeamUIMessages.OpenRevisionAction_DeletedRevisionTitle, TeamUIMessages.OpenRevisionAction_DeletedRevisionMessage);
42                 } else {
43                     String JavaDoc id = getEditorID(state.getName(), state.getContents());
44                     IWorkbenchPage page = TeamUIPlugin.getActivePage();
45                     if (page != null) {
46                         page.openEditor(new FileRevisionEditorInput(state), id);
47                     }
48                 }
49
50             }
51
52         } catch (Exception JavaDoc e) {
53
54         }
55     }
56
57     /* private */String JavaDoc getEditorID(String JavaDoc fileName, InputStream JavaDoc contents) {
58         IWorkbench workbench = TeamUIPlugin.getPlugin().getWorkbench();
59         IEditorRegistry registry = workbench.getEditorRegistry();
60
61         IContentType type = null;
62         if (contents != null) {
63             try {
64                 type = Platform.getContentTypeManager().findContentTypeFor(contents, fileName);
65             } catch (IOException JavaDoc e) {
66
67             }
68         }
69         if (type == null) {
70             type = Platform.getContentTypeManager().findContentTypeFor(fileName);
71         }
72         IEditorDescriptor descriptor = registry.getDefaultEditor(fileName, type);
73         String JavaDoc id;
74         if (descriptor == null) {
75             id = "org.eclipse.ui.DefaultTextEditor"; //$NON-NLS-1$
76
} else {
77             id = descriptor.getId();
78         }
79
80         return id;
81     }
82
83 }
84
Popular Tags