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

Collapse All | Expand All

(-)compare/org/eclipse/compare/internal/CompareWithOtherResourceAction.java (+16 lines)
Lines 10-15 Link Here
10
 *******************************************************************************/
10
 *******************************************************************************/
11
package org.eclipse.compare.internal;
11
package org.eclipse.compare.internal;
12
12
13
import org.eclipse.core.resources.IProject;
14
import org.eclipse.core.resources.ResourcesPlugin;
15
import org.eclipse.core.runtime.CoreException;
13
import org.eclipse.jface.viewers.ISelection;
16
import org.eclipse.jface.viewers.ISelection;
14
17
15
/**
18
/**
Lines 23-28 Link Here
23
		// Show CompareWithOtherResourceDialog which return resources to compare
26
		// Show CompareWithOtherResourceDialog which return resources to compare
24
		// and ancestor if specified. Don't need to display the other dialog
27
		// and ancestor if specified. Don't need to display the other dialog
25
		showSelectAncestorDialog = false;
28
		showSelectAncestorDialog = false;
29
		// prevent reusing old temporary files
30
		cleanup();
26
		super.run(selection);
31
		super.run(selection);
27
	}
32
	}
28
33
Lines 32-35 Link Here
32
		return true;
37
		return true;
33
	}
38
	}
34
39
40
	private void cleanup() {
41
		try {
42
			String projectName = CompareWithOtherResourceDialog.TMP_PROJECT_NAME;
43
			IProject project = ResourcesPlugin.getWorkspace().getRoot()
44
					.getProject(projectName);
45
			project.delete(true, true, null);
46
		} catch (CoreException e) {
47
			CompareUIPlugin.log(e);
48
		}
49
	}
50
35
}
51
}
(-)compare/org/eclipse/compare/internal/CompareMessages.java (+14 lines)
Lines 120-125 Link Here
120
	public static String CompareWithOther_clear;
120
	public static String CompareWithOther_clear;
121
	public static String CompareWithOther_warning_two_way;
121
	public static String CompareWithOther_warning_two_way;
122
	public static String CompareWithOther_info;
122
	public static String CompareWithOther_info;
123
	public static String CompareWithOther_externalFileButton;
124
	public static String CompareWithOther_externalFile_errorTitle;
125
	public static String CompareWithOther_externalFile_errorMessage;
126
	public static String CompareWithOther_pathLabel;
127
	public static String CompareWithOther_externalFolderBUtton;
128
	public static String CompareWithOther_fileName;
129
	public static String CompareWithOther_createTmpFile_title;
130
	public static String CompareWithOther_createTmpFile_message;
131
	public static String CompareWithOther_showInEditorButton;
132
	public static String CompareWithOther_refreshButton;
133
	public static String CompareWithOther_clipboardCheckBox;
134
	public static String CompareWithOther_clipboardRadioAncestor;
135
	public static String CompareWithOther_clipboardRadioLeft;
136
	public static String CompareWithOther_clipboardRadioRight;
123
137
124
	static {
138
	static {
125
		NLS.initializeMessages(BUNDLE_NAME, CompareMessages.class);
139
		NLS.initializeMessages(BUNDLE_NAME, CompareMessages.class);
(-)compare/org/eclipse/compare/internal/CompareWithOtherResourceDialog.java (-34 / +558 lines)
Lines 10-25 Link Here
10
 *******************************************************************************/
10
 *******************************************************************************/
11
package org.eclipse.compare.internal;
11
package org.eclipse.compare.internal;
12
12
13
import java.io.ByteArrayInputStream;
14
import java.io.FileOutputStream;
15
import java.io.IOException;
16
import java.io.InputStream;
17
13
import org.eclipse.compare.CompareConfiguration;
18
import org.eclipse.compare.CompareConfiguration;
19
import org.eclipse.compare.CompareUI;
20
import org.eclipse.core.resources.IFile;
21
import org.eclipse.core.resources.IFolder;
22
import org.eclipse.core.resources.IProject;
23
import org.eclipse.core.resources.IProjectDescription;
14
import org.eclipse.core.resources.IResource;
24
import org.eclipse.core.resources.IResource;
25
import org.eclipse.core.resources.IWorkspace;
15
import org.eclipse.core.resources.IWorkspaceRoot;
26
import org.eclipse.core.resources.IWorkspaceRoot;
16
import org.eclipse.core.resources.ResourcesPlugin;
27
import org.eclipse.core.resources.ResourcesPlugin;
28
import org.eclipse.core.runtime.CoreException;
29
import org.eclipse.core.runtime.IPath;
30
import org.eclipse.core.runtime.Path;
17
import org.eclipse.jface.dialogs.IDialogConstants;
31
import org.eclipse.jface.dialogs.IDialogConstants;
18
import org.eclipse.jface.dialogs.IMessageProvider;
32
import org.eclipse.jface.dialogs.IMessageProvider;
33
import org.eclipse.jface.dialogs.MessageDialog;
19
import org.eclipse.jface.dialogs.TitleAreaDialog;
34
import org.eclipse.jface.dialogs.TitleAreaDialog;
20
import org.eclipse.jface.viewers.ISelection;
35
import org.eclipse.jface.viewers.ISelection;
21
import org.eclipse.jface.viewers.StructuredSelection;
36
import org.eclipse.jface.viewers.StructuredSelection;
22
import org.eclipse.swt.SWT;
37
import org.eclipse.swt.SWT;
38
import org.eclipse.swt.dnd.Clipboard;
23
import org.eclipse.swt.dnd.DND;
39
import org.eclipse.swt.dnd.DND;
24
import org.eclipse.swt.dnd.DragSource;
40
import org.eclipse.swt.dnd.DragSource;
25
import org.eclipse.swt.dnd.DragSourceEvent;
41
import org.eclipse.swt.dnd.DragSourceEvent;
Lines 31-36 Link Here
31
import org.eclipse.swt.dnd.Transfer;
47
import org.eclipse.swt.dnd.Transfer;
32
import org.eclipse.swt.events.ModifyEvent;
48
import org.eclipse.swt.events.ModifyEvent;
33
import org.eclipse.swt.events.ModifyListener;
49
import org.eclipse.swt.events.ModifyListener;
50
import org.eclipse.swt.events.SelectionAdapter;
34
import org.eclipse.swt.events.SelectionEvent;
51
import org.eclipse.swt.events.SelectionEvent;
35
import org.eclipse.swt.events.SelectionListener;
52
import org.eclipse.swt.events.SelectionListener;
36
import org.eclipse.swt.layout.GridData;
53
import org.eclipse.swt.layout.GridData;
Lines 38-50 Link Here
38
import org.eclipse.swt.widgets.Button;
55
import org.eclipse.swt.widgets.Button;
39
import org.eclipse.swt.widgets.Composite;
56
import org.eclipse.swt.widgets.Composite;
40
import org.eclipse.swt.widgets.Control;
57
import org.eclipse.swt.widgets.Control;
58
import org.eclipse.swt.widgets.DirectoryDialog;
59
import org.eclipse.swt.widgets.Display;
60
import org.eclipse.swt.widgets.Event;
61
import org.eclipse.swt.widgets.FileDialog;
41
import org.eclipse.swt.widgets.Group;
62
import org.eclipse.swt.widgets.Group;
42
import org.eclipse.swt.widgets.Label;
63
import org.eclipse.swt.widgets.Label;
64
import org.eclipse.swt.widgets.Listener;
43
import org.eclipse.swt.widgets.Shell;
65
import org.eclipse.swt.widgets.Shell;
44
import org.eclipse.swt.widgets.Text;
66
import org.eclipse.swt.widgets.Text;
67
import org.eclipse.ui.IEditorDescriptor;
68
import org.eclipse.ui.IEditorInput;
69
import org.eclipse.ui.IEditorReference;
70
import org.eclipse.ui.IWorkbenchPage;
71
import org.eclipse.ui.PartInitException;
72
import org.eclipse.ui.PlatformUI;
45
import org.eclipse.ui.forms.events.ExpansionAdapter;
73
import org.eclipse.ui.forms.events.ExpansionAdapter;
46
import org.eclipse.ui.forms.events.ExpansionEvent;
74
import org.eclipse.ui.forms.events.ExpansionEvent;
47
import org.eclipse.ui.forms.widgets.ExpandableComposite;
75
import org.eclipse.ui.forms.widgets.ExpandableComposite;
76
import org.eclipse.ui.part.FileEditorInput;
48
import org.eclipse.ui.part.ResourceTransfer;
77
import org.eclipse.ui.part.ResourceTransfer;
49
78
50
/**
79
/**
Lines 55-63 Link Here
55
public class CompareWithOtherResourceDialog extends TitleAreaDialog {
84
public class CompareWithOtherResourceDialog extends TitleAreaDialog {
56
85
57
	private int CLEAR_RETURN_CODE = 150; // any number != 0
86
	private int CLEAR_RETURN_CODE = 150; // any number != 0
87
	private int OPEN_EXTERNAL_RETURN_CODE = 153;
58
	private int MIN_WIDTH = 300;
88
	private int MIN_WIDTH = 300;
59
	private int MIN_HEIGHT = 175;
89
	private int MIN_HEIGHT = 175;
60
90
91
	public static final String TMP_PROJECT_NAME = ".org.eclipse.compare.tmp"; //$NON-NLS-1$
92
	private final static String TMP_PROJECT_FILE = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" //$NON-NLS-1$
93
			+ "<projectDescription>\n" //$NON-NLS-1$
94
			+ "\t<name>" + TMP_PROJECT_NAME + "\t</name>\n" //$NON-NLS-1$ //$NON-NLS-2$
95
			+ "\t<comment></comment>\n" //$NON-NLS-1$
96
			+ "\t<projects>\n" //$NON-NLS-1$
97
			+ "\t</projects>\n" //$NON-NLS-1$
98
			+ "\t<buildSpec>\n" //$NON-NLS-1$
99
			+ "\t</buildSpec>\n" //$NON-NLS-1$
100
			+ "\t<natures>\n" + "\t</natures>\n" //$NON-NLS-1$//$NON-NLS-2$
101
			+ "</projectDescription>"; //$NON-NLS-1$
102
	public final static String EXTERNAL_FILES_FOLDER_NAME = "ExternalFilesFolder"; //$NON-NLS-1$
103
61
	private class FileTextDragListener implements DragSourceListener {
104
	private class FileTextDragListener implements DragSourceListener {
62
105
63
		private InternalSection section;
106
		private InternalSection section;
Lines 71-77 Link Here
71
		}
114
		}
72
115
73
		public void dragSetData(DragSourceEvent event) {
116
		public void dragSetData(DragSourceEvent event) {
74
			event.data = section.fileText.getText();
117
			if (TextTransfer.getInstance().isSupportedType(event.dataType)) {
118
				event.data = section.fileText.getText();
119
			} else
120
				event.data = section.fileText.getText();
75
		}
121
		}
76
122
77
		public void dragStart(DragSourceEvent event) {
123
		public void dragStart(DragSourceEvent event) {
Lines 137-148 Link Here
137
183
138
			if (textTransfer.isSupportedType(event.currentDataType)) {
184
			if (textTransfer.isSupportedType(event.currentDataType)) {
139
				String txt = (String) event.data;
185
				String txt = (String) event.data;
140
				IResource r = ResourcesPlugin.getWorkspace().getRoot().findMember(txt);
186
				section.setResource(ResourcesPlugin.getWorkspace().getRoot()
141
				if (r != null)
187
						.findMember(txt));
142
					section.setResource(r);
188
				section.updateAllSectionInfo();
143
			} else if (resourceTransfer.isSupportedType(event.currentDataType)) {
189
			} else if (resourceTransfer.isSupportedType(event.currentDataType)) {
144
				IResource[] files = (IResource[]) event.data;
190
				IResource[] files = (IResource[]) event.data;
145
				section.setResource(files[0]);
191
				section.setResource(files[0]);
192
				section.updateAllSectionInfo();
146
			}
193
			}
147
194
148
			updateErrorInfo();
195
			updateErrorInfo();
Lines 159-164 Link Here
159
		protected Group group;
206
		protected Group group;
160
		protected Text fileText;
207
		protected Text fileText;
161
		private IResource resource;
208
		private IResource resource;
209
		private Button openExternalFileButton, openExternalFolderButton;
210
		private String path = ""; //$NON-NLS-1$
211
		private Text pathLabel;
212
		protected Label pLabel, fileLabel;
162
213
163
		public InternalSection(Composite parent) {
214
		public InternalSection(Composite parent) {
164
			createContents(parent);
215
			createContents(parent);
Lines 169-181 Link Here
169
		}
220
		}
170
221
171
		public void createContents(Composite parent) {
222
		public void createContents(Composite parent) {
172
			createGroup(parent);
223
173
			createFileLabel();
224
			group = new Group(parent, SWT.NONE);
174
			createFileCombo();
225
			group.setLayout(new GridLayout(2, false));
226
			group.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
227
228
			Composite infoComposite = new Composite(group, SWT.NONE);
229
			infoComposite.setLayout(new GridLayout(2, false));
230
			infoComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
231
			createInfo(infoComposite);
232
233
			Composite buttonsComposite = new Composite(group, SWT.NONE);
234
			buttonsComposite.setLayout(new GridLayout(-1, true));
235
			buttonsComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, true));
236
			createButtons(buttonsComposite);
237
175
			initDrag();
238
			initDrag();
176
			initDrop();
239
			initDrop();
177
		}
240
		}
178
241
242
		public void setEnabled(boolean enabled) {
243
			fileText.setEnabled(enabled);
244
			pLabel.setEnabled(enabled);
245
			openExternalFileButton.setEnabled(enabled);
246
			openExternalFolderButton.setEnabled(enabled);
247
			pathLabel.setEnabled(enabled);
248
			fileLabel.setEnabled(enabled);
249
			group.setEnabled(enabled);
250
			if (enabled)
251
				setResource(fileText.getText());
252
		}
253
179
		public IResource getResource() {
254
		public IResource getResource() {
180
			return resource;
255
			return resource;
181
		}
256
		}
Lines 184-199 Link Here
184
			this.resource = resource;
259
			this.resource = resource;
185
			String txt = resource.getFullPath().toString();
260
			String txt = resource.getFullPath().toString();
186
			fileText.setText(txt);
261
			fileText.setText(txt);
262
			path = resource.getLocation().toOSString();
263
			pathLabel.setText(dividePath(path));
187
		}
264
		}
188
265
189
		public void setResource(String s) {
266
		public void setResource(String s) {
190
			IResource tmp = ResourcesPlugin.getWorkspace().getRoot()
267
			IResource tmp = ResourcesPlugin.getWorkspace().getRoot()
191
					.findMember(s);
268
					.findMember(s);
192
			if (tmp instanceof IWorkspaceRoot)
269
			if (tmp instanceof IWorkspaceRoot) {
193
				resource = null;
270
				resource = null;
194
			else
271
				pathLabel.setText(""); //$NON-NLS-1$
272
			}
273
			else {
195
				resource = tmp;
274
				resource = tmp;
275
				path = resource.getLocation().toOSString();
276
				pathLabel.setText(dividePath(path));
277
			}
278
		}
196
279
280
		public void setResourceFromClipboard(IFile file) {
281
			resource = file;
282
			updateErrorInfo();
197
		}
283
		}
198
284
199
		protected void clearResource() {
285
		protected void clearResource() {
Lines 220-261 Link Here
220
			target.addDropListener(new FileTextDropListener(this));
306
			target.addDropListener(new FileTextDropListener(this));
221
		}
307
		}
222
308
223
		protected void createGroup(Composite parent) {
309
		protected void createButtons(Composite parent) {
224
			group = new Group(parent, SWT.NONE);
310
			createOpenExternalFileButton(parent);
225
			group.setLayout(new GridLayout(3, false));
311
			createOpenExternalFolderButton(parent);
226
			group.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
227
		}
312
		}
228
313
229
		protected void createFileCombo() {
314
		protected void createInfo(Composite parent) {
230
			fileText = new Text(group, SWT.BORDER);
315
			createFileLabel(parent);
231
			fileText
316
			createFileCombo(parent);
232
					.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
317
			createPathLabel(parent);
318
		}
319
320
		protected void createFileCombo(Composite parent) {
321
			fileText = new Text(parent, SWT.BORDER);
322
			fileText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
233
323
234
			fileText.addModifyListener(new ModifyListener() {
324
			fileText.addModifyListener(new ModifyListener() {
235
				public void modifyText(ModifyEvent e) {
325
				public void modifyText(ModifyEvent e) {
236
					setResource(fileText.getText());
326
					setResource(fileText.getText());
327
					if (getResource() != null) {
328
						path = resource.getLocation().toOSString();
329
						pathLabel.setText(dividePath(path));
330
					} else {
331
						pathLabel.setText(""); //$NON-NLS-1$
332
					}
237
					updateErrorInfo();
333
					updateErrorInfo();
238
				}
334
				}
239
			});
335
			});
240
336
241
			fileText.addSelectionListener(new SelectionListener() {
337
			fileText.addSelectionListener(new SelectionListener() {
242
243
				public void widgetDefaultSelected(SelectionEvent e) {
338
				public void widgetDefaultSelected(SelectionEvent e) {
244
					widgetSelected(e);
339
					widgetSelected(e);
245
				}
340
				}
246
247
				public void widgetSelected(SelectionEvent e) {
341
				public void widgetSelected(SelectionEvent e) {
248
					setResource(fileText.getText());
342
					setResource(fileText.getText());
249
					updateErrorInfo();
343
					updateErrorInfo();
250
				}
344
				}
345
			});
346
		}
347
348
		protected void createOpenExternalFileButton(Composite parent) {
349
			openExternalFileButton = createButton(parent,
350
					OPEN_EXTERNAL_RETURN_CODE,
351
					CompareMessages.CompareWithOther_externalFileButton, false);
352
			openExternalFileButton.addSelectionListener(new SelectionListener() {
353
				public void widgetDefaultSelected(SelectionEvent e) {
354
					widgetSelected(e);
355
				}
356
				public void widgetSelected(SelectionEvent e) {
357
					resource = getExtenalFile();
358
					updateAllSectionInfo();
359
				}
360
			});
361
		}
251
362
363
		protected void createOpenExternalFolderButton(Composite parent) {
364
			openExternalFolderButton = createButton(parent,
365
					OPEN_EXTERNAL_RETURN_CODE,
366
					CompareMessages.CompareWithOther_externalFolderBUtton, false);
367
			openExternalFolderButton.addSelectionListener(new SelectionListener() {
368
				public void widgetDefaultSelected(SelectionEvent e) {
369
					widgetSelected(e);
370
				}
371
				public void widgetSelected(SelectionEvent e) {
372
					resource = getExternalDirectory();
373
					updateAllSectionInfo();
374
				}
252
			});
375
			});
253
		}
376
		}
254
377
255
		protected void createFileLabel() {
378
		private  void updateAllSectionInfo() {
256
			final Label fileLabel = new Label(group, SWT.NONE);
379
			fileText.setText(resource.getFullPath().toOSString());
380
			pathLabel.setText(dividePath(path));
381
			getShell().pack();
382
			pathLabel.getParent().layout();
383
			updateErrorInfo();
384
		}
385
386
		private IFile getExtenalFile() {
387
			FileDialog dialog = new FileDialog(getShell());
388
			path = dialog.open();
389
			IFile  f = (IFile) getIResource(new Path(path), IResource.FILE);
390
			return f;
391
		}
392
393
		private IFolder getExternalDirectory() {
394
			DirectoryDialog dialog = new DirectoryDialog(getShell());
395
			path = dialog.open();
396
			IFolder f = (IFolder) getIResource(new Path(path), IResource.FOLDER);
397
			return f;
398
		}
399
400
		private IResource getIResource(IPath iPath, int type) {
401
			IResource r = null;
402
			IWorkspace workspace = ResourcesPlugin.getWorkspace();
403
			IWorkspaceRoot root = workspace.getRoot();
404
			IProject project = root.getProject(TMP_PROJECT_NAME);
405
			String resourceName = iPath.lastSegment();
406
			try {
407
				project = createTmpProject();
408
				if (!project.isOpen())
409
					project.open(null);
410
				IFolder folderForExternal = project.getFolder(EXTERNAL_FILES_FOLDER_NAME);
411
				if (!folderForExternal.exists())
412
					folderForExternal.create(IResource.NONE, true, null);
413
				if (type == IResource.FILE) {
414
					r = folderForExternal.getFile(resourceName);
415
					if (!r.exists())
416
						((IFile)r).createLink(iPath, IResource.REPLACE, null);
417
				}
418
				else {
419
					r = folderForExternal.getFolder(resourceName);
420
					if (!r.exists())
421
						((IFolder)r).createLink(iPath, IResource.REPLACE, null);
422
				}
423
			} catch (CoreException e) {
424
				CompareUIPlugin.log(e);
425
				MessageDialog.openError(getShell(),
426
								CompareMessages.CompareWithOther_externalFile_errorTitle,
427
								CompareMessages.CompareWithOther_externalFile_errorMessage);
428
			}
429
			return r;
430
		}
431
432
		protected void createFileLabel(Composite parent) {
433
			fileLabel = new Label(parent, SWT.NONE);
257
			fileLabel.setText(CompareMessages.CompareWithOther_fileLabel);
434
			fileLabel.setText(CompareMessages.CompareWithOther_fileLabel);
258
		}
435
		}
436
437
		protected void createPathLabel(Composite parent) {
438
			pLabel = new Label(parent, SWT.NONE);
439
			pLabel.setText(CompareMessages.CompareWithOther_pathLabel);
440
			pLabel.setLayoutData(new GridData(SWT.BEGINNING, SWT.BEGINNING, false, true));
441
			pathLabel = new Text(parent, SWT.MULTI);
442
			pathLabel.setBackground(PlatformUI.getWorkbench().getDisplay().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
443
			pathLabel.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
444
		}
445
446
		private String dividePath(String text) {
447
			String[] parts;
448
			int lineLengthLimit = computeLineLengthLimit(this);
449
			int lineLength = 0;
450
			String s = new String();
451
			String separator = new String(System.getProperty("file.separator")); //$NON-NLS-1$
452
			if (separator.equals("\\")) //$NON-NLS-1$
453
				parts = text.split("\\" + separator); //$NON-NLS-1$
454
			else
455
				parts = text.split(separator);
456
			for (int i = 0; i < parts.length; i++) {
457
				lineLength += parts[i].length();
458
				if (lineLength >= lineLengthLimit) {
459
					s += "\n"; //$NON-NLS-1$
460
					lineLength = 0;
461
				}
462
				s += parts[i];
463
				s += separator;
464
			}
465
			return s;
466
		}
259
	}
467
	}
260
468
261
	private class InternalGroup extends InternalSection {
469
	private class InternalGroup extends InternalSection {
Lines 283-317 Link Here
283
		}
491
		}
284
492
285
		public void createContents(Composite parent) {
493
		public void createContents(Composite parent) {
286
			createGroup(parent);
287
			createFileLabel();
288
			createFileCombo();
289
			createClearButton(group);
290
			initDrag();
291
			initDrop();
292
		}
293
294
		public void createGroup(Composite parent) {
295
			final Composite p = parent;
494
			final Composite p = parent;
296
			expandable = new ExpandableComposite(parent, SWT.NONE,
495
			expandable = new ExpandableComposite(parent, SWT.NONE,
297
					ExpandableComposite.TREE_NODE | ExpandableComposite.TWISTIE);
496
					ExpandableComposite.TREE_NODE | ExpandableComposite.TWISTIE);
298
			super.createGroup(expandable);
497
			super.createContents(expandable);
299
			expandable.setClient(group);
498
			expandable.setClient(group);
300
			expandable.addExpansionListener(new ExpansionAdapter() {
499
			expandable.addExpansionListener(new ExpansionAdapter() {
301
				public void expansionStateChanged(ExpansionEvent e) {
500
				public void expansionStateChanged(ExpansionEvent e) {
302
					p.layout();
501
					p.layout();
502
					getShell().pack();
303
				}
503
				}
304
			});
504
			});
305
		}
505
		}
306
506
307
		protected void createClearButton(Composite parent) {
507
		public void setEnabled(boolean enabled) {
508
			expandable.setEnabled(enabled);
509
			if (enabled)
510
				setResource(fileText.getText());
511
		}
512
513
		protected void createInfo(Composite parent) {
514
			createFileLabel(parent);
515
			createFileCombo(parent);
516
			createClearButton(parent);
517
			createPathLabel(parent);
518
		}
519
520
		private void createClearButton(Composite parent) {
308
			clearButton = createButton(parent, CLEAR_RETURN_CODE,
521
			clearButton = createButton(parent, CLEAR_RETURN_CODE,
309
					CompareMessages.CompareWithOther_clear, false);
522
					CompareMessages.CompareWithOther_clear, false);
310
			clearButton.addSelectionListener(new SelectionListener() {
523
			clearButton.addSelectionListener(new SelectionListener() {
311
				public void widgetDefaultSelected(SelectionEvent e) {
524
				public void widgetDefaultSelected(SelectionEvent e) {
312
					widgetSelected(e);
525
					widgetSelected(e);
313
				}
526
				}
314
315
				public void widgetSelected(SelectionEvent e) {
527
				public void widgetSelected(SelectionEvent e) {
316
					clearResource();
528
					clearResource();
317
				}
529
				}
Lines 328-337 Link Here
328
		}
540
		}
329
	}
541
	}
330
542
543
	private class ClipboardSection {
544
545
		private Composite contents;
546
		protected Button check;
547
		protected Text preview;
548
		protected Button showInEditorButton, refreshButton;
549
		protected Button[] panels;
550
		private IFile clipboardFile;
551
		private Clipboard clipboard;
552
		private InternalSection selectedSection;
553
554
		public ClipboardSection(Composite parent) {
555
			createContents(parent);
556
			clipboard = new Clipboard(Display.getDefault());
557
			String fileContent = (String) clipboard.getContents(TextTransfer.getInstance());
558
			clipboardFile = createClipboardFile(fileContent);
559
		}
560
561
		private void createContents(Composite parent) {
562
563
			contents = new Composite(parent, SWT.NONE);
564
			contents.setLayout(new GridLayout(3, false));
565
566
			createCheckRadioComposite(contents);
567
568
			preview = new Text(contents, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL);
569
			preview.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
570
			preview.setEnabled(false);
571
572
			createButtonComposite(contents);
573
		}
574
575
		private void createButtonComposite(Composite parent) {
576
577
			Composite buttonComposite = new Composite(parent, SWT.NONE);
578
			buttonComposite.setLayout(new GridLayout(1, false));
579
580
			createShowInEditorButton(buttonComposite);
581
			createRefreshButton(buttonComposite);
582
		}
583
584
		private void createShowInEditorButton(Composite parent) {
585
			showInEditorButton = new Button(parent, SWT.PUSH);
586
			showInEditorButton.setText(CompareMessages.CompareWithOther_showInEditorButton);
587
			showInEditorButton.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false));
588
			showInEditorButton.setEnabled(false);
589
			showInEditorButton.addSelectionListener(new SelectionListener() {
590
				public void widgetDefaultSelected(SelectionEvent e) {
591
					widgetSelected(e);
592
				}
593
				public void widgetSelected(SelectionEvent e) {
594
					IWorkbenchPage page = getWorkbenchPage();
595
					String id = getEditorId(page);
596
					try {
597
						FileEditorInput input = new FileEditorInput(clipboardFile);
598
						closeEditorWithClipboard(page, id, input);
599
						page.openEditor(input, id);
600
					} catch (PartInitException e1) {
601
						CompareUIPlugin.log(e1);
602
					}
603
				}
604
			});
605
		}
606
607
		private void createRefreshButton(Composite parent) {
608
			refreshButton = new Button(parent, SWT.PUSH);
609
			refreshButton.setText(CompareMessages.CompareWithOther_refreshButton);
610
			refreshButton.setEnabled(false);
611
			refreshButton.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false));
612
			refreshButton.addSelectionListener(new SelectionListener() {
613
				public void widgetDefaultSelected(SelectionEvent e) {
614
					widgetSelected(e);
615
				}
616
				public void widgetSelected(SelectionEvent e) {
617
					setPreviewContent();
618
					clipboardFile = createClipboardFile(preview.getText());
619
					selectedSection.setResourceFromClipboard(clipboardFile);
620
				}
621
			});
622
		}
623
624
		private String getEditorId(IWorkbenchPage page) {
625
			IEditorDescriptor descriptor = CompareUI.getPlugin().getWorkbench().getEditorRegistry().getDefaultEditor(clipboardFile.getName());
626
			return descriptor.getId();
627
		}
628
629
		private void closeEditorWithClipboard(IWorkbenchPage page, String id, IEditorInput input) throws PartInitException {
630
			IEditorReference[] editors = page.getEditorReferences();
631
			for (int i = 0; i < editors.length; i++)
632
				if (editors[i].getId().equals(id) && editors[i].getEditorInput().equals(input))
633
					page.closeEditor(editors[i].getEditor(false), false);
634
		}
635
636
		public void closeEditorWithClipboard(IWorkbenchPage page) {
637
			String id = getEditorId(page);
638
			IEditorInput input = new FileEditorInput(clipboardFile);
639
			try {
640
				closeEditorWithClipboard(page, id, input);
641
			} catch (PartInitException e) {
642
				CompareUIPlugin.log(e);
643
			}
644
		}
645
646
		private void createCheckRadioComposite(Composite parent) {
647
648
			Composite checkAndRadioComposite = new Composite(parent, SWT.NONE);
649
			checkAndRadioComposite.setLayout(new GridLayout(1, false));
650
651
			check = new Button(checkAndRadioComposite, SWT.CHECK);
652
			check.setText(CompareMessages.CompareWithOther_clipboardCheckBox);
653
654
			createRadioComposite(checkAndRadioComposite);
655
656
			check.addSelectionListener(new SelectionAdapter() {
657
				public void widgetSelected(SelectionEvent e) {
658
					if (check.getSelection() == true) {
659
						setEnableForWidgets(true);
660
						setPreviewContent();
661
					}
662
					else {
663
						setEnableForWidgets(false);
664
						setEnableForPanels(true);
665
					}
666
				}
667
			});
668
		}
669
670
		private void createRadioComposite(Composite parent) {
671
672
			Composite radioComposite = new Composite(parent, SWT.NONE);
673
			radioComposite.setLayout(new GridLayout(3, false));
674
675
			panels = new Button[3];
676
677
			panels[0] = new Button(radioComposite, SWT.RADIO);
678
			panels[0].setText(CompareMessages.CompareWithOther_clipboardRadioAncestor);
679
680
			panels[1] = new Button(radioComposite, SWT.RADIO);
681
			panels[1].setText(CompareMessages.CompareWithOther_clipboardRadioLeft);
682
683
			panels[2] = new Button(radioComposite, SWT.RADIO);
684
			panels[2].setText(CompareMessages.CompareWithOther_clipboardRadioRight);
685
686
			for (int i = 0; i < panels.length; i++)
687
				panels[i].setEnabled(false);
688
689
			Listener listener = new Listener() {
690
				public void handleEvent(Event event) {
691
					for (int i = 0; i < panels.length; i++)
692
						if (event.widget != panels[i])
693
							panels[i].setSelection(false);
694
					if (event.widget instanceof Button) {
695
						((Button)event.widget).setSelection(true);
696
						if (panels[0].getSelection())
697
							setEnableForPanels(ancestorPanel);
698
						else if (panels[1].getSelection())
699
							setEnableForPanels(leftPanel);
700
						else if (panels[2].getSelection())
701
							setEnableForPanels(rightPanel);
702
					}
703
				}
704
			};
705
			for (int i = 0; i < panels.length; i++)
706
				panels[i].addListener(SWT.Selection, listener);
707
		}
708
709
		private void setPreviewContent() {
710
			String clipboardContent = (String) clipboard.getContents(TextTransfer.getInstance());
711
			preview.setText(clipboardContent);
712
		}
713
714
		private IFile createClipboardFile(String fileContent) {
715
			IFile file = null;
716
			try {
717
				file = getClipboardFile();
718
				if (file.exists())
719
					file.delete(false, null);
720
				InputStream source = new ByteArrayInputStream(fileContent.getBytes());
721
				file.create(source, IResource.NONE, null);
722
			} catch (CoreException e) {
723
				CompareUIPlugin.log(e);
724
				MessageDialog.openError(getShell(),
725
						CompareMessages.CompareWithOther_createTmpFile_title,
726
						CompareMessages.CompareWithOther_createTmpFile_message);
727
				return file;
728
			}
729
			return file;
730
		}
731
732
		private IFile getClipboardFile() throws CoreException {
733
			IFile file = null;
734
			IProject project = createTmpProject();
735
			if (!project.isOpen())
736
				project.open(null);
737
			IFolder folder = project.getFolder("ClipboardFolder"); //$NON-NLS-1$
738
			if (!folder.exists())
739
				folder.create(IResource.NONE, true, null);
740
			file = folder.getFile(CompareMessages.CompareWithOther_fileName);
741
			return file;
742
		}
743
744
		private void setEnableForPanels(InternalSection selectedSection) {
745
			this.selectedSection = selectedSection;
746
			InternalSection[] sections = {ancestorPanel, leftPanel, rightPanel};
747
			for (int i = 0; i < sections.length; i++)
748
				if (sections[i] == selectedSection) {
749
					sections[i].setEnabled(false);
750
					sections[i].setResourceFromClipboard(clipboardFile);
751
				} else
752
					sections[i].setEnabled(true);
753
		}
754
755
		private void setEnableForPanels(boolean enabled) {
756
			InternalSection[] sections = {ancestorPanel, leftPanel, rightPanel};
757
			for (int i = 0; i < sections.length; i++)
758
				sections[i].setEnabled(enabled);
759
		}
760
761
		private void setEnableForWidgets(boolean enabled) {
762
			for (int i = 0; i < panels.length; i++)
763
				panels[i].setEnabled(enabled);
764
			showInEditorButton.setEnabled(enabled);
765
			refreshButton.setEnabled(enabled);
766
			preview.setEnabled(enabled);
767
		}
768
769
		public void setLayoutData(GridData data) {
770
			contents.setLayoutData(data);
771
		}
772
773
	}
774
331
	private Button okButton;
775
	private Button okButton;
332
	private InternalGroup rightPanel, leftPanel;
776
	private InternalGroup rightPanel, leftPanel;
333
	private InternalExpandable ancestorPanel;
777
	public InternalExpandable ancestorPanel;
778
	private ClipboardSection clipboardSection;
334
	private ISelection fselection;
779
	private ISelection fselection;
780
	private int lineLimit = 0;
335
781
336
	/**
782
	/**
337
	 * Creates the dialog.
783
	 * Creates the dialog.
Lines 376-381 Link Here
376
		rightPanel.setText(CompareMessages.CompareWithOther_rightPanel);
822
		rightPanel.setText(CompareMessages.CompareWithOther_rightPanel);
377
		rightPanel.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
823
		rightPanel.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
378
824
825
		clipboardSection = new ClipboardSection(mainPanel);
826
		GridData data = new GridData(SWT.FILL, SWT.FILL, false, true);
827
		data.horizontalSpan = 2;
828
		clipboardSection.setLayoutData(data);
829
379
		setSelection(fselection);
830
		setSelection(fselection);
380
		getShell().setText(CompareMessages.CompareWithOther_dialogTitle);
831
		getShell().setText(CompareMessages.CompareWithOther_dialogTitle);
381
		setTitle(CompareMessages.CompareWithOther_dialogMessage);
832
		setTitle(CompareMessages.CompareWithOther_dialogMessage);
Lines 457-462 Link Here
457
		}
908
		}
458
	}
909
	}
459
910
911
	public int computeLineLengthLimit(InternalSection section) {
912
		int leftLenght = leftPanel.fileText.getCharCount();
913
		int rightLength = rightPanel.fileText.getCharCount();
914
		int maxLenght = (leftLenght > rightLength ? leftLenght : rightLength);
915
		if (maxLenght > lineLimit)
916
			lineLimit = maxLenght;
917
		if (section instanceof InternalExpandable)
918
			return Math.max(lineLimit * 2 + 20, 90);
919
		return maxLenght;
920
	}
921
460
	/**
922
	/**
461
	 * Returns table with selected resources. If any resource wasn't chosen in
923
	 * Returns table with selected resources. If any resource wasn't chosen in
462
	 * the ancestor panel, table has only two elements -- resources chosen in
924
	 * the ancestor panel, table has only two elements -- resources chosen in
Lines 477-480 Link Here
477
					rightResource };
939
					rightResource };
478
		return resources;
940
		return resources;
479
	}
941
	}
942
943
	/*
944
	 * (non-javadoc)
945
	 *
946
	 * Implementation based on org.eclipse.jdt.internakl.core.ExternalFoldersManager#createExternalFoldersProject
947
	 */
948
	private IProject createTmpProject() throws CoreException {
949
		IProject tmpProject = getTmpProject();
950
		if (!tmpProject.isAccessible()) {
951
			try {
952
				if (!tmpProject.exists()) {
953
					IProjectDescription desc = tmpProject.getWorkspace()
954
							.newProjectDescription(tmpProject.getName());
955
					IPath location = CompareUI.getPlugin()
956
							.getStateLocation();
957
					desc.setLocation(location.append(TMP_PROJECT_NAME));
958
					tmpProject.create(desc, null);
959
				}
960
				try {
961
					tmpProject.open(null);
962
				} catch (CoreException e1) { // in case .project file or folder has been deleted
963
					IPath location1 = CompareUI.getPlugin()
964
							.getStateLocation();
965
					IPath projectPath1 = location1.append(TMP_PROJECT_NAME);
966
					projectPath1.toFile().mkdirs();
967
					FileOutputStream output = new FileOutputStream(
968
							projectPath1.append(".project").toOSString()); //$NON-NLS-1$
969
					try {
970
						output.write(TMP_PROJECT_FILE.getBytes());
971
					} finally {
972
						output.close();
973
					}
974
					tmpProject.open(null);
975
				}
976
			} catch (IOException ioe) {
977
				return tmpProject;
978
			} catch (CoreException ce) {
979
				throw new CoreException(ce.getStatus());
980
			}
981
		}
982
		return tmpProject;
983
	}
984
985
	/*
986
	 * (non-javadoc)
987
	 *
988
	 * Following method is analogical to org.eclipse.jdt.internal
989
	 */
990
	private IProject getTmpProject() {
991
		return ResourcesPlugin.getWorkspace().getRoot().getProject(
992
				TMP_PROJECT_NAME);
993
	}
994
995
	private IWorkbenchPage getWorkbenchPage() {
996
		return CompareUI.getPlugin().getWorkbench().getActiveWorkbenchWindow().getActivePage();
997
	}
998
999
	public void okPressed() {
1000
		IWorkbenchPage page = getWorkbenchPage();
1001
		clipboardSection.closeEditorWithClipboard(page);
1002
		super.okPressed();
1003
	}
480
}
1004
}
(-)compare/org/eclipse/compare/internal/CompareMessages.properties (+14 lines)
Lines 132-134 Link Here
132
CompareWithOther_warning_two_way=Ancestor is not a valid resource. Two-way compare will be performed.
132
CompareWithOther_warning_two_way=Ancestor is not a valid resource. Two-way compare will be performed.
133
CompareWithOther_clear=Clear
133
CompareWithOther_clear=Clear
134
CompareWithOther_info=Drag files from a view or between dialog's fields.
134
CompareWithOther_info=Drag files from a view or between dialog's fields.
135
CompareWithOther_externalFileButton=External file...
136
CompareWithOther_externalFolderBUtton=External folder...
137
CompareWithOther_externalFile_errorTitle=Compare With Other Resource Error
138
CompareWithOther_externalFile_errorMessage=I cannot create a link to external file.
139
CompareWithOther_pathLabel=Path:
140
CompareWithOther_fileName=clipboard.txt
141
CompareWithOther_createTmpFile_title= Unable to create file
142
CompareWithOther_createTmpFile_message= A file cannot be created from clipboard content.
143
CompareWithOther_showInEditorButton=Show in editor
144
CompareWithOther_refreshButton=Refresh
145
CompareWithOther_clipboardCheckBox=Use clipboard's content
146
CompareWithOther_clipboardRadioAncestor=Ancestor
147
CompareWithOther_clipboardRadioLeft=Left
148
CompareWithOther_clipboardRadioRight=Right

Return to bug 241088