KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Sebastian Davids <sdavids@gmx.de> - bug 75886
11  *******************************************************************************/

12 package org.eclipse.team.internal.ui.dialogs;
13
14 import java.io.IOException JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.Map JavaDoc;
17
18 import org.eclipse.compare.internal.TabFolderLayout;
19 import org.eclipse.jface.dialogs.*;
20 import org.eclipse.jface.preference.IPersistentPreferenceStore;
21 import org.eclipse.jface.preference.IPreferencePageContainer;
22 import org.eclipse.jface.preference.IPreferenceStore;
23 import org.eclipse.jface.preference.PreferenceDialog;
24 import org.eclipse.jface.preference.PreferencePage;
25 import org.eclipse.jface.resource.ImageDescriptor;
26 import org.eclipse.jface.resource.ImageRegistry;
27 import org.eclipse.jface.resource.JFaceColors;
28 import org.eclipse.jface.resource.JFaceResources;
29 import org.eclipse.jface.util.IPropertyChangeListener;
30 import org.eclipse.jface.util.PropertyChangeEvent;
31 import org.eclipse.swt.SWT;
32 import org.eclipse.swt.custom.CLabel;
33 import org.eclipse.swt.events.DisposeEvent;
34 import org.eclipse.swt.events.DisposeListener;
35 import org.eclipse.swt.events.SelectionAdapter;
36 import org.eclipse.swt.events.SelectionEvent;
37 import org.eclipse.swt.graphics.Color;
38 import org.eclipse.swt.graphics.Image;
39 import org.eclipse.swt.graphics.Point;
40 import org.eclipse.swt.layout.FillLayout;
41 import org.eclipse.swt.layout.GridData;
42 import org.eclipse.swt.layout.GridLayout;
43 import org.eclipse.swt.widgets.Button;
44 import org.eclipse.swt.widgets.Composite;
45 import org.eclipse.swt.widgets.Control;
46 import org.eclipse.swt.widgets.Display;
47 import org.eclipse.swt.widgets.Label;
48 import org.eclipse.swt.widgets.Shell;
49 import org.eclipse.swt.widgets.TabFolder;
50 import org.eclipse.swt.widgets.TabItem;
51 import org.eclipse.team.internal.ui.*;
52 import org.eclipse.ui.PlatformUI;
53
54 public class PreferencePageContainerDialog extends TrayDialog implements IPreferencePageContainer {
55
56     private PreferencePage[] pages;
57     private PreferencePage currentPage;
58     
59     private Composite fTitleArea;
60     private Label fTitleImage;
61     private CLabel fMessageLabel;
62     
63     private String JavaDoc fMessage;
64     private Color fNormalMsgAreaBackground;
65     private Image fErrorMsgImage;
66
67     private Button fOkButton;
68     
69     /**
70      * The Composite in which a page is shown.
71      */

72     private Composite fPageContainer;
73
74     /**
75      * The minimum page size; 200 by 200 by default.
76      *
77      * @see #setMinimumPageSize(Point)
78      */

79     private Point fMinimumPageSize = new Point(200,200);
80     private TabFolder tabFolder;
81     private Map JavaDoc pageMap = new HashMap JavaDoc();
82     
83     /**
84      * Must declare our own images as the JFaceResource images will not be created unless
85      * a property/preference dialog has been shown
86      */

87     protected static final String JavaDoc PREF_DLG_TITLE_IMG = "preference_page_container_image";//$NON-NLS-1$
88
protected static final String JavaDoc PREF_DLG_IMG_TITLE_ERROR = "preference_page_container_title_error_image";//$NON-NLS-1$
89
private String JavaDoc helpId;
90     static {
91         ImageRegistry reg = TeamUIPlugin.getPlugin().getImageRegistry();
92         reg.put(PREF_DLG_TITLE_IMG, ImageDescriptor.createFromFile(PreferenceDialog.class, "images/pref_dialog_title.gif"));//$NON-NLS-1$
93
reg.put(PREF_DLG_IMG_TITLE_ERROR, ImageDescriptor.createFromFile(Dialog.class, "images/message_error.gif"));//$NON-NLS-1$
94
}
95         
96     public PreferencePageContainerDialog(Shell shell, PreferencePage[] pages) {
97         super(shell);
98         this.pages = pages;
99     }
100     
101     /**
102      * @see Dialog#okPressed()
103      */

104     protected void okPressed() {
105         for (int i = 0; i < pages.length; i++) {
106             PreferencePage page = pages[i];
107             page.performOk();
108         }
109         
110         handleSave();
111         
112         super.okPressed();
113     }
114     
115     /**
116      * Sets the title for this dialog.
117      * @param title the title.
118      */

119     public void setTitle(String JavaDoc title) {
120         Shell shell= getShell();
121         if ((shell != null) && !shell.isDisposed()) {
122             shell.setText(title);
123         }
124     }
125     
126     /**
127      * @see Dialog#createDialogArea(Composite)
128      */

129     protected Control createDialogArea(Composite parent) {
130         Composite composite = (Composite)super.createDialogArea(parent);
131         ((GridLayout) composite.getLayout()).numColumns = 1;
132         
133         createDescriptionArea(composite);
134         
135         if (isSinglePage()) {
136             createSinglePageArea(composite, pages[0]);
137         } else {
138             createMultiplePageArea(composite);
139         }
140             
141         // Build the separator line
142
Label separator = new Label(composite, SWT.HORIZONTAL | SWT.SEPARATOR);
143         GridData gd = new GridData(GridData.FILL_HORIZONTAL);
144         gd.horizontalSpan = 2;
145         separator.setLayoutData(gd);
146     
147         setTitle(TeamUIMessages.PreferencePageContainerDialog_6);
148         applyDialogFont(parent);
149         if (helpId != null) {
150             PlatformUI.getWorkbench().getHelpSystem().setHelp(composite, helpId);
151         }
152         return composite;
153     }
154     
155     private void createMultiplePageArea(Composite composite) {
156         // create a tab folder for the page
157
tabFolder = new TabFolder(composite, SWT.NONE);
158         tabFolder.setLayout(new TabFolderLayout());
159         tabFolder.setLayoutData(new GridData(GridData.FILL_BOTH));
160         
161         for (int i = 0; i < pages.length; i++) {
162             PreferencePage page = pages[i];
163             // text decoration options
164
TabItem tabItem = new TabItem(tabFolder, SWT.NONE);
165             tabItem.setText(page.getTitle());//
166
tabItem.setControl(createPageArea(tabFolder, page));
167             pageMap.put(tabItem, page);
168         }
169         
170         tabFolder.addSelectionListener(new SelectionAdapter() {
171             public void widgetSelected(SelectionEvent e) {
172                 updatePageSelection();
173             }
174         });
175         updatePageSelection();
176     }
177
178     protected void updatePageSelection() {
179         TabItem[] items = tabFolder.getSelection();
180         if (items.length == 1) {
181             currentPage = (PreferencePage)pageMap.get(items[0]);
182             updateMessage();
183         }
184     }
185
186     private boolean isSinglePage() {
187         return pages.length == 1;
188     }
189
190     /*
191      * Create the page contents for a single preferences page
192      */

193     private void createSinglePageArea(Composite composite, PreferencePage page) {
194         createPageArea(composite, page);
195         currentPage = page;
196         updateMessage();
197     }
198
199     private Control createPageArea(Composite composite, PreferencePage page) {
200         // Build the Page container
201
fPageContainer = createPageContainer(composite);
202         fPageContainer.setLayoutData(new GridData(GridData.FILL_BOTH));
203
204         page.setContainer(this);
205         page.createControl(fPageContainer);
206         return fPageContainer;
207     }
208
209     private void createDescriptionArea(Composite composite) {
210         // Build the title area and separator line
211
Composite titleComposite = new Composite(composite, SWT.NONE);
212         GridLayout layout = new GridLayout();
213         layout.marginHeight = 0;
214         layout.marginWidth = 0;
215         layout.verticalSpacing = 0;
216         layout.horizontalSpacing = 0;
217         titleComposite.setLayout(layout);
218         titleComposite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
219         
220         createMessageArea(titleComposite);
221     
222         Label titleBarSeparator = new Label(titleComposite, SWT.HORIZONTAL | SWT.SEPARATOR);
223         GridData gd = new GridData(GridData.FILL_HORIZONTAL);
224         titleBarSeparator.setLayoutData(gd);
225     }
226
227     /**
228      * Creates the dialog's title area.
229      *
230      * @param parent the SWT parent for the title area composite
231      * @return the created title area composite
232      */

233     private Composite createMessageArea(Composite parent) {
234         
235         // Create the title area which will contain
236
// a title, message, and image.
237
fTitleArea = new Composite(parent, SWT.NONE);
238         GridLayout layout = new GridLayout();
239         layout.marginHeight = 0;
240         layout.marginWidth = 0;
241         layout.verticalSpacing = 0;
242         layout.horizontalSpacing = 0;
243         layout.numColumns = 2;
244         
245         // Get the colors for the title area
246
Display display = parent.getDisplay();
247         Color bg = JFaceColors.getBannerBackground(display);
248         Color fg = JFaceColors.getBannerForeground(display);
249         
250         GridData layoutData = new GridData(GridData.FILL_BOTH);
251         fTitleArea.setLayout(layout);
252         fTitleArea.setLayoutData(layoutData);
253         fTitleArea.setBackground(bg);
254     
255         // Message label
256
fMessageLabel = new CLabel(fTitleArea, SWT.LEFT);
257         fMessageLabel.setBackground(bg);
258         fMessageLabel.setForeground(fg);
259         fMessageLabel.setText(" ");//$NON-NLS-1$
260
fMessageLabel.setFont(JFaceResources.getBannerFont());
261         
262         final IPropertyChangeListener fontListener = new IPropertyChangeListener() {
263             public void propertyChange(PropertyChangeEvent event) {
264                 if(JFaceResources.BANNER_FONT.equals(event.getProperty()) ||
265                     JFaceResources.DIALOG_FONT.equals(event.getProperty())) {
266                     updateMessage();
267                 }
268             }
269         };
270         
271         fMessageLabel.addDisposeListener(new DisposeListener() {
272             public void widgetDisposed(DisposeEvent event) {
273                 JFaceResources.getFontRegistry().removeListener(fontListener);
274             }
275         });
276         
277         JFaceResources.getFontRegistry().addListener(fontListener);
278                 
279         GridData gd = new GridData(GridData.FILL_BOTH);
280         fMessageLabel.setLayoutData(gd);
281     
282         // Title image
283
fTitleImage = new Label(fTitleArea, SWT.LEFT);
284         fTitleImage.setBackground(bg);
285         fTitleImage.setImage(TeamUIPlugin.getPlugin().getImageRegistry().get(PREF_DLG_TITLE_IMG));
286         gd = new GridData();
287         gd.horizontalAlignment = GridData.END;
288         fTitleImage.setLayoutData(gd);
289         updateMessage();
290         return fTitleArea;
291     }
292
293     /**
294      * Creates the inner page container.
295      */

296     private Composite createPageContainer(Composite parent) {
297         Composite result = new Composite(parent, SWT.NULL);
298         FillLayout layout = new FillLayout();
299         layout.marginHeight = 5;
300         layout.marginWidth = 5;
301         result.setLayout(layout);
302         return result;
303     }
304
305     /**
306      * Sets the minimum page size.
307      *
308      * @param size the page size encoded as
309      * <code>new Point(width,height)</code>
310      */

311     public void setMinimumPageSize(Point size) {
312         fMinimumPageSize.x = size.x;
313         fMinimumPageSize.y = size.y;
314     }
315     
316     /**
317      * Display the given error message. The currently displayed message
318      * is saved and will be redisplayed when the error message is set
319      * to <code>null</code>.
320      *
321      * @param errorMessage the errorMessage to display or <code>null</code>
322      */

323     public void setErrorMessage(String JavaDoc errorMessage) {
324         if (errorMessage == null) {
325             if (fMessageLabel.getImage() != null) {
326                 // we were previously showing an error
327
fMessageLabel.setBackground(fNormalMsgAreaBackground);
328                 fMessageLabel.setImage(null);
329                 fTitleImage.setImage(TeamUIPlugin.getPlugin().getImageRegistry().get(PREF_DLG_TITLE_IMG));
330                 fTitleArea.layout(true);
331             }
332     
333             // show the message
334
setMessage(fMessage);
335     
336         } else {
337             fMessageLabel.setText(errorMessage);
338             if (fMessageLabel.getImage() == null) {
339                 // we were not previously showing an error
340

341                 // lazy initialize the error background color and image
342
if (fErrorMsgImage == null) {
343                     fErrorMsgImage = TeamUIPlugin.getPlugin().getImageRegistry().get(PREF_DLG_IMG_TITLE_ERROR);
344                 }
345     
346                 // show the error
347
fNormalMsgAreaBackground = fMessageLabel.getBackground();
348                 fMessageLabel.setBackground(JFaceColors.getErrorBackground(fMessageLabel.getDisplay()));
349                 fMessageLabel.setImage(fErrorMsgImage);
350                 fTitleImage.setImage(null);
351                 fTitleArea.layout(true);
352             }
353         }
354     }
355     /**
356      * Set the message text. If the message line currently displays an error,
357      * the message is stored and will be shown after a call to clearErrorMessage
358      */

359     public void setMessage(String JavaDoc newMessage) {
360         fMessage = newMessage;
361         if (fMessage == null) {
362             fMessage = "";//$NON-NLS-1$
363
}
364         if (fMessageLabel.getImage() == null) {
365             // we are not showing an error
366
fMessageLabel.setText(fMessage);
367         }
368     }
369     
370     /**
371      * @see IPreferencePageContainer#updateMessage()
372      */

373     public void updateMessage() {
374         if (currentPage != null) {
375             String JavaDoc pageMessage = currentPage.getMessage();
376             String JavaDoc pageErrorMessage = currentPage.getErrorMessage();
377     
378             // Adjust the font
379
if (pageMessage == null && pageErrorMessage == null)
380                 fMessageLabel.setFont(JFaceResources.getBannerFont());
381             else
382                 fMessageLabel.setFont(JFaceResources.getDialogFont());
383     
384             // Set the message and error message
385
if (pageMessage == null) {
386                 if (isSinglePage()) {
387                     setMessage(TeamUIMessages.PreferencePageContainerDialog_6);
388                 } else {
389                     //remove mnemonic see bug 75886
390
String JavaDoc title = currentPage.getTitle();
391                     title = title.replaceAll("&", "");//$NON-NLS-1$ //$NON-NLS-2$
392
setMessage(title);
393                 }
394             } else {
395                 setMessage(pageMessage);
396             }
397             setErrorMessage(pageErrorMessage);
398         }
399     }
400     
401     /**
402      * @see IPreferencePageContainer#getPreferenceStore()
403      */

404     public IPreferenceStore getPreferenceStore() {
405         return null;
406     }
407
408     /**
409      * @see IPreferencePageContainer#updateButtons()
410      */

411     public void updateButtons() {
412         if (fOkButton != null) {
413             boolean isValid = true;
414             for (int i = 0; i < pages.length; i++) {
415                 PreferencePage page = pages[i];
416                 if (!page.isValid()) {
417                     isValid = false;
418                     break;
419                 }
420             }
421             fOkButton.setEnabled(isValid);
422         }
423     }
424
425     /**
426      * @see IPreferencePageContainer#updateTitle()
427      */

428     public void updateTitle() {
429         updateMessage();
430     }
431     
432     /**
433      * @see Dialog#createButtonsForButtonBar(Composite)
434      */

435     protected void createButtonsForButtonBar(Composite parent) {
436         fOkButton= createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, true);
437         createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false);
438     }
439     
440     /**
441      * Save the values specified in the pages.
442      * <p>
443      * The default implementation of this framework method saves all
444      * pages of type <code>PreferencePage</code> (if their store needs saving
445      * and is a <code>PreferenceStore</code>).
446      * </p>
447      * <p>
448      * Subclasses may override.
449      * </p>
450      */

451     protected void handleSave() {
452         // Save now in case tbe workbench does not shutdown cleanly
453
for (int i = 0; i < pages.length; i++) {
454             PreferencePage page = pages[i];
455             IPreferenceStore store = page.getPreferenceStore();
456             if (store != null
457                 && store.needsSaving()
458                 && store instanceof IPersistentPreferenceStore) {
459                 try {
460                     ((IPersistentPreferenceStore) store).save();
461                 } catch (IOException JavaDoc e) {
462                     Utils.handle(e);
463                 }
464             }
465         }
466     }
467
468     public void setHelpContextId(String JavaDoc helpId) {
469         this.helpId = helpId;
470     }
471 }
472
Popular Tags