1 19 20 package org.netbeans.modules.debugger.jpda.ui.models; 21 22 import org.netbeans.spi.debugger.ui.Constants; 23 import org.netbeans.spi.viewmodel.*; 24 import org.netbeans.api.debugger.jpda.*; 25 import org.openide.util.actions.Presenter; 26 import org.openide.util.NbBundle; 27 28 import javax.swing.*; 29 import java.util.*; 30 import java.awt.event.ActionEvent ; 31 32 39 public class NumericDisplayFilter implements TableModelFilter, 40 NodeActionsProviderFilter, Constants { 41 42 private final Map variableToDisplaySettings = new HashMap (); 43 private HashSet listeners; 44 45 46 48 public Object getValueAt ( 49 TableModel original, 50 Object node, 51 String columnID 52 ) throws UnknownTypeException { 53 if ( (columnID == Constants.WATCH_VALUE_COLUMN_ID || 54 columnID == Constants.LOCALS_VALUE_COLUMN_ID) && 55 node instanceof Variable && 56 isIntegralType ((Variable) node) 57 ) { 58 Variable var = (Variable) node; 59 return getValue ( 60 var, 61 (NumericDisplaySettings) variableToDisplaySettings.get (var) 62 ); 63 } 64 return original.getValueAt (node, columnID); 65 } 66 67 public boolean isReadOnly ( 68 TableModel original, 69 Object node, 70 String columnID 71 ) throws UnknownTypeException { 72 return original.isReadOnly(node, columnID); 73 } 74 75 public void setValueAt ( 76 TableModel original, 77 Object node, 78 String columnID, 79 Object value 80 ) throws UnknownTypeException { 81 original.setValueAt(node, columnID, value); 82 } 83 84 public void addModelListener (ModelListener l) { 85 HashSet newListeners = (listeners == null) ? 86 new HashSet () : (HashSet) listeners.clone (); 87 newListeners.add (l); 88 listeners = newListeners; 89 } 90 91 public void removeModelListener (ModelListener l) { 92 if (listeners == null) return; 93 HashSet newListeners = (HashSet) listeners.clone(); 94 newListeners.remove (l); 95 listeners = newListeners; 96 } 97 98 99 101 public void performDefaultAction ( 102 NodeActionsProvider original, 103 Object node 104 ) throws UnknownTypeException { 105 original.performDefaultAction (node); 106 } 107 108 public Action[] getActions ( 109 NodeActionsProvider original, 110 Object node 111 ) throws UnknownTypeException { 112 if (!(node instanceof Variable)) return original.getActions(node); 113 Action [] actions; 114 try { 115 actions = original.getActions(node); 116 } catch (UnknownTypeException e) { 117 actions = new Action[0]; 118 } 119 List myActions = new ArrayList(); 120 if (node instanceof Variable) { 121 Variable var = (Variable) node; 122 if (isIntegralType(var)) { 123 myActions.add(new DisplayAsAction((Variable) node)); 124 } 125 } 126 myActions.addAll(Arrays.asList(actions)); 127 return (Action[]) myActions.toArray(new Action[myActions.size()]); 128 } 129 130 131 133 private Object getValue (Variable var, NumericDisplaySettings settings) { 134 if (settings == null) return var.getValue (); 135 String type = var.getType (); 136 switch (settings.getDisplayAs ()) { 137 case NumericDisplaySettings.DECIMAL: 138 return var.getValue (); 139 case NumericDisplaySettings.HEXADECIMAL: 140 if ("int".equals (type)) 141 return "0x" + Integer.toHexString ( 142 Integer.parseInt (var.getValue ()) 143 ); 144 else 145 if ("short".equals (type)) { 146 String rv = Integer.toHexString(Short.parseShort(var.getValue())); 147 if (rv.length() > 4) rv = rv.substring(rv.length() - 4, rv.length()); 148 return "0x" + rv; 149 } else if ("byte".equals(type)) { 150 String rv = Integer.toHexString(Byte.parseByte(var.getValue())); 151 if (rv.length() > 2) rv = rv.substring(rv.length() - 2, rv.length()); 152 return "0x" + rv; 153 } else 154 return "0x" + Long.toHexString ( 155 Long.parseLong (var.getValue ()) 156 ); 157 case NumericDisplaySettings.OCTAL: 158 if ("int".equals (type)) 159 return "0" + Integer.toOctalString ( 160 Integer.parseInt (var.getValue ()) 161 ); 162 else 163 if ("short".equals(type)) { 164 String rv = Integer.toOctalString(Short.parseShort(var.getValue())); 165 if (rv.length() > 5) rv = rv.substring(rv.length() - 5, rv.length()); 166 return "0" + (rv.charAt(0) == '0' ? "1" : "") + rv; 167 } else 168 if ("byte".equals(type)) { 169 String rv = Integer.toOctalString(Byte.parseByte(var.getValue())); 170 if (rv.length() > 3) rv = "1" + rv.substring(rv.length() - 2, rv.length()); 171 return "0" + rv; 172 } else 173 return "0" + Long.toOctalString ( 174 Long.parseLong (var.getValue ()) 175 ); 176 case NumericDisplaySettings.BINARY: 177 if ("int".equals(type)) 178 return Integer.toBinaryString(Integer.parseInt(var.getValue())); 179 else if ("short".equals(type)) { 180 String rv = Integer.toBinaryString(Short.parseShort(var.getValue())); 181 if (rv.length() > 16) rv = rv.substring(rv.length() - 16, rv.length()); 182 return rv; 183 } else if ("byte".equals(type)) { 184 String rv = Integer.toBinaryString(Byte.parseByte(var.getValue())); 185 if (rv.length() > 8) rv = rv.substring(rv.length() - 8, rv.length()); 186 return rv; 187 } else 188 return Long.toBinaryString (Long.parseLong (var.getValue ())); 189 case NumericDisplaySettings.CHAR: 190 try { 191 return "'" + new Character ( 192 (char) Integer.parseInt (var.getValue ()) 193 ) + "'"; 194 } catch (Exception e) { 195 return "?"; 196 } 197 default: 198 return var.getValue (); 199 } 200 } 201 202 private boolean isIntegralType (Variable v) { 203 if (!VariablesTreeModelFilter.isEvaluated(v)) { 204 return false; 205 } 206 207 String type = v.getType (); 208 return "int".equals (type) || 209 "char".equals (type) || 210 "byte".equals (type) || 211 "long".equals (type) || 212 "short".equals (type); 213 } 214 215 private String localize(String s) { 216 return NbBundle.getBundle(NumericDisplayFilter.class).getString(s); 217 } 218 219 private class DisplayAsAction extends AbstractAction 220 implements Presenter.Popup { 221 222 private Variable variable; 223 224 public DisplayAsAction(Variable variable) { 225 this.variable = variable; 226 } 227 228 public void actionPerformed(ActionEvent e) { 229 } 230 231 public JMenuItem getPopupPresenter() { 232 JMenu displayAsPopup = new JMenu 233 (localize ("CTL_Variable_DisplayAs_Popup")); 234 235 JRadioButtonMenuItem decimalItem = new JRadioButtonMenuItem ( 236 new AbstractAction ( 237 localize ("CTL_Variable_DisplayAs_Decimal") 238 ) { 239 public void actionPerformed (ActionEvent e) { 240 onDisplayAs (NumericDisplaySettings.DECIMAL); 241 } 242 } 243 ); 244 JRadioButtonMenuItem hexadecimalItem = new JRadioButtonMenuItem ( 245 new AbstractAction ( 246 localize ("CTL_Variable_DisplayAs_Hexadecimal") 247 ) { 248 public void actionPerformed (ActionEvent e) { 249 onDisplayAs (NumericDisplaySettings.HEXADECIMAL); 250 } 251 } 252 ); 253 JRadioButtonMenuItem octalItem = new JRadioButtonMenuItem ( 254 new AbstractAction ( 255 localize ("CTL_Variable_DisplayAs_Octal") 256 ) { 257 public void actionPerformed (ActionEvent e) { 258 onDisplayAs (NumericDisplaySettings.OCTAL); 259 } 260 } 261 ); 262 JRadioButtonMenuItem binaryItem = new JRadioButtonMenuItem ( 263 new AbstractAction ( 264 localize ("CTL_Variable_DisplayAs_Binary") 265 ) { 266 public void actionPerformed (ActionEvent e) { 267 onDisplayAs (NumericDisplaySettings.BINARY); 268 } 269 } 270 ); 271 JRadioButtonMenuItem charItem = new JRadioButtonMenuItem ( 272 new AbstractAction ( 273 localize ("CTL_Variable_DisplayAs_Character") 274 ) { 275 public void actionPerformed (ActionEvent e) { 276 onDisplayAs (NumericDisplaySettings.CHAR); 277 } 278 } 279 ); 280 281 NumericDisplaySettings lds = (NumericDisplaySettings) 282 variableToDisplaySettings.get (variable); 283 if (lds != null) { 284 switch (lds.getDisplayAs ()) { 285 case NumericDisplaySettings.DECIMAL: 286 decimalItem.setSelected (true); 287 break; 288 case NumericDisplaySettings.HEXADECIMAL: 289 hexadecimalItem.setSelected (true); 290 break; 291 case NumericDisplaySettings.OCTAL: 292 octalItem.setSelected (true); 293 break; 294 case NumericDisplaySettings.BINARY: 295 binaryItem.setSelected (true); 296 break; 297 case NumericDisplaySettings.CHAR: 298 charItem.setSelected (true); 299 break; 300 } 301 } else { 302 decimalItem.setSelected (true); 303 } 304 305 displayAsPopup.add (decimalItem); 306 displayAsPopup.add (hexadecimalItem); 307 displayAsPopup.add (octalItem); 308 displayAsPopup.add (binaryItem); 309 displayAsPopup.add (charItem); 310 return displayAsPopup; 311 } 312 313 private void onDisplayAs (int how) { 314 NumericDisplaySettings lds = (NumericDisplaySettings) 315 variableToDisplaySettings.get (variable); 316 if (lds == null) { 317 lds = new NumericDisplaySettings 318 (NumericDisplaySettings.DECIMAL); 319 } 320 if (lds.getDisplayAs () == how) return; 321 variableToDisplaySettings.put 322 (variable, new NumericDisplaySettings (how)); 323 fireModelChanged (); 324 } 325 326 private void fireModelChanged () { 327 if (listeners == null) return; 328 ModelEvent evt = new ModelEvent.TableValueChanged(this, variable, null); 329 for (Iterator i = listeners.iterator (); i.hasNext ();) { 330 ModelListener listener = (ModelListener) i.next (); 331 listener.modelChanged (evt); 332 } 333 } 334 } 335 336 337 private static class NumericDisplaySettings { 338 339 public static final int DECIMAL = 0; 340 public static final int HEXADECIMAL = 1; 341 public static final int OCTAL = 2; 342 public static final int BINARY = 3; 343 public static final int CHAR = 4; 344 345 private int displayAs; 346 347 public NumericDisplaySettings (int displayAs) { 348 this.displayAs = displayAs; 349 } 350 351 public int getDisplayAs () { 352 return displayAs; 353 } 354 } 355 356 } 357 | Popular Tags |