001: /*
002: * Created on 03/11/2005
003: *
004: * Swing Components - visit http://sf.net/projects/gfd
005: *
006: * Copyright (C) 2005 Igor Regis da Silva Simões
007: *
008: * This program is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public License
010: * as published by the Free Software Foundation; either version 2
011: * of the License, or (at your option) any later version.
012: *
013: * This program is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: * GNU General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public License
019: * along with this program; if not, write to the Free Software
020: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
021: *
022: */
023: package br.com.gfp.wizard;
024:
025: import java.awt.BorderLayout;
026: import java.awt.CardLayout;
027: import java.awt.Frame;
028: import java.awt.GridBagConstraints;
029: import java.awt.GridBagLayout;
030: import java.awt.Insets;
031: import java.awt.event.ActionEvent;
032: import java.awt.event.ActionListener;
033:
034: import javax.swing.ImageIcon;
035: import javax.swing.JButton;
036: import javax.swing.JDialog;
037: import javax.swing.JLabel;
038: import javax.swing.JPanel;
039: import javax.swing.JTextArea;
040:
041: import br.com.gfp.internationalization.BasicWizardMessages;
042:
043: /**
044: *
045: * @author Igor Regis da Silva Simoes
046: * @since 21/10/2005
047: */
048: public abstract class BasicWizard extends JDialog {
049: protected JButton nextButton = new JButton();
050:
051: protected JButton backButton = new JButton();
052:
053: private JPanel centralPanel = new JPanel();
054:
055: private byte tela = 1;
056:
057: /**
058: *
059: * @param owner
060: * @param versaoDB
061: * @param dbManager
062: */
063: public BasicWizard(Frame owner) {
064: super (owner, true);
065:
066: if (precisaDeWizard()) {
067: setLocationRelativeTo(null);
068: montarTelaBasica();
069: adicionaPainelWelcome();
070: adicionarPaineis();
071: adicionaPainelFim();
072: migrar();
073: }
074: }
075:
076: /**
077: * Executa os demais passos para migracao de versão de banco de dados
078: */
079: protected abstract void migrar();
080:
081: /**
082: * Adiciona os demais passos para migracao de versão de banco de dados
083: */
084: protected abstract void adicionarPaineis();
085:
086: /**
087: * Indica se o wizard será ou não executado
088: * @return boolean
089: */
090: protected abstract boolean precisaDeWizard();
091:
092: /**
093: * Executa um passo do wizard, este método é chamado na
094: * passagem de uma tela para outra do wizard;
095: * ou seja, executa o passo da passagem da tela
096: * <code>passo</code> para a <code>passo + 1</code>
097: * @param passo
098: */
099: protected abstract void executeStep(int passo);
100:
101: /**
102: * Monta o layout básico da trela de wizard
103: */
104: private void montarTelaBasica() {
105: JPanel mainPanel = new JPanel();
106: mainPanel.setLayout(new BorderLayout());
107:
108: getContentPane().setLayout(new BorderLayout());
109: getContentPane().add(mainPanel, BorderLayout.CENTER);
110:
111: JPanel leftPanel = new JPanel();
112: leftPanel.setLayout(new BorderLayout());
113: leftPanel.add(new JLabel(new ImageIcon(getClass().getResource(
114: "/icones/dbMigration.jpeg"))), BorderLayout.CENTER);
115:
116: mainPanel.add(leftPanel, BorderLayout.WEST);
117:
118: JPanel southPanel = new JPanel();
119: southPanel.setLayout(new GridBagLayout());
120:
121: NavListener navListener = new NavListener();
122:
123: nextButton.setText(BasicWizardMessages.getMessages().getString(
124: "avancar"));
125: nextButton.addActionListener(navListener);
126: GridBagConstraints constraints = new GridBagConstraints();
127: constraints.gridx = 2;
128: constraints.gridy = 0;
129: constraints.anchor = GridBagConstraints.EAST;
130: constraints.insets = new Insets(10, 10, 10, 100);
131: southPanel.add(nextButton, constraints);
132:
133: backButton.setText(BasicWizardMessages.getMessages().getString(
134: "voltar"));
135: backButton.addActionListener(navListener);
136: backButton.setEnabled(false);
137: constraints.gridx = 1;
138: constraints.gridy = 0;
139: constraints.anchor = GridBagConstraints.EAST;
140: constraints.insets = new Insets(0, 0, 0, 0);
141: constraints.weightx = 1;
142: southPanel.add(backButton, constraints);
143:
144: mainPanel.add(southPanel, BorderLayout.SOUTH);
145:
146: centralPanel.setLayout(new CardLayout());
147: mainPanel.add(centralPanel, BorderLayout.CENTER);
148: }
149:
150: /**
151: * Adiciona o painel inicial do Wizard
152: *
153: */
154: protected void adicionaPainelWelcome() {
155:
156: JPanel painelWelcome = new JPanel();
157: painelWelcome.setLayout(new GridBagLayout());
158: painelWelcome.setSize(421, 242);
159:
160: JTextArea jtaTitulo = new JTextArea();
161: jtaTitulo.setText(BasicWizardMessages.getMessages().getString(
162: "defaultWelcomeTitle"));
163: jtaTitulo.setFont(new java.awt.Font("Times New Roman",
164: java.awt.Font.BOLD | java.awt.Font.ITALIC, 18));
165: jtaTitulo.setEditable(false);
166: jtaTitulo.setOpaque(false);
167: GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
168: gridBagConstraints1.gridx = 0;
169: gridBagConstraints1.gridy = 0;
170: gridBagConstraints1.insets = new java.awt.Insets(00, 0, 20, 0);
171: gridBagConstraints1.anchor = java.awt.GridBagConstraints.NORTH;
172: gridBagConstraints1.fill = java.awt.GridBagConstraints.HORIZONTAL;
173: painelWelcome.add(jtaTitulo, gridBagConstraints1);
174:
175: JTextArea jtaIntro = new JTextArea();
176: jtaIntro.setText(BasicWizardMessages.getMessages().getString(
177: "defaultIntro"));
178: jtaIntro.setEditable(false);
179: jtaIntro.setOpaque(false);
180: GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
181: gridBagConstraints2.gridx = 0;
182: gridBagConstraints2.gridy = 1;
183: gridBagConstraints2.anchor = java.awt.GridBagConstraints.WEST;
184: painelWelcome.add(jtaIntro, gridBagConstraints2);
185:
186: JTextArea jtaFinal = new JTextArea();
187: jtaFinal.setText(BasicWizardMessages.getMessages().getString(
188: "final"));
189: jtaFinal.setEditable(false);
190: jtaFinal.setOpaque(false);
191: GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
192: gridBagConstraints4.gridx = 0;
193: gridBagConstraints4.gridy = 3;
194: gridBagConstraints4.anchor = java.awt.GridBagConstraints.WEST;
195: painelWelcome.add(jtaFinal, gridBagConstraints4);
196:
197: addPage(painelWelcome);
198: }
199:
200: /**
201: * Adiciona o painel que informa finalização do wizard
202: */
203: protected void adicionaPainelFim() {
204:
205: JPanel painelWelcome = new JPanel();
206: painelWelcome.setLayout(new GridBagLayout());
207: painelWelcome.setSize(421, 242);
208:
209: JTextArea jtaTitulo = new JTextArea();
210: jtaTitulo.setText(BasicWizardMessages.getMessages().getString(
211: "endTitle"));
212: jtaTitulo.setFont(new java.awt.Font("Times New Roman",
213: java.awt.Font.BOLD | java.awt.Font.ITALIC, 18));
214: jtaTitulo.setEditable(false);
215: jtaTitulo.setOpaque(false);
216: GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
217: gridBagConstraints1.gridx = 0;
218: gridBagConstraints1.gridy = 0;
219: gridBagConstraints1.insets = new java.awt.Insets(00, 0, 30, 0);
220: gridBagConstraints1.anchor = java.awt.GridBagConstraints.NORTH;
221: gridBagConstraints1.fill = java.awt.GridBagConstraints.HORIZONTAL;
222: painelWelcome.add(jtaTitulo, gridBagConstraints1);
223:
224: JTextArea jtaIntro = new JTextArea();
225: jtaIntro.setText(BasicWizardMessages.getMessages().getString(
226: "endText"));
227: jtaIntro.setEditable(false);
228: jtaIntro.setOpaque(false);
229: GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
230: gridBagConstraints2.gridx = 0;
231: gridBagConstraints2.gridy = 1;
232: gridBagConstraints2.anchor = java.awt.GridBagConstraints.WEST;
233: painelWelcome.add(jtaIntro, gridBagConstraints2);
234:
235: addPage(painelWelcome);
236: }
237:
238: /**
239: * Adiciona uma página a este wizard
240: * @param page
241: */
242: protected void addPage(JPanel page) {
243: centralPanel.add(page, "Tela"
244: + centralPanel.getComponentCount());
245: }
246:
247: /**
248: * Classe que ouve as ações dos botões de navegação
249: *
250: * @author Igor Regis da Silva Simoes
251: * @since 21/10/2005
252: */
253: private class NavListener implements ActionListener {
254: /**
255: * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
256: */
257: public void actionPerformed(ActionEvent e) {
258: if (e.getSource() == nextButton) {
259: if (nextButton.getText().equals(
260: BasicWizardMessages.getMessages().getString(
261: "Finalizar"))) {
262: BasicWizard.this .setVisible(false);
263: BasicWizard.this .dispose();
264: return;
265: }
266: executeStep(tela);
267: ((CardLayout) centralPanel.getLayout())
268: .next(centralPanel);
269: if (++tela >= centralPanel.getComponentCount()) {
270: nextButton.setText(BasicWizardMessages
271: .getMessages().getString("Finalizar"));
272: }
273: if (nextButton.getText().equals(
274: BasicWizardMessages.getMessages().getString(
275: "Finalizar"))) {
276: backButton.setEnabled(false);
277: } else {
278: backButton.setEnabled(true);
279: }
280: } else if (e.getSource() == backButton) {
281: ((CardLayout) centralPanel.getLayout())
282: .previous(centralPanel);
283: if (--tela <= 1) {
284: backButton.setEnabled(false);
285: }
286: nextButton.setEnabled(true);
287: }
288: }
289: }
290: }
|