KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > forms > MasterDetailsBlock


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.forms;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15
16 import org.eclipse.swt.SWT;
17 import org.eclipse.swt.custom.SashForm;
18 import org.eclipse.swt.graphics.GC;
19 import org.eclipse.swt.graphics.Point;
20 import org.eclipse.swt.layout.GridData;
21 import org.eclipse.swt.layout.GridLayout;
22 import org.eclipse.swt.widgets.Composite;
23 import org.eclipse.swt.widgets.Control;
24 import org.eclipse.swt.widgets.Event;
25 import org.eclipse.swt.widgets.Listener;
26 import org.eclipse.swt.widgets.Sash;
27 import org.eclipse.ui.forms.widgets.FormToolkit;
28 import org.eclipse.ui.forms.widgets.ScrolledForm;
29
30 /**
31  * This class implements the 'master/details' UI pattern suitable for inclusion
32  * in a form. The block consists of two parts: 'master' and 'details' in a sash
33  * form that allows users to change the relative ratio on the page. The master
34  * part needs to be created by the users of this class. The details part is
35  * created by the block.
36  * <p>
37  * The master part is responsible for adding itself as a form part and firing
38  * selection events. The details part catches the selection events and tries to
39  * load a page registered to handle the selected object(s). The page shows the
40  * details of the selected object(s) and allows users to edit them.
41  * <p>
42  * Details pages can be registered statically using 'registerPage' or
43  * dynamically through the use of 'IDetailsPageProvider' in case where different
44  * pages need to be shown for objects of the same type depending on their state.
45  * <p>
46  * Subclasses are required to implement abstract methods of this class. Master
47  * part must be created and at least one details page should be registered in
48  * order to show details of the objects selected in the master part. Tool bar
49  * actions can be optionally added to the tool bar manager.
50  *
51  * @see DetailsPart
52  * @see IDetailsPage
53  * @see IDetailsPageProvider
54  * @since 3.0
55  */

56 public abstract class MasterDetailsBlock {
57     /**
58      * Details part created by the block. No attempt should be made to access
59      * this field inside <code>createMasterPart</code> because it has not been
60      * created yet and will be <code>null</code>.
61      */

62     protected DetailsPart detailsPart;
63
64     /**
65      * The form that is the parent of both master and details part. The form
66      * allows users to change the ratio between the two parts.
67      */

68     protected SashForm sashForm;
69     
70     static final int DRAGGER_SIZE = 40;
71     
72     class MDSashForm extends SashForm {
73         ArrayList JavaDoc sashes = new ArrayList JavaDoc();
74         Listener listener = new Listener () {
75             public void handleEvent(Event e) {
76                 switch (e.type) {
77                 case SWT.MouseEnter:
78                     e.widget.setData("hover", Boolean.TRUE); //$NON-NLS-1$
79
((Control)e.widget).redraw();
80                     break;
81                 case SWT.MouseExit:
82                     e.widget.setData("hover", null); //$NON-NLS-1$
83
((Control)e.widget).redraw();
84                     break;
85                 case SWT.Paint:
86                     onSashPaint(e);
87                 break;
88                 case SWT.Resize:
89                     hookSashListeners();
90                 break;
91                 }
92             }
93         };
94         public MDSashForm(Composite parent, int style) {
95             super(parent, style);
96         }
97         
98         public void layout(boolean changed) {
99             super.layout(changed);
100             hookSashListeners();
101         }
102         
103         public void layout(Control [] children) {
104             super.layout(children);
105             hookSashListeners();
106         }
107
108         private void hookSashListeners() {
109             purgeSashes();
110             Control [] children = getChildren();
111             for (int i=0; i<children.length; i++) {
112                 if (children[i] instanceof Sash) {
113                     Sash sash = (Sash)children[i];
114                     if (sashes.contains(sash))
115                         continue;
116                     sash.addListener(SWT.Paint, listener);
117                     sash.addListener(SWT.MouseEnter, listener);
118                     sash.addListener(SWT.MouseExit, listener);
119                     sashes.add(sash);
120                 }
121             }
122         }
123         private void purgeSashes() {
124             for (Iterator JavaDoc iter=sashes.iterator(); iter.hasNext();) {
125                 Sash sash = (Sash)iter.next();
126                 if (sash.isDisposed())
127                     iter.remove();
128             }
129         }
130     }
131
132     /**
133      * Creates the content of the master/details block inside the managed form.
134      * This method should be called as late as possible inside the parent part.
135      *
136      * @param managedForm
137      * the managed form to create the block in
138      */

139     public void createContent(IManagedForm managedForm) {
140         final ScrolledForm form = managedForm.getForm();
141         FormToolkit toolkit = managedForm.getToolkit();
142         GridLayout layout = new GridLayout();
143         layout.marginWidth = 0;
144         layout.marginHeight = 0;
145         form.getBody().setLayout(layout);
146         sashForm = new MDSashForm(form.getBody(), SWT.NULL);
147         sashForm.setData("form", managedForm); //$NON-NLS-1$
148
toolkit.adapt(sashForm, false, false);
149         sashForm.setMenu(form.getBody().getMenu());
150         sashForm.setLayoutData(new GridData(GridData.FILL_BOTH));
151         createMasterPart(managedForm, sashForm);
152         createDetailsPart(managedForm, sashForm);
153         hookResizeListener();
154         createToolBarActions(managedForm);
155         form.updateToolBar();
156     }
157     
158     private void hookResizeListener() {
159         Listener listener = ((MDSashForm)sashForm).listener;
160         Control [] children = sashForm.getChildren();
161         for (int i=0; i<children.length; i++) {
162             if (children[i] instanceof Sash) continue;
163             children[i].addListener(SWT.Resize, listener);
164         }
165     }
166
167     /**
168      * Implement this method to create a master part in the provided parent.
169      * Typical master parts are section parts that contain tree or table viewer.
170      *
171      * @param managedForm
172      * the parent form
173      * @param parent
174      * the parent composite
175      */

176     protected abstract void createMasterPart(IManagedForm managedForm,
177             Composite parent);
178
179     /**
180      * Implement this method to statically register pages for the expected
181      * object types. This mechanism can be used when there is 1-&gt;1 mapping
182      * between object classes and details pages.
183      *
184      * @param detailsPart
185      * the details part
186      */

187     protected abstract void registerPages(DetailsPart detailsPart);
188
189     /**
190      * Implement this method to create form tool bar actions and add them to the
191      * form tool bar if desired.
192      *
193      * @param managedForm
194      * the form that owns the tool bar
195      */

196     protected abstract void createToolBarActions(IManagedForm managedForm);
197
198     private void createDetailsPart(final IManagedForm mform, Composite parent) {
199         detailsPart = new DetailsPart(mform, parent, SWT.NULL);
200         mform.addPart(detailsPart);
201         registerPages(detailsPart);
202     }
203     
204     private void onSashPaint(Event e) {
205         Sash sash = (Sash)e.widget;
206         IManagedForm form = (IManagedForm)sash.getParent().getData("form"); //$NON-NLS-1$
207
FormColors colors = form.getToolkit().getColors();
208         boolean vertical = (sash.getStyle() & SWT.VERTICAL)!=0;
209         GC gc = e.gc;
210         Boolean JavaDoc hover = (Boolean JavaDoc)sash.getData("hover"); //$NON-NLS-1$
211
gc.setBackground(colors.getColor(IFormColors.TB_BG));
212         gc.setForeground(colors.getColor(IFormColors.TB_BORDER));
213         Point size = sash.getSize();
214         if (vertical) {
215             if (hover!=null)
216                 gc.fillRectangle(0, 0, size.x, size.y);
217             //else
218
//gc.drawLine(1, 0, 1, size.y-1);
219
}
220         else {
221             if (hover!=null)
222                 gc.fillRectangle(0, 0, size.x, size.y);
223             //else
224
//gc.drawLine(0, 1, size.x-1, 1);
225
}
226     }
227 }
228
Popular Tags