001: /*
002: * uDig - User Friendly Desktop Internet GIS client http://udig.refractions.net (C) 2004,
003: * Refractions Research Inc. This library is free software; you can redistribute it and/or modify it
004: * under the terms of the GNU Lesser General Public License as published by the Free Software
005: * Foundation; version 2.1 of the License. This library is distributed in the hope that it will be
006: * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
007: * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
008: */
009: package net.refractions.udig.project.ui.internal;
010:
011: import java.util.ArrayList;
012: import java.util.List;
013:
014: import org.eclipse.jface.resource.JFaceResources;
015: import org.eclipse.swt.SWT;
016: import org.eclipse.swt.graphics.Color;
017: import org.eclipse.swt.graphics.Font;
018: import org.eclipse.swt.graphics.GC;
019: import org.eclipse.swt.graphics.Image;
020: import org.eclipse.swt.graphics.Point;
021: import org.eclipse.swt.graphics.RGB;
022: import org.eclipse.swt.layout.GridData;
023: import org.eclipse.swt.layout.GridLayout;
024: import org.eclipse.swt.widgets.Button;
025: import org.eclipse.swt.widgets.Composite;
026: import org.eclipse.swt.widgets.Control;
027:
028: /**
029: * @author ptozer TODO To change the template for this generated type comment go to Window -
030: * Preferences - Java - Code Style - Code Templates
031: */
032: public class PaletteRadioPanel {
033: List<Color> colours;
034: List<Image> images;
035: Point fExtent;
036: Composite composite;
037:
038: public PaletteRadioPanel(Composite parent) {
039: // for ulterior motives
040: colours = new ArrayList<Color>();
041: images = new ArrayList<Image>();
042: composite = new Composite(parent, SWT.NONE);
043: fExtent = computeImageSize(composite);
044: }
045:
046: public Control getRadioButtons(AWTColor[] coloursToUse) {
047:
048: GridLayout gridLayout = new GridLayout();
049: gridLayout.numColumns = coloursToUse.length * 2;
050:
051: composite.setLayout(gridLayout);
052: // Display display = parent.getDisplay();
053:
054: for (int i = 0; i < coloursToUse.length; i++) {
055:
056: AWTColor awtColour = coloursToUse[i];
057: Color swtColour = new Color(composite.getDisplay(),
058: new RGB(awtColour.getColour().getRed(), awtColour
059: .getColour().getGreen(), awtColour
060: .getColour().getBlue()));
061: colours.add(swtColour);
062:
063: // Button b = new Button(composite, SWT.TOGGLE);
064: Button b = new Button(composite, SWT.FLAT);
065: Image image = new Image(composite.getDisplay(), fExtent.x,
066: fExtent.y);
067: images.add(image);
068:
069: GC gc = new GC(image);
070: gc.setBackground(swtColour);
071: gc.fillRectangle(0, 0, fExtent.x, fExtent.y);
072: gc.dispose();
073: // Button b = new Button(image);
074: // b.setStyle(STYLE_TOGGLE);
075: b.setImage(image);
076:
077: GridData data = new GridData();
078: b.setLayoutData(data);
079:
080: Button radio = new Button(composite, SWT.RADIO);
081: data = new GridData();
082: radio.setLayoutData(data);
083: }
084: // we end up with a line of flat boxes (buttons that don't work) with a radio button beside
085: // it
086:
087: return composite;
088: }
089:
090: public void dispose() {
091: // clean up the colours and images
092:
093: for (int i = 0; i < colours.size(); i++) {
094: colours.remove(i).dispose();
095: images.remove(i).dispose();
096: }
097:
098: }
099:
100: protected Point computeImageSize(Control window) {
101: GC gc = new GC(window);
102: Font f = JFaceResources.getFontRegistry().get(
103: JFaceResources.DEFAULT_FONT);
104: gc.setFont(f);
105: int height = gc.getFontMetrics().getHeight();
106: gc.dispose();
107: Point p = new Point(height * 3 - 6, height);
108: return p;
109: }
110: }
|