使用Draw2D来实现任务进度条

 

去年年底一直都在研究GMF,GEF和EMF,也做一了些例子,但总是没有机会在项目中用到,本来是希望在这次的项目中能够集成进来,但还是由于种种原因不成实现,感觉非常遗憾。不过在项目中有一个用进度条来监控任务完成情况的功能,正好需要用到图形方面的东西,但是功能很简单,如果用GEF那真是杀鸡用牛刀了,最后还是使用Draw2D来实现的,也正好弥补一下我在Draw2D方面的不足。

该功能的主要要求如下:

1,  要用进度条来实现任务执行状态的监控。

2,  进度条要根据任务的不同状态来显示不同的颜色用以区分。

3,  进度条要能响应事件。

4,  根据显示时间类型的不同(如天,小时,分钟等等)来自动调整进度条的长度比例等等。

 

下面是整个功能的实现过程:

1,  环境配置,本例使用了Eclipse3.3.1 + WTP插件来构建。

2,  建立一个RCP项目,项目名为draw2d.example,并添加一个ViewPart。

3,  新建一个包,名为draw2d。

4,  运行一个效果下图:

                                         初次运行效果

 

1,  加入Draw2D的依赖项目,在plugin.xml页面中单击Dependencies标签,单击Required Plug-ins下面的“Add…”按钮,在弹出的窗口中输入“*.draw2d”字样,在列表中选择“org.eclipse.draw2d”,然后单击“OK”按钮并保存文件。如下图:

 

1,  这样,一个基本的RCP项目就创建好了。再说说功能实现的方式:

因为任务有任务名,任务的开始执行时间和结束时间,这两个时间是已经预定好了的,所以进度条的长度就是由开始时间和结束时间之差来决定的,任务还有子任务。

在确定了进度条的长度之后,就可以得到在开始时间和结束时间内每秒钟进度条应该增长的长度。任务名就放在进度条的前面来显示。

因为子任务不要求在进度条上显示,所以就放到ToolTip中来显示。布局的示意图如下:

 

 

 

下面是关键部分的代码:

 

 

===================================================================

AbstractProcessorFigure.class进度条的超类,一些公共方法和属性入在其中。

===================================================================

/**

 * Copyright (C) 2006 - 2008

 */

package draw2d.example.draw2d;

 

import org.eclipse.draw2d.ColorConstants;

import org.eclipse.draw2d.Figure;

import org.eclipse.draw2d.Graphics;

import org.eclipse.swt.graphics.Color;

 

import draw2d.example.entity.SActivity;

 

/**

 * 进度条的超类,一些公共方法和属性入在其中

 * @author 严军

 * @create 2008-4-23 下午04:33:52

 * @version v1.0

 */

public abstract class AbstractProcessorFigure extends Figure {

    public static final int WEEK = Util.WEEK;

    public static final int DAY = Util.DAY;

    public static final int HOUR = Util.HOUR;

    public static final int MINUTE = Util.MINUTE;

    public static final int SECOND = Util.SECOND;

   

    public static final String week = "星期";

    public static final String day = "";

    public static final String hour = "小时";

    public static final String minute = "分钟";

   

    public static final String all = "全部";

    public static final String doing = "正在执行";

    public static final String canceled = "已取消";

    public static final String waiting = "等待";

    public static final String finished = "已完成";

    public static final String timeouted = "已超时";

   

    /**

     * 每周的刻度数

     */

    protected static final int weekMark = 7;

    /**

     * 每天的刻度数

     */

    protected static final int dayMark = 12;

    /**

     * 每小时的刻度数

     */

    protected static final int hourMark = 10;

    /**

     * 每分钟的刻度数

     */

    protected static final int minuteMark = 10;

 

    /**

     * 周刻度的距离

     */

    protected static final int weekMarkSpace = 15;

 

    /**

     * 天刻度的距离

     */

    protected static final int dayMarkSpace = 6;

 

    /**

     * 小时刻度的距离

     */

    protected static final int hourMarkSpace = 6;

 

    /**

     * 分钟刻度的距离

     */

    protected static final int minuteMarkSpace = 8;

    /**

     * 刻度、时间的比例

     */

    protected int scale = 1;

    /**

     * 时间类型,默认为按小时

     */

    protected int timeType = HOUR;

    /**

     * 时间值,默认为秒

     */

    protected int timeValue = 0;

    /**

     * 一共能画多少格刻度

     */

    protected int markCount = 0;

    /**

     * 刻度的起点

     */

    protected static final int x_pos = 100;

    /**

     * 图形的前景色

     */

    protected Color foregroundColor = ColorConstants.lightGray;

    /**

     * 图形的背景色

     */

    protected Color backgroundColor = ColorConstants.lightBlue;

   

    protected SActivity activity;

 

    public AbstractProcessorFigure() {}

   

    public AbstractProcessorFigure(SActivity activity) {

       this.activity = activity;

    }

 

    /**

     * 画星期刻度

     * @param g

     */

    protected void drawWeekRuler(Graphics g) {

       //一周的刻度宽度

       int weekWidth = weekMark * weekMarkSpace;

       //把秒钟换算成周

       double time =  Double.valueOf(timeValue).doubleValue() / (60d * 60d * 24d * 7d);

       //总共要画的宽度

       double drawWidth = time * weekWidth;

       //改变图形的宽度

       bounds.width = Double.valueOf(drawWidth).intValue();

    }

    /**

     * 画天刻度

     * @param g

     */

    protected void drawDayRuler(Graphics g) {

       //一天的刻度宽度

       int dayWidth = dayMark * dayMarkSpace;

       //把秒钟换算成天

       double time =  Double.valueOf(timeValue).doubleValue() / (60d * 60d * 24d);

       //总共要画的宽度

       double drawWidth = time * dayWidth;

       //改变图形的宽度

       bounds.width = Double.valueOf(drawWidth).intValue();

    }

    /**

     * 画小时刻度

     * @param g

     */

    protected void drawHourRuler(Graphics g) {

       //一小时的刻度宽度

       int hourWidth = hourMark * hourMarkSpace;

       //把秒钟换算成小时

       double time =  Double.valueOf(timeValue).doubleValue() / (60d * 60d);

       //总共要画的宽度

       double drawWidth = time * hourWidth;

       //改变图形的宽度

       bounds.width = Double.valueOf(drawWidth).intValue();

    }

    /**

     * 画分钟刻度

     * @param g

     */

    protected void drawMinuteRuler(Graphics g) {

       //一分钟的刻度宽度

       int minuteWidth = minuteMark * minuteMarkSpace;

       //把秒钟换算成分钟

       double time =  Double.valueOf(timeValue).doubleValue() / 60d;

       //总共要画的宽度

       double drawWidth = time * minuteWidth;

       //改变图形的宽度

       bounds.width = Double.valueOf(drawWidth).intValue();

    }

 

    public int getScale() {

       return scale;

    }

 

    public void setScale(int scale) {

       this.scale = scale;

    }

 

    public int getTimeType() {

       return timeType;

    }

 

    public void setTimeType(int timeType) {

       this.timeType = timeType;

    }

 

    public int getTimeValue() {

       return timeValue;

    }

 

    public void setTimeValue(int timeValue) {

       this.timeValue = timeValue;

    }

 

    public SActivity getActivity() {

       return activity;

    }

 

    public void setActivity(SActivity activity) {

       this.activity = activity;

    }

 

    public int getMarkCount() {

       return markCount;

    }

 

    public void setMarkCount(int markCount) {

       this.markCount = markCount;

    }

 

    public Color getForegroundColor() {

       return foregroundColor;

    }

 

    public void setForegroundColor(Color foregroundColor) {

       this.foregroundColor = foregroundColor;

    }

 

    public Color getBackgroundColor() {

       return backgroundColor;

    }

 

    public void setBackgroundColor(Color backgroundColor) {

       this.backgroundColor = backgroundColor;

    }

}

 

 

 

===================================================================

TimeRuler.class时间轴,按不同时间显示类型来画出相应的时间刻度。

===================================================================

 

/**

 * Copyright (C) 2006 - 2008

 */

package draw2d.example.draw2d;

 

import org.eclipse.draw2d.FigureUtilities;

import org.eclipse.draw2d.Graphics;

import org.eclipse.draw2d.geometry.Dimension;

import org.eclipse.swt.graphics.Color;

/**

 * 时间轴,按不同时间显示类型来画出相应的时间刻度

 * @author 严军

 * @create 2008-4-22 下午12:11:17

 * @version v1.0

 */

public class TimeRuler extends AbstractProcessorFigure implements ColorConstants {

    /**

     * 短刻度的长度

     */

    private static final int shortLen = 8;

    /**

     * 长刻度的长度

     */

    private static final int longLen = 14;

 

    public TimeRuler() {

       setPreferredSize(getBounds().width, 30);

    }

 

    @Override

    protected void paintFigure(Graphics g) {

       super.paintFigure(g);

       //渐变色的背景

       Color oldForeground = g.getForegroundColor();

       Color oldBackground = g.getBackgroundColor();

       g.setForegroundColor(timeRulerForegroundColor);

       g.setBackgroundColor(timeRulerBackgroundColor);

       g.fillGradient(bounds, true);

       g.setForegroundColor(oldForeground);

       g.setBackgroundColor(oldBackground);

       //画出提示

       g.drawLine(0, 0, 100, 29);

       g.drawText("任务名称", 2, 15);

       g.setLineWidth(2);

       g.drawLine(100, 0, 100, 30);

       g.drawText("时间轴", 60, 2);

       g.drawRectangle(bounds);

       g.setLineWidth(1);

      

       switch (timeType) {

           case WEEK:

              drawWeekRuler(g);

              break;

           case DAY:

              drawDayRuler(g);

              break;

           case HOUR:

              drawHourRuler(g);

              break;

           case MINUTE:

              drawMinuteRuler(g);

              break;

       }

    }

    @Override

    protected void drawDayRuler(Graphics g) {

       // 一共能画多少格刻度

       markCount = (bounds.width - x_pos) / dayMarkSpace;

       //比例

       //scale = timeValue / markCount + 1;

       int x = bounds.x + x_pos;

       int y = bounds.y;

       // 要画多少长刻度

       int longLineCount = markCount / dayMark + 1;

       //画长刻度线

       g.setLineWidth(2);

       for (int i = 0; i < longLineCount; i++) {

           g.drawLine(x, y, x, y + longLen);

 

           String s = String.valueOf(i) + getTypeString();

           if (s.startsWith("0")) {

              g.drawString(s, x + 2, y + 15);

           } else {

              Dimension fsize = FigureUtilities.getStringExtents(s, getFont());

              g.drawString(s, x - (fsize.width / 2), y + 15);

           }

           x = x + (dayMarkSpace * dayMark);

       }

       g.setLineWidth(1);

       //重新设定x坐标起点

       x = bounds.x + x_pos;

       //画短刻度线

       for (int i = 0; i < longLineCount; i ++) {

           for (int j = 0; j < dayMark; j ++) {

              g.drawLine(x, y, x, y + shortLen);

              x = x + dayMarkSpace;

           }

       }

    }

 

    @Override

    protected void drawHourRuler(Graphics g) {

       // 一共能画多少格刻度

       markCount = (bounds.width - x_pos) / hourMarkSpace;

       //比例

       //scale = timeValue / markCount + 1;

       int x = bounds.x + x_pos;

       int y = bounds.y;

       // 要画多少长刻度

       int longLineCount = markCount / hourMark + 1;

       //画长刻度线

       g.setLineWidth(2);

       for (int i = 0; i < longLineCount; i++) {

           g.drawLine(x, y, x, y + longLen);

 

           String s = String.valueOf(i) + getTypeString();

           if (s.startsWith("0")) {

              g.drawString(s, x + 2, y + 15);

           } else {

              Dimension fsize = FigureUtilities

                     .getStringExtents(s, getFont());

              g.drawString(s, x - (fsize.width / 2), y + 15);

           }

           x = x + (hourMarkSpace * hourMark);

       }

       g.setLineWidth(1);

       //重新设定x坐标起点

       x = bounds.x + x_pos;

       //画短刻度线

       for (int i = 0; i < longLineCount; i ++) {

           for (int j = 0; j < hourMark; j ++) {

              g.drawLine(x, y, x, y + shortLen);

              x = x + hourMarkSpace;

           }

       }

    }

    @Override

    protected void drawMinuteRuler(Graphics g) {

       // 一共能画多少格刻度

       markCount = (bounds.width - x_pos) / minuteMarkSpace;

       //比例

       //scale = timeValue / markCount + 1;

       int x = bounds.x + x_pos;

       int y = bounds.y;

       // 要画多少长刻度

       int longLineCount = markCount / minuteMark + 1;

       //画长刻度线

       g.setLineWidth(2);

       for (int i = 0; i < longLineCount; i++) {

           g.drawLine(x, y, x, y + longLen);

 

           String s = String.valueOf(i) + getTypeString();

           if (s.startsWith("0")) {

              g.drawString(s, x + 2, y + 15);

           } else {

              Dimension fsize = FigureUtilities

                     .getStringExtents(s, getFont());

              g.drawString(s, x - (fsize.width / 2), y + 15);

           }

           x = x + (minuteMarkSpace * minuteMark);

       }

       g.setLineWidth(1);

       //重新设定x坐标起点

       x = bounds.x + x_pos;

       //画短刻度线

       for (int i = 0; i < longLineCount; i ++) {

           for (int j = 0; j < minuteMark; j ++) {

              g.drawLine(x, y, x, y + shortLen);

              x = x + minuteMarkSpace;

           }

       }

    }

    @Override

    protected void drawWeekRuler(Graphics g) {

       // 一共能画多少格刻度

       markCount = (bounds.width - x_pos) / weekMarkSpace;

       //比例

       //scale = timeValue / markCount + 1;

       int x = bounds.x + x_pos;

       int y = bounds.y;

       // 要画多少长刻度

       int longLineCount = markCount / weekMark + 1;

       //画长刻度线

       g.setLineWidth(2);

       for (int i = 0; i < longLineCount; i++) {

           g.drawLine(x, y, x, y + longLen);

 

           String s = String.valueOf(i) + getTypeString();

           if (s.startsWith("0")) {

              g.drawString(s, x + 2, y + 15);

           } else {

              Dimension fsize = FigureUtilities

                     .getStringExtents(s, getFont());

              g.drawString(s, x - (fsize.width / 2), y + 15);

           }

           x = x + (weekMarkSpace * weekMark);

       }

       g.setLineWidth(1);

       //重新设定x坐标起点

       x = bounds.x + x_pos;

       //画短刻度线

       for (int i = 0; i < longLineCount; i ++) {

           for (int j = 0; j < weekMark; j ++) {

              g.drawLine(x, y, x, y + shortLen);

              x = x + weekMarkSpace;

           }

       }

    }

    private String getTypeString() {

       String msg = "";

       switch (timeType) {

       case MINUTE:

           msg = "分钟";

           break;

       case HOUR:

           msg = "小时";

           break;

       case DAY:

           msg = "";

           break;

       case WEEK:

           msg = "星期";

           break;

       }

       return msg;

    }

}

 

===================================================================

TimeProcessorRuler.class显示任务总时间的长度的图形,当任务进度条的长度和其相等时,表示任务的执行已经完成。

===================================================================

/**

 * Copyright (C) 2006 - 2008

 */

package draw2d.example.draw2d;

 

import org.eclipse.draw2d.Graphics;

 

import draw2d.example.entity.SActivity;

 

/**

 * 显示任务总时间的长度的图形,当任务进度条的长度和其相等时,表示任务的执行已经完成。

 * @author 严军

 * @create 2008-4-23 下午05:06:09

 * @version v1.0

 */

public class TimeProcessorRuler extends AbstractProcessorFigure {

    public TimeProcessorRuler(SActivity activity) {

       super(activity);

    }

 

    @Override

    protected void paintFigure(Graphics g) {

       super.paintFigure(g);

       g.setLineWidth(2);

       g.drawRectangle(bounds);

       g.setLineWidth(1);

      

       switch (timeType) {

       case WEEK:

           drawWeekRuler(g);

           break;

       case DAY:

           drawDayRuler(g);

           break;

       case HOUR:

           drawHourRuler(g);

           break;

       case MINUTE:

           drawMinuteRuler(g);

           break;

       }

    }

}

 

===================================================================

TimeProcessor.class任务执行时的进度条,根据不同的任务状态来控制进度条的背景色,提供了ToolTip来显示子任务的状态。

===================================================================

/**

 * Copyright (C) 2006 - 2008

 */

package draw2d.example.draw2d;

 

import org.eclipse.draw2d.Graphics;

import org.eclipse.draw2d.MouseListener;

import org.eclipse.jface.dialogs.MessageDialog;

import org.eclipse.swt.graphics.Color;

 

import draw2d.example.entity.SActivity;

 

/**

 * 任务执行时的进度条,根据不同的任务状态来控制进度条的背景色,提供了ToolTip来显示子任务的状态

 * @author 严军

 * @create 2008-4-22 下午05:49:13

 * @version v1.0

 */

public class TimeProcessor extends AbstractProcessorFigure implements ColorConstants {

   

    private MouseListener listener = new MouseListener(){

 

       public void mouseDoubleClicked(org.eclipse.draw2d.MouseEvent me) {

           MessageDialog.openInformation(null, "提示", "新东西!");

       }

 

       public void mousePressed(org.eclipse.draw2d.MouseEvent me) {

          

       }

 

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

          

       }

    };

   

    public TimeProcessor(SActivity activity) {

       super(activity);

       setToolTip(new TaskToolTip());

       addMouseListener(listener);

    }

   

    @Override

    public void paintFigure(Graphics g) {

       super.paintFigure(g);

       //绘制渐变背景色

        Color oldForeground = g.getForegroundColor();

        Color oldBackground = g.getBackgroundColor();

        g.setForegroundColor(foregroundColor);

        g.setBackgroundColor(backgroundColor);

        g.fillGradient(bounds, true);

        g.setForegroundColor(oldForeground);

        g.setBackgroundColor(oldBackground);

        g.setLineWidth(2);

        g.drawRectangle(bounds);

      

       switch (timeType) {

       case WEEK:

           drawWeekRuler(g);

           break;

       case DAY:

           drawDayRuler(g);

           break;

       case HOUR:

           drawHourRuler(g);

           break;

       case MINUTE:

           drawMinuteRuler(g);

           break;

       }

    }

}

 

 

 

===================================================================

TaskToolTip.class一个显示子任务的图形,作为TimeProcessorToolTip

===================================================================

 

 

/**

 * Copyright (C) 2006 - 2008

 */

package draw2d.example.draw2d;

 

import java.util.ArrayList;

import java.util.List;

 

import org.eclipse.draw2d.ColorConstants;

import org.eclipse.draw2d.Label;

import org.eclipse.draw2d.PositionConstants;

import org.eclipse.draw2d.RectangleFigure;

import org.eclipse.draw2d.XYLayout;

import org.eclipse.draw2d.geometry.Rectangle;

 

import draw2d.example.Images;

import draw2d.example.entity.SSubActivity;

 

/**

 * 一个显示子任务的图形,作为TimeProcessorToolTip

 * @author 严军

 * @create 2008-4-25 上午10:10:36

 * @version v1.0

 */

public class TaskToolTip extends RectangleFigure {

    private Label taskName = new Label("任务名称:");

    private Label taskCode = new Label("任务编号:");

    private Label taskStatus = new Label("任务状态:");

    private Label taskBeginTime = new Label("预计开始时间:");

    private Label taskEndTime = new Label("预计结束时间:");

    private Label taskSBeginTime = new Label("实际开始时间:");

    private Label taskSEndTime = new Label("实际结束时间:");

    private Label taskSubTasks = new Label("子任务状态:");

 

    private Label taskNameValue = new Label("sdfsdf");

    private Label taskCodeValue = new Label("sg=====");;

    private Label taskStatusValue = new Label("aaaaaaaaaaaaa");;

    private Label taskBeginTimeValue = new Label("444444444444");;

    private Label taskEndTimeValue = new Label("6666666666");;

    private Label taskSBeginTimeValue = new Label("dddddddddddddddddddddddddddddddddddddddddddddddd");;

    private Label taskSEndTimeValue = new Label("fffffffffffffffffffffffffffffffffffffffffffffffffffffffff");;

 

    private RectangleFigure taskSubTasksValue = new RectangleFigure();

   

    private Label[] labels = new Label[] {

       taskName, taskCode, taskStatus, taskBeginTime, taskEndTime, taskSBeginTime, taskSEndTime, taskSubTasks,

    };

    private Label[] values = new Label[] {

       taskNameValue, taskCodeValue, taskStatusValue, taskBeginTimeValue, taskEndTimeValue, taskSBeginTimeValue, taskSEndTimeValue

    };

 

    public TaskToolTip() {

       setPreferredSize(400, 300);

       setLayoutManager(new XYLayout());

 

       int y = 5;

       for (Label label : labels) {

           if (label == null)

              continue;

 

           label.setTextAlignment(PositionConstants.LEFT);

           label.setLabelAlignment(PositionConstants.LEFT);

           label.setIconAlignment(PositionConstants.LEFT);

           label.setForegroundColor(ColorConstants.blue);

           add(label);

           getLayoutManager().setConstraint(label, new Rectangle(5, y, 100, 15));

           y = y + 17;

       }

       taskStatusValue.setIcon(Images.getImage(Images.RUN_TASK));

       y = 5;

       for (Label label : values) {

           if (label == null)

              continue;

 

           label.setTextAlignment(PositionConstants.LEFT);

           label.setLabelAlignment(PositionConstants.LEFT);

           label.setIconAlignment(PositionConstants.LEFT);

           add(label);

           getLayoutManager().setConstraint(label, new Rectangle(115, y, 285, 15));

           y = y + 17;

       }

 

       y = y + 17;

       taskSubTasksValue.setLayoutManager(new XYLayout());

       add(taskSubTasksValue);

       getLayoutManager().setConstraint(taskSubTasksValue, new Rectangle(5, y, 390, 140));

 

       y = 283;

       Label message = new Label("更多信息请双击进度条进行查看...");

       message.setTextAlignment(PositionConstants.RIGHT);

       message.setLabelAlignment(PositionConstants.RIGHT);

       message.setForegroundColor(ColorConstants.blue);

       add(message);

       getLayoutManager().setConstraint(message, new Rectangle(5, y, 390, 14));

      

       y = 5;

       for (SSubActivity sub : subs) {

           Label label = new Label(sub.getName() == null ? "" : sub.getName());

           label.setTextAlignment(PositionConstants.LEFT);

           label.setLabelAlignment(PositionConstants.LEFT);

           label.setIconAlignment(PositionConstants.LEFT);

           taskSubTasksValue.add(label);

           taskSubTasksValue.getLayoutManager().setConstraint(label, new Rectangle(5, y, 230, 15));

          

           label = new Label(Images.getImage(Images.RUN_TASK));

           label.setText("正在执行中...");

           label.setTextAlignment(PositionConstants.LEFT);

           label.setLabelAlignment(PositionConstants.LEFT);

           label.setIconAlignment(PositionConstants.LEFT);

           taskSubTasksValue.add(label);

           taskSubTasksValue.getLayoutManager().setConstraint(label, new Rectangle(240, y, 150, 15));

           y = y + 17;

       }

      

    }

   

    //测试数据-----------------------------------------------

    private List<SSubActivity> subs = new ArrayList<SSubActivity>();

   

    {

       SSubActivity activity = new SSubActivity();

       activity.setName("子任务");

       activity.setStatu(SSubActivity.DOING);

       subs.add(activity);

       activity = new SSubActivity();

       activity.setName("子任务1");

       activity.setStatu(SSubActivity.FINISH);

       subs.add(activity);

       activity = new SSubActivity();

       activity.setName("子任务2");

       activity.setStatu(SSubActivity.PAUSE);

       subs.add(activity);

       activity = new SSubActivity();

       activity.setName("子任务3");

       activity.setStatu(SSubActivity.CANCAL);

       subs.add(activity);

    }

    //测试数据-----------------------------------------------

}

 

===================================================================

Title.class显示任务名称的图形。

===================================================================

 

 

/**

 * Copyright (C) 2006 - 2008

 */

package draw2d.example.draw2d;

 

import org.eclipse.draw2d.Graphics;

import org.eclipse.draw2d.Label;

import org.eclipse.draw2d.PositionConstants;

 

import draw2d.example.Images;

 

 

/**

 * 显示任务名称的图形

 * @author 严军

 * @create 2008-4-22 下午12:11:17

 * @version v1.0

 */

public class Title extends Label {

    private String title;

   

    public Title(String title) {

       this.title = title;

       setTextAlignment(PositionConstants.LEFT);

       setLabelAlignment(PositionConstants.LEFT);

       setIconAlignment(PositionConstants.LEFT);

       setText(title);

       setToolTip(new Label(title));

       setIcon(Images.getImage(Images.RUN_TASK));

    }

   

    @Override

    public void paintFigure(Graphics g) {

       super.paintFigure(g);

       g.setLineWidth(2);

       g.drawRectangle(bounds);

    }

 

    public String getTitle() {

       return title;

    }

 

    public void setTitle(String title) {

       this.title = title;

    }

}

 

 

 

以上几个类是实现图形的核心代码,下面是在视图中进行显示的控制的类:

package draw2d.example;

 

import java.util.Date;

import java.util.List;

 

import org.eclipse.draw2d.Figure;

import org.eclipse.draw2d.FigureCanvas;

import org.eclipse.draw2d.ScrollPane;

import org.eclipse.draw2d.ToolbarLayout;

import org.eclipse.draw2d.Viewport;

import org.eclipse.draw2d.XYLayout;

import org.eclipse.draw2d.geometry.Rectangle;

import org.eclipse.jface.action.ControlContribution;

import org.eclipse.jface.action.IMenuManager;

import org.eclipse.jface.action.IToolBarManager;

import org.eclipse.swt.SWT;

import org.eclipse.swt.events.SelectionAdapter;

import org.eclipse.swt.events.SelectionEvent;

import org.eclipse.swt.layout.FillLayout;

import org.eclipse.swt.layout.GridLayout;

import org.eclipse.swt.widgets.Combo;

import org.eclipse.swt.widgets.Composite;

import org.eclipse.swt.widgets.Control;

import org.eclipse.swt.widgets.Label;

import org.eclipse.swt.widgets.Shell;

import org.eclipse.ui.part.ViewPart;

 

import draw2d.example.draw2d.AbstractProcessorFigure;

import draw2d.example.draw2d.ColorConstants;

import draw2d.example.draw2d.TimeProcessor;

import draw2d.example.draw2d.TimeProcessorRuler;

import draw2d.example.draw2d.TimeRuler;

import draw2d.example.draw2d.Title;

import draw2d.example.draw2d.Util;

import draw2d.example.entity.SActivity;

 

 

/**

 * 任务执行状态监控

 * @author 严军

 * @createBy 2008-4-2 下午03:47:22

 * @version 1.0

 */

public class MonitoringTaskView extends ViewPart implements ColorConstants {

    private static ScrollPane scrollPane;

    private static Figure monitor;

    private static Figure tasksContainer;

    private static TimeRuler timeRuler;

    private static int y = 0;

    private static int type = AbstractProcessorFigure.HOUR;

    private Composite container;

   

    public MonitoringTaskView() {

       monitor = new Figure();

       timeRuler = new TimeRuler();

       tasksContainer = new Figure();

       scrollPane = new ScrollPane();

       scrollPane.setViewport(new Viewport(true));

       scrollPane.setScrollBarVisibility(ScrollPane.NEVER);

      

       ToolbarLayout layout = new ToolbarLayout();

       layout.setSpacing(5);

       monitor.setLayoutManager(layout);

       tasksContainer.setLayoutManager(new XYLayout());

       //tasksContainer.setLayoutManager(layout);

 

       monitor.add(timeRuler);

       monitor.add(tasksContainer);

      

    }

   

   

    @Override

    public void createPartControl(Composite parent) {

       container = new Composite(parent, SWT.NONE);

       container.setLayout(new FillLayout());

       FigureCanvas canvas = new FigureCanvas(container);

 

       y = 0;

       //测试数据---------------------------------------

       SActivity activity = new SActivity();

       activity.setId(1l);

       activity.setCode(1);

       activity.setStatu(SActivity.CANCAL);

       activity.setBeginTime(new Date(2008 - 1900,3,24,0,0,0));

       activity.setEndTime(new Date(2008 - 1900,3,24,18,0,0));

       activity.setName("任务1");     

       addFigure(activity);

      

       activity = new SActivity();

       activity.setId(1l);

       activity.setCode(12);

       activity.setStatu(SActivity.PAUSE);

       activity.setBeginTime(new Date(2008 - 1900,3,24,8,0,0));

       activity.setEndTime(new Date(2008 - 1900,3,24,20,0,0));

       activity.setName("任务2");     

       addFigure(activity);

      

       activity = new SActivity();

       activity.setId(1l);

       activity.setCode(31);

       activity.setStatu(SActivity.OUTTIME);

       activity.setBeginTime(new Date(2008 - 1900,3,24,0,0,0));

       activity.setEndTime(new Date(2008 - 1900,3,24,19,0,0));

       activity.setName("任务3");     

       addFigure(activity);

      

       activity = new SActivity();

       activity.setId(1l);

       activity.setCode(10);

       activity.setStatu(SActivity.DOING);

       activity.setBeginTime(new Date(2008 - 1900,3,25,8,57,0));

       activity.setEndTime(new Date(2008 - 1900,3,25,8,59,0));

       activity.setName("任务4");     

       addFigure(activity);

       //------------------------------------------------

      

       scrollPane.setContents(monitor);

       canvas.setContents(scrollPane);

      

       //

       createActions();

       initializeToolBar();

       initializeMenu();

       //重绘图的线程

       new PaintFigure(container).run();

       //加载数据的线程

       new ReloadActivity(container).run();

    }

   

    //线程-------------------------------------------------

    /**

     * 加载数据的线程,每隔一段时间去读取一次数据库,有新的任务就会在下一次重绘图形时显示在进度条中

     */

    private class ReloadActivity implements Runnable {

       private Control parent;

       public ReloadActivity(Control parent) {

           this.parent = parent;

       }

      

       public void run() {

           if (!parent.isDisposed()) {

              //每秒执行一次

              parent.getDisplay().timerExec(1000, this);

           }

       }

      

    }

    /**

     * 重绘图形的线程,并加入新的任务

     * @author 严军

     * @create 2008-4-28 下午01:59:13

     * @version v1.0

     */

    private class PaintFigure implements Runnable {

       private Control parent;

       public PaintFigure(Control parent) {

           this.parent = parent;

       }

      

       public void run() {

           if (!parent.isDisposed()) {

              changedFigure();

              monitor.repaint();

              //每秒执行一次

              parent.getDisplay().timerExec(1000, this);

           }

       }

      

    }

    //显示控制---------------------------------------------

    /**

     * 根据任务的情况来控制进度条及其状态

     */

    @SuppressWarnings("unchecked")

    private void changedFigure() {

       if (tasksContainer != null) {

           List figures = tasksContainer.getChildren();

           if (figures.isEmpty()) {

              return;

           }

           int size = figures.size();

           for (int i = 0; i < size; i ++) {

              Object obj = figures.get(i);

              if (obj instanceof TimeProcessorRuler) {

                 

              } else if (obj instanceof TimeProcessor) {

                  AbstractProcessorFigure figure = (AbstractProcessorFigure) obj;

                  SActivity activity = figure.getActivity();

                  Date now = new Date();

                  figure.setTimeValue(Util.getTimeValue(activity.getBeginTime(), activity.getEndTime()));

                  final int status = activity.getStatu();

                  if (status == SActivity.WAIT) {

                     figure.setBackgroundColor(taskWaitingBackgroundColor);

                     figure.setForegroundColor(taskWaitingForegroundColor);

                     continue;

                  }

                  if (status == SActivity.CANCAL) {

                     figure.setBackgroundColor(taskCanceledBackgroundColor);

                     figure.setForegroundColor(taskCanceledForegroundColor);

                     continue;

                  }

                  if (status == SActivity.DOING) {

                     figure.setBackgroundColor(taskDoingBackgroundColor);

                     figure.setForegroundColor(taskDoingForegroundColor);

                  }

                  if (status == SActivity.FINISH) {

                     figure.setBackgroundColor(taskFinishedBackgroundColor);

                     figure.setForegroundColor(taskFinishedForegroundColor);

                     continue;

                  }

                  if (status == SActivity.OUTTIME) {

                     figure.setBackgroundColor(taskOuttimedBackgroundColor);

                     figure.setForegroundColor(taskOuttimedForegroundColor);

                     continue;

                  }

                  if (status == SActivity.PAUSE) {

                     figure.setBackgroundColor(taskPausedBackgroundColor);

                     figure.setForegroundColor(taskPausedForegroundColor);

                     continue;

                  }

                  int time = Util.getTimeValue(activity.getBeginTime(), now);

                  figure.setTimeValue(time);

              }

           }

       }

    }

    /**

     * 要显示的时间类型改变时,重画所有的图形

     */

    @SuppressWarnings("unchecked")

    private void timeTypeChanged() {

       changeTimeType();

       timeRuler.setTimeType(type);

       monitor.repaint();

       if (tasksContainer != null) {

           List figures = tasksContainer.getChildren();

           if (figures.isEmpty()) {

              return;

           }

           int size = figures.size();

           for (int i = 0; i < size; i ++) {

              Object obj = figures.get(i);

              if (obj instanceof AbstractProcessorFigure) {

                  ((AbstractProcessorFigure) obj).setTimeType(type);

              }

           }

       }

    }

    /**

     * 取出时间类型

     */

    private void changeTimeType() {

       String text = cmbTimes.getText();

       if (AbstractProcessorFigure.week.equals(text)) {

           type = AbstractProcessorFigure.WEEK;

       }

       if (AbstractProcessorFigure.day.equals(text)) {

           type = AbstractProcessorFigure.DAY;

       }

       if (AbstractProcessorFigure.hour.equals(text)) {

           type = AbstractProcessorFigure.HOUR;

       }

       if (AbstractProcessorFigure.minute.equals(text)) {

           type = AbstractProcessorFigure.MINUTE;

       }

    }

   

    //Draw2D----------------------------------------------

    /**

     * 增加新的任务,并在图形上显示

     */

    private void addFigure(SActivity activity) {

       if (activity == null || activity.getId() == null || activity.getBeginTime() == null || activity.getEndTime() == null || activity.getStatu() == SActivity.WAIT) return;

      

       Title title = new Title(activity.getName() == null ? "" : activity.getName());

       tasksContainer.add(title);

       tasksContainer.getLayoutManager().setConstraint(title, new Rectangle(0, y , 98, 20));

 

       TimeProcessorRuler processorRuler = new TimeProcessorRuler(activity);

       processorRuler.setTimeValue(Util.getTimeValue(activity.getBeginTime(), activity.getEndTime()));

       tasksContainer.add(processorRuler);

       tasksContainer.getLayoutManager().setConstraint(processorRuler, new Rectangle(100, y, processorRuler.getTimeValue(), 20));

       //tasksContainer.getLayoutManager().setConstraint(processorRuler, new Rectangle(100, y, 0, 20));

 

       TimeProcessor processor = new TimeProcessor(activity);

       processor.setTimeValue(Util.getTimeValue(activity.getBeginTime(), activity.getEndTime()));

       tasksContainer.add(processor);

       tasksContainer.getLayoutManager().setConstraint(processor, new Rectangle(100, y, 0, 20));

 

       y = y + 22;

    }

   

    //事件监听器-------------------------------------------

    private SelectionAdapter selectionAdapter = new SelectionAdapter() {

       @Override

       public void widgetSelected(final SelectionEvent e) {

           e.display.asyncExec(new Runnable(){

              public void run() {

                  if (e.widget == cmbTimes) {

                     timeTypeChanged();

                     return;

                  }

                  if (e.widget == cmbFilters) {

                     return;

                  }                

              }

           });

       }

    };

    //Actions-------------------------------------------

    private Combo cmbTimes;

    private Combo cmbFilters;

   

    private class ComboItem extends ControlContribution {

       private Control control;

       public ComboItem(String id, Control control) {

           super(id);

           this.control = control;

       }

       @Override

       protected Control createControl(Composite parent) {

           control.setParent(parent);

           return control;

       }

      

    }

 

    private ControlContribution timeType;

    private ControlContribution filterType;

    /**

     * Create the actions

     */

    private void createActions() {

       Composite cmpTime = new Composite(new Shell(), SWT.NONE);

       GridLayout gridLayout = new GridLayout(2, false);

       gridLayout.verticalSpacing = 0;

       gridLayout.marginWidth = 0;

       gridLayout.marginHeight = 0;

       gridLayout.horizontalSpacing = 0;

       cmpTime.setLayout(gridLayout);

       Label label = new Label(cmpTime, SWT.NONE);

       label.setText("时间格式:");

       label.setAlignment(SWT.CENTER);

       cmbTimes = new Combo(cmpTime, SWT.READ_ONLY);

       cmbTimes.add(AbstractProcessorFigure.week);

       cmbTimes.add(AbstractProcessorFigure.day);

       cmbTimes.add(AbstractProcessorFigure.hour);

       cmbTimes.add(AbstractProcessorFigure.minute);

       cmbTimes.select(2);

       timeType = new ComboItem("TimeType", cmpTime);

      

       Composite cmpFilter = new Composite(new Shell(), SWT.NONE);

       cmpFilter.setLayout(gridLayout);

       label = new Label(cmpFilter, SWT.NONE);

       label.setText("    任务状态:");

       label.setAlignment(SWT.CENTER);

       cmbFilters = new Combo(cmpFilter, SWT.READ_ONLY);

       cmbFilters.add(AbstractProcessorFigure.all);

       cmbFilters.add(AbstractProcessorFigure.doing);

       cmbFilters.add(AbstractProcessorFigure.canceled);

       cmbFilters.add(AbstractProcessorFigure.waiting);

       cmbFilters.add(AbstractProcessorFigure.finished);

       cmbFilters.add(AbstractProcessorFigure.timeouted);

       cmbFilters.setVisibleItemCount(10);

       cmbFilters.select(0);

       filterType = new ComboItem("FilterType", cmpFilter);

 

       cmbTimes.addSelectionListener(selectionAdapter);

       cmbFilters.addSelectionListener(selectionAdapter);

    }

 

    /**

     * Initialize the toolbar

     */

    private void initializeToolBar() {

       IToolBarManager toolbarManager = getViewSite().getActionBars().getToolBarManager();

       toolbarManager.add(timeType);

       toolbarManager.add(filterType);

    }

 

    /**

     * Initialize the menu

     */

    private void initializeMenu() {

       IMenuManager menuManager = getViewSite().getActionBars()

              .getMenuManager();

    }

 

    @Override

    public void setFocus() {

       if (!container.isDisposed())

           container.setFocus();

    }

}

 

 

 

在这里面只实现了按时间类型来显示图形,按任务状态过滤功能还没有实现,运行效果如下:

 

按星期显示

按天显示

 

 

 

 

 按小时显示

 

 

按分钟显示

 

ToolTip显示子任务

 

上面只是实现了主要的功能,细节部分还有许多地方要完善的。

2008-04-28

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值