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); }
/** * 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(); }