KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > compare > ResizableDialog


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.ui.compare;
12
13 import java.util.ResourceBundle JavaDoc;
14
15 import org.eclipse.swt.SWT;
16 import org.eclipse.swt.events.ControlEvent;
17 import org.eclipse.swt.events.ControlListener;
18 import org.eclipse.swt.graphics.Point;
19 import org.eclipse.swt.graphics.Rectangle;
20 import org.eclipse.swt.widgets.Shell;
21
22 import org.eclipse.jface.dialogs.DialogSettings;
23 import org.eclipse.jface.dialogs.IDialogSettings;
24 import org.eclipse.jface.dialogs.TrayDialog;
25
26 import org.eclipse.compare.CompareUI;
27
28 /**
29  * Base class for resizable Dialogs with persistent window bounds.
30  */

31 public abstract class ResizableDialog extends TrayDialog {
32
33     // dialog store id constants
34
private final static String JavaDoc DIALOG_BOUNDS_KEY= "ResizableDialogBounds"; //$NON-NLS-1$
35
private static final String JavaDoc X= "x"; //$NON-NLS-1$
36
private static final String JavaDoc Y= "y"; //$NON-NLS-1$
37
private static final String JavaDoc WIDTH= "width"; //$NON-NLS-1$
38
private static final String JavaDoc HEIGHT= "height"; //$NON-NLS-1$
39

40     protected ResourceBundle JavaDoc fBundle;
41     private Rectangle fNewBounds;
42     private IDialogSettings fSettings;
43
44     public ResizableDialog(Shell parent, ResourceBundle JavaDoc bundle) {
45         super(parent);
46         setShellStyle(getShellStyle() | SWT.RESIZE | SWT.MAX);
47         
48         fBundle= bundle;
49         
50         fSettings= CompareUI.getPlugin().getDialogSettings();
51     }
52     
53     protected Point getInitialSize() {
54         
55         int width= 0;
56         int height= 0;
57         
58         final Shell s= getShell();
59         if (s != null) {
60             s.addControlListener(
61                 new ControlListener() {
62                     public void controlMoved(ControlEvent arg0) {
63                         fNewBounds= s.getBounds();
64                     }
65                     public void controlResized(ControlEvent arg0) {
66                         fNewBounds= s.getBounds();
67                     }
68                 }
69             );
70         }
71         
72         IDialogSettings bounds= fSettings.getSection(DIALOG_BOUNDS_KEY);
73         if (bounds == null) {
74             if (fBundle != null) {
75                 width= JavaCompareUtilities.getInteger(fBundle, WIDTH, 0);
76                 height= JavaCompareUtilities.getInteger(fBundle, HEIGHT, 0);
77                 Shell shell= getParentShell();
78                 if (shell != null) {
79                     Point parentSize= shell.getSize();
80                     if (width <= 0)
81                         width= parentSize.x-300;
82                     if (height <= 0)
83                         height= parentSize.y-200;
84                 }
85             } else {
86                 Shell shell= getParentShell();
87                 if (shell != null) {
88                     Point parentSize= shell.getSize();
89                     width= parentSize.x-100;
90                     height= parentSize.y-100;
91                 }
92             }
93             if (width < 700)
94                 width= 700;
95             if (height < 500)
96                 height= 500;
97         } else {
98             try {
99                 width= bounds.getInt(WIDTH);
100             } catch (NumberFormatException JavaDoc e) {
101                 width= 700;
102             }
103             try {
104                 height= bounds.getInt(HEIGHT);
105             } catch (NumberFormatException JavaDoc e) {
106                 height= 500;
107             }
108         }
109     
110         return new Point(width, height);
111     }
112     
113     protected Point getInitialLocation(Point initialSize) {
114         Point loc= super.getInitialLocation(initialSize);
115         
116         IDialogSettings bounds= fSettings.getSection(DIALOG_BOUNDS_KEY);
117         if (bounds != null) {
118             try {
119                 loc.x= bounds.getInt(X);
120             } catch (NumberFormatException JavaDoc e) {
121                 // silently ignored
122
}
123             try {
124                 loc.y= bounds.getInt(Y);
125             } catch (NumberFormatException JavaDoc e) {
126                 // silently ignored
127
}
128         }
129         return loc;
130     }
131     
132     public boolean close() {
133         boolean closed= super.close();
134         if (closed && fNewBounds != null)
135             saveBounds(fNewBounds);
136         return closed;
137     }
138
139     private void saveBounds(Rectangle bounds) {
140         IDialogSettings dialogBounds= fSettings.getSection(DIALOG_BOUNDS_KEY);
141         if (dialogBounds == null) {
142             dialogBounds= new DialogSettings(DIALOG_BOUNDS_KEY);
143             fSettings.addSection(dialogBounds);
144         }
145         dialogBounds.put(X, bounds.x);
146         dialogBounds.put(Y, bounds.y);
147         dialogBounds.put(WIDTH, bounds.width);
148         dialogBounds.put(HEIGHT, bounds.height);
149     }
150 }
151
Popular Tags