KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > versioning > util > DialogBoundsPreserver


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.versioning.util;
20
21 import java.awt.Rectangle JavaDoc;
22 import java.awt.event.WindowEvent JavaDoc;
23 import java.awt.event.WindowListener JavaDoc;
24 import java.util.prefs.Preferences JavaDoc;
25 import org.openide.util.Utilities;
26
27 /**
28  *
29  * Stores under a key the registered windows size and position when it si closed and
30  * sets them back when the window gets open again.
31  *
32  * @author Tomas Stupka
33  */

34 public class DialogBoundsPreserver implements WindowListener JavaDoc {
35
36     private static final String JavaDoc DELIMITER = "#"; // NOI18N
37
private Preferences JavaDoc preferences;
38     private String JavaDoc key;
39
40     public DialogBoundsPreserver(Preferences JavaDoc preferences, String JavaDoc key) {
41         this.preferences = preferences;
42         this.key = key;
43     }
44
45     public void windowOpened(WindowEvent JavaDoc evt) {
46         Rectangle JavaDoc r = getDialogBounds();
47         if(r != null && checkBounds(r) ) {
48             evt.getWindow().setBounds(r);
49         }
50     }
51     public void windowClosing(WindowEvent JavaDoc evt) {
52         // ignore
53
}
54     public void windowClosed(WindowEvent JavaDoc evt) {
55         Rectangle JavaDoc r = evt.getWindow().getBounds();
56         if(checkBounds(r)) {
57             setDialogBounds(r);
58         }
59     }
60     public void windowIconified(WindowEvent JavaDoc arg0) {
61         // ignore
62
}
63     public void windowDeiconified(WindowEvent JavaDoc arg0) {
64         // ignore
65
}
66     public void windowActivated(WindowEvent JavaDoc arg0) {
67         // ignore
68
}
69     public void windowDeactivated(WindowEvent JavaDoc arg0) {
70         // ignore
71
}
72
73     private boolean checkBounds(Rectangle JavaDoc r) {
74         Rectangle JavaDoc 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 JavaDoc r) {
82         preferences.put(key, r.getX() + DELIMITER + r.getY() + DELIMITER + r.getWidth() + DELIMITER + r.getHeight()); // NOI18N
83
}
84
85     private Rectangle JavaDoc getDialogBounds() {
86         String JavaDoc size = preferences.get(key, DELIMITER);
87         if(size != null) {
88             String JavaDoc[] dim = size.split(DELIMITER);
89             if(dim.length != 4 ||
90                dim[0].trim().equals("") || // NOI18N
91
dim[1].trim().equals("") || // NOI18N
92
dim[2].trim().equals("") || // NOI18N
93
dim[3].trim().equals("") ) // NOI18N
94
{
95                 return null;
96             }
97             Rectangle JavaDoc r = new Rectangle JavaDoc();
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