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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.jdt.internal.ui.compare;
13 import java.util.ResourceBundle;
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;
21 import org.eclipse.jface.dialogs.DialogSettings;
22 import org.eclipse.jface.dialogs.IDialogSettings;
23 import org.eclipse.jface.dialogs.TrayDialog;
25 import org.eclipse.compare.CompareUI;
28 * Base class for resizable Dialogs with persistent window bounds.
30 public abstract class ResizableDialog extends TrayDialog {
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$
39 protected ResourceBundle fBundle;
40 private Rectangle fNewBounds;
41 private IDialogSettings fSettings;
43 public ResizableDialog(Shell parent, ResourceBundle bundle) {
46 fSettings= CompareUI.getPlugin().getDialogSettings();
50 * @see org.eclipse.jface.dialogs.Dialog#isResizable()
54 protected boolean isResizable() {
60 protected Point getInitialSize() {
65 final Shell s= getShell();
68 new ControlListener() {
69 public void controlMoved(ControlEvent arg0) {
70 fNewBounds= s.getBounds();
72 public void controlResized(ControlEvent arg0) {
73 fNewBounds= s.getBounds();
79 IDialogSettings bounds= fSettings.getSection(DIALOG_BOUNDS_KEY);
81 if (fBundle != null) {
82 width= JavaCompareUtilities.getInteger(fBundle, WIDTH, 0);
83 height= JavaCompareUtilities.getInteger(fBundle, HEIGHT, 0);
84 Shell shell= getParentShell();
86 Point parentSize= shell.getSize();
88 width= parentSize.x-300;
90 height= parentSize.y-200;
93 Shell shell= getParentShell();
95 Point parentSize= shell.getSize();
96 width= parentSize.x-100;
97 height= parentSize.y-100;
106 width= bounds.getInt(WIDTH);
107 } catch (NumberFormatException e) {
111 height= bounds.getInt(HEIGHT);
112 } catch (NumberFormatException e) {
117 return new Point(width, height);
121 protected Point getInitialLocation(Point initialSize) {
122 Point loc= super.getInitialLocation(initialSize);
124 IDialogSettings bounds= fSettings.getSection(DIALOG_BOUNDS_KEY);
125 if (bounds != null) {
127 loc.x= bounds.getInt(X);
128 } catch (NumberFormatException e) {
132 loc.y= bounds.getInt(Y);
133 } catch (NumberFormatException e) {
141 public boolean close() {
142 boolean closed= super.close();
143 if (closed && fNewBounds != null)
144 saveBounds(fNewBounds);
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);
154 dialogBounds.put(X, bounds.x);
155 dialogBounds.put(Y, bounds.y);
156 dialogBounds.put(WIDTH, bounds.width);
157 dialogBounds.put(HEIGHT, bounds.height);