001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019:
020: package org.apache.axis2.tool.service.swing.ui;
021:
022: import org.apache.axis2.tool.service.bean.WizardBean;
023: import org.apache.axis2.tool.service.control.Controller;
024: import org.apache.axis2.tool.service.control.ProcessException;
025:
026: import javax.swing.JButton;
027: import javax.swing.JFrame;
028: import javax.swing.JOptionPane;
029: import javax.swing.JPanel;
030: import java.awt.HeadlessException;
031: import java.awt.Toolkit;
032: import java.awt.event.ActionEvent;
033: import java.awt.event.ActionListener;
034:
035: public class MainWindow extends JFrame {
036:
037: private JPanel wizardPaneContainer;
038: private JButton nextButton;
039: private JButton previousButton;
040: private JButton cancelButton;
041: private JButton finishButton;
042: private int currentPage;
043: private WizardPane currentWizardPane;
044:
045: private static final int PAGE_1 = 1;
046: private static final int PAGE_2 = 2;
047: private static final int PAGE_3 = 3;
048: //private static final int PAGE_4=4;
049:
050: private WizardBean wizardBean = new WizardBean();
051:
052: public MainWindow() throws HeadlessException {
053: super ("Axis 2 - Service Jar Builder");
054: init();
055:
056: }
057:
058: private void init() {
059: this .getContentPane().setLayout(null);
060:
061: this .setBounds((int) Toolkit.getDefaultToolkit()
062: .getScreenSize().getWidth() / 2 - 400 / 2,
063: (int) Toolkit.getDefaultToolkit().getScreenSize()
064: .getHeight() / 2 - 360 / 2, 400, 360);
065: this .setResizable(false);
066: this .setDefaultCloseOperation(EXIT_ON_CLOSE);
067:
068: int hgap = 5;
069: int vgap = 5;
070: int bWidth = 80;
071: int bHeight = 20;
072:
073: this .wizardPaneContainer = new JPanel(null);
074: this .getContentPane().add(this .wizardPaneContainer);
075: this .wizardPaneContainer.setBounds(0, 0, 400, 300);
076:
077: this .previousButton = new JButton("Previous");
078: this .getContentPane().add(this .previousButton);
079: this .previousButton
080: .setBounds(hgap, 300 + vgap, bWidth, bHeight);
081: this .previousButton.addActionListener(new ActionListener() {
082: public void actionPerformed(ActionEvent e) {
083: moveBackWard();
084: }
085:
086: });
087:
088: this .nextButton = new JButton("Next");
089: this .getContentPane().add(this .nextButton);
090: this .nextButton.setBounds(hgap + bWidth + hgap, 300 + vgap,
091: bWidth, bHeight);
092: this .nextButton.addActionListener(new ActionListener() {
093: public void actionPerformed(ActionEvent e) {
094: moveForward();
095: }
096: });
097:
098: this .cancelButton = new JButton("Close");
099: this .getContentPane().add(this .cancelButton);
100: this .cancelButton.setBounds(hgap + (bWidth + hgap) * 2,
101: 300 + vgap, bWidth, bHeight);
102: this .cancelButton.addActionListener(new ActionListener() {
103: public void actionPerformed(ActionEvent e) {
104: if (confirmExit())
105: System.exit(0);
106: }
107: });
108:
109: this .finishButton = new JButton("Finish");
110: this .getContentPane().add(this .finishButton);
111: this .finishButton.setBounds(hgap + (bWidth + hgap) * 3,
112: 300 + vgap, bWidth, bHeight);
113: this .finishButton.addActionListener(new ActionListener() {
114: public void actionPerformed(ActionEvent e) {
115: processFinish();
116: }
117: });
118:
119: this .currentPage = PAGE_1;
120: moveToPage(currentPage); //add the first page as default
121: }
122:
123: private void showErrorMessage() {
124: JOptionPane.showMessageDialog(this ,
125: "Required Value Not set!!!", "Error",
126: JOptionPane.ERROR_MESSAGE);
127: }
128:
129: private void showErrorMessage(String message) {
130: JOptionPane.showMessageDialog(this , message, "Error",
131: JOptionPane.ERROR_MESSAGE);
132: }
133:
134: private void showSuccessMessage(String message) {
135: JOptionPane.showMessageDialog(this , message, "Error",
136: JOptionPane.INFORMATION_MESSAGE);
137: }
138:
139: private boolean confirmExit() {
140: int returnType = JOptionPane.showOptionDialog(this ,
141: "Are you sure you want to exit?",
142: "Exit service builder", JOptionPane.YES_NO_OPTION,
143: JOptionPane.WARNING_MESSAGE, null, null, null);
144: return (returnType == JOptionPane.YES_OPTION);
145: }
146:
147: private void moveForward() {
148: if (currentWizardPane.validateValues()) {
149: this .currentPage++;
150: moveToPage(this .currentPage);
151: } else {
152: showErrorMessage();
153: }
154: }
155:
156: private void moveBackWard() {
157: this .currentPage--;
158: moveToPage(this .currentPage);
159:
160: }
161:
162: private void moveToPage(int page) {
163: switch (page) {
164: case PAGE_1:
165: processPage(new WizardPane1(this .wizardBean, this ), false,
166: true, false);
167: break;
168: case PAGE_2:
169: processPage(new WizardPane2(this .wizardBean, this ), true,
170: true, false);
171: break;
172: case PAGE_3:
173: processPage(new WizardPane3(this .wizardBean, this ), true,
174: false, true);
175: break;
176: default:
177: return;
178: }
179: }
180:
181: private void processFinish() {
182: if (currentWizardPane.validateValues()) {
183: try {
184: new Controller().process(wizardBean);
185: showSuccessMessage(" jar file creation successful! ");
186: } catch (ProcessException e) {
187: showErrorMessage(e.getMessage());
188: } catch (Exception e) {
189: showErrorMessage("Unknown Error! " + e.getMessage());
190: }
191: } else {
192: showErrorMessage();
193: }
194: }
195:
196: private void processPage(WizardPane pane, boolean prevButtonState,
197: boolean nextButtonState, boolean finishButtonState) {
198: this .wizardPaneContainer.removeAll();
199: currentWizardPane = pane;
200: this .wizardPaneContainer.add(pane);
201: this .previousButton.setEnabled(prevButtonState);
202: this .nextButton.setEnabled(nextButtonState);
203: this .finishButton.setEnabled(finishButtonState);
204: this .repaint();
205: }
206:
207: public static void main(String[] args) {
208: new MainWindow().show();
209: }
210:
211: }
|