001: /*
002: * Enhydra Java Application Server Project
003: *
004: * The contents of this file are subject to the Enhydra Public License
005: * Version 1.1 (the "License"); you may not use this file except in
006: * compliance with the License. You may obtain a copy of the License on
007: * the Enhydra web site ( http://www.enhydra.org/ ).
008: *
009: * Software distributed under the License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
011: * the License for the specific terms governing rights and limitations
012: * under the License.
013: *
014: * The Initial Developer of the Enhydra Application Server is Lutris
015: * Technologies, Inc. The Enhydra Application Server and portions created
016: * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
017: * All Rights Reserved.
018: *
019: * Contributor(s):
020: * Paul Mahar
021: *
022: */
023: package org.enhydra.kelp.common.deployer;
024:
025: // Kelp imports
026: import org.enhydra.kelp.common.event.WriteEvent;
027: import org.enhydra.kelp.common.event.WriteListener;
028: import org.enhydra.kelp.common.node.OtterNode;
029: import org.enhydra.kelp.common.swing.AddinInnerPanel;
030:
031: // ToolBox imports
032: import org.enhydra.tool.common.DataValidationException;
033: import org.enhydra.tool.common.ButtonPanel;
034: import org.enhydra.tool.common.ToolException;
035:
036: // Standard imports
037: import java.awt.*;
038: import java.lang.ref.WeakReference;
039: import javax.swing.*;
040: import javax.swing.event.*;
041: import java.beans.*;
042:
043: //
044: public class DeployInnerPanel extends AddinInnerPanel {
045: private int showIndex = 0;
046: private JTabbedPane tab = null;
047: private BorderLayout layoutMain = null;
048: private GeneralPanel generalPanel = null;
049: private InputPanel inputPanel = null;
050: private ContentPanel contentPanel = null;
051: private ArchivePanel archivePanel = null;
052: private RunPanel runPanel = null;
053: private TabChangeListener tabListener = null;
054: private WeakReference backRef = null;
055: private WeakReference nextRef = null;
056:
057: public DeployInnerPanel() {
058: super ();
059: try {
060: jbInit();
061: pmInit();
062: } catch (Exception ex) {
063: ex.printStackTrace();
064: }
065: }
066:
067: // override AddinInnerPanel
068: public void preShow() {
069: Component back = getBack();
070:
071: if (back != null) {
072: back.setEnabled(false);
073: }
074: }
075:
076: // override AddinInnerPanel
077: public void read(OtterNode node) {
078: super .read(node);
079: DeployStep[] steps = new DeployStep[0];
080:
081: steps = getSteps();
082: for (int i = 0; i < steps.length; i++) {
083: steps[i].read(getProject());
084: }
085: }
086:
087: // override AddinInnerPanel
088: public void write(OtterNode node) throws DataValidationException {
089: super .write(node);
090: DeployStep[] steps = new DeployStep[0];
091:
092: steps = getSteps();
093: for (int i = 0; i < steps.length; i++) {
094: steps[i].write(getProject());
095: }
096: }
097:
098: // override AddinInnerPanel
099: public void initPreferredSize() {
100: Dimension d = new Dimension();
101:
102: d.height = 425;
103: d.width = 600;
104: setPreferredSize(d);
105: }
106:
107: // override AddinInnerPanel
108: public void clearAll() {
109: super .clearAll();
110: DeployStep[] steps = new DeployStep[0];
111:
112: tab.removeChangeListener(tabListener);
113: if (backRef != null) {
114: backRef.clear();
115: }
116: if (nextRef != null) {
117: nextRef.clear();
118: }
119: steps = getSteps();
120: for (int i = 0; i < steps.length; i++) {
121: steps[i].clearAll();
122: }
123: backRef = null;
124: nextRef = null;
125: tabListener = null;
126: generalPanel = null;
127: inputPanel = null;
128: contentPanel = null;
129: archivePanel = null;
130: runPanel = null;
131: }
132:
133: //
134: private void refresh() {
135: Component back = null;
136: Component next = null;
137: boolean backEnable = true;
138: boolean nextEnable = true;
139:
140: back = getBack();
141: next = getNext();
142: int index = tab.getSelectedIndex();
143:
144: if (index == 0) {
145: backEnable = false;
146: } else if (index == (tab.getTabCount() - 1)) {
147: nextEnable = false;
148: }
149: if (back != null) {
150: back.setEnabled(backEnable);
151: }
152: if (next != null) {
153: next.setEnabled(nextEnable);
154: }
155: getSelectedStep().refresh();
156: }
157:
158: private Component getNext() {
159: Component comp = null;
160:
161: if (nextRef == null) {
162: comp = ButtonPanel.findButton(getTopLevelAncestor(),
163: ButtonPanel.COMMAND_NEXT);
164: if (comp != null) {
165: nextRef = new WeakReference(comp);
166: }
167: } else {
168: comp = (Component) nextRef.get();
169: }
170: return comp;
171: }
172:
173: private Component getBack() {
174: Component comp = null;
175:
176: if (backRef == null) {
177: comp = ButtonPanel.findButton(getTopLevelAncestor(),
178: ButtonPanel.COMMAND_BACK);
179: if (comp != null) {
180: backRef = new WeakReference(comp);
181: }
182: } else {
183: comp = (Component) backRef.get();
184: }
185: return comp;
186: }
187:
188: protected void back() {
189: int index = tab.getSelectedIndex();
190:
191: if (index > 0) {
192: showIndex = --index;
193: tab.setSelectedIndex(showIndex);
194: }
195: refresh();
196: }
197:
198: protected boolean next() {
199: int index = tab.getSelectedIndex();
200: boolean valid = true;
201: DeployStep step = getSelectedStep();
202:
203: if (index < tab.getTabCount()) {
204: try {
205: step.validateData();
206: if (getProject() != null) {
207: step.write(getProject());
208: step.build(getProject(), getWriteListener());
209: }
210: showIndex = ++index;
211: tab.setSelectedIndex(showIndex);
212: } catch (DataValidationException e) {
213: valid = false;
214: JOptionPane.showMessageDialog(this , e
215: .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,
238: new String());
239: trySave();
240: builder = new DeployBuilder(getWriteListener());
241: builder.setProject(getProject());
242: builder.setEcho(true);
243: builder.addProgressListener(tool.getProgressMeter());
244: tool.getProgressMeter().addCancelListener(builder);
245: tool.openProgress();
246: getWriteListener().onWrite(clear);
247: if (tab.getSelectedIndex() > 0) {
248: builder.setOwner(getWindow());
249: builder.setDoneDialog(true);
250: }
251: builder.build();
252: } catch (DataValidationException e) {
253: JOptionPane.showMessageDialog(this , e
254: .getValidationMessage());
255: } catch (ToolException e) {
256: JOptionPane.showMessageDialog(this , e.getMessage());
257: e.printStackTrace(System.err);
258: }
259: }
260: }
261:
262: // override InnerPanel
263: protected Component[] getFirstFocusComponents() {
264: Component[] comps = new Component[0];
265: Component[] genComps = new Component[0];
266:
267: genComps = generalPanel.getFirstFocusComponents();
268: comps = new Component[genComps.length + 1];
269: comps[0] = tab;
270: for (int i = 0; i < genComps.length; i++) {
271: comps[i + 1] = genComps[i];
272: }
273: return comps;
274: }
275:
276: // override InnerPanel
277: public void save() {
278: if (getProject() == null) {
279: System.err
280: .println("DeployInnerPanel.save() - project null");
281: } else {
282: try {
283: trySave();
284: } catch (DataValidationException e) {
285: System.err.println(e.getValidationMessage());
286: } catch (ToolException e) {
287: System.err.println(e.getMessage());
288: }
289: }
290: }
291:
292: private void trySave() throws DataValidationException,
293: ToolException {
294: try {
295: write(getProject());
296: } catch (DataValidationException e) {
297: buildSteps();
298: }
299: }
300:
301: private void buildSteps() throws DataValidationException,
302: ToolException {
303: DeployStep[] steps = new DeployStep[0];
304:
305: steps = getSteps();
306: for (int i = 0; i < steps.length; i++) {
307: steps[i].build(getProject(), getWriteListener());
308: }
309: }
310:
311: //
312: private DeployStep getSelectedStep() {
313: DeployStep[] steps = new DeployStep[5];
314:
315: steps = getSteps();
316: return steps[tab.getSelectedIndex()];
317: }
318:
319: private DeployStep[] getSteps() {
320: DeployStep[] steps = new DeployStep[5];
321:
322: steps[0] = generalPanel;
323: steps[1] = inputPanel;
324: steps[2] = contentPanel;
325: steps[3] = archivePanel;
326: steps[4] = runPanel;
327: return steps;
328: }
329:
330: //
331: private void pmInit() {
332: tabListener = new TabChangeListener();
333: tab.addChangeListener(tabListener);
334: generalPanel = new GeneralPanel();
335: inputPanel = new InputPanel();
336: contentPanel = new ContentPanel();
337: archivePanel = new ArchivePanel();
338: runPanel = new RunPanel();
339: addInstructor(generalPanel);
340: addInstructor(inputPanel);
341: addInstructor(contentPanel);
342: addInstructor(archivePanel);
343: addInstructor(runPanel);
344: }
345:
346: private void addInstructor(Instructor i) {
347: InstructPanel ip = null;
348:
349: if (i instanceof JPanel) {
350: ip = new InstructPanel();
351: ip.addInstuctor(i);
352: tab.add(ip, i.getTab());
353: } else {
354: throw new RuntimeException("Instructor not a JPanel: "
355: + i.getTitle());
356: }
357: }
358:
359: private void jbInit() throws Exception {
360: tab = (JTabbedPane) Beans.instantiate(getClass()
361: .getClassLoader(), JTabbedPane.class.getName());
362: layoutMain = (BorderLayout) Beans.instantiate(getClass()
363: .getClassLoader(), BorderLayout.class.getName());
364: this .setLayout(layoutMain);
365: this .add(tab, BorderLayout.CENTER);
366: }
367:
368: private class TabChangeListener implements ChangeListener {
369: public void stateChanged(ChangeEvent e) {
370: if (!tab.getSelectedComponent().isShowing()) {
371: int selectIndex = tab.getSelectedIndex();
372:
373: if (selectIndex > showIndex) {
374: tab.setSelectedIndex(showIndex);
375: for (int i = showIndex; i < selectIndex; i++) {
376: if (!next()) {
377: break;
378: }
379: }
380: showIndex = tab.getSelectedIndex();
381: } else if (selectIndex < showIndex) {
382: showIndex = selectIndex;
383: }
384: }
385: refresh();
386: }
387:
388: }
389: }
|