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.Combo; |
40 |
import org.eclipse.swt.widgets.Composite; |
41 |
import org.eclipse.swt.widgets.Control; |
42 |
import org.eclipse.swt.widgets.Group; |
43 |
import org.eclipse.swt.widgets.Label; |
44 |
import org.eclipse.swt.widgets.Shell; |
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 |
* <p> |
53 |
* This class can be used as is or can be subclassed. |
54 |
* |
55 |
* @since 3.4 |
56 |
*/ |
57 |
public class CompareWithOtherResourceDialog extends TitleAreaDialog { |
58 |
|
59 |
private int CLEAR_RETURN_CODE = 150; // any number != 0 |
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.fileCombo.setText(""); //$NON-NLS-1$ |
71 |
} |
72 |
|
73 |
public void dragSetData(DragSourceEvent event) { |
74 |
if (TextTransfer.getInstance().isSupportedType(event.dataType)) |
75 |
event.data = section.fileCombo.getText(); |
76 |
} |
77 |
|
78 |
public void dragStart(DragSourceEvent event) { |
79 |
if (section.fileCombo.getText() == null) |
80 |
event.doit = false; |
81 |
} |
82 |
} |
83 |
|
84 |
private class FileTextDropListener implements DropTargetListener { |
85 |
|
86 |
private InternalSection section; |
87 |
private ResourceTransfer resourceTransfer; |
88 |
private TextTransfer textTransfer; |
89 |
|
90 |
public FileTextDropListener(InternalSection section) { |
91 |
this.section = section; |
92 |
resourceTransfer = ResourceTransfer.getInstance(); |
93 |
textTransfer = TextTransfer.getInstance(); |
94 |
} |
95 |
|
96 |
public void dragEnter(DropTargetEvent event) { |
97 |
|
98 |
if (event.detail == DND.DROP_DEFAULT) { |
99 |
if ((event.operations & DND.DROP_COPY) != 0) |
100 |
event.detail = DND.DROP_COPY; |
101 |
else |
102 |
event.detail = DND.DROP_NONE; |
103 |
} |
104 |
|
105 |
for (int i = 0; i < event.dataTypes.length; i++) { |
106 |
if (resourceTransfer.isSupportedType(event.dataTypes[i])) { |
107 |
event.currentDataType = event.dataTypes[i]; |
108 |
if (event.detail != DND.DROP_COPY) |
109 |
event.detail = DND.DROP_NONE; |
110 |
break; |
111 |
} |
112 |
} |
113 |
} |
114 |
|
115 |
public void dragLeave(DropTargetEvent event) { |
116 |
// intentionally empty |
117 |
} |
118 |
|
119 |
public void dragOperationChanged(DropTargetEvent event) { |
120 |
|
121 |
if (event.detail == DND.DROP_DEFAULT) { |
122 |
if ((event.operations & DND.DROP_COPY) != 0) |
123 |
event.detail = DND.DROP_COPY; |
124 |
else |
125 |
event.detail = DND.DROP_NONE; |
126 |
} else if (resourceTransfer.isSupportedType(event.currentDataType)) { |
127 |
if (event.detail != DND.DROP_COPY) |
128 |
event.detail = DND.DROP_NONE; |
129 |
} |
130 |
} |
131 |
|
132 |
public void dragOver(DropTargetEvent event) { |
133 |
// intentionally empty |
134 |
} |
135 |
|
136 |
public void drop(DropTargetEvent event) { |
137 |
|
138 |
if (textTransfer.isSupportedType(event.currentDataType)) { |
139 |
String txt = (String) event.data; |
140 |
section.setResource(ResourcesPlugin.getWorkspace().getRoot() |
141 |
.findMember(txt)); |
142 |
} else if (resourceTransfer.isSupportedType(event.currentDataType)) { |
143 |
IResource[] files = (IResource[]) event.data; |
144 |
section.setResource(files[0]); |
145 |
} |
146 |
|
147 |
updateErrorInfo(); |
148 |
} |
149 |
|
150 |
public void dropAccept(DropTargetEvent event) { |
151 |
// intentionally empty |
152 |
} |
153 |
|
154 |
} |
155 |
|
156 |
private abstract class InternalSection { |
157 |
|
158 |
protected Group group; |
159 |
private Combo fileCombo; |
160 |
private IResource resource; |
161 |
private Button clearButton; |
162 |
|
163 |
public InternalSection(Composite parent) { |
164 |
createContents(parent); |
165 |
} |
166 |
|
167 |
public InternalSection() { |
168 |
// not to instantiate |
169 |
} |
170 |
|
171 |
public void createContents(Composite parent) { |
172 |
createGroup(parent); |
173 |
createFileLabel(); |
174 |
createFileCombo(); |
175 |
createClearButton(group); |
176 |
initDrag(); |
177 |
initDrop(); |
178 |
} |
179 |
|
180 |
public IResource getResource() { |
181 |
return resource; |
182 |
} |
183 |
|
184 |
public void setResource(IResource resource) { |
185 |
this.resource = resource; |
186 |
String txt = resource.getFullPath().toString(); |
187 |
fileCombo.add(txt); |
188 |
fileCombo.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 |
fileCombo.setText(""); //$NON-NLS-1$ |
204 |
updateErrorInfo(); |
205 |
} |
206 |
|
207 |
protected void initDrag() { |
208 |
DragSource source = new DragSource(fileCombo, DND.DROP_MOVE |
209 |
| DND.DROP_COPY); |
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(fileCombo, 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, true)); |
229 |
} |
230 |
|
231 |
protected void createFileCombo() { |
232 |
fileCombo = new Combo(group, SWT.NONE); |
233 |
fileCombo.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, |
234 |
false)); |
235 |
fileCombo.setItems(new String[] { "" }); //$NON-NLS-1$ |
236 |
fileCombo.setText(fileCombo.getItem(0)); |
237 |
|
238 |
fileCombo.addModifyListener(new ModifyListener() { |
239 |
public void modifyText(ModifyEvent e) { |
240 |
setResource(fileCombo.getText()); |
241 |
updateErrorInfo(); |
242 |
} |
243 |
}); |
244 |
|
245 |
fileCombo.addSelectionListener(new SelectionListener() { |
246 |
|
247 |
public void widgetDefaultSelected(SelectionEvent e) { |
248 |
widgetSelected(e); |
249 |
} |
250 |
|
251 |
public void widgetSelected(SelectionEvent e) { |
252 |
setResource(fileCombo.getText()); |
253 |
updateErrorInfo(); |
254 |
} |
255 |
|
256 |
}); |
257 |
} |
258 |
|
259 |
protected void createFileLabel() { |
260 |
final Label fileLabel = new Label(group, SWT.NONE); |
261 |
fileLabel.setText(CompareMessages.CompareWithOther_fileLabel); |
262 |
} |
263 |
|
264 |
protected void createClearButton(Composite parent) { |
265 |
clearButton = createButton(parent, CLEAR_RETURN_CODE, |
266 |
CompareMessages.CompareWithOther_clear, false); |
267 |
clearButton.addSelectionListener(new SelectionListener() { |
268 |
public void widgetDefaultSelected(SelectionEvent e) { |
269 |
widgetSelected(e); |
270 |
} |
271 |
|
272 |
public void widgetSelected(SelectionEvent e) { |
273 |
clearResource(); |
274 |
} |
275 |
}); |
276 |
} |
277 |
} |
278 |
|
279 |
private class InternalGroup extends InternalSection { |
280 |
|
281 |
public InternalGroup(Composite parent) { |
282 |
createContents(parent); |
283 |
} |
284 |
|
285 |
public void setText(String text) { |
286 |
group.setText(text); |
287 |
} |
288 |
|
289 |
public void setLayoutData(GridData layoutData) { |
290 |
group.setLayoutData(layoutData); |
291 |
} |
292 |
} |
293 |
|
294 |
private class InternalExpandable extends InternalSection { |
295 |
|
296 |
private ExpandableComposite expandable; |
297 |
|
298 |
public InternalExpandable(Composite parent) { |
299 |
createContents(parent); |
300 |
} |
301 |
|
302 |
public void createGroup(Composite parent) { |
303 |
final Composite p = parent; |
304 |
expandable = new ExpandableComposite(parent, SWT.NONE, |
305 |
ExpandableComposite.TREE_NODE | ExpandableComposite.TWISTIE); |
306 |
super.createGroup(expandable); |
307 |
expandable.setClient(group); |
308 |
expandable.addExpansionListener(new ExpansionAdapter() { |
309 |
public void expansionStateChanged(ExpansionEvent e) { |
310 |
p.getShell().pack(); |
311 |
} |
312 |
}); |
313 |
} |
314 |
|
315 |
public void setText(String text) { |
316 |
expandable.setText(text); |
317 |
group.setText(text); |
318 |
} |
319 |
|
320 |
public void setLayoutData(GridData layoutData) { |
321 |
expandable.setLayoutData(layoutData); |
322 |
} |
323 |
} |
324 |
|
325 |
private Button okButton, clearAllButton; |
326 |
private InternalGroup rightPanel, leftPanel; |
327 |
private InternalExpandable ancestorPanel; |
328 |
private ISelection fselection; |
329 |
|
330 |
/** |
331 |
* Creates the dialog. |
332 |
* |
333 |
* @param shell |
334 |
* a shell |
335 |
* @param selection |
336 |
* if the selection is not null, it will be set as initial files |
337 |
* for comparison |
338 |
* @since 3.4 |
339 |
*/ |
340 |
protected CompareWithOtherResourceDialog(Shell shell, ISelection selection) { |
341 |
super(shell); |
342 |
setShellStyle(SWT.MODELESS | SWT.RESIZE | SWT.MAX); |
343 |
fselection = selection; |
344 |
} |
345 |
|
346 |
/* |
347 |
* (non-Javadoc) |
348 |
* |
349 |
* @see |
350 |
* org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets |
351 |
* .Composite) |
352 |
*/ |
353 |
protected Control createDialogArea(Composite parent) { |
354 |
|
355 |
Composite mainPanel = new Composite(parent, SWT.NULL); |
356 |
mainPanel.setLayout(new GridLayout(2, true)); |
357 |
mainPanel.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); |
358 |
|
359 |
ancestorPanel = new InternalExpandable(mainPanel); |
360 |
ancestorPanel.setText(CompareMessages.CompareWithOther_ancestor); |
361 |
GridData ancestorGD = new GridData(SWT.FILL, SWT.FILL, true, true); |
362 |
ancestorGD.horizontalSpan = 2; |
363 |
ancestorPanel.setLayoutData(ancestorGD); |
364 |
|
365 |
rightPanel = new InternalGroup(mainPanel); |
366 |
rightPanel.setText(CompareMessages.CompareWithOther_rightPanel); |
367 |
rightPanel.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); |
368 |
|
369 |
leftPanel = new InternalGroup(mainPanel); |
370 |
leftPanel.setText(CompareMessages.CompareWithOther_leftPanel); |
371 |
leftPanel.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); |
372 |
|
373 |
setSelection(fselection); |
374 |
getShell().setText(CompareMessages.CompareWithOther_dialogTitle); |
375 |
setTitle(CompareMessages.CompareWithOther_dialogMessage); |
376 |
|
377 |
return mainPanel; |
378 |
} |
379 |
|
380 |
/* |
381 |
* (non-Javadoc) |
382 |
* |
383 |
* @see |
384 |
* org.eclipse.jface.dialogs.Dialog#createButtonsForButtonBar(org.eclipse |
385 |
* .swt.widgets.Composite) |
386 |
*/ |
387 |
protected void createButtonsForButtonBar(Composite parent) { |
388 |
|
389 |
super.createButtonsForButtonBar(parent); |
390 |
okButton = getButton(IDialogConstants.OK_ID); |
391 |
|
392 |
clearAllButton = createButton(parent, CLEAR_RETURN_CODE, |
393 |
CompareMessages.CompareWithOther_clearAll, false); |
394 |
clearAllButton.addSelectionListener(new SelectionListener() { |
395 |
public void widgetDefaultSelected(SelectionEvent e) { |
396 |
widgetSelected(e); |
397 |
} |
398 |
|
399 |
public void widgetSelected(SelectionEvent e) { |
400 |
leftPanel.clearResource(); |
401 |
rightPanel.clearResource(); |
402 |
ancestorPanel.clearResource(); |
403 |
} |
404 |
}); |
405 |
|
406 |
updateErrorInfo(); |
407 |
} |
408 |
|
409 |
private void setSelection(ISelection selection) { |
410 |
IResource[] selectedResources = Utilities.getResources(selection); |
411 |
switch (selectedResources.length) { |
412 |
case 0: |
413 |
break; |
414 |
case 1: |
415 |
leftPanel.setResource(selectedResources[0]); |
416 |
break; |
417 |
case 2: |
418 |
leftPanel.setResource(selectedResources[0]); |
419 |
rightPanel.setResource(selectedResources[1]); |
420 |
break; |
421 |
case 3: |
422 |
ancestorPanel.setResource(selectedResources[0]); |
423 |
ancestorPanel.expandable.setExpanded(true); |
424 |
leftPanel.setResource(selectedResources[1]); |
425 |
rightPanel.setResource(selectedResources[2]); |
426 |
break; |
427 |
} |
428 |
} |
429 |
|
430 |
private boolean comparePossible() { |
431 |
IResource[] resources; |
432 |
if (ancestorPanel.getResource() == null) |
433 |
resources = new IResource[] { leftPanel.getResource(), |
434 |
rightPanel.getResource() }; |
435 |
else |
436 |
resources = new IResource[] { ancestorPanel.getResource(), |
437 |
leftPanel.getResource(), rightPanel.getResource() }; |
438 |
|
439 |
ResourceCompareInput r = new ResourceCompareInput( |
440 |
new CompareConfiguration()); |
441 |
return r.isEnabled(new StructuredSelection(resources)); |
442 |
} |
443 |
|
444 |
private void updateErrorInfo() { |
445 |
if (okButton != null) { |
446 |
if (leftPanel.getResource() == null |
447 |
|| rightPanel.getResource() == null) { |
448 |
setMessage(CompareMessages.CompareWithOther_error_empty, |
449 |
IMessageProvider.ERROR); |
450 |
okButton.setEnabled(false); |
451 |
} else if (!comparePossible()) { |
452 |
setMessage( |
453 |
CompareMessages.CompareWithOther_error_not_comparable, |
454 |
IMessageProvider.ERROR); |
455 |
okButton.setEnabled(false); |
456 |
} else { |
457 |
setMessage(null); |
458 |
okButton.setEnabled(true); |
459 |
} |
460 |
} |
461 |
} |
462 |
|
463 |
/** |
464 |
* Returns table with selected resources. If any resource wasn't chosen in |
465 |
* the ancestor panel, table has only two elements -- resources chosen in |
466 |
* left and right panel. In the other case table contains all three |
467 |
* resources. |
468 |
* |
469 |
* @return table with selected resources |
470 |
*/ |
471 |
public IResource[] getResult() { |
472 |
IResource[] resources; |
473 |
IResource rightResource = rightPanel.getResource(); |
474 |
IResource leftResource = leftPanel.getResource(); |
475 |
IResource ancestorResource = ancestorPanel.getResource(); |
476 |
if (ancestorResource == null) |
477 |
resources = new IResource[] { leftResource, rightResource }; |
478 |
else |
479 |
resources = new IResource[] { ancestorResource, leftResource, |
480 |
rightResource }; |
481 |
return resources; |
482 |
} |
483 |
} |