1 19 package org.netbeans.modules.versioning.util; 20 21 import java.awt.Rectangle ; 22 import java.awt.event.WindowEvent ; 23 import java.awt.event.WindowListener ; 24 import java.util.prefs.Preferences ; 25 import org.openide.util.Utilities; 26 27 34 public class DialogBoundsPreserver implements WindowListener { 35 36 private static final String DELIMITER = "#"; private Preferences preferences; 38 private String key; 39 40 public DialogBoundsPreserver(Preferences preferences, String key) { 41 this.preferences = preferences; 42 this.key = key; 43 } 44 45 public void windowOpened(WindowEvent evt) { 46 Rectangle r = getDialogBounds(); 47 if(r != null && checkBounds(r) ) { 48 evt.getWindow().setBounds(r); 49 } 50 } 51 public void windowClosing(WindowEvent evt) { 52 } 54 public void windowClosed(WindowEvent evt) { 55 Rectangle r = evt.getWindow().getBounds(); 56 if(checkBounds(r)) { 57 setDialogBounds(r); 58 } 59 } 60 public void windowIconified(WindowEvent arg0) { 61 } 63 public void windowDeiconified(WindowEvent arg0) { 64 } 66 public void windowActivated(WindowEvent arg0) { 67 } 69 public void windowDeactivated(WindowEvent arg0) { 70 } 72 73 private boolean checkBounds(Rectangle r) { 74 Rectangle screen = Utilities.getUsableScreenBounds(); 75 return r.getX() >= 0 && r.getX() < screen.getWidth() && 76 r.getY() >= 0 && r.getY() < screen.getHeight() && 77 r.getWidth() <= screen.getWidth() - r.getX() && 78 r.getHeight() <= screen.getHeight() - r.getY(); 79 } 80 81 private void setDialogBounds(Rectangle r) { 82 preferences.put(key, r.getX() + DELIMITER + r.getY() + DELIMITER + r.getWidth() + DELIMITER + r.getHeight()); } 84 85 private Rectangle getDialogBounds() { 86 String size = preferences.get(key, DELIMITER); 87 if(size != null) { 88 String [] dim = size.split(DELIMITER); 89 if(dim.length != 4 || 90 dim[0].trim().equals("") || dim[1].trim().equals("") || dim[2].trim().equals("") || dim[3].trim().equals("") ) { 95 return null; 96 } 97 Rectangle r = new Rectangle (); 98 r.setRect(Double.parseDouble(dim[0]), 99 Double.parseDouble(dim[1]), 100 Double.parseDouble(dim[2]), 101 Double.parseDouble(dim[3])); 102 return r; 103 } 104 return null; 105 } 106 107 } 108 | Popular Tags |