001    /*
002     * ConfigurationWizard.java
003     *
004     * Created on May 20, 2003, 5:13 PM
005     *
006     * This file is part of the STAR Scheduler.
007     * Copyright (c) 2002-2006 STAR Collaboration - Brookhaven National Laboratory
008     *
009     * STAR Scheduler is free software; you can redistribute it and/or modify
010     * it under the terms of the GNU General Public License as published by
011     * the Free Software Foundation; either version 2 of the License, or
012     * (at your option) any later version.
013     *
014     * STAR Scheduler is distributed in the hope that it will be useful,
015     * but WITHOUT ANY WARRANTY; without even the implied warranty of
016     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
017     * GNU General Public License for more details.
018     *
019     * You should have received a copy of the GNU General Public License
020     * along with STAR Scheduler; if not, write to the Free Software
021     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
022     */
023    
024    package gov.bnl.star.offline.scheduler.config;
025    
026    import java.awt.BorderLayout;
027    import java.awt.Component;
028    import java.awt.Font;
029    import java.awt.GridBagConstraints;
030    import java.beans.PropertyChangeListener;
031    import java.io.File;
032    import java.io.FileWriter;
033    import java.io.Writer;
034    import java.util.ArrayList;
035    import java.util.Hashtable;
036    import java.util.Iterator;
037    import java.util.List;
038    import java.util.Map;
039    import javax.swing.JLabel;
040    import javax.swing.JOptionPane;
041    import javax.swing.JPanel;
042    
043    /**
044     *
045     * @author  Gabriele Carcassi
046     */
047    public class ConfigurationWizard extends javax.swing.JFrame implements PropertyChangeListener{
048        private int currentPanel = 0;
049        private List panels;
050        
051        /** Creates new form ConfigurationWizard */
052        public ConfigurationWizard() {
053            initComponents();
054            panels = new ArrayList();
055            panels.add(new ProgramLocation());
056            panels.add(new LogPanel());
057            panels.add(new PassivePolicyPanel());
058            panels.add(new LSFDispatcherPanel());
059            panels.add(new StatisticsPanel());
060            panels.add(new FileCatalogPanel());
061            for (int i = 0; i < panels.size(); i++) {
062                ((JPanel) panels.get(i)).addPropertyChangeListener(this);
063            }
064            initSteps();
065            changePanel();
066        }
067        
068        private Font font;
069        private Font bigFont;
070        private List labels;
071        private void initSteps() {
072            labels = new ArrayList();
073            JLabel label1 = new JLabel();
074            Font oldFont = label1.getFont();
075            font = new Font(oldFont.getName(), Font.PLAIN, oldFont.getSize());
076            bigFont = new Font(oldFont.getName(), Font.BOLD, oldFont.getSize());
077            for (int i = 0; i < panels.size()-1; i++) {
078                JLabel label = new JLabel();
079                label.setFont(font);
080                label.setText(((WizardPanel) panels.get(i)).getTitle());
081                GridBagConstraints gridBagConstraints = new java.awt.GridBagConstraints();
082                gridBagConstraints.gridx = 0;
083                gridBagConstraints.gridy = i+1;
084                gridBagConstraints.insets = new java.awt.Insets(5, 10, 0, 5);
085                gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
086                steps.add(label, gridBagConstraints);
087                labels.add(label);
088            }
089            
090            JLabel label = new JLabel();
091            label.setFont(font);
092            label.setText(((WizardPanel) panels.get(panels.size()-1)).getTitle());
093            GridBagConstraints gridBagConstraints = new java.awt.GridBagConstraints();
094            gridBagConstraints.gridx = 0;
095            gridBagConstraints.gridy = panels.size();
096            gridBagConstraints.insets = new java.awt.Insets(5, 10, 5, 5);
097            gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
098            gridBagConstraints.weightx = 1.0;
099            gridBagConstraints.weighty = 1.0;
100            steps.add(label, gridBagConstraints);
101            labels.add(label);
102        }
103        
104        private void changePanel() {
105            if (currentPanel == 0) backButton.setEnabled(false);
106            else backButton.setEnabled(true);
107            if (currentPanel == panels.size() -1) nextButton.setText("Finish");
108            else nextButton.setText("Next >");
109            if (content.getComponentCount() > 0)
110                content.remove(0);
111            content.add((JPanel) panels.get(currentPanel), BorderLayout.CENTER);
112            nextButton.setEnabled(((WizardPanel) panels.get(currentPanel)).isReady());
113            content.revalidate();
114            content.repaint();
115            JLabel label = (JLabel) labels.get(currentPanel);
116            label.setFont(bigFont);
117            if (currentPanel != 0) {
118                JLabel label1 = (JLabel) labels.get(currentPanel-1);
119                label1.setFont(font);
120            }
121            if (currentPanel != labels.size()-1) {
122                JLabel label1 = (JLabel) labels.get(currentPanel+1);
123                label1.setFont(font);
124            }
125        }
126        
127        /** This method is called from within the constructor to
128         * initialize the form.
129         * WARNING: Do NOT modify this code. The content of this method is
130         * always regenerated by the Form Editor.
131         */
132        private void initComponents() {//GEN-BEGIN:initComponents
133            java.awt.GridBagConstraints gridBagConstraints;
134    
135            jEditorPane1 = new javax.swing.JEditorPane();
136            jSeparator1 = new javax.swing.JSeparator();
137            jPanel1 = new javax.swing.JPanel();
138            backButton = new javax.swing.JButton();
139            cancelButton = new javax.swing.JButton();
140            nextButton = new javax.swing.JButton();
141            content = new javax.swing.JPanel();
142            steps = new javax.swing.JPanel();
143            jLabel1 = new javax.swing.JLabel();
144    
145            jEditorPane1.setContentType("text/html");
146            jEditorPane1.setPreferredSize(new java.awt.Dimension(150, 200));
147    
148            getContentPane().setLayout(new java.awt.GridBagLayout());
149    
150            setTitle("Scheduler Configuration Wizard");
151            addWindowListener(new java.awt.event.WindowAdapter() {
152                public void windowClosing(java.awt.event.WindowEvent evt) {
153                    exitForm(evt);
154                }
155            });
156    
157            gridBagConstraints = new java.awt.GridBagConstraints();
158            gridBagConstraints.gridx = 0;
159            gridBagConstraints.gridy = 1;
160            gridBagConstraints.gridwidth = 2;
161            gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
162            gridBagConstraints.ipadx = 1;
163            getContentPane().add(jSeparator1, gridBagConstraints);
164    
165            jPanel1.setLayout(new java.awt.GridBagLayout());
166    
167            backButton.setText("< Back");
168            backButton.addActionListener(new java.awt.event.ActionListener() {
169                public void actionPerformed(java.awt.event.ActionEvent evt) {
170                    backButtonActionPerformed(evt);
171                }
172            });
173    
174            gridBagConstraints = new java.awt.GridBagConstraints();
175            gridBagConstraints.gridx = 0;
176            gridBagConstraints.gridy = 0;
177            gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
178            gridBagConstraints.weightx = 1.0;
179            jPanel1.add(backButton, gridBagConstraints);
180    
181            cancelButton.setText("Cancel");
182            gridBagConstraints = new java.awt.GridBagConstraints();
183            gridBagConstraints.gridx = 2;
184            gridBagConstraints.gridy = 0;
185            gridBagConstraints.insets = new java.awt.Insets(0, 5, 0, 0);
186            jPanel1.add(cancelButton, gridBagConstraints);
187    
188            nextButton.setText("Next >");
189            nextButton.addActionListener(new java.awt.event.ActionListener() {
190                public void actionPerformed(java.awt.event.ActionEvent evt) {
191                    nextButtonActionPerformed(evt);
192                }
193            });
194    
195            gridBagConstraints = new java.awt.GridBagConstraints();
196            gridBagConstraints.gridx = 1;
197            gridBagConstraints.gridy = 0;
198            jPanel1.add(nextButton, gridBagConstraints);
199    
200            gridBagConstraints = new java.awt.GridBagConstraints();
201            gridBagConstraints.gridx = 0;
202            gridBagConstraints.gridy = 2;
203            gridBagConstraints.gridwidth = 2;
204            gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
205            gridBagConstraints.weightx = 1.0;
206            gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
207            getContentPane().add(jPanel1, gridBagConstraints);
208    
209            content.setLayout(new java.awt.BorderLayout());
210    
211            gridBagConstraints = new java.awt.GridBagConstraints();
212            gridBagConstraints.gridx = 1;
213            gridBagConstraints.gridy = 0;
214            gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
215            gridBagConstraints.weighty = 1.0;
216            gridBagConstraints.insets = new java.awt.Insets(5, 0, 5, 5);
217            getContentPane().add(content, gridBagConstraints);
218    
219            steps.setLayout(new java.awt.GridBagLayout());
220    
221            steps.setBackground(new java.awt.Color(255, 255, 255));
222            steps.setPreferredSize(new java.awt.Dimension(150, 10));
223            jLabel1.setText("Scheduler configuration");
224            gridBagConstraints = new java.awt.GridBagConstraints();
225            gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
226            gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 5);
227            steps.add(jLabel1, gridBagConstraints);
228    
229            gridBagConstraints = new java.awt.GridBagConstraints();
230            gridBagConstraints.gridx = 0;
231            gridBagConstraints.gridy = 0;
232            gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
233            getContentPane().add(steps, gridBagConstraints);
234    
235            java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
236            setBounds((screenSize.width-640)/2, (screenSize.height-480)/2, 640, 480);
237        }//GEN-END:initComponents
238        
239        private void backButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_backButtonActionPerformed
240            currentPanel--;
241            changePanel();
242        }//GEN-LAST:event_backButtonActionPerformed
243        
244        private void nextButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nextButtonActionPerformed
245            currentPanel++;
246            if (currentPanel == panels.size()) finish();
247            else changePanel();
248        }//GEN-LAST:event_nextButtonActionPerformed
249        
250        /** Exit the Application */
251        private void exitForm(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_exitForm
252            System.exit(0);
253        }//GEN-LAST:event_exitForm
254        
255        public void finish() {
256            Map props = new Hashtable();
257            for (int i = 0; i < panels.size(); i++) {
258                ((WizardPanel) panels.get(i)).addProperties(props);
259            }
260            try {
261            Writer wr = new FileWriter("scheduler.properties");
262            for (Iterator iter = props.keySet().iterator(); iter.hasNext();) {
263                String prop = (String) iter.next();
264                String value = props.get(prop).toString();
265                wr.write(prop);
266                wr.write(" = ");
267                wr.write(value);
268                wr.write("\n");
269            }
270            wr.close();
271            } catch (Exception e) {
272                e.printStackTrace();
273                JOptionPane.showMessageDialog(this, "Unable to save properties.\n"+e.getMessage());
274            }
275            System.exit(0);
276        }
277        
278        /**
279         * @param args the command line arguments
280         */
281        public static void main(String args[]) {
282            new ConfigurationWizard().show();
283        }
284        
285        public void propertyChange(java.beans.PropertyChangeEvent evt) {
286            if (evt.getPropertyName().equals("ready"))
287                nextButton.setEnabled(((WizardPanel) panels.get(currentPanel)).isReady());
288        }    
289        
290        // Variables declaration - do not modify//GEN-BEGIN:variables
291        private javax.swing.JButton backButton;
292        private javax.swing.JButton cancelButton;
293        private javax.swing.JPanel content;
294        private javax.swing.JEditorPane jEditorPane1;
295        private javax.swing.JLabel jLabel1;
296        private javax.swing.JPanel jPanel1;
297        private javax.swing.JSeparator jSeparator1;
298        private javax.swing.JButton nextButton;
299        private javax.swing.JPanel steps;
300        // End of variables declaration//GEN-END:variables
301        
302    }