001    /* BEE - Book Editing Environment for PAVLOV
002     * Copyright (C) 2004 T.J. Willis
003     * 
004     * This program is free software; you can redistribute it and/or
005     * modify it under the terms of the GNU General Public License
006     * as published by the Free Software Foundation; either version 2
007     * of the License, or (at your option) any later version.
008     * 
009     * This program is distributed in the hope that it will be useful,
010     * but WITHOUT ANY WARRANTY; without even the implied warranty of
011     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012     * GNU General Public License for more details.
013     * 
014     * You should have received a copy of the GNU General Public License
015     * along with this program; if not, write to the Free Software
016     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
017     *
018     * $Header: /cvsroot/pavlov/net/sourceforge/bee/WelcomeDialog.java,v 1.1 2004/05/10 14:50:13 tj_willis Exp $
019     */
020    package net.sourceforge.bee;
021    
022    import javax.swing.*;
023    import java.awt.*;
024    import java.awt.event.*;
025    import net.sourceforge.pavlov.zipUtilities.XMLFileFilter;
026    import net.sourceforge.pavlov.library.Book;
027    import java.io.File;
028    
029    /**
030     * Welcomes user and streamlines process of opening new or existing book.
031     *
032     * @author <a href="mailto:"></a>
033     * @version 1.0
034     */
035    public class WelcomeDialog
036    extends JInternalFrame
037    implements ActionListener
038    {
039        private JPanel p3;
040        private Container content;
041        private JRadioButton newBook, existingBook;
042        private ButtonGroup buttonGroup;
043        private JTextField fTitle, fAuthor;
044        private JTextArea aDescription;
045        private static int CREATE_BOOK =0;
046        private static int OPEN_BOOK =1;
047        private static int mode = OPEN_BOOK;
048        private JFileChooser chooser;
049        private Bee bee;
050    
051        /**
052         * Creates a new <code>WelcomeDialog</code> instance.
053         *
054         * @param b a <code>Bee</code> value
055         */
056        public WelcomeDialog(Bee b)
057        {
058            super("Welcome to BEE!", true, true, true);
059            bee=b;
060            //setClosable(true);
061            content = getContentPane();
062            content.setLayout(new BorderLayout());
063    
064            Box b1 = new Box(BoxLayout.Y_AXIS);
065            
066            String msg1 = new String();
067    //      msg1 +="<html><head><meta http-equiv=\"content-type\" content=\"text/html; charset=ISO-8859-1\"></head>"; 
068    //      msg1 +="<body><div align=\"Center\"><h1>Welcome to BEE!</h1>";
069    //      msg1 +="BEE is the Book Editing Environment for Pavlov.  You can use BEE to <br>";
070    //      msg1+="edit and create Books of questions for use with Pavlov.  To get started,<br>";
071    //      msg1+="click \"Open Existing Book\" or \"Create New Book.\"<br>";    
072    //      msg1+="</div></body></html>";
073    
074    
075            JPanel pp = new JPanel();
076            msg1 +="<html>"; 
077            msg1 +="<h1>Welcome to BEE!</h1>";
078            msg1 +="BEE is the Book Editing Environment for Pavlov.  You can use BEE to <br>";
079            msg1+="edit and create Books of questions for use with Pavlov.  To get started,<br>";
080            msg1+="click \"Open Existing Book\" or \"Create New Book.\"<br>";    
081            msg1+="</html>";
082            JLabel l1 = new JLabel(msg1,SwingConstants.CENTER);
083            pp.add(l1);
084            b1.add(pp);
085    
086    
087    
088            JPanel p2 = new JPanel();
089            p2.setLayout(new FlowLayout());
090            p2.setBorder(BorderFactory.createEtchedBorder());
091            newBook = new JRadioButton("Create New Book",false);
092            existingBook = new JRadioButton("Open Existing Book",true);
093            newBook.addActionListener(this);
094            existingBook.addActionListener(this);
095            buttonGroup = new ButtonGroup();
096            buttonGroup.add(newBook);
097            buttonGroup.add(existingBook);
098    
099            p2.add(newBook);
100            p2.add(existingBook);
101            b1.add(p2);
102            content.add(b1,BorderLayout.NORTH);
103    
104            openExisting();
105    
106    
107    //      JPanel p4 = new JPanel();
108    //      p4.setLayout(new FlowLayout());
109    
110    //      JButton openBook = new JButton("Open Book");
111    //      p4.add(openBook);
112    
113    //      content.add(p4,BorderLayout.SOUTH);
114    
115            pack();
116            setVisible(true);
117    
118        }
119    
120    
121        /**
122         * Opens an existing book for editing.
123         *
124         */
125        protected void openExisting()
126        {
127            mode = OPEN_BOOK;
128            if(p3==null){
129                p3 = new JPanel();
130                content.add(p3,BorderLayout.CENTER);
131            } else {
132                p3.removeAll();
133            }
134            p3.setLayout( new FlowLayout());
135    
136            p3.setBorder(BorderFactory.createTitledBorder("Choose Existing Book File"));
137            if(chooser==null){
138                chooser = new JFileChooser();
139                chooser.addActionListener(this);
140                XMLFileFilter filter = new XMLFileFilter();
141                chooser.setFileFilter(filter);
142                chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES  );
143            }
144            chooser.setBorder(BorderFactory.createTitledBorder(""));
145            chooser.setApproveButtonText("Edit This Book File");
146            p3.add(chooser);
147            pack();
148        }
149    
150    
151        /**
152         * Creates a new book and opens it for editing.
153         *
154         */
155        protected void createNew()
156        {
157            mode = CREATE_BOOK;
158            if(p3==null){
159                p3 = new JPanel();
160    
161            } else {
162                p3.removeAll();
163            }
164            p3.setBorder(BorderFactory.createTitledBorder("Create a New Book"));
165            //CardLayout card = new CardLayout();
166            p3.setLayout(new BorderLayout());
167    
168            JTabbedPane tp = new JTabbedPane();
169    
170            JPanel pan4 = new JPanel();
171            pan4.setLayout(new BorderLayout());
172    
173            //JPanel b0 = new JPanel();
174            //b0.setLayout(new BorderLayout()); //Box(BoxLayout.Y_AXIS);
175    
176            JPanel pan1 = new JPanel();
177            pan1.setLayout(new java.awt.GridLayout(2,1));
178    
179            Box b1 = new Box(BoxLayout.X_AXIS);
180            b1.setBorder( BorderFactory.createTitledBorder( "Title" ) );
181            fTitle = new JTextField();
182            fTitle.addActionListener(this);
183            b1.add(fTitle);
184    
185            Box b2 = new Box(BoxLayout.X_AXIS);
186            b2.setBorder( BorderFactory.createTitledBorder( "Author" ) );
187            fAuthor = new JTextField();
188            fAuthor.addActionListener(this);
189            b2.add(fAuthor);
190    
191            pan1.add(b1);
192            pan1.add(b2);
193    
194            pan4.add(pan1,BorderLayout.NORTH);
195    
196            Box b3 = new Box(BoxLayout.X_AXIS);
197            b3.setBorder( BorderFactory.createTitledBorder( "Description" ) );
198            aDescription = new JTextArea(5,45);
199            aDescription.setLineWrap(true);
200            JScrollPane sp1 = new JScrollPane(aDescription);
201            //sp1.setMinimumSize(new Dimension(360,80));
202            //bUpdate = new JButton("Update");
203            //bUpdate.addActionListener(this);
204    
205    
206            
207            b3.add(sp1);
208            pan4.add(b3,BorderLayout.CENTER);
209            //.addLayoutComponent("Step One",pan4);
210            tp.addTab("Step One",pan4);//, new String("One"));
211    
212            if(chooser==null){
213                chooser = new JFileChooser();
214                chooser.addActionListener(this);
215                XMLFileFilter filter = new XMLFileFilter();
216                chooser.setFileFilter(filter);
217                chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES  );
218            }
219            chooser.setBorder(BorderFactory.createTitledBorder("Save to Book File"));
220            chooser.setApproveButtonText("Save Book To This File");
221            //p3.add(chooser,BorderLayout.SOUTH);
222            //card.addLayoutComponent("Two",chooser);
223            tp.add("Step Two",chooser);
224    
225            //pack();
226    
227            //b3.add(bUpdate);
228            //p3.add(b0);
229            //p3.add(new JLabel("Create New Panel"));
230            p3.add(tp,BorderLayout.CENTER);
231            content.add(p3,BorderLayout.CENTER);
232            pack();
233        }
234    
235        /**
236         * Handles UI events.
237         *
238         * @param e an <code>ActionEvent</code> value
239         */
240        public void actionPerformed(ActionEvent e)
241        {
242            Object o = e.getSource();
243            String x = e.getActionCommand();
244            System.out.println(x);
245            if(x.equals(JFileChooser.APPROVE_SELECTION)){
246                if(mode==CREATE_BOOK){
247                    File f = chooser.getSelectedFile();
248    //              if(!checkCreateFile(f)) {
249    //                  //show a dialog
250    //                  return;
251    //              }
252                    this.dispose();
253                    Book b = Book.makeBlankBook();
254                    b.setName(fTitle.getText());
255                    b.setAuthor(fAuthor.getText());
256                    b.setDescription(aDescription.getText());
257                    bee.newBook(f,b);
258                    
259                } else if (mode==OPEN_BOOK) {
260                    File f = chooser.getSelectedFile();
261                    this.dispose();
262                    bee.openBook(f);
263                } else {
264                    System.out.println("Invalid mode");
265                }
266                return;
267            } else if (x.equals(JFileChooser.CANCEL_SELECTION)){
268                this.dispose();
269            }
270            if(o.equals(existingBook))
271                openExisting();
272            else if(o.equals(newBook))
273                createNew();
274            //else
275            //    super.actionPerformed(e);
276    
277        }
278    }