1 11 package org.eclipse.pde.internal.ui.editor.product; 12 13 import org.eclipse.jface.fieldassist.ControlDecoration; 14 import org.eclipse.jface.fieldassist.FieldDecoration; 15 import org.eclipse.jface.fieldassist.FieldDecorationRegistry; 16 import org.eclipse.jface.preference.ColorSelector; 17 import org.eclipse.jface.util.IPropertyChangeListener; 18 import org.eclipse.jface.util.PropertyChangeEvent; 19 import org.eclipse.pde.core.IModelChangedEvent; 20 import org.eclipse.pde.internal.core.iproduct.IProduct; 21 import org.eclipse.pde.internal.core.iproduct.IProductModel; 22 import org.eclipse.pde.internal.core.iproduct.IProductObject; 23 import org.eclipse.pde.internal.core.iproduct.ISplashInfo; 24 import org.eclipse.pde.internal.core.product.SplashInfo; 25 import org.eclipse.pde.internal.core.util.PDETextHelper; 26 import org.eclipse.pde.internal.ui.PDEUIMessages; 27 import org.eclipse.pde.internal.ui.editor.FormLayoutFactory; 28 import org.eclipse.pde.internal.ui.editor.PDEFormPage; 29 import org.eclipse.pde.internal.ui.editor.PDESection; 30 import org.eclipse.pde.internal.ui.parts.ComboPart; 31 import org.eclipse.pde.internal.ui.wizards.product.ISplashHandlerConstants; 32 import org.eclipse.swt.SWT; 33 import org.eclipse.swt.dnd.Clipboard; 34 import org.eclipse.swt.events.ModifyEvent; 35 import org.eclipse.swt.events.ModifyListener; 36 import org.eclipse.swt.events.SelectionAdapter; 37 import org.eclipse.swt.events.SelectionEvent; 38 import org.eclipse.swt.graphics.Color; 39 import org.eclipse.swt.graphics.RGB; 40 import org.eclipse.swt.layout.GridData; 41 import org.eclipse.swt.layout.GridLayout; 42 import org.eclipse.swt.widgets.Button; 43 import org.eclipse.swt.widgets.Composite; 44 import org.eclipse.swt.widgets.Control; 45 import org.eclipse.swt.widgets.Display; 46 import org.eclipse.swt.widgets.Label; 47 import org.eclipse.swt.widgets.Spinner; 48 import org.eclipse.swt.widgets.Text; 49 import org.eclipse.ui.forms.IFormColors; 50 import org.eclipse.ui.forms.widgets.FormToolkit; 51 import org.eclipse.ui.forms.widgets.Section; 52 53 54 public class SplashConfigurationSection extends PDESection { 55 56 private static final int F_NUM_COLUMNS = 10; 57 58 private ColorSelector fColorSelector; 59 private boolean fBlockNotification; 60 61 private Button fAddBarButton; 62 private Spinner[] fBarSpinners = new Spinner[4]; 64 private Control[] fBarControls = new Control[8]; 66 67 private Button fAddMessageButton; 68 private Spinner[] fMessageSpinners = new Spinner[4]; 70 private Control[] fMessageControls = new Control[10]; 72 73 private Section fSection; 74 75 private FormToolkit fToolkit; 76 77 private ComboPart fFieldTemplateCombo; 78 79 public SplashConfigurationSection(PDEFormPage page, Composite parent) { 80 super(page, parent, Section.DESCRIPTION); 81 fFieldTemplateCombo = null; 82 createClient(getSection(), page.getEditor().getToolkit()); 83 } 84 85 88 protected void createClient(Section section, FormToolkit toolkit) { 89 fSection = section; 91 fToolkit = toolkit; 92 configureUISection(); 94 createUI(); 96 createUIListenerFieldTemplateCombo(); 98 } 100 101 104 private void createUIListenerFieldTemplateCombo() { 105 fFieldTemplateCombo.addSelectionListener(new SelectionAdapter() { 106 public void widgetSelected(SelectionEvent e) { 107 handleTemplateComboWidgetSelected(); 108 } 109 }); 110 } 111 112 115 private void handleTemplateComboWidgetSelected() { 116 if (fBlockNotification) { 118 return; 119 } 120 String template = getSelectedTemplate(); 122 getSplashInfo().setFieldSplashHandlerType(template, false); 123 updateFieldEnablement(); 125 } 126 127 130 private String getSelectedTemplate() { 131 int index = fFieldTemplateCombo.getSelectionIndex(); 132 int position = index - 1; 133 if ((index <= 0) || 134 (index > ISplashHandlerConstants.F_SPLASH_SCREEN_TYPE_CHOICES.length)) { 135 return null; 136 } 137 return ISplashHandlerConstants.F_SPLASH_SCREEN_TYPE_CHOICES[position][0]; 138 } 139 140 143 private void createUI() { 144 Composite container = createUISectionContainer(fSection); 146 createUILabelType(container); 148 createUIFieldTemplateCombo(container); 150 createUIFieldDecorationTemplate(); 152 createUILabelProgress(container); 154 createProgressBarConfig(container); 156 createProgressMessageConfig(container); 158 fToolkit.paintBordersFor(container); 160 fSection.setClient(container); 162 getModel().addModelChangedListener(this); 164 } 165 166 169 private void createUIFieldDecorationTemplate() { 170 int bits = SWT.TOP | SWT.LEFT; 172 ControlDecoration controlDecoration = 173 new ControlDecoration(fFieldTemplateCombo.getControl(), bits); 174 controlDecoration.setMarginWidth(0); 177 controlDecoration.setDescriptionText( 179 PDEUIMessages.SplashConfigurationSection_msgDecorationTemplateSupport); 180 controlDecoration.setShowHover(true); 182 controlDecoration.setShowOnlyOnFocus(true); 183 FieldDecoration contentProposalImage = 185 FieldDecorationRegistry.getDefault().getFieldDecoration( 186 FieldDecorationRegistry.DEC_INFORMATION); 187 controlDecoration.setImage(contentProposalImage.getImage()); 188 } 189 190 193 private void createUILabelType(Composite parent) { 194 Color foreground = fToolkit.getColors().getColor(IFormColors.TITLE); 195 Label label = fToolkit.createLabel(parent, 196 PDEUIMessages.SplashTemplatesSection_typeName, SWT.WRAP); 197 label.setForeground(foreground); 198 } 199 200 203 private void createUILabelProgress(Composite parent) { 204 Label label = fToolkit.createLabel(parent, 205 PDEUIMessages.SplashConfigurationSection_sectionDescCustomization, SWT.WRAP); 206 GridData data = new GridData(GridData.FILL_HORIZONTAL); 207 data.horizontalSpan = F_NUM_COLUMNS; 208 data.verticalIndent = 5; 209 label.setLayoutData(data); 210 } 211 212 215 private void createUIFieldTemplateCombo(Composite parent) { 216 int style = SWT.READ_ONLY | SWT.BORDER; 217 fFieldTemplateCombo = new ComboPart(); 218 fFieldTemplateCombo.createControl(parent, fToolkit, style); 219 GridData data = new GridData(GridData.FILL_HORIZONTAL); 220 data.horizontalSpan = F_NUM_COLUMNS - 1; 221 fFieldTemplateCombo.getControl().setLayoutData(data); 222 fFieldTemplateCombo.add(PDEUIMessages.SimpleCSCommandDetails_6, 0); 224 for (int i = 0; i < ISplashHandlerConstants.F_SPLASH_SCREEN_TYPE_CHOICES.length; i++) { 226 int position = i + 1; 227 fFieldTemplateCombo.add( 228 ISplashHandlerConstants.F_SPLASH_SCREEN_TYPE_CHOICES[i][1], 229 position); 230 } 231 } 232 233 237 private Composite createUISectionContainer(Composite parent) { 238 Composite client = fToolkit.createComposite(fSection); 239 client.setLayout(FormLayoutFactory.createSectionClientGridLayout(false, F_NUM_COLUMNS)); 240 client.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 241 return client; 242 } 243 244 247 private void configureUISection() { 248 fSection.setLayout(FormLayoutFactory.createClearGridLayout(false, 1)); 249 GridData data = new GridData(GridData.FILL_HORIZONTAL); 250 fSection.setLayoutData(data); 251 fSection.setText(PDEUIMessages.SplashProgressSection_progressName); 252 fSection.setDescription(PDEUIMessages.SplashProgressSection_progressSectionDesc); 253 } 254 255 private void createProgressBarConfig(Composite parent) { 256 fAddBarButton = createButton(parent, fToolkit, PDEUIMessages.SplashSection_progressBar); 257 fAddBarButton.addSelectionListener(new SelectionAdapter() { 258 public void widgetSelected(SelectionEvent e) { 259 boolean enable = fAddBarButton.getSelection(); 260 getSplashInfo().addProgressBar(enable, false); 261 updateFieldEnablement(); 262 } 263 }); 264 GridData data = new GridData(GridData.FILL_HORIZONTAL); 265 data.verticalIndent = 5; 266 data.horizontalSpan = F_NUM_COLUMNS; 267 fAddBarButton.setLayoutData(data); 268 269 Color foreground = fToolkit.getColors().getColor(IFormColors.TITLE); 270 271 fBarControls[0] = createLabel(parent, fToolkit, foreground, PDEUIMessages.SplashSection_progressX); 272 fBarControls[1] = fBarSpinners[0] = createSpinner(parent, fToolkit); 273 fBarControls[2] = createLabel(parent, fToolkit, foreground, PDEUIMessages.SplashSection_progressY); 274 fBarControls[3] = fBarSpinners[1] = createSpinner(parent, fToolkit); 275 fBarControls[4] = createLabel(parent, fToolkit, foreground, PDEUIMessages.SplashSection_progressWidth); 276 fBarControls[5] = fBarSpinners[2] = createSpinner(parent, fToolkit); 277 fBarControls[6] = createLabel(parent, fToolkit, foreground, PDEUIMessages.SplashSection_progressHeight); 278 fBarControls[7] = fBarSpinners[3] = createSpinner(parent, fToolkit); 279 addOffsetTooltips(fBarControls); 281 282 for (int i = 0; i < fBarSpinners.length; i++) { 283 fBarSpinners[i].addModifyListener(new ModifyListener() { 284 public void modifyText(ModifyEvent e) { 285 applySpinners(true); 286 } 287 }); 288 } 289 290 Composite filler = fToolkit.createComposite(parent); 291 filler.setLayout(new GridLayout()); 292 GridData gd = new GridData(); 293 gd.horizontalSpan = 2; 294 filler.setLayoutData(gd); 295 } 296 297 private void createProgressMessageConfig(Composite parent) { 298 fAddMessageButton = createButton(parent, fToolkit, PDEUIMessages.SplashSection_progressMessage); 299 fAddMessageButton.addSelectionListener(new SelectionAdapter() { 300 public void widgetSelected(SelectionEvent e) { 301 boolean enable = fAddMessageButton.getSelection(); 302 getSplashInfo().addProgressMessage(enable, false); 303 updateFieldEnablement(); 304 } 305 }); 306 307 Color foreground = fToolkit.getColors().getColor(IFormColors.TITLE); 308 309 fMessageControls[0] = createLabel(parent, fToolkit, foreground, PDEUIMessages.SplashSection_messageX); 310 fMessageControls[1] = fMessageSpinners[0] = createSpinner(parent, fToolkit); 311 fMessageControls[2] = createLabel(parent, fToolkit, foreground, PDEUIMessages.SplashSection_messageY); 312 fMessageControls[3] = fMessageSpinners[1] = createSpinner(parent, fToolkit); 313 314 fMessageControls[4] = createLabel(parent, fToolkit, foreground, PDEUIMessages.SplashSection_messageWidth); 315 fMessageControls[5] = fMessageSpinners[2] = createSpinner(parent, fToolkit); 316 fMessageControls[6] = createLabel(parent, fToolkit, foreground, PDEUIMessages.SplashSection_messageHeight); 317 fMessageControls[7] = fMessageSpinners[3] = createSpinner(parent, fToolkit); 318 319 fMessageControls[8] = createLabel(parent, fToolkit, foreground, PDEUIMessages.SplashSection_messageColor); 320 fColorSelector = new ColorSelector(parent); 321 fColorSelector.addListener(new IPropertyChangeListener() { 322 public void propertyChange(PropertyChangeEvent event) { 323 if (!event.getNewValue().equals(event.getOldValue())) 324 applyColor(); 325 } 326 }); 327 fToolkit.adapt(fColorSelector.getButton(), true, true); 328 fMessageControls[9] = fColorSelector.getButton(); 329 addOffsetTooltips(fMessageControls); 331 332 for (int i = 0; i < fMessageSpinners.length; i++) { 333 fMessageSpinners[i].addModifyListener(new ModifyListener() { 334 public void modifyText(ModifyEvent e) { 335 applySpinners(false); 336 } 337 }); 338 } 339 } 340 341 private void applyColor() { 342 if (fBlockNotification) 343 return; 344 RGB rgb = fColorSelector.getColorValue(); 345 getSplashInfo().setForegroundColor(rgbToHEX(rgb), false); 346 } 347 348 private void applySpinners(boolean bar) { 349 if (fBlockNotification) 350 return; 351 Spinner[] spinners = bar ? fBarSpinners : fMessageSpinners; 352 int[] geo = new int[] { 353 spinners[0].getSelection(), 354 spinners[1].getSelection(), 355 spinners[2].getSelection(), 356 spinners[3].getSelection() 357 }; 358 if (bar) 359 getSplashInfo().setProgressGeometry(geo, false); 360 else 361 getSplashInfo().setMessageGeometry(geo, false); 362 } 363 364 private Label createLabel(Composite parent, FormToolkit toolkit, Color color, String labelName) { 365 Label label = toolkit.createLabel(parent, labelName); 366 label.setForeground(color); 367 GridData gd = new GridData(); 368 gd.horizontalIndent = 10; 369 label.setLayoutData(gd); 370 return label; 371 } 372 373 private Button createButton(Composite parent, FormToolkit toolkit, String label) { 374 Button button = toolkit.createButton(parent, label, SWT.CHECK); 375 GridData gd = new GridData(); 376 gd.horizontalSpan = F_NUM_COLUMNS; 377 button.setLayoutData(gd); 378 return button; 379 } 380 381 private Spinner createSpinner(Composite parent, FormToolkit toolkit) { 382 Spinner spinner = new Spinner(parent, SWT.BORDER); 383 spinner.setMinimum(0); 384 spinner.setMaximum(9999); 385 toolkit.adapt(spinner, false, false); 386 return spinner; 387 } 388 389 392 private void resetProgressBarGeometry() { 393 fBarSpinners[0].setSelection(SplashInfo.F_DEFAULT_BAR_X_OFFSET); 395 fBarSpinners[1].setSelection(SplashInfo.F_DEFAULT_BAR_Y_OFFSET); 397 fBarSpinners[2].setSelection(SplashInfo.F_DEFAULT_BAR_WIDTH); 399 fBarSpinners[3].setSelection(SplashInfo.F_DEFAULT_BAR_HEIGHT); 401 } 402 403 406 private void resetProgressMessageGeometry() { 407 fMessageSpinners[0].setSelection(SplashInfo.F_DEFAULT_MESSAGE_X_OFFSET); 409 fMessageSpinners[1].setSelection(SplashInfo.F_DEFAULT_MESSAGE_Y_OFFSET); 411 fMessageSpinners[2].setSelection(SplashInfo.F_DEFAULT_MESSAGE_WIDTH); 413 fMessageSpinners[3].setSelection(SplashInfo.F_DEFAULT_MESSAGE_HEIGHT); 415 } 416 417 420 public void refresh() { 421 ISplashInfo info = getSplashInfo(); 422 fBlockNotification = true; 423 424 fColorSelector.setColorValue(hexToRGB(info.getForegroundColor())); 425 426 int[] pgeo = info.getProgressGeometry(); 427 boolean addProgress = pgeo != null; 428 info.addProgressBar(addProgress, fBlockNotification); 429 if (addProgress) { 430 for (int i = 0; i < pgeo.length; i++) { 431 fBarSpinners[i].setSelection(pgeo[i]); 432 } 433 } else { 434 resetProgressBarGeometry(); 435 } 436 437 fAddBarButton.setSelection(addProgress); 438 439 int[] mgeo = info.getMessageGeometry(); 440 boolean addMessage = mgeo != null; 441 info.addProgressMessage(addMessage, fBlockNotification); 442 if (addMessage) { 443 for (int i = 0; i < mgeo.length; i++) { 444 fMessageSpinners[i].setSelection(mgeo[i]); 445 } 446 } else { 447 resetProgressMessageGeometry(); 448 } 449 fColorSelector.setColorValue( 450 addMessage ? 451 hexToRGB(info.getForegroundColor()) : 452 new RGB(0,0,0)); 453 454 fAddMessageButton.setSelection(addMessage); 455 456 updateUIFieldTemplateCombo(); 458 fBlockNotification = false; 459 super.refresh(); 460 updateFieldEnablement(); 462 } 463 464 467 private void updateUIFieldTemplateCombo() { 468 updateFieldEnablement(); 470 ISplashInfo info = getSplashInfo(); 472 if (info.isDefinedSplashHandlerType() == false) { 473 fFieldTemplateCombo.setText(PDEUIMessages.SimpleCSCommandDetails_6); 475 return; 476 } 477 String splashHandlerType = info.getFieldSplashHandlerType(); 478 for (int i = 0; i < ISplashHandlerConstants.F_SPLASH_SCREEN_TYPE_CHOICES.length; i++) { 480 String key = ISplashHandlerConstants.F_SPLASH_SCREEN_TYPE_CHOICES[i][0]; 481 if (splashHandlerType.equals(key)) { 482 String displayName = ISplashHandlerConstants.F_SPLASH_SCREEN_TYPE_CHOICES[i][1]; 483 fFieldTemplateCombo.setText(displayName); 484 } 485 } 486 } 487 488 private ISplashInfo getSplashInfo() { 489 ISplashInfo info = getProduct().getSplashInfo(); 490 if (info == null) { 491 info = getModel().getFactory().createSplashInfo(); 492 getProduct().setSplashInfo(info); 493 } 494 return info; 495 } 496 497 private IProduct getProduct() { 498 return getModel().getProduct(); 499 } 500 501 private IProductModel getModel() { 502 return (IProductModel)getPage().getPDEEditor().getAggregateModel(); 503 } 504 505 public boolean canPaste(Clipboard clipboard) { 506 Display d = getSection().getDisplay(); 507 Control c = d.getFocusControl(); 508 if (c instanceof Text) 509 return true; 510 return false; 511 } 512 513 private String rgbToHEX(RGB rgb) { 514 if (rgb == null) 515 return null; 516 return rgbToHex(rgb.red) + rgbToHex(rgb.green) + rgbToHex(rgb.blue); 517 } 518 519 private String rgbToHex(int value) { 520 value = Math.max(0, value); 521 value = Math.min(value, 255); 522 String hex = Integer.toHexString(value).toUpperCase(); 523 if (hex.length() == 1) 524 hex = '0' + hex; 525 return hex; 526 } 527 528 private RGB hexToRGB(String hexValue) { 529 if (hexValue == null || hexValue.length() < 6) 530 return new RGB(0,0,0); 531 return new RGB( 532 Integer.parseInt(hexValue.substring(0,2),16), 533 Integer.parseInt(hexValue.substring(2,4),16), 534 Integer.parseInt(hexValue.substring(4,6),16) 535 ); 536 } 537 538 541 public void modelChanged(IModelChangedEvent e) { 542 if (e.getChangeType() == IModelChangedEvent.WORLD_CHANGED) { 544 handleModelEventWorldChanged(e); 545 } else if (e.getChangeType() == IModelChangedEvent.CHANGE) { 546 handleModelEventChange(e); 547 } 548 } 549 550 553 private void handleModelEventChange(IModelChangedEvent event) { 554 Object [] objects = event.getChangedObjects(); 556 IProductObject object = (IProductObject) objects[0]; 557 if (object == null) { 558 } else if ((object instanceof IProduct) && 560 (event.getChangedProperty() == IProduct.P_ID)) { 561 updateFieldEnablement(); 562 } 563 } 564 565 568 private void handleModelEventWorldChanged(IModelChangedEvent event) { 569 refresh(); 570 } 571 572 575 public void dispose() { 576 IProductModel model = getModel(); 577 if (model != null) { 578 model.removeModelChangedListener(this); 579 } 580 super.dispose(); 581 } 582 583 586 private void updateFieldEnablement() { 587 updateFieldProgressEnablement(); 589 updateFieldTemplateEnablement(); 591 } 592 593 596 private void updateFieldProgressEnablement() { 597 ISplashInfo info = getSplashInfo(); 599 if ((PDETextHelper.isDefined(getProduct().getId()) == false) || 604 ((info.isDefinedGeometry() == false) && 605 (info.isDefinedSplashHandlerType() == true))) { 606 fAddBarButton.setEnabled(false); 607 fAddMessageButton.setEnabled(false); 608 updateFieldProgressBarEnablement(false); 609 updateFieldProgressMessageEnablement(false); 610 } else { 611 fAddBarButton.setEnabled(true); 612 fAddMessageButton.setEnabled(true); 613 updateFieldProgressBarEnablement(true); 614 updateFieldProgressMessageEnablement(true); 615 } 616 } 617 618 621 private void updateFieldProgressBarEnablement(boolean buttonEnabled) { 622 boolean enable = (fAddBarButton.getSelection() && buttonEnabled); 623 for (int i = 0; i < fBarControls.length; i++) { 624 fBarControls[i].setEnabled(enable); 625 } 626 } 627 628 631 private void updateFieldProgressMessageEnablement(boolean buttonEnabled) { 632 boolean enable = (fAddMessageButton.getSelection() && buttonEnabled); 633 for (int i = 0; i < fMessageControls.length; i++) { 634 fMessageControls[i].setEnabled(enable); 635 } 636 } 637 638 641 private void updateFieldTemplateEnablement() { 642 ISplashInfo info = getSplashInfo(); 644 if ((PDETextHelper.isDefined(getProduct().getId()) == false) || 649 ((info.isDefinedGeometry() == true) && 650 (info.isDefinedSplashHandlerType() == false))) { 651 fFieldTemplateCombo.setEnabled(false); 652 } else { 653 fFieldTemplateCombo.setEnabled(true); 654 } 655 } 656 657 660 private void addOffsetTooltips(Control[] controls) { 661 int limit = 4; 663 if (controls.length < limit) { 665 return; 667 } 668 for (int i = 0; i < limit; i++) { 670 controls[i].setToolTipText(PDEUIMessages.SplashConfigurationSection_msgTooltipOffsetRelative); 671 } 672 } 673 674 } 675 | Popular Tags |