Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: fbe8f5d9589ac06fcb207d8fdd7215930f4d46ec (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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/*******************************************************************************
 * Copyright (c) 2015 Wind River Systems, Inc. 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:
 * Wind River Systems - initial API and implementation
 *******************************************************************************/
package org.eclipse.tcf.te.tcf.launch.cdt.preferences;

import java.util.regex.Pattern;

import org.eclipse.jface.preference.FieldEditorPreferencePage;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.preference.PreferenceStore;
import org.eclipse.jface.preference.StringFieldEditor;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.tcf.te.runtime.preferences.ScopedEclipsePreferences;
import org.eclipse.tcf.te.tcf.launch.cdt.activator.Activator;
import org.eclipse.tcf.te.tcf.launch.cdt.nls.Messages;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;

/**
 * GDB launcher preference page implementation.
 */
public class GdbPreferencePage extends FieldEditorPreferencePage implements IWorkbenchPreferencePage {
	// References to the field editors
	private StringFieldEditor portList;
	private StringFieldEditor mappedToPortList;
	private StringFieldEditor portListAttach;
	private StringFieldEditor mappedToPortListAttach;


	// The preference store used internally for the field editors
	private IPreferenceStore store;

	/**
	 * Port list field editor implementation.
	 */
	private abstract class PortListFieldEditor extends StringFieldEditor {
		private Pattern valid = Pattern.compile("[1-9][0-9,]*"); //$NON-NLS-1$

		/**
		 * Constructor.
		 *
		 * @param name The name of the preference this field editor works on.
		 * @param labelText The label text.
		 * @param parent the parent of the field editor's control
		 */
		public PortListFieldEditor(String name, String labelText, Composite parent) {
			super(name, labelText, parent);
			this.setEmptyStringAllowed(true);
			this.setErrorMessage(NLS.bind(Messages.GdbPreferencePage_portList_error, getErrorLabel()));
		}

		/**
		 * Returns the label of the port list for the error message.
		 */
		protected abstract String getErrorLabel();

		/* (non-Javadoc)
		 * @see org.eclipse.jface.preference.StringFieldEditor#doCheckState()
		 */
		@Override
		protected boolean doCheckState() {
			if ("".equals(getStringValue())) { //$NON-NLS-1$
				return isEmptyStringAllowed();
			} else if (valid.matcher(getStringValue()).matches()) {
				return true;
			}
			return false;
		}
	}

	/**
	 * Constructor
	 */
	public GdbPreferencePage() {
    	super(FieldEditorPreferencePage.GRID);

    	// The internal preference store never needs saving
    	store = new PreferenceStore() {
    		@Override
    		public boolean needsSaving() {
    		    return false;
    		}
    	};
	}


	/* (non-Javadoc)
	 * @see org.eclipse.ui.IWorkbenchPreferencePage#init(org.eclipse.ui.IWorkbench)
	 */
	@Override
	public void init(IWorkbench workbench) {
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.preference.FieldEditorPreferencePage#createFieldEditors()
	 */
	@Override
	protected void createFieldEditors() {
		Composite parent = getFieldEditorParent();
		((GridLayout)parent.getLayout()).makeColumnsEqualWidth = false;

		Composite panel = new Composite(parent, SWT.NONE);
		panel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
		GridLayout layout = new GridLayout();
		layout.marginHeight = 0; layout.marginWidth = 0;
		panel.setLayout(layout);
		panel.setFont(parent.getFont());

		Label label = new Label(panel, SWT.HORIZONTAL);
		label.setText(Messages.GdbPreferencePage_label);
		GridData layoutData = new GridData(SWT.FILL, SWT.CENTER, true, false);
		label.setLayoutData(layoutData);

		Group appLaunchGroup = new Group(panel, SWT.NONE);
		appLaunchGroup.setText(Messages.GdbPreferencePage_appLaunchGroup_label);
		appLaunchGroup.setLayout(new GridLayout());
		appLaunchGroup.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));

		Composite panel2 = new Composite(appLaunchGroup, SWT.NONE);
		panel2.setLayout(new GridLayout());
		panel2.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));

		portList = new PortListFieldEditor("portList", Messages.GdbPreferencePage_portList_label, panel2) { //$NON-NLS-1$
			@Override
			protected String getErrorLabel() {
			    return Messages.GdbPreferencePage_portList_error_portList;
			}
		};
		addField(portList);

		mappedToPortList = new PortListFieldEditor("mappedToPortList", Messages.GdbPreferencePage_mappedToPortList_label, panel2) { //$NON-NLS-1$
			@Override
			protected String getErrorLabel() {
			    return Messages.GdbPreferencePage_portList_error_mappedToPortList;
			}
		};
		addField(mappedToPortList);

		Group attachLaunchGroup = new Group(panel, SWT.NONE);
		attachLaunchGroup.setText(Messages.GdbPreferencePage_attachLaunchGroup_label);
		attachLaunchGroup.setLayout(new GridLayout());
		attachLaunchGroup.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));

		Composite panel3 = new Composite(attachLaunchGroup, SWT.NONE);
		panel3.setLayout(new GridLayout());
		panel3.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));

		portListAttach = new PortListFieldEditor("portListAttach", Messages.GdbPreferencePage_portList_label, panel3) { //$NON-NLS-1$
			@Override
			protected String getErrorLabel() {
			    return Messages.GdbPreferencePage_portList_error_portList;
			}
		};
		addField(portListAttach);

		mappedToPortListAttach = new PortListFieldEditor("mappedToPortListAttach", Messages.GdbPreferencePage_mappedToPortList_label, panel3) { //$NON-NLS-1$
			@Override
			protected String getErrorLabel() {
			    return Messages.GdbPreferencePage_portList_error_mappedToPortList;
			}
		};
		addField(mappedToPortListAttach);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.preference.PreferencePage#getPreferenceStore()
	 */
	@Override
	public IPreferenceStore getPreferenceStore() {
	    return store;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.preference.FieldEditorPreferencePage#initialize()
	 */
	@Override
	protected void initialize() {
        ScopedEclipsePreferences prefs = Activator.getScopedPreferences();

        String port = prefs.getString(IPreferenceKeys.PREF_GDBSERVER_PORT);
        String portAlternatives = prefs.getString(IPreferenceKeys.PREF_GDBSERVER_PORT_ALTERNATIVES);

        String portList = ""; //$NON-NLS-1$
        if (port != null && !"".equals(port)) portList += port.trim(); //$NON-NLS-1$
        if (portAlternatives != null && !"".equals(portAlternatives)) portList += "," + portAlternatives.trim(); //$NON-NLS-1$ //$NON-NLS-2$

		store.setDefault("portList", portList); //$NON-NLS-1$
		store.setValue("portList", portList); //$NON-NLS-1$

        String mappedTo = prefs.getString(IPreferenceKeys.PREF_GDBSERVER_PORT_MAPPED_TO);
        String mappedToAlternatives = prefs.getString(IPreferenceKeys.PREF_GDBSERVER_PORT_MAPPED_TO_ALTERNATIVES);

        String mappedToList = ""; //$NON-NLS-1$
        if (mappedTo != null && !"".equals(mappedTo)) mappedToList += mappedTo.trim(); //$NON-NLS-1$
        if (mappedToAlternatives != null && !"".equals(mappedToAlternatives)) mappedToList += "," + mappedToAlternatives.trim(); //$NON-NLS-1$ //$NON-NLS-2$

		store.setDefault("mappedToPortList", mappedToList); //$NON-NLS-1$
		store.setValue("mappedToPortList", mappedToList); //$NON-NLS-1$

        port = prefs.getString(IPreferenceKeys.PREF_GDBSERVER_PORT_ATTACH);
        portAlternatives = prefs.getString(IPreferenceKeys.PREF_GDBSERVER_PORT_ATTACH_ALTERNATIVES);

        portList = ""; //$NON-NLS-1$
        if (port != null && !"".equals(port)) portList += port.trim(); //$NON-NLS-1$
        if (portAlternatives != null && !"".equals(portAlternatives)) portList += "," + portAlternatives.trim(); //$NON-NLS-1$ //$NON-NLS-2$

		store.setDefault("portListAttach", portList); //$NON-NLS-1$
		store.setValue("portListAttach", portList); //$NON-NLS-1$

        mappedTo = prefs.getString(IPreferenceKeys.PREF_GDBSERVER_PORT_ATTACH_MAPPED_TO);
        mappedToAlternatives = prefs.getString(IPreferenceKeys.PREF_GDBSERVER_PORT_ATTACH_MAPPED_TO_ALTERNATIVES);

        mappedToList = ""; //$NON-NLS-1$
        if (mappedTo != null && !"".equals(mappedTo)) mappedToList += mappedTo.trim(); //$NON-NLS-1$
        if (mappedToAlternatives != null && !"".equals(mappedToAlternatives)) mappedToList += "," + mappedToAlternatives.trim(); //$NON-NLS-1$ //$NON-NLS-2$

		store.setDefault("mappedToPortListAttach", mappedToList); //$NON-NLS-1$
		store.setValue("mappedToPortListAttach", mappedToList); //$NON-NLS-1$

		// Load values into field editors
	    super.initialize();
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.preference.FieldEditorPreferencePage#performOk()
	 */
	@Override
	public boolean performOk() {
	    boolean success = super.performOk();

	    if (success) {
	        ScopedEclipsePreferences prefs = Activator.getScopedPreferences();

	        String portList = store.getString("portList"); //$NON-NLS-1$

	        if ("".equals(portList.trim())) { //$NON-NLS-1$
	        	prefs.putString(IPreferenceKeys.PREF_GDBSERVER_PORT, null);
	        	prefs.putString(IPreferenceKeys.PREF_GDBSERVER_PORT_ALTERNATIVES, null);
	        } else {
	        	int idx = portList.indexOf(',');
	        	if (idx == -1) {
		        	prefs.putString(IPreferenceKeys.PREF_GDBSERVER_PORT, portList.trim());
		        	prefs.putString(IPreferenceKeys.PREF_GDBSERVER_PORT_ALTERNATIVES, null);
	        	} else {
		        	prefs.putString(IPreferenceKeys.PREF_GDBSERVER_PORT, portList.substring(0, idx).trim());
		        	prefs.putString(IPreferenceKeys.PREF_GDBSERVER_PORT_ALTERNATIVES, portList.length() > (idx + 1) ? portList.substring(idx + 1) : null);
	        	}
	        }

	        portList = store.getString("mappedToPortList"); //$NON-NLS-1$

	        if ("".equals(portList.trim())) { //$NON-NLS-1$
	        	prefs.putString(IPreferenceKeys.PREF_GDBSERVER_PORT_MAPPED_TO, null);
	        	prefs.putString(IPreferenceKeys.PREF_GDBSERVER_PORT_MAPPED_TO_ALTERNATIVES, null);
	        } else {
	        	int idx = portList.indexOf(',');
	        	if (idx == -1) {
		        	prefs.putString(IPreferenceKeys.PREF_GDBSERVER_PORT_MAPPED_TO, portList.trim());
		        	prefs.putString(IPreferenceKeys.PREF_GDBSERVER_PORT_MAPPED_TO_ALTERNATIVES, null);
	        	} else {
		        	prefs.putString(IPreferenceKeys.PREF_GDBSERVER_PORT_MAPPED_TO, portList.substring(0, idx).trim());
		        	prefs.putString(IPreferenceKeys.PREF_GDBSERVER_PORT_MAPPED_TO_ALTERNATIVES, portList.length() > (idx + 1) ? portList.substring(idx + 1) : null);
	        	}
	        }

	        portList = store.getString("portListAttach"); //$NON-NLS-1$

	        if ("".equals(portList.trim())) { //$NON-NLS-1$
	        	prefs.putString(IPreferenceKeys.PREF_GDBSERVER_PORT_ATTACH, null);
	        	prefs.putString(IPreferenceKeys.PREF_GDBSERVER_PORT_ATTACH_ALTERNATIVES, null);
	        } else {
	        	int idx = portList.indexOf(',');
	        	if (idx == -1) {
		        	prefs.putString(IPreferenceKeys.PREF_GDBSERVER_PORT_ATTACH, portList.trim());
		        	prefs.putString(IPreferenceKeys.PREF_GDBSERVER_PORT_ATTACH_ALTERNATIVES, null);
	        	} else {
		        	prefs.putString(IPreferenceKeys.PREF_GDBSERVER_PORT_ATTACH, portList.substring(0, idx).trim());
		        	prefs.putString(IPreferenceKeys.PREF_GDBSERVER_PORT_ATTACH_ALTERNATIVES, portList.length() > (idx + 1) ? portList.substring(idx + 1) : null);
	        	}
	        }

	        portList = store.getString("mappedToPortListAttach"); //$NON-NLS-1$

	        if ("".equals(portList.trim())) { //$NON-NLS-1$
	        	prefs.putString(IPreferenceKeys.PREF_GDBSERVER_PORT_ATTACH_MAPPED_TO, null);
	        	prefs.putString(IPreferenceKeys.PREF_GDBSERVER_PORT_ATTACH_MAPPED_TO_ALTERNATIVES, null);
	        } else {
	        	int idx = portList.indexOf(',');
	        	if (idx == -1) {
		        	prefs.putString(IPreferenceKeys.PREF_GDBSERVER_PORT_ATTACH_MAPPED_TO, portList.trim());
		        	prefs.putString(IPreferenceKeys.PREF_GDBSERVER_PORT_ATTACH_MAPPED_TO_ALTERNATIVES, null);
	        	} else {
		        	prefs.putString(IPreferenceKeys.PREF_GDBSERVER_PORT_ATTACH_MAPPED_TO, portList.substring(0, idx).trim());
		        	prefs.putString(IPreferenceKeys.PREF_GDBSERVER_PORT_ATTACH_MAPPED_TO_ALTERNATIVES, portList.length() > (idx + 1) ? portList.substring(idx + 1) : null);
	        	}
	        }
	    }

	    return success;
	}

}

Back to the top