]> git.uio.no Git - ifi-stolz-refaktor.git/blob - case-study/jdt-before/ui/org/eclipse/jdt/internal/ui/compare/ResizableDialog.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-before / ui / org / eclipse / jdt / internal / ui / compare / ResizableDialog.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2011 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;
14
15 import org.eclipse.swt.events.ControlEvent;
16 import org.eclipse.swt.events.ControlListener;
17 import org.eclipse.swt.graphics.Point;
18 import org.eclipse.swt.graphics.Rectangle;
19 import org.eclipse.swt.widgets.Shell;
20
21 import org.eclipse.jface.dialogs.DialogSettings;
22 import org.eclipse.jface.dialogs.IDialogSettings;
23 import org.eclipse.jface.dialogs.TrayDialog;
24
25 import org.eclipse.compare.CompareUI;
26
27 /**
28  * Base class for resizable Dialogs with persistent window bounds.
29  */
30 public abstract class ResizableDialog extends TrayDialog {
31
32         // dialog store id constants
33         private final static String DIALOG_BOUNDS_KEY= "ResizableDialogBounds"; //$NON-NLS-1$
34         private static final String X= "x"; //$NON-NLS-1$
35         private static final String Y= "y"; //$NON-NLS-1$
36         private static final String WIDTH= "width"; //$NON-NLS-1$
37         private static final String HEIGHT= "height"; //$NON-NLS-1$
38
39         protected ResourceBundle fBundle;
40         private Rectangle fNewBounds;
41         private IDialogSettings fSettings;
42
43         public ResizableDialog(Shell parent, ResourceBundle bundle) {
44                 super(parent);
45                 fBundle= bundle;
46                 fSettings= CompareUI.getPlugin().getDialogSettings();
47         }
48
49         /*
50          * @see org.eclipse.jface.dialogs.Dialog#isResizable()
51          * @since 3.4
52          */
53         @Override
54         protected boolean isResizable() {
55                 return true;
56         }
57
58
59         @Override
60         protected Point getInitialSize() {
61
62                 int width= 0;
63                 int height= 0;
64
65                 final Shell s= getShell();
66                 if (s != null) {
67                         s.addControlListener(
68                                 new ControlListener() {
69                                         public void controlMoved(ControlEvent arg0) {
70                                                 fNewBounds= s.getBounds();
71                                         }
72                                         public void controlResized(ControlEvent arg0) {
73                                                 fNewBounds= s.getBounds();
74                                         }
75                                 }
76                         );
77                 }
78
79                 IDialogSettings bounds= fSettings.getSection(DIALOG_BOUNDS_KEY);
80                 if (bounds == null) {
81                         if (fBundle != null) {
82                                 width= JavaCompareUtilities.getInteger(fBundle, WIDTH, 0);
83                                 height= JavaCompareUtilities.getInteger(fBundle, HEIGHT, 0);
84                                 Shell shell= getParentShell();
85                                 if (shell != null) {
86                                         Point parentSize= shell.getSize();
87                                         if (width <= 0)
88                                                 width= parentSize.x-300;
89                                         if (height <= 0)
90                                                 height= parentSize.y-200;
91                                 }
92                         } else {
93                                 Shell shell= getParentShell();
94                                 if (shell != null) {
95                                         Point parentSize= shell.getSize();
96                                         width= parentSize.x-100;
97                                         height= parentSize.y-100;
98                                 }
99                         }
100                         if (width < 700)
101                                 width= 700;
102                         if (height < 500)
103                                 height= 500;
104                 } else {
105                         try {
106                                 width= bounds.getInt(WIDTH);
107                         } catch (NumberFormatException e) {
108                                 width= 700;
109                         }
110                         try {
111                                 height= bounds.getInt(HEIGHT);
112                         } catch (NumberFormatException e) {
113                                 height= 500;
114                         }
115                 }
116
117                 return new Point(width, height);
118         }
119
120         @Override
121         protected Point getInitialLocation(Point initialSize) {
122                 Point loc= super.getInitialLocation(initialSize);
123
124                 IDialogSettings bounds= fSettings.getSection(DIALOG_BOUNDS_KEY);
125                 if (bounds != null) {
126                         try {
127                                 loc.x= bounds.getInt(X);
128                         } catch (NumberFormatException e) {
129                                 // silently ignored
130                         }
131                         try {
132                                 loc.y= bounds.getInt(Y);
133                         } catch (NumberFormatException e) {
134                                 // silently ignored
135                         }
136                 }
137                 return loc;
138         }
139
140         @Override
141         public boolean close() {
142                 boolean closed= super.close();
143                 if (closed && fNewBounds != null)
144                         saveBounds(fNewBounds);
145                 return closed;
146         }
147
148         private void saveBounds(Rectangle bounds) {
149                 IDialogSettings dialogBounds= fSettings.getSection(DIALOG_BOUNDS_KEY);
150                 if (dialogBounds == null) {
151                         dialogBounds= new DialogSettings(DIALOG_BOUNDS_KEY);
152                         fSettings.addSection(dialogBounds);
153                 }
154                 dialogBounds.put(X, bounds.x);
155                 dialogBounds.put(Y, bounds.y);
156                 dialogBounds.put(WIDTH, bounds.width);
157                 dialogBounds.put(HEIGHT, bounds.height);
158         }
159 }