001: /**
002: * LibreSource
003: * Copyright (C) 2004-2008 Artenum SARL / INRIA
004: * http://www.libresource.org - contact@artenum.com
005: *
006: * This file is part of the LibreSource software,
007: * which can be used and distributed under license conditions.
008: * The license conditions are provided in the LICENSE.TXT file
009: * at the root path of the packaging that enclose this file.
010: * More information can be found at
011: * - http://dev.libresource.org/home/license
012: *
013: * Initial authors :
014: *
015: * Guillaume Bort / INRIA
016: * Francois Charoy / Universite Nancy 2
017: * Julien Forest / Artenum
018: * Claude Godart / Universite Henry Poincare
019: * Florent Jouille / INRIA
020: * Sebastien Jourdain / INRIA / Artenum
021: * Yves Lerumeur / Artenum
022: * Pascal Molli / Universite Henry Poincare
023: * Gerald Oster / INRIA
024: * Mariarosa Penzi / Artenum
025: * Gerard Sookahet / Artenum
026: * Raphael Tani / INRIA
027: *
028: * Contributors :
029: *
030: * Stephane Bagnier / Artenum
031: * Amadou Dia / Artenum-IUP Blois
032: * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
033: */package org.libresource.so6.plugin.compare;
034:
035: import org.eclipse.compare.CompareConfiguration;
036: import org.eclipse.compare.CompareEditorInput;
037: import org.eclipse.compare.CompareUI;
038: import org.eclipse.compare.IStreamContentAccessor;
039: import org.eclipse.compare.ITypedElement;
040: import org.eclipse.compare.ResourceNode;
041: import org.eclipse.compare.structuremergeviewer.Differencer;
042: import org.eclipse.compare.structuremergeviewer.IStructureComparator;
043:
044: import org.eclipse.core.resources.IFile;
045: import org.eclipse.core.resources.IResource;
046: import org.eclipse.core.runtime.CoreException;
047: import org.eclipse.core.runtime.IProgressMonitor;
048: import org.eclipse.core.runtime.IStatus;
049: import org.eclipse.core.runtime.Status;
050:
051: import org.eclipse.jface.dialogs.ProgressMonitorDialog;
052: import org.eclipse.jface.operation.IRunnableWithProgress;
053:
054: import org.eclipse.swt.graphics.Image;
055: import org.eclipse.swt.layout.GridData;
056: import org.eclipse.swt.widgets.Composite;
057: import org.eclipse.swt.widgets.Control;
058: import org.eclipse.swt.widgets.Shell;
059:
060: import org.eclipse.team.internal.ui.Utils;
061: import org.eclipse.team.ui.ISaveableWorkbenchPart;
062:
063: import org.eclipse.ui.IPropertyListener;
064: import org.eclipse.ui.IWorkbenchPartSite;
065:
066: import org.libresource.so6.plugin.Plugin;
067: import org.libresource.so6.plugin.core.So6Util;
068:
069: /**
070: * @author Guillaume Bort
071: */
072: import java.io.ByteArrayInputStream;
073: import java.io.File;
074: import java.io.FileInputStream;
075: import java.io.InputStream;
076:
077: import java.lang.reflect.InvocationTargetException;
078:
079: import java.util.Vector;
080:
081: /**
082: * DOCUMENT ME!
083: *
084: * @author $author$
085: * @version $Revision: 1.3 $
086: */
087: public class So6RefCopyCompareInput extends CompareEditorInput
088: implements ISaveableWorkbenchPart {
089: private IResource resource;
090: private ITypedElement left;
091: private ITypedElement right;
092: private Shell shell;
093:
094: /**
095: * Creates a new So6RefCopyCompareInput object.
096: *
097: * @param resource DOCUMENT ME!
098: *
099: * @throws Exception DOCUMENT ME!
100: */
101: public So6RefCopyCompareInput(IResource resource) throws Exception {
102: super (new CompareConfiguration());
103: this .resource = resource;
104:
105: if (resource instanceof IFile) {
106: left = new TypedBufferedContent((IFile) resource);
107: } else {
108: left = new FolderResourceNode(resource);
109: }
110:
111: right = new So6RefCopyResourceNode(resource);
112: setTitle("Compare " + left.getName() + " with "
113: + right.getName() + " (ref copy)");
114: initLabels();
115: }
116:
117: /**
118: * DOCUMENT ME!
119: *
120: * @param listener DOCUMENT ME!
121: */
122: public void addPropertyListener(IPropertyListener listener) {
123: // noop
124: }
125:
126: /**
127: * DOCUMENT ME!
128: *
129: * @param parent DOCUMENT ME!
130: */
131: public void createPartControl(Composite parent) {
132: Control c = super .createContents(parent);
133: c.setLayoutData(new GridData(GridData.FILL_BOTH));
134: }
135:
136: /**
137: * DOCUMENT ME!
138: */
139: public void dispose() {
140: }
141:
142: /**
143: * DOCUMENT ME!
144: *
145: * @param monitor DOCUMENT ME!
146: */
147: public void doSave(IProgressMonitor monitor) {
148: try {
149: saveChanges(monitor);
150: } catch (CoreException e) {
151: Utils.handle(e);
152: }
153: }
154:
155: /**
156: * DOCUMENT ME!
157: */
158: public void doSaveAs() {
159: // noop
160: }
161:
162: /**
163: * DOCUMENT ME!
164: *
165: * @return DOCUMENT ME!
166: */
167: public IWorkbenchPartSite getSite() {
168: return null;
169: }
170:
171: /**
172: * DOCUMENT ME!
173: *
174: * @return DOCUMENT ME!
175: */
176: public String getTitleToolTip() {
177: return null;
178: }
179:
180: /**
181: * DOCUMENT ME!
182: *
183: * @return DOCUMENT ME!
184: */
185: public boolean isDirty() {
186: return isSaveNeeded();
187: }
188:
189: /**
190: * DOCUMENT ME!
191: *
192: * @return DOCUMENT ME!
193: */
194: public boolean isSaveAsAllowed() {
195: return true;
196: }
197:
198: /**
199: * DOCUMENT ME!
200: *
201: * @return DOCUMENT ME!
202: */
203: public boolean isSaveOnCloseNeeded() {
204: return true;
205: }
206:
207: /**
208: * DOCUMENT ME!
209: *
210: * @param monitor DOCUMENT ME!
211: *
212: * @return DOCUMENT ME!
213: *
214: * @throws InvocationTargetException DOCUMENT ME!
215: * @throws InterruptedException DOCUMENT ME!
216: */
217: public Object prepareInput(IProgressMonitor monitor)
218: throws InvocationTargetException, InterruptedException {
219: Differencer differencer = new Differencer();
220: Object result = differencer.findDifferences(false, monitor,
221: null, null, left, right);
222:
223: return result;
224: }
225:
226: /**
227: * DOCUMENT ME!
228: *
229: * @param listener DOCUMENT ME!
230: */
231: public void removePropertyListener(IPropertyListener listener) {
232: // noop
233: }
234:
235: private void initLabels() {
236: CompareConfiguration cc = getCompareConfiguration();
237: cc.setLeftLabel(left.getName());
238: cc.setRightLabel(right.getName() + " (ref copy)");
239: cc.setLeftEditable(true);
240: cc.setRightEditable(false);
241: }
242:
243: class FolderResourceNode extends ResourceNode {
244: private Vector children;
245:
246: public FolderResourceNode(IResource resource) {
247: super (resource);
248: }
249:
250: public boolean equals(Object other) {
251: return other.hashCode() == hashCode();
252: }
253:
254: public Object[] getChildren() {
255: try {
256: if (children == null) {
257: Object[] o = super .getChildren();
258: children = new Vector();
259:
260: for (int i = 0; i < o.length; i++) {
261: if (!So6Util.isIgnored(((ResourceNode) o[i])
262: .getResource())) {
263: children
264: .add(new FolderResourceNode(
265: ((ResourceNode) o[i])
266: .getResource()));
267: }
268: }
269: }
270: } catch (Exception e) {
271: e.printStackTrace();
272: }
273:
274: if ((children == null) || (children.size() == 0)) {
275: return null;
276: }
277:
278: return children.toArray();
279: }
280:
281: public int hashCode() {
282: String path = "/"
283: + getResource().getProjectRelativePath().toString();
284:
285: return path.hashCode();
286: }
287: }
288:
289: class So6RefCopyResourceNode implements IStructureComparator,
290: IStreamContentAccessor, ITypedElement {
291: protected File refCopyItem;
292: private So6RefCopyResourceNode[] children;
293:
294: public So6RefCopyResourceNode(File refCopy) {
295: this .refCopyItem = refCopy;
296: }
297:
298: public So6RefCopyResourceNode(IResource resource)
299: throws Exception {
300: this .refCopyItem = So6Util.getRefCopy(resource);
301: }
302:
303: public boolean equals(Object other) {
304: return other.hashCode() == hashCode();
305: }
306:
307: public Object[] getChildren() {
308: if (children == null) {
309: if ((refCopyItem != null) && refCopyItem.isDirectory()) {
310: File[] list = refCopyItem.listFiles();
311: children = new So6RefCopyResourceNode[list.length];
312:
313: for (int i = 0; i < list.length; i++) {
314: children[i] = new So6RefCopyResourceNode(
315: list[i]);
316: }
317: }
318: }
319:
320: return children;
321: }
322:
323: public InputStream getContents() throws CoreException {
324: if (refCopyItem.isDirectory()) {
325: return new ByteArrayInputStream(new byte[0]);
326: }
327:
328: try {
329: return new FileInputStream(refCopyItem);
330: } catch (Exception e) {
331: throw new CoreException(new Status(IStatus.ERROR,
332: Plugin.ID, IStatus.OK, e.getMessage(), e));
333: }
334: }
335:
336: public Image getImage() {
337: return CompareUI.getImage(getType());
338: }
339:
340: public String getName() {
341: return refCopyItem.getName();
342: }
343:
344: public String getType() {
345: if (refCopyItem == null) {
346: return UNKNOWN_TYPE;
347: }
348:
349: if (refCopyItem.isDirectory()) {
350: return FOLDER_TYPE;
351: }
352:
353: String name = refCopyItem.getName();
354: name = name.substring(name.lastIndexOf('.') + 1);
355:
356: return (name.length() == 0) ? UNKNOWN_TYPE : name;
357: }
358:
359: public int hashCode() {
360: String path = refCopyItem.getAbsolutePath().substring(
361: refCopyItem.getAbsolutePath()
362: .lastIndexOf("REFCOPY") + 7);
363:
364: return path.hashCode();
365: }
366: }
367:
368: class TypedBufferedContent extends ResourceNode {
369: public TypedBufferedContent(IFile resource) {
370: super (resource);
371: }
372:
373: public void fireChange() {
374: fireContentChanged();
375: }
376:
377: public ITypedElement replace(ITypedElement child,
378: ITypedElement other) {
379: return null;
380: }
381:
382: public void setContent(byte[] contents) {
383: if (contents == null) {
384: contents = new byte[0];
385: }
386:
387: final InputStream is = new ByteArrayInputStream(contents);
388: IRunnableWithProgress runnable = new IRunnableWithProgress() {
389: public void run(IProgressMonitor monitor)
390: throws InvocationTargetException,
391: InterruptedException {
392: try {
393: IFile file = (IFile) resource;
394:
395: if (is != null) {
396: if (!file.exists()) {
397: file.create(is, false, monitor);
398: } else {
399: file.setContents(is, false, true,
400: monitor);
401: }
402: } else {
403: file.delete(false, true, monitor);
404: }
405: } catch (CoreException e) {
406: throw new InvocationTargetException(e);
407: }
408: }
409: };
410:
411: try {
412: new ProgressMonitorDialog(shell).run(false, false,
413: runnable);
414: } catch (InvocationTargetException e) {
415: e.printStackTrace();
416: } catch (InterruptedException e) {
417: // Ignore
418: }
419:
420: fireContentChanged();
421: }
422:
423: protected InputStream createStream() throws CoreException {
424: return ((IFile) getResource()).getContents();
425: }
426: }
427: }
|