View | Details | Raw Unified | Return to bug 265030 | Differences between
and this patch

Collapse All | Expand All

(-)compare/org/eclipse/compare/internal/GenerateDiffFileWizard.java (-30 / +369 lines)
Lines 15-27 Link Here
15
15
16
import java.io.File;
16
import java.io.File;
17
import java.io.IOException;
17
import java.io.IOException;
18
import java.io.InputStream;
18
import java.net.MalformedURLException;
19
import java.net.MalformedURLException;
19
import java.net.URL;
20
import java.net.URL;
20
import java.util.ArrayList;
21
import java.util.ArrayList;
22
import java.util.HashMap;
21
import java.util.Iterator;
23
import java.util.Iterator;
22
import java.util.List;
24
import java.util.List;
25
import java.util.Map;
26
import java.util.Set;
23
27
28
import org.eclipse.compare.CompareConfiguration;
29
import org.eclipse.compare.CompareEditorInput;
30
import org.eclipse.compare.ResourceNode;
31
import org.eclipse.compare.contentmergeviewer.ITokenComparator;
32
import org.eclipse.compare.contentmergeviewer.TokenComparator;
33
import org.eclipse.compare.internal.ResourceCompareInput.FilteredBufferedResourceNode;
24
import org.eclipse.compare.internal.merge.DocumentMerger;
34
import org.eclipse.compare.internal.merge.DocumentMerger;
35
import org.eclipse.compare.internal.merge.DocumentMerger.IDocumentMergerInput;
36
import org.eclipse.compare.structuremergeviewer.DiffNode;
37
import org.eclipse.compare.structuremergeviewer.Differencer;
38
import org.eclipse.compare.structuremergeviewer.ICompareInput;
25
import org.eclipse.core.resources.IContainer;
39
import org.eclipse.core.resources.IContainer;
26
import org.eclipse.core.resources.IFile;
40
import org.eclipse.core.resources.IFile;
27
import org.eclipse.core.resources.IProject;
41
import org.eclipse.core.resources.IProject;
Lines 35-45 Link Here
35
import org.eclipse.jface.dialogs.Dialog;
49
import org.eclipse.jface.dialogs.Dialog;
36
import org.eclipse.jface.dialogs.IDialogConstants;
50
import org.eclipse.jface.dialogs.IDialogConstants;
37
import org.eclipse.jface.dialogs.IDialogSettings;
51
import org.eclipse.jface.dialogs.IDialogSettings;
52
import org.eclipse.jface.dialogs.IPageChangingListener;
38
import org.eclipse.jface.dialogs.MessageDialog;
53
import org.eclipse.jface.dialogs.MessageDialog;
54
import org.eclipse.jface.dialogs.PageChangingEvent;
39
import org.eclipse.jface.dialogs.TitleAreaDialog;
55
import org.eclipse.jface.dialogs.TitleAreaDialog;
40
import org.eclipse.jface.resource.ImageDescriptor;
56
import org.eclipse.jface.resource.ImageDescriptor;
57
import org.eclipse.jface.text.Document;
41
import org.eclipse.jface.text.IDocument;
58
import org.eclipse.jface.text.IDocument;
59
import org.eclipse.jface.text.Position;
60
import org.eclipse.jface.viewers.CheckStateChangedEvent;
61
import org.eclipse.jface.viewers.CheckboxTreeViewer;
42
import org.eclipse.jface.viewers.DoubleClickEvent;
62
import org.eclipse.jface.viewers.DoubleClickEvent;
63
import org.eclipse.jface.viewers.ICheckStateListener;
43
import org.eclipse.jface.viewers.IDoubleClickListener;
64
import org.eclipse.jface.viewers.IDoubleClickListener;
44
import org.eclipse.jface.viewers.ISelection;
65
import org.eclipse.jface.viewers.ISelection;
45
import org.eclipse.jface.viewers.ISelectionChangedListener;
66
import org.eclipse.jface.viewers.ISelectionChangedListener;
Lines 69-74 Link Here
69
import org.eclipse.swt.widgets.Listener;
90
import org.eclipse.swt.widgets.Listener;
70
import org.eclipse.swt.widgets.Shell;
91
import org.eclipse.swt.widgets.Shell;
71
import org.eclipse.swt.widgets.Text;
92
import org.eclipse.swt.widgets.Text;
93
import org.eclipse.swt.widgets.TreeItem;
94
import org.eclipse.ui.IWorkbenchPart;
95
import org.eclipse.ui.dialogs.ContainerCheckedTreeViewer;
72
import org.eclipse.ui.model.BaseWorkbenchContentProvider;
96
import org.eclipse.ui.model.BaseWorkbenchContentProvider;
73
import org.eclipse.ui.model.WorkbenchLabelProvider;
97
import org.eclipse.ui.model.WorkbenchLabelProvider;
74
import org.eclipse.ui.views.navigator.ResourceComparator;
98
import org.eclipse.ui.views.navigator.ResourceComparator;
Lines 88-95 Link Here
88
		final GenerateDiffFileWizard wizard = new GenerateDiffFileWizard(merger, rightToLeft);
112
		final GenerateDiffFileWizard wizard = new GenerateDiffFileWizard(merger, rightToLeft);
89
		wizard.setWindowTitle(title);
113
		wizard.setWindowTitle(title);
90
		WizardDialog dialog = new WizardDialog(shell, wizard);
114
		WizardDialog dialog = new WizardDialog(shell, wizard);
115
		
116
		dialog.addPageChangingListener(new IPageChangingListener() {
117
118
			public void handlePageChanging(PageChangingEvent event) {
119
				if(event.getTargetPage() instanceof LocationPage) {
120
					LocationPage page = (LocationPage) event.getTargetPage();
121
					page.updateChangesControl();
122
				}
123
			}
124
			
125
		});
126
		
127
		dialog.setMinimumPageSize(INITIAL_WIDTH, INITIAL_HEIGHT);
128
		dialog.open();
129
	}
130
	public static void run(IResource[] input, Shell shell) {
131
		final String title = CompareMessages.GenerateLocalDiff_title;
132
		final GenerateDiffFileWizard wizard = new GenerateDiffFileWizard(input);
133
		wizard.setWindowTitle(title);
134
		WizardDialog dialog = new WizardDialog(shell, wizard);
135
		
136
		dialog.addPageChangingListener(new IPageChangingListener() {
137
138
			public void handlePageChanging(PageChangingEvent event) {
139
				if(event.getTargetPage() instanceof LocationPage) {
140
					LocationPage page = (LocationPage) event.getTargetPage();
141
					page.updateChangesControl();
142
				}
143
			}
144
			
145
		});
146
		
91
		dialog.setMinimumPageSize(INITIAL_WIDTH, INITIAL_HEIGHT);
147
		dialog.setMinimumPageSize(INITIAL_WIDTH, INITIAL_HEIGHT);
148
		dialog.setBlockOnOpen(false);
92
		dialog.open();
149
		dialog.open();
150
		
93
	}
151
	}
94
152
95
	protected class DirectionSelectionPage extends WizardPage {
153
	protected class DirectionSelectionPage extends WizardPage {
Lines 209-219 Link Here
209
		protected IPath[] foldersToCreate;
267
		protected IPath[] foldersToCreate;
210
		protected int selectedLocation;
268
		protected int selectedLocation;
211
269
270
		private ContainerCheckedTreeViewer viewer;
271
		private LocalChangesContentProvider provider;
212
		/**
272
		/**
213
		 * The default values store used to initialize the selections.
273
		 * The default values store used to initialize the selections.
214
		 */
274
		 */
215
		private final DefaultValuesStore store;
275
		private final DefaultValuesStore store;
216
276
        private Button chgSelectAll;
277
        private Button chgDeselectAll;
217
278
218
		class LocationPageContentProvider extends BaseWorkbenchContentProvider {
279
		class LocationPageContentProvider extends BaseWorkbenchContentProvider {
219
			//Never show closed projects
280
			//Never show closed projects
Lines 448-453 Link Here
448
			this.store= store;
509
			this.store= store;
449
		}
510
		}
450
511
512
		public void updateChangesControl() {
513
			Differencer dif = new Differencer();
514
			if(directionSelectionPage.isRightToLeft())
515
				startNode = (DiffNode) dif.findDifferences(false, null, null, null, 
516
														new FilteredBufferedResourceNode(rightResource), 
517
														new FilteredBufferedResourceNode(leftResource));
518
			else if(!directionSelectionPage.isRightToLeft())
519
				startNode = (DiffNode) dif.findDifferences(false, null, null, null, 
520
														new FilteredBufferedResourceNode(leftResource), 
521
														new FilteredBufferedResourceNode(rightResource));
522
			provider.setNode(startNode);
523
			viewer.refresh();
524
			initCheckedItems();
525
			associatedResources = getSelectedAssociatedFiles();
526
		}
527
451
		/**
528
		/**
452
		 * Allow the user to finish if a valid file has been entered.
529
		 * Allow the user to finish if a valid file has been entered.
453
		 */
530
		 */
Lines 464-469 Link Here
464
				break;
541
				break;
465
			}
542
			}
466
543
544
            if ((associatedResources = getSelectedAssociatedFiles()).size() == 0) {
545
            	pageValid = false;
546
            	setErrorMessage(CompareMessages.GenerateDiffFileWizard_noChangesSelected);
547
            }
548
			
467
			/**
549
			/**
468
			 * Avoid draw flicker by clearing error message
550
			 * Avoid draw flicker by clearing error message
469
			 * if all is valid.
551
			 * if all is valid.
Lines 623-629 Link Here
623
		public void createControl(Composite parent) {
705
		public void createControl(Composite parent) {
624
706
625
			final Composite composite= new Composite(parent, SWT.NULL);
707
			final Composite composite= new Composite(parent, SWT.NULL);
626
			composite.setLayout(new GridLayout());
708
			GridLayout lay = new GridLayout();
709
			lay.verticalSpacing = 15;
710
			composite.setLayout(lay);
627
			setControl(composite);
711
			setControl(composite);
628
			initializeDialogUnits(composite);
712
			initializeDialogUnits(composite);
629
713
Lines 632-640 Link Here
632
716
633
			//Create a location group
717
			//Create a location group
634
			setupLocationControls(composite);
718
			setupLocationControls(composite);
635
719
			
636
			initializeDefaultValues();
720
			initializeDefaultValues();
637
721
			
722
			Label changesLabel = new Label(composite, SWT.HORIZONTAL);
723
			changesLabel.setText("Changes");
724
			
725
			viewer = new ContainerCheckedTreeViewer(composite, SWT.CHECK | SWT.H_SCROLL
726
					  | SWT.V_SCROLL | SWT.BORDER | SWT.HORIZONTAL);
727
			
728
			viewer.getControl().setLayoutData(new GridData(GridData.FILL_BOTH));
729
			provider = new LocalChangesContentProvider(startNode);
730
			viewer.setContentProvider(provider);
731
			viewer.setLabelProvider(new LocalChangesLabelProvider());
732
			viewer.setInput(leftResource.getWorkspace().getRoot());
733
			initCheckedItems();
734
			
735
			createSelectionButtons(composite);
736
			
638
			Dialog.applyDialogFont(parent);
737
			Dialog.applyDialogFont(parent);
639
738
640
			validatePage();
739
			validatePage();
Lines 642-648 Link Here
642
			updateEnablements();
741
			updateEnablements();
643
			setupListeners();
742
			setupListeners();
644
		}
743
		}
645
744
		
745
        private void createSelectionButtons(Composite composite) {
746
        	final Composite buttonGroup = new Composite(composite,SWT.NONE);
747
            GridLayout layout = new GridLayout();
748
            layout.numColumns = 2;
749
            layout.marginWidth = 0;
750
            layout.marginHeight = 0;
751
            layout.horizontalSpacing = 0;
752
            layout.verticalSpacing = 0;
753
            buttonGroup.setLayout(layout);
754
            GridData data = new GridData(GridData.HORIZONTAL_ALIGN_END
755
                    | GridData.VERTICAL_ALIGN_CENTER);
756
            buttonGroup.setLayoutData(data);
757
            
758
            chgSelectAll = createSelectionButton(CompareMessages.GenerateDiffFileWizard_SelectAll, buttonGroup);
759
            chgDeselectAll = createSelectionButton(CompareMessages.GenerateDiffFileWizard_DeselectAll, buttonGroup);
760
		}
761
762
		private Button createSelectionButton(String buttonName, Composite buttonGroup) {
763
			Button button = new Button(buttonGroup,SWT.PUSH);
764
			button.setText(buttonName);
765
			GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
766
	        int widthHint = convertHorizontalDLUsToPixels(IDialogConstants.BUTTON_WIDTH);
767
	        Point minSize = button.computeSize(SWT.DEFAULT, SWT.DEFAULT, true);
768
	        data.widthHint = Math.max(widthHint, minSize.x);
769
	        button.setLayoutData(data);
770
	        return button;
771
		}
772
		
646
		/**
773
		/**
647
		 * Setup the controls for the location.
774
		 * Setup the controls for the location.
648
		 */
775
		 */
Lines 813-821 Link Here
813
					validatePage();
940
					validatePage();
814
				}
941
				}
815
			});
942
			});
943
			
944
            chgSelectAll.addSelectionListener(new SelectionAdapter() {
945
    			public void widgetSelected(SelectionEvent e) {
946
    				initCheckedItems();
947
    				//Only bother changing isPageComplete state if the current state
948
    				//is not enabled
949
    				if (!isPageComplete())
950
    					setPageComplete(validatePage());
951
    			}
952
    		});
953
            
954
            chgDeselectAll.addSelectionListener(new SelectionAdapter() {
955
    			public void widgetSelected(SelectionEvent e) {
956
    				viewer.setCheckedElements(new Object[0]);
957
    				//Only bother changing isPageComplete state if the current state
958
    				//is enabled
959
    				if (isPageComplete())
960
    					setPageComplete(validatePage());
961
    			}
962
    		});
963
            
964
            viewer.addCheckStateListener(new ICheckStateListener() {
965
				public void checkStateChanged(CheckStateChangedEvent event) {
966
					setPageComplete(validatePage());
967
				}
968
			});
816
969
817
		}
970
		}
971
		
972
        protected void initCheckedItems() {
973
					TreeItem[] items=((CheckboxTreeViewer)viewer).getTree().getItems();
974
					for (int i = 0; i < items.length; i++) {
975
						((CheckboxTreeViewer)viewer).setChecked(items[i].getData(), true);
976
					}
977
		}
818
978
979
		protected Map getSelectedAssociatedFiles() {
980
			Object[] elements = viewer.getCheckedElements();
981
			Map result = new HashMap();
982
			for (int i = 0; i < elements.length; i++) {
983
				if(!(elements[i] instanceof IProject)) {
984
					DiffNode node = (DiffNode) elements[i];
985
					IResource lr = Utilities.getResource(node.getLeft());
986
					IResource rr = Utilities.getResource(node.getRight());
987
					if(lr instanceof IFile || rr instanceof IFile)
988
						result.put(lr, rr);
989
				}
990
			}
991
			return result;
992
    	}
993
        
819
		/**
994
		/**
820
		 * Enable and disable controls based on the selected radio button.
995
		 * Enable and disable controls based on the selected radio button.
821
		 */
996
		 */
Lines 1197-1203 Link Here
1197
1372
1198
	private final DefaultValuesStore defaultValuesStore;
1373
	private final DefaultValuesStore defaultValuesStore;
1199
1374
1200
	private DocumentMerger merger;
1201
	private IDocument leftDoc;
1375
	private IDocument leftDoc;
1202
	private IDocument rightDoc;
1376
	private IDocument rightDoc;
1203
	private String leftPath;
1377
	private String leftPath;
Lines 1209-1229 Link Here
1209
	private Text unified_customRelativeText;
1383
	private Text unified_customRelativeText;
1210
	private Button unified_customRelativeOption;
1384
	private Button unified_customRelativeOption;
1211
1385
1386
	private IResource rightResource;
1387
	private IResource leftResource;
1388
	
1389
	private Map associatedResources;
1390
	
1212
	public GenerateDiffFileWizard() {
1391
	public GenerateDiffFileWizard() {
1213
		super();
1392
		super();
1214
		setWindowTitle(CompareMessages.GenerateLocalDiff_title);
1393
		setWindowTitle(CompareMessages.GenerateLocalDiff_title);
1215
		initializeDefaultPageImageDescriptor();
1394
		initializeDefaultPageImageDescriptor();
1216
		defaultValuesStore = new DefaultValuesStore();
1395
		defaultValuesStore = new DefaultValuesStore();
1396
		associatedResources = new HashMap();
1217
	}
1397
	}
1218
1398
1399
	private DiffNode startNode;
1400
	
1401
	public GenerateDiffFileWizard(IResource[] input) {
1402
		this();
1403
		
1404
		this.leftDoc = new Document();
1405
		this.rightDoc = new Document(); 
1406
		
1407
		leftResource = input[0];
1408
		rightResource = input[1];
1409
		
1410
		leftPath = leftResource.getFullPath().toString();
1411
		rightPath = rightResource.getFullPath().toString();
1412
1413
		Differencer dif = new Differencer();
1414
		startNode = (DiffNode) dif.findDifferences(false, null, null, null, 
1415
													new FilteredBufferedResourceNode(leftResource), 
1416
													new FilteredBufferedResourceNode(rightResource));
1417
		
1418
	}
1419
	
1219
	public GenerateDiffFileWizard(DocumentMerger merger, boolean rightToLeft) {
1420
	public GenerateDiffFileWizard(DocumentMerger merger, boolean rightToLeft) {
1220
		this();
1421
		this();
1221
		this.merger = merger;
1222
		this.leftDoc = merger.getDocument(MergeViewerContentProvider.LEFT_CONTRIBUTOR);
1422
		this.leftDoc = merger.getDocument(MergeViewerContentProvider.LEFT_CONTRIBUTOR);
1223
		this.rightDoc = merger.getDocument(MergeViewerContentProvider.RIGHT_CONTRIBUTOR);
1423
		this.rightDoc = merger.getDocument(MergeViewerContentProvider.RIGHT_CONTRIBUTOR);
1224
		this.leftPath = merger.getCompareConfiguration().getLeftLabel(leftDoc);
1424
		this.leftPath = merger.getCompareConfiguration().getLeftLabel(leftDoc);
1225
		this.rightPath = merger.getCompareConfiguration().getRightLabel(rightDoc);
1425
		this.rightPath = merger.getCompareConfiguration().getRightLabel(rightDoc);
1226
		this.rightToLeft = rightToLeft;
1426
		this.rightToLeft = rightToLeft;
1427
		IWorkbenchPart workbenchPart = merger.getCompareConfiguration().getContainer().getWorkbenchPart();
1428
		
1429
		if(workbenchPart instanceof CompareEditor) {
1430
						
1431
			CompareEditor editor = (CompareEditor)workbenchPart;
1432
			
1433
			CompareEditorInput input = (CompareEditorInput)editor.getEditorInput();
1434
			input.getCompareResult();
1435
			if(input.getCompareResult() instanceof ICompareInput) {
1436
				ICompareInput node = (ICompareInput)input.getCompareResult();	
1437
				
1438
				leftResource = ((ResourceNode)node.getLeft()).getResource();
1439
				rightResource = ((ResourceNode)node.getRight()).getResource();
1440
				Differencer dif = new Differencer();
1441
				startNode = (DiffNode) dif.findDifferences(false, null, null, null, 
1442
															new FilteredBufferedResourceNode(leftResource), 
1443
															new FilteredBufferedResourceNode(rightResource));
1444
			}
1445
			
1446
		}
1227
	}
1447
	}
1228
1448
1229
	public void addPages() {
1449
	public void addPages() {
Lines 1325-1331 Link Here
1325
	public boolean needsProgressMonitor() {
1545
	public boolean needsProgressMonitor() {
1326
		return true;
1546
		return true;
1327
	}
1547
	}
1328
1548
	
1329
	/**
1549
	/**
1330
	 * Completes processing of the wizard. If this method returns <code>
1550
	 * Completes processing of the wizard. If this method returns <code>
1331
	 * true</code>, the wizard will close; otherwise, it will stay active.
1551
	 * true</code>, the wizard will close; otherwise, it will stay active.
Lines 1340-1363 Link Here
1340
		}
1560
		}
1341
1561
1342
		//Validation of patch root
1562
		//Validation of patch root
1343
		if(optionsPage.getRootSelection() == OptionsPage.ROOT_CUSTOM) {
1563
//		if(optionsPage.getRootSelection() == OptionsPage.ROOT_CUSTOM) {
1344
			String path = optionsPage.getPath();
1564
//			String path = optionsPage.getPath();
1345
			IFile file2;
1565
//			IFile file2;
1346
			try {
1566
//			try {
1347
				file2 = ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(path));
1567
//				file2 = ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(path));
1348
			} catch(IllegalArgumentException e) {
1568
//			} catch(IllegalArgumentException e) {
1349
				final String title = CompareMessages.GenerateLocalDiff_3;
1569
//				final String title = CompareMessages.GenerateLocalDiff_3;
1350
				final String msg = CompareMessages.GenerateLocalDiff_4;
1570
//				final String msg = CompareMessages.GenerateLocalDiff_4;
1351
				final MessageDialog dialog = new MessageDialog(getShell(), title,
1571
//				final MessageDialog dialog = new MessageDialog(getShell(), title,
1352
						null, msg, MessageDialog.ERROR,
1572
//						null, msg, MessageDialog.ERROR,
1353
						new String[] { IDialogConstants.OK_LABEL }, 0);
1573
//						new String[] { IDialogConstants.OK_LABEL }, 0);
1354
				dialog.open();
1574
//				dialog.open();
1355
				return false;
1575
//				return false;
1356
			}
1576
//			}
1357
			if(!validateFile2(file2)) {
1577
//			if(!validateFile2(file2)) {
1358
				return false;
1578
//				return false;
1359
			}
1579
//			}
1360
		}
1580
//		}
1361
1581
1362
		// Create the patch
1582
		// Create the patch
1363
		generateDiffFile(file);
1583
		generateDiffFile(file);
Lines 1424-1433 Link Here
1424
		} else {
1644
		} else {
1425
			oldPath = this.rightPath;
1645
			oldPath = this.rightPath;
1426
		}
1646
		}
1427
1647
		
1428
		UnifiedDiffFormatter formatter = new UnifiedDiffFormatter(merger,
1648
		//TODO extend formatter to use list of mergers.
1429
				leftDoc, rightDoc, oldPath, toPath, directionSelectionPage
1649
		UnifiedDiffFormatter formatter = new UnifiedDiffFormatter(createFormatterInput());
1430
				.isRightToLeft());
1431
		try {
1650
		try {
1432
			if (file == null) {
1651
			if (file == null) {
1433
				formatter.generateDiff();
1652
				formatter.generateDiff();
Lines 1438-1443 Link Here
1438
			throw new RuntimeException(e);
1657
			throw new RuntimeException(e);
1439
		}
1658
		}
1440
	}
1659
	}
1660
	
1661
	private Object[][] createFormatterInput() {
1662
		Set keys = associatedResources.keySet();
1663
		Object[][] result = new Object[keys.size()][3];
1664
		DocumentMerger merg = null;
1665
		int i = 0;
1666
		for (Iterator iterator = keys.iterator(); iterator.hasNext();) {
1667
			IFile sourceRes = (IFile) iterator.next();
1668
			IFile targetRes = (IFile) associatedResources.get(sourceRes);
1669
			merg = createDocumentMerger(sourceRes, targetRes);
1670
			result[i][0] = merg;
1671
			result[i][1] = sourceRes;
1672
			result[i][2] = targetRes;
1673
			i++;
1674
		}
1675
		return result;
1676
	}
1677
	
1678
	private DocumentMerger createDocumentMerger(IFile leftFile, IFile rightFile){
1679
			IDocument leftDoc = convertFileToDocument(leftFile);
1680
			IDocument rightDoc = convertFileToDocument(rightFile);
1681
            if (leftDoc == null) {
1682
                leftDoc = new Document();
1683
            }
1684
            if (rightDoc == null) {
1685
                rightDoc = new Document();
1686
            }
1687
			DocumentMerger docMerger;
1688
			docMerger = new DocumentMerger(new LocalDiffMergerInput(leftDoc, rightDoc));
1689
			try {
1690
				docMerger.doDiff();
1691
			} catch (Exception e) {
1692
				throw new RuntimeException(e);
1693
			}
1694
			return docMerger;
1695
	}
1696
	
1697
	private IDocument convertFileToDocument(IFile file) {
1698
		if(file == null)
1699
			return null;
1700
		StringBuffer str = new StringBuffer();
1701
		int c;
1702
		try{
1703
			InputStream inputStream = file.getContents();
1704
			while ((c = inputStream.read())!=-1){
1705
				str.append((char)c);
1706
			}
1707
			inputStream.close();
1708
			return new Document(str.toString());
1709
		}catch(Exception ex){
1710
			CompareUIPlugin.log(ex);
1711
		}
1712
	
1713
		return null;
1714
	}
1715
	
1716
	class LocalDiffMergerInput implements IDocumentMergerInput {
1717
		        
1718
			private IDocument leftDoc;
1719
			private IDocument rightDoc;
1720
			
1721
			public LocalDiffMergerInput(IDocument leftDoc, IDocument rightDoc) {
1722
				this.leftDoc = leftDoc ;
1723
				this.rightDoc= rightDoc;
1724
			}
1725
				
1726
			public ITokenComparator createTokenComparator(String line) {
1727
		            return new TokenComparator(line);
1728
		    }
1729
		    public CompareConfiguration getCompareConfiguration() {
1730
		            return new CompareConfiguration();
1731
		    }
1732
		    public IDocument getDocument(char contributor) {
1733
		            switch (contributor) {
1734
		            case MergeViewerContentProvider.LEFT_CONTRIBUTOR:
1735
		                    return leftDoc;
1736
		            case MergeViewerContentProvider.RIGHT_CONTRIBUTOR:
1737
		                    return rightDoc;
1738
		            case MergeViewerContentProvider.ANCESTOR_CONTRIBUTOR: 
1739
		                    return null;
1740
		            }
1741
		            return null;
1742
		    }
1743
		    public int getHunkStart() {
1744
		            return 0;
1745
		    }
1746
		    public Position getRegion(char contributor) {
1747
		            switch (contributor) {
1748
		            case MergeViewerContentProvider.LEFT_CONTRIBUTOR:
1749
		                    return new Position(0, leftDoc.getLength());
1750
		            case MergeViewerContentProvider.RIGHT_CONTRIBUTOR:
1751
		                    return new Position(0, rightDoc.getLength());
1752
		            case MergeViewerContentProvider.ANCESTOR_CONTRIBUTOR:
1753
		                    return new Position(0, 0);
1754
		            }
1755
		            return null;
1756
		    }
1757
		    public boolean isHunkOnLeft() {
1758
		    	return false;
1759
		    }
1760
		    public boolean isIgnoreAncestor() {
1761
		    	return true;
1762
		    }
1763
		    public boolean isPatchHunk() {
1764
		    	return false;
1765
		    }
1766
		
1767
		    public boolean isShowPseudoConflicts() {
1768
		    	return false;
1769
		    }
1770
		    public boolean isThreeWay() {
1771
		    	return false;
1772
		    }
1773
		    public boolean isPatchHunkOk() {
1774
		    	return false;
1775
		    }
1776
		    
1777
		}
1441
1778
1442
	public boolean validateFile(File file) {
1779
	public boolean validateFile(File file) {
1443
1780
Lines 1644-1647 Link Here
1644
		}
1981
		}
1645
	}
1982
	}
1646
1983
1984
1985
1647
}
1986
}
(-)compare/org/eclipse/compare/internal/CompareMessages.java (+3 lines)
Lines 177-182 Link Here
177
	public static String GenerateLocalDiff_4;
177
	public static String GenerateLocalDiff_4;
178
	public static String GenerateLocalDiff_5;
178
	public static String GenerateLocalDiff_5;
179
	public static String GenerateLocalDiff_6;
179
	public static String GenerateLocalDiff_6;
180
	public static String GenerateDiffFileWizard_SelectAll;
181
	public static String GenerateDiffFileWizard_DeselectAll;
182
	public static String GenerateDiffFileWizard_noChangesSelected;
180
183
181
	static {
184
	static {
182
		NLS.initializeMessages(BUNDLE_NAME, CompareMessages.class);
185
		NLS.initializeMessages(BUNDLE_NAME, CompareMessages.class);
(-)compare/org/eclipse/compare/internal/Utilities.java (+9 lines)
Lines 781-784 Link Here
781
			});
781
			});
782
		}
782
		}
783
	}
783
	}
784
785
	public static IResource getResource(ITypedElement element) {
786
		if (element instanceof BufferedResourceNode) {
787
		    BufferedResourceNode bn= (BufferedResourceNode) element;
788
		    return bn.getResource();
789
		}
790
		return null;
791
		
792
	}
784
}
793
}
(-)compare/org/eclipse/compare/internal/CompareMessages.properties (-2 / +6 lines)
Lines 145-150 Link Here
145
CompareWithOtherResourceDialog_workspaceMainButton=Browse...
145
CompareWithOtherResourceDialog_workspaceMainButton=Browse...
146
CompareWithOtherResourceDialog_workspaceRadioButton=Workspace
146
CompareWithOtherResourceDialog_workspaceRadioButton=Workspace
147
147
148
CreatePatchActionTitle=Create Patch...
148
WorkspacePatchDialogTitle=Set a Patch Location
149
WorkspacePatchDialogTitle=Set a Patch Location
149
WorkspacePatchDialogDescription=Select a folder in the workspace and enter a name for the patch.
150
WorkspacePatchDialogDescription=Select a folder in the workspace and enter a name for the patch.
150
Save_To_Clipboard_2=&Clipboard
151
Save_To_Clipboard_2=&Clipboard
Lines 188-194 Link Here
188
GenerateDiffFileWizard_13=Use only &file path:
189
GenerateDiffFileWizard_13=Use only &file path:
189
GenerateDiffFileWizard_Left=&Left:
190
GenerateDiffFileWizard_Left=&Left:
190
GenerateDiffFileWizard_Right=&Right:
191
GenerateDiffFileWizard_Right=&Right:
191
CreatePatchActionTitle=Create Patch...
192
CreatePatchActionTitle=Create Patch
192
WorkspacePatchDialogTitle=Set a Patch Location
193
WorkspacePatchDialogTitle=Set a Patch Location
193
WorkspacePatchDialogDescription=Select a folder in the workspace and enter a name for the patch.
194
WorkspacePatchDialogDescription=Select a folder in the workspace and enter a name for the patch.
194
Save_To_Clipboard_2=&Clipboard
195
Save_To_Clipboard_2=&Clipboard
Lines 231-234 Link Here
231
GenerateDiffFileWizard_ProjectClosed=The specified path points to a closed project.
232
GenerateDiffFileWizard_ProjectClosed=The specified path points to a closed project.
232
GenerateDiffFileWizard_13=Use only &file path:
233
GenerateDiffFileWizard_13=Use only &file path:
233
GenerateDiffFileWizard_Left=&Left:
234
GenerateDiffFileWizard_Left=&Left:
234
GenerateDiffFileWizard_Right=&Right:
235
GenerateDiffFileWizard_Right=&Right:
236
GenerateDiffFileWizard_SelectAll=Select &All
237
GenerateDiffFileWizard_DeselectAll=&Deselect All
238
GenerateDiffFileWizard_noChangesSelected=No changes selected.
(-)compare/org/eclipse/compare/internal/ICompareUIConstants.java (-1 / +1 lines)
Lines 40-46 Link Here
40
	public static final String PROP_TITLE_IMAGE = PREFIX + "TitleImage"; //$NON-NLS-1$
40
	public static final String PROP_TITLE_IMAGE = PREFIX + "TitleImage"; //$NON-NLS-1$
41
	public static final String PROP_SELECTED_EDITION = PREFIX + "SelectedEdition"; //$NON-NLS-1$
41
	public static final String PROP_SELECTED_EDITION = PREFIX + "SelectedEdition"; //$NON-NLS-1$
42
	
42
	
43
	public static final int COMPARE_IMAGE_WIDTH= 22;
43
	public static final int COMPARE_IMAGE_WIDTH= 17;
44
	
44
	
45
	public static final String PREF_NAVIGATION_END_ACTION= PREFIX + "NavigationEndAction"; //$NON-NLS-1$
45
	public static final String PREF_NAVIGATION_END_ACTION= PREFIX + "NavigationEndAction"; //$NON-NLS-1$
46
	public static final String PREF_NAVIGATION_END_ACTION_LOCAL= PREFIX + "NavigationEndActionLocal"; //$NON-NLS-1$
46
	public static final String PREF_NAVIGATION_END_ACTION_LOCAL= PREFIX + "NavigationEndActionLocal"; //$NON-NLS-1$
(-)compare/org/eclipse/compare/internal/UnifiedDiffFormatter.java (-96 / +126 lines)
Lines 17-22 Link Here
17
import java.io.File;
17
import java.io.File;
18
import java.io.FileOutputStream;
18
import java.io.FileOutputStream;
19
import java.io.IOException;
19
import java.io.IOException;
20
import java.io.InputStream;
20
import java.io.PrintStream;
21
import java.io.PrintStream;
21
import java.util.ArrayList;
22
import java.util.ArrayList;
22
import java.util.Arrays;
23
import java.util.Arrays;
Lines 26-32 Link Here
26
import org.eclipse.compare.internal.merge.DocumentMerger;
27
import org.eclipse.compare.internal.merge.DocumentMerger;
27
import org.eclipse.compare.internal.merge.DocumentMerger.Diff;
28
import org.eclipse.compare.internal.merge.DocumentMerger.Diff;
28
import org.eclipse.compare.rangedifferencer.RangeDifference;
29
import org.eclipse.compare.rangedifferencer.RangeDifference;
30
import org.eclipse.core.resources.IFile;
29
import org.eclipse.jface.text.BadLocationException;
31
import org.eclipse.jface.text.BadLocationException;
32
import org.eclipse.jface.text.Document;
30
import org.eclipse.jface.text.IDocument;
33
import org.eclipse.jface.text.IDocument;
31
import org.eclipse.swt.dnd.Clipboard;
34
import org.eclipse.swt.dnd.Clipboard;
32
import org.eclipse.swt.dnd.TextTransfer;
35
import org.eclipse.swt.dnd.TextTransfer;
Lines 52-73 Link Here
52
	public static final String RANGE_INFORMATION_PREFIX = "@@ -"; //$NON-NLS-1$
55
	public static final String RANGE_INFORMATION_PREFIX = "@@ -"; //$NON-NLS-1$
53
	public static final String RANGE_INFORMATION_AFFIX = " @@"; //$NON-NLS-1$
56
	public static final String RANGE_INFORMATION_AFFIX = " @@"; //$NON-NLS-1$
54
57
55
	private DocumentMerger merger;
58
	private Object[][] input;
56
	private IDocument leftDoc;
59
57
	private IDocument rightDoc;
60
	public UnifiedDiffFormatter(Object[][] input) {
58
	private String oldPath;
61
		this.input = input;
59
	private String newPath;
60
	private boolean rightToLeft;
61
62
	public UnifiedDiffFormatter(DocumentMerger merger, IDocument leftDoc,
63
			IDocument rightDoc, String oldPath, String newPath,
64
			boolean rightToLeft) {
65
		this.merger = merger;
66
		this.leftDoc = leftDoc;
67
		this.rightDoc = rightDoc;
68
		this.oldPath = oldPath;
69
		this.newPath = newPath;
70
		this.rightToLeft = rightToLeft;
71
	}
62
	}
72
63
73
	/**
64
	/**
Lines 148-242 Link Here
148
	 * @param strictUnixFormat determinates if the format should be fully compatible with the Unix one.
139
	 * @param strictUnixFormat determinates if the format should be fully compatible with the Unix one.
149
	 */
140
	 */
150
	private void generateDiff(PrintStream output, boolean strictUnixFormat) {
141
	private void generateDiff(PrintStream output, boolean strictUnixFormat) {
151
		ArrayList allDiffs = merger.getAllDiffs();
142
		for (int fileNr = 0; fileNr < input.length; fileNr++) {
152
		// If the first block isn't the only one, or first block is different
143
			DocumentMerger merger = (DocumentMerger) input[fileNr][0];
153
		if (allDiffs.size() > 1 || isPartDifferent(allDiffs, 0)) {
144
			IDocument leftDoc = convertFileToDocument((IFile)input[fileNr][1]);
154
			output.println(INDEX_MARKER + oldPath);
145
			IDocument rightDoc = convertFileToDocument((IFile)input[fileNr][2]);
155
			output.println(DELIMITER);
146
			String oldPath;
156
			Date oldDate = Calendar.getInstance().getTime();
147
			String newPath;
157
			Date newDate = Calendar.getInstance().getTime();
148
			if (leftDoc == null) {
158
			String oldDateFormat = DateFormat.getDateTimeInstance().format(oldDate);
149
				leftDoc = new Document();
159
			String newDateFormat = DateFormat.getDateTimeInstance().format(newDate);
150
				newPath = ((IFile)input[fileNr][2]).getFullPath().toString();
160
			output.println(OLD_FILE_PREFIX + oldPath + '\t'
151
				oldPath = ((IFile)input[fileNr][2]).getFullPath().toString();
161
					+ oldDateFormat + " -0000"); //$NON-NLS-1$
152
			}
162
			output.println(NEW_FILE_PREFIX + newPath + '\t'
153
			else if (rightDoc == null) {
163
					+ newDateFormat + " -0000"); //$NON-NLS-1$
154
				rightDoc = new Document();
164
155
				newPath = ((IFile)input[fileNr][1]).getFullPath().toString();
165
			boolean firstHunk = true;
156
				oldPath = ((IFile)input[fileNr][1]).getFullPath().toString();
166
			Hunk currentHunk = null;
157
			}
167
158
			else {
168
			int currentLineNumberOld = 0;
159
				oldPath = ((IFile)input[fileNr][1]).getFullPath().toString();
169
			int currentLineNumberNew = 0;
160
				newPath = ((IFile)input[fileNr][2]).getFullPath().toString();
170
161
			}
171
			for (int partNumber = 0; partNumber < allDiffs.size(); partNumber++) {
162
			
172
163
			ArrayList allDiffs = merger.getAllDiffs();
173
				ArrayList oldPart = getPart(partNumber, LEFT_CONTRIBUTOR);
164
			// If the first block isn't the only one, or first block is different
174
				ArrayList newPart = getPart(partNumber, RIGHT_CONTRIBUTOR);
165
			if (allDiffs.size() > 1 || isPartDifferent(allDiffs, 0)) {
175
166
				output.println(INDEX_MARKER + oldPath);
176
				if (isPartDifferent(allDiffs, partNumber)) {
167
				output.println(DELIMITER);
177
					// This part has some changes
168
				Date oldDate = Calendar.getInstance().getTime();
178
					if (firstHunk) {
169
				Date newDate = Calendar.getInstance().getTime();
179
						// Hunk will start with changed block
170
				String oldDateFormat = DateFormat.getDateTimeInstance().format(oldDate);
180
						currentHunk = new Hunk(0, 0, strictUnixFormat);
171
				String newDateFormat = DateFormat.getDateTimeInstance().format(newDate);
181
						firstHunk = false;
172
				output.println(OLD_FILE_PREFIX + oldPath + '\t'
182
					}
173
						+ oldDateFormat + " -0000"); //$NON-NLS-1$
183
					if (partNumber == (allDiffs.size() - 1)) {
174
				output.println(NEW_FILE_PREFIX + newPath + '\t'
184
						// If it is the last part
175
						+ newDateFormat + " -0000"); //$NON-NLS-1$
185
						currentHunk.addPartRangeToOld(oldPart, 0, oldPart
176
186
								.size(), true);
177
				boolean firstHunk = true;
187
						currentHunk.addPartRangeToNew(newPart, 0, newPart
178
				Hunk currentHunk = null;
188
								.size(), true);
179
189
					} else {
180
				int currentLineNumberOld = 0;
190
						currentHunk.addPartRangeToOld(oldPart, 0, oldPart
181
				int currentLineNumberNew = 0;
191
								.size(), false);
182
192
						currentHunk.addPartRangeToNew(newPart, 0, newPart
183
				for (int partNumber = 0; partNumber < allDiffs.size(); partNumber++) {
193
								.size(), false);
184
194
					}
185
					ArrayList oldPart = getPart(partNumber, LEFT_CONTRIBUTOR, merger, leftDoc, rightDoc);
195
				} else {
186
					ArrayList newPart = getPart(partNumber, RIGHT_CONTRIBUTOR, merger, leftDoc, rightDoc);
196
					if (firstHunk) {
187
197
						// Hunk will start with context
188
					if (isPartDifferent(allDiffs, partNumber)) {
198
						currentHunk = new Hunk((oldPart.size() - 1) - NUMBER_OF_CONTEXT_LINES,
189
						// This part has some changes
199
								(oldPart.size() - 1) - NUMBER_OF_CONTEXT_LINES, strictUnixFormat);
190
						if (firstHunk) {
200
						firstHunk = false;
191
							// Hunk will start with changed block
201
						currentHunk.addPartRangeToBoth(oldPart,
192
							currentHunk = new Hunk(0, 0, strictUnixFormat);
202
								(oldPart.size() - 1) - NUMBER_OF_CONTEXT_LINES, oldPart.size(), false);
193
							firstHunk = false;
203
					} else {
194
						}
204
						if (partNumber == (allDiffs.size() - 1)) {
195
						if (partNumber == (allDiffs.size() - 1)) {
205
							// If it is the last part
196
							// If it is the last part
206
							currentHunk.addPartRangeToBoth(oldPart, 0, NUMBER_OF_CONTEXT_LINES, true);
197
							currentHunk.addPartRangeToOld(oldPart, 0, oldPart
198
									.size(), true);
199
							currentHunk.addPartRangeToNew(newPart, 0, newPart
200
									.size(), true);
201
						} else {
202
							currentHunk.addPartRangeToOld(oldPart, 0, oldPart
203
									.size(), false);
204
							currentHunk.addPartRangeToNew(newPart, 0, newPart
205
									.size(), false);
206
						}
207
					} else {
208
						if (firstHunk) {
209
							// Hunk will start with context
210
							currentHunk = new Hunk((oldPart.size() - 1) - NUMBER_OF_CONTEXT_LINES,
211
									(oldPart.size() - 1) - NUMBER_OF_CONTEXT_LINES, strictUnixFormat);
212
							firstHunk = false;
213
							currentHunk.addPartRangeToBoth(oldPart,
214
									(oldPart.size() - 1) - NUMBER_OF_CONTEXT_LINES, oldPart.size(), false);
207
						} else {
215
						} else {
208
							if (oldPart.size() - 1 < 2*NUMBER_OF_CONTEXT_LINES) {
216
							if (partNumber == (allDiffs.size() - 1)) {
209
								// Context too short to start new hunk
217
								// If it is the last part
210
								currentHunk.addPartRangeToBoth(oldPart, 0,
218
								currentHunk.addPartRangeToBoth(oldPart, 0, NUMBER_OF_CONTEXT_LINES, true);
211
										oldPart.size(), false);
212
							} else {
219
							} else {
213
								// Context long enough to start new hunk
220
								if (oldPart.size() - 1 < 2*NUMBER_OF_CONTEXT_LINES) {
214
								currentHunk.addPartRangeToBoth(oldPart, 0, NUMBER_OF_CONTEXT_LINES,
221
									// Context too short to start new hunk
215
										false);
222
									currentHunk.addPartRangeToBoth(oldPart, 0,
216
								currentHunk.printTo(output);
223
											oldPart.size(), false);
217
								currentHunk = new Hunk(currentLineNumberOld
224
								} else {
218
										+ (oldPart.size() - 1) - NUMBER_OF_CONTEXT_LINES,
225
									// Context long enough to start new hunk
219
										currentLineNumberNew + (oldPart.size() - 1)
226
									currentHunk.addPartRangeToBoth(oldPart, 0, NUMBER_OF_CONTEXT_LINES,
220
										- NUMBER_OF_CONTEXT_LINES, strictUnixFormat);
227
											false);
221
								currentHunk.addPartRangeToBoth(oldPart,
228
									currentHunk.printTo(output);
222
										(oldPart.size() - 1) - NUMBER_OF_CONTEXT_LINES,
229
									currentHunk = new Hunk(currentLineNumberOld
223
										oldPart.size(), false);
230
											+ (oldPart.size() - 1) - NUMBER_OF_CONTEXT_LINES,
231
											currentLineNumberNew + (oldPart.size() - 1)
232
											- NUMBER_OF_CONTEXT_LINES, strictUnixFormat);
233
									currentHunk.addPartRangeToBoth(oldPart,
234
											(oldPart.size() - 1) - NUMBER_OF_CONTEXT_LINES,
235
											oldPart.size(), false);
236
								}
224
							}
237
							}
225
						}
238
						}
226
					}
239
					}
240
					currentLineNumberOld += oldPart.size();
241
					currentLineNumberNew += newPart.size();
227
				}
242
				}
228
				currentLineNumberOld += oldPart.size();
243
				// Print the last hunk
229
				currentLineNumberNew += newPart.size();
244
				currentHunk.printTo(output);
230
			}
245
			}			
231
			// Print the last hunk
232
			currentHunk.printTo(output);
233
		}
246
		}
234
	}
247
	}
235
248
236
	private ArrayList getPart(int nr, char side) {
249
	private IDocument convertFileToDocument(IFile file) {
250
		if(file == null)
251
			return null;
252
		StringBuffer str = new StringBuffer();
253
		int c;
254
		try{
255
			InputStream inputStream = file.getContents();
256
			while ((c = inputStream.read())!=-1){
257
				str.append((char)c);
258
			}
259
			inputStream.close();
260
			return new Document(str.toString());
261
		}catch(Exception ex){
262
			CompareUIPlugin.log(ex);
263
		}
264
	
265
		return null;
266
	}
267
	
268
	private ArrayList getPart(int nr, char side, DocumentMerger merger, IDocument leftDoc, IDocument rightDoc) {
237
		try {
269
		try {
238
			String s = extract(nr, side).replaceAll("\r\n", "\n"); //$NON-NLS-1$ //$NON-NLS-2$
270
			String s = extract(nr, side, merger, leftDoc, rightDoc).replaceAll("\r", ""); //$NON-NLS-1$ //$NON-NLS-2$
239
			s.replaceAll("\r", "\n"); //$NON-NLS-1$ //$NON-NLS-2$
240
			ArrayList diffLines = new ArrayList(Arrays
271
			ArrayList diffLines = new ArrayList(Arrays
241
					.asList(s.split("\n", -1))); //$NON-NLS-1$
272
					.asList(s.split("\n", -1))); //$NON-NLS-1$
242
			return diffLines;
273
			return diffLines;
Lines 246-255 Link Here
246
		return null;
277
		return null;
247
	}
278
	}
248
279
249
	private String extract(int nr, char side) throws BadLocationException {
280
	private String extract(int nr, char side, DocumentMerger merger, IDocument leftDoc, IDocument rightDoc) throws BadLocationException {
250
		Diff diff = ((Diff) merger.getAllDiffs().get(nr));
281
		Diff diff = ((Diff) merger.getAllDiffs().get(nr));
251
		if (side == LEFT_CONTRIBUTOR && !rightToLeft
282
		if (side == LEFT_CONTRIBUTOR) {
252
				|| side == RIGHT_CONTRIBUTOR && rightToLeft) {
253
			return leftDoc.get(diff.getPosition(LEFT_CONTRIBUTOR).offset, diff
283
			return leftDoc.get(diff.getPosition(LEFT_CONTRIBUTOR).offset, diff
254
					.getPosition(LEFT_CONTRIBUTOR).length);
284
					.getPosition(LEFT_CONTRIBUTOR).length);
255
		}
285
		}
(-)compare/org/eclipse/compare/internal/ResourceCompareInput.java (+5 lines)
Lines 129-134 Link Here
129
	ResourceCompareInput(CompareConfiguration config) {
129
	ResourceCompareInput(CompareConfiguration config) {
130
		super(config);
130
		super(config);
131
	}
131
	}
132
	
133
	public Object createDiff(){
134
		Differencer dif = new Differencer();
135
		return dif.findDifferences(false, null, null, null, fLeft, fRight);
136
	}
132
			
137
			
133
	public Viewer createDiffViewer(Composite parent) {
138
	public Viewer createDiffViewer(Composite parent) {
134
		fDiffViewer= new DiffTreeViewer(parent, getCompareConfiguration()) {
139
		fDiffViewer= new DiffTreeViewer(parent, getCompareConfiguration()) {
(-)plugin.xml (+23 lines)
Lines 192-197 Link Here
192
               id="compareWithMenu">
192
               id="compareWithMenu">
193
            <separator
193
            <separator
194
                  name="compareWithGroup">
194
                  name="compareWithGroup">
195
	    </separator>
196
            <separator
197
                  name="localPatchGroup">
195
            </separator>
198
            </separator>
196
         </menu>
199
         </menu>
197
      </objectContribution>
200
      </objectContribution>
Lines 280-285 Link Here
280
               id="addFromHistoryAction">
283
               id="addFromHistoryAction">
281
         </action>
284
         </action>
282
      </objectContribution>
285
      </objectContribution>
286
      <objectContribution
287
            adaptable="true"
288
            id="org.eclipse.compare.GenerateDiffAction"
289
            objectClass="org.eclipse.core.resources.IResource">
290
         <action
291
         	   label="%CreateLocalPatch.label"	
292
         	   tooltip="%CreateLocalPatch.tooltip"
293
               class="org.eclipse.compare.internal.GenerateDiffAction"
294
               enablesFor="2"
295
               id="generateDiff"
296
               menubarPath="compareWithMenu/localPatchGroup">
297
         </action>
298
         <menu
299
               id="creatLocalPatch"
300
               label="%CreateLocalPatch.label">
301
            <separator
302
                  name="org.eclipse.compare.generateDiffSeparator">
303
            </separator>
304
         </menu>
305
      </objectContribution>
283
   </extension>
306
   </extension>
284
   
307
   
285
   <extension
308
   <extension
(-)plugin.properties (+3 lines)
Lines 91-96 Link Here
91
CompareWithHistoryAction.label= &Local History...
91
CompareWithHistoryAction.label= &Local History...
92
CompareWithHistoryAction.tooltip= Compare the Selected Resource with Local History
92
CompareWithHistoryAction.tooltip= Compare the Selected Resource with Local History
93
93
94
CreateLocalPatch.label= &Create Patch...
95
CreateLocalPatch.tooltip= Create Patch from the Selected Resource
96
94
ReplaceWithMenu.label= Rep&lace With
97
ReplaceWithMenu.label= Rep&lace With
95
98
96
ReplaceFromHistoryAction.label= &Local History...
99
ReplaceFromHistoryAction.label= &Local History...
(-)compare/org/eclipse/compare/internal/LocalChangesLabelProvider.java (+104 lines)
Added Link Here
1
package org.eclipse.compare.internal;
2
3
import org.eclipse.compare.CompareConfiguration;
4
import org.eclipse.compare.ITypedElement;
5
import org.eclipse.compare.structuremergeviewer.DiffNode;
6
import org.eclipse.compare.structuremergeviewer.Differencer;
7
import org.eclipse.core.resources.IFile;
8
import org.eclipse.core.resources.IFolder;
9
import org.eclipse.core.resources.IProject;
10
import org.eclipse.core.resources.IResource;
11
import org.eclipse.jface.viewers.ILabelProvider;
12
import org.eclipse.jface.viewers.ILabelProviderListener;
13
import org.eclipse.swt.graphics.Image;
14
import org.eclipse.ui.ISharedImages;
15
import org.eclipse.ui.PlatformUI;
16
17
public class LocalChangesLabelProvider implements ILabelProvider {
18
19
	public static String LEFT = "LEFT";
20
	public static String RIGHT = "RIGHT";
21
	private CompareConfiguration config;
22
23
	public LocalChangesLabelProvider() {
24
		config = new CompareConfiguration();
25
	}
26
	
27
	public Image getImage(Object element) {
28
		if(element instanceof IProject)
29
			return PlatformUI.getWorkbench().getSharedImages().getImage(
30
					org.eclipse.ui.ide.IDE.SharedImages.IMG_OBJ_PROJECT);
31
		
32
		IResource resource = getResource((DiffNode)element);
33
		
34
		if(resource instanceof IFile) {
35
			int kind = ((DiffNode)element).getKind();
36
			return config.getImage(PlatformUI.getWorkbench().getSharedImages().getImage(
37
					  ISharedImages.IMG_OBJ_FILE), Differencer.RIGHT +kind);			
38
			
39
		} else if(resource instanceof IFolder) {
40
			int kind = ((DiffNode)element).getKind();
41
			return config.getImage(PlatformUI.getWorkbench().getSharedImages().getImage(
42
					  ISharedImages.IMG_OBJ_FOLDER), Differencer.RIGHT +kind);	
43
			
44
		} else if(resource instanceof IProject) {
45
			return PlatformUI.getWorkbench().getSharedImages().getImage(
46
				org.eclipse.ui.ide.IDE.SharedImages.IMG_OBJ_PROJECT);	
47
			
48
		} else {
49
			return PlatformUI.getWorkbench().getSharedImages().getImage(
50
					  ISharedImages.IMG_OBJ_ELEMENT);	
51
		}
52
		
53
	}
54
55
	public String getText(Object element) {
56
		if(element instanceof IProject)
57
			return ((IProject)element).getName();
58
		
59
		IResource resource = getResource((DiffNode)element);
60
61
		if(resource instanceof IFile)
62
			return resource.getName();
63
		else if(resource instanceof IFolder)
64
			return resource.getProjectRelativePath().toString();
65
		else if(resource instanceof IProject)
66
			return resource.getName();
67
		else 
68
			return resource.getName();
69
	}
70
71
	public void addListener(ILabelProviderListener listener) {
72
		// TODO Auto-generated method stub
73
		
74
	}
75
76
	public void dispose() {
77
		// TODO Auto-generated method stub
78
		
79
	}
80
81
	public boolean isLabelProperty(Object element, String property) {
82
		// TODO Auto-generated method stub
83
		return false;
84
	}
85
86
	public void removeListener(ILabelProviderListener listener) {
87
		// TODO Auto-generated method stub
88
		
89
	}
90
	
91
	private IResource getResource(DiffNode node) {
92
		ITypedElement te = node.getLeft();
93
		if(te == null)
94
			te = node.getRight();
95
		
96
		if (te instanceof BufferedResourceNode) {
97
		    BufferedResourceNode bn = (BufferedResourceNode) te;
98
		    IResource resource = bn.getResource();
99
		    return resource;
100
		}
101
		return null;
102
	}
103
104
}
(-)compare/org/eclipse/compare/internal/LocalChangesContentProvider.java (+83 lines)
Added Link Here
1
package org.eclipse.compare.internal;
2
3
import java.util.ArrayList;
4
5
import org.eclipse.compare.ITypedElement;
6
import org.eclipse.compare.structuremergeviewer.DiffNode;
7
import org.eclipse.compare.structuremergeviewer.Differencer;
8
import org.eclipse.compare.structuremergeviewer.IDiffElement;
9
import org.eclipse.core.resources.IProject;
10
import org.eclipse.core.resources.IResource;
11
import org.eclipse.jface.viewers.ITreeContentProvider;
12
import org.eclipse.jface.viewers.Viewer;
13
14
public class LocalChangesContentProvider implements ITreeContentProvider {
15
16
	private DiffNode node;
17
18
	public static String LEFT = "LEFT";
19
	public static String RIGHT = "RIGHT";
20
	
21
	public LocalChangesContentProvider(DiffNode node) {
22
		super();
23
		this.node = node;
24
	}
25
	
26
	public Object[] getElements(Object inputElement) {
27
		IResource r = getResource(node.getLeft());
28
		return new Object[] {r.getProject()};
29
	}
30
31
	public void dispose() {
32
		//
33
	}
34
35
	public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
36
		//
37
	}
38
39
	public Object[] getChildren(Object parentElement) {
40
		if(parentElement instanceof IProject) {
41
			return new Object[] {node};
42
		}
43
			
44
		IDiffElement[] elems = ((DiffNode)parentElement).getChildren();
45
		ArrayList result = new ArrayList();
46
		for (int i = 0; i < elems.length; i++) {
47
			DiffNode diffNode = (DiffNode)elems[i];
48
			if(diffNode.getKind() != Differencer.NO_CHANGE) {
49
				result.add(elems[i]);
50
			}
51
		}
52
		return result.toArray();
53
	}
54
55
	public Object getParent(Object element) {
56
		return ((DiffNode)element).getParent();
57
	}
58
59
	public boolean hasChildren(Object element) {
60
		if(element instanceof IProject) {
61
			return true;
62
		}
63
		return ((DiffNode)element).hasChildren();
64
	}
65
	
66
	
67
	private IResource getResource(ITypedElement element) {
68
		if (element instanceof BufferedResourceNode) {
69
		    BufferedResourceNode bn= (BufferedResourceNode) element;
70
		    return bn.getResource();
71
		}
72
		return null;
73
	}
74
	
75
	public void setNode(DiffNode node) {
76
		this.node = node;
77
	}
78
79
	public DiffNode getNode() {
80
		return node;
81
	}
82
83
}
(-)compare/org/eclipse/compare/internal/GenerateDiffAction.java (+61 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2008 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
 *     Krzysztof Poglodzinski (intuicje@gmail.com) - initial API and implementation
10
 *     Mariusz Tanski (mariusztanski@gmail.com) - initial API and implementation
11
 *     Kacper Zdanowicz (kacper.zdanowicz@gmail.com) - initial API and implementation
12
 *******************************************************************************/
13
package org.eclipse.compare.internal;
14
15
16
import org.eclipse.compare.CompareConfiguration;
17
import org.eclipse.core.resources.IResource;
18
import org.eclipse.jface.action.IAction;
19
import org.eclipse.jface.viewers.ISelection;
20
import org.eclipse.swt.widgets.Shell;
21
import org.eclipse.ui.IObjectActionDelegate;
22
import org.eclipse.ui.IWorkbenchPage;
23
import org.eclipse.ui.IWorkbenchPart;
24
25
26
/*
27
 * The "Generate diff" action
28
 */
29
public class GenerateDiffAction extends BaseCompareAction implements IObjectActionDelegate {
30
31
	protected ResourceCompareInput fInput;
32
	protected IWorkbenchPage fWorkbenchPage;
33
	protected boolean showSelectAncestorDialog = true;
34
	
35
	public void run(ISelection selection) {
36
		
37
		IResource[] ir = Utilities.getResources(selection);
38
		Shell shell = fWorkbenchPage.getWorkbenchWindow().getShell();
39
		boolean ok = fInput.setSelection(selection,shell, showSelectAncestorDialog);
40
		if (!ok) return;
41
42
		GenerateDiffFileWizard.run(ir, shell);
43
		
44
45
	}
46
47
	protected boolean isEnabled(ISelection selection) {
48
		if (fInput == null) {
49
			CompareConfiguration cc= new CompareConfiguration();
50
			cc.setProperty(CompareEditor.CONFIRM_SAVE_PROPERTY, new Boolean(false));											
51
			fInput= new ResourceCompareInput(cc);
52
		}
53
		return fInput.isEnabled(selection);
54
	}
55
56
	public void setActivePart(IAction action, IWorkbenchPart targetPart) {
57
		fWorkbenchPage= targetPart.getSite().getPage();
58
	}
59
	
60
}
61

Return to bug 265030