alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 *                 Sun Public License Notice
 * 
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 * 
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2000 Sun
 * Microsystems, Inc. All Rights Reserved.
 */

package org.netbeans.modules.debugger.jpda.ui.models;

import org.netbeans.api.debugger.Breakpoint;
import org.netbeans.api.debugger.DebuggerEngine;
import org.netbeans.api.debugger.Watch;
import org.netbeans.api.debugger.jpda.CallStackFrame;
import org.netbeans.api.debugger.jpda.Field;
import org.netbeans.api.debugger.jpda.InvalidExpressionException;
import org.netbeans.api.debugger.jpda.JPDABreakpoint;
import org.netbeans.api.debugger.jpda.JPDADebugger;
import org.netbeans.api.debugger.jpda.JPDAThread;
import org.netbeans.api.debugger.jpda.JPDAWatch;
import org.netbeans.api.debugger.jpda.LocalVariable;
import org.netbeans.api.debugger.jpda.ObjectVariable;
import org.netbeans.api.debugger.jpda.Super;
import org.netbeans.api.debugger.jpda.This;
import org.netbeans.api.debugger.jpda.Variable;
import org.netbeans.spi.debugger.ui.Constants;
import org.netbeans.spi.viewmodel.NoInformationException;
import org.netbeans.spi.viewmodel.TableModel;
import org.netbeans.spi.viewmodel.TreeModelListener;
import org.netbeans.spi.viewmodel.UnknownTypeException;
import org.netbeans.modules.debugger.jpda.ui.FixedWatch;

import org.openide.DialogDisplayer;
import org.openide.NotifyDescriptor;
import org.openide.util.NbBundle;


/**
 *
 * @author   Jan Jancura
 */
public class JPDATableModel implements TableModel, Constants {
    
    private static String loc(String key) {
        return NbBundle.getBundle(JPDATableModel.class).getString(key);
    }

    public Object getValueAt (Object row, String columnID) throws 
    UnknownTypeException {
        if (row instanceof JPDAThread) {
            if (columnID.equals (THREAD_STATE_COLUMN_ID)) 
                switch (((JPDAThread) row).getState ()) {
                    case JPDAThread.STATE_MONITOR:
                        return loc("CTL_Thread_State_OnMonitor");
                    case JPDAThread.STATE_NOT_STARTED:
                        return loc("CTL_Thread_State_NotStarted");
                    case JPDAThread.STATE_RUNNING:
                        return loc("CTL_Thread_State_Running");
                    case JPDAThread.STATE_SLEEPING:
                        return loc("CTL_Thread_State_Sleeping");
                    case JPDAThread.STATE_UNKNOWN:
                        return loc("CTL_Thread_State_Unknown");
                    case JPDAThread.STATE_WAIT:
                        return loc("CTL_Thread_State_Waiting");
                    case JPDAThread.STATE_ZOMBIE:
                        return loc("CTL_Thread_State_Zombie");
                }
            else
            if (columnID.equals (THREAD_SUSPENDED_COLUMN_ID))
                return new Boolean (((JPDAThread) row).isSuspended ());
        } else
        if (row instanceof JPDABreakpoint) {
            if (columnID.equals (BREAKPOINT_ENABLED_COLUMN_ID))
                return new Boolean (((JPDABreakpoint) row).isEnabled ());
        } else
        if (row instanceof JPDAWatch) {
            if (columnID.equals (WATCH_TO_STRING_COLUMN_ID))
                try {
                    return ((JPDAWatch) row).getToStringValue ();
                } catch (InvalidExpressionException ex) {
                    return getMessage (ex);
                }
            else
            if (columnID.equals (WATCH_TYPE_COLUMN_ID))
                return getShort (((JPDAWatch) row).getType ());
            else
            if (columnID.equals (WATCH_VALUE_COLUMN_ID)) {
                JPDAWatch w = (JPDAWatch) row;
                String e = w.getExceptionDescription ();
                if (e != null)
                    return ">" + e + "<";
                return w.getValue ();
            }
        } else if (row instanceof FixedWatch) {
            FixedWatch fw = (FixedWatch) row;
            if (columnID.equals (WATCH_TO_STRING_COLUMN_ID))
                try {
                    Variable v = fw.getVariable ();
                    if (v instanceof ObjectVariable)
                        return ((ObjectVariable) v).getToStringValue ();
                    else
                        return v.getValue ();
                } catch (InvalidExpressionException ex) {
                    return getMessage (ex);
                }
            else
            if (columnID.equals (WATCH_TYPE_COLUMN_ID))
                return fw.getType();
            else
            if (columnID.equals (WATCH_VALUE_COLUMN_ID)) {
                return fw.getValue();
            }
        } else
//        if (row instanceof Watch) {
//            if (columnID.equals (WATCH_TO_STRING_COLUMN_ID))
//                return "";
//            else
//            if (columnID.equals (WATCH_TYPE_COLUMN_ID))
//                return "";
//            else
//            if (columnID.equals (WATCH_VALUE_COLUMN_ID))
//                return "";
//        } else
        if (row instanceof LocalVariable) {
            if (columnID.equals (LOCALS_TO_STRING_COLUMN_ID))
                if (row instanceof ObjectVariable)
                    try {
                        return ((ObjectVariable) row).getToStringValue ();
                    } catch (InvalidExpressionException ex) {
                        return getMessage (ex);
                    }
                else
                    return ((LocalVariable) row).getValue ();
            else
            if (columnID.equals (LOCALS_TYPE_COLUMN_ID))
                if (row instanceof ObjectVariable)
                    return getShort (((ObjectVariable) row).getType ());
                else
                    return ((LocalVariable) row).getDeclaredType ();
            else
            if (columnID.equals (LOCALS_VALUE_COLUMN_ID))
                return ((LocalVariable) row).getValue ();
        } else
        if (row instanceof Field) {
            if ( columnID.equals (LOCALS_TO_STRING_COLUMN_ID) ||
                 columnID.equals (WATCH_TO_STRING_COLUMN_ID)
            )
                if (row instanceof ObjectVariable)
                    try {
                        return ((ObjectVariable) row).getToStringValue ();
                    } catch (InvalidExpressionException ex) {
                        return getMessage (ex);
                    }
                else
                    return ((Field) row).getValue ();
            else
            if ( columnID.equals (LOCALS_TYPE_COLUMN_ID) ||
                 columnID.equals (WATCH_TYPE_COLUMN_ID)
            )
                if (row instanceof ObjectVariable)
                    return getShort (((ObjectVariable) row).getType ());
                else
                    return ((Field) row).getDeclaredType ();
            else
            if ( columnID.equals (LOCALS_VALUE_COLUMN_ID) ||
                 columnID.equals (WATCH_VALUE_COLUMN_ID)
            )
                return ((Field) row).getValue ();
        } else
        if (row instanceof ObjectVariable) {
            if ( columnID.equals (LOCALS_TO_STRING_COLUMN_ID) ||
                 columnID.equals (WATCH_TO_STRING_COLUMN_ID)
            ) {
                if (! (row instanceof Super))
                    try {
                        return ((ObjectVariable) row).getToStringValue ();
                    } catch (InvalidExpressionException ex) {
                        return getMessage (ex);
                    }
            } else
            if ( columnID.equals (LOCALS_TYPE_COLUMN_ID) ||
                 columnID.equals (WATCH_TYPE_COLUMN_ID)
            )
                return getShort (((ObjectVariable) row).getType ());
            else
            if ( columnID.equals (LOCALS_VALUE_COLUMN_ID) ||
                 columnID.equals (WATCH_VALUE_COLUMN_ID)
            )
                if (row instanceof This)
                    return ((This) row).getValue ();
        } else
        if (row instanceof CallStackFrame) {
            if (columnID.equals (CALL_STACK_FRAME_LOCATION_COLUMN_ID))
                try {
                    return ((CallStackFrame) row).getSourceName (
                        null // default stratumn for current csf is used
                    );
                } catch (NoInformationException e) {
                    return loc("MSG_Callstack_NoInformation");
                }
        }
        throw new UnknownTypeException (row);
    }
    
    public boolean isReadOnly (Object row, String columnID) throws 
    UnknownTypeException {
        if (row instanceof JPDAThread) {
            if (columnID.equals (THREAD_STATE_COLUMN_ID))
                return true;
            else
            if (columnID.equals (THREAD_SUSPENDED_COLUMN_ID))
                return false;
        } else
        if (row instanceof Breakpoint) {
            if (columnID.equals (BREAKPOINT_ENABLED_COLUMN_ID))
                return false;
        } else
        if (row instanceof JPDAWatch) {
            if (columnID.equals (WATCH_TO_STRING_COLUMN_ID))
                return true;
            else
            if (columnID.equals (WATCH_TYPE_COLUMN_ID))
                return true;
            else
            if (columnID.equals (WATCH_VALUE_COLUMN_ID))
                return false;
        } else
        if (row instanceof FixedWatch) {
            return true;
        } else
        if (row instanceof Watch) {
            if (columnID.equals (WATCH_TO_STRING_COLUMN_ID))
                return true;
            else
            if (columnID.equals (WATCH_TYPE_COLUMN_ID))
                return true;
            else
            if (columnID.equals (WATCH_VALUE_COLUMN_ID))
                return true;
        } else
        if (row instanceof LocalVariable) {
            if (columnID.equals (LOCALS_TO_STRING_COLUMN_ID))
                return true;
            else
            if (columnID.equals (LOCALS_TYPE_COLUMN_ID))
                return true;
            else
            if (columnID.equals (LOCALS_VALUE_COLUMN_ID))
                return false;
        } else
        if (row instanceof Field) {
            if ( columnID.equals (LOCALS_TO_STRING_COLUMN_ID) ||
                 columnID.equals (WATCH_TO_STRING_COLUMN_ID)
            )
                return true;
            else
            if ( columnID.equals (LOCALS_TYPE_COLUMN_ID) ||
                 columnID.equals (WATCH_TYPE_COLUMN_ID)
            )
                return true;
            else
            if ( columnID.equals (LOCALS_VALUE_COLUMN_ID) ||
                 columnID.equals (WATCH_VALUE_COLUMN_ID)
            )
                return false;
        } else
        if (row instanceof ObjectVariable) {
            if ( columnID.equals (LOCALS_TO_STRING_COLUMN_ID) ||
                 columnID.equals (WATCH_TO_STRING_COLUMN_ID)
            ) {
                if (! (row instanceof Super))
                    return true;
            } else
            if ( columnID.equals (LOCALS_TYPE_COLUMN_ID) ||
                 columnID.equals (WATCH_TYPE_COLUMN_ID)
            )
                return true;
            else
            if ( columnID.equals (LOCALS_VALUE_COLUMN_ID) ||
                 columnID.equals (WATCH_VALUE_COLUMN_ID)
            )
                if (row instanceof This)
                    return true;

        } else
        if (row instanceof CallStackFrame) {
            if (columnID.equals (CALL_STACK_FRAME_LOCATION_COLUMN_ID))
                return true;
        }
        throw new UnknownTypeException (row);
    }
    
    public void setValueAt (Object row, String columnID, Object value) 
    throws UnknownTypeException {
        if (row instanceof JPDAThread) {
            if (columnID.equals (THREAD_SUSPENDED_COLUMN_ID))
                if (value.equals (Boolean.TRUE))
                    ((JPDAThread) row).suspend ();
                else 
                    ((JPDAThread) row).resume ();
        } else
        if (row instanceof JPDABreakpoint) {
            if (columnID.equals (BREAKPOINT_ENABLED_COLUMN_ID))
                if (((Boolean) value).equals (Boolean.TRUE))
                    ((Breakpoint) row).enable ();
                else
                    ((Breakpoint) row).disable ();
        } else
        if (row instanceof JPDAWatch) {
            if (columnID.equals (WATCH_VALUE_COLUMN_ID))
                try {
                    ((JPDAWatch) row).setValue ((String) value);
                } catch (InvalidExpressionException e) {
                    NotifyDescriptor.Message descriptor = new NotifyDescriptor.Message (
                        e.getLocalizedMessage (), 
                        NotifyDescriptor.WARNING_MESSAGE
                    );
                    DialogDisplayer.getDefault ().notify (descriptor);
                }
        } else
        if (row instanceof LocalVariable) {
            if (columnID.equals (LOCALS_VALUE_COLUMN_ID))
                try {
                    ((LocalVariable) row).setValue ((String) value);
                } catch (InvalidExpressionException e) {
                    NotifyDescriptor.Message descriptor = new NotifyDescriptor.Message (
                        e.getLocalizedMessage (), 
                        NotifyDescriptor.WARNING_MESSAGE
                    );
                    DialogDisplayer.getDefault ().notify (descriptor);
                }
        } else
        if (row instanceof Field) {
            if ( columnID.equals (LOCALS_VALUE_COLUMN_ID) ||
                 columnID.equals (WATCH_VALUE_COLUMN_ID)
            )
                try {
                    ((Field) row).setValue ((String) value);
                } catch (InvalidExpressionException e) {
                    NotifyDescriptor.Message descriptor = new NotifyDescriptor.Message (
                        e.getLocalizedMessage (), 
                        NotifyDescriptor.WARNING_MESSAGE
                    );
                    DialogDisplayer.getDefault ().notify (descriptor);
                }
        } else
        throw new UnknownTypeException (row);
    }
    
    /** 
     * Registers given listener.
     * 
     * @param l the listener to add
     */
    public void addTreeModelListener (TreeModelListener l) {
    }

    /** 
     * Unregisters given listener.
     *
     * @param l the listener to remove
     */
    public void removeTreeModelListener (TreeModelListener l) {
    }
    
    private static String getShort (String c) {
        int i = c.lastIndexOf ('.');
        if (i < 0) return c;
        return c.substring (i + 1);
    }
    
    private static String getMessage (InvalidExpressionException e) {
        String m = e.getLocalizedMessage ();
        if (m == null)
            m = e.getMessage ();
        return ">" + m + "<";
    }
}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.