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

Collapse All | Expand All

(-)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/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/CompareWithOtherResourceDialog.java (-100 / +591 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 38-50 Link Here
38
import org.eclipse.swt.widgets.Button;
54
import org.eclipse.swt.widgets.Button;
39
import org.eclipse.swt.widgets.Composite;
55
import org.eclipse.swt.widgets.Composite;
40
import org.eclipse.swt.widgets.Control;
56
import org.eclipse.swt.widgets.Control;
57
import org.eclipse.swt.widgets.DirectoryDialog;
58
import org.eclipse.swt.widgets.Display;
59
import org.eclipse.swt.widgets.Event;
60
import org.eclipse.swt.widgets.FileDialog;
41
import org.eclipse.swt.widgets.Group;
61
import org.eclipse.swt.widgets.Group;
42
import org.eclipse.swt.widgets.Label;
62
import org.eclipse.swt.widgets.Listener;
43
import org.eclipse.swt.widgets.Shell;
63
import org.eclipse.swt.widgets.Shell;
44
import org.eclipse.swt.widgets.Text;
64
import org.eclipse.swt.widgets.Text;
65
import org.eclipse.ui.IEditorDescriptor;
66
import org.eclipse.ui.IEditorReference;
67
import org.eclipse.ui.IWorkbenchPage;
68
import org.eclipse.ui.PartInitException;
45
import org.eclipse.ui.forms.events.ExpansionAdapter;
69
import org.eclipse.ui.forms.events.ExpansionAdapter;
46
import org.eclipse.ui.forms.events.ExpansionEvent;
70
import org.eclipse.ui.forms.events.ExpansionEvent;
47
import org.eclipse.ui.forms.widgets.ExpandableComposite;
71
import org.eclipse.ui.forms.widgets.ExpandableComposite;
72
import org.eclipse.ui.part.FileEditorInput;
48
import org.eclipse.ui.part.ResourceTransfer;
73
import org.eclipse.ui.part.ResourceTransfer;
49
74
50
/**
75
/**
Lines 53-93 Link Here
53
 * @since 3.4
78
 * @since 3.4
54
 */
79
 */
55
public class CompareWithOtherResourceDialog extends TitleAreaDialog {
80
public class CompareWithOtherResourceDialog extends TitleAreaDialog {
56
81
	
57
	private int CLEAR_RETURN_CODE = 150; // any number != 0
58
	private int MIN_WIDTH = 300;
82
	private int MIN_WIDTH = 300;
59
	private int MIN_HEIGHT = 175;
83
	private int MIN_HEIGHT = 175;
84
	
85
	private static final String TMP_PROJECT_NAME = ".org.eclipse.compare.tmp"; //$NON-NLS-1$
86
	private final static String TMP_PROJECT_FILE = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" //$NON-NLS-1$
87
			+ "<projectDescription>\n" //$NON-NLS-1$
88
			+ "\t<name>" + TMP_PROJECT_NAME + "\t</name>\n" //$NON-NLS-1$ //$NON-NLS-2$
89
			+ "\t<comment></comment>\n" //$NON-NLS-1$
90
			+ "\t<projects>\n" //$NON-NLS-1$
91
			+ "\t</projects>\n" //$NON-NLS-1$
92
			+ "\t<buildSpec>\n" //$NON-NLS-1$
93
			+ "\t</buildSpec>\n" //$NON-NLS-1$
94
			+ "\t<natures>\n" + "\t</natures>\n" //$NON-NLS-1$//$NON-NLS-2$
95
			+ "</projectDescription>"; //$NON-NLS-1$
96
	private final static String EXTERNAL_FILES_FOLDER_NAME = "ExternalFilesFolder"; //$NON-NLS-1$
60
97
61
	private class FileTextDragListener implements DragSourceListener {
98
	private class FileTextDragListener implements DragSourceListener {
62
99
63
		private InternalSection section;
100
		private ContentTypeElement element;
64
101
65
		public FileTextDragListener(InternalSection section) {
102
		public FileTextDragListener(ContentTypeElement element) {
66
			this.section = section;
103
			this.element = element;
67
		}
104
		}
68
105
69
		public void dragFinished(DragSourceEvent event) {
106
		public void dragFinished(DragSourceEvent event) {
70
			section.fileText.setText(""); //$NON-NLS-1$
107
			element.setText(""); //$NON-NLS-1$
71
		}
108
		}
72
109
73
		public void dragSetData(DragSourceEvent event) {
110
		public void dragSetData(DragSourceEvent event) {
74
			event.data = section.fileText.getText();
111
			event.data = element.getText();
75
		}
112
		}
76
113
77
		public void dragStart(DragSourceEvent event) {
114
		public void dragStart(DragSourceEvent event) {
78
			if (section.fileText.getText() == null)
115
			if (element.getText() == null)
79
				event.doit = false;
116
				event.doit = false;
80
		}
117
		}
81
	}
118
	}
82
119
83
	private class FileTextDropListener implements DropTargetListener {
120
	private class FileTextDropListener implements DropTargetListener {
84
121
85
		private InternalSection section;
122
		private ContentTypeElement element;
86
		private ResourceTransfer resourceTransfer;
123
		private ResourceTransfer resourceTransfer;
87
		private TextTransfer textTransfer;
124
		private TextTransfer textTransfer;
88
125
89
		public FileTextDropListener(InternalSection section) {
126
		public FileTextDropListener(ContentTypeElement element) {
90
			this.section = section;
127
			this.element = element;
91
			resourceTransfer = ResourceTransfer.getInstance();
128
			resourceTransfer = ResourceTransfer.getInstance();
92
			textTransfer = TextTransfer.getInstance();
129
			textTransfer = TextTransfer.getInstance();
93
		}
130
		}
Lines 139-151 Link Here
139
				String txt = (String) event.data;
176
				String txt = (String) event.data;
140
				IResource r = ResourcesPlugin.getWorkspace().getRoot().findMember(txt);
177
				IResource r = ResourcesPlugin.getWorkspace().getRoot().findMember(txt);
141
				if (r != null)
178
				if (r != null)
142
					section.setResource(r);
179
					element.setResource(r);
143
			} else if (resourceTransfer.isSupportedType(event.currentDataType)) {
180
			} else if (resourceTransfer.isSupportedType(event.currentDataType)) {
144
				IResource[] files = (IResource[]) event.data;
181
				IResource[] files = (IResource[]) event.data;
145
				section.setResource(files[0]);
182
				if (files.length > 0)
146
			}
183
					element.setResource(files[0]);
184
			} 
147
185
148
			updateErrorInfo();
149
		}
186
		}
150
187
151
		public void dropAccept(DropTargetEvent event) {
188
		public void dropAccept(DropTargetEvent event) {
Lines 153-209 Link Here
153
		}
190
		}
154
191
155
	}
192
	}
156
193
	
157
	private abstract class InternalSection {
194
	private abstract class ContentTypeElement {
158
195
		
159
		protected Group group;
196
		private Button radioButton;
160
		protected Text fileText;
197
		protected Button mainButton;
198
		protected Text text;
199
		private String type;
200
		protected InternalSection section;
161
		private IResource resource;
201
		private IResource resource;
162
202
		
163
		public InternalSection(Composite parent) {
203
		public ContentTypeElement(Composite parent, String type, InternalSection section) {
204
			this.type = type;
205
			this.section = section;
164
			createContents(parent);
206
			createContents(parent);
165
		}
207
		}
166
208
		
167
		private InternalSection() {
209
		private void createContents(Composite parent) {
168
			// not to instantiate
210
			createRadioButton(parent);
211
			createText(parent);
212
			createMainButton(parent);
213
		}
214
		
215
		private void createRadioButton(Composite parent) {
216
			radioButton = new Button(parent, SWT.RADIO);
217
			radioButton.setText(type);
218
		}
219
220
		protected void createText(Composite parent) {
221
			text = new Text(parent, SWT.BORDER);
222
			text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
223
			text.setEditable(false);
224
		}
225
		
226
		protected void createMainButton(Composite parent) {
227
			mainButton = new Button(parent, SWT.PUSH);
228
			mainButton.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false));
229
		}
230
		
231
		protected Button getRadioButton() {
232
			return radioButton;
233
		}
234
		
235
		protected String getText() {
236
			return text.getText();
237
		}
238
		
239
		protected void setText(String string) {
240
			text.setText(string);
241
		}
242
		
243
		protected InternalSection getSection() {
244
			return section;
245
		}
246
		
247
		protected void setEnabled(boolean enabled) {
248
			radioButton.setSelection(enabled);
249
			mainButton.setEnabled(enabled);
250
			text.setEnabled(enabled);
169
		}
251
		}
170
252
		
171
		public void createContents(Composite parent) {
253
		protected void setResource(IResource resource) {
172
			createGroup(parent);
254
			this.resource = resource;
173
			createFileLabel();
255
			section.setResource(resource);
174
			createFileCombo();
175
			initDrag();
176
			initDrop();
177
		}
256
		}
178
257
179
		public IResource getResource() {
258
		public IResource getResource() {
180
			return resource;
259
			return resource;
181
		}
260
		}
182
261
		
183
		public void setResource(IResource resource) {
262
		void clearResource() {
184
			this.resource = resource;
263
			resource = null;
185
			String txt = resource.getFullPath().toString();
264
			text.setText(""); //$NON-NLS-1$
186
			fileText.setText(txt);
187
		}
265
		}
266
		
267
	}
268
	
269
	private class WorkspaceContent extends ContentTypeElement {
270
		
271
		public WorkspaceContent(Composite parent, InternalSection section) {
272
			super(parent, CompareMessages.CompareWithOtherResourceDialog_workspaceRadioButton, section);
273
		}
274
		
275
		protected void createMainButton(Composite parent) {
276
			super.createMainButton(parent);
277
			mainButton.setText(CompareMessages.CompareWithOtherResourceDialog_workspaceMainButton);
278
		}
279
		
280
		protected void createText(Composite parent) {
281
			
282
			super.createText(parent);
283
			text.setEditable(true);
284
			
285
			text.addModifyListener(new ModifyListener() {
286
				public void modifyText(ModifyEvent e) {
287
					section.setResource(text.getText());
288
					updateErrorInfo();
289
				}
290
			});
188
291
189
		public void setResource(String s) {
292
			text.addSelectionListener(new SelectionListener() {
190
			IResource tmp = ResourcesPlugin.getWorkspace().getRoot()
293
				public void widgetDefaultSelected(SelectionEvent e) {
191
					.findMember(s);
294
					widgetSelected(e);
192
			if (tmp instanceof IWorkspaceRoot)
295
				}
193
				resource = null;
296
				public void widgetSelected(SelectionEvent e) {
194
			else
297
					section.setResource(text.getText());
195
				resource = tmp;
298
					updateErrorInfo();
196
299
				}
300
			});
301
			
302
			initDrag();
303
			initDrop();
197
		}
304
		}
198
305
199
		protected void clearResource() {
306
		protected void setResource(IResource resource) {
200
			resource = null;
307
			super.setResource(resource);
201
			fileText.setText(""); //$NON-NLS-1$
308
			text.setText(resource.getFullPath().toOSString());
202
			updateErrorInfo();
203
		}
309
		}
204
310
205
		protected void initDrag() {
311
		protected void initDrag() {
206
			DragSource source = new DragSource(fileText, DND.DROP_MOVE
312
			DragSource source = new DragSource(text, DND.DROP_MOVE
207
					| DND.DROP_COPY | DND.DROP_DEFAULT);
313
					| DND.DROP_COPY | DND.DROP_DEFAULT);
208
			Transfer[] types = new Transfer[] { TextTransfer.getInstance(),
314
			Transfer[] types = new Transfer[] { TextTransfer.getInstance(),
209
					ResourceTransfer.getInstance() };
315
					ResourceTransfer.getInstance() };
Lines 212-260 Link Here
212
		}
318
		}
213
319
214
		protected void initDrop() {
320
		protected void initDrop() {
215
			DropTarget target = new DropTarget(fileText, DND.DROP_MOVE
321
			DropTarget target = new DropTarget(text, DND.DROP_MOVE
216
					| DND.DROP_COPY | DND.DROP_DEFAULT);
322
					| DND.DROP_COPY | DND.DROP_DEFAULT);
217
			Transfer[] types = new Transfer[] { TextTransfer.getInstance(),
323
			Transfer[] types = new Transfer[] { TextTransfer.getInstance(),
218
					ResourceTransfer.getInstance() };
324
					ResourceTransfer.getInstance() };
219
			target.setTransfer(types);
325
			target.setTransfer(types);
220
			target.addDropListener(new FileTextDropListener(this));
326
			target.addDropListener(new FileTextDropListener(this));
221
		}
327
		}
222
328
		
223
		protected void createGroup(Composite parent) {
329
	}
224
			group = new Group(parent, SWT.NONE);
330
	
225
			group.setLayout(new GridLayout(3, false));
331
	private class ExternalFileContent extends ContentTypeElement {
226
			group.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
332
		
333
		public ExternalFileContent(Composite parent, InternalSection section) {
334
			super(parent, CompareMessages.CompareWithOtherResourceDialog_externalFileRadioButton, section);
335
		}
336
		
337
		protected void createMainButton(Composite parent) {
338
			super.createMainButton(parent);
339
			mainButton.setText(CompareMessages.CompareWithOtherResourceDialog_externalFileMainButton);
340
			mainButton.addSelectionListener(new SelectionListener() {
341
				public void widgetDefaultSelected(SelectionEvent e) {
342
					widgetSelected(e);
343
				}
344
				public void widgetSelected(SelectionEvent e) {
345
					IResource r = getExtenalFile();
346
					if (r == null) 
347
						return;
348
					setResource(r);
349
				}
350
			});
227
		}
351
		}
228
352
229
		protected void createFileCombo() {
353
		protected void setResource(IResource resource) {
230
			fileText = new Text(group, SWT.BORDER);
354
			super.setResource(resource);
231
			fileText
355
			text.setText(resource.getLocation().toOSString());
232
					.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
356
		}
233
357
		
234
			fileText.addModifyListener(new ModifyListener() {
358
	}
235
				public void modifyText(ModifyEvent e) {
359
	
236
					setResource(fileText.getText());
360
	private class ExternalFolderContent extends ContentTypeElement {
237
					updateErrorInfo();
361
		
362
		public ExternalFolderContent(Composite parent, InternalSection section) {
363
			super(parent, CompareMessages.CompareWithOtherResourceDialog_externalFolderRadioButton, section);
364
		}
365
		
366
		protected void createMainButton(Composite parent) {
367
			super.createMainButton(parent);
368
			mainButton.setText(CompareMessages.CompareWithOtherResourceDialog_externalFolderMainButton);
369
			mainButton.addSelectionListener(new SelectionListener() {
370
				public void widgetDefaultSelected(SelectionEvent e) {
371
					widgetSelected(e);
372
				}
373
				public void widgetSelected(SelectionEvent e) {
374
					IResource r = getExternalDirectory();
375
					if (r == null)
376
						return;
377
					setResource(r);
238
				}
378
				}
239
			});
379
			});
380
		}
240
381
241
			fileText.addSelectionListener(new SelectionListener() {
382
		protected void setResource(IResource resource) {
383
			super.setResource(resource);
384
			text.setText(resource.getLocation().toOSString());
385
		}
386
		
387
	}
388
	
389
	private class ClipboardContent extends ContentTypeElement {
390
391
		protected Button showInEditorButton;
392
		private Clipboard clipboard;
393
394
		public ClipboardContent(Composite parent, InternalSection section) {
395
			super(parent, CompareMessages.CompareWithOther_clipboardRadioButton, section);
396
			clipboard = new Clipboard(Display.getDefault());
397
		}
398
		
399
		protected void createText(Composite parent) {
400
			text = new Text(parent, SWT.BORDER | SWT.MULTI);
401
			text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
402
			text.setEditable(false);
403
		}
404
405
		protected void createMainButton(Composite parent) {
406
407
			Composite buttonComposite = new Composite(parent, SWT.NONE);
408
			buttonComposite.setLayout(new GridLayout(1, false));
409
			
410
			createRefreshButton(buttonComposite);
411
			createShowInEditorButton(buttonComposite);
412
		}
413
		
414
		protected void setEnabled(boolean enabled) {
415
			super.setEnabled(enabled);
416
			showInEditorButton.setEnabled(enabled);
417
			if (enabled) {
418
				String fileContent = clipboard.getContents(TextTransfer.getInstance()).toString();
419
				text.setText(fileContent);
420
				setResource(createClipboardFile(fileContent));
421
			}
422
		}
242
423
424
		private void createShowInEditorButton(Composite parent) {
425
			showInEditorButton = new Button(parent, SWT.PUSH);
426
			showInEditorButton.setText(CompareMessages.CompareWithOther_showInEditorButton);
427
			showInEditorButton.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false));
428
			showInEditorButton.setEnabled(false);
429
			showInEditorButton.addSelectionListener(new SelectionListener() {
243
				public void widgetDefaultSelected(SelectionEvent e) {
430
				public void widgetDefaultSelected(SelectionEvent e) {
244
					widgetSelected(e);
431
					widgetSelected(e);
245
				}
432
				}
246
247
				public void widgetSelected(SelectionEvent e) {
433
				public void widgetSelected(SelectionEvent e) {
248
					setResource(fileText.getText());
434
					IWorkbenchPage page = getWorkbenchPage();
249
					updateErrorInfo();
435
					String id = getEditorId(page);
436
					try {
437
						FileEditorInput input = new FileEditorInput((IFile)getResource());
438
						closeEditorWithClipboard(page);
439
						page.openEditor(input, id);
440
					} catch (PartInitException e1) {
441
						CompareUIPlugin.log(e1);
442
					}
250
				}
443
				}
444
			});
445
		}
251
446
447
		private void createRefreshButton(Composite parent) {
448
			mainButton = new Button(parent, SWT.PUSH);
449
			mainButton.setText(CompareMessages.CompareWithOther_refreshButton);
450
			mainButton.setEnabled(false);
451
			mainButton.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false));
452
			mainButton.addSelectionListener(new SelectionListener() {
453
				public void widgetDefaultSelected(SelectionEvent e) {
454
					widgetSelected(e);
455
				}
456
				public void widgetSelected(SelectionEvent e) {
457
					String fileContent = clipboard.getContents(TextTransfer.getInstance()).toString();
458
					text.setText(fileContent);
459
					setResource(createClipboardFile(fileContent));
460
				}
252
			});
461
			});
253
		}
462
		}
463
	}
254
464
255
		protected void createFileLabel() {
465
256
			final Label fileLabel = new Label(group, SWT.NONE);
466
	private abstract class InternalSection {
257
			fileLabel.setText(CompareMessages.CompareWithOther_fileLabel);
467
		
468
		// there is no "enum" support in Java 1.4. Sigh...
469
		public static final int WORKSPACE = 0;
470
		public static final int EXTERNAL_FILE = 1;
471
		public static final int EXTERNAL_FOLDER = 2;
472
473
		protected Group group;
474
		private IResource resource;
475
		
476
		ExternalFileContent externalFileContent;
477
		ExternalFolderContent externalFolderContent;
478
		WorkspaceContent workspaceContent;
479
		ClipboardContent clipboardContent;
480
		
481
		public InternalSection(Composite parent) {
482
			createContents(parent);
483
		}
484
485
		private InternalSection() {
486
			// not to instantiate
487
		}
488
489
		protected void createContents(Composite parent) {
490
491
			group = new Group(parent, SWT.NONE);
492
			group.setLayout(new GridLayout(3, false));
493
			group.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
494
			
495
			workspaceContent = new WorkspaceContent(group, this);
496
			externalFileContent = new ExternalFileContent(group, this);
497
			externalFolderContent = new ExternalFolderContent(group, this);
498
			clipboardContent = new ClipboardContent(group, this);
499
			
500
			addListenersToRadioButtons();
501
		}
502
		
503
		private void addListenersToRadioButtons() {
504
			final ContentTypeElement[] elements = new ContentTypeElement[] { workspaceContent, 
505
					externalFileContent, externalFolderContent, clipboardContent };
506
			for (int i = 0; i < elements.length; i++)
507
				elements[i].getRadioButton().addListener(SWT.Selection, new Listener() {
508
					public void handleEvent(Event event) {
509
						for (int j = 0; j < elements.length; j++)
510
							if (event.widget != elements[j].getRadioButton())
511
								elements[j].setEnabled(false);
512
							else {
513
								elements[j].setEnabled(true);
514
								setResource(elements[j].getResource());
515
							}
516
					}
517
				});
518
		}
519
520
		protected IResource getResource() {
521
			return resource;
522
		}
523
524
		protected void setResource(IResource resource) {
525
			this.resource = resource;
526
			updateErrorInfo();
527
		}
528
529
		protected void setResource(String s) {
530
			IResource tmp = ResourcesPlugin.getWorkspace().getRoot()
531
					.findMember(s);
532
			if (tmp instanceof IWorkspaceRoot) 
533
				resource = null;
534
			else 
535
				resource = tmp;
536
			updateErrorInfo();
537
		}
538
539
		protected void clearResource() {
540
			resource = null;
541
			workspaceContent.clearResource();
542
			externalFileContent.clearResource();
543
			externalFolderContent.clearResource();
544
			updateErrorInfo();
545
		}
546
		
547
		protected void setContentType(int type) {
548
			switch(type) {
549
			case WORKSPACE: 
550
				workspaceContent.setEnabled(true);
551
				externalFileContent.setEnabled(false);
552
				externalFolderContent.setEnabled(false);
553
				break;
554
			case EXTERNAL_FILE:
555
				workspaceContent.setEnabled(false);
556
				externalFileContent.setEnabled(true);
557
				externalFolderContent.setEnabled(false);
558
				break;
559
			case EXTERNAL_FOLDER:
560
				workspaceContent.setEnabled(false);
561
				externalFileContent.setEnabled(false);
562
				externalFolderContent.setEnabled(true);
563
			}
258
		}
564
		}
259
	}
565
	}
260
566
Lines 282-317 Link Here
282
			createContents(parent);
588
			createContents(parent);
283
		}
589
		}
284
590
285
		public void createContents(Composite parent) {
591
		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;
592
			final Composite p = parent;
296
			expandable = new ExpandableComposite(parent, SWT.NONE,
593
			expandable = new ExpandableComposite(parent, SWT.NONE,
297
					ExpandableComposite.TREE_NODE | ExpandableComposite.TWISTIE);
594
					ExpandableComposite.TREE_NODE | ExpandableComposite.TWISTIE);
298
			super.createGroup(expandable);
595
			super.createContents(expandable);
596
			createClearButton(group);
299
			expandable.setClient(group);
597
			expandable.setClient(group);
300
			expandable.addExpansionListener(new ExpansionAdapter() {
598
			expandable.addExpansionListener(new ExpansionAdapter() {
301
				public void expansionStateChanged(ExpansionEvent e) {
599
				public void expansionStateChanged(ExpansionEvent e) {
302
					p.layout();
600
					p.layout();
601
					getShell().pack();
303
				}
602
				}
304
			});
603
			});
305
		}
604
		}
306
605
307
		protected void createClearButton(Composite parent) {
606
		private void createClearButton(Composite parent) {
308
			clearButton = createButton(parent, CLEAR_RETURN_CODE,
607
			clearButton = new Button(parent, SWT.PUSH);
309
					CompareMessages.CompareWithOther_clear, false);
608
			clearButton.setText(CompareMessages.CompareWithOther_clear);
310
			clearButton.addSelectionListener(new SelectionListener() {
609
			clearButton.addSelectionListener(new SelectionListener() {
311
				public void widgetDefaultSelected(SelectionEvent e) {
610
				public void widgetDefaultSelected(SelectionEvent e) {
312
					widgetSelected(e);
611
					widgetSelected(e);
313
				}
612
				}
314
315
				public void widgetSelected(SelectionEvent e) {
613
				public void widgetSelected(SelectionEvent e) {
316
					clearResource();
614
					clearResource();
317
				}
615
				}
Lines 332-337 Link Here
332
	private InternalGroup rightPanel, leftPanel;
630
	private InternalGroup rightPanel, leftPanel;
333
	private InternalExpandable ancestorPanel;
631
	private InternalExpandable ancestorPanel;
334
	private ISelection fselection;
632
	private ISelection fselection;
633
	private int externalFilesCounter = 0;
634
	int clipboardCounter = 0;
335
635
336
	/**
636
	/**
337
	 * Creates the dialog.
637
	 * Creates the dialog.
Lines 403-447 Link Here
403
		IResource[] selectedResources = Utilities.getResources(selection);
703
		IResource[] selectedResources = Utilities.getResources(selection);
404
		switch (selectedResources.length) {
704
		switch (selectedResources.length) {
405
		case 1:
705
		case 1:
406
			leftPanel.setResource(selectedResources[0]);
706
			leftPanel.workspaceContent.setResource(selectedResources[0]);
407
			break;
707
			break;
408
		case 2:
708
		case 2:
409
			leftPanel.setResource(selectedResources[0]);
709
			leftPanel.workspaceContent.setResource(selectedResources[0]);
410
			rightPanel.setResource(selectedResources[1]);
710
			rightPanel.workspaceContent.setResource(selectedResources[1]);
411
			break;
711
			break;
412
		case 3:
712
		case 3:
413
			ancestorPanel.setResource(selectedResources[0]);
713
			ancestorPanel.workspaceContent.setResource(selectedResources[0]);
414
			ancestorPanel.expandable.setExpanded(true);
714
			ancestorPanel.expandable.setExpanded(true);
415
			leftPanel.setResource(selectedResources[1]);
715
			leftPanel.workspaceContent.setResource(selectedResources[1]);
416
			rightPanel.setResource(selectedResources[2]);
716
			rightPanel.workspaceContent.setResource(selectedResources[2]);
417
			break;
717
			break;
418
		}
718
		}
719
		setInitialContentTypes();
720
	}
721
722
	private void setInitialContentTypes() {
723
		ancestorPanel.setContentType(InternalSection.WORKSPACE);
724
		leftPanel.setContentType(InternalSection.WORKSPACE);
725
		rightPanel.setContentType(InternalSection.WORKSPACE);
419
	}
726
	}
420
727
421
	private boolean isComparePossible() {
728
	private boolean isComparePossible() {
422
		IResource[] resources;
729
		IResource[] resources;
423
		if (ancestorPanel.getResource() == null) {
730
		if (ancestorPanel.getResource() == null)
424
			resources = new IResource[] { leftPanel.getResource(),
731
			resources = new IResource[] { leftPanel.getResource(),
425
					rightPanel.getResource() };
732
					rightPanel.getResource() };
426
		} else {
733
		else
427
			resources = new IResource[] { ancestorPanel.getResource(),
734
			resources = new IResource[] { ancestorPanel.getResource(),
428
					leftPanel.getResource(), rightPanel.getResource() };
735
					leftPanel.getResource(), rightPanel.getResource() };
429
		}
430
736
431
		ResourceCompareInput r = new ResourceCompareInput(
737
		ResourceCompareInput r = new ResourceCompareInput(
432
				new CompareConfiguration());
738
				new CompareConfiguration());
433
		return r.isEnabled(new StructuredSelection(resources));
739
		return r.isEnabled(new StructuredSelection(resources));
434
	}
740
	}
435
741
436
	private void updateErrorInfo() {
742
	public void updateErrorInfo() {
437
		if (okButton != null) {
743
		if (okButton != null) {
438
			if (leftPanel.getResource() == null
744
			if (leftPanel.getResource() == null
439
					|| rightPanel.getResource() == null) {
745
					|| rightPanel.getResource() == null) {
440
				setMessage(CompareMessages.CompareWithOther_error_empty,
746
				setMessage(CompareMessages.CompareWithOther_error_empty,
441
						IMessageProvider.ERROR);
747
						IMessageProvider.ERROR);
442
				okButton.setEnabled(false);
748
				okButton.setEnabled(false);
443
			} else if (ancestorPanel.getResource() == null
749
			} else if (ancestorPanel.getResource() == null && isComparePossible()) {
444
					&& ancestorPanel.fileText.getText() != "") { //$NON-NLS-1$
445
				setMessage(CompareMessages.CompareWithOther_warning_two_way,
750
				setMessage(CompareMessages.CompareWithOther_warning_two_way,
446
						IMessageProvider.WARNING);
751
						IMessageProvider.WARNING);
447
				okButton.setEnabled(true);
752
				okButton.setEnabled(true);
Lines 456-461 Link Here
456
			}
761
			}
457
		}
762
		}
458
	}
763
	}
764
	
765
	private IFile getExtenalFile() {
766
		FileDialog dialog = new FileDialog(getShell());
767
		String path = dialog.open();
768
		if (path != null)
769
			return (IFile) getResource(new Path(path), IResource.FILE);
770
		return null;
771
	}
772
773
	private IFolder getExternalDirectory() {
774
		DirectoryDialog dialog = new DirectoryDialog(getShell());
775
		String path = dialog.open();
776
		if (path != null)
777
			return (IFolder) getResource(new Path(path), IResource.FOLDER);
778
		return null;
779
	}
780
781
	private IResource getResource(IPath path, int type) {
782
		IResource r = null;
783
		IWorkspace workspace = ResourcesPlugin.getWorkspace();
784
		IWorkspaceRoot root = workspace.getRoot();
785
		IProject project = root.getProject(TMP_PROJECT_NAME);
786
		String resourceName = path.lastSegment();
787
		try {
788
			project = createTmpProject();
789
			if (!project.isOpen())
790
				project.open(null);
791
			IFolder folderForExternal = project.getFolder(EXTERNAL_FILES_FOLDER_NAME);
792
			if (!folderForExternal.exists())
793
				folderForExternal.create(IResource.NONE, true, null);
794
			if (type == IResource.FILE) {
795
				r = folderForExternal.getFile(resourceName);
796
				if (r.exists()) { 	// add a number to file's name when there already is a file with that name in a folder
797
					String extension = path.getFileExtension();
798
					String newName = path.removeFileExtension().lastSegment(); 
799
					newName += "-" + externalFilesCounter + "." + extension; //$NON-NLS-1$ //$NON-NLS-2$
800
					r = folderForExternal.getFile(newName);
801
				}
802
				((IFile)r).createLink(path, IResource.REPLACE, null);
803
				externalFilesCounter++;
804
			}
805
			else {
806
				r = folderForExternal.getFolder(resourceName);
807
				if (r.exists()) {
808
					String newName = resourceName + "-" + externalFilesCounter; //$NON-NLS-1$
809
					r = folderForExternal.getFolder(newName);
810
				}
811
				((IFolder)r).createLink(path, IResource.REPLACE, null);
812
				externalFilesCounter++;
813
			}
814
		} catch (CoreException e) {
815
			CompareUIPlugin.log(e);
816
			MessageDialog.openError(getShell(),
817
							CompareMessages.CompareWithOther_externalFile_errorTitle,
818
							CompareMessages.CompareWithOther_externalFile_errorMessage);
819
		}
820
		return r;
821
	}
822
823
	/*
824
	 * (non-javadoc)
825
	 *
826
	 * Implementation based on org.eclipse.jdt.internal.core.ExternalFoldersManager#createExternalFoldersProject
827
	 */
828
	private IProject createTmpProject() throws CoreException {
829
		IProject tmpProject = getTmpProject();
830
		if (!tmpProject.isAccessible()) {
831
			try {
832
				if (!tmpProject.exists()) {
833
					IProjectDescription desc = tmpProject.getWorkspace()
834
							.newProjectDescription(tmpProject.getName());
835
					IPath location = CompareUI.getPlugin()
836
							.getStateLocation();
837
					desc.setLocation(location.append(TMP_PROJECT_NAME));
838
					tmpProject.create(desc, null);
839
				}
840
				try {
841
					tmpProject.open(null);
842
				} catch (CoreException e1) { // in case .project file or folder has been deleted
843
					IPath location1 = CompareUI.getPlugin()
844
							.getStateLocation();
845
					IPath projectPath1 = location1.append(TMP_PROJECT_NAME);
846
					projectPath1.toFile().mkdirs();
847
					FileOutputStream output = new FileOutputStream(
848
							projectPath1.append(".project").toOSString()); //$NON-NLS-1$
849
					try {
850
						output.write(TMP_PROJECT_FILE.getBytes());
851
					} finally {
852
						output.close();
853
					}
854
					tmpProject.open(null);
855
				}
856
			} catch (IOException ioe) {
857
				return tmpProject;
858
			} catch (CoreException ce) {
859
				throw new CoreException(ce.getStatus());
860
			}
861
		}
862
		return tmpProject;
863
	}
864
865
	/*
866
	 * (non-javadoc)
867
	 *
868
	 * Following method is analogical to org.eclipse.jdt.internal
869
	 */
870
	private IProject getTmpProject() {
871
		return ResourcesPlugin.getWorkspace().getRoot().getProject(
872
				TMP_PROJECT_NAME);
873
	}
874
	
875
	private String getEditorId(IWorkbenchPage page) {
876
		IFile file = createClipboardFile("test content"); //$NON-NLS-1$ // create empty clipboard file to get it's default editor
877
		IEditorDescriptor descriptor = CompareUI.getPlugin().getWorkbench().getEditorRegistry().getDefaultEditor(file.getName());
878
		if (descriptor != null)
879
			return descriptor.getId();
880
		return null;
881
	}
882
883
	private void closeEditorWithClipboard(IWorkbenchPage page) {
884
		IEditorReference[] editors = page.getEditorReferences();
885
		for (int j = 0; j < clipboardCounter; j++)
886
			for (int i = 0; i < editors.length; i++) {
887
				String name = CompareMessages.CompareWithOther_fileName + "-" + j + ".txt"; //$NON-NLS-1$ //$NON-NLS-2$
888
				try {
889
					if (editors[i].getEditorInput().getName().equals(name))
890
						page.closeEditor(editors[i].getEditor(false), false);
891
				} catch (PartInitException e) {
892
					CompareUIPlugin.log(e);
893
				}
894
			}
895
	}
896
897
	private IFile createClipboardFile(String fileContent) {
898
		IFile file = null;
899
		try {
900
			file = getClipboardFileHandler();
901
			InputStream source = new ByteArrayInputStream(fileContent.getBytes());
902
			file.create(source, IResource.NONE, null);
903
		} catch (CoreException e) {
904
			CompareUIPlugin.log(e);
905
			MessageDialog.openError(getShell(),
906
					CompareMessages.CompareWithOther_createTmpFile_title,
907
					CompareMessages.CompareWithOther_createTmpFile_message);
908
			return file;
909
		}
910
		return file;
911
	}
912
913
	private IFile getClipboardFileHandler() throws CoreException {
914
		IFile file = null;
915
		IProject project = createTmpProject();
916
		if (!project.isOpen())
917
			project.open(null);
918
		IFolder folder = project.getFolder("ClipboardFolder"); //$NON-NLS-1$
919
		if (!folder.exists())
920
			folder.create(IResource.NONE, true, null);
921
		file = folder.getFile(CompareMessages.CompareWithOther_fileName + "-" + clipboardCounter + ".txt"); //$NON-NLS-1$ //$NON-NLS-2$
922
		clipboardCounter++;
923
		return file;
924
	}
925
459
926
460
	/**
927
	/**
461
	 * Returns table with selected resources. If any resource wasn't chosen in
928
	 * Returns table with selected resources. If any resource wasn't chosen in
Lines 477-480 Link Here
477
					rightResource };
944
					rightResource };
478
		return resources;
945
		return resources;
479
	}
946
	}
947
	
948
	/**
949
	 * Removes temporary files created from clipboard's content or imported to 
950
	 * workspace only for comparing.
951
	 */
952
	public static void cleanup() {
953
		try {
954
			IProject project = ResourcesPlugin.getWorkspace().getRoot()
955
					.getProject(TMP_PROJECT_NAME);
956
			project.delete(true, true, null);
957
		} catch (CoreException e) {
958
			CompareUIPlugin.log(e);
959
		}
960
	}
961
	
962
	private IWorkbenchPage getWorkbenchPage() {		
963
		return CompareUI.getPlugin().getWorkbench().getActiveWorkbenchWindow().getActivePage();
964
	}
965
966
	public void okPressed() {
967
		IWorkbenchPage page = getWorkbenchPage();
968
		closeEditorWithClipboard(page);
969
		super.okPressed();
970
	}
480
}
971
}
(-)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 241088