Suzan Cioc
Suzan Cioc

Reputation: 30127

Simplest way to make custom debug watch in Eclipse?

Is it possible to create custom variable viewers in Eclipse? Suppose I wish to decompose bitfield or see bitmap as an image. Is it possible?

Can you give some simplest example?

UPDATE

I have tried to implement the following example: http://alvinalexander.com/java/jwarehouse/eclipse/org.eclipse.jdt.debug.tests/test-plugin/org/eclipse/jdt/debug/testplugin/detailpane/SimpleDetailPane.java.shtml

My plugin.xml is follows:

<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.0"?>

<plugin>
   <extension
         point="org.eclipse.debug.ui.detailPaneFactories">
      <detailFactories
            class="tests.debug.details.DetailPaneFactory"
            id="tests.debug.details.detailFactories">
      </detailFactories>
   </extension>



</plugin>

My DetailPaneFactory.java is follows:

package tests.debug.details;

import java.util.AbstractSet;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import org.eclipse.debug.ui.IDetailPane;
import org.eclipse.debug.ui.IDetailPaneFactory;
import org.eclipse.jface.viewers.IStructuredSelection;

public class DetailPaneFactory implements IDetailPaneFactory {

    private HashMap<String,Class<? extends IDetailPane>> classes = new HashMap<String,Class<? extends IDetailPane>>();

    private void addClass(Class<? extends IDetailPane> cls) {
        try {
            String paneID = (String) cls.getField("ID").get(null);
            classes.put(paneID, cls);
        } catch (IllegalArgumentException | IllegalAccessException
                | NoSuchFieldException | SecurityException e) {
            throw new RuntimeException(e);
        }
        finally {

        }

    }

    private Class<? extends IDetailPane> getClass(String paneID) {
        Class<? extends IDetailPane> ans = classes.get(paneID);
        return ans;
    }

    public DetailPaneFactory() {
        addClass(SimpleDetailPane.class);
    }


    @Override
    public IDetailPane createDetailPane(String paneID) {

        Class<? extends IDetailPane> cls = getClass(paneID);
        if( cls != null ) {
            try {
                return cls.newInstance();
            } catch (InstantiationException | IllegalAccessException e) {
                throw new RuntimeException();
            }
        }
        else {
            return null;
        }
    }

    @Override
    public String getDetailPaneName(String paneID) {
        Class<? extends IDetailPane> cls = getClass(paneID);
        if( cls != null ) {
            try {
                return (String)cls.getField("NAME").get(null);
            } catch (IllegalArgumentException | IllegalAccessException
                    | NoSuchFieldException | SecurityException e) {
                throw new RuntimeException(e);
            }
        }
        else {
            return null;
        }
    }

    @Override
    public String getDetailPaneDescription(String paneID) {
        Class<? extends IDetailPane> cls = getClass(paneID);
        if( cls != null ) {
            try {
                return (String)cls.getField("DESCRIPTION").get(null);
            } catch (IllegalArgumentException | IllegalAccessException
                    | NoSuchFieldException | SecurityException e) {
                throw new RuntimeException(e);
            }
        }
        else {
            return null;
        }
    }

    @Override
    public Set<String> getDetailPaneTypes(IStructuredSelection selection) {
        return new AbstractSet<String>() {

            @Override
            public Iterator<String> iterator() {
                return new Iterator<String>() {

                    private Iterator<Map.Entry<String,Class<? extends IDetailPane>>> it = classes.entrySet().iterator();

                    @Override
                    public void remove() {
                        it.remove();
                    }

                    @Override
                    public String next() {
                        return it.next().getKey();
                    }

                    @Override
                    public boolean hasNext() {
                        return it.hasNext();
                    }
                };
            }

            @Override
            public int size() {
                return classes.size();
            }

        };
    }

    @Override
    public String getDefaultDetailPane(IStructuredSelection selection) {
        return SimpleDetailPane.ID;
    }

}

and my SimpleDetailPane.java is as in example.

And it apparently works.

Upvotes: 3

Views: 1050

Answers (1)

Eugene Kuleshov
Eugene Kuleshov

Reputation: 31795

You can use Window / Show View / Expressions and add your expression that could make some calculations and show a textual output. But in order to anything else than a text output you'll have to contribute your own model presentation trough Eclipse platform extension points. See Detail Pane Factories Extension and source of org.eclipse.temp.JavaTableDetailPaneFactory class in Eclipse's own JDT.

As a quick workaround you can also write a static utility method that will open a new window with your image converted from a bitfield and call that method from a Display view using Ctrl-U shortcut.

Upvotes: 3

Related Questions