KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > nightlabs > rcp > dialog > ExpandableAreaDialog


1 /*
2  * Created on Sep 30, 2004
3  * by Alexander Bieber
4  */

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 /**
23  * Defines a Dialog with a "twistie" for toggling the expansion-state of a Composite.
24  * This class is ment to be subclassed. You have to
25  * define a constructor and override {@link #createStaticArea(Composite)} as well as
26  * {@link #createExpandableArea(Composite)} to have your own Composites inside the Dialog.
27  * <br/>
28  * <br/>
29  * Here is a example:
30  * <pre>
31  * public class MyExpandableDialog extends ExpandableAreaDialog{
32  * public MyExpandableDialog(Shell parent)
33  * {
34  * super(parent);
35  * setDialogTitle("My Dialog's Title");
36  * setExpandText("Expand Me");
37  * }
38  *
39  * protected Composite createStaticArea(Composite parent)
40  * {
41  * return new Composite(parent,SWT.NONE);
42  * }
43  *
44  * protected Composite createExpandableArea(Composite parent)
45  * {
46  * return new Composite(parent,SWT.NONE);
47  * }
48  * }
49  * </pre>
50  * You can still override methods from {@link org.eclipse.jface.dialogs.Dialog} in order to provide
51  * your own ButtonBar. Please do not override createDialogArea or at least return super(parent).
52  *
53  * @author Alexander Bieber
54  * @see org.eclipse.ui.forms.widgets.ExpandableComposite
55  * @see org.eclipse.jface.dialogs.Dialog
56  *
57  */

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 JavaDoc title)
72     {
73         this(parent,title,"",SWT.RESIZE);
74     }
75
76     public ExpandableAreaDialog(Shell parent, String JavaDoc title, String JavaDoc expandText)
77     {
78         this(parent,title,expandText,SWT.RESIZE);
79     }
80     
81     
82     public ExpandableAreaDialog(Shell parent, String JavaDoc title, String JavaDoc expandText, int style)
83     {
84         super(parent);
85         setShellStyle(getShellStyle()|style);
86         this.dialogTitle = title;
87         this.expandText = expandText;
88     }
89     
90     private String JavaDoc dialogTitle = "";
91     private String JavaDoc expandText = "";
92     
93     /**
94      * @return Returns the dialogTitle.
95      */

96     public String JavaDoc getDialogTitle() {
97         return dialogTitle;
98     }
99     /**
100      * @param dialogTitle The dialogTitle to set.
101      */

102     public void setDialogTitle(String JavaDoc dialogTitle) {
103         this.dialogTitle = dialogTitle;
104     }
105     /**
106      * @return Returns the expandText.
107      */

108     public String JavaDoc getExpandText() {
109         return expandText;
110     }
111     /**
112      * @param expandText The expandText to set.
113      */

114     public void setExpandText(String JavaDoc 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     /** Do not override this method.
126      * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
127      */

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     /**
137      * Override this method to return the Composite you want to be visible in the
138      * upper area of the Dialog.
139      * @param parent Add your Composite as child of this
140      * @return
141      */

142     protected Composite createStaticArea(Composite parent){
143         return new Composite(parent,SWT.NONE);
144     }
145
146     private Composite expandableArea = null;
147     /**
148      * Override this method to return the Composite you want to be the client of the
149      * ExpandableComposite.
150      * @param parent Add your Composite as child of this
151      * @return
152      */

153     protected Composite createExpandableArea(Composite parent){
154         return new Composite(parent,SWT.NONE);
155     }
156     
157     
158     /**
159      * Returns the expandable area Composite
160      * @return
161      * @see #createExpandableArea(Composite)
162      */

163     public Composite getExpandableArea() {
164         return expandableArea;
165     }
166     /**
167      * Returns the static area Composite
168      * @return
169      * @see #createStaticArea(Composite)
170      */

171     public Composite getStaticArea() {
172         return staticArea;
173     }
174
175     /**
176      * Packs, layouts and resizes the Dialog according to
177      * its contents and {@link #getMaxWidth()} and
178      * {@link #getMaxHeight()}.
179      */

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     /**
197      * Overrides {@link Dialog#create()} and adds a call to {@link #doRelayout()}
198      */

199     public void create() {
200         super.create();
201         doRelayout();
202     }
203     
204     
205     private int maxWidth = -1;
206     private int maxHeight = -1;
207     
208     /**
209      * Returns the maximal Width for the dialog.
210      * Default value is the screens width.
211      * @return
212      */

213     public int getMaxWidth() {
214         if ( maxWidth > 0 ) {
215             return maxWidth;
216         }
217         else
218             return Display.getCurrent().getClientArea().width;
219     }
220     
221     /**
222      * Returns the maximal Height for the dialog.
223      * Default value is the screens height.
224      * @return
225      */

226     public int getMaxHeight() {
227         if ( maxHeight > 0 ) {
228             return maxHeight;
229         }
230         else
231             return Display.getCurrent().getClientArea().height;
232     }
233     
234     /**
235      * Sets the maximum size for the dialog.
236      * @param maxSize
237      */

238     public void setMaxSize(Point maxSize) {
239         setMaxWidth(maxSize.x);
240         setMaxHeight(maxSize.y);
241     }
242     
243     /**
244      * Sets the maximum width for the dialog.
245      * maxHeight is not affected by this method.
246      * @param width
247      */

248     public void setMaxWidth(int width) {
249         this.maxWidth = width;
250     }
251     
252     /**
253      * Sets the maximum height for the dialog.
254      * maxWidth is not affected by this method.
255      * @param height
256      */

257     public void setMaxHeight(int height) {
258         this.maxHeight = height;
259     }
260     
261     /**
262      * This class is internally used.
263      * @author Alexander Bieber
264      */

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         /**
274          * Calls {@link ExpandableAreaDialog#createStaticArea(Composite)}, creates an
275          * {@link ExpandableComposite} and adds the result of
276          * {@link ExpandableAreaDialog#createExpandableArea(Composite)} to it.
277          * An anonymous {@link ExpansionAdapter} is added that handles
278          * relayouting of the parent Dialog.
279          * @param parent
280          * @param dialog
281          * @param expandText
282          * @return
283          */

284         public Composite createComposite(
285                 Composite parent,
286                 ExpandableAreaDialog dialog,
287                 String JavaDoc expandText
288             )
289         {
290             if (dialog == null)
291                 throw new IllegalArgumentException JavaDoc(this.getClass().getName()+"#createComposite: Parameter dialog can not be null.");
292             this.dialog = dialog;
293             // create the Composite
294
expComp = new Composite(parent,SWT.NONE);
295             // LayoutData takes care of layouting within the dialog ...
296
GridData myData = new GridData(GridData.FILL_BOTH);
297             expComp.setLayoutData(myData);
298             
299             // Use a TableWrapLayout for the parent
300
GridLayout layout = new GridLayout();
301             expComp.setLayout(layout);
302             
303             // create the static Composite
304
staticArea = createStaticArea(expComp);
305             if (staticArea != null) {
306                 // take care of its layoutData
307
GridData gdStatic = new GridData(GridData.FILL_BOTH);
308 // gdStatic.grabExcessHorizontalSpace = true;
309
// gdStatic.horizontalAlignment=GridData.FILL_;
310
staticArea.setLayoutData(gdStatic);
311             }
312                     
313             // the ExpandableComposite
314
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             // the ExpansionListener re-packs the dialog
320
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             // dummyComp wraps the expandable Comp one more time.
333
// Workaround, otherwise half of first row in expandableArea
334
// visible even when collapsed
335
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                 // set the LayoutData
344
gd = new GridData(GridData.FILL_BOTH);
345                 expandableArea.setLayoutData(gd);
346                 // tell the parent to manage this as expandable client
347
ec.setClient(dummyComp);
348             }
349
350             return expComp;
351         }
352         
353     }
354 }
355
Popular Tags