1 11 package org.eclipse.compare.internal; 12 13 import java.util.ResourceBundle ; 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 28 public abstract class ResizableDialog extends Dialog { 29 30 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"; 37 protected ResourceBundle fBundle; 38 private Rectangle fNewBounds; 39 private IDialogSettings fSettings; 40 private String fContextId; 41 42 43 public ResizableDialog(Shell parent, ResourceBundle 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 contextId) { 53 fContextId= contextId; 54 } 55 56 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 e) { 113 width= 700; 114 } 115 try { 116 height= bounds.getInt(HEIGHT); 117 } catch (NumberFormatException 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 e) { 133 } 135 try { 136 loc.y= bounds.getInt(Y); 137 } catch (NumberFormatException e) { 138 } 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 |