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 */
16package org.tp23.antinstaller.renderer.swing;
17
18import javax.swing.JFrame;
19import javax.swing.JLabel;
20import javax.swing.SwingUtilities;
21
22import org.apache.tools.ant.BuildEvent;
23import org.tp23.antinstaller.InstallerContext;
24import org.tp23.antinstaller.runtime.SwingRunner;
25
26public class SwingInstallerContext{
27
28    private static JFrame masterFrame;
29    private JLabel feedBackPanel;
30    private ProgressPanel progressPanel;
31    private InstallerContext ctx;
32
33    public SwingInstallerContext(InstallerContext ctx,
34                                 JFrame masterFrame) {
35        this.ctx = ctx;
36        SwingInstallerContext.masterFrame = masterFrame;
37    }
38
39    public JFrame getMasterFrame() {
40        return masterFrame;
41    }
42    public SwingRunner getSwingRunner() {
43        return (SwingRunner)ctx.getRunner();
44    }
45
46    public void setFeedBackLabel(JLabel feedBackPanel) {
47        this.feedBackPanel = feedBackPanel;
48    }
49    /**
50     * The progress panel is optional so not calling this method
51     * should not cause errors or NPEs
52     * @param progressPanel
53     */
54    public void setProgressPanel(ProgressPanel progressPanel) {
55        this.progressPanel = progressPanel;
56    }
57    
58    public void buildStarted(BuildEvent buildEvent) {
59        provideAntFeedBack(buildEvent.getMessage());
60        try {
61            SwingUtilities.invokeAndWait(new Runnable(){
62                public void run(){
63                    if(SwingInstallerContext.this.progressPanel != null){
64                        SwingInstallerContext.this.progressPanel.prepareCalledTargets();
65                    }
66                }
67            });
68        } catch (Exception e) { //Interrupted or InvocationTarget
69            SwingInstallerContext.this.ctx.log(e);
70        }
71    }
72    
73    public void buildFinished(BuildEvent buildEvent) {
74        if(this.progressPanel != null){
75            try {
76                SwingUtilities.invokeLater(new Runnable(){
77                    public void run(){
78                        SwingInstallerContext.this.progressPanel.buildFinished();
79                    }
80                });
81            } catch (Exception e) { //Interrupted or InvocationTarget
82                SwingInstallerContext.this.ctx.log(e);
83            }           
84        }
85    }
86    
87    public void targetStarted(BuildEvent buildEvent) {
88        TargetStarted targetStarted = new TargetStarted();
89        targetStarted.buildEvent = buildEvent;
90        try {
91            if(this.progressPanel != null){
92                //Invoke and wait used since strict ordering od started and finished is requried
93                SwingUtilities.invokeAndWait(targetStarted);
94            }
95        } catch (Exception e) { //Interrupted or InvocationTarget
96            SwingInstallerContext.this.ctx.log(e);
97        }           
98    }
99    
00    public void targetFinished(BuildEvent buildEvent) {
01        try {
02            //Invoke and wait used since strict ordering od started and finished is requried
03            SwingUtilities.invokeAndWait(new Runnable(){
04                public void run(){
05                    if(SwingInstallerContext.this.progressPanel != null){
06                        SwingInstallerContext.this.progressPanel.targetFinished();
07                    }
08                }
09            });
10        } catch (Exception e) { //Interrupted or InvocationTarget
11            SwingInstallerContext.this.ctx.log(e);
12        }
13    }
14    
15    public void provideAntFeedBack(String message){
16        // We should never have Ant running without a ProgressPane
17        // but do an if null here in case future FilterChains are different
18        ProvideAntFeedBack provideAntFeedBack = new ProvideAntFeedBack();
19        provideAntFeedBack.message = message;
20        try {
21            if(feedBackPanel != null){
22                SwingUtilities.invokeLater(provideAntFeedBack);
23            }
24        } catch (Exception e) { //Interrupted or InvocationTarget
25            SwingInstallerContext.this.ctx.log(e);
26        }
27    }
28    
29    
30    /**
31     * @return Returns the ctx.
32     */
33    public InstallerContext getInstallerContext() {
34        return ctx;
35    }
36    
37    private class TargetStarted implements Runnable{
38        private BuildEvent buildEvent;
39        public void run(){
40            SwingInstallerContext.this.progressPanel.targetStarted(buildEvent);
41        }
42    }
43    private class ProvideAntFeedBack implements Runnable{
44        private String message;
45        public void run(){
46            SwingInstallerContext.this.feedBackPanel.setText(message);
47        }
48    }
49}
50