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

Collapse All | Expand All

(-)compare/org/eclipse/compare/internal/CompareWithOtherResourceAction.java (+2 lines)
Lines 23-28 Link Here
23
		// Show CompareWithOtherResourceDialog which return resources to compare
23
		// Show CompareWithOtherResourceDialog which return resources to compare
24
		// and ancestor if specified. Don't need to display the other dialog
24
		// and ancestor if specified. Don't need to display the other dialog
25
		showSelectAncestorDialog = false;
25
		showSelectAncestorDialog = false;
26
		// prevent reusing old temporary files
27
		CompareWithOtherResourceDialog.cleanup();
26
		super.run(selection);
28
		super.run(selection);
27
	}
29
	}
28
30
(-)compare/org/eclipse/compare/internal/CompareMessages.java (+17 lines)
Lines 118-125 Link Here
118
	public static String CompareWithOther_error_not_comparable;
118
	public static String CompareWithOther_error_not_comparable;
119
	public static String CompareWithOther_error_empty;
119
	public static String CompareWithOther_error_empty;
120
	public static String CompareWithOther_clear;
120
	public static String CompareWithOther_clear;
121
	public static String CompareWithOther_clipboardRadioButton;
121
	public static String CompareWithOther_warning_two_way;
122
	public static String CompareWithOther_warning_two_way;
122
	public static String CompareWithOther_info;
123
	public static String CompareWithOther_info;
124
	public static String CompareWithOther_externalFileButton;
125
	public static String CompareWithOther_externalFile_errorTitle;
126
	public static String CompareWithOther_externalFile_errorMessage;
127
	public static String CompareWithOther_pathLabel;
128
	public static String CompareWithOther_externalFolderBUtton;
129
	public static String CompareWithOtherResourceDialog_externalFileMainButton;
130
	public static String CompareWithOtherResourceDialog_externalFileRadioButton;
131
	public static String CompareWithOtherResourceDialog_externalFolderMainButton;
132
	public static String CompareWithOtherResourceDialog_externalFolderRadioButton;
133
	public static String CompareWithOtherResourceDialog_workspaceMainButton;
134
	public static String CompareWithOtherResourceDialog_workspaceRadioButton;
135
	public static String CompareWithOther_showInEditorButton;
136
	public static String CompareWithOther_refreshButton;
137
	public static String CompareWithOther_createTmpFile_title;
138
	public static String CompareWithOther_createTmpFile_message;
139
	public static String CompareWithOther_fileName;
123
140
124
	static {
141
	static {
125
		NLS.initializeMessages(BUNDLE_NAME, CompareMessages.class);
142
		NLS.initializeMessages(BUNDLE_NAME, CompareMessages.class);
(-)compare/org/eclipse/compare/internal/CompareWithOtherResourceDialog.java (-99 / +665 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
import java.util.ArrayList;
18
13
import org.eclipse.compare.CompareConfiguration;
19
import org.eclipse.compare.CompareConfiguration;
20
import org.eclipse.compare.CompareUI;
21
import org.eclipse.core.resources.IFile;
22
import org.eclipse.core.resources.IFolder;
23
import org.eclipse.core.resources.IProject;
24
import org.eclipse.core.resources.IProjectDescription;
14
import org.eclipse.core.resources.IResource;
25
import org.eclipse.core.resources.IResource;
26
import org.eclipse.core.resources.IWorkspace;
15
import org.eclipse.core.resources.IWorkspaceRoot;
27
import org.eclipse.core.resources.IWorkspaceRoot;
16
import org.eclipse.core.resources.ResourcesPlugin;
28
import org.eclipse.core.resources.ResourcesPlugin;
29
import org.eclipse.core.runtime.CoreException;
30
import org.eclipse.core.runtime.IPath;
31
import org.eclipse.core.runtime.Path;
17
import org.eclipse.jface.dialogs.IDialogConstants;
32
import org.eclipse.jface.dialogs.IDialogConstants;
18
import org.eclipse.jface.dialogs.IMessageProvider;
33
import org.eclipse.jface.dialogs.IMessageProvider;
34
import org.eclipse.jface.dialogs.MessageDialog;
19
import org.eclipse.jface.dialogs.TitleAreaDialog;
35
import org.eclipse.jface.dialogs.TitleAreaDialog;
20
import org.eclipse.jface.viewers.ISelection;
36
import org.eclipse.jface.viewers.ISelection;
21
import org.eclipse.jface.viewers.StructuredSelection;
37
import org.eclipse.jface.viewers.StructuredSelection;
22
import org.eclipse.swt.SWT;
38
import org.eclipse.swt.SWT;
39
import org.eclipse.swt.dnd.Clipboard;
23
import org.eclipse.swt.dnd.DND;
40
import org.eclipse.swt.dnd.DND;
24
import org.eclipse.swt.dnd.DragSource;
41
import org.eclipse.swt.dnd.DragSource;
25
import org.eclipse.swt.dnd.DragSourceEvent;
42
import org.eclipse.swt.dnd.DragSourceEvent;
Lines 36-50 Link Here
36
import org.eclipse.swt.layout.GridData;
53
import org.eclipse.swt.layout.GridData;
37
import org.eclipse.swt.layout.GridLayout;
54
import org.eclipse.swt.layout.GridLayout;
38
import org.eclipse.swt.widgets.Button;
55
import org.eclipse.swt.widgets.Button;
56
import org.eclipse.swt.widgets.Combo;
39
import org.eclipse.swt.widgets.Composite;
57
import org.eclipse.swt.widgets.Composite;
40
import org.eclipse.swt.widgets.Control;
58
import org.eclipse.swt.widgets.Control;
59
import org.eclipse.swt.widgets.DirectoryDialog;
60
import org.eclipse.swt.widgets.Display;
61
import org.eclipse.swt.widgets.Event;
62
import org.eclipse.swt.widgets.FileDialog;
41
import org.eclipse.swt.widgets.Group;
63
import org.eclipse.swt.widgets.Group;
42
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.IEditorReference;
69
import org.eclipse.ui.IWorkbenchPage;
70
import org.eclipse.ui.PartInitException;
45
import org.eclipse.ui.forms.events.ExpansionAdapter;
71
import org.eclipse.ui.forms.events.ExpansionAdapter;
46
import org.eclipse.ui.forms.events.ExpansionEvent;
72
import org.eclipse.ui.forms.events.ExpansionEvent;
47
import org.eclipse.ui.forms.widgets.ExpandableComposite;
73
import org.eclipse.ui.forms.widgets.ExpandableComposite;
74
import org.eclipse.ui.internal.ide.dialogs.OpenResourceDialog;
75
import org.eclipse.ui.part.FileEditorInput;
48
import org.eclipse.ui.part.ResourceTransfer;
76
import org.eclipse.ui.part.ResourceTransfer;
49
77
50
/**
78
/**
Lines 53-93 Link Here
53
 * @since 3.4
81
 * @since 3.4
54
 */
82
 */
55
public class CompareWithOtherResourceDialog extends TitleAreaDialog {
83
public class CompareWithOtherResourceDialog extends TitleAreaDialog {
56
84
	
57
	private int CLEAR_RETURN_CODE = 150; // any number != 0
58
	private int MIN_WIDTH = 300;
85
	private int MIN_WIDTH = 300;
59
	private int MIN_HEIGHT = 175;
86
	private int MIN_HEIGHT = 175;
87
	
88
	private static final String TMP_PROJECT_NAME = ".org.eclipse.compare.tmp"; //$NON-NLS-1$
89
	private final static String TMP_PROJECT_FILE = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" //$NON-NLS-1$
90
			+ "<projectDescription>\n" //$NON-NLS-1$
91
			+ "\t<name>" + TMP_PROJECT_NAME + "\t</name>\n" //$NON-NLS-1$ //$NON-NLS-2$
92
			+ "\t<comment></comment>\n" //$NON-NLS-1$
93
			+ "\t<projects>\n" //$NON-NLS-1$
94
			+ "\t</projects>\n" //$NON-NLS-1$
95
			+ "\t<buildSpec>\n" //$NON-NLS-1$
96
			+ "\t</buildSpec>\n" //$NON-NLS-1$
97
			+ "\t<natures>\n" + "\t</natures>\n" //$NON-NLS-1$//$NON-NLS-2$
98
			+ "</projectDescription>"; //$NON-NLS-1$
99
	private final static String EXTERNAL_FILES_FOLDER_NAME = "ExternalFilesFolder"; //$NON-NLS-1$
60
100
61
	private class FileTextDragListener implements DragSourceListener {
101
	private class FileTextDragListener implements DragSourceListener {
62
102
63
		private InternalSection section;
103
		private ContentTypeElement element;
64
104
65
		public FileTextDragListener(InternalSection section) {
105
		public FileTextDragListener(ContentTypeElement element) {
66
			this.section = section;
106
			this.element = element;
67
		}
107
		}
68
108
69
		public void dragFinished(DragSourceEvent event) {
109
		public void dragFinished(DragSourceEvent event) {
70
			section.fileText.setText(""); //$NON-NLS-1$
110
			element.setText(""); //$NON-NLS-1$
71
		}
111
		}
72
112
73
		public void dragSetData(DragSourceEvent event) {
113
		public void dragSetData(DragSourceEvent event) {
74
			event.data = section.fileText.getText();
114
			event.data = element.getText();
75
		}
115
		}
76
116
77
		public void dragStart(DragSourceEvent event) {
117
		public void dragStart(DragSourceEvent event) {
78
			if (section.fileText.getText() == null)
118
			if (element.getText() == null)
79
				event.doit = false;
119
				event.doit = false;
80
		}
120
		}
81
	}
121
	}
82
122
83
	private class FileTextDropListener implements DropTargetListener {
123
	private class FileTextDropListener implements DropTargetListener {
84
124
85
		private InternalSection section;
125
		private ContentTypeElement element;
86
		private ResourceTransfer resourceTransfer;
126
		private ResourceTransfer resourceTransfer;
87
		private TextTransfer textTransfer;
127
		private TextTransfer textTransfer;
88
128
89
		public FileTextDropListener(InternalSection section) {
129
		public FileTextDropListener(ContentTypeElement element) {
90
			this.section = section;
130
			this.element = element;
91
			resourceTransfer = ResourceTransfer.getInstance();
131
			resourceTransfer = ResourceTransfer.getInstance();
92
			textTransfer = TextTransfer.getInstance();
132
			textTransfer = TextTransfer.getInstance();
93
		}
133
		}
Lines 139-151 Link Here
139
				String txt = (String) event.data;
179
				String txt = (String) event.data;
140
				IResource r = ResourcesPlugin.getWorkspace().getRoot().findMember(txt);
180
				IResource r = ResourcesPlugin.getWorkspace().getRoot().findMember(txt);
141
				if (r != null)
181
				if (r != null)
142
					section.setResource(r);
182
					element.setResource(r);
143
			} else if (resourceTransfer.isSupportedType(event.currentDataType)) {
183
			} else if (resourceTransfer.isSupportedType(event.currentDataType)) {
144
				IResource[] files = (IResource[]) event.data;
184
				IResource[] files = (IResource[]) event.data;
145
				section.setResource(files[0]);
185
				if (files.length > 0)
146
			}
186
					element.setResource(files[0]);
187
			} 
147
188
148
			updateErrorInfo();
149
		}
189
		}
150
190
151
		public void dropAccept(DropTargetEvent event) {
191
		public void dropAccept(DropTargetEvent event) {
Lines 153-209 Link Here
153
		}
193
		}
154
194
155
	}
195
	}
156
196
	
157
	private abstract class InternalSection {
197
	private abstract class ContentTypeElement {
158
198
		
159
		protected Group group;
199
		private Button radioButton;
160
		protected Text fileText;
200
		protected Button mainButton;
201
		private String type;
202
		protected Combo text;
203
		protected InternalSection section;
161
		private IResource resource;
204
		private IResource resource;
162
205
		protected ArrayList history;
163
		public InternalSection(Composite parent) {
206
		
207
		public ContentTypeElement(Composite parent, String type, InternalSection section) {
208
			this.type = type;
209
			this.section = section;
210
			history = new ArrayList();
164
			createContents(parent);
211
			createContents(parent);
165
		}
212
		}
213
		
214
		private void createContents(Composite parent) {
215
			createRadioButton(parent);
216
			createText(parent);
217
			createMainButton(parent);
218
		}
219
		
220
		private void createRadioButton(Composite parent) {
221
			radioButton = new Button(parent, SWT.RADIO);
222
			radioButton.setText(type);
223
		}
224
		
225
		protected void createText(Composite parent) {
226
			text = new Combo(parent, SWT.BORDER);
227
			text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
166
228
167
		private InternalSection() {
229
			text.addSelectionListener(new SelectionListener() {
168
			// not to instantiate
230
				public void widgetDefaultSelected(SelectionEvent e) {
231
					widgetSelected(e);
232
				}
233
				public void widgetSelected(SelectionEvent e) {
234
					int index = text.getSelectionIndex();
235
					if (index < 0)
236
						return;
237
					if (history.get(index) instanceof IResource) {
238
						setResource((IResource)history.get(index));
239
						updateErrorInfo();
240
					}
241
				}
242
			});
169
		}
243
		}
170
244
		
171
		public void createContents(Composite parent) {
245
		protected void createMainButton(Composite parent) {
172
			createGroup(parent);
246
			mainButton = new Button(parent, SWT.PUSH);
173
			createFileLabel();
247
			mainButton.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false));
174
			createFileCombo();
248
		}
175
			initDrag();
249
		
176
			initDrop();
250
		protected Button getRadioButton() {
251
			return radioButton;
252
		}
253
		
254
		protected String getText() {
255
			return text.getText();
256
		}
257
		
258
		protected void setText(String string) {
259
			text.setText(string);
260
		}
261
		
262
		protected InternalSection getSection() {
263
			return section;
264
		}
265
		
266
		protected void setEnabled(boolean enabled) {
267
			radioButton.setSelection(enabled);
268
			mainButton.setEnabled(enabled);
269
			if (text != null)
270
				text.setEnabled(enabled);
271
		}
272
		
273
		protected void setResource(IResource resource) {
274
			this.resource = resource;
275
			section.setResource(resource);
276
			if (resource != null) {
277
				for (int i = 0; i < history.size(); i++)
278
					if (history.get(i).equals(resource))
279
						return;
280
				history.add(resource);
281
			}
177
		}
282
		}
178
283
179
		public IResource getResource() {
284
		public IResource getResource() {
180
			return resource;
285
			return resource;
181
		}
286
		}
182
287
		
183
		public void setResource(IResource resource) {
288
		void clearResource() {
184
			this.resource = resource;
289
			resource = null;
185
			String txt = resource.getFullPath().toString();
290
			text.setText(""); //$NON-NLS-1$
186
			fileText.setText(txt);
291
		}
292
		
293
	}
294
	
295
	private class WorkspaceContent extends ContentTypeElement {
296
		
297
		public WorkspaceContent(Composite parent, InternalSection section) {
298
			super(parent, CompareMessages.CompareWithOtherResourceDialog_workspaceRadioButton, section);
299
		}
300
		
301
		protected void createMainButton(Composite parent) {
302
			super.createMainButton(parent);
303
			mainButton.setText(CompareMessages.CompareWithOtherResourceDialog_workspaceMainButton);
304
			mainButton.addSelectionListener(new SelectionListener() {
305
				public void widgetDefaultSelected(SelectionEvent e) {
306
					widgetSelected(e);
307
				}
308
				public void widgetSelected(SelectionEvent e) {
309
					setResource(getResourceFromDialog());
310
				}
311
			});
312
		}
313
		
314
		protected void createText(Composite parent) {
315
			super.createText(parent);
316
			
317
			text.addModifyListener(new ModifyListener() {
318
				public void modifyText(ModifyEvent e) {
319
					setResource(text.getText());
320
					updateErrorInfo();
321
				}
322
			});
323
			
324
			initDrag();
325
			initDrop();
187
		}
326
		}
188
327
189
		public void setResource(String s) {
328
		protected void setResource(IResource resource) {
329
			super.setResource(resource);
330
			if (resource == null)
331
				return;
332
			String forDisplay = resource.getFullPath().toOSString();
333
			text.setText(forDisplay);
334
			for (int i = 0; i < text.getItemCount(); i++)
335
				if (forDisplay.equals(text.getItem(i)))
336
					return;
337
			text.add(forDisplay);
338
		}
339
		
340
		protected void setResource(String s) {
190
			IResource tmp = ResourcesPlugin.getWorkspace().getRoot()
341
			IResource tmp = ResourcesPlugin.getWorkspace().getRoot()
191
					.findMember(s);
342
					.findMember(s);
192
			if (tmp instanceof IWorkspaceRoot)
343
			if (tmp instanceof IWorkspaceRoot || tmp == null) {
193
				resource = null;
344
				setResource((IResource)null);
194
			else
345
			} else { // can't use simply setResource(tmp) because it causes stack overflow
195
				resource = tmp;
346
				super.setResource(tmp);
196
347
				String forDisplay = tmp.getFullPath().toOSString();
197
		}
348
				for (int i = 0; i < text.getItemCount(); i++)
198
349
					if (forDisplay.equals(text.getItem(i)))
199
		protected void clearResource() {
350
						return;
200
			resource = null;
351
				text.add(forDisplay);
201
			fileText.setText(""); //$NON-NLS-1$
352
			}
202
			updateErrorInfo();
203
		}
353
		}
354
		
355
		private IResource getResourceFromDialog() {
356
			IResource r = null;
357
			IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
358
			OpenResourceDialog dialog = new OpenResourceDialog(getShell(), root, IResource.FILE);
359
			int returnCode = dialog.open();
360
			if (returnCode != IDialogConstants.OK_ID)
361
				return null;
362
			Object[] result = dialog.getResult();
363
			if (result.length > 0)
364
				r = (IResource) result[0];
365
			return r;
366
		 }
204
367
205
		protected void initDrag() {
368
		protected void initDrag() {
206
			DragSource source = new DragSource(fileText, DND.DROP_MOVE
369
			DragSource source = new DragSource(text, DND.DROP_MOVE
207
					| DND.DROP_COPY | DND.DROP_DEFAULT);
370
					| DND.DROP_COPY | DND.DROP_DEFAULT);
208
			Transfer[] types = new Transfer[] { TextTransfer.getInstance(),
371
			Transfer[] types = new Transfer[] { TextTransfer.getInstance(),
209
					ResourceTransfer.getInstance() };
372
					ResourceTransfer.getInstance() };
Lines 212-260 Link Here
212
		}
375
		}
213
376
214
		protected void initDrop() {
377
		protected void initDrop() {
215
			DropTarget target = new DropTarget(fileText, DND.DROP_MOVE
378
			DropTarget target = new DropTarget(text, DND.DROP_MOVE
216
					| DND.DROP_COPY | DND.DROP_DEFAULT);
379
					| DND.DROP_COPY | DND.DROP_DEFAULT);
217
			Transfer[] types = new Transfer[] { TextTransfer.getInstance(),
380
			Transfer[] types = new Transfer[] { TextTransfer.getInstance(),
218
					ResourceTransfer.getInstance() };
381
					ResourceTransfer.getInstance() };
219
			target.setTransfer(types);
382
			target.setTransfer(types);
220
			target.addDropListener(new FileTextDropListener(this));
383
			target.addDropListener(new FileTextDropListener(this));
221
		}
384
		}
222
385
		
223
		protected void createGroup(Composite parent) {
386
	}
224
			group = new Group(parent, SWT.NONE);
387
	
225
			group.setLayout(new GridLayout(3, false));
388
	private class ExternalFileContent extends ContentTypeElement {
226
			group.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
389
		
390
		public ExternalFileContent(Composite parent, InternalSection section) {
391
			super(parent, CompareMessages.CompareWithOtherResourceDialog_externalFileRadioButton, section);
392
		}
393
		
394
		protected void createMainButton(Composite parent) {
395
			super.createMainButton(parent);
396
			mainButton.setText(CompareMessages.CompareWithOtherResourceDialog_externalFileMainButton);
397
			mainButton.addSelectionListener(new SelectionListener() {
398
				public void widgetDefaultSelected(SelectionEvent e) {
399
					widgetSelected(e);
400
				}
401
				public void widgetSelected(SelectionEvent e) {
402
					IResource r = getExtenalFile();
403
					if (r == null) 
404
						return;
405
					setResource(r);
406
				}
407
			});
227
		}
408
		}
228
409
229
		protected void createFileCombo() {
410
		protected void setResource(IResource resource) {
230
			fileText = new Text(group, SWT.BORDER);
411
			super.setResource(resource);
231
			fileText
412
			String forDisplay = resource.getLocation().toOSString();
232
					.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
413
			text.setText(forDisplay);
233
414
			for (int i = 0; i < text.getItemCount(); i++)
234
			fileText.addModifyListener(new ModifyListener() {
415
				if (forDisplay.equals(text.getItem(i)))
235
				public void modifyText(ModifyEvent e) {
416
					return;
236
					setResource(fileText.getText());
417
			text.add(forDisplay);
237
					updateErrorInfo();
418
		}
419
		
420
	}
421
	
422
	private class ExternalFolderContent extends ContentTypeElement {
423
		
424
		public ExternalFolderContent(Composite parent, InternalSection section) {
425
			super(parent, CompareMessages.CompareWithOtherResourceDialog_externalFolderRadioButton, section);
426
		}
427
		
428
		protected void createMainButton(Composite parent) {
429
			super.createMainButton(parent);
430
			mainButton.setText(CompareMessages.CompareWithOtherResourceDialog_externalFolderMainButton);
431
			mainButton.addSelectionListener(new SelectionListener() {
432
				public void widgetDefaultSelected(SelectionEvent e) {
433
					widgetSelected(e);
434
				}
435
				public void widgetSelected(SelectionEvent e) {
436
					IResource r = getExternalDirectory();
437
					if (r == null)
438
						return;
439
					setResource(r);
238
				}
440
				}
239
			});
441
			});
442
		}
240
443
241
			fileText.addSelectionListener(new SelectionListener() {
444
		protected void setResource(IResource resource) {
445
			super.setResource(resource);
446
			String forDisplay = resource.getLocation().toOSString();
447
			text.setText(forDisplay);
448
			for (int i = 0; i < text.getItemCount(); i++)
449
				if (forDisplay.equals(text.getItem(i)))
450
					return;
451
			text.add(forDisplay);
452
		}
453
		
454
	}
455
	
456
	private class ClipboardContent extends ContentTypeElement {
457
458
		protected Button showInEditorButton;
459
		private Text preview;
460
		private Clipboard clipboard;
461
462
		public ClipboardContent(Composite parent, InternalSection section) {
463
			super(parent, CompareMessages.CompareWithOther_clipboardRadioButton, section);
464
			clipboard = new Clipboard(Display.getDefault());
465
		}
466
		
467
		protected void createText(Composite parent) {
468
			preview = new Text(parent, SWT.BORDER | SWT.MULTI);
469
			preview.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
470
			preview.setEditable(false);
471
		}
472
473
		protected void createMainButton(Composite parent) {
474
475
			Composite buttonComposite = new Composite(parent, SWT.NONE);
476
			buttonComposite.setLayout(new GridLayout(1, false));
477
			
478
			createRefreshButton(buttonComposite);
479
			createShowInEditorButton(buttonComposite);
480
		}
481
		
482
		protected void setEnabled(boolean enabled) {
483
			super.setEnabled(enabled);
484
			showInEditorButton.setEnabled(enabled);
485
			if (enabled) {
486
				String fileContent = clipboard.getContents(TextTransfer.getInstance()).toString();
487
				preview.setText(fileContent);
488
				setResource(createClipboardFile(fileContent));
489
			}
490
		}
242
491
492
		private void createShowInEditorButton(Composite parent) {
493
			showInEditorButton = new Button(parent, SWT.PUSH);
494
			showInEditorButton.setText(CompareMessages.CompareWithOther_showInEditorButton);
495
			showInEditorButton.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false));
496
			showInEditorButton.setEnabled(false);
497
			showInEditorButton.addSelectionListener(new SelectionListener() {
243
				public void widgetDefaultSelected(SelectionEvent e) {
498
				public void widgetDefaultSelected(SelectionEvent e) {
244
					widgetSelected(e);
499
					widgetSelected(e);
245
				}
500
				}
246
247
				public void widgetSelected(SelectionEvent e) {
501
				public void widgetSelected(SelectionEvent e) {
248
					setResource(fileText.getText());
502
					IWorkbenchPage page = getWorkbenchPage();
249
					updateErrorInfo();
503
					String id = getEditorId(page);
504
					try {
505
						FileEditorInput input = new FileEditorInput((IFile)getResource());
506
						closeEditorWithClipboard(page);
507
						page.openEditor(input, id);
508
					} catch (PartInitException e1) {
509
						CompareUIPlugin.log(e1);
510
					}
250
				}
511
				}
512
			});
513
		}
251
514
515
		private void createRefreshButton(Composite parent) {
516
			mainButton = new Button(parent, SWT.PUSH);
517
			mainButton.setText(CompareMessages.CompareWithOther_refreshButton);
518
			mainButton.setEnabled(false);
519
			mainButton.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false));
520
			mainButton.addSelectionListener(new SelectionListener() {
521
				public void widgetDefaultSelected(SelectionEvent e) {
522
					widgetSelected(e);
523
				}
524
				public void widgetSelected(SelectionEvent e) {
525
					String fileContent = clipboard.getContents(TextTransfer.getInstance()).toString();
526
					preview.setText(fileContent);
527
					setResource(createClipboardFile(fileContent));
528
				}
252
			});
529
			});
253
		}
530
		}
531
	}
532
254
533
255
		protected void createFileLabel() {
534
	private abstract class InternalSection {
256
			final Label fileLabel = new Label(group, SWT.NONE);
535
		
257
			fileLabel.setText(CompareMessages.CompareWithOther_fileLabel);
536
		// there is no "enum" support in Java 1.4. Sigh...
537
		public static final int WORKSPACE = 0;
538
		public static final int EXTERNAL_FILE = 1;
539
		public static final int EXTERNAL_FOLDER = 2;
540
541
		protected Group group;
542
		private IResource resource;
543
		
544
		ExternalFileContent externalFileContent;
545
		ExternalFolderContent externalFolderContent;
546
		WorkspaceContent workspaceContent;
547
		ClipboardContent clipboardContent;
548
		
549
		public InternalSection(Composite parent) {
550
			createContents(parent);
551
		}
552
553
		private InternalSection() {
554
			// not to instantiate
555
		}
556
557
		protected void createContents(Composite parent) {
558
559
			group = new Group(parent, SWT.NONE);
560
			group.setLayout(new GridLayout(3, false));
561
			group.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
562
			
563
			workspaceContent = new WorkspaceContent(group, this);
564
			externalFileContent = new ExternalFileContent(group, this);
565
			externalFolderContent = new ExternalFolderContent(group, this);
566
			clipboardContent = new ClipboardContent(group, this);
567
			
568
			addListenersToRadioButtons();
569
		}
570
		
571
		private void addListenersToRadioButtons() {
572
			final ContentTypeElement[] elements = new ContentTypeElement[] { workspaceContent, 
573
					externalFileContent, externalFolderContent, clipboardContent };
574
			for (int i = 0; i < elements.length; i++)
575
				elements[i].getRadioButton().addListener(SWT.Selection, new Listener() {
576
					public void handleEvent(Event event) {
577
						for (int j = 0; j < elements.length; j++)
578
							if (event.widget != elements[j].getRadioButton())
579
								elements[j].setEnabled(false);
580
							else {
581
								elements[j].setEnabled(true);
582
								setResource(elements[j].getResource());
583
							}
584
					}
585
				});
586
		}
587
588
		protected IResource getResource() {
589
			return resource;
590
		}
591
592
		protected void setResource(IResource resource) {
593
			this.resource = resource;
594
			updateErrorInfo();
595
		}
596
597
		protected void clearResource() {
598
			resource = null;
599
			workspaceContent.clearResource();
600
			externalFileContent.clearResource();
601
			externalFolderContent.clearResource();
602
			updateErrorInfo();
603
		}
604
		
605
		protected void setContentType(int type) {
606
			switch(type) {
607
			case WORKSPACE: 
608
				workspaceContent.setEnabled(true);
609
				externalFileContent.setEnabled(false);
610
				externalFolderContent.setEnabled(false);
611
				break;
612
			case EXTERNAL_FILE:
613
				workspaceContent.setEnabled(false);
614
				externalFileContent.setEnabled(true);
615
				externalFolderContent.setEnabled(false);
616
				break;
617
			case EXTERNAL_FOLDER:
618
				workspaceContent.setEnabled(false);
619
				externalFileContent.setEnabled(false);
620
				externalFolderContent.setEnabled(true);
621
			}
258
		}
622
		}
259
	}
623
	}
260
624
Lines 282-317 Link Here
282
			createContents(parent);
646
			createContents(parent);
283
		}
647
		}
284
648
285
		public void createContents(Composite parent) {
649
		protected 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;
650
			final Composite p = parent;
296
			expandable = new ExpandableComposite(parent, SWT.NONE,
651
			expandable = new ExpandableComposite(parent, SWT.NONE,
297
					ExpandableComposite.TREE_NODE | ExpandableComposite.TWISTIE);
652
					ExpandableComposite.TREE_NODE | ExpandableComposite.TWISTIE);
298
			super.createGroup(expandable);
653
			super.createContents(expandable);
654
			createClearButton(group);
299
			expandable.setClient(group);
655
			expandable.setClient(group);
300
			expandable.addExpansionListener(new ExpansionAdapter() {
656
			expandable.addExpansionListener(new ExpansionAdapter() {
301
				public void expansionStateChanged(ExpansionEvent e) {
657
				public void expansionStateChanged(ExpansionEvent e) {
302
					p.layout();
658
					p.layout();
659
					getShell().pack();
303
				}
660
				}
304
			});
661
			});
305
		}
662
		}
306
663
307
		protected void createClearButton(Composite parent) {
664
		private void createClearButton(Composite parent) {
308
			clearButton = createButton(parent, CLEAR_RETURN_CODE,
665
			clearButton = new Button(parent, SWT.PUSH);
309
					CompareMessages.CompareWithOther_clear, false);
666
			clearButton.setText(CompareMessages.CompareWithOther_clear);
310
			clearButton.addSelectionListener(new SelectionListener() {
667
			clearButton.addSelectionListener(new SelectionListener() {
311
				public void widgetDefaultSelected(SelectionEvent e) {
668
				public void widgetDefaultSelected(SelectionEvent e) {
312
					widgetSelected(e);
669
					widgetSelected(e);
313
				}
670
				}
314
315
				public void widgetSelected(SelectionEvent e) {
671
				public void widgetSelected(SelectionEvent e) {
316
					clearResource();
672
					clearResource();
317
				}
673
				}
Lines 332-337 Link Here
332
	private InternalGroup rightPanel, leftPanel;
688
	private InternalGroup rightPanel, leftPanel;
333
	private InternalExpandable ancestorPanel;
689
	private InternalExpandable ancestorPanel;
334
	private ISelection fselection;
690
	private ISelection fselection;
691
	private int externalFilesCounter = 0;
692
	int clipboardCounter = 0;
335
693
336
	/**
694
	/**
337
	 * Creates the dialog.
695
	 * Creates the dialog.
Lines 403-447 Link Here
403
		IResource[] selectedResources = Utilities.getResources(selection);
761
		IResource[] selectedResources = Utilities.getResources(selection);
404
		switch (selectedResources.length) {
762
		switch (selectedResources.length) {
405
		case 1:
763
		case 1:
406
			leftPanel.setResource(selectedResources[0]);
764
			leftPanel.workspaceContent.setResource(selectedResources[0]);
407
			break;
765
			break;
408
		case 2:
766
		case 2:
409
			leftPanel.setResource(selectedResources[0]);
767
			leftPanel.workspaceContent.setResource(selectedResources[0]);
410
			rightPanel.setResource(selectedResources[1]);
768
			rightPanel.workspaceContent.setResource(selectedResources[1]);
411
			break;
769
			break;
412
		case 3:
770
		case 3:
413
			ancestorPanel.setResource(selectedResources[0]);
771
			ancestorPanel.workspaceContent.setResource(selectedResources[0]);
414
			ancestorPanel.expandable.setExpanded(true);
772
			ancestorPanel.expandable.setExpanded(true);
415
			leftPanel.setResource(selectedResources[1]);
773
			leftPanel.workspaceContent.setResource(selectedResources[1]);
416
			rightPanel.setResource(selectedResources[2]);
774
			rightPanel.workspaceContent.setResource(selectedResources[2]);
417
			break;
775
			break;
418
		}
776
		}
777
		setInitialContentTypes();
778
	}
779
780
	private void setInitialContentTypes() {
781
		ancestorPanel.setContentType(InternalSection.WORKSPACE);
782
		leftPanel.setContentType(InternalSection.WORKSPACE);
783
		rightPanel.setContentType(InternalSection.WORKSPACE);
419
	}
784
	}
420
785
421
	private boolean isComparePossible() {
786
	private boolean isComparePossible() {
422
		IResource[] resources;
787
		IResource[] resources;
423
		if (ancestorPanel.getResource() == null) {
788
		if (ancestorPanel.getResource() == null)
424
			resources = new IResource[] { leftPanel.getResource(),
789
			resources = new IResource[] { leftPanel.getResource(),
425
					rightPanel.getResource() };
790
					rightPanel.getResource() };
426
		} else {
791
		else
427
			resources = new IResource[] { ancestorPanel.getResource(),
792
			resources = new IResource[] { ancestorPanel.getResource(),
428
					leftPanel.getResource(), rightPanel.getResource() };
793
					leftPanel.getResource(), rightPanel.getResource() };
429
		}
430
794
431
		ResourceCompareInput r = new ResourceCompareInput(
795
		ResourceCompareInput r = new ResourceCompareInput(
432
				new CompareConfiguration());
796
				new CompareConfiguration());
433
		return r.isEnabled(new StructuredSelection(resources));
797
		return r.isEnabled(new StructuredSelection(resources));
434
	}
798
	}
435
799
436
	private void updateErrorInfo() {
800
	/**
801
	 * Sets a message in the title area according to selected resources.
802
	 */
803
	public void updateErrorInfo() {
437
		if (okButton != null) {
804
		if (okButton != null) {
438
			if (leftPanel.getResource() == null
805
			if (leftPanel.getResource() == null
439
					|| rightPanel.getResource() == null) {
806
					|| rightPanel.getResource() == null) {
440
				setMessage(CompareMessages.CompareWithOther_error_empty,
807
				setMessage(CompareMessages.CompareWithOther_error_empty,
441
						IMessageProvider.ERROR);
808
						IMessageProvider.ERROR);
442
				okButton.setEnabled(false);
809
				okButton.setEnabled(false);
443
			} else if (ancestorPanel.getResource() == null
810
			} else if (ancestorPanel.getResource() == null && isComparePossible()) {
444
					&& ancestorPanel.fileText.getText() != "") { //$NON-NLS-1$
445
				setMessage(CompareMessages.CompareWithOther_warning_two_way,
811
				setMessage(CompareMessages.CompareWithOther_warning_two_way,
446
						IMessageProvider.WARNING);
812
						IMessageProvider.WARNING);
447
				okButton.setEnabled(true);
813
				okButton.setEnabled(true);
Lines 456-461 Link Here
456
			}
822
			}
457
		}
823
		}
458
	}
824
	}
825
	
826
	private IFile getExtenalFile() {
827
		FileDialog dialog = new FileDialog(getShell());
828
		String path = dialog.open();
829
		if (path != null)
830
			return (IFile) getResource(new Path(path), IResource.FILE);
831
		return null;
832
	}
833
834
	private IFolder getExternalDirectory() {
835
		DirectoryDialog dialog = new DirectoryDialog(getShell());
836
		String path = dialog.open();
837
		if (path != null)
838
			return (IFolder) getResource(new Path(path), IResource.FOLDER);
839
		return null;
840
	}
841
842
	private IResource getResource(IPath path, int type) {
843
		IResource r = null;
844
		IWorkspace workspace = ResourcesPlugin.getWorkspace();
845
		IWorkspaceRoot root = workspace.getRoot();
846
		IProject project = root.getProject(TMP_PROJECT_NAME);
847
		String resourceName = path.lastSegment();
848
		try {
849
			project = createTmpProject();
850
			if (!project.isOpen())
851
				project.open(null);
852
			IFolder folderForExternal = project.getFolder(EXTERNAL_FILES_FOLDER_NAME);
853
			if (!folderForExternal.exists())
854
				folderForExternal.create(IResource.NONE, true, null);
855
			if (type == IResource.FILE) {
856
				r = folderForExternal.getFile(resourceName);
857
				if (r.exists()) { 	// add a number to file's name when there already is a file with that name in a folder
858
					String extension = path.getFileExtension();
859
					String newName = path.removeFileExtension().lastSegment(); 
860
					newName += "-" + externalFilesCounter + "." + extension; //$NON-NLS-1$ //$NON-NLS-2$
861
					r = folderForExternal.getFile(newName);
862
				}
863
				((IFile)r).createLink(path, IResource.REPLACE, null);
864
				externalFilesCounter++;
865
			}
866
			else {
867
				r = folderForExternal.getFolder(resourceName);
868
				if (r.exists()) {
869
					String newName = resourceName + "-" + externalFilesCounter; //$NON-NLS-1$
870
					r = folderForExternal.getFolder(newName);
871
				}
872
				((IFolder)r).createLink(path, IResource.REPLACE, null);
873
				externalFilesCounter++;
874
			}
875
		} catch (CoreException e) {
876
			CompareUIPlugin.log(e);
877
			MessageDialog.openError(getShell(),
878
							CompareMessages.CompareWithOther_externalFile_errorTitle,
879
							CompareMessages.CompareWithOther_externalFile_errorMessage);
880
		}
881
		return r;
882
	}
883
884
	/*
885
	 * (non-javadoc)
886
	 *
887
	 * Implementation based on org.eclipse.jdt.internal.core.ExternalFoldersManager#createExternalFoldersProject
888
	 */
889
	private IProject createTmpProject() throws CoreException {
890
		IProject tmpProject = getTmpProject();
891
		if (!tmpProject.isAccessible()) {
892
			try {
893
				if (!tmpProject.exists()) {
894
					IProjectDescription desc = tmpProject.getWorkspace()
895
							.newProjectDescription(tmpProject.getName());
896
					IPath location = CompareUI.getPlugin()
897
							.getStateLocation();
898
					desc.setLocation(location.append(TMP_PROJECT_NAME));
899
					tmpProject.create(desc, null);
900
				}
901
				try {
902
					tmpProject.open(null);
903
				} catch (CoreException e1) { // in case .project file or folder has been deleted
904
					IPath location1 = CompareUI.getPlugin()
905
							.getStateLocation();
906
					IPath projectPath1 = location1.append(TMP_PROJECT_NAME);
907
					projectPath1.toFile().mkdirs();
908
					FileOutputStream output = new FileOutputStream(
909
							projectPath1.append(".project").toOSString()); //$NON-NLS-1$
910
					try {
911
						output.write(TMP_PROJECT_FILE.getBytes());
912
					} finally {
913
						output.close();
914
					}
915
					tmpProject.open(null);
916
				}
917
			} catch (IOException ioe) {
918
				return tmpProject;
919
			} catch (CoreException ce) {
920
				throw new CoreException(ce.getStatus());
921
			}
922
		}
923
		return tmpProject;
924
	}
925
926
	/*
927
	 * (non-javadoc)
928
	 *
929
	 * Following method is analogical to org.eclipse.jdt.internal
930
	 */
931
	private IProject getTmpProject() {
932
		return ResourcesPlugin.getWorkspace().getRoot().getProject(
933
				TMP_PROJECT_NAME);
934
	}
935
	
936
	private String getEditorId(IWorkbenchPage page) {
937
		IFile file = createClipboardFile("test content"); //$NON-NLS-1$ // create empty clipboard file to get it's default editor
938
		IEditorDescriptor descriptor = CompareUI.getPlugin().getWorkbench().getEditorRegistry().getDefaultEditor(file.getName());
939
		if (descriptor != null)
940
			return descriptor.getId();
941
		return null;
942
	}
943
944
	private void closeEditorWithClipboard(IWorkbenchPage page) {
945
		IEditorReference[] editors = page.getEditorReferences();
946
		for (int j = 0; j < clipboardCounter; j++)
947
			for (int i = 0; i < editors.length; i++) {
948
				String name = CompareMessages.CompareWithOther_fileName + "-" + j + ".txt"; //$NON-NLS-1$ //$NON-NLS-2$
949
				try {
950
					if (editors[i].getEditorInput().getName().equals(name))
951
						page.closeEditor(editors[i].getEditor(false), false);
952
				} catch (PartInitException e) {
953
					CompareUIPlugin.log(e);
954
				}
955
			}
956
	}
957
958
	private IFile createClipboardFile(String fileContent) {
959
		IFile file = null;
960
		try {
961
			file = getClipboardFileHandler();
962
			InputStream source = new ByteArrayInputStream(fileContent.getBytes());
963
			file.create(source, IResource.NONE, null);
964
		} catch (CoreException e) {
965
			CompareUIPlugin.log(e);
966
			MessageDialog.openError(getShell(),
967
					CompareMessages.CompareWithOther_createTmpFile_title,
968
					CompareMessages.CompareWithOther_createTmpFile_message);
969
			return file;
970
		}
971
		return file;
972
	}
973
974
	private IFile getClipboardFileHandler() throws CoreException {
975
		IFile file = null;
976
		IProject project = createTmpProject();
977
		if (!project.isOpen())
978
			project.open(null);
979
		IFolder folder = project.getFolder("ClipboardFolder"); //$NON-NLS-1$
980
		if (!folder.exists())
981
			folder.create(IResource.NONE, true, null);
982
		file = folder.getFile(CompareMessages.CompareWithOther_fileName + "-" + clipboardCounter + ".txt"); //$NON-NLS-1$ //$NON-NLS-2$
983
		clipboardCounter++;
984
		return file;
985
	}
986
459
987
460
	/**
988
	/**
461
	 * Returns table with selected resources. If any resource wasn't chosen in
989
	 * Returns table with selected resources. If any resource wasn't chosen in
Lines 477-480 Link Here
477
					rightResource };
1005
					rightResource };
478
		return resources;
1006
		return resources;
479
	}
1007
	}
1008
	
1009
	/**
1010
	 * Removes temporary files created from clipboard's content or imported to 
1011
	 * workspace only for comparing.
1012
	 */
1013
	public static void cleanup() {
1014
		try {
1015
			IProject project = ResourcesPlugin.getWorkspace().getRoot()
1016
					.getProject(TMP_PROJECT_NAME);
1017
			project.delete(true, true, null);
1018
		} catch (CoreException e) {
1019
			CompareUIPlugin.log(e);
1020
		}
1021
	}
1022
	
1023
	private IWorkbenchPage getWorkbenchPage() {		
1024
		return CompareUI.getPlugin().getWorkbench().getActiveWorkbenchWindow().getActivePage();
1025
	}
1026
1027
	/**
1028
	 * Closes all editor pages containing cliboard files before closing the dialog
1029
	 * and sets its return code to OK.
1030
	 */
1031
	public void okPressed() {
1032
		IWorkbenchPage page = getWorkbenchPage();
1033
		closeEditorWithClipboard(page);
1034
		super.okPressed();
1035
	}
1036
1037
	/**
1038
	 * Closes all editor pages containing cliboard files before closing the dialog
1039
	 * and sets its return code to CANCEL.
1040
	 */
1041
	public void cancelPressed() {
1042
		IWorkbenchPage page = getWorkbenchPage();
1043
		closeEditorWithClipboard(page);
1044
		super.cancelPressed();
1045
	}
480
}
1046
}
(-)compare/org/eclipse/compare/internal/CompareMessages.properties (+17 lines)
Lines 131-134 Link Here
131
CompareWithOther_error_empty=Both left and right panel must contain a valid path.
131
CompareWithOther_error_empty=Both left and right panel must contain a valid path.
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_clipboardRadioButton=Clipboard
134
CompareWithOther_info=Drag files from a view or between dialog's fields.
135
CompareWithOther_info=Drag files from a view or between dialog's fields.
136
CompareWithOther_externalFileButton=External file...
137
CompareWithOther_externalFolderBUtton=External folder...
138
CompareWithOther_externalFile_errorTitle=Compare With Other Resource Error
139
CompareWithOther_externalFile_errorMessage=Cannot create a link to an external file.
140
CompareWithOther_pathLabel=Path:
141
CompareWithOtherResourceDialog_externalFileMainButton=Browse...
142
CompareWithOtherResourceDialog_externalFileRadioButton=External file
143
CompareWithOtherResourceDialog_externalFolderMainButton=Browse...
144
CompareWithOtherResourceDialog_externalFolderRadioButton=External folder
145
CompareWithOtherResourceDialog_workspaceMainButton=Browse...
146
CompareWithOtherResourceDialog_workspaceRadioButton=Workspace
147
CompareWithOther_showInEditorButton=Show in editor
148
CompareWithOther_refreshButton=Refresh
149
CompareWithOther_createTmpFile_title=Unable to create a file
150
CompareWithOther_createTmpFile_message=A file cannot be created from clipbard's content.
151
CompareWithOther_fileName=clipboard

Return to bug 240298