1 12 package org.eclipse.team.internal.ui.dialogs; 13 14 import java.io.IOException ; 15 import java.util.HashMap ; 16 import java.util.Map ; 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 fMessage; 64 private Color fNormalMsgAreaBackground; 65 private Image fErrorMsgImage; 66 67 private Button fOkButton; 68 69 72 private Composite fPageContainer; 73 74 79 private Point fMinimumPageSize = new Point(200,200); 80 private TabFolder tabFolder; 81 private Map pageMap = new HashMap (); 82 83 87 protected static final String PREF_DLG_TITLE_IMG = "preference_page_container_image"; protected static final String PREF_DLG_IMG_TITLE_ERROR = "preference_page_container_title_error_image"; private String 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")); reg.put(PREF_DLG_IMG_TITLE_ERROR, ImageDescriptor.createFromFile(Dialog.class, "images/message_error.gif")); } 95 96 public PreferencePageContainerDialog(Shell shell, PreferencePage[] pages) { 97 super(shell); 98 this.pages = pages; 99 } 100 101 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 119 public void setTitle(String title) { 120 Shell shell= getShell(); 121 if ((shell != null) && !shell.isDisposed()) { 122 shell.setText(title); 123 } 124 } 125 126 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 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 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 TabItem tabItem = new TabItem(tabFolder, SWT.NONE); 165 tabItem.setText(page.getTitle()); 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 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 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 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 233 private Composite createMessageArea(Composite parent) { 234 235 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 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 fMessageLabel = new CLabel(fTitleArea, SWT.LEFT); 257 fMessageLabel.setBackground(bg); 258 fMessageLabel.setForeground(fg); 259 fMessageLabel.setText(" "); 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 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 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 311 public void setMinimumPageSize(Point size) { 312 fMinimumPageSize.x = size.x; 313 fMinimumPageSize.y = size.y; 314 } 315 316 323 public void setErrorMessage(String errorMessage) { 324 if (errorMessage == null) { 325 if (fMessageLabel.getImage() != null) { 326 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 setMessage(fMessage); 335 336 } else { 337 fMessageLabel.setText(errorMessage); 338 if (fMessageLabel.getImage() == null) { 339 341 if (fErrorMsgImage == null) { 343 fErrorMsgImage = TeamUIPlugin.getPlugin().getImageRegistry().get(PREF_DLG_IMG_TITLE_ERROR); 344 } 345 346 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 359 public void setMessage(String newMessage) { 360 fMessage = newMessage; 361 if (fMessage == null) { 362 fMessage = ""; } 364 if (fMessageLabel.getImage() == null) { 365 fMessageLabel.setText(fMessage); 367 } 368 } 369 370 373 public void updateMessage() { 374 if (currentPage != null) { 375 String pageMessage = currentPage.getMessage(); 376 String pageErrorMessage = currentPage.getErrorMessage(); 377 378 if (pageMessage == null && pageErrorMessage == null) 380 fMessageLabel.setFont(JFaceResources.getBannerFont()); 381 else 382 fMessageLabel.setFont(JFaceResources.getDialogFont()); 383 384 if (pageMessage == null) { 386 if (isSinglePage()) { 387 setMessage(TeamUIMessages.PreferencePageContainerDialog_6); 388 } else { 389 String title = currentPage.getTitle(); 391 title = title.replaceAll("&", ""); setMessage(title); 393 } 394 } else { 395 setMessage(pageMessage); 396 } 397 setErrorMessage(pageErrorMessage); 398 } 399 } 400 401 404 public IPreferenceStore getPreferenceStore() { 405 return null; 406 } 407 408 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 428 public void updateTitle() { 429 updateMessage(); 430 } 431 432 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 451 protected void handleSave() { 452 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 e) { 462 Utils.handle(e); 463 } 464 } 465 } 466 } 467 468 public void setHelpContextId(String helpId) { 469 this.helpId = helpId; 470 } 471 } 472 | Popular Tags |