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

Collapse All | Expand All

(-)compare/org/eclipse/compare/internal/CompareMessages.java (-1 / +13 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2007 IBM Corporation and others.
2
 * Copyright (c) 2000, 2008 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 8-13 Link Here
8
 * Contributors:
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
9
 *     IBM Corporation - initial API and implementation
10
 *     Matt McCutchen (hashproduct+eclipse@gmail.com) - Bug 35390 Three-way compare cannot select (mis-selects) )ancestor resource
10
 *     Matt McCutchen (hashproduct+eclipse@gmail.com) - Bug 35390 Three-way compare cannot select (mis-selects) )ancestor resource
11
 *     Aleksandra Wozniak (aleksandra.k.wozniak@gmail.com) - Bug 239959
11
 *******************************************************************************/
12
 *******************************************************************************/
12
package org.eclipse.compare.internal;
13
package org.eclipse.compare.internal;
13
14
Lines 108-113 Link Here
108
	public static String SelectAncestorDialog_title;
109
	public static String SelectAncestorDialog_title;
109
	public static String SelectAncestorDialog_message;
110
	public static String SelectAncestorDialog_message;
110
	public static String SelectAncestorDialog_option;
111
	public static String SelectAncestorDialog_option;
112
	public static String CompareWithOther_fileLabel;
113
	public static String CompareWithOther_ancestor;
114
	public static String CompareWithOther_rightPanel;
115
	public static String CompareWithOther_leftPanel;
116
	public static String CompareWithOther_dialogTitle;
117
	public static String CompareWithOther_dialogMessage;
118
	public static String CompareWithOther_error_not_comparable;
119
	public static String CompareWithOther_error_empty;
120
	public static String CompareWithOther_clear;
121
	public static String CompareWithOther_warning_two_way;
122
	public static String CompareWithOther_info;
111
123
112
	static {
124
	static {
113
		NLS.initializeMessages(BUNDLE_NAME, CompareMessages.class);
125
		NLS.initializeMessages(BUNDLE_NAME, CompareMessages.class);
(-)compare/org/eclipse/compare/internal/CompareMessages.properties (+13 lines)
Lines 8-13 Link Here
8
# Contributors:
8
# Contributors:
9
#     IBM Corporation - initial API and implementation
9
#     IBM Corporation - initial API and implementation
10
#     Matt McCutchen (hashproduct+eclipse@gmail.com) - Bug 35390 Three-way compare cannot select (mis-selects) )ancestor resource
10
#     Matt McCutchen (hashproduct+eclipse@gmail.com) - Bug 35390 Three-way compare cannot select (mis-selects) )ancestor resource
11
#     Aleksandra Wozniak (aleksandra.k.wozniak@gmail.com) - Bug 239959
11
###############################################################################
12
###############################################################################
12
13
13
ComparePlugin_internal_error= Internal Error
14
ComparePlugin_internal_error= Internal Error
Lines 119-121 Link Here
119
SelectAncestorDialog_message=Which resource would you like to use as the common ancestor in the three-way compare?
120
SelectAncestorDialog_message=Which resource would you like to use as the common ancestor in the three-way compare?
120
SelectAncestorDialog_option=''{0}''
121
SelectAncestorDialog_option=''{0}''
121
ShowWhitespaceAction_0=Show &Whitespace Characters
122
ShowWhitespaceAction_0=Show &Whitespace Characters
123
124
CompareWithOther_fileLabel=File:
125
CompareWithOther_ancestor=Ancestor
126
CompareWithOther_rightPanel=Right
127
CompareWithOther_leftPanel=Left
128
CompareWithOther_dialogTitle=Compare with Other Resource
129
CompareWithOther_dialogMessage=Select resources to compare
130
CompareWithOther_error_not_comparable=Selected resources are not comparable.
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.
133
CompareWithOther_clear=Clear
134
CompareWithOther_info=Drag files from a view or between dialog's fields.
(-)compare/org/eclipse/compare/internal/CompareAction.java (-9 / +9 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2007 IBM Corporation and others.
2
 * Copyright (c) 2000, 2008 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 8-24 Link Here
8
 * Contributors:
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
9
 *     IBM Corporation - initial API and implementation
10
 *     Matt McCutchen (hashproduct+eclipse@gmail.com) - Bug 35390 Three-way compare cannot select (mis-selects) )ancestor resource
10
 *     Matt McCutchen (hashproduct+eclipse@gmail.com) - Bug 35390 Three-way compare cannot select (mis-selects) )ancestor resource
11
 *     Aleksandra Wozniak (aleksandra.k.wozniak@gmail.com) - Bug 239959
11
 *******************************************************************************/
12
 *******************************************************************************/
12
package org.eclipse.compare.internal;
13
package org.eclipse.compare.internal;
13
14
15
import org.eclipse.compare.CompareConfiguration;
16
import org.eclipse.compare.CompareUI;
14
import org.eclipse.jface.action.IAction;
17
import org.eclipse.jface.action.IAction;
15
import org.eclipse.jface.viewers.ISelection;
18
import org.eclipse.jface.viewers.ISelection;
16
17
import org.eclipse.ui.IObjectActionDelegate;
19
import org.eclipse.ui.IObjectActionDelegate;
18
import org.eclipse.ui.IWorkbenchPage;
20
import org.eclipse.ui.IWorkbenchPage;
19
import org.eclipse.ui.IWorkbenchPart;
21
import org.eclipse.ui.IWorkbenchPart;
20
import org.eclipse.compare.CompareConfiguration;
21
import org.eclipse.compare.CompareUI;
22
22
23
23
24
/*
24
/*
Lines 26-41 Link Here
26
 */
26
 */
27
public class CompareAction extends BaseCompareAction implements IObjectActionDelegate {
27
public class CompareAction extends BaseCompareAction implements IObjectActionDelegate {
28
28
29
	private ResourceCompareInput fInput;
29
	protected ResourceCompareInput fInput;
30
	private IWorkbenchPage fWorkbenchPage;
30
	protected IWorkbenchPage fWorkbenchPage;
31
31
	protected boolean showSelectAncestorDialog = true;
32
32
33
	public void run(ISelection selection) {
33
	public void run(ISelection selection) {
34
		if (fInput != null) {
34
		if (fInput != null) {
35
			// Pass the shell so setSelection can prompt the user for which
35
			// Pass the shell so setSelection can prompt the user for which
36
			// resource should be the ancestor
36
			// resource should be the ancestor
37
			boolean ok = fInput.setSelection(selection,
37
			boolean ok = fInput.setSelection(selection, fWorkbenchPage
38
				fWorkbenchPage.getWorkbenchWindow().getShell());
38
					.getWorkbenchWindow().getShell(), showSelectAncestorDialog);
39
			if (!ok) return;
39
			if (!ok) return;
40
			fInput.initializeCompareConfiguration();
40
			fInput.initializeCompareConfiguration();
41
			CompareUI.openCompareEditorOnPage(fInput, fWorkbenchPage);
41
			CompareUI.openCompareEditorOnPage(fInput, fWorkbenchPage);
(-)compare/org/eclipse/compare/internal/ResourceCompareInput.java (-6 / +42 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2007 IBM Corporation and others.
2
 * Copyright (c) 2000, 2008 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 8-13 Link Here
8
 * Contributors:
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
9
 *     IBM Corporation - initial API and implementation
10
 *     Matt McCutchen (hashproduct+eclipse@gmail.com) - Bug 35390 Three-way compare cannot select (mis-selects) )ancestor resource
10
 *     Matt McCutchen (hashproduct+eclipse@gmail.com) - Bug 35390 Three-way compare cannot select (mis-selects) )ancestor resource
11
 *     Aleksandra Wozniak (aleksandra.k.wozniak@gmail.com) - Bug 239959
11
 *******************************************************************************/
12
 *******************************************************************************/
12
package org.eclipse.compare.internal;
13
package org.eclipse.compare.internal;
13
14
Lines 196-207 Link Here
196
			}
197
			}
197
		};
198
		};
198
	}
199
	}
199
200
	// If the compare is three-way, this method asks the user which resource
200
	// If the compare is three-way, this method asks the user which resource
201
	// to use as the ancestor.  Returns false if the user cancels the prompt,
201
	// to use as the ancestor. Depending on the value of
202
	// showSelectAncestorDialog flag it uses different dialogs to get the
203
	// feedback from the user. Returns false if the user cancels the prompt,
202
	// true otherwise.
204
	// true otherwise.
203
	boolean setSelection(ISelection s, Shell shell) {
205
	boolean setSelection(ISelection s, Shell shell, boolean showSelectAncestorDialog) {
204
		
206
207
		if (!showSelectAncestorDialog)
208
			return showCompareWithOtherResourceDialog(shell, s);
209
205
		IResource[] selection= Utilities.getResources(s);
210
		IResource[] selection= Utilities.getResources(s);
206
211
207
		fThreeWay= selection.length == 3;
212
		fThreeWay= selection.length == 3;
Lines 223-234 Link Here
223
			fLeftResource= selection[0];
228
			fLeftResource= selection[0];
224
			fRightResource= selection[1];
229
			fRightResource= selection[1];
225
		}
230
		}
231
		fLeft= getStructure(fLeftResource);
232
		fRight= getStructure(fRightResource);
233
		return true;
234
	}
235
236
	private boolean showCompareWithOtherResourceDialog(Shell shell, ISelection s) {
237
		CompareWithOtherResourceDialog dialog = new CompareWithOtherResourceDialog(shell, s);
238
		if (dialog.open() != IDialogConstants.OK_ID)
239
			return false;
240
		IResource[] selection = dialog.getResult();
241
		if (!checkSelection(selection))
242
			return false;
226
243
244
		fThreeWay = selection.length == 3;
245
		if (fThreeWay) {
246
			fAncestorResource = selection[0];
247
			fAncestor = getStructure(fAncestorResource);
248
			fLeftResource = selection[1];
249
			fRightResource = selection[2];
250
		} else {
251
			fAncestorResource = null;
252
			fAncestor = null;
253
			fLeftResource = selection[0];
254
			fRightResource = selection[1];
255
		}
227
		fLeft= getStructure(fLeftResource);
256
		fLeft= getStructure(fLeftResource);
228
		fRight= getStructure(fRightResource);
257
		fRight= getStructure(fRightResource);
229
		return true;
258
		return true;
230
	}
259
	}
231
	
260
261
	private boolean checkSelection(IResource[] resources) {
262
		for (int i = 0; i < resources.length; i++)
263
			if (resources[i] == null)
264
				return false;
265
		return true;
266
	}
267
232
	/*
268
	/*
233
	 * Returns true if compare can be executed for the given selection.
269
	 * Returns true if compare can be executed for the given selection.
234
	 */
270
	 */
(-)plugin.xml (+8 lines)
Lines 201-206 Link Here
201
               enablesFor="2+"
201
               enablesFor="2+"
202
               id="compareWithEachOther">
202
               id="compareWithEachOther">
203
         </action>
203
         </action>
204
         <action
205
               class="org.eclipse.compare.internal.CompareWithOtherResourceAction"
206
               enablesFor="+"
207
               id="compareWithOtherResource"
208
               label="%CompareWithOtherResource.label"
209
               menubarPath="compareWithMenu/compareWithGroup"
210
               tooltip="%CompareWithOtherResourceAction.tooltip">
211
         </action>
204
      </objectContribution>
212
      </objectContribution>
205
      <objectContribution
213
      <objectContribution
206
            objectClass="org.eclipse.core.resources.IFile"
214
            objectClass="org.eclipse.core.resources.IFile"
(-)plugin.properties (+3 lines)
Lines 83-88 Link Here
83
CompareWithEachOtherAction.label= &Each Other
83
CompareWithEachOtherAction.label= &Each Other
84
CompareWithEachOtherAction.tooltip= Compare the Selected Resources
84
CompareWithEachOtherAction.tooltip= Compare the Selected Resources
85
85
86
CompareWithOtherResource.label= &Other Resource...
87
CompareWithOtherResource.tooltip= Open the 'Compare With' Dialog
88
86
CompareWithHistoryAction.label= &Local History...
89
CompareWithHistoryAction.label= &Local History...
87
CompareWithHistoryAction.tooltip= Compare the Selected Resource with Local History
90
CompareWithHistoryAction.tooltip= Compare the Selected Resource with Local History
88
91
(-)compare/org/eclipse/compare/internal/CompareWithOtherResourceAction.java (+35 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2008 Aleksandra Wozniak and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *    Aleksandra Wozniak (aleksandra.k.wozniak@gmail.com) - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.compare.internal;
12
13
import org.eclipse.jface.viewers.ISelection;
14
15
/**
16
 * The "Compare with other resource" action
17
 * 
18
 * @since 3.4
19
 */
20
public class CompareWithOtherResourceAction extends CompareAction {
21
22
	public void run(ISelection selection) {
23
		// Show CompareWithOtherResourceDialog which return resources to compare
24
		// and ancestor if specified. Don't need to display the other dialog
25
		showSelectAncestorDialog = false;
26
		super.run(selection);
27
	}
28
29
	protected boolean isEnabled(ISelection selection) {
30
		// ignore returned value
31
		super.isEnabled(selection);
32
		return true;
33
	}
34
35
}
(-)compare/org/eclipse/compare/internal/CompareWithOtherResourceDialog.java (+481 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2008 Aleksandra Wozniak and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *    Aleksandra Wozniak (aleksandra.k.wozniak@gmail.com) - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.compare.internal;
12
13
import org.eclipse.compare.CompareConfiguration;
14
import org.eclipse.core.resources.IResource;
15
import org.eclipse.core.resources.IWorkspaceRoot;
16
import org.eclipse.core.resources.ResourcesPlugin;
17
import org.eclipse.jface.dialogs.IDialogConstants;
18
import org.eclipse.jface.dialogs.IMessageProvider;
19
import org.eclipse.jface.dialogs.TitleAreaDialog;
20
import org.eclipse.jface.viewers.ISelection;
21
import org.eclipse.jface.viewers.StructuredSelection;
22
import org.eclipse.swt.SWT;
23
import org.eclipse.swt.dnd.DND;
24
import org.eclipse.swt.dnd.DragSource;
25
import org.eclipse.swt.dnd.DragSourceEvent;
26
import org.eclipse.swt.dnd.DragSourceListener;
27
import org.eclipse.swt.dnd.DropTarget;
28
import org.eclipse.swt.dnd.DropTargetEvent;
29
import org.eclipse.swt.dnd.DropTargetListener;
30
import org.eclipse.swt.dnd.TextTransfer;
31
import org.eclipse.swt.dnd.Transfer;
32
import org.eclipse.swt.events.ModifyEvent;
33
import org.eclipse.swt.events.ModifyListener;
34
import org.eclipse.swt.events.SelectionEvent;
35
import org.eclipse.swt.events.SelectionListener;
36
import org.eclipse.swt.layout.GridData;
37
import org.eclipse.swt.layout.GridLayout;
38
import org.eclipse.swt.widgets.Button;
39
import org.eclipse.swt.widgets.Composite;
40
import org.eclipse.swt.widgets.Control;
41
import org.eclipse.swt.widgets.Group;
42
import org.eclipse.swt.widgets.Label;
43
import org.eclipse.swt.widgets.Shell;
44
import org.eclipse.swt.widgets.Text;
45
import org.eclipse.ui.forms.events.ExpansionAdapter;
46
import org.eclipse.ui.forms.events.ExpansionEvent;
47
import org.eclipse.ui.forms.widgets.ExpandableComposite;
48
import org.eclipse.ui.part.ResourceTransfer;
49
50
/**
51
 * This is a dialog that can invoke the compare editor on chosen files.
52
 * 
53
 * @since 3.4
54
 */
55
public class CompareWithOtherResourceDialog extends TitleAreaDialog {
56
57
	private int CLEAR_RETURN_CODE = 150; // any number != 0
58
	private int MIN_WIDTH = 300;
59
	private int MIN_HEIGHT = 175;
60
61
	private class FileTextDragListener implements DragSourceListener {
62
63
		private InternalSection section;
64
65
		public FileTextDragListener(InternalSection section) {
66
			this.section = section;
67
		}
68
69
		public void dragFinished(DragSourceEvent event) {
70
			section.fileText.setText(""); //$NON-NLS-1$
71
		}
72
73
		public void dragSetData(DragSourceEvent event) {
74
			if (TextTransfer.getInstance().isSupportedType(event.dataType)) {
75
				event.data = section.fileText.getText();
76
			} else
77
				event.data = section.fileText.getText();
78
		}
79
80
		public void dragStart(DragSourceEvent event) {
81
			if (section.fileText.getText() == null)
82
				event.doit = false;
83
		}
84
	}
85
86
	private class FileTextDropListener implements DropTargetListener {
87
88
		private InternalSection section;
89
		private ResourceTransfer resourceTransfer;
90
		private TextTransfer textTransfer;
91
92
		public FileTextDropListener(InternalSection section) {
93
			this.section = section;
94
			resourceTransfer = ResourceTransfer.getInstance();
95
			textTransfer = TextTransfer.getInstance();
96
		}
97
98
		public void dragEnter(DropTargetEvent event) {
99
100
			if (event.detail == DND.DROP_DEFAULT) {
101
				if ((event.operations & DND.DROP_COPY) != 0)
102
					event.detail = DND.DROP_COPY;
103
				else
104
					event.detail = DND.DROP_NONE;
105
			}
106
107
			for (int i = 0; i < event.dataTypes.length; i++) {
108
				if (resourceTransfer.isSupportedType(event.dataTypes[i])
109
						|| textTransfer.isSupportedType(event.dataTypes[i])) {
110
					event.currentDataType = event.dataTypes[i];
111
					if (event.detail != DND.DROP_COPY)
112
						event.detail = DND.DROP_NONE;
113
					break;
114
				}
115
			}
116
		}
117
118
		public void dragLeave(DropTargetEvent event) {
119
			// intentionally empty
120
		}
121
122
		public void dragOperationChanged(DropTargetEvent event) {
123
124
			if (event.detail == DND.DROP_DEFAULT) {
125
				if ((event.operations & DND.DROP_COPY) != 0)
126
					event.detail = DND.DROP_COPY;
127
				else
128
					event.detail = DND.DROP_NONE;
129
			} else if (resourceTransfer.isSupportedType(event.currentDataType)) {
130
				if (event.detail != DND.DROP_COPY)
131
					event.detail = DND.DROP_NONE;
132
			}
133
		}
134
135
		public void dragOver(DropTargetEvent event) {
136
			// intentionally empty
137
		}
138
139
		public void drop(DropTargetEvent event) {
140
141
			if (textTransfer.isSupportedType(event.currentDataType)) {
142
				String txt = (String) event.data;
143
				section.setResource(ResourcesPlugin.getWorkspace().getRoot()
144
						.findMember(txt));
145
			} else if (resourceTransfer.isSupportedType(event.currentDataType)) {
146
				IResource[] files = (IResource[]) event.data;
147
				section.setResource(files[0]);
148
			}
149
150
			updateErrorInfo();
151
		}
152
153
		public void dropAccept(DropTargetEvent event) {
154
			// intentionally empty
155
		}
156
157
	}
158
159
	private abstract class InternalSection {
160
161
		protected Group group;
162
		protected Text fileText;
163
		private IResource resource;
164
165
		public InternalSection(Composite parent) {
166
			createContents(parent);
167
		}
168
169
		private InternalSection() {
170
			// not to instantiate
171
		}
172
173
		public void createContents(Composite parent) {
174
			createGroup(parent);
175
			createFileLabel();
176
			createFileCombo();
177
			initDrag();
178
			initDrop();
179
		}
180
181
		public IResource getResource() {
182
			return resource;
183
		}
184
185
		public void setResource(IResource resource) {
186
			this.resource = resource;
187
			String txt = resource.getFullPath().toString();
188
			fileText.setText(txt);
189
		}
190
191
		public void setResource(String s) {
192
			IResource tmp = ResourcesPlugin.getWorkspace().getRoot()
193
					.findMember(s);
194
			if (tmp instanceof IWorkspaceRoot)
195
				resource = null;
196
			else
197
				resource = tmp;
198
199
		}
200
201
		protected void clearResource() {
202
			resource = null;
203
			fileText.setText(""); //$NON-NLS-1$
204
			updateErrorInfo();
205
		}
206
207
		protected void initDrag() {
208
			DragSource source = new DragSource(fileText, DND.DROP_MOVE
209
					| DND.DROP_COPY | DND.DROP_DEFAULT);
210
			Transfer[] types = new Transfer[] { TextTransfer.getInstance(),
211
					ResourceTransfer.getInstance() };
212
			source.setTransfer(types);
213
			source.addDragListener(new FileTextDragListener(this));
214
		}
215
216
		protected void initDrop() {
217
			DropTarget target = new DropTarget(fileText, DND.DROP_MOVE
218
					| DND.DROP_COPY | DND.DROP_DEFAULT);
219
			Transfer[] types = new Transfer[] { TextTransfer.getInstance(),
220
					ResourceTransfer.getInstance() };
221
			target.setTransfer(types);
222
			target.addDropListener(new FileTextDropListener(this));
223
		}
224
225
		protected void createGroup(Composite parent) {
226
			group = new Group(parent, SWT.NONE);
227
			group.setLayout(new GridLayout(3, false));
228
			group.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
229
		}
230
231
		protected void createFileCombo() {
232
			fileText = new Text(group, SWT.BORDER);
233
			fileText
234
					.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
235
236
			fileText.addModifyListener(new ModifyListener() {
237
				public void modifyText(ModifyEvent e) {
238
					setResource(fileText.getText());
239
					updateErrorInfo();
240
				}
241
			});
242
243
			fileText.addSelectionListener(new SelectionListener() {
244
245
				public void widgetDefaultSelected(SelectionEvent e) {
246
					widgetSelected(e);
247
				}
248
249
				public void widgetSelected(SelectionEvent e) {
250
					setResource(fileText.getText());
251
					updateErrorInfo();
252
				}
253
254
			});
255
		}
256
257
		protected void createFileLabel() {
258
			final Label fileLabel = new Label(group, SWT.NONE);
259
			fileLabel.setText(CompareMessages.CompareWithOther_fileLabel);
260
		}
261
	}
262
263
	private class InternalGroup extends InternalSection {
264
265
		public InternalGroup(Composite parent) {
266
			createContents(parent);
267
		}
268
269
		public void setText(String text) {
270
			group.setText(text);
271
		}
272
273
		public void setLayoutData(GridData layoutData) {
274
			group.setLayoutData(layoutData);
275
		}
276
	}
277
278
	private class InternalExpandable extends InternalSection {
279
280
		private ExpandableComposite expandable;
281
		private Button clearButton;
282
283
		public InternalExpandable(Composite parent) {
284
			createContents(parent);
285
		}
286
287
		public void createContents(Composite parent) {
288
			createGroup(parent);
289
			createFileLabel();
290
			createFileCombo();
291
			createClearButton(group);
292
			initDrag();
293
			initDrop();
294
		}
295
296
		public void createGroup(Composite parent) {
297
			final Composite p = parent;
298
			expandable = new ExpandableComposite(parent, SWT.NONE,
299
					ExpandableComposite.TREE_NODE | ExpandableComposite.TWISTIE);
300
			super.createGroup(expandable);
301
			expandable.setClient(group);
302
			expandable.addExpansionListener(new ExpansionAdapter() {
303
				public void expansionStateChanged(ExpansionEvent e) {
304
					p.layout();
305
				}
306
			});
307
		}
308
309
		protected void createClearButton(Composite parent) {
310
			clearButton = createButton(parent, CLEAR_RETURN_CODE,
311
					CompareMessages.CompareWithOther_clear, false);
312
			clearButton.addSelectionListener(new SelectionListener() {
313
				public void widgetDefaultSelected(SelectionEvent e) {
314
					widgetSelected(e);
315
				}
316
317
				public void widgetSelected(SelectionEvent e) {
318
					clearResource();
319
				}
320
			});
321
		}
322
323
		public void setText(String text) {
324
			expandable.setText(text);
325
			group.setText(text);
326
		}
327
328
		public void setLayoutData(GridData layoutData) {
329
			expandable.setLayoutData(layoutData);
330
		}
331
	}
332
333
	private Button okButton;
334
	private InternalGroup rightPanel, leftPanel;
335
	private InternalExpandable ancestorPanel;
336
	private ISelection fselection;
337
338
	/**
339
	 * Creates the dialog.
340
	 * 
341
	 * @param shell
342
	 *            a shell
343
	 * @param selection
344
	 *            if the selection is not null, it will be set as initial files
345
	 *            for comparison
346
	 * @since 3.4
347
	 */
348
	protected CompareWithOtherResourceDialog(Shell shell, ISelection selection) {
349
		super(shell);
350
		setShellStyle(SWT.MODELESS | SWT.RESIZE | SWT.MAX);
351
		fselection = selection;
352
	}
353
354
	/*
355
	 * (non-Javadoc)
356
	 * 
357
	 * @see
358
	 * org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets
359
	 * .Composite)
360
	 */
361
	protected Control createDialogArea(Composite parent) {
362
363
		Composite mainPanel = new Composite(parent, SWT.NULL);
364
		mainPanel.setLayout(new GridLayout(2, true));
365
		mainPanel.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
366
367
		ancestorPanel = new InternalExpandable(mainPanel);
368
		ancestorPanel.setText(CompareMessages.CompareWithOther_ancestor);
369
		GridData ancestorGD = new GridData(SWT.FILL, SWT.FILL, true, false);
370
		ancestorGD.horizontalSpan = 2;
371
		ancestorPanel.setLayoutData(ancestorGD);
372
373
		leftPanel = new InternalGroup(mainPanel);
374
		leftPanel.setText(CompareMessages.CompareWithOther_leftPanel);
375
		leftPanel.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
376
377
		rightPanel = new InternalGroup(mainPanel);
378
		rightPanel.setText(CompareMessages.CompareWithOther_rightPanel);
379
		rightPanel.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
380
381
		setSelection(fselection);
382
		getShell().setText(CompareMessages.CompareWithOther_dialogTitle);
383
		setTitle(CompareMessages.CompareWithOther_dialogMessage);
384
		getShell().setMinimumSize(convertHorizontalDLUsToPixels(MIN_WIDTH),
385
				convertVerticalDLUsToPixels(MIN_HEIGHT));
386
387
		return mainPanel;
388
	}
389
390
	/*
391
	 * (non-Javadoc)
392
	 * 
393
	 * @see
394
	 * org.eclipse.jface.dialogs.Dialog#createButtonsForButtonBar(org.eclipse
395
	 * .swt.widgets.Composite)
396
	 */
397
	protected void createButtonsForButtonBar(Composite parent) {
398
		super.createButtonsForButtonBar(parent);
399
		okButton = getButton(IDialogConstants.OK_ID);
400
		updateErrorInfo();
401
		setMessage(CompareMessages.CompareWithOther_info);
402
	}
403
404
	private void setSelection(ISelection selection) {
405
		IResource[] selectedResources = Utilities.getResources(selection);
406
		switch (selectedResources.length) {
407
		case 1:
408
			leftPanel.setResource(selectedResources[0]);
409
			break;
410
		case 2:
411
			leftPanel.setResource(selectedResources[0]);
412
			rightPanel.setResource(selectedResources[1]);
413
			break;
414
		case 3:
415
			ancestorPanel.setResource(selectedResources[0]);
416
			ancestorPanel.expandable.setExpanded(true);
417
			leftPanel.setResource(selectedResources[1]);
418
			rightPanel.setResource(selectedResources[2]);
419
			break;
420
		}
421
	}
422
423
	private boolean isComparePossible() {
424
		IResource[] resources;
425
		if (ancestorPanel.getResource() == null) {
426
			resources = new IResource[] { leftPanel.getResource(),
427
					rightPanel.getResource() };
428
		} else {
429
			resources = new IResource[] { ancestorPanel.getResource(),
430
					leftPanel.getResource(), rightPanel.getResource() };
431
		}
432
433
		ResourceCompareInput r = new ResourceCompareInput(
434
				new CompareConfiguration());
435
		return r.isEnabled(new StructuredSelection(resources));
436
	}
437
438
	private void updateErrorInfo() {
439
		if (okButton != null) {
440
			if (leftPanel.getResource() == null
441
					|| rightPanel.getResource() == null) {
442
				setMessage(CompareMessages.CompareWithOther_error_empty,
443
						IMessageProvider.ERROR);
444
				okButton.setEnabled(false);
445
			} else if (ancestorPanel.getResource() == null
446
					&& ancestorPanel.fileText.getText() != "") { //$NON-NLS-1$
447
				setMessage(CompareMessages.CompareWithOther_warning_two_way,
448
						IMessageProvider.WARNING);
449
			} else if (!isComparePossible()) {
450
				setMessage(
451
						CompareMessages.CompareWithOther_error_not_comparable,
452
						IMessageProvider.ERROR);
453
				okButton.setEnabled(false);
454
			} else {
455
				setMessage(CompareMessages.CompareWithOther_info);
456
				okButton.setEnabled(true);
457
			}
458
		}
459
	}
460
461
	/**
462
	 * Returns table with selected resources. If any resource wasn't chosen in
463
	 * the ancestor panel, table has only two elements -- resources chosen in
464
	 * left and right panel. In the other case table contains all three
465
	 * resources.
466
	 * 
467
	 * @return table with selected resources
468
	 */
469
	public IResource[] getResult() {
470
		IResource[] resources;
471
		IResource rightResource = rightPanel.getResource();
472
		IResource leftResource = leftPanel.getResource();
473
		IResource ancestorResource = ancestorPanel.getResource();
474
		if (ancestorResource == null)
475
			resources = new IResource[] { leftResource, rightResource };
476
		else
477
			resources = new IResource[] { ancestorResource, leftResource,
478
					rightResource };
479
		return resources;
480
	}
481
}

Return to bug 239959