001: package net.refractions.udig.project.ui.preferences;
002:
003: import net.refractions.udig.ui.PlatformGIS;
004: import net.refractions.udig.ui.graphics.Glyph;
005:
006: import org.eclipse.jface.preference.FieldEditor;
007: import org.eclipse.jface.viewers.ColumnWeightData;
008: import org.eclipse.jface.viewers.ISelection;
009: import org.eclipse.jface.viewers.IStructuredContentProvider;
010: import org.eclipse.jface.viewers.LabelProvider;
011: import org.eclipse.jface.viewers.StructuredSelection;
012: import org.eclipse.jface.viewers.TableLayout;
013: import org.eclipse.jface.viewers.TableViewer;
014: import org.eclipse.jface.viewers.Viewer;
015: import org.eclipse.jface.viewers.ViewerSorter;
016: import org.eclipse.swt.SWT;
017: import org.eclipse.swt.graphics.Image;
018: import org.eclipse.swt.layout.GridData;
019: import org.eclipse.swt.widgets.Composite;
020: import org.eclipse.swt.widgets.Control;
021: import org.eclipse.swt.widgets.Table;
022: import org.eclipse.swt.widgets.TableColumn;
023: import org.geotools.brewer.color.BrewerPalette;
024: import org.geotools.brewer.color.ColorBrewer;
025:
026: public class PaletteSelectionFieldEditor extends FieldEditor {
027:
028: TableViewer palettes;
029:
030: public PaletteSelectionFieldEditor(String name, String string,
031: Composite parent) {
032: super (name, string, parent);
033: }
034:
035: @Override
036: protected void adjustForNumColumns(int numColumns) {
037: Control control = getLabelControl();
038: ((GridData) control.getLayoutData()).horizontalSpan = numColumns;
039: ((GridData) palettes.getControl().getLayoutData()).horizontalSpan = numColumns;
040: }
041:
042: @Override
043: protected void doFillIntoGrid(Composite parent, int numColumns) {
044: Control control = getLabelControl(parent);
045: GridData gd = new GridData();
046: gd.horizontalSpan = numColumns;
047: control.setLayoutData(gd);
048:
049: palettes = getTableControl(parent);
050: gd = new GridData(GridData.FILL_HORIZONTAL);
051: gd.verticalAlignment = GridData.FILL;
052: gd.horizontalSpan = numColumns;
053: gd.grabExcessHorizontalSpace = true;
054: gd.heightHint = 95;
055: palettes.getControl().setLayoutData(gd);
056:
057: // buttonBox = getButtonBoxControl(parent);
058: // gd = new GridData();
059: // gd.verticalAlignment = GridData.BEGINNING;
060: // buttonBox.setLayoutData(gd);
061: }
062:
063: private TableViewer getTableControl(Composite parent) {
064: TableViewer paletteTable = new TableViewer(new Table(parent,
065: SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL
066: | SWT.FULL_SELECTION));
067: TableLayout tableLayout = new TableLayout();
068: tableLayout.addColumnData(new ColumnWeightData(1, 20, false));
069: TableColumn firstColumn = new TableColumn(paletteTable
070: .getTable(), SWT.LEFT);
071: firstColumn.setAlignment(SWT.LEFT);
072: paletteTable.getTable().setLayout(tableLayout);
073: // gridData = new GridData(SWT.FILL, SWT.FILL, true, true);
074: // gridData.horizontalSpan = 2;
075: // gridData.heightHint = 150;
076: // gridData.widthHint = 175;
077:
078: paletteTable.setLabelProvider(new LabelProvider() {
079: public Image getImage(Object element) {
080: if (element instanceof BrewerPalette) {
081: BrewerPalette palette = (BrewerPalette) element;
082: return Glyph.palette(
083: palette.getColors(palette.getMaxColors()))
084: .createImage();
085: }
086: return null;
087: }
088:
089: public String getText(Object element) {
090: if (element instanceof BrewerPalette) {
091: BrewerPalette palette = (BrewerPalette) element;
092: String text = null;
093: text = palette.getName()
094: + ": " + palette.getDescription(); //$NON-NLS-1$
095: if (text == null)
096: text = palette.getName();
097: return text;
098: }
099: return null;
100: }
101: });
102:
103: paletteTable
104: .setContentProvider(new IStructuredContentProvider() {
105:
106: public Object[] getElements(Object inputElement) {
107: if (inputElement instanceof ColorBrewer) {
108: ColorBrewer brewer = (ColorBrewer) inputElement;
109: return brewer.getPalettes();
110: } else {
111: return new Object[0];
112: }
113: }
114:
115: public void dispose() {
116: }
117:
118: public void inputChanged(Viewer viewer,
119: Object oldInput, Object newInput) {
120: }
121: });
122:
123: paletteTable.setSorter(new ViewerSorter() {
124:
125: @Override
126: public int compare(Viewer viewer, Object e1, Object e2) {
127: if (e1 instanceof BrewerPalette
128: && e2 instanceof BrewerPalette) {
129: BrewerPalette p1 = (BrewerPalette) e1;
130: BrewerPalette p2 = (BrewerPalette) e2;
131: //alphabetical by name
132: return p1.getName().compareTo(p2.getName());
133: } else
134: return super .compare(viewer, e1, e2);
135: }
136:
137: });
138:
139: paletteTable.setInput(PlatformGIS.getColorBrewer());
140: return paletteTable;
141: }
142:
143: @Override
144: protected void doLoad() {
145: if (palettes != null) {
146: select(getPreferenceStore().getString(getPreferenceName()));
147: }
148: }
149:
150: @Override
151: protected void doLoadDefault() {
152: if (palettes != null) {
153: select(getPreferenceStore().getDefaultString(
154: getPreferenceName()));
155: }
156: }
157:
158: private void select(String paletteName) {
159: ColorBrewer brewer = PlatformGIS.getColorBrewer();
160: if (paletteName == null || !brewer.hasPalette(paletteName))
161: return;
162: BrewerPalette palette = brewer.getPalette(paletteName);
163: palettes.setSelection(new StructuredSelection(palette));
164: }
165:
166: @Override
167: protected void doStore() {
168: ISelection select = palettes.getSelection();
169: if (select == null) {
170: getPreferenceStore().setToDefault(getPreferenceName());
171: return;
172: }
173: BrewerPalette palette = (BrewerPalette) ((StructuredSelection) select)
174: .getFirstElement();
175: getPreferenceStore().setValue(getPreferenceName(),
176: palette.getName());
177: }
178:
179: @Override
180: public int getNumberOfControls() {
181: return 2;
182: }
183:
184: }
|