Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 01a8b2f5ed70fd3d088f4981b635e045908cec39 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*******************************************************************************
 * Copyright (c) 2000, 2009 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.jdt.internal.debug.ui;

import com.ibm.icu.text.MessageFormat;

import org.eclipse.core.runtime.IStatus;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.BusyIndicator;
import org.eclipse.swt.events.TraverseEvent;
import org.eclipse.swt.events.TraverseListener;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Shell;

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.preference.IPreferenceStore;

import org.eclipse.debug.core.DebugException;
import org.eclipse.debug.core.ILaunch;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.model.IDebugTarget;
import org.eclipse.debug.ui.DebugUITools;

/**
 * An error dialog reporting a problem with a debug
 * target which gives the user the option to continue
 * or terminate/disconnect or restart the target.
 */
public class HotCodeReplaceErrorDialog extends ErrorDialogWithToggle {

	protected IDebugTarget target;
	// The IDs of the buttons. Set to the sum of the other possible IDs generated by 
	// this dialog to ensure the IDs' uniqueness.
	protected int TERMINATE_ID= IDialogConstants.OK_ID + IDialogConstants.DETAILS_ID + IDialogConstants.CANCEL_ID;
	protected int DISCONNECT_ID= TERMINATE_ID + 1;
	protected int RESTART_ID= TERMINATE_ID + 2;

	/**
	 * Creates a new dialog which can terminate, disconnect or restart the given debug target.
	 * 
	 * @param target the debug target
	 * @see ErrorDialogWithToggle#ErrorDialogWithToggle(Shell, String, String, IStatus, String, String, IPreferenceStore)
	 */
	public HotCodeReplaceErrorDialog(Shell parentShell, String dialogTitle, String message, IStatus status, String preferenceKey, String toggleMessage, IPreferenceStore store, IDebugTarget target) {
		super(parentShell, dialogTitle, message, status, preferenceKey, toggleMessage, store);
		this.target = target;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.dialogs.Dialog#createButtonsForButtonBar(org.eclipse.swt.widgets.Composite)
	 */
	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		super.createButtonsForButtonBar(parent);
		getButton(IDialogConstants.OK_ID).setText(DebugUIMessages.HotCodeReplaceErrorDialog_0); 
		boolean canTerminate= target.canTerminate();
		boolean canDisconnect= target.canDisconnect();
		if (canTerminate) {
			createButton(parent, TERMINATE_ID, DebugUIMessages.HotCodeReplaceErrorDialog_1, false); 
		} 
		if (canDisconnect) {
			createButton(parent, DISCONNECT_ID, DebugUIMessages.HotCodeReplaceErrorDialog_3, false); 
		}
		if (canTerminate && !canDisconnect) {
			createButton(parent, RESTART_ID, DebugUIMessages.HotCodeReplaceErrorDialog_7, false); 
		}
		blockMnemonicWithoutModifier(getToggleButton());
	}

	/*
	 * @see org.eclipse.jface.dialogs.Dialog#createButton(org.eclipse.swt.widgets.Composite, int, java.lang.String, boolean)
	 * @since 3.6
	 */
	@Override
	protected Button createButton(Composite parent, int id, String label, boolean defaultButton) {
		Button button= super.createButton(parent, id, label, defaultButton);
		blockMnemonicWithoutModifier(button);
		return button;
	}

	/**
	 * Ensures that simple key presses don't activate the given button.
	 *  
	 * @param button the button to tweak
	 * @since 3.6
	 */
	protected void blockMnemonicWithoutModifier(Button button) {
		button.addTraverseListener(new TraverseListener() {
			public void keyTraversed(TraverseEvent e) {
				if (e.detail == SWT.TRAVERSE_MNEMONIC && e.doit == true && e.stateMask != SWT.MOD3) {
					e.doit= false;
				}
			}
		});
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.jface.dialogs.Dialog#buttonPressed(int)
	 */
	@Override
	protected void buttonPressed(final int id) {
		if (id == TERMINATE_ID || id == DISCONNECT_ID || id == RESTART_ID) {
			final DebugException[] ex = new DebugException[1];
			final String[] operation = new String[1];
			ex[0] = null;
			Runnable r = new Runnable() {
				public void run() {
					try {
						if (id == TERMINATE_ID) {
							operation[0]= DebugUIMessages.HotCodeReplaceErrorDialog_5; 
							target.terminate();
						} else if (id == DISCONNECT_ID){
							operation[0]= DebugUIMessages.HotCodeReplaceErrorDialog_6; 
							target.disconnect();
						} else {
							operation[0]= DebugUIMessages.HotCodeReplaceErrorDialog_8; 
							ILaunch launch = target.getLaunch();
							launch.terminate();
							ILaunchConfiguration config = launch.getLaunchConfiguration();
							if (config != null  && config.exists()) {
								DebugUITools.launch(config, launch.getLaunchMode());
							}
						}
					} catch (DebugException e) {
						ex[0] = e;
					}
				}
			};
			BusyIndicator.showWhile(getShell().getDisplay(), r);
			if (ex[0] != null) {
				JDIDebugUIPlugin.statusDialog(MessageFormat.format(DebugUIMessages.HotCodeReplaceErrorDialog_2, operation), ex[0].getStatus()); 
			}
			okPressed();
		} else {
			super.buttonPressed(id);
		}
	}
}

Back to the top