1 | /*
|
---|
2 | * Copyright 2005 Paul Hinds
|
---|
3 | *
|
---|
4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
5 | * you may not use this file except in compliance with the License.
|
---|
6 | * You may obtain a copy of the License at
|
---|
7 | *
|
---|
8 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
9 | *
|
---|
10 | * Unless required by applicable law or agreed to in writing, software
|
---|
11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
13 | * See the License for the specific language governing permissions and
|
---|
14 | * limitations under the License.
|
---|
15 | */
|
---|
16 | package org.tp23.antinstaller.renderer.swing;
|
---|
17 |
|
---|
18 | import java.awt.Color;
|
---|
19 | import java.awt.Dimension;
|
---|
20 | import java.awt.Font;
|
---|
21 | import java.awt.Graphics;
|
---|
22 | import java.awt.Graphics2D;
|
---|
23 | import java.awt.Rectangle;
|
---|
24 | import java.awt.RenderingHints;
|
---|
25 | import java.util.ArrayList;
|
---|
26 | import java.util.Iterator;
|
---|
27 | import java.util.List;
|
---|
28 |
|
---|
29 | import javax.swing.JPanel;
|
---|
30 | import javax.swing.Scrollable;
|
---|
31 |
|
---|
32 | import org.apache.tools.ant.BuildEvent;
|
---|
33 | import org.apache.tools.ant.Target;
|
---|
34 | import org.tp23.antinstaller.InstallerContext;
|
---|
35 |
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * Progress panel prints a graphical view of the progress of the Ant targets
|
---|
39 | * being run. It supports displaying one layer of dependent targets.
|
---|
40 | * @author Paul Hinds
|
---|
41 | * @version $Id: ProgressPanel.java,v 1.5 2006/12/21 00:02:59 teknopaul Exp $
|
---|
42 | */
|
---|
43 | public class ProgressPanel extends JPanel implements Scrollable{
|
---|
44 |
|
---|
45 | public static final int tHeight = 19;
|
---|
46 | public static final int leftIndent = 15;
|
---|
47 | public static final int DONE = 0;
|
---|
48 | public static final int INPROGRESS = 1;
|
---|
49 | public static final int TODO = 2;
|
---|
50 | private static final Color progressColor = new Color(0, 125, 0);
|
---|
51 | private static final Font mainFont = new Font("Dialog", Font.PLAIN, 11);
|
---|
52 | private static final Font subFont = new Font("Dialog", Font.PLAIN, 10);
|
---|
53 | List targets = null;
|
---|
54 |
|
---|
55 | private final InstallerContext ctx;
|
---|
56 | private int mainTargetPos = 0;
|
---|
57 | private ProgressModel currentPM = null;
|
---|
58 |
|
---|
59 | public ProgressPanel(InstallerContext ctx) {
|
---|
60 | super(true);
|
---|
61 | this.ctx = ctx;
|
---|
62 | }
|
---|
63 |
|
---|
64 | public synchronized void prepareCalledTargets(){
|
---|
65 | List targetStrings = ctx.getInstaller().getTargets(ctx);
|
---|
66 | Iterator iter = targetStrings.iterator();
|
---|
67 | targets = new ArrayList();
|
---|
68 | while (iter.hasNext()) {
|
---|
69 | String tgt = (String) iter.next();
|
---|
70 | targets.add(new ProgressModel(tgt));
|
---|
71 | }
|
---|
72 | this.setSize(getSize());// panel size changes
|
---|
73 | revalidate();
|
---|
74 | repaint();
|
---|
75 | }
|
---|
76 | /**
|
---|
77 | * This method assumes that we are send target started methods in order
|
---|
78 | * but that we do not have the information about "depends" targets and have to
|
---|
79 | * insert the information as it arrives. If a TargetStarted event arrives that
|
---|
80 | * is not the expected target is is assumed to be a depends.
|
---|
81 | * @param buildEvent
|
---|
82 | */
|
---|
83 | public synchronized void targetStarted(BuildEvent buildEvent){
|
---|
84 | try {
|
---|
85 | Target tgt = buildEvent.getTarget();
|
---|
86 | ProgressModel pm = (ProgressModel)targets.get(mainTargetPos);
|
---|
87 | pm.state = INPROGRESS;
|
---|
88 | if(tgt.getName().equals(pm.name)){
|
---|
89 | // main target
|
---|
90 | currentPM = pm;
|
---|
91 | mainTargetPos++;
|
---|
92 | revalidate();
|
---|
93 | repaint();
|
---|
94 | }
|
---|
95 | else {
|
---|
96 | //dependency
|
---|
97 | ProgressModel dependency = new ProgressModel(tgt.getName());
|
---|
98 | dependency.state = INPROGRESS;
|
---|
99 | if(currentPM != null){
|
---|
100 | currentPM.state = DONE;// this to catch antcall strangenesses
|
---|
101 | }
|
---|
102 | currentPM = dependency;
|
---|
103 | pm.subTargets.add(dependency);
|
---|
104 | this.setSize(getSize());// panel size changes
|
---|
105 | revalidate();
|
---|
106 | repaint();
|
---|
107 | }
|
---|
108 | }
|
---|
109 | catch (Exception e) {
|
---|
110 | ctx.log(e);
|
---|
111 | }
|
---|
112 | }
|
---|
113 | public synchronized void targetFinished() {
|
---|
114 | // BUG 1494105 reports NPE here, looks like no targets specified in his antinstaller-config.xml
|
---|
115 | currentPM.state = DONE;
|
---|
116 | }
|
---|
117 |
|
---|
118 | /**
|
---|
119 | * N.B. buildFinished must be fired manually in Ant
|
---|
120 | */
|
---|
121 | public synchronized void buildFinished() {
|
---|
122 | // this is done because antcall sometimes results in targetFinished not being called
|
---|
123 | setToDone(targets);
|
---|
124 | repaint();
|
---|
125 | }
|
---|
126 |
|
---|
127 | private void setToDone(List pModels){
|
---|
128 | if(pModels == null || pModels.size() == 0){
|
---|
129 | return;
|
---|
130 | }
|
---|
131 | Iterator iter = pModels.iterator();
|
---|
132 | while (iter.hasNext()) {
|
---|
133 | ProgressModel pm = (ProgressModel)iter.next();
|
---|
134 | pm.state = DONE;
|
---|
135 | setToDone(pm.subTargets);
|
---|
136 | }
|
---|
137 | }
|
---|
138 | /**
|
---|
139 | */
|
---|
140 | public synchronized void paintComponent(Graphics g){
|
---|
141 | super.paintComponent(g);
|
---|
142 | if(targets == null){
|
---|
143 | return;
|
---|
144 | }
|
---|
145 | // g.setColor(getBackground());
|
---|
146 | // g.fillRect(g.getClipBounds().x,
|
---|
147 | // g.getClipBounds().y,
|
---|
148 | // g.getClipBounds().width,
|
---|
149 | // g.getClipBounds().height);
|
---|
150 | Iterator iter = targets.iterator();
|
---|
151 | int offset = 0;
|
---|
152 | for(int i = 1; iter.hasNext(); i++){
|
---|
153 | ProgressModel pmodel = (ProgressModel)iter.next();
|
---|
154 | drawTarget(pmodel, (Graphics2D)g, offset, i < targets.size(), i > 1);
|
---|
155 | offset += tHeight;
|
---|
156 | offset += pmodel.subTargets.size() * tHeight;
|
---|
157 | }
|
---|
158 | }
|
---|
159 |
|
---|
160 | public synchronized Dimension getPreferredSize(){
|
---|
161 | return getSize();
|
---|
162 | }
|
---|
163 | /**
|
---|
164 | * @see java.awt.Component#getSize()
|
---|
165 | */
|
---|
166 | public synchronized Dimension getSize() {
|
---|
167 | if(targets == null){
|
---|
168 | return new Dimension(SizeConstants.PAGE_WIDTH, tHeight * 5);
|
---|
169 | }
|
---|
170 | int count = targets.size();
|
---|
171 | Iterator iter = targets.iterator();
|
---|
172 | while(iter.hasNext()){
|
---|
173 | ProgressModel pmodel = (ProgressModel)iter.next();
|
---|
174 | count += pmodel.subTargets.size();
|
---|
175 | }
|
---|
176 | return new Dimension(SizeConstants.PAGE_WIDTH, count * tHeight);
|
---|
177 | }
|
---|
178 | /**
|
---|
179 | *
|
---|
180 | * @param target the main target to be rendered
|
---|
181 | * @param g
|
---|
182 | * @param yOffset the vertical offset where this target should be drawn
|
---|
183 | * @param hasMore if false this target is the last in the list
|
---|
184 | * @param hasPrev if false this target is the first in the list
|
---|
185 | */
|
---|
186 | private void drawTarget(ProgressModel target, Graphics2D g,
|
---|
187 | int yOffset, boolean hasMore, boolean hasPrev){
|
---|
188 | g.setFont(mainFont);
|
---|
189 | g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
|
---|
190 | g.setColor(Color.gray);
|
---|
191 | //7 vertical line (up)
|
---|
192 | if(hasPrev){
|
---|
193 | g.drawLine(leftIndent + 8, yOffset, leftIndent + 8, yOffset + 8);
|
---|
194 | }
|
---|
195 | //7 vertical line (down)
|
---|
196 | if(hasMore || target.subTargets.size() > 0){
|
---|
197 | g.drawLine(leftIndent + 8, (tHeight/2) + yOffset,
|
---|
198 | leftIndent + 8, tHeight + yOffset);
|
---|
199 | }
|
---|
200 | // sideways line
|
---|
201 | int xOffset = 0;
|
---|
202 | g.drawLine(leftIndent + 8, 8 + yOffset, leftIndent + xOffset + 20, 8 + yOffset);
|
---|
203 | if(target.state == DONE){
|
---|
204 | g.setColor(Color.darkGray);
|
---|
205 | }
|
---|
206 | if(target.state == INPROGRESS){
|
---|
207 | g.setColor(progressColor);
|
---|
208 | }
|
---|
209 | if(target.state == TODO){
|
---|
210 | g.setColor(Color.gray);
|
---|
211 | }
|
---|
212 | g.fillRoundRect(leftIndent + xOffset + 3, yOffset + 4, 11, 9, 7, 7);
|
---|
213 | g.setColor(Color.black);
|
---|
214 | g.drawString(target.name, leftIndent + xOffset + 22, 13 + yOffset);
|
---|
215 | if(target.subTargets.size() > 0){
|
---|
216 | Iterator iter = target.subTargets.iterator();
|
---|
217 | for(int i = 1; iter.hasNext(); i++){
|
---|
218 | drawSubTarget((ProgressModel)iter.next(), g,
|
---|
219 | yOffset += tHeight, hasMore | i < target.subTargets.size());
|
---|
220 | }
|
---|
221 | }
|
---|
222 | }
|
---|
223 | private void drawSubTarget(ProgressModel target, Graphics2D g,
|
---|
224 | int yOffset, boolean hasMore){
|
---|
225 | g.setFont(subFont);
|
---|
226 | g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
|
---|
227 | g.setColor(Color.gray);
|
---|
228 | //7 vertical line (up)
|
---|
229 | g.drawLine(leftIndent + 8, yOffset, leftIndent + 8, yOffset + 8);
|
---|
230 | //7 vertical line (down)
|
---|
231 | if(hasMore){
|
---|
232 | g.drawLine(leftIndent + 8, (tHeight/2) + yOffset, leftIndent + 8, tHeight + yOffset);
|
---|
233 | }
|
---|
234 | int xOffset = 15;
|
---|
235 | // sideways line
|
---|
236 | g.drawLine(leftIndent + 8, 8 + yOffset, leftIndent + xOffset + 4, 8 + yOffset);
|
---|
237 | if(target.state == DONE){
|
---|
238 | g.setColor(Color.darkGray);
|
---|
239 | }
|
---|
240 | if(target.state == INPROGRESS){
|
---|
241 | g.setColor(progressColor);
|
---|
242 | }
|
---|
243 | if(target.state == TODO){
|
---|
244 | g.setColor(Color.gray);
|
---|
245 | }
|
---|
246 | g.fillRoundRect(leftIndent + xOffset + 4, yOffset + 5, 9, 7, 7, 7);
|
---|
247 | g.setColor(Color.black);
|
---|
248 | g.drawString(target.name, leftIndent + xOffset + 15, 12 + yOffset);
|
---|
249 | }
|
---|
250 |
|
---|
251 | public synchronized Dimension getPreferredScrollableViewportSize(){
|
---|
252 | return getPreferredSize();
|
---|
253 | }
|
---|
254 | public synchronized int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction){
|
---|
255 | return tHeight;
|
---|
256 | }
|
---|
257 | public synchronized int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction){
|
---|
258 | return tHeight * 3;
|
---|
259 | }
|
---|
260 | public synchronized boolean getScrollableTracksViewportWidth(){
|
---|
261 | return true;
|
---|
262 | }
|
---|
263 | public synchronized boolean getScrollableTracksViewportHeight(){
|
---|
264 | return false;
|
---|
265 | }
|
---|
266 | public static class ProgressModel{
|
---|
267 | int state = TODO;
|
---|
268 | String name;
|
---|
269 | List subTargets = new ArrayList();
|
---|
270 |
|
---|
271 | public ProgressModel(String name){
|
---|
272 | this.name = name;
|
---|
273 | }
|
---|
274 |
|
---|
275 | int getHeight(){
|
---|
276 | return tHeight + subTargets.size() * tHeight;
|
---|
277 | }
|
---|
278 | }
|
---|
279 | }
|
---|