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.ActionEvent;
022    import java.awt.event.ActionListener;
023    import java.awt.event.InputEvent;
024    import java.awt.event.KeyEvent;
025    import java.awt.event.WindowEvent;
026    import java.awt.event.WindowListener;
027    
028    import javax.swing.BorderFactory;
029    import javax.swing.JButton;
030    import javax.swing.JComponent;
031    import javax.swing.JDialog;
032    import javax.swing.JPanel;
033    import javax.swing.JRootPane;
034    import javax.swing.KeyStroke;
035    import javax.swing.border.Border;
036    
037    import sears.tools.SearsProperties;
038    import sears.tools.SearsResourceBundle;
039    import sears.tools.Utils;
040    
041    /**
042     * Class SearsJDialog.
043     * <br><b>Summary:</b><br>
044     * This class defines the general comportement of the Sears JDialog.
045     */
046    public abstract class SearsJDialog extends JDialog implements WindowListener {
047            
048            private static final long serialVersionUID = 1L;
049    
050            protected static int border = 4;
051            
052            protected JPanel jPanelButtons = null;
053    
054            protected JButton jButtonOk = null;
055    
056            protected JButton jButtonCancel = null;
057    
058            /**A boolean to know if user has validated*/
059            protected boolean validationStatus;
060    
061            /**
062             * Constructor SearsJDialog.
063             * <br><b>Summary:</b><br>
064             * Constructor of the class.
065             * @param title     The title of the JDialog.
066             */
067            public SearsJDialog(String title) {
068                    super(MainWindow.instance, title, true);
069                    //manage closing event.
070                    this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
071                    addWindowListener(this);
072                    configureSize();
073            }
074    
075            /*
076             * (non-Javadoc)
077             * @see javax.swing.JDialog#createRootPane()
078             */
079            protected JRootPane createRootPane() {
080                    // **************************
081                    // ADDING KEYBOARD SHORTCUT *
082                    // **************************
083                    
084                    // we create a new action listener:
085                    ActionListener theActionListener = new ActionListener() {
086                            public void actionPerformed(ActionEvent actionEvent) {
087                                    // we close the window:
088                                    cancelAction();
089                            }
090                    };
091                    
092                    KeyStroke stroke = null;
093                    // on a Mac platform:
094                    if(Utils.isMacPlatform) {
095                            stroke = KeyStroke.getKeyStroke(KeyEvent.VK_W, InputEvent.META_MASK);
096                    } else {
097                            stroke = KeyStroke.getKeyStroke(KeyEvent.VK_W, InputEvent.CTRL_MASK);
098                    }
099                    
100                    JRootPane rootPane = new JRootPane();
101                    rootPane.registerKeyboardAction(theActionListener, stroke, JComponent.WHEN_IN_FOCUSED_WINDOW);
102                    return rootPane;
103            }
104    
105            /**
106             * Method configureSize.
107             * <br><b>Summary:</b><br>
108             * This method permits to restore dialog size, or pack it for the first launch.
109             */
110            protected void configureSize() {
111                    //try to restaure the dimension and position
112                    Dimension restauredDimension = null;
113                    Point location = null;
114                    try {
115                            int width = Integer.parseInt(SearsProperties.getProperty(getDialogName() + SearsProperties.SUFFIX_WIDTH));
116                            int heigth = Integer.parseInt(SearsProperties.getProperty(getDialogName() + SearsProperties.SUFFIX_HEIGTH));
117                            restauredDimension = new Dimension(width, heigth);
118                            int posX = Integer.parseInt(SearsProperties.getProperty(getDialogName() + SearsProperties.SUFFIX_POSX));
119                            int posY = Integer.parseInt(SearsProperties.getProperty(getDialogName() + SearsProperties.SUFFIX_POSY));
120                            location = new Point(posX, posY);
121                            //set the size
122                            setSize(restauredDimension);
123                    } catch (NumberFormatException e) {
124                            //use packed size.
125                            pack();
126                    }
127                    //set the location
128                    if (location == null) {
129                            setLocationRelativeTo(MainWindow.instance);
130                    } else {
131                            setLocation(location);
132                    }
133            }
134    
135            /**
136             * Method getDialogName.
137             * <br><b>Summary:</b><br>
138             * This method return the dialog name.
139             * It will be used to save the dialog dimensions in the config file.
140             * @return  String      The dialog name.
141             */
142            protected abstract String getDialogName();
143    
144            /**
145             * This method initializes jButton  
146             *  
147             * @return javax.swing.JButton  
148             */
149            protected JButton getJButtonCancel() {
150                    if (jButtonCancel == null) {
151                            jButtonCancel = new JButton(SearsResourceBundle.getResource("button_cancel"));
152                            jButtonCancel.addActionListener(new java.awt.event.ActionListener() {
153                                    public void actionPerformed(java.awt.event.ActionEvent e) {
154                                            cancelAction();
155                                    }
156                            });
157                    }
158                    return jButtonCancel;
159            }
160    
161            /**
162             * Method cancelAction.
163             * <br><b>Summary:</b><br>
164             * This method is called when user wants to cancel split dialog.
165             */
166            protected void cancelAction() {
167                    validationStatus = false;
168                    //saveDialogProperties();
169                    dispose();
170            }
171    
172            /**
173             * This method initializes jPanel   
174             *  
175             * @return javax.swing.JPanel   
176             */
177            protected JPanel getJPanelButtons() {
178                    if (jPanelButtons == null) {
179                            jPanelButtons = new JPanel();
180                            jPanelButtons.add(getJButtonOk(), null);
181                            jPanelButtons.add(getJButtonCancel(), null);
182                    }
183                    return jPanelButtons;
184            }
185    
186            /**
187             * This method initializes jButton  
188             *  
189             * @return javax.swing.JButton  
190             */
191            private JButton getJButtonOk() {
192                    if (jButtonOk == null) {
193                            jButtonOk = new JButton(SearsResourceBundle.getResource("button_ok"));
194                            jButtonOk.addActionListener(new java.awt.event.ActionListener() {
195                                    public void actionPerformed(java.awt.event.ActionEvent e) {
196                                            okAction();
197                                    }
198                            });
199                    }
200                    return jButtonOk;
201            }
202    
203            /**
204             * Method okAction.
205             * <br><b>Summary:</b><br>
206             * This method is called when user validate the dialog.
207             */
208            protected void okAction() {
209                    //saveDialogProperties();
210                    validationStatus = true;
211                    dispose();
212            }
213    
214            /**
215             * Method hasBeenValidated.
216             * <br><b>Summary:</b><br>
217             * return true if user has validated.
218             * @return  <b>boolean</b>  True if user has validated. False otherwise.
219             */
220            public boolean hasBeenValidated() {
221                    return validationStatus;
222            }
223    
224            /**
225             * Method saveDialogProperties.
226             * <br><b>Summary:</b><br>
227             * This method save the current Dialog properties in the properties.
228             */
229            protected void saveDialogProperties() {
230                    SearsProperties.setProperty(getDialogName() + SearsProperties.SUFFIX_WIDTH, "" + getWidth());
231                    SearsProperties.setProperty(getDialogName() + SearsProperties.SUFFIX_HEIGTH, "" + getHeight());
232                    SearsProperties.setProperty(getDialogName() + SearsProperties.SUFFIX_POSX, "" + getX());
233                    SearsProperties.setProperty(getDialogName() + SearsProperties.SUFFIX_POSY, "" + getY());
234            }
235            
236            /**
237             * Create empty border related to <tt>border</tt> class constant.
238             * @return an empty border
239             */
240            protected static Border createEmptyBorder() {
241                    return BorderFactory.createEmptyBorder(border,border,0,border);
242            }
243    
244            /* (non-Javadoc)
245             * @see java.awt.event.WindowListener#windowClosed(java.awt.event.WindowEvent)
246             */
247            public void windowClosed(WindowEvent e) {
248                    saveDialogProperties();
249            }
250    
251            /* (non-Javadoc)
252             * @see java.awt.event.WindowListener#windowClosing(java.awt.event.WindowEvent)
253             */
254            public void windowClosing(WindowEvent e) {
255                    cancelAction();
256            }
257    
258            /* (non-Javadoc)
259             * @see java.awt.event.WindowListener#windowActivated(java.awt.event.WindowEvent)
260             */
261            public void windowActivated(WindowEvent e) {
262                    // TODO Auto-generated method stub
263            }
264    
265            /* (non-Javadoc)
266             * @see java.awt.event.WindowListener#windowDeactivated(java.awt.event.WindowEvent)
267             */
268            public void windowDeactivated(WindowEvent e) {
269                    // TODO Auto-generated method stub
270            }
271    
272            /* (non-Javadoc)
273             * @see java.awt.event.WindowListener#windowDeiconified(java.awt.event.WindowEvent)
274             */
275            public void windowDeiconified(WindowEvent e) {
276                    // TODO Auto-generated method stub
277            }
278    
279            /* (non-Javadoc)
280             * @see java.awt.event.WindowListener#windowIconified(java.awt.event.WindowEvent)
281             */
282            public void windowIconified(WindowEvent e) {
283                    // TODO Auto-generated method stub
284            }
285    
286            /* (non-Javadoc)
287             * @see java.awt.event.WindowListener#windowOpened(java.awt.event.WindowEvent)
288             */
289            public void windowOpened(WindowEvent e) {
290                    // TODO Auto-generated method stub
291            }
292    }