Added
Link Here
|
1 |
/******************************************************************************* |
2 |
* Copyright (c) 2007, 2008 IBM Corporation 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 |
* Renato Silva - initial API and implementation |
10 |
* (http://bugs.eclipse.org/bugs/show_bug.cgi?id=261716) |
11 |
* |
12 |
*******************************************************************************/ |
13 |
|
14 |
package org.eclipse.ui.texteditor; |
15 |
|
16 |
import org.eclipse.swt.SWT; |
17 |
import org.eclipse.swt.events.SelectionEvent; |
18 |
import org.eclipse.swt.events.SelectionListener; |
19 |
import org.eclipse.swt.layout.GridData; |
20 |
import org.eclipse.swt.layout.GridLayout; |
21 |
import org.eclipse.swt.widgets.Button; |
22 |
import org.eclipse.swt.widgets.Composite; |
23 |
import org.eclipse.swt.widgets.Control; |
24 |
import org.eclipse.swt.widgets.Shell; |
25 |
|
26 |
import org.eclipse.jface.dialogs.MessageDialog; |
27 |
|
28 |
import org.eclipse.ui.internal.texteditor.NLSUtility; |
29 |
|
30 |
/** |
31 |
* Presents a dialog asking the user to choose an action to be performed |
32 |
* when an out-of-sync file saving occurs. Each option is related to |
33 |
* a out-of-sync handler extension. |
34 |
*/ |
35 |
public class OutOfSyncHandlerSelectionDialog extends MessageDialog { |
36 |
|
37 |
private OutOfSyncHandler[] handlers; |
38 |
private OutOfSyncHandler defaultHandler; |
39 |
private OutOfSyncHandler selectedHandler; |
40 |
|
41 |
/** |
42 |
* Initializes the dialog. |
43 |
* |
44 |
* @param parentShell the parent shell |
45 |
* @param handlers the available out-of-sync handlers |
46 |
* @param defaultHandler an additional, non-registered default handler |
47 |
* @param fileName The underlying file name |
48 |
*/ |
49 |
public OutOfSyncHandlerSelectionDialog(Shell parentShell, OutOfSyncHandler[] handlers, OutOfSyncHandler defaultHandler, String fileName) { |
50 |
|
51 |
super(parentShell, EditorMessages.Editor_error_save_outofsync_title, |
52 |
null, NLSUtility.format(EditorMessages.Editor_error_save_outofsync_message, fileName), WARNING, |
53 |
new String[] { EditorMessages.OutOfSyncHandlerSelectionDialog_ok, |
54 |
EditorMessages.OutOfSyncHandlerSelectionDialog_cancel }, 0); |
55 |
this.handlers = handlers; |
56 |
this.defaultHandler = defaultHandler; |
57 |
} |
58 |
|
59 |
private void updateSelection(OutOfSyncHandler selected) { |
60 |
this.selectedHandler = selected; |
61 |
} |
62 |
|
63 |
private void createOption(Composite buttonArea, final OutOfSyncHandler handler) { |
64 |
Button radioButton = new Button(buttonArea, SWT.RADIO); |
65 |
radioButton.setText(handler.getAnswer()); |
66 |
radioButton.addSelectionListener(new SelectionListener(){ |
67 |
public void widgetSelected(SelectionEvent e) { |
68 |
updateSelection(handler); |
69 |
} |
70 |
public void widgetDefaultSelected(SelectionEvent e) { |
71 |
updateSelection(handler); |
72 |
} |
73 |
}); |
74 |
} |
75 |
|
76 |
protected Control createCustomArea(Composite parent) { |
77 |
Composite buttonArea = new Composite(parent, SWT.NULL); |
78 |
buttonArea.setLayout(new GridLayout(1, true)); |
79 |
GridData data = new GridData(SWT.FILL, SWT.FILL, true, false); |
80 |
data.horizontalIndent = 38; |
81 |
buttonArea.setLayoutData(data); |
82 |
for (int i = 0; i < handlers.length; i++) |
83 |
if (handlers[i] != null) |
84 |
createOption(buttonArea, handlers[i]); |
85 |
createOption(buttonArea, defaultHandler); |
86 |
return parent; |
87 |
} |
88 |
|
89 |
/** |
90 |
* Gets the selected handler. Should normally be called after an <code>open()</code>. |
91 |
* @return the selected handler |
92 |
*/ |
93 |
public OutOfSyncHandler getSelected() { |
94 |
return selectedHandler; |
95 |
} |
96 |
|
97 |
} |