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.awt.Color;
012: import java.util.HashMap;
013:
014: import net.refractions.udig.project.internal.Layer;
015: import net.refractions.udig.ui.palette.ColourScheme;
016:
017: import org.eclipse.jface.resource.JFaceResources;
018: import org.eclipse.swt.SWT;
019: import org.eclipse.swt.events.SelectionEvent;
020: import org.eclipse.swt.events.SelectionListener;
021: import org.eclipse.swt.graphics.Font;
022: import org.eclipse.swt.graphics.GC;
023: import org.eclipse.swt.graphics.Image;
024: import org.eclipse.swt.graphics.Point;
025: import org.eclipse.swt.graphics.RGB;
026: import org.eclipse.swt.layout.GridData;
027: import org.eclipse.swt.widgets.Button;
028: import org.eclipse.swt.widgets.Combo;
029: import org.eclipse.swt.widgets.Composite;
030: import org.eclipse.swt.widgets.Control;
031: import org.eclipse.swt.widgets.Label;
032:
033: /**
034: * @author ptozer TODO To change the template for this generated type comment go to Window -
035: * Preferences - Java - Code Style - Code Templates
036: * @author chorner
037: */
038: public class PaletteCombo {
039: Point fExtent = null;
040: Composite composite = null;
041: Combo colourLetterCombo = null;
042: Button colourIndicatorButton = null;
043: Image image = null;
044: Button checkbox = null;
045: Layer layerReference = null;
046: org.eclipse.swt.graphics.Color swtColour = null;
047:
048: public PaletteCombo(Composite parent) {
049: composite = parent; //new Composite(parent, SWT.NONE);
050: fExtent = computeImageSize(composite);
051: image = new Image(parent.getDisplay(), fExtent.x, fExtent.y);
052: }
053:
054: /**
055: * @param colourToUse
056: * @param layerNumber
057: * @param colourLetters
058: * @return
059: */
060: public Control getPaletteCombo(Layer layer) {
061: layerReference = layer;
062: String layerName = layer.getName();
063: ColourScheme layerScheme = layer.getColourScheme();
064: ColourScheme mapScheme = layer.getMapInternal()
065: .getColourScheme();
066:
067: ColourScheme currentScheme;
068: if (layerScheme != null && !(layerScheme.equals(mapScheme))) { //TODO: check logic
069: currentScheme = layerScheme;
070: } else {
071: currentScheme = mapScheme;
072: }
073: String[] colourLetters = getColourLetters(currentScheme);
074:
075: int currentColourIndex = layer.getMapInternal().getMapLayers()
076: .indexOf(layer);
077: layerReference = layer;
078:
079: // GridLayout gridLayout = new GridLayout();
080: // gridLayout.numColumns = 4;
081: //
082: // composite.setLayout(gridLayout);
083:
084: Label layerLabel = new Label(composite, SWT.NONE);
085: layerLabel.setText(layerName + ":"); //$NON-NLS-1$
086:
087: GridData data = new GridData();
088: data.horizontalSpan = 1;
089: layerLabel.setLayoutData(data);
090:
091: colourIndicatorButton = new Button(composite, SWT.FLAT
092: | SWT.TRAIL);
093: updateButtonColourDisplay(currentColourIndex);
094:
095: data = new GridData();
096: colourIndicatorButton.setLayoutData(data);
097:
098: colourLetterCombo = new Combo(composite, SWT.DROP_DOWN
099: | SWT.READ_ONLY);
100: colourLetterCombo.setItems(colourLetters);
101: colourLetterCombo.select(currentColourIndex);
102: colourLetterCombo.addSelectionListener(new SelectionListener() {
103: public void widgetSelected(SelectionEvent e) {
104: int selectIndex = colourLetterCombo.getSelectionIndex();
105: updateButtonColourDisplay(selectIndex);
106: }
107:
108: public void widgetDefaultSelected(SelectionEvent e) {
109: }
110: });
111:
112: data = new GridData();
113: colourLetterCombo.setLayoutData(data);
114:
115: checkbox = new Button(composite, SWT.CHECK);
116: checkbox.setSelection(true);
117:
118: data = new GridData();
119: checkbox.setLayoutData(data);
120:
121: // composite.layout(true);
122: return composite;
123: }
124:
125: /**
126: *
127: */
128: public void dispose() {
129: // clean up the colours and images
130: if (composite != null) {
131: Control[] kids = composite.getChildren();
132: for (int i = 0; i < kids.length; i++) {
133: Control c = kids[i];
134: if (c instanceof Button) {
135: Image img = ((Button) c).getImage();
136: if (img != null) {
137: img.dispose();
138: ((Button) c).getImage().dispose();
139: }
140: }
141: c.dispose();
142: }
143: }
144:
145: fExtent = null;
146: composite = null;
147: colourLetterCombo = null;
148: colourIndicatorButton = null;
149: image = null;
150: checkbox = null;
151: layerReference = null;
152: }
153:
154: public void updateContents(ColourScheme scheme) {
155: int index = colourLetterCombo.getSelectionIndex();
156: String[] colourLetters = getColourLetters(scheme);
157: colourLetterCombo.setItems(colourLetters);
158: if (index == -1) {
159: index = colourLetters.length - 1;
160: }
161: colourLetterCombo.select(index);
162: updateButtonColourDisplay(index);
163: }
164:
165: /**
166: * @param index
167: */
168: protected void updateButtonColourDisplay(int index) {
169: GC gc = new GC(image);
170: gc.drawRectangle(0, 2, fExtent.x, fExtent.y);
171:
172: if (swtColour != null)
173: swtColour.dispose();
174:
175: Color clr = layerReference.getMapInternal().getColourScheme()
176: .getColour(index);
177: swtColour = new org.eclipse.swt.graphics.Color(composite
178: .getDisplay(), new RGB(clr.getRed(), clr.getGreen(),
179: clr.getBlue()));
180: gc.setBackground(swtColour);
181: gc.fillRectangle(0, 2, fExtent.x, fExtent.y);
182: gc.dispose();
183: colourIndicatorButton.setImage(image);
184: }
185:
186: /**
187: * @param window
188: * @return
189: */
190: protected Point computeImageSize(Control window) {
191: GC gc = new GC(window);
192: Font f = JFaceResources.getFontRegistry().get(
193: JFaceResources.DEFAULT_FONT);
194: gc.setFont(f);
195: int height = gc.getFontMetrics().getHeight();
196: gc.dispose();
197: Point p = new Point(height * 3 - 6, height);
198: return p;
199: }
200:
201: /**
202: * @return Returns the colourLetterCombo.
203: */
204: public Combo getColourLetterCombo() {
205: return colourLetterCombo;
206: }
207:
208: /**
209: * @return Returns the checkbox.
210: */
211: public Button getCheckbox() {
212: return checkbox;
213: }
214:
215: private String[] getColourLetters(ColourScheme scheme) {
216: int size = scheme.getSizePalette();
217: String[] allColourLetters = new String[] {
218: "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" }; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$ //$NON-NLS-9$ //$NON-NLS-10$ //$NON-NLS-11$ //$NON-NLS-12$ //$NON-NLS-13$ //$NON-NLS-14$ //$NON-NLS-15$ //$NON-NLS-16$ //$NON-NLS-17$ //$NON-NLS-18$ //$NON-NLS-19$ //$NON-NLS-20$ //$NON-NLS-21$ //$NON-NLS-22$ //$NON-NLS-23$ //$NON-NLS-24$ //$NON-NLS-25$ //$NON-NLS-26$
219: String[] colourLetters;
220: int[] colourIndex = scheme.getColourPalette().getColorScheme()
221: .getSampleScheme(size);
222: colourLetters = new String[size];
223: if (scheme.getSizeScheme() < size) {
224: scheme.setSizeScheme(size);
225: }
226: HashMap<Integer, Integer> colourMap = scheme.getColourMap();
227: for (int i = 0; i < size; i++) {
228: int schemeIndex = colourMap.get(i);
229: int actualIndex = colourIndex[schemeIndex];
230: colourLetters[i] = allColourLetters[actualIndex];
231: }
232: return colourLetters;
233: }
234: }
|