Class Analyzer : 2D « SWT JFace Eclipse « Java






Class Analyzer

Class Analyzer
 

/*******************************************************************************
 * All Right Reserved. Copyright (c) 1998, 2004 Jackwind Li Guojie
 * 
 * Created on 2004-7-4 19:32:27 by JACK $Id$
 *  
 ******************************************************************************/
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

import org.eclipse.draw2d.AbstractBorder;
import org.eclipse.draw2d.ChopboxAnchor;
import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.LightweightSystem;
import org.eclipse.draw2d.LineBorder;
import org.eclipse.draw2d.MouseListener;
import org.eclipse.draw2d.PolygonDecoration;
import org.eclipse.draw2d.PolylineConnection;
import org.eclipse.draw2d.ToolbarLayout;
import org.eclipse.draw2d.XYLayout;
import org.eclipse.draw2d.geometry.Insets;
import org.eclipse.draw2d.geometry.PointList;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.InputDialog;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.resource.ImageRegistry;
import org.eclipse.jface.window.ApplicationWindow;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

class ClassFigure extends Figure {
  ImageRegistry registry = new ImageRegistry();
  // image keys.
  String KEY_CLASS = "class";
  String KEY_METHOD_PUBLIC = "method_public";
  String KEY_METHOD_DEFAULT = "method_default";
  String KEY_METHOD_PROTECTED = "method_protected";
  String KEY_METHOD_PRIVATE = "method_private";
  String KEY_FIELD_PUBLIC = "field_public";
  String KEY_FIELD_DEFAULT = "field_default";
  String KEY_FIELD_PROTECTED = "field_protected";
  String KEY_FIELD_PRIVATE = "field_private";

  String[] keys =
    {
      KEY_CLASS,
      KEY_METHOD_PUBLIC,
      KEY_METHOD_DEFAULT,
      KEY_METHOD_PROTECTED,
      KEY_METHOD_PRIVATE,
      KEY_FIELD_PUBLIC,
      KEY_FIELD_DEFAULT,
      KEY_FIELD_PROTECTED,
      KEY_FIELD_PRIVATE };

  public Box fieldBox = new Box();
  public Box methodBox = new Box();

  public ClassFigure(Class cls) {
    setLayoutManager(new ToolbarLayout());
    setBorder(new LineBorder(ColorConstants.black));
    setBackgroundColor(ColorConstants.yellow);
    setOpaque(true);

    for (int i = 0; i < keys.length; i++)
      registry.put(
        keys[i],
        ImageDescriptor.createFromFile(
          null,
          "icons/java/" + keys[i] + ".gif"));

    Label title = new Label(cls.getName(), registry.get(KEY_CLASS));
    add(title);
    add(fieldBox);
    add(methodBox);

    // fields.
    Field[] fields = cls.getDeclaredFields();
    for (int i = 0; i < fields.length; i++) {
      Field field = fields[i];
      Image image = null;
      if (Modifier.isPublic(field.getModifiers())) {
        image = registry.get(KEY_FIELD_PUBLIC);
      } else if (Modifier.isProtected(field.getModifiers())) {
        image = registry.get(KEY_FIELD_PROTECTED);
      } else if (Modifier.isPrivate(field.getModifiers())) {
        image = registry.get(KEY_FIELD_PRIVATE);
      } else {
        image = registry.get(KEY_FIELD_DEFAULT);
      }
      fieldBox.add(new Label(fields[i].getName(), image));
    }

    // fields.
    Method[] methods = cls.getDeclaredMethods();
    for (int i = 0; i < methods.length; i++) {
      Method method = methods[i];
      Image image = null;
      if (Modifier.isPublic(method.getModifiers())) {
        image = registry.get(KEY_METHOD_PUBLIC);
      } else if (Modifier.isProtected(method.getModifiers())) {
        image = registry.get(KEY_METHOD_PROTECTED);
      } else if (Modifier.isPrivate(method.getModifiers())) {
        image = registry.get(KEY_METHOD_PRIVATE);
      } else {
        image = registry.get(KEY_METHOD_DEFAULT);
      }
      methodBox.add(new Label(methods[i].getName(), image));
    }

  }
}

class Box extends Figure {
  public Box() {
    setBorder(new BoxBorder());

    ToolbarLayout toolbarLayout = new ToolbarLayout();
    toolbarLayout.setStretchMinorAxis(false);

    setLayoutManager(toolbarLayout);
  }

  private class BoxBorder extends AbstractBorder {

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.draw2d.Border#getInsets(org.eclipse.draw2d.IFigure)
     */
    public Insets getInsets(IFigure figure) {
      return new Insets(1, 0, 0, 0);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.draw2d.Border#paint(org.eclipse.draw2d.IFigure,
     *      org.eclipse.draw2d.Graphics,
     *      org.eclipse.draw2d.geometry.Insets)
     */
    public void paint(IFigure figure, Graphics graphics, Insets insets) {
      Rectangle rect = getPaintRectangle(figure, insets);
      graphics.drawLine(rect.getTopLeft(), rect.getTopRight());
    }

  }
}

/**
 *  
 */
public class ClassAnalyzer extends ApplicationWindow {
  Text className;

  Canvas canvas;
  Figure contents;
  XYLayout xyLayout;

  /**
   * @param parentShell
   */
  public ClassAnalyzer(Shell parentShell) {
    super(parentShell);
    addToolBar(SWT.NULL);
  }

  private void showClass(Class cls) {
    if (cls == null)
      return;

    // removes all existing items.
    contents.removeAll();

    // draws super class.
    Label sup = null;
    if (cls.getSuperclass() != null) {
      final Class superClass = cls.getSuperclass();
      sup = new Label(superClass.getName());
      sup.setBorder(new LineBorder());
      sup.setBackgroundColor(ColorConstants.lightGreen);
      sup.setOpaque(true);
      sup.addMouseListener(new MouseListener() {
        public void mousePressed(org.eclipse.draw2d.MouseEvent me) {
        }

        public void mouseReleased(org.eclipse.draw2d.MouseEvent me) {
        }

        public void mouseDoubleClicked(
          org.eclipse.draw2d.MouseEvent me) {
          showClass(superClass);
        }
      });
    }

    if (sup != null) {
      contents.add(sup);
      xyLayout.setConstraint(sup, new Rectangle(20, 20, -1, -1));
    }

    ClassFigure classFigure = new ClassFigure(cls);
    contents.add(classFigure);
    if (sup == null)
      xyLayout.setConstraint(classFigure, new Rectangle(20, 20, -1, -1));
    else
      xyLayout.setConstraint(
        classFigure,
        new Rectangle(20, sup.getBounds().height + 40, -1, -1));

    // adds connection.
    if (sup != null) {
      PolylineConnection connection = new PolylineConnection();
      ChopboxAnchor source = new ChopboxAnchor(classFigure);
      ChopboxAnchor target = new ChopboxAnchor(sup);
      connection.setSourceAnchor(source);
      connection.setTargetAnchor(target);

      PolygonDecoration decoration = new PolygonDecoration();
      PointList list = new PointList();
      list.addPoint(-2, -2);
      list.addPoint(0, 0);
      list.addPoint(-2, 2);
      decoration.setTemplate(list);

      connection.setTargetDecoration(decoration);

      contents.add(connection);
    }

    // resizes the shell.
    getShell().setSize(
      contents.getPreferredSize().width + 30,
      contents.getPreferredSize().height + 80);

  }

  /*
   * (non-Javadoc)
   * 
   * @see org.eclipse.jface.window.Window#createContents(org.eclipse.swt.widgets.Composite)
   */
  protected Control createContents(Composite parent) {
    Composite composite = new Composite(parent, SWT.NULL);
    composite.setLayout(new FillLayout());

    canvas = new Canvas(composite, SWT.NULL);
    canvas.setBackground(getShell().getDisplay().getSystemColor(SWT.COLOR_WHITE));

    LightweightSystem lws = new LightweightSystem(canvas);
    contents = new Figure();
    xyLayout = new XYLayout();
    contents.setLayoutManager(xyLayout);

    lws.setContents(contents);

    showClass(this.getClass());

    // Creates tool bar items.
    getToolBarManager().add(new Action("Set class ...") {
      public void run() {
        InputDialog dialog =
          new InputDialog(
            getShell(),
            "",
            "Please enter the class name",
            "",
            null);
        if (dialog.open() != Dialog.OK)
          return;

        contents.removeAll();
        Class cls = null;
        try {
          cls = Class.forName(dialog.getValue());
        } catch (ClassNotFoundException e) {
          e.printStackTrace();
        }
        if (cls != null) {
          showClass(cls);
        }
      }
    });
    getToolBarManager().update(true);

    return composite;
  }

  public static void main(String[] args) {
    ClassAnalyzer window = new ClassAnalyzer(null);

    window.setBlockOnOpen(true);
    window.open();

    Display.getCurrent().dispose();
  }
}


           
         
  








Related examples in the same category

1.SWT 2D Chart: FlowchartSWT 2D Chart: Flowchart
2.Use of Java2D on SWT or Draw2D graphical contextUse of Java2D on SWT or Draw2D graphical context
3.SWT 2D UnicodeSWT 2D Unicode
4.SWT 2D Simple DemoSWT 2D Simple Demo
5.SWT Draw 2DSWT Draw 2D
6.SWT Draw2D Example
7.XORXOR
8.Animations ExampleAnimations Example
9.Alpha Fade InAlpha Fade In
10.DrawingsDrawings
11.Draw Text DemoDraw Text Demo
12.GC Creation
13.PalettesPalettes
14.TransparencyTransparency
15.Draw2D SampleDraw2D Sample
16.Demonstrates animation. It uses double buffering.Demonstrates animation. It uses double buffering.
17.Demonstrates drawing an ArcDemonstrates drawing an Arc
18.Demonstrates drawing polygonsDemonstrates drawing polygons
19.Demonstrates drawing points. It draws a sine waveDemonstrates drawing points. It draws a sine wave
20.Draw Round RectangleDraw Round Rectangle
21.Displays information about the display deviceDisplays information about the display device
22.Demonstrates the effects of the flags on the constructorDemonstrates the effects of the flags on the constructor
23.Demonstrates how to draw vertical textDemonstrates how to draw vertical text
24.Utility methods for drawing graphics
25.Demonstrates drawing linesDemonstrates drawing lines
26.Demonstrates animationDemonstrates animation
27.Demonstrates how to draw textDemonstrates how to draw text
28.Demonstrates drawing ovalsDemonstrates drawing ovals
29.Demonstrates how to draw text in colorsDemonstrates how to draw text in colors
30.SWT Paint Example
31.SWT Graphics ExampleSWT Graphics Example
32.SWT OpenGL snippet: draw a square
33.Drawing with transformations, paths and alpha blendingDrawing with transformations, paths and alpha blending
34.Draw lines and polygons with different cap and join stylesDraw lines and polygons with different cap and join styles
35.GC: implement a simple scribble programGC: implement a simple scribble program
36.GC: measure a stringGC: measure a string
37.GC example: draw a thick lineGC example: draw a thick line
38.How to draw directly on an SWT ControlHow to draw directly on an SWT Control
39.SWT useful utilities