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 |
} |