1 11 package org.eclipse.jdt.internal.ui.compare; 12 13 import java.util.ResourceBundle ; 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 31 public abstract class ResizableDialog extends TrayDialog { 32 33 private final static String DIALOG_BOUNDS_KEY= "ResizableDialogBounds"; private static final String X= "x"; private static final String Y= "y"; private static final String WIDTH= "width"; private static final String HEIGHT= "height"; 40 protected ResourceBundle fBundle; 41 private Rectangle fNewBounds; 42 private IDialogSettings fSettings; 43 44 public ResizableDialog(Shell parent, ResourceBundle 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 e) { 101 width= 700; 102 } 103 try { 104 height= bounds.getInt(HEIGHT); 105 } catch (NumberFormatException 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 e) { 121 } 123 try { 124 loc.y= bounds.getInt(Y); 125 } catch (NumberFormatException e) { 126 } 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 |