PageRenderTime 166ms CodeModel.GetById 40ms app.highlight 68ms RepoModel.GetById 52ms app.codeStats 1ms

/src/mpv5/ui/dialogs/Wizard.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 346 lines | 230 code | 44 blank | 72 comment | 12 complexity | e8cf6dc3f34e72683118695ade66ed56 MD5 | raw file
  1/*
  2 *  This file is part of YaBS.
  3 *
  4 *  YaBS is free software: you can redistribute it and/or modify
  5 *  it under the terms of the GNU General Public License as published by
  6 *  the Free Software Foundation, either version 3 of the License, or
  7 *  (at your option) any later version.
  8 *
  9 *  YaBS is distributed in the hope that it will be useful,
 10 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 11 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 12 *  GNU General Public License for more details.
 13 *
 14 *  You should have received a copy of the GNU General Public License
 15 *  along with YaBS.  If not, see <http://www.gnu.org/licenses/>.
 16 */
 17
 18/*
 19 * Wizard.java
 20 *
 21 * Created on Dec 18, 2008, 5:14:12 AM
 22 */
 23package mpv5.ui.dialogs;
 24
 25import java.awt.BorderLayout;
 26import java.awt.Component;
 27import java.awt.Cursor;
 28import java.util.ArrayList;
 29import javax.swing.JComponent;
 30import javax.swing.JFrame;
 31import javax.swing.JPanel;
 32import mpv5.data.PropertyStore;
 33import mpv5.globals.Messages;
 34import mpv5.ui.frames.MPView;
 35
 36/**
 37 *
 38 * 
 39 */
 40public class Wizard extends javax.swing.JDialog implements WizardMaster {
 41
 42    private static final long serialVersionUID = 1L;
 43    public PropertyStore actionVars = new PropertyStore();
 44
 45//    private int index = 4300;
 46    private Component lastpanel;
 47    private boolean isEnded = false;
 48    private boolean standalone;
 49    private ArrayList<JComponent> contentlist = new ArrayList<JComponent>();
 50
 51    ;
 52    private int level;
 53
 54    /** Creates new form Wizard
 55     * @param standalone
 56     */
 57    public Wizard(boolean standalone) {
 58        initComponents();
 59        setModalityType(ModalityType.MODELESS);
 60        back.setEnabled(false);
 61        this.standalone = standalone;
 62        if (standalone) {
 63            this.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
 64        }
 65        level = 0;
 66    }
 67
 68    public void showWiz() {
 69        back.setEnabled(false);
 70        content.add(contentlist.get(level), BorderLayout.CENTER);
 71        pack();
 72        setLocationRelativeTo(mpv5.YabsViewProxy.instance().getIdentifierFrame());
 73        setAlwaysOnTop(!standalone);
 74        setVisible(true);
 75        requestFocus();
 76    }
 77
 78    /** This method is called from within the constructor to
 79     * initialize the form.
 80     * WARNING: Do NOT modify this code. The content of this method is
 81     * always regenerated by the Form Editor.
 82     */
 83    @SuppressWarnings("unchecked")
 84    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
 85    private void initComponents() {
 86
 87        jScrollPane1 = new javax.swing.JScrollPane();
 88        content = new javax.swing.JPanel();
 89        control = new javax.swing.JPanel();
 90        cancel = new javax.swing.JButton();
 91        back = new javax.swing.JButton();
 92        next = new javax.swing.JButton();
 93        jScrollPane2 = new javax.swing.JScrollPane();
 94        message = new javax.swing.JTextPane();
 95        progressbar = new javax.swing.JProgressBar();
 96
 97        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
 98        java.util.ResourceBundle bundle = mpv5.i18n.LanguageManager.getBundle(); // NOI18N
 99        setTitle(bundle.getString("Wizard.title")); // NOI18N
100        setBackground(new java.awt.Color(255, 255, 255));
101        setName("Form"); // NOI18N
102
103        jScrollPane1.setName("jScrollPane1"); // NOI18N
104
105        content.setBackground(new java.awt.Color(255, 255, 255));
106        content.setBorder(javax.swing.BorderFactory.createEtchedBorder());
107        content.setName("content"); // NOI18N
108        content.setLayout(new java.awt.BorderLayout());
109        jScrollPane1.setViewportView(content);
110
111        getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER);
112
113        control.setBackground(new java.awt.Color(255, 255, 255));
114        control.setMaximumSize(new java.awt.Dimension(32767, 100));
115        control.setName("control"); // NOI18N
116        control.setPreferredSize(new java.awt.Dimension(375, 60));
117
118        cancel.setText(bundle.getString("Wizard.cancel.text")); // NOI18N
119        cancel.setName("cancel"); // NOI18N
120        cancel.addActionListener(new java.awt.event.ActionListener() {
121            public void actionPerformed(java.awt.event.ActionEvent evt) {
122                cancelActionPerformed(evt);
123            }
124        });
125
126        back.setText(bundle.getString("Wizard.back.text")); // NOI18N
127        back.setName("back"); // NOI18N
128        back.addActionListener(new java.awt.event.ActionListener() {
129            public void actionPerformed(java.awt.event.ActionEvent evt) {
130                backActionPerformed(evt);
131            }
132        });
133
134        next.setText(bundle.getString("Wizard.next.text")); // NOI18N
135        next.setName("next"); // NOI18N
136        next.addActionListener(new java.awt.event.ActionListener() {
137            public void actionPerformed(java.awt.event.ActionEvent evt) {
138                nextActionPerformed(evt);
139            }
140        });
141
142        jScrollPane2.setBorder(null);
143        jScrollPane2.setName("jScrollPane2"); // NOI18N
144
145        message.setBackground(new java.awt.Color(254, 254, 254));
146        message.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
147        message.setEditable(false);
148        message.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N
149        message.setName("message"); // NOI18N
150        jScrollPane2.setViewportView(message);
151
152        progressbar.setName("progressbar"); // NOI18N
153
154        javax.swing.GroupLayout controlLayout = new javax.swing.GroupLayout(control);
155        control.setLayout(controlLayout);
156        controlLayout.setHorizontalGroup(
157            controlLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
158            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, controlLayout.createSequentialGroup()
159                .addContainerGap()
160                .addComponent(cancel)
161                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
162                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 347, Short.MAX_VALUE)
163                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
164                .addComponent(back)
165                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
166                .addComponent(next)
167                .addContainerGap())
168            .addComponent(progressbar, javax.swing.GroupLayout.DEFAULT_SIZE, 541, Short.MAX_VALUE)
169        );
170        controlLayout.setVerticalGroup(
171            controlLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
172            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, controlLayout.createSequentialGroup()
173                .addComponent(progressbar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
174                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
175                .addGroup(controlLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
176                    .addGroup(controlLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
177                        .addComponent(next)
178                        .addComponent(back)
179                        .addComponent(cancel))
180                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 30, Short.MAX_VALUE))
181                .addContainerGap())
182        );
183
184        getContentPane().add(control, java.awt.BorderLayout.SOUTH);
185
186        pack();
187    }// </editor-fold>//GEN-END:initComponents
188
189    private void cancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelActionPerformed
190        if (!standalone) {
191            this.dispose();
192        } else {
193            System.exit(1);
194        }
195}//GEN-LAST:event_cancelActionPerformed
196
197    private void nextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nextActionPerformed
198
199        Runnable runnable = new Runnable() {
200
201            public void run() {
202                if (isEnded && standalone) {
203                    System.exit(0);
204                } else if (isEnded) {
205                    dispose();
206                }
207
208                try {
209                    lastpanel = content.getComponent(0);
210                    if (!isEnded && ((Wizardable) lastpanel).next()) {
211                        content.remove(lastpanel);
212                        back.setEnabled(true);
213                        content.add(contentlist.get(level + 1), BorderLayout.CENTER);
214                        ((Wizardable) contentlist.get(level + 1)).load();
215                    }
216
217                    level++;
218                } catch (Exception e) {
219                    next.setEnabled(false);
220                }
221
222                pack();
223            }
224        };
225        new Thread(runnable).start();
226}//GEN-LAST:event_nextActionPerformed
227
228    private void backActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_backActionPerformed
229        Runnable runnable = new Runnable() {
230
231            public void run() {
232                try {
233                    if (((Wizardable) content.getComponent(0)).back() && level > 0) {
234                        content.remove(lastpanel);
235                        content.validate();
236                        validate();
237                        repaint();
238                        content.add(contentlist.get(level - 1), BorderLayout.CENTER);
239                        level--;
240                    }
241                } catch (Exception e) {
242                    back.setEnabled(false);
243                }
244
245                pack();
246                validate();
247                repaint();
248            }
249        };
250        new Thread(runnable).start();
251}//GEN-LAST:event_backActionPerformed
252
253    /**
254     * Add a apanel to this wizard. Panels will be shown in the order they are added here,
255     * while first panel is shown first.
256     * @param panel
257     */
258    public void addPanel(Wizardable panel) {
259        JPanel pane = (JPanel) panel;
260//        (pane).setPreferredSize(new Dimension(content.getPreferredSize()));
261//        (pane).setBounds(0, 0, content.getWidth(), content.getHeight());
262//        if (pane.getWidth() > content.getWidth() || pane.getHeight() > content.getHeight()) {
263//            content.setMinimumSize(new Dimension(pane.getWidth(), pane.getHeight()));
264//            content.setPreferredSize(new Dimension(pane.getWidth(), pane.getHeight()));
265//            this.setMinimumSize(new Dimension(pane.getWidth() + 10, pane.getHeight() + 10));
266//            this.setPreferredSize(new Dimension(pane.getWidth() + 10, pane.getHeight() + 10));
267//        }
268        (pane).setOpaque(true);
269        contentlist.add(pane);
270    }
271
272    /**
273     * Notifies the wizard to show the "Finnish" button which closes the wizard
274     * @param end
275     */
276    public void isEnd(boolean end) {
277        this.isEnded = end;
278        if (end) {
279            next.setText(Messages.FINISH.toString());
280        }
281        cancel.setEnabled(!end);
282        back.setEnabled(!end);
283    }
284
285    /**
286     * ActionVars are temporay vars which are used between different panels of this wizrd.
287     * @return A PropertyStore which can be used to hold ActionVars
288     */
289    public PropertyStore getStore() {
290        return actionVars;
291    }
292
293    /**
294     * Set the message shown on the bottom of the wizard
295     * @param message
296     */
297    public void setMessage(String message) {
298        this.message.setText(message);
299    }
300
301    /**
302     * @param args the command line arguments
303     */
304    public static void main(String args[]) {
305        java.awt.EventQueue.invokeLater(new Runnable() {
306
307            public void run() {
308                new Wizard(true).setVisible(true);
309            }
310        });
311    }
312    // Variables declaration - do not modify//GEN-BEGIN:variables
313    private javax.swing.JButton back;
314    private javax.swing.JButton cancel;
315    private javax.swing.JPanel content;
316    private javax.swing.JPanel control;
317    private javax.swing.JScrollPane jScrollPane1;
318    private javax.swing.JScrollPane jScrollPane2;
319    private javax.swing.JTextPane message;
320    private javax.swing.JButton next;
321    private javax.swing.JProgressBar progressbar;
322    // End of variables declaration//GEN-END:variables
323
324    /**
325     *
326     * @return
327     */
328    public Wizardable getNext() {
329        return (Wizardable) contentlist.get(level + 1);
330    }
331
332    /**
333     * @return the progressbar
334     */
335    public javax.swing.JProgressBar getProgressbar() {
336        return progressbar;
337    }
338
339    public void enableBackButton(boolean enable) {
340        back.setEnabled(enable);
341    }
342
343    public void setCursor(int c) {
344       this.setCursor(Cursor.getPredefinedCursor(c));
345    }
346}