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