1 11 package org.eclipse.ui.internal.editors.text; 12 13 import org.eclipse.swt.SWT; 14 import org.eclipse.swt.events.DisposeEvent; 15 import org.eclipse.swt.events.DisposeListener; 16 import org.eclipse.swt.events.SelectionAdapter; 17 import org.eclipse.swt.events.SelectionEvent; 18 import org.eclipse.swt.graphics.Color; 19 import org.eclipse.swt.graphics.Font; 20 import org.eclipse.swt.graphics.GC; 21 import org.eclipse.swt.graphics.Image; 22 import org.eclipse.swt.graphics.Point; 23 import org.eclipse.swt.graphics.RGB; 24 import org.eclipse.swt.widgets.Button; 25 import org.eclipse.swt.widgets.ColorDialog; 26 import org.eclipse.swt.widgets.Composite; 27 import org.eclipse.swt.widgets.Control; 28 import org.eclipse.swt.widgets.Display; 29 30 import org.eclipse.jface.resource.JFaceResources; 31 32 37 class ColorEditor { 38 39 40 private Point fExtent; 41 42 private Image fImage; 43 44 private RGB fColorValue; 45 46 private Color fColor; 47 48 private Button fButton; 49 50 55 public ColorEditor(Composite parent) { 56 57 fButton= new Button(parent, SWT.PUSH); 58 fExtent= computeImageSize(parent); 59 fImage= new Image(parent.getDisplay(), fExtent.x, fExtent.y); 60 61 GC gc= new GC(fImage); 62 gc.setBackground(fButton.getBackground()); 63 gc.fillRectangle(0, 0, fExtent.x, fExtent.y); 64 gc.dispose(); 65 66 fButton.setImage(fImage); 67 fButton.addSelectionListener(new SelectionAdapter() { 68 public void widgetSelected(SelectionEvent event) { 69 ColorDialog colorDialog= new ColorDialog(fButton.getShell()); 70 colorDialog.setRGB(fColorValue); 71 RGB newColor = colorDialog.open(); 72 if (newColor != null) { 73 fColorValue= newColor; 74 updateColorImage(); 75 } 76 } 77 }); 78 79 fButton.addDisposeListener(new DisposeListener() { 80 public void widgetDisposed(DisposeEvent event) { 81 if (fImage != null) { 82 fImage.dispose(); 83 fImage= null; 84 } 85 if (fColor != null) { 86 fColor.dispose(); 87 fColor= null; 88 } 89 } 90 }); 91 } 92 93 98 public RGB getColorValue() { 99 return fColorValue; 100 } 101 102 107 public void setColorValue(RGB rgb) { 108 fColorValue= rgb; 109 updateColorImage(); 110 } 111 112 117 public Button getButton() { 118 return fButton; 119 } 120 121 124 protected void updateColorImage() { 125 126 Display display= fButton.getDisplay(); 127 128 GC gc= new GC(fImage); 129 gc.setForeground(display.getSystemColor(SWT.COLOR_BLACK)); 130 gc.drawRectangle(0, 2, fExtent.x - 1, fExtent.y - 4); 131 132 if (fColor != null) 133 fColor.dispose(); 134 135 fColor= new Color(display, fColorValue); 136 gc.setBackground(fColor); 137 gc.fillRectangle(1, 3, fExtent.x - 2, fExtent.y - 5); 138 gc.dispose(); 139 140 fButton.setImage(fImage); 141 } 142 143 144 150 protected Point computeImageSize(Control window) { 151 GC gc= new GC(window); 152 Font f= JFaceResources.getFontRegistry().get(JFaceResources.DEFAULT_FONT); 153 gc.setFont(f); 154 int height= gc.getFontMetrics().getHeight(); 155 gc.dispose(); 156 Point p= new Point(height * 3 - 6, height); 157 return p; 158 } 159 } 160 | Popular Tags |