001    /////////////////////////////////////////////////
002    // This file is part of Sears project.
003    // Subtitle Editor And Re-Synch
004    // A tool to easily modify and resynch movies subtitles.
005    /////////////////////////////////////////////////
006    //This program is free software; 
007    //you can redistribute it and/or modify it under the terms 
008    //of the GNU General Public License 
009    //as published by the Free Software Foundation; 
010    //either version 2 of the License, or (at your option) any later version.
011    /////////////////////////////////////////////////
012    //Sears project is available under sourceforge
013    // at adress: http://sourceforge.net/projects/sears/
014    //Copyright (C) 2005 Booba Skaya
015    //Mail: booba.skaya@gmail.com
016    ////////////////////////////////////////////////
017    package sears.gui;
018    
019    import java.awt.Dimension;
020    import java.awt.Point;
021    import java.awt.event.WindowEvent;
022    import java.awt.event.WindowListener;
023    import javax.swing.JButton;
024    import javax.swing.JDialog;
025    import javax.swing.JPanel;
026    import sears.tools.SearsProperties;
027    import sears.tools.SearsResourceBundle;
028    
029    /**
030     * Class SearsJDialog.
031     * <br><b>Summary:</b><br>
032     * This class defines the general comportement of the Sears JDialog.
033     */
034    public abstract class SearsJDialog extends JDialog implements WindowListener {
035        protected JPanel jPanelButtons = null;
036    
037        protected JButton jButtonOk = null;
038    
039        protected JButton jButtonCancel = null;
040    
041        /**A boolean to know if user has validated*/
042        protected boolean validationStatus;
043    
044        /**
045         * Constructor SearsJDialog.
046         * <br><b>Summary:</b><br>
047         * Constructor of the class.
048         * @param title     The title of the JDialog.
049         */
050        public SearsJDialog(String title) {
051            super(MainWindow.instance, title, true);
052            //manage closing event.
053            this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
054            addWindowListener(this);
055            configureSize();
056        }
057    
058            /**
059             * Method configureSize.
060             * <br><b>Summary:</b><br>
061             * This method permits to restore dialog size, or pack it for the first launch.
062             */
063            protected void configureSize() {
064                    //try to restaure the dimension and position
065            Dimension restauredDimension = null;
066            Point location = null;
067            try {
068                int width = Integer.parseInt(SearsProperties.getProperty(getDialogName() + SearsProperties.SUFFIX_WIDTH));
069                int heigth = Integer.parseInt(SearsProperties.getProperty(getDialogName() + SearsProperties.SUFFIX_HEIGTH));
070                restauredDimension = new Dimension(width, heigth);
071                int posX = Integer.parseInt(SearsProperties.getProperty(getDialogName() + SearsProperties.SUFFIX_POSX));
072                int posY = Integer.parseInt(SearsProperties.getProperty(getDialogName() + SearsProperties.SUFFIX_POSY));
073                location = new Point(posX, posY);
074                //set the size
075                setSize(restauredDimension);
076            } catch (NumberFormatException e) {
077                //use packed size.
078                pack();
079            }
080            //set the location
081            if (location == null) {
082                setLocationRelativeTo(MainWindow.instance);
083            } else {
084                setLocation(location);
085            }
086            }
087    
088        /**
089         * Method getDialogName.
090         * <br><b>Summary:</b><br>
091         * This method return the dialog name.
092         * It will be used to save the dialog dimensions in the config file.
093         * @return  String      The dialog name.
094         */
095        protected abstract String getDialogName();
096    
097        /**
098         * This method initializes jButton  
099         *  
100         * @return javax.swing.JButton  
101         */
102        protected JButton getJButtonCancel() {
103            if (jButtonCancel == null) {
104                jButtonCancel = new JButton(SearsResourceBundle.getResource("button_cancel"));
105                jButtonCancel.addActionListener(new java.awt.event.ActionListener() {
106                    public void actionPerformed(java.awt.event.ActionEvent e) {
107                        cancelAction();
108                    }
109                });
110            }
111            return jButtonCancel;
112        }
113    
114        /**
115         * Method cancelAction.
116         * <br><b>Summary:</b><br>
117         * This method is called when user wants to cancel split dialog.
118         */
119        protected void cancelAction() {
120            validationStatus = false;
121            //saveDialogProperties();
122            dispose();
123        }
124    
125        /**
126         * This method initializes jPanel   
127         *  
128         * @return javax.swing.JPanel   
129         */
130        protected JPanel getJPanelButtons() {
131            if (jPanelButtons == null) {
132                jPanelButtons = new JPanel();
133                jPanelButtons.add(getJButtonOk(), null);
134                jPanelButtons.add(getJButtonCancel(), null);
135            }
136            return jPanelButtons;
137        }
138    
139        /**
140         * This method initializes jButton  
141         *  
142         * @return javax.swing.JButton  
143         */
144        private JButton getJButtonOk() {
145            if (jButtonOk == null) {
146                jButtonOk = new JButton(SearsResourceBundle.getResource("button_ok"));
147                jButtonOk.addActionListener(new java.awt.event.ActionListener() {
148                    public void actionPerformed(java.awt.event.ActionEvent e) {
149                        okAction();
150                    }
151                });
152            }
153            return jButtonOk;
154        }
155    
156        /**
157         * Method okAction.
158         * <br><b>Summary:</b><br>
159         * This method is called when user validate the dialog.
160         */
161        protected void okAction() {
162            //saveDialogProperties();
163            validationStatus = true;
164            dispose();
165        }
166    
167        /**
168         * Method hasBeenValidated.
169         * <br><b>Summary:</b><br>
170         * return true if user has validated.
171         * @return  <b>boolean</b>  True if user has validated. False otherwise.
172         */
173        public boolean hasBeenValidated() {
174            return validationStatus;
175        }
176    
177        /**
178         * Method saveDialogProperties.
179         * <br><b>Summary:</b><br>
180         * This method save the current Dialog properties in the properties.
181         */
182        protected void saveDialogProperties() {
183            SearsProperties.setProperty(getDialogName() + SearsProperties.SUFFIX_WIDTH, "" + getWidth());
184            SearsProperties.setProperty(getDialogName() + SearsProperties.SUFFIX_HEIGTH, "" + getHeight());
185            SearsProperties.setProperty(getDialogName() + SearsProperties.SUFFIX_POSX, "" + getX());
186            SearsProperties.setProperty(getDialogName() + SearsProperties.SUFFIX_POSY, "" + getY());
187        }
188    
189        /* (non-Javadoc)
190         * @see java.awt.event.WindowListener#windowClosed(java.awt.event.WindowEvent)
191         */
192        public void windowClosed(WindowEvent e) {
193            saveDialogProperties();
194        }
195    
196        /* (non-Javadoc)
197         * @see java.awt.event.WindowListener#windowClosing(java.awt.event.WindowEvent)
198         */
199        public void windowClosing(WindowEvent e) {
200            cancelAction();
201        }
202    
203        /* (non-Javadoc)
204         * @see java.awt.event.WindowListener#windowActivated(java.awt.event.WindowEvent)
205         */
206        public void windowActivated(WindowEvent e) {
207            // TODO Auto-generated method stub
208        }
209    
210        /* (non-Javadoc)
211         * @see java.awt.event.WindowListener#windowDeactivated(java.awt.event.WindowEvent)
212         */
213        public void windowDeactivated(WindowEvent e) {
214            // TODO Auto-generated method stub
215        }
216    
217        /* (non-Javadoc)
218         * @see java.awt.event.WindowListener#windowDeiconified(java.awt.event.WindowEvent)
219         */
220        public void windowDeiconified(WindowEvent e) {
221            // TODO Auto-generated method stub
222        }
223    
224        /* (non-Javadoc)
225         * @see java.awt.event.WindowListener#windowIconified(java.awt.event.WindowEvent)
226         */
227        public void windowIconified(WindowEvent e) {
228            // TODO Auto-generated method stub
229        }
230    
231        /* (non-Javadoc)
232         * @see java.awt.event.WindowListener#windowOpened(java.awt.event.WindowEvent)
233         */
234        public void windowOpened(WindowEvent e) {
235            // TODO Auto-generated method stub
236        }
237    }