1 5 package com.nightlabs.rcp.dialog; 6 7 import org.eclipse.jface.dialogs.Dialog; 8 import org.eclipse.swt.SWT; 9 import org.eclipse.swt.graphics.Point; 10 import org.eclipse.swt.layout.GridData; 11 import org.eclipse.swt.layout.GridLayout; 12 import org.eclipse.swt.widgets.Composite; 13 import org.eclipse.swt.widgets.Control; 14 import org.eclipse.swt.widgets.Display; 15 import org.eclipse.swt.widgets.Shell; 16 import org.eclipse.ui.forms.events.ExpansionAdapter; 17 import org.eclipse.ui.forms.events.ExpansionEvent; 18 import org.eclipse.ui.forms.widgets.ExpandableComposite; 19 20 import com.nightlabs.rcp.composite.ExpandableWrapperComposite; 21 22 58 public class ExpandableAreaDialog extends Dialog { 59 60 61 public ExpandableAreaDialog(Shell parent) 62 { 63 this(parent,"","",SWT.RESIZE); 64 } 65 66 public ExpandableAreaDialog(Shell parent, int style) 67 { 68 this(parent,"","",style); 69 } 70 71 public ExpandableAreaDialog(Shell parent, String title) 72 { 73 this(parent,title,"",SWT.RESIZE); 74 } 75 76 public ExpandableAreaDialog(Shell parent, String title, String expandText) 77 { 78 this(parent,title,expandText,SWT.RESIZE); 79 } 80 81 82 public ExpandableAreaDialog(Shell parent, String title, String expandText, int style) 83 { 84 super(parent); 85 setShellStyle(getShellStyle()|style); 86 this.dialogTitle = title; 87 this.expandText = expandText; 88 } 89 90 private String dialogTitle = ""; 91 private String expandText = ""; 92 93 96 public String getDialogTitle() { 97 return dialogTitle; 98 } 99 102 public void setDialogTitle(String dialogTitle) { 103 this.dialogTitle = dialogTitle; 104 } 105 108 public String getExpandText() { 109 return expandText; 110 } 111 114 public void setExpandText(String expandText) { 115 this.expandText = expandText; 116 } 117 118 protected void configureShell(Shell shell) { 119 super.configureShell(shell); 120 shell.setText(dialogTitle); 121 } 122 123 124 private ExpandableAreaComp dialogAreaComp = null; 125 128 protected Control createDialogArea(Composite parent) { 129 dialogAreaComp = new ExpandableAreaComp(); 130 return dialogAreaComp.createComposite(parent,this,expandText); 131 } 132 133 134 135 private Composite staticArea = null; 136 142 protected Composite createStaticArea(Composite parent){ 143 return new Composite(parent,SWT.NONE); 144 } 145 146 private Composite expandableArea = null; 147 153 protected Composite createExpandableArea(Composite parent){ 154 return new Composite(parent,SWT.NONE); 155 } 156 157 158 163 public Composite getExpandableArea() { 164 return expandableArea; 165 } 166 171 public Composite getStaticArea() { 172 return staticArea; 173 } 174 175 180 public void doRelayout() { 181 if (getShell() != null) { 182 getShell().layout(); 183 getShell().pack(); 184 Point sizeAfterPack = getShell().getSize(); 185 Point sizeToSet = new Point(sizeAfterPack.x,sizeAfterPack.y); 186 if (sizeAfterPack.x > getMaxWidth()) 187 sizeToSet.x = getMaxWidth(); 188 if (sizeAfterPack.y > getMaxHeight()) 189 sizeToSet.y = getMaxHeight(); 190 191 getShell().setSize(sizeToSet.x,sizeToSet.y); 192 getShell().layout(); 193 } 194 } 195 196 199 public void create() { 200 super.create(); 201 doRelayout(); 202 } 203 204 205 private int maxWidth = -1; 206 private int maxHeight = -1; 207 208 213 public int getMaxWidth() { 214 if ( maxWidth > 0 ) { 215 return maxWidth; 216 } 217 else 218 return Display.getCurrent().getClientArea().width; 219 } 220 221 226 public int getMaxHeight() { 227 if ( maxHeight > 0 ) { 228 return maxHeight; 229 } 230 else 231 return Display.getCurrent().getClientArea().height; 232 } 233 234 238 public void setMaxSize(Point maxSize) { 239 setMaxWidth(maxSize.x); 240 setMaxHeight(maxSize.y); 241 } 242 243 248 public void setMaxWidth(int width) { 249 this.maxWidth = width; 250 } 251 252 257 public void setMaxHeight(int height) { 258 this.maxHeight = height; 259 } 260 261 265 private class ExpandableAreaComp { 266 267 private Composite expComp = null; 268 private Composite getComp(){return expComp;} 269 270 private ExpandableAreaDialog dialog = null; 271 private ExpandableAreaDialog getDialog(){return dialog;} 272 273 284 public Composite createComposite( 285 Composite parent, 286 ExpandableAreaDialog dialog, 287 String expandText 288 ) 289 { 290 if (dialog == null) 291 throw new IllegalArgumentException (this.getClass().getName()+"#createComposite: Parameter dialog can not be null."); 292 this.dialog = dialog; 293 expComp = new Composite(parent,SWT.NONE); 295 GridData myData = new GridData(GridData.FILL_BOTH); 297 expComp.setLayoutData(myData); 298 299 GridLayout layout = new GridLayout(); 301 expComp.setLayout(layout); 302 303 staticArea = createStaticArea(expComp); 305 if (staticArea != null) { 306 GridData gdStatic = new GridData(GridData.FILL_BOTH); 308 staticArea.setLayoutData(gdStatic); 311 } 312 313 ExpandableWrapperComposite ec = new ExpandableWrapperComposite(parent, SWT.NONE, ExpandableComposite.TWISTIE|ExpandableComposite.COMPACT); 315 ec.removeExpansionListener(); 316 ec.setText(expandText); 317 GridData gd = new GridData(GridData.FILL_BOTH); 318 ec.setLayoutData(gd); 319 ec.addExpansionListener(new ExpansionAdapter() { 321 public void expansionStateChanged(ExpansionEvent e) { 322 getComp().layout(true); 323 getDialog().doRelayout(); 324 } 325 }); 326 327 GridLayout ecLayout = new GridLayout(); 328 ecLayout.verticalSpacing = 5; 329 ecLayout.horizontalSpacing= 5; 330 ec.setLayout(ecLayout); 331 332 Composite dummyComp = new Composite(ec,SWT.NONE); 336 gd = new GridData(GridData.FILL_BOTH); 337 dummyComp.setLayoutData(gd); 338 GridLayout gl = new GridLayout(); 339 dummyComp.setLayout(gl); 340 341 expandableArea = createExpandableArea(dummyComp); 342 if (expandableArea != null) { 343 gd = new GridData(GridData.FILL_BOTH); 345 expandableArea.setLayoutData(gd); 346 ec.setClient(dummyComp); 348 } 349 350 return expComp; 351 } 352 353 } 354 } 355 | Popular Tags |