Source code: com/memoire/bu/BuTaskView.java
1 /**
2 * @modification $Date: 2002/12/16 18:56:25 $
3 * @statut unstable
4 * @file BuTaskView.java
5 * @version 0.36
6 * @author Guillaume Desnoix
7 * @email guillaume@desnoix.com
8 * @license GNU General Public License 2 (GPL2)
9 * @copyright 1998-2001 Guillaume Desnoix
10 */
11
12 package com.memoire.bu;
13
14 import com.memoire.bu.*;
15 import javax.swing.*;
16 import java.awt.*;
17 import java.io.*;
18 import java.util.*;
19
20 /**
21 * A simple list to display running tasks.
22 */
23 public class BuTaskView
24 extends BuEmptyList
25 {
26 DefaultListModel model_;
27
28 public BuTaskView()
29 {
30 super();
31 setName("buTASKVIEW");
32 model_=new DefaultListModel();
33 setModel(model_);
34 setEmptyText(BuResource.BU.getString("Aucune tāche en cours"));
35 setCellRenderer(new XCR());
36 // setFont(BuLib.deriveFont("List",Font.PLAIN,-2));
37 }
38
39 public void setFont(Font _f)
40 {
41 super.setFont(getETF());
42 }
43
44 public void addTask(BuTask _t)
45 {
46 model_.addElement(_t);
47 revalidate();
48 repaint(200);
49 }
50
51 public void removeTask(BuTask _t)
52 {
53 model_.removeElement(_t);
54 revalidate();
55 repaint(200);
56 }
57
58 private static final class Cell extends BuLabel
59 {
60 private BuTask task_;
61
62 public Cell(BuTask _task)
63 {
64 super();
65
66 task_=_task;
67
68 String p;
69 switch(task_.getPriority())
70 {
71 case Thread.MIN_PRIORITY: p="-"; break;
72 case Thread.MIN_PRIORITY+1: p="-"; break;
73 case Thread.NORM_PRIORITY-1: p="="; break;
74 case Thread.NORM_PRIORITY: p="="; break;
75 case Thread.MAX_PRIORITY: p="+"; break;
76 default: p="?"; break;
77 }
78 if(task_.isInterrupted()) p="~";
79 if(!task_.isAlive()) p="!";
80
81 setText(" "+p+" "+task_.toString());
82 }
83
84 /*
85 public boolean isOpaque()
86 {
87 return false;
88 }
89 */
90
91 public final void paintComponent(Graphics _g)
92 {
93 //super.paintComponent(_g);
94
95 Insets insets=this.getInsets();
96 Color fg=UIManager.getColor("ProgressBar.foreground");
97 Color bg=UIManager.getColor("ProgressBar.background");
98 if(fg==null) fg=this.getForeground();
99 if(bg==null) bg=this.getBackground();
100
101 int v=task_.getProgression();
102 int vmin=0;
103 int vmax=100;
104 String s=this.getText();
105 int xs=insets.left;
106 int ys=insets.top+this.getFont().getSize();
107
108 int wb=this.getWidth ()-insets.left-insets.right;
109 int hb=this.getHeight()-insets.top-insets.bottom;
110 int xb=(v-vmin)*wb/(vmax-vmin);
111
112 Shape old=_g.getClip();
113 _g.clipRect(insets.left+xb,insets.top,wb-xb,hb);
114 if(isOpaque())
115 {
116 Rectangle r=_g.getClipBounds();
117 _g.setColor(bg);
118 _g.fillRect(r.x,r.y,r.width,r.height);
119 }
120
121 Color xfg=UIManager.getColor("ProgressBar.selectionForeground");
122 if(xfg==null) xfg=fg;
123 _g.setColor(xfg);
124 _g.drawString(s,xs,ys);
125 _g.setClip(old);
126
127 _g.clipRect(insets.left,insets.top,xb,hb);
128 {
129 Rectangle r=_g.getClipBounds();
130 _g.setColor(fg);
131 _g.fillRect(r.x,r.y,r.width,r.height);
132 }
133
134 Color xbg=UIManager.getColor("ProgressBar.selectionBackground");
135 if(xbg==null) xbg=bg;
136 _g.setColor(xbg);
137 _g.drawString(s,xs+1,ys);
138 _g.setClip(old);
139 }
140
141 /*
142 public void paint(Graphics _g)
143 {
144 super.paint(_g);
145
146 Dimension size=this.getSize();
147 int w=task_.getProgression()*size.width/100;
148
149 Color c=UIManager.getColor("ProgressBar.foreground");
150 if(c==null) c=this.getForeground();
151 _g.setXORMode(c);
152 _g.setColor(this.getBackground());
153
154 int cl=1,cs=1;
155 try
156 {
157 cl=((Integer)UIManager.get("ProgressBar.cellLength")).intValue();
158 cs=((Integer)UIManager.get("ProgressBar.cellSpacing")).intValue();
159 }
160 catch(Exception ex) { }
161
162 if(cl==1)
163 _g.fillRect(0,0,w,size.height);
164 else
165 for(int x=0;x+cl<=w;x+=cl+cs+1)
166 _g.fillRect(x,0,cl+1,size.height);
167
168 if((cs==0)&&(BuLib.jdk()<1.2))
169 {
170 Rectangle old=_g.getClipBounds();
171 _g.setClip(0,0,w,size.height);
172 super.paint(_g);
173 _g.setClip(old);
174 _g.setPaintMode();
175 }
176 else
177 {
178 _g.setPaintMode();
179 super.paint(_g);
180 }
181 }
182 */
183 }
184
185 private static final class XCR
186 implements ListCellRenderer, Serializable
187 {
188 public final Component getListCellRendererComponent
189 (JList _list, Object _value, int _index, boolean _selected, boolean _cellHasFocus)
190 {
191 Cell r=new Cell((BuTask)_value);
192
193 r.setFont(_list.getFont());
194 r.setBackground(_list.getBackground());
195 r.setForeground(_list.getForeground());
196
197 return r;
198 }
199 }
200 }