Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: e62dee7c48be0d41e87b1e4fe2c44fce57311e12 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
 * Licensed Materials - Property of IBM,
 * WebSphere Studio Workbench
 * (c) Copyright IBM Corp 2001
 */
package org.eclipse.compare.internal;

import java.io.InputStream;
import java.util.ResourceBundle;

import org.eclipse.swt.widgets.Shell;

import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;

import org.eclipse.core.resources.*;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.ui.IActionDelegate;
import org.eclipse.compare.*;


public class ReplaceWithEditionAction implements IActionDelegate {
		
	private static final String ACTION_LABEL= "Replace with Edition";
	private ISelection fSelection;
	

	public void run(IAction action) {
			
		Object[] s= Utilities.toArray(fSelection);
		
		for (int i= 0; i < s.length; i++) {
			Object o= s[i];
			if (o instanceof IFile) {
				replaceFromHistory((IFile)o);
				continue;
			}
			if (o instanceof IAdaptable) {
				IAdaptable a= (IAdaptable) o;
				Object adapter= a.getAdapter(IResource.class);
				if (adapter instanceof IFile)
					replaceFromHistory((IFile)adapter);
				continue;
			}
		}
	}
	
	public void selectionChanged(IAction a, ISelection s) {
		fSelection= s;
	}

	void replaceFromHistory(IFile file) {
				
		Shell parent= CompareUIPlugin.getShell();

		IFileState states[]= null;
		try {
			states= file.getHistory(null);
		} catch (CoreException ex) {		
			MessageDialog.openError(parent, ACTION_LABEL, ex.getMessage());
			return;
		}
		
		if (states != null && states.length > 0) {

			ITypedElement base= new ResourceNode(file);
		
			ITypedElement[] editions= new ITypedElement[states.length];
			for (int i= 0; i < states.length; i++)
				editions[i]= new HistoryItem(base, states[i]);

			ResourceBundle bundle= ResourceBundle.getBundle("org.eclipse.compare.internal.ReplaceWithEditionAction");
			EditionSelectionDialog d= new EditionSelectionDialog(parent, bundle);

			ITypedElement ti= d.selectEdition(base, editions, null);			
			if (ti instanceof IStreamContentAccessor) {
				try {
					InputStream is= ((IStreamContentAccessor)ti).getContents();
					file.setContents(is, false, true, null);
				} catch (CoreException ex) {
					MessageDialog.openError(parent, ACTION_LABEL, ex.getMessage());
				}
			}
		} else
			MessageDialog.openInformation(parent, ACTION_LABEL, "No local editions available for selected resource.");
	}
}

Back to the top