/** * Processes the save action. * * @param rgb The new RGB value. */ protected void processAction(RGB rgb) { String newComboText = predefinedColor; if (newComboText == null) newComboText = formatRGB(rgb); if (!combo.getText().equals(newComboText)) combo.setText(newComboText); if (oldRgb == null && rgb == null) return; if (rgb != null && rgb.equals(oldRgb)) return; oldRgb = rgb; if (rgb == null || !rgb.equals(colorSelector.getColorValue())) colorSelector.setColorValue(rgb); notifyListeners(SWT.Modify, null); }
public Point computeSize(int wHint, int hHint, boolean changed) { checkWidget(); int width = 0, height = 0; GC gc = new GC(combo); Point labelExtent = gc.textExtent("RGB(255,255,255)"); // $NON-NLS-1$ gc.dispose(); Point labelSize = combo.computeSize(labelExtent.x, labelExtent.y, changed); Point tableSize = colorSelector.getButton().computeSize(wHint, SWT.DEFAULT, changed); int borderWidth = getBorderWidth(); height = Math.max(hHint, Math.max(labelSize.y, tableSize.y) + 2 * borderWidth); width = Math.max(wHint, labelSize.x + tableSize.x + 2 * borderWidth); return new Point(width, height); }
public void setRGB(RGB rgb) { oldRgb = rgb; if (combo.isDisposed()) { return; } colorSelector.setColorValue(rgb); String newComboText = predefinedColor; if (predefinedColor == null) { if (rgb == null) { newComboText = NONE_CHOICE; } else { newComboText = formatRGB(rgb); } } if (!combo.getText().equals(newComboText)) { combo.setText(newComboText); } }
/** * The constructor. * * @param parent a widget which will be the parent of the new instance (cannot be null) * @param style the style of widget to construct */ public ColorControl(Composite parent, int style) { super(parent, style); setLayout(WidgetUtil.createSpaceGridLayout(2, 0)); ((GridLayout) getLayout()).horizontalSpacing = 2; colorSelector = new ColorSelector(this); { GridData data = new GridData(); data.widthHint = 50; data.grabExcessHorizontalSpace = false; colorSelector.getButton().setLayoutData(data); } colorSelector.addListener( new IPropertyChangeListener() { public void propertyChange(PropertyChangeEvent event) { predefinedColor = null; processAction(colorSelector.getColorValue()); } }); combo = new Combo(this, SWT.DROP_DOWN); { GridData data = new GridData(); data.horizontalAlignment = GridData.FILL; data.grabExcessHorizontalSpace = true; // data.widthHint = 80; combo.setLayoutData(data); } combo.add(NONE_CHOICE); combo.addFocusListener( new FocusListener() { public void focusGained(org.eclipse.swt.events.FocusEvent e) {} public void focusLost(org.eclipse.swt.events.FocusEvent e) { combo.notifyListeners(SWT.DefaultSelection, null); } }); combo.addSelectionListener( new SelectionListener() { public void widgetSelected(SelectionEvent e) { predefinedColor = combo.getText(); if (NONE_CHOICE.equals(predefinedColor)) { predefinedColor = null; processAction(null); return; } String colorName = predefinedColor; if (choiceSet != null) { if (choiceSet.findChoiceByDisplayName(colorName) != null) colorName = choiceSet.findChoiceByDisplayName(colorName).getName(); } int[] intRgb = ColorUtil.getRGBs(colorName); RGB rgb = null; if (intRgb != null) { rgb = new RGB(intRgb[0], intRgb[1], intRgb[2]); } processAction(rgb); } public void widgetDefaultSelected(SelectionEvent e) { predefinedColor = null; String string = combo.getText(); int index = -1; String[] items = combo.getItems(); for (int i = 0; i < items.length; i++) { if (items[i].equalsIgnoreCase(string)) { index = i; combo.setText(items[i]); break; } } if (index != -1) { widgetSelected(null); return; } RGB rgb = parseString(string); if (rgb == null) { combo.deselectAll(); notifyListeners(SWT.Modify, null); } else processAction(rgb); } }); initAccessible(); }
/* * (non-Javadoc) * * @see org.eclipse.swt.widgets.Control#setEnabled(boolean) */ public void setEnabled(boolean enabled) { combo.setEnabled(enabled); colorSelector.setEnabled(enabled); super.setEnabled(enabled); }