KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > kelp > common > deployer > DeployInnerPanel


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  *
19  * Contributor(s):
20  * Paul Mahar
21  *
22  */

23 package org.enhydra.kelp.common.deployer;
24
25 // Kelp imports
26
import org.enhydra.kelp.common.event.WriteEvent;
27 import org.enhydra.kelp.common.event.WriteListener;
28 import org.enhydra.kelp.common.node.OtterNode;
29 import org.enhydra.kelp.common.node.OtterProject;
30 import org.enhydra.kelp.common.swing.AddinInnerPanel;
31
32 // ToolBox imports
33
import org.enhydra.tool.common.DataValidationException;
34 import org.enhydra.tool.common.ButtonPanel;
35 import org.enhydra.tool.common.ToolException;
36
37 // Standard imports
38
import java.awt.*;
39 import java.lang.ref.WeakReference JavaDoc;
40 import javax.swing.*;
41 import javax.swing.event.*;
42 import java.beans.*;
43
44 //
45
public class DeployInnerPanel extends AddinInnerPanel {
46     private int showIndex = 0;
47     private JTabbedPane tab = null;
48     private BorderLayout layoutMain = null;
49     private GeneralPanel generalPanel = null;
50     private InputPanel inputPanel = null;
51     private ContentPanel contentPanel = null;
52     private ArchivePanel archivePanel = null;
53     private RunPanel runPanel = null;
54     private TabChangeListener tabListener = null;
55     private WeakReference JavaDoc backRef = null;
56     private WeakReference JavaDoc nextRef = null;
57
58     public DeployInnerPanel() {
59         super();
60         try {
61             jbInit();
62             pmInit();
63         } catch (Exception JavaDoc ex) {
64             ex.printStackTrace();
65         }
66     }
67
68     // override AddinInnerPanel
69
public void preShow() {
70         Component back = getBack();
71
72         if (back != null) {
73             back.setEnabled(false);
74         }
75     }
76
77     // override AddinInnerPanel
78
public void read(OtterNode node) {
79         super.read(node);
80         DeployStep[] steps = new DeployStep[0];
81
82         steps = getSteps();
83         for (int i = 0; i < steps.length; i++) {
84             steps[i].read(getProject());
85         }
86     }
87
88     // override AddinInnerPanel
89
public void write(OtterNode node) throws DataValidationException {
90         super.write(node);
91         DeployStep[] steps = new DeployStep[0];
92
93         steps = getSteps();
94         for (int i = 0; i < steps.length; i++) {
95             steps[i].write(getProject());
96         }
97     }
98
99     // override AddinInnerPanel
100
public void initPreferredSize() {
101         Dimension d = new Dimension();
102
103         d.height = 425;
104         d.width = 600;
105         setPreferredSize(d);
106     }
107
108     // override AddinInnerPanel
109
public void clearAll() {
110         super.clearAll();
111         DeployStep[] steps = new DeployStep[0];
112
113         tab.removeChangeListener(tabListener);
114         if (backRef != null) {
115             backRef.clear();
116         }
117         if (nextRef != null) {
118             nextRef.clear();
119         }
120         steps = getSteps();
121         for (int i = 0; i < steps.length; i++) {
122             steps[i].clearAll();
123         }
124         backRef = null;
125         nextRef = null;
126         tabListener = null;
127         generalPanel = null;
128         inputPanel = null;
129         contentPanel = null;
130         archivePanel = null;
131         runPanel = null;
132     }
133
134     //
135
private void refresh() {
136         Component back = null;
137         Component next = null;
138         boolean backEnable = true;
139         boolean nextEnable = true;
140
141         back = getBack();
142         next = getNext();
143         int index = tab.getSelectedIndex();
144
145         if (index == 0) {
146             backEnable = false;
147         } else if (index == (tab.getTabCount() - 1)) {
148             nextEnable = false;
149         }
150         if (back != null) {
151             back.setEnabled(backEnable);
152         }
153         if (next != null) {
154             next.setEnabled(nextEnable);
155         }
156         getSelectedStep().refresh();
157     }
158
159     private Component getNext() {
160         Component comp = null;
161
162         if (nextRef == null) {
163             comp = ButtonPanel.findButton(getTopLevelAncestor(),
164                                           ButtonPanel.COMMAND_NEXT);
165             if (comp != null) {
166                 nextRef = new WeakReference JavaDoc(comp);
167             }
168         } else {
169             comp = (Component) nextRef.get();
170         }
171         return comp;
172     }
173
174     private Component getBack() {
175         Component comp = null;
176
177         if (backRef == null) {
178             comp = ButtonPanel.findButton(getTopLevelAncestor(),
179                                           ButtonPanel.COMMAND_BACK);
180             if (comp != null) {
181                 backRef = new WeakReference JavaDoc(comp);
182             }
183         } else {
184             comp = (Component) backRef.get();
185         }
186         return comp;
187     }
188
189     protected void back() {
190         int index = tab.getSelectedIndex();
191
192         if (index > 0) {
193             showIndex = --index;
194             tab.setSelectedIndex(showIndex);
195         }
196         refresh();
197     }
198
199     protected boolean next() {
200         int index = tab.getSelectedIndex();
201         boolean valid = true;
202         DeployStep step = getSelectedStep();
203
204         if (index < tab.getTabCount()) {
205             try {
206                 step.validateData();
207                 if (getProject() != null) {
208                     step.write(getProject());
209                     step.build(getProject(), getWriteListener());
210                 }
211                 showIndex = ++index;
212                 tab.setSelectedIndex(showIndex);
213             } catch (DataValidationException e) {
214                 valid = false;
215                 JOptionPane.showMessageDialog(this, e.getValidationMessage());
216             } catch (ToolException e) {
217                 valid = false;
218                 JOptionPane.showMessageDialog(this, e.getMessage());
219                 e.printStackTrace(System.err);
220             }
221         }
222         refresh();
223         return valid;
224     }
225
226     private WriteListener getWriteListener() {
227         return generalPanel.getWriteListener();
228     }
229
230     protected void deploy(CoreDeployTool tool) {
231         if (getProject() != null) {
232             DeployBuilder builder = null;
233
234             try {
235                 WriteEvent clear;
236
237                 clear = new WriteEvent(this, WriteEvent.CLEAR, new String JavaDoc());
238                 trySave();
239                 builder = new DeployBuilder(getWriteListener());
240                 builder.setProject(getProject());
241                 builder.setEcho(true);
242                 builder.addProgressListener(tool.getProgressMeter());
243                 tool.getProgressMeter().addCancelListener(builder);
244                 tool.openProgress();
245                 getWriteListener().onWrite(clear);
246                 if (tab.getSelectedIndex() > 0) {
247                     builder.setOwner(getWindow());
248                     builder.setDoneDialog(true);
249                 }
250                 builder.build();
251             } catch (DataValidationException e) {
252                 JOptionPane.showMessageDialog(this, e.getValidationMessage());
253             } catch (ToolException e) {
254                 JOptionPane.showMessageDialog(this, e.getMessage());
255                 e.printStackTrace(System.err);
256             }
257         }
258     }
259
260     // override InnerPanel
261
protected Component[] getFirstFocusComponents() {
262         Component[] comps = new Component[0];
263         Component[] genComps = new Component[0];
264
265         genComps = generalPanel.getFirstFocusComponents();
266         comps = new Component[genComps.length + 1];
267         comps[0] = tab;
268         for (int i = 0; i < genComps.length; i++) {
269             comps[i + 1] = genComps[i];
270         }
271         return comps;
272     }
273
274     // override InnerPanel
275
public void save() {
276         if (getProject() == null) {
277             System.err.println("DeployInnerPanel.save() - project null");
278         } else {
279             try {
280                 trySave();
281             } catch (DataValidationException e) {
282                 System.err.println(e.getValidationMessage());
283             } catch (ToolException e) {
284                 System.err.println(e.getMessage());
285             }
286         }
287     }
288
289     private void trySave() throws DataValidationException, ToolException {
290         try {
291             write(getProject());
292         } catch (DataValidationException e) {
293             buildSteps();
294         }
295     }
296
297     private void buildSteps() throws DataValidationException, ToolException {
298         DeployStep[] steps = new DeployStep[0];
299
300         steps = getSteps();
301         for (int i = 0; i < steps.length; i++) {
302             steps[i].build(getProject(), getWriteListener());
303         }
304     }
305
306     //
307
private DeployStep getSelectedStep() {
308         DeployStep[] steps = new DeployStep[5];
309
310         steps = getSteps();
311         return steps[tab.getSelectedIndex()];
312     }
313
314     private DeployStep[] getSteps() {
315         DeployStep[] steps = new DeployStep[5];
316
317         steps[0] = generalPanel;
318         steps[1] = inputPanel;
319         steps[2] = contentPanel;
320         steps[3] = archivePanel;
321         steps[4] = runPanel;
322         return steps;
323     }
324
325     //
326
private void pmInit() {
327         tabListener = new TabChangeListener();
328         tab.addChangeListener(tabListener);
329         generalPanel = new GeneralPanel();
330         inputPanel = new InputPanel();
331         contentPanel = new ContentPanel();
332         archivePanel = new ArchivePanel();
333         runPanel = new RunPanel();
334         addInstructor(generalPanel);
335         addInstructor(inputPanel);
336         addInstructor(contentPanel);
337         addInstructor(archivePanel);
338         addInstructor(runPanel);
339     }
340
341     private void addInstructor(Instructor i) {
342         InstructPanel ip = null;
343
344         if (i instanceof JPanel) {
345             ip = new InstructPanel();
346             ip.addInstuctor(i);
347             tab.add(ip, i.getTab());
348         } else {
349             throw new RuntimeException JavaDoc("Instructor not a JPanel: "
350                                        + i.getTitle());
351         }
352     }
353
354     private void jbInit() throws Exception JavaDoc {
355         tab = (JTabbedPane) Beans.instantiate(getClass().getClassLoader(),
356                                               JTabbedPane.class.getName());
357         layoutMain =
358             (BorderLayout) Beans.instantiate(getClass().getClassLoader(),
359                                              BorderLayout.class.getName());
360         this.setLayout(layoutMain);
361         this.add(tab, BorderLayout.CENTER);
362     }
363
364     private class TabChangeListener implements ChangeListener {
365         public void stateChanged(ChangeEvent e) {
366             if (!tab.getSelectedComponent().isShowing()) {
367                 int selectIndex = tab.getSelectedIndex();
368
369                 if (selectIndex > showIndex) {
370                     tab.setSelectedIndex(showIndex);
371                     for (int i = showIndex; i < selectIndex; i++) {
372                         if (!next()) {
373                             break;
374                         }
375                     }
376                     showIndex = tab.getSelectedIndex();
377                 } else if (selectIndex < showIndex) {
378                     showIndex = selectIndex;
379                 }
380             }
381             refresh();
382         }
383
384     }
385 }
386
Popular Tags