KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > compare > internal > 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.compare.internal;
12
13 import java.util.ResourceBundle JavaDoc;
14
15 import org.eclipse.swt.SWT;
16 import org.eclipse.swt.events.*;
17 import org.eclipse.swt.graphics.*;
18 import org.eclipse.swt.widgets.Shell;
19 import org.eclipse.ui.PlatformUI;
20 import org.eclipse.jface.dialogs.Dialog;
21 import org.eclipse.jface.dialogs.IDialogSettings;
22 import org.eclipse.jface.dialogs.DialogSettings;
23
24
25 /**
26  * Base class for resizable Dialogs with persistent window bounds.
27  */

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

37     protected ResourceBundle JavaDoc fBundle;
38     private Rectangle fNewBounds;
39     private IDialogSettings fSettings;
40     private String JavaDoc fContextId;
41
42
43     public ResizableDialog(Shell parent, ResourceBundle JavaDoc bundle) {
44         super(parent);
45         setShellStyle(getShellStyle() | SWT.RESIZE | SWT.MAX);
46         
47         fBundle= bundle;
48         
49         fSettings= CompareUIPlugin.getDefault().getDialogSettings();
50     }
51     
52     public void setHelpContextId(String JavaDoc contextId) {
53         fContextId= contextId;
54     }
55
56     /*
57      * @see org.eclipse.jface.window.Window#configureShell(Shell)
58      */

59     protected void configureShell(Shell newShell) {
60         super.configureShell(newShell);
61         if (fContextId != null)
62             PlatformUI.getWorkbench().getHelpSystem().setHelp(newShell, fContextId);
63     }
64     
65     protected Point getInitialSize() {
66         
67         int width= 0;
68         int height= 0;
69         
70         final Shell s= getShell();
71         if (s != null) {
72             s.addControlListener(
73                 new ControlListener() {
74                     public void controlMoved(ControlEvent arg0) {
75                         fNewBounds= s.getBounds();
76                     }
77                     public void controlResized(ControlEvent arg0) {
78                         fNewBounds= s.getBounds();
79                     }
80                 }
81             );
82         }
83         
84         IDialogSettings bounds= fSettings.getSection(DIALOG_BOUNDS_KEY);
85         if (bounds == null) {
86             if (fBundle != null) {
87                 width= Utilities.getInteger(fBundle, WIDTH, 0);
88                 height= Utilities.getInteger(fBundle, HEIGHT, 0);
89                 Shell shell= getParentShell();
90                 if (shell != null) {
91                     Point parentSize= shell.getSize();
92                     if (width <= 0)
93                         width= parentSize.x-300;
94                     if (height <= 0)
95                         height= parentSize.y-200;
96                 }
97             } else {
98                 Shell shell= getParentShell();
99                 if (shell != null) {
100                     Point parentSize= shell.getSize();
101                     width= parentSize.x-100;
102                     height= parentSize.y-100;
103                 }
104             }
105             if (width < 700)
106                 width= 700;
107             if (height < 500)
108                 height= 500;
109         } else {
110             try {
111                 width= bounds.getInt(WIDTH);
112             } catch (NumberFormatException JavaDoc e) {
113                 width= 700;
114             }
115             try {
116                 height= bounds.getInt(HEIGHT);
117             } catch (NumberFormatException JavaDoc e) {
118                 height= 500;
119             }
120         }
121     
122         return new Point(width, height);
123     }
124     
125     protected Point getInitialLocation(Point initialSize) {
126         Point loc= super.getInitialLocation(initialSize);
127         
128         IDialogSettings bounds= fSettings.getSection(DIALOG_BOUNDS_KEY);
129         if (bounds != null) {
130             try {
131                 loc.x= bounds.getInt(X);
132             } catch (NumberFormatException JavaDoc e) {
133                 // silently ignored
134
}
135             try {
136                 loc.y= bounds.getInt(Y);
137             } catch (NumberFormatException JavaDoc e) {
138                 // silently ignored
139
}
140         }
141         return loc;
142     }
143     
144     public boolean close() {
145         boolean closed= super.close();
146         if (closed && fNewBounds != null)
147             saveBounds(fNewBounds);
148         return closed;
149     }
150
151     private void saveBounds(Rectangle bounds) {
152         IDialogSettings dialogBounds= fSettings.getSection(DIALOG_BOUNDS_KEY);
153         if (dialogBounds == null) {
154             dialogBounds= new DialogSettings(DIALOG_BOUNDS_KEY);
155             fSettings.addSection(dialogBounds);
156         }
157         dialogBounds.put(X, bounds.x);
158         dialogBounds.put(Y, bounds.y);
159         dialogBounds.put(WIDTH, bounds.width);
160         dialogBounds.put(HEIGHT, bounds.height);
161     }
162 }
163
Popular Tags