KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > compare > internal > AddFromHistoryAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.compare.internal;
12
13 import java.util.ResourceBundle JavaDoc;
14 import java.lang.reflect.InvocationTargetException JavaDoc;
15
16 import org.eclipse.swt.widgets.Shell;
17
18 import org.eclipse.jface.dialogs.MessageDialog;
19 import org.eclipse.jface.dialogs.ProgressMonitorDialog;
20 import org.eclipse.jface.viewers.ISelection;
21
22 import org.eclipse.ui.actions.WorkspaceModifyOperation;
23 import org.eclipse.core.resources.*;
24 import org.eclipse.core.runtime.*;
25
26
27 public class AddFromHistoryAction extends BaseCompareAction {
28     
29     private static final String JavaDoc BUNDLE_NAME= "org.eclipse.compare.internal.AddFromHistoryAction"; //$NON-NLS-1$
30

31     public AddFromHistoryAction() {
32         // empty default implementation
33
}
34     
35     protected boolean isEnabled(ISelection selection) {
36         return Utilities.getResources(selection).length == 1;
37     }
38
39     protected void run(ISelection selection) {
40             
41         ResourceBundle JavaDoc bundle= ResourceBundle.getBundle(BUNDLE_NAME);
42         String JavaDoc title= Utilities.getString(bundle, "title"); //$NON-NLS-1$
43

44         Shell parentShell= CompareUIPlugin.getShell();
45         AddFromHistoryDialog dialog= null;
46
47         Object JavaDoc[] s= Utilities.getResources(selection);
48         
49         for (int i= 0; i < s.length; i++) {
50             Object JavaDoc o= s[i];
51             if (o instanceof IContainer) {
52                 IContainer container= (IContainer) o;
53                 
54                 ProgressMonitorDialog pmdialog= new ProgressMonitorDialog(parentShell);
55                 IProgressMonitor pm= pmdialog.getProgressMonitor();
56                 IFile[] states= null;
57                 try {
58                     states= container.findDeletedMembersWithHistory(IResource.DEPTH_INFINITE, pm);
59                 } catch (CoreException ex) {
60                     pm.done();
61                 }
62         
63                 if (states == null || states.length <= 0) {
64                     String JavaDoc msg= Utilities.getString(bundle, "noLocalHistoryError"); //$NON-NLS-1$
65
MessageDialog.openInformation(parentShell, title, msg);
66                     return;
67                 }
68         
69                 if (dialog == null) {
70                     dialog= new AddFromHistoryDialog(parentShell, bundle);
71                     dialog.setHelpContextId(ICompareContextIds.ADD_FROM_HISTORY_DIALOG);
72                 }
73                     
74                 if (dialog.select(container, states)) {
75                             
76                     AddFromHistoryDialog.HistoryInput[] selected= dialog.getSelected();
77
78                     if (selected != null && selected.length > 0) {
79                         try {
80                             updateWorkspace(bundle, parentShell, selected);
81     
82                         } catch (InterruptedException JavaDoc x) {
83                             // Do nothing. Operation has been canceled by user.
84

85                         } catch (InvocationTargetException JavaDoc x) {
86                             String JavaDoc reason= x.getTargetException().getMessage();
87                             MessageDialog.openError(parentShell, title, Utilities.getFormattedString(bundle, "replaceError", reason)); //$NON-NLS-1$
88
}
89                     }
90                 }
91             }
92         }
93     }
94     
95     void createContainers(IResource resource) throws CoreException {
96         IContainer container= resource.getParent();
97         if (container instanceof IFolder) {
98             IFolder parent= (IFolder) container;
99             if (parent != null && !parent.exists()) {
100                 createContainers(parent);
101                 parent.create(false, true, null);
102             }
103         }
104     }
105     
106     private void updateWorkspace(final ResourceBundle JavaDoc bundle, Shell shell,
107                     final AddFromHistoryDialog.HistoryInput[] selected)
108                                     throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
109         
110         WorkspaceModifyOperation operation= new WorkspaceModifyOperation() {
111             public void execute(IProgressMonitor pm) throws InvocationTargetException JavaDoc {
112                 try {
113                     String JavaDoc taskName= Utilities.getString(bundle, "taskName"); //$NON-NLS-1$
114
pm.beginTask(taskName, selected.length);
115                     
116                     for (int i= 0; i < selected.length; i++) {
117                         IFile file= selected[i].fFile;
118                         IFileState fileState= selected[i].fFileState;
119                         createContainers(file);
120                         
121                         SubProgressMonitor subMonitor= new SubProgressMonitor(pm, 1);
122                         try {
123                             file.create(fileState.getContents(), false, subMonitor);
124                         } finally {
125                             subMonitor.done();
126                         }
127                     }
128                 } catch (CoreException e) {
129                     throw new InvocationTargetException JavaDoc(e);
130                 } finally {
131                     pm.done();
132                 }
133             }
134         };
135         
136         ProgressMonitorDialog pmdialog= new ProgressMonitorDialog(shell);
137         pmdialog.run(false, true, operation);
138     }
139 }
140
Popular Tags