001    /*
002     Copyright (C) 2003 Adam Olsen
003    
004     This program is free software; you can redistribute it and/or modify
005     it under the terms of the GNU General Public License as published by
006     the Free Software Foundation; either version 1, or (at your option)
007     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., 675 Mass Ave, Cambridge, MA 02139, USA.
017     */
018    
019    package com.valhalla.jbother;
020    
021    import java.awt.Container;
022    import java.awt.event.ActionEvent;
023    import java.awt.event.ActionListener;
024    import java.util.Locale;
025    import java.util.ResourceBundle;
026    
027    import javax.swing.BorderFactory;
028    import javax.swing.BoxLayout;
029    import javax.swing.JButton;
030    import javax.swing.JDialog;
031    import javax.swing.JFrame;
032    import javax.swing.JLabel;
033    import javax.swing.JPanel;
034    
035    import com.valhalla.gui.DialogTracker;
036    import com.valhalla.gui.MJTextField;
037    import com.valhalla.gui.Standard;
038    import com.valhalla.settings.Settings;
039    
040    /**
041     * Displays a dialog that allows you to change your priority
042     *
043     * @author Adam Olsen
044     * @version 1.0
045     */
046    public class PriorityDialog extends JDialog {
047        private ResourceBundle resources = ResourceBundle.getBundle(
048                "JBotherBundle", Locale.getDefault());
049    
050        private JLabel label = new JLabel(resources.getString("priority") + ":    ");
051    
052        private MJTextField priorityBox = new MJTextField(4);
053    
054        private JButton okButton = new JButton(resources.getString("okButton")),
055                cancelButton = new JButton(resources.getString("cancelButton"));
056    
057        private JPanel container = new JPanel();
058    
059        /**
060         * Default constructor
061         */
062        public PriorityDialog() {
063            super(BuddyList.getInstance().getContainerFrame(), "Set Priority", false);
064            setTitle(resources.getString("setPriority"));
065    
066            String current = Settings.getInstance().getProperty("priority");
067            if (current != null)
068                priorityBox.setText(current);
069    
070            DialogTracker.addDialog(this, true, true);
071            setContentPane(container);
072            container.setBorder(BorderFactory.createEmptyBorder(10, 35, 10, 35));
073    
074            container.setLayout(new BoxLayout(container, BoxLayout.Y_AXIS));
075            JLabel setPriorityLabel = new JLabel(resources.getString("setPriority"));
076            setPriorityLabel
077                    .setBorder(BorderFactory.createEmptyBorder(5, 10, 5, 0));
078            setPriorityLabel.setAlignmentX(Container.CENTER_ALIGNMENT);
079    
080            container.add(setPriorityLabel);
081    
082            JPanel labelPanel = new JPanel();
083            labelPanel.setLayout(new BoxLayout(labelPanel, BoxLayout.X_AXIS));
084            labelPanel.add(label);
085            labelPanel.add(priorityBox);
086            container.add(labelPanel);
087    
088            JPanel buttonPanel = new JPanel();
089            buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS));
090            buttonPanel.setBorder(BorderFactory.createEmptyBorder(5, 0, 5, 0));
091            buttonPanel.add(okButton);
092            buttonPanel.add(cancelButton);
093    
094            container.add(buttonPanel);
095    
096            initComponents();
097            pack();
098            setLocationRelativeTo(null);
099        }
100    
101        /**
102         * Adds the various event listeners to the various components
103         */
104        private void initComponents() {
105            cancelButton.addActionListener(new ActionListener() {
106                public void actionPerformed(ActionEvent e) {
107                    DialogTracker.removeDialog(PriorityDialog.this);
108                }
109            });
110    
111            PriorityListener listener = new PriorityListener(this);
112    
113            okButton.addActionListener(listener);
114            priorityBox.addActionListener(listener);
115        }
116    
117        /**
118         * Listens for the OK button to be pressed and sends the presence packet.
119         *
120         * @author Adam Olsen
121         * @version 1.0
122         */
123        class PriorityListener implements ActionListener {
124            private PriorityDialog dialog;
125    
126            public PriorityListener(PriorityDialog dialog) {
127                this.dialog = dialog;
128            }
129    
130            public void actionPerformed(ActionEvent e) {
131                // this try block makes sure that the user entered a valid number
132                // greater than 0
133                try {
134                    if (Integer.parseInt(priorityBox.getText()) < 1)
135                        throw new NumberFormatException();
136    
137                    Settings.getInstance().setProperty("priority",
138                            priorityBox.getText());
139                    BuddyList.getInstance()
140                            .setStatus(
141                                    BuddyList.getInstance()
142                                            .getCurrentPresenceMode(),
143                                    BuddyList.getInstance()
144                                            .getCurrentStatusString(), false);
145    
146                    DialogTracker.removeDialog(dialog);
147    
148                } catch (NumberFormatException nfe) {
149                    Standard.warningMessage(null, resources
150                            .getString("setPriority"), resources
151                            .getString("specifyGreaterThanZero"));
152                }
153            }
154        }
155    }