Skip to main content
summaryrefslogtreecommitdiffstats
blob: f8455894298286888089ab5c0cde37fe59cd29e9 (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
/*******************************************************************************
 * Copyright (c) 2008 Wind River Systems 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.cdt.dsf.debug.ui.viewmodel.register;

import org.eclipse.cdt.dsf.datamodel.DMContexts;
import org.eclipse.cdt.dsf.datamodel.IDMContext;
import org.eclipse.cdt.dsf.debug.service.IFormattedValues;
import org.eclipse.cdt.dsf.debug.service.IRegisters.IBitFieldDMContext;
import org.eclipse.cdt.dsf.debug.service.IRegisters.IBitFieldDMData;
import org.eclipse.cdt.dsf.debug.service.IRegisters.IMnemonic;
import org.eclipse.cdt.dsf.debug.ui.viewmodel.IDebugVMConstants;
import org.eclipse.cdt.dsf.debug.ui.viewmodel.expression.WatchExpressionCellModifier;
import org.eclipse.cdt.dsf.debug.ui.viewmodel.numberformat.FormattedValueVMUtil;
import org.eclipse.cdt.dsf.ui.viewmodel.IVMContext;
import org.eclipse.cdt.dsf.ui.viewmodel.datamodel.IDMVMContext;
import org.eclipse.cdt.dsf.ui.viewmodel.update.AbstractCachingVMProvider;
import org.eclipse.cdt.dsf.ui.viewmodel.update.UserEditEvent;
import org.eclipse.debug.internal.ui.viewers.model.provisional.IPresentationContext;

public class RegisterBitFieldCellModifier extends WatchExpressionCellModifier {
    
    public static enum BitFieldEditorStyle { NOTHING, BITFIELDCOMBO, BITFIELDTEXT }
    
    private AbstractCachingVMProvider fProvider;
    private BitFieldEditorStyle fStyle;
    private IBitFieldDMData fBitFieldData = null;
    private Object fElement = null;
    private SyncRegisterDataAccess fDataAccess = null;
    
    /**
     * @since 2.0
     */
    public RegisterBitFieldCellModifier(AbstractCachingVMProvider provider, 
        BitFieldEditorStyle style, SyncRegisterDataAccess access ) 
    {
        fProvider = provider;
        fStyle = style;
        fDataAccess = access;
    }

    /*
     *  Used to make sure we are dealing with a valid register.
     */
    private IBitFieldDMContext getBitFieldDMC(Object element) {
        if (element instanceof IDMVMContext) {
            IDMContext dmc = ((IDMVMContext)element).getDMContext();
            return DMContexts.getAncestorOfType(dmc, IBitFieldDMContext.class);
        }
        return null;
    }
    
    @Override
    public boolean canModify(Object element, String property) {
        
        /*
         * If we're in the column value, modify the register data.
         * Otherwise, call the super-class to edit the watch expression.
         */
        if ( IDebugVMConstants.COLUMN_ID__VALUE.equals(property) ) {
            /*
             *  Make sure we are are dealing with a valid set of information.
             */
            if ( getBitFieldDMC(element) == null ) return false;
    
            fElement = element;
            
            /*
             *  We need to read the register in order to get the attributes.
             */
            fBitFieldData = fDataAccess.readBitField(element);
            
            if ( ( fBitFieldData != null ) && ( ! fBitFieldData.isWriteable() ) ) return false;
            
            return true ;
        } else {
            return super.canModify(element, property);
        }
    }

    @Override
    public Object getValue(Object element, String property) {
        /*
         * If we're in the column value, modify the register data.
         * Otherwise, call the super-class to edit the watch expression.
         */
        if ( IDebugVMConstants.COLUMN_ID__VALUE.equals(property) ) {
            /*
             *  Make sure we are working on the editable areas.
             */
            if ( element != fElement ) return false; 
    
            if ( fStyle == BitFieldEditorStyle.BITFIELDTEXT ) {
                /*
                 *  We let the Model provider supply the current format.
                 */
                String formatId;
                
                if ( element instanceof IVMContext) {
                    /*
                     *  Find the presentation context and then use it to get the current desired format.
                     */
                    IVMContext ctx = (IVMContext) element;
                    IPresentationContext presCtx = ctx.getVMNode().getVMProvider().getPresentationContext();
                    formatId = FormattedValueVMUtil.getPreferredFormat(presCtx);
                }
                else {
                    formatId = IFormattedValues.NATURAL_FORMAT;
                }
                
                String value = fDataAccess.getFormattedBitFieldValue(fElement, formatId);
                
                if ( value == null ) { value = "..."; } //$NON-NLS-1$
                
                return value;
            }
            else {
                /*
                 *  This is a COMBO BOX. So we need to take the value of the bitfield and
                 *  compare it to the associated mnemonic values to see which mnemonic is
                 *  representing the current value. At this point the Bitfield Model data
                 *  has already been established since the "canModify()" method is called
                 *  first by the flexible hierarchy proxies.
                 */
                IMnemonic curMnemonic = fBitFieldData.getCurrentMnemonicValue();
                
                int index = 0 ;
                for ( IMnemonic mnemonic : fBitFieldData.getMnemonics() ) {
                    if ( mnemonic.equals( curMnemonic ) ) {
                        return new Integer( index );
                    }
                    index ++;
                }
                
                return null;
            }
        } else {
            return super.getValue(element, property);
        }
    }
    
    @Override
    public void modify(Object element, String property, Object value) {
        /*
         * If we're in the column value, modify the register data.
         * Otherwise, call the super-class to edit the watch expression.
         */
        if ( IDebugVMConstants.COLUMN_ID__VALUE.equals(property) ) {
            if ( fStyle == BitFieldEditorStyle.BITFIELDTEXT ) {
                if (value instanceof String) {
                    /*
                     *  We let the Model provider supply the current format.
                     */
                    String formatId;
                    
                    if ( element instanceof IVMContext) {
                        /*
                         *  Find the presentation context and then use it to get the current desired format.
                         */
                        IVMContext ctx = (IVMContext) element;
                        IPresentationContext presCtx = ctx.getVMNode().getVMProvider().getPresentationContext();
                        formatId = FormattedValueVMUtil.getPreferredFormat(presCtx);
                    }
                    else {
                        formatId = IFormattedValues.NATURAL_FORMAT;
                    }
                    fDataAccess.writeBitField(element, (String) value, formatId);
                    fProvider.handleEvent(new UserEditEvent(element));
                }
            }
            else {
                if (value instanceof Integer) {
                    /*
                     *  Get the integer value corresponding to the selected entry.
                     */
                    Integer val = (Integer) value;
                    
                    /*
                     *  Write the bit field using the selected mnemonic.
                     */
                    fDataAccess.writeBitField(element, fBitFieldData.getMnemonics()[val.intValue()]);
                    fProvider.handleEvent(new UserEditEvent(element));
                }
            }
        } else {
            super.modify(element, property, value);
        }
    }
}

Back to the top