KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ui > dialogs > DialogArea


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.team.internal.ui.dialogs;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.jface.dialogs.*;
18 import org.eclipse.jface.dialogs.Dialog;
19 import org.eclipse.jface.util.IPropertyChangeListener;
20 import org.eclipse.jface.util.PropertyChangeEvent;
21 import org.eclipse.swt.SWT;
22 import org.eclipse.swt.graphics.*;
23 import org.eclipse.swt.layout.GridData;
24 import org.eclipse.swt.layout.GridLayout;
25 import org.eclipse.swt.widgets.*;
26
27 /**
28  * This class provides facilities to allow common widget groupings to be shared
29  * by mulitple dialogs or wizards.
30  */

31 public abstract class DialogArea {
32
33     private FontMetrics fontMetrics;
34     private List JavaDoc listeners;
35     
36     /**
37      * Create a dialog area
38      */

39     protected DialogArea() {
40         this.listeners = new ArrayList JavaDoc();
41     }
42     
43     /**
44      * Listener for property change events. The only event of interest is for
45      * property SELECTED_WORKING_SET which contains the selected working set or
46      * <code>null</code> if none is selected.
47      *
48      * @param listener
49      */

50     public void addPropertyChangeListener(IPropertyChangeListener listener) {
51         if (!listeners.contains(listener))
52             listeners.add(listener);
53     }
54     /**
55      * Remove the provided listener from the receiver.
56      *
57      * @param listener
58      */

59     public void removePropertyChangeListener(IPropertyChangeListener listener) {
60         listeners.remove(listener);
61     }
62
63     protected void firePropertyChangeChange(String JavaDoc property, Object JavaDoc oldValue, Object JavaDoc newValue) {
64         PropertyChangeEvent event = new PropertyChangeEvent(this, property, oldValue, newValue);
65         for (Iterator JavaDoc iter = listeners.iterator(); iter.hasNext();) {
66             IPropertyChangeListener listener = (IPropertyChangeListener) iter.next();
67             listener.propertyChange(event);
68         }
69     }
70     
71     /**
72      * Code copied from <code>org.eclipse.jface.dialogs.Dialog</code> to obtain
73      * a FontMetrics.
74      *
75      * @param control a control from which to obtain the current font
76      *
77      * @see org.eclipse.jface.dialogs.Dialog
78      */

79     protected void initializeDialogUnits(Control control) {
80         // Compute and store a font metric
81
GC gc = new GC(control);
82         gc.setFont(control.getFont());
83         fontMetrics = gc.getFontMetrics();
84         gc.dispose();
85     }
86
87     /**
88      * Create the area using the given parent as the containing composite
89      * @param parent
90      */

91     public abstract void createArea(Composite parent);
92     
93     protected Button createCheckbox(Composite parent, String JavaDoc label, int span) {
94         Button button = new Button(parent, SWT.CHECK | SWT.LEFT);
95         button.setText(label);
96         button.setFont(parent.getFont());
97         GridData data = new GridData();
98         data.horizontalSpan = span;
99         button.setLayoutData(data);
100         return button;
101     }
102     
103     protected Button createButton(Composite parent, String JavaDoc label, int style) {
104         Button button = new Button(parent, SWT.PUSH);
105         button.setText(label);
106         // we need to explicitly set the font to the parent's font for dialogs
107
button.setFont(parent.getFont());
108         GridData data = new GridData(style);
109         data.heightHint = Dialog.convertVerticalDLUsToPixels(fontMetrics, IDialogConstants.BUTTON_HEIGHT);
110         int widthHint = Dialog.convertHorizontalDLUsToPixels(fontMetrics, IDialogConstants.BUTTON_WIDTH);
111         data.widthHint = Math.max(widthHint, button.computeSize(SWT.DEFAULT, SWT.DEFAULT, true).x);
112         button.setLayoutData(data);
113         return button;
114     }
115
116     protected Button createRadioButton(Composite parent, String JavaDoc label, int span) {
117         Button button = new Button(parent, SWT.RADIO);
118         button.setText(label);
119         GridData data = new GridData();
120         data.horizontalSpan = span;
121         button.setLayoutData(data);
122         return button;
123     }
124     protected Label createWrappingLabel(Composite parent, String JavaDoc text, int horizontalSpan) {
125         Label label = new Label(parent, SWT.LEFT | SWT.WRAP);
126         label.setText(text);
127         label.setFont(parent.getFont());
128         GridData data = new GridData();
129         data.horizontalSpan = horizontalSpan;
130         data.horizontalAlignment = GridData.FILL;
131         data.grabExcessHorizontalSpace = true;
132         data.widthHint= 0;
133         label.setLayoutData(data);
134         return label;
135     }
136     protected Label createLabel(Composite parent, String JavaDoc text, int horizontalSpan) {
137         Label label = new Label(parent, SWT.LEFT);
138         label.setText(text);
139         GridData data = new GridData();
140         data.horizontalSpan = horizontalSpan;
141         data.horizontalAlignment = GridData.FILL;
142         label.setLayoutData(data);
143         return label;
144     }
145     /**
146      * Creates composite control and sets the default layout data.
147      * @param parent the parent of the new composite
148      * @param numColumns the number of columns for the new composite
149      * @param grab specify whether the composite should grab for excessive space in both directions.
150      * @return the newly-created coposite
151      */

152     protected Composite createComposite(Composite parent, int numColumns, boolean grab) {
153         final Composite composite = new Composite(parent, SWT.NULL);
154         final Font font = parent.getFont();
155         composite.setFont(font);
156         
157         composite.setLayout(new GridLayout(numColumns, false));
158         composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, grab, grab));
159         
160         return composite;
161     }
162     
163     /**
164      * Creates composite control and sets the default layout data.
165      *
166      * @param parent the parent of the new composite
167      * @param numColumns the number of columns for the new composite
168      * @return the newly-created coposite
169      */

170     protected Composite createGrabbingComposite(Composite parent, int numColumns) {
171         Composite composite = new Composite(parent, SWT.NULL);
172         Font font = parent.getFont();
173         composite.setFont(font);
174         
175         // GridLayout
176
GridLayout layout = new GridLayout();
177         layout.numColumns = numColumns;
178         layout.marginHeight = 0;
179         layout.marginWidth = 0;
180         composite.setLayout(layout);
181
182         // GridData
183
GridData data = new GridData();
184         data.verticalAlignment = GridData.FILL;
185         data.horizontalAlignment = GridData.FILL;
186         data.grabExcessHorizontalSpace = true;
187         data.grabExcessVerticalSpace = true;
188         composite.setLayoutData(data);
189         return composite;
190     }
191     
192     protected int convertVerticalDLUsToPixels(int dlus) {
193         return Dialog.convertVerticalDLUsToPixels(fontMetrics, dlus);
194     }
195     
196     protected int convertHorizontalDLUsToPixels(int dlus) {
197         return Dialog.convertHorizontalDLUsToPixels(fontMetrics, dlus);
198     }
199 }
200
Popular Tags