Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 39a3149634b3957f88bfdfd2ad06c9298839919b (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
/*******************************************************************************
 * Copyright (c) 2010 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.tm.internal.tcf.debug.ui.commands;

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

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
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.tm.internal.tcf.debug.ui.model.TCFModel;
import org.eclipse.tm.tcf.protocol.JSON;
import org.eclipse.tm.tcf.services.IMemoryMap;

class MemoryMapItemDialog extends Dialog {

    private final Map<String,Object> props;
    private final boolean enable_editing;
    private final Image image;

    private Text addr_text;
    private Text size_text;
    private Text offset_text;
    private Text file_text;
    private Button rd_button;
    private Button wr_button;
    private Button ex_button;

    MemoryMapItemDialog(Shell parent, Image image, Map<String,Object> props, boolean enable_editing) {
        super(parent);
        this.image = image;
        this.props = props;
        this.enable_editing = enable_editing;
    }

    @Override
    protected void configureShell(Shell shell) {
        super.configureShell(shell);
        shell.setText("Symbol File");
        shell.setImage(image);
    }

    @Override
    protected void createButtonsForButtonBar(Composite parent) {
        createButton(parent, IDialogConstants.OK_ID, "&OK", true);
        updateButtons();
    }

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

    private void createFileNameFields(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 file_label = new Label(composite, SWT.WRAP);
        file_label.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING));
        file_label.setFont(font);
        file_label.setText("File name:");

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

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

        Button button = new Button(composite, SWT.PUSH);
        button.setFont(font);
        button.setText("...");
        button.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING));
        button.setEnabled(enable_editing);
        button.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent e) {
                FileDialog file_dialog = new FileDialog(getShell(), SWT.NONE);
                file_dialog.setFileName(file_text.getText());
                String path = file_dialog.open();
                if (path != null) file_text.setText(path);
            }
        });
    }

    private void createPropsFields(Composite parent) {
        Font font = parent.getFont();
        Composite composite = new Composite(parent, SWT.NONE);
        GridLayout layout = new GridLayout(2, false);
        composite.setFont(font);
        composite.setLayout(layout);
        composite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
        createTextFields(composite);
        createFlagsGroup(composite);
    }

    private void createTextFields(Composite parent) {
        Font font = parent.getFont();
        Composite composite = new Composite(parent, SWT.NONE);
        GridLayout layout = new GridLayout(2, false);
        composite.setFont(font);
        composite.setLayout(layout);
        GridData gd = new GridData(GridData.FILL_HORIZONTAL);
        gd.widthHint = 250;
        composite.setLayoutData(gd);

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

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

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

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

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

        offset_text = new Text(composite, SWT.SINGLE | SWT.BORDER);
        offset_text.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
        offset_text.setFont(font);
        offset_text.setEditable(enable_editing);
    }

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

        Group group = new Group(parent, SWT.NONE);
        GridLayout layout = new GridLayout();
        layout.verticalSpacing = 0;
        layout.numColumns = 1;
        group.setLayout(layout);
        group.setLayoutData(new GridData(GridData.FILL_BOTH));
        group.setFont(font);
        group.setText("Flags");

        rd_button = new Button(group, SWT.CHECK);
        rd_button.setFont(font);
        rd_button.setText("Data read");
        rd_button.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
        rd_button.setEnabled(enable_editing);

        wr_button = new Button(group, SWT.CHECK);
        wr_button.setFont(font);
        wr_button.setText("Data write");
        wr_button.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
        wr_button.setEnabled(enable_editing);

        ex_button = new Button(group, SWT.CHECK);
        ex_button.setFont(font);
        ex_button.setText("Instructions read");
        ex_button.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
        ex_button.setEnabled(enable_editing);
    }

    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 setText(Text text, String str) {
        if (str == null) str = "";
        text.setText(str);
    }

    private void setData() {
        setText(addr_text, toHex((Number)props.get(IMemoryMap.PROP_ADDRESS)));
        setText(size_text, toHex((Number)props.get(IMemoryMap.PROP_SIZE)));
        if (props.get(IMemoryMap.PROP_SECTION_NAME) != null) {
            setText(offset_text, (String)props.get(IMemoryMap.PROP_SECTION_NAME));
        }
        else {
            setText(offset_text, toHex((Number)props.get(IMemoryMap.PROP_OFFSET)));
        }
        setText(file_text, (String)props.get(IMemoryMap.PROP_FILE_NAME));
        int flags = 0;
        Number n = (Number)props.get(IMemoryMap.PROP_FLAGS);
        if (n != null) flags = n.intValue();
        rd_button.setSelection((flags & IMemoryMap.FLAG_READ) != 0);
        wr_button.setSelection((flags & IMemoryMap.FLAG_WRITE) != 0);
        ex_button.setSelection((flags & IMemoryMap.FLAG_EXECUTE) != 0);
        updateButtons();
    }

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

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

    private void getData() {
        getNumber(addr_text, IMemoryMap.PROP_ADDRESS);
        getNumber(size_text, IMemoryMap.PROP_SIZE);
        if (offset_text.getText().startsWith(".")) {
            props.put(IMemoryMap.PROP_SECTION_NAME, offset_text.getText());
            props.remove(IMemoryMap.PROP_OFFSET);
        }
        else {
            getNumber(offset_text, IMemoryMap.PROP_OFFSET);
            props.remove(IMemoryMap.PROP_SECTION_NAME);
        }
        getText(file_text, IMemoryMap.PROP_FILE_NAME);
        int flags = 0;
        if (rd_button.getSelection()) flags |= IMemoryMap.FLAG_READ;
        if (wr_button.getSelection()) flags |= IMemoryMap.FLAG_WRITE;
        if (ex_button.getSelection()) flags |= IMemoryMap.FLAG_EXECUTE;
        props.put(IMemoryMap.PROP_FLAGS, flags);
    }

    private void updateButtons() {
        Button btn = getButton(IDialogConstants.OK_ID);
        if (btn != null && file_text != null) btn.setEnabled(!enable_editing || file_text.getText().trim().length() > 0);
    }

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

Back to the top