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.*;
025    
026    import javax.swing.*;
027    
028    import org.jivesoftware.smack.packet.Presence;
029    
030    import com.valhalla.gui.*;
031    import com.valhalla.jbother.*;
032    import com.valhalla.jbother.jabber.*;
033    
034    import com.valhalla.settings.Settings;
035    
036    /**
037     * Displays a dialog that allows you to change your priority
038     *
039     * @author Adam Olsen
040     * @version 1.0
041    */
042    public class PriorityDialog extends JDialog
043    {
044            private ResourceBundle resources = ResourceBundle.getBundle( "JBotherBundle", Locale.getDefault() );
045            private JLabel label = new JLabel( resources.getString( "priority" ) + ":    " );
046            private JTextField priorityBox = new JTextField( 4 );
047            private JButton         okButton = new JButton( resources.getString( "okButton" ) ),
048                                                    cancelButton = new JButton( resources.getString( "cancelButton" ) );
049    
050            private JPanel container = new JPanel();
051    
052            /**
053             * Default constructor
054            */
055            public PriorityDialog()
056            {
057                    super( BuddyList.getInstance(), "Set Priority", false );
058                    setTitle( resources.getString( "setPriority" ) );
059    
060                    String current = Settings.getInstance().getProperty( "priority" );
061                    if( current != null ) priorityBox.setText( current );
062    
063                    DialogTracker.addDialog( this, true,true );
064                    setContentPane( container );
065                    container.setBorder( BorderFactory.createEmptyBorder( 10, 35, 10, 35 ) );
066    
067                    container.setLayout( new BoxLayout( container, BoxLayout.Y_AXIS ) );
068                    JLabel setPriorityLabel = new JLabel( resources.getString( "setPriority" ) );
069                    setPriorityLabel.setBorder( BorderFactory.createEmptyBorder( 5, 10, 5, 0 ) );
070                    setPriorityLabel.setAlignmentX( Container.CENTER_ALIGNMENT );
071    
072                    container.add( setPriorityLabel );
073    
074    
075                    JPanel labelPanel = new JPanel();
076                    labelPanel.setLayout( new BoxLayout( labelPanel, BoxLayout.X_AXIS ) );
077                    labelPanel.add( label );
078                    labelPanel.add( priorityBox );
079                    container.add( labelPanel );
080    
081                    JPanel buttonPanel = new JPanel();
082                    buttonPanel.setLayout( new BoxLayout( buttonPanel, BoxLayout.X_AXIS ) );
083                    buttonPanel.setBorder( BorderFactory.createEmptyBorder( 5, 0, 5, 0 ) );
084                    buttonPanel.add( okButton );
085                    buttonPanel.add( cancelButton );
086    
087                    container.add( buttonPanel );
088    
089                    initComponents();
090                    pack();
091                    setLocationRelativeTo( null );
092            }
093    
094            /**
095             * Adds the various event listeners to the
096             * various components
097            */
098            private void initComponents()
099            {
100                    final PriorityDialog thisPointer = this;
101                    cancelButton.addActionListener( new ActionListener()
102                    {
103                            public void actionPerformed( ActionEvent e )
104                            {
105                                    DialogTracker.removeDialog( thisPointer );
106                            }
107                    } );
108    
109                    PriorityListener listener = new PriorityListener( this );
110    
111                    okButton.addActionListener( listener );
112                    priorityBox.addActionListener( listener );
113            }
114    
115            /**
116             * Listens for the OK button to be pressed and sends the presence packet.
117             * @author Adam Olsen
118             * @version 1.0
119            */
120            class PriorityListener implements ActionListener
121            {
122                    private PriorityDialog dialog;
123                    public PriorityListener( PriorityDialog dialog ) { this.dialog = dialog; }
124                    public void actionPerformed( ActionEvent e )
125                    {
126                            // this try block makes sure that the user entered a valid number greater than 0
127                            try {
128                                    if( Integer.parseInt( priorityBox.getText() ) < 1 ) throw new NumberFormatException();
129    
130                                    Settings.getInstance().setProperty( "priority", priorityBox.getText() );
131                                    BuddyList.getInstance().setStatus( BuddyList.getInstance().getCurrentPresenceMode(),
132                                            BuddyList.getInstance().getCurrentStatusString(), false );
133    
134                                    DialogTracker.removeDialog( dialog );
135    
136                            }
137                            catch( NumberFormatException nfe )
138                            {
139                                    Standard.warningMessage( null, resources.getString( "setPriority" ),
140                                            resources.getString( "specifyGreaterThanZero" ) );
141                            }
142                    }
143            }
144    }