001: /*
002: * Copyright 2005 Paul Hinds
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.tp23.antinstaller.renderer.swing;
017:
018: import javax.swing.JFrame;
019: import javax.swing.JLabel;
020: import javax.swing.JProgressBar;
021: import javax.swing.SwingUtilities;
022:
023: import org.apache.tools.ant.BuildEvent;
024: import org.tp23.antinstaller.InstallerContext;
025: import org.tp23.antinstaller.runtime.SwingRunner;
026:
027: /**
028: * Hold a series of callbacks for the Swing UI for installation progress
029: * @author teknopaul
030: *
031: */
032: public class SwingInstallerContext {
033:
034: private static JFrame masterFrame;
035: private JLabel feedBackPanel;
036: private ProgressPanel progressPanel;
037: private JProgressBar jProgressBar;
038: private InstallerContext ctx;
039:
040: public SwingInstallerContext(InstallerContext ctx,
041: JFrame masterFrame) {
042: this .ctx = ctx;
043: SwingInstallerContext.masterFrame = masterFrame;
044: }
045:
046: public JFrame getMasterFrame() {
047: return masterFrame;
048: }
049:
050: public SwingRunner getSwingRunner() {
051: return (SwingRunner) ctx.getRunner();
052: }
053:
054: public void setFeedBackLabel(JLabel feedBackPanel) {
055: this .feedBackPanel = feedBackPanel;
056: }
057:
058: /**
059: * The progress panel is optional so not calling this method
060: * should not cause errors or NPEs
061: * @param progressPanel
062: */
063: public void setProgressPanel(ProgressPanel progressPanel) {
064: this .progressPanel = progressPanel;
065: }
066:
067: /**
068: * The progress bar is optional so not calling this method
069: * should not cause errors or NPEs
070: * @param progressPanel
071: */
072: public void setProgressBar(JProgressBar progressBar) {
073: this .jProgressBar = progressBar;
074: }
075:
076: public void buildStarted(BuildEvent buildEvent) {
077: provideAntFeedBack(buildEvent.getMessage());
078: try {
079: SwingUtilities.invokeAndWait(new Runnable() {
080: public void run() {
081: if (SwingInstallerContext.this .progressPanel != null) {
082: SwingInstallerContext.this .progressPanel
083: .prepareCalledTargets();
084: }
085: }
086: });
087: } catch (Exception e) { //Interrupted or InvocationTarget
088: SwingInstallerContext.this .ctx.log(e);
089: }
090: }
091:
092: public void buildFinished(BuildEvent buildEvent) {
093: if (this .progressPanel != null) {
094: try {
095: SwingUtilities.invokeLater(new Runnable() {
096: public void run() {
097: SwingInstallerContext.this .progressPanel
098: .buildFinished();
099: }
100: });
101: } catch (Exception e) { //Interrupted or InvocationTarget
102: SwingInstallerContext.this .ctx.log(e);
103: }
104: }
105: if (this .jProgressBar != null) {
106: jProgressBar.setValue(jProgressBar.getMaximum());
107: }
108: }
109:
110: public void targetStarted(BuildEvent buildEvent) {
111: TargetStarted targetStarted = new TargetStarted();
112: targetStarted.buildEvent = buildEvent;
113: try {
114: if (this .progressPanel != null) {
115: //Invoke and wait used since strict ordering od started and finished is requried
116: SwingUtilities.invokeAndWait(targetStarted);
117: }
118: } catch (Exception e) { //Interrupted or InvocationTarget
119: SwingInstallerContext.this .ctx.log(e);
120: }
121: }
122:
123: public void targetFinished(BuildEvent buildEvent) {
124: try {
125: //Invoke and wait used since strict ordering od started and finished is requried
126: SwingUtilities.invokeAndWait(new Runnable() {
127: public void run() {
128: if (SwingInstallerContext.this .progressPanel != null) {
129: SwingInstallerContext.this .progressPanel
130: .targetFinished();
131: }
132: }
133: });
134: } catch (Exception e) { //Interrupted or InvocationTarget
135: SwingInstallerContext.this .ctx.log(e);
136: }
137: }
138:
139: public void provideAntFeedBack(String message) {
140: // We should never have Ant running without a ProgressPane
141: // but do an if null here in case future FilterChains are different
142: ProvideAntFeedBack provideAntFeedBack = new ProvideAntFeedBack();
143: provideAntFeedBack.message = message;
144: try {
145: if (feedBackPanel != null) {
146: SwingUtilities.invokeLater(provideAntFeedBack);
147: }
148: } catch (Exception e) { //Interrupted or InvocationTarget
149: SwingInstallerContext.this .ctx.log(e);
150: }
151: }
152:
153: public void tick() {
154: if (jProgressBar != null) {
155: jProgressBar.setValue(jProgressBar.getValue() + 1);
156: }
157: }
158:
159: /**
160: * @return Returns the ctx.
161: */
162: public InstallerContext getInstallerContext() {
163: return ctx;
164: }
165:
166: private class TargetStarted implements Runnable {
167: private BuildEvent buildEvent;
168:
169: public void run() {
170: SwingInstallerContext.this .progressPanel
171: .targetStarted(buildEvent);
172: }
173: }
174:
175: private class ProvideAntFeedBack implements Runnable {
176: private String message;
177:
178: public void run() {
179: SwingInstallerContext.this.feedBackPanel.setText(message);
180: }
181: }
182: }
|