Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: dde60d720fbdbfa35431fa551d81bd081b5aad08 (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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
/*******************************************************************************
 * Copyright (c) 2013 Xilinx, 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:
 *     Xilinx - initial API and implementation
 *******************************************************************************/
package org.eclipse.tcf.internal.debug.ui.launch;

import java.math.BigInteger;
import java.util.Map;

import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.tcf.internal.debug.launch.TCFLaunchDelegate;
import org.eclipse.tcf.internal.debug.ui.ImageCache;
import org.eclipse.tcf.internal.debug.ui.model.TCFModel;
import org.eclipse.tcf.protocol.JSON;

class DownloadFileDialog extends Dialog {

    private final Image image;
    private final String peer_id;
    private final Map<String,Object> map;

    private Text context_text;
    private Text file_text;
    private Text addr_text;
    private Text size_text;
    private Text offs_text;
    private Button file_button;
    private Button context_button;
    private Button load_syms_button;
    private Button relocate_button;
    private Button download_button;
    private Button set_pc_button;
    private Button osa_button;

    DownloadFileDialog(Shell parentShell, String peer_id,  Map<String,Object> map) {
        super(parentShell);
        this.image = ImageCache.getImage(ImageCache.IMG_DOWNLOAD_TAB);
        this.peer_id = peer_id;
        this.map = map;
    }

    @Override
    protected void configureShell(Shell shell) {
        super.configureShell(shell);
        shell.setText("Download File"); //$NON-NLS-1$
        shell.setImage(image);
    }

    @Override
    protected void createButtonsForButtonBar(Composite parent) {
        createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, true);
        createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false);
        updateButtons();
    }

    @Override
    protected Control createDialogArea(Composite parent) {
        Composite composite = (Composite)super.createDialogArea(parent);
        createFileFields(composite);
        setData();
        composite.setSize(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
        return composite;
    }

    private void createFileFields(Composite parent) {
        Font font = parent.getFont();
        Composite composite = new Composite(parent, SWT.NONE);
        GridLayout layout = new GridLayout(3, false);
        composite.setFont(font);
        composite.setLayout(layout);
        composite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

        Label context_label = new Label(composite, SWT.NONE);
        context_label.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING));
        context_label.setFont(font);
        context_label.setText("Context:"); //$NON-NLS-1$

        context_text = new Text(composite, SWT.SINGLE | SWT.BORDER);
        GridData gd = new GridData(GridData.FILL_HORIZONTAL);
        gd.widthHint = 400;
        context_text.setLayoutData(gd);
        context_text.setFont(font);

        context_text.addModifyListener(new ModifyListener() {
            public void modifyText(ModifyEvent e) {
                updateButtons();
            }
        });

        context_button = new Button(composite, SWT.PUSH);
        context_button.setText("Select..."); //$NON-NLS-1$
        context_button.setFont(font);
        context_button.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
        context_button.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent e) {
                String context = context_text.getText().trim();
                ContextSelection selection = new ContextSelection();
                selection.fPeerId = peer_id;
                selection.fContextFullName = context;
                ContextSelectionDialog diag = new ContextSelectionDialog(DownloadFileDialog.this, false);
                diag.setSelection(selection);
                if (diag.open() == Window.OK) {
                    selection = diag.getSelection();
                    context_text.setText(selection.fContextFullName);
                }
            }
        });

        Label file_label = new Label(composite, SWT.NONE);
        file_label.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING));
        file_label.setFont(font);
        file_label.setText("File:"); //$NON-NLS-1$

        file_text = new Text(composite, SWT.SINGLE | SWT.BORDER);
        file_text.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
        file_text.setFont(font);

        file_text.addModifyListener(new ModifyListener() {
            public void modifyText(ModifyEvent e) {
                updateButtons();
            }
        });

        file_button = new Button(composite, SWT.PUSH);
        file_button.setText("Browse..."); //$NON-NLS-1$
        file_button.setFont(font);
        file_button.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
        file_button.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent e) {
                String path = file_text.getText().trim();
                if (path.length() == 0) path = ResourcesPlugin.getWorkspace().getRoot().getLocation().toOSString();
                FileDialog dialog = new FileDialog(getShell(), SWT.NONE);
                dialog.setFilterPath(path);
                path = dialog.open();
                if (path != null) file_text.setText(path);
            }
        });

        load_syms_button = new Button(composite, SWT.CHECK);
        load_syms_button.setText("Load symbols");
        gd = new GridData(GridData.FILL_HORIZONTAL);
        gd.horizontalSpan = 3;
        load_syms_button.setLayoutData(gd);
        load_syms_button.setFont(font);
        load_syms_button.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent evt) {
                updateButtons();
            }
        });
        load_syms_button.setEnabled(true);

        relocate_button = new Button(composite, SWT.CHECK);
        relocate_button.setText("Relocate the file");
        gd = new GridData(GridData.FILL_HORIZONTAL);
        gd.horizontalSpan = 3;
        relocate_button.setLayoutData(gd);
        relocate_button.setFont(font);
        relocate_button.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent evt) {
                updateButtons();
            }
        });
        relocate_button.setEnabled(true);
        Composite rel_group = createRelocateGroup(composite);
        gd = new GridData(GridData.FILL_HORIZONTAL);
        gd.horizontalSpan = 3;
        rel_group.setLayoutData(gd);

        download_button = new Button(composite, SWT.CHECK);
        download_button.setText("Download the file into the context memory");
        gd = new GridData(GridData.FILL_HORIZONTAL);
        gd.horizontalSpan = 3;
        download_button.setLayoutData(gd);
        download_button.setFont(font);
        download_button.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent evt) {
                updateButtons();
            }
        });
        download_button.setEnabled(true);

        set_pc_button = new Button(composite, SWT.CHECK);
        set_pc_button.setText("Set PC to program entry address");
        gd = new GridData(GridData.FILL_HORIZONTAL);
        gd.horizontalSpan = 3;
        set_pc_button.setLayoutData(gd);
        set_pc_button.setFont(font);
        set_pc_button.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent evt) {
                updateButtons();
            }
        });
        set_pc_button.setEnabled(true);

        osa_button = new Button(composite, SWT.CHECK);
        osa_button.setText("Enable OS awarennes - the file is an OS kernel");
        gd = new GridData(GridData.FILL_HORIZONTAL);
        gd.horizontalSpan = 3;
        osa_button.setLayoutData(gd);
        osa_button.setFont(font);
        osa_button.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent evt) {
                updateButtons();
            }
        });
        osa_button.setEnabled(true);
    }

    private Composite createRelocateGroup(Composite parent) {
        Font font = parent.getFont();

        Group group = new Group(parent, SWT.NONE);
        GridLayout layout = new GridLayout();
        layout.verticalSpacing = 0;
        layout.numColumns = 2;
        group.setLayout(layout);
        group.setLayoutData(new GridData(GridData.FILL_BOTH));
        group.setFont(font);
        group.setText("File location in the context memory");

        Label addr_label = new Label(group, SWT.WRAP);
        addr_label.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING));
        addr_label.setFont(font);
        addr_label.setText("Address:");

        addr_text = new Text(group, SWT.SINGLE | SWT.BORDER);
        addr_text.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
        addr_text.setFont(font);

        Label size_label = new Label(group, SWT.WRAP);
        size_label.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING));
        size_label.setFont(font);
        size_label.setText("Size:");

        size_text = new Text(group, SWT.SINGLE | SWT.BORDER);
        size_text.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
        size_text.setFont(font);

        Label offset_label = new Label(group, SWT.WRAP);
        offset_label.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING));
        offset_label.setFont(font);
        offset_label.setText("File offset:");

        offs_text = new Text(group, SWT.SINGLE | SWT.BORDER);
        offs_text.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
        offs_text.setFont(font);

        return group;
    }

    private String toHex(Number n) {
        if (n == null) return null;
        BigInteger x = JSON.toBigInteger(n);
        String s = x.toString(16);
        int l = 16 - s.length();
        if (l < 0) l = 0;
        if (l > 16) l = 16;
        return "0x0000000000000000".substring(0, 2 + l) + s;
    }

    private void setString(Text text, String key) {
        String s = (String)map.get(key);
        if (s == null) s = "";
        text.setText(s);
    }

    private void setBoolean(Button btn, String key) {
        Boolean b = (Boolean)map.get(key);
        if (b == null) btn.setSelection(false);
        else btn.setSelection(b.booleanValue());
    }

    private void setNumber(Text text, String key) {
        Number n = (Number)map.get(key);
        if (n == null) text.setText("");
        else text.setText(toHex(n));
    }

    private void setData() {
        setString(context_text, TCFLaunchDelegate.FILES_CONTEXT_FULL_NAME);
        setString(file_text, TCFLaunchDelegate.FILES_FILE_NAME);
        setBoolean(load_syms_button, TCFLaunchDelegate.FILES_LOAD_SYMBOLS);
        setBoolean(relocate_button, TCFLaunchDelegate.FILES_RELOCATE);
        setBoolean(download_button, TCFLaunchDelegate.FILES_DOWNLOAD);
        setBoolean(set_pc_button, TCFLaunchDelegate.FILES_SET_PC);
        setBoolean(osa_button, TCFLaunchDelegate.FILES_ENABLE_OSA);
        setNumber(addr_text, TCFLaunchDelegate.FILES_ADDRESS);
        setNumber(offs_text, TCFLaunchDelegate.FILES_OFFSET);
        setNumber(size_text, TCFLaunchDelegate.FILES_SIZE);
        updateButtons();
    }

    private void getBoolean(Button btn, String key) {
        boolean b = btn.getSelection();
        if (!b) {
            map.remove(key);
        }
        else {
            map.put(key, Boolean.TRUE);
        }
    }

    private void getString(Text text, String key) {
        String s = text.getText().trim();
        if (s == null || s.length() == 0) {
            map.remove(key);
        }
        else {
            map.put(key, s);
        }
    }

    private void getNumber(Text text, String key) {
        String s = text.getText().trim();
        if (s == null || s.length() == 0) {
            map.remove(key);
        }
        else if (s.startsWith("0x")) {
            map.put(key, new BigInteger(s.substring(2), 16));
        }
        else {
            map.put(key, new BigInteger(s));
        }
    }

    private void getData() {
        getString(context_text, TCFLaunchDelegate.FILES_CONTEXT_FULL_NAME);
        getString(file_text, TCFLaunchDelegate.FILES_FILE_NAME);
        getBoolean(load_syms_button, TCFLaunchDelegate.FILES_LOAD_SYMBOLS);
        getBoolean(relocate_button, TCFLaunchDelegate.FILES_RELOCATE);
        getBoolean(download_button, TCFLaunchDelegate.FILES_DOWNLOAD);
        getBoolean(set_pc_button, TCFLaunchDelegate.FILES_SET_PC);
        getBoolean(osa_button, TCFLaunchDelegate.FILES_ENABLE_OSA);
        getNumber(addr_text, TCFLaunchDelegate.FILES_ADDRESS);
        getNumber(offs_text, TCFLaunchDelegate.FILES_OFFSET);
        getNumber(size_text, TCFLaunchDelegate.FILES_SIZE);
    }

    private void updateButtons() {
        Button btn = getButton(IDialogConstants.OK_ID);
        if (btn != null && context_text != null && file_text != null) {
            String context = context_text.getText().trim();
            String file = file_text.getText().trim();
            btn.setEnabled(context.length() > 0 && file.length() > 0);
        }
        if (relocate_button != null) {
            boolean reloc = relocate_button.getSelection();
            addr_text.setEnabled(reloc);
            size_text.setEnabled(reloc);
            offs_text.setEnabled(reloc);
        }
    }

    @Override
    protected void okPressed() {
        try {
            getData();
        }
        catch (Throwable x) {
            MessageBox mb = new MessageBox(getShell(), SWT.ICON_ERROR | SWT.OK);
            mb.setText("Invalid data"); //$NON-NLS-1$
            mb.setMessage(TCFModel.getErrorMessage(x, true));
            mb.open();
            return;
        }
        super.okPressed();
    }
}

Back to the top