001    /*
002     Copyright (C) 2003 Adam Olsen
003     This program is free software; you can redistribute it and/or modify
004     it under the terms of the GNU General Public License as published by
005     the Free Software Foundation; either version 1, or (at your option)
006     any later version.
007     This program is distributed in the hope that it will be useful,
008     but WITHOUT ANY WARRANTY; without even the implied warranty of
009     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
010     GNU General Public License for more details.
011     You should have received a copy of the GNU General Public License
012     along with this program; if not, write to the Free Software
013     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
014     */
015    package com.valhalla.jbother;
016    
017    import javax.swing.*;
018    import java.awt.*;
019    import java.awt.event.ActionListener;
020    import java.awt.event.ActionEvent;
021    import com.valhalla.jbother.jabber.smack.*;
022    import java.util.*;
023    
024    import com.valhalla.gui.*;
025    
026    /**
027     *  as Dialogs for sending and receiving files will be very similar, this is the
028     *  basic window with 3 areas: To/From field, Date field, Filename field,
029     *  Description field, size field and two buttons: Reject/Cancel & Accept/Send
030     *
031     *@author     Lukasz Wiechec
032     *@created    Feb 2, 2005 4:27:37 PM
033     */
034    
035    public abstract class FileDialog extends JFrame implements ActionListener {
036        protected ResourceBundle resources = ResourceBundle.getBundle(
037                "JBotherBundle", Locale.getDefault());
038        /**
039         * The button that corresponds with accept or send, depending on the
040         * transfer type
041         */
042        protected JButton ayeButton = new JButton();
043        /**
044         * The button that corresponds with deny or cancel, depending on the
045         * transfer type
046         */
047        protected JButton nayButton = new JButton();
048        
049        /**
050         * The from or to label
051         */
052        protected JLabel fromToLabel = new JLabel();
053        /**
054         * The from or to field
055         */
056        protected MJTextField fromToTF = new MJTextField();
057        /**
058         * The description of the file
059         */
060        protected MJTextArea descriptionArea = new MJTextArea();
061    
062        /**
063         * The file being transferred
064         */
065        protected MJTextField fileTF = new MJTextField();
066    
067        /**
068         * The status of the file
069         */
070        protected JLabel statusLabel = new JLabel("");
071    
072        /**
073         * A file chooser to pick the file to send or save to
074         */
075        protected static JFileChooser fileChooser;
076    
077        protected JFrame parent;
078    
079        protected GridBagLayout grid = new GridBagLayout();
080        protected GridBagConstraints c = new GridBagConstraints();
081        protected JPanel main;
082        protected JPanel topPanel = new JPanel(grid);
083    
084        /**
085         * Constructs a file dialog
086         */
087        public FileDialog()
088        {
089            setIconImage(Standard.getImage("frameicon.png"));
090    
091            parent = BuddyList.getInstance().getContainerFrame();
092    
093            initialize();
094        }
095    
096    
097        /**
098         *  initialize the dialog
099         */
100        private void initialize() {
101            getRootPane().setDefaultButton(ayeButton);
102            // set up the layout
103            main = (JPanel)getContentPane();
104            main.setLayout(new BorderLayout());
105            main.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
106            main.add(topPanel, BorderLayout.NORTH);
107            c.gridx = 0;
108            c.gridy = 0;
109            c.insets = new Insets(0, 0, 5, 0);
110    
111            addComponent(fromToLabel, fromToTF);
112            addComponent(new JLabel("File: "), fileTF);
113    
114            main.add(new JScrollPane(descriptionArea), BorderLayout.CENTER);
115    
116            JPanel buttonPanel = new JPanel();
117            buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS));
118            buttonPanel.setBorder(BorderFactory.createEmptyBorder(5, 0, 0, 0));
119            buttonPanel.add(Box.createHorizontalGlue());
120            buttonPanel.add(nayButton);
121            buttonPanel.add(ayeButton);
122    
123            main.add(buttonPanel, BorderLayout.SOUTH);
124    
125            // set the buttons
126            ayeButton.setActionCommand("aye");
127            ayeButton.addActionListener(this);
128            nayButton.setActionCommand("nay");
129            nayButton.addActionListener(this);
130    
131            setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
132    
133            // add to DialogTracker so that when the connection is lost, it will be closed
134            DialogTracker.addDialog(this, true, true);
135    
136            setSize(450, 300);
137            setFocusable(true);
138            Standard.cascadePlacement(this);
139    
140        }
141    
142        /**
143         * Adds a field to the dialog form, and sets up the gridbag for the next
144         * element
145         * @param label the label corresponding to this field
146         * @param field the text field 
147         */ 
148        protected void addComponent(JLabel label, JTextField field)
149        {
150            label.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 5));
151            c.weightx = 0;
152            c.gridx = 0;
153            c.anchor = GridBagConstraints.WEST;
154            c.fill = GridBagConstraints.NONE;
155            grid.setConstraints(label, c);
156            topPanel.add(label);
157            c.weightx = 1;
158            c.gridx++;
159            c.fill = GridBagConstraints.HORIZONTAL;
160            grid.setConstraints(field, c);
161            topPanel.add(field);
162            c.gridy++;
163        }
164    
165        /**
166         * Called when the users clicks the 'aye' or 'nay' button
167         */
168        public void actionPerformed(java.awt.event.ActionEvent e) {
169            if (e.getActionCommand().equals("aye")) {
170                doAye();
171            } else if (e.getActionCommand().equals("nay")) {
172                doNay();
173            }
174        }
175    
176    
177        /**
178         *  Disables all the form fields in this dialog (for fields that cannot be
179         *  changed
180         */
181        protected void disableAll() {
182            ayeButton.setEnabled(false);
183            fromToTF.setEditable(false);
184            descriptionArea.setEditable(false);
185            fileTF.setEditable(false);
186        }
187    
188        /**
189         *  method called when "Aye" button is pressed to be implemented in child
190         *  class
191         */
192        protected abstract void doAye();
193    
194    
195        /**
196         *  method called when "Nay" button is pressed to be implemented in child
197         *  class
198         */
199        protected abstract void doNay();
200    }