gvsig-raster / org.gvsig.raster / branches / org.gvsig.raster.2.4 / org.gvsig.raster / org.gvsig.raster.app / org.gvsig.raster.app.mainplugin / src / main / java / org / gvsig / raster / app / mainplugin / colortable / ColorTablePagePanelController.java @ 6900
History | View | Annotate | Download (17.8 KB)
1 |
/* gvSIG. Desktop Geographic Information System.
|
---|---|
2 |
*
|
3 |
* Copyright ? 2007-2017 gvSIG Association
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* as published by the Free Software Foundation; either version 2
|
8 |
* of the License, or (at your option) any later version.
|
9 |
*
|
10 |
* This program is distributed in the hope that it will be useful,
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
* GNU General Public License for more details.
|
14 |
*
|
15 |
* You should have received a copy of the GNU General Public License
|
16 |
* along with this program; if not, write to the Free Software
|
17 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
18 |
* MA 02110-1301, USA.
|
19 |
*
|
20 |
* For any additional information, do not hesitate to contact us
|
21 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
22 |
*/
|
23 |
package org.gvsig.raster.app.mainplugin.colortable; |
24 |
|
25 |
import java.awt.Component; |
26 |
import java.awt.Dimension; |
27 |
import java.awt.Rectangle; |
28 |
import java.awt.event.ItemEvent; |
29 |
import java.awt.event.ItemListener; |
30 |
import java.io.File; |
31 |
import java.util.List; |
32 |
import java.util.Locale; |
33 |
|
34 |
import javax.swing.DefaultComboBoxModel; |
35 |
import javax.swing.JComponent; |
36 |
import javax.swing.JLabel; |
37 |
import javax.swing.JList; |
38 |
import javax.swing.JPanel; |
39 |
import javax.swing.JSplitPane; |
40 |
import javax.swing.ListCellRenderer; |
41 |
import javax.swing.SwingUtilities; |
42 |
import javax.swing.event.ChangeEvent; |
43 |
import javax.swing.event.ChangeListener; |
44 |
import javax.swing.event.ListSelectionEvent; |
45 |
import javax.swing.event.ListSelectionListener; |
46 |
import javax.swing.event.TableModelEvent; |
47 |
import javax.swing.event.TableModelListener; |
48 |
|
49 |
import org.apache.commons.lang3.tuple.Pair; |
50 |
import org.slf4j.Logger; |
51 |
import org.slf4j.LoggerFactory; |
52 |
|
53 |
import org.gvsig.andami.PluginServices; |
54 |
import org.gvsig.andami.PluginsLocator; |
55 |
import org.gvsig.app.project.documents.view.toolListeners.PanListener; |
56 |
import org.gvsig.fmap.dal.exception.ReadException; |
57 |
import org.gvsig.fmap.dal.raster.api.BandDescriptor; |
58 |
import org.gvsig.fmap.dal.raster.api.RasterStore; |
59 |
import org.gvsig.fmap.mapcontext.MapContext; |
60 |
import org.gvsig.fmap.mapcontext.MapContextLocator; |
61 |
import org.gvsig.fmap.mapcontext.layers.FLayer; |
62 |
import org.gvsig.fmap.mapcontext.raster.api.RasterLayer; |
63 |
import org.gvsig.fmap.mapcontext.raster.swing.MapContextRasterSwingLocator; |
64 |
import org.gvsig.fmap.mapcontext.raster.swing.previewer.OneLayerPreviewer; |
65 |
import org.gvsig.fmap.mapcontrol.MapControl; |
66 |
import org.gvsig.fmap.mapcontrol.MapControlCreationException; |
67 |
import org.gvsig.fmap.mapcontrol.MapControlLocator; |
68 |
import org.gvsig.fmap.mapcontrol.tools.Behavior.Behavior; |
69 |
import org.gvsig.fmap.mapcontrol.tools.Behavior.MoveBehavior; |
70 |
import org.gvsig.raster.lib.buffer.api.statistics.Statistics; |
71 |
import org.gvsig.raster.lib.legend.api.RasterLegend; |
72 |
import org.gvsig.raster.lib.legend.api.RasterLegendLocator; |
73 |
import org.gvsig.raster.lib.legend.api.colorinterpretation.ColorInterpretation; |
74 |
import org.gvsig.raster.lib.legend.api.colortable.ColorTable; |
75 |
import org.gvsig.raster.lib.legend.api.colortable.colortableclass.ColorTableClass; |
76 |
import org.gvsig.raster.swing.legend.RasterSwingLegendLocator; |
77 |
import org.gvsig.raster.swing.legend.colortable.editor.ColorTableEditorPanel; |
78 |
import org.gvsig.raster.swing.legend.colortable.selector.ColorTableSelectorPanel; |
79 |
import org.gvsig.tools.ToolsLocator; |
80 |
import org.gvsig.tools.dynobject.DynObject; |
81 |
import org.gvsig.tools.i18n.I18nManager; |
82 |
import org.gvsig.tools.locator.LocatorException; |
83 |
|
84 |
|
85 |
/**
|
86 |
* @author fdiaz
|
87 |
*
|
88 |
*/
|
89 |
public class ColorTablePagePanelController extends ColorTablePagePanelView implements ColorTablePagePanel { |
90 |
|
91 |
/**
|
92 |
*
|
93 |
*/
|
94 |
private static final long serialVersionUID = 798930218545360652L; |
95 |
private static final Logger LOG = LoggerFactory.getLogger(ColorTablePagePanelController.class); |
96 |
|
97 |
private ColorTableSelectorPanel selector;
|
98 |
private ColorTableEditorPanel editor;
|
99 |
private ColorTable colorTable;
|
100 |
private JSplitPane splPanelRight = new JSplitPane(JSplitPane.VERTICAL_SPLIT); |
101 |
private RasterLayer rasterLayer;
|
102 |
// private MapControl previewerMapControl;
|
103 |
private OneLayerPreviewer previewer;
|
104 |
private boolean initialized; |
105 |
private TableModelListener editorTableModelListener; |
106 |
private RasterLayer clonedLayerForPreview;
|
107 |
|
108 |
|
109 |
/**
|
110 |
*
|
111 |
*/
|
112 |
public ColorTablePagePanelController() {
|
113 |
this.initialized = false; |
114 |
translate(); |
115 |
initializeComponents(); |
116 |
} |
117 |
|
118 |
private void initializeComponents() { |
119 |
selector = RasterSwingLegendLocator.getSwingManager().createColorTableSelectorPanel(); |
120 |
PluginServices rasterPlugin = PluginsLocator.getManager().getPlugin("org.gvsig.raster.app.mainplugin");
|
121 |
DynObject properties = rasterPlugin.getPluginProperties(); |
122 |
File defaultColorTableLibraryPath = (File) properties.getDynValue("defaultColorTableLibraryPath"); |
123 |
List<Pair<File, ColorTable>> colorTables = RasterLegendLocator.getRasterLegendManager().getColorTables(defaultColorTableLibraryPath); |
124 |
selector.set(colorTables); |
125 |
selector.setEditable(false);
|
126 |
|
127 |
splPanelRight.setTopComponent(getPreviewer().asJComponent()); |
128 |
splPanelRight.setBottomComponent(selector.asJComponent()); |
129 |
|
130 |
splPanel.setRightComponent(splPanelRight); |
131 |
|
132 |
editor = RasterSwingLegendLocator.getSwingManager().createColorTableEditorPanel(); |
133 |
splPanel.setLeftComponent(editor.asJComponent()); |
134 |
|
135 |
selector.addListSelectionListener(new ListSelectionListener() { |
136 |
|
137 |
@Override
|
138 |
public void valueChanged(ListSelectionEvent e) { |
139 |
Thread thread = new Thread(new Runnable() { |
140 |
@Override
|
141 |
public void run() { |
142 |
SwingUtilities.invokeLater(new Runnable() { |
143 |
@Override
|
144 |
public void run() { |
145 |
colorTable = selector.getSelected(); |
146 |
if (((BandItem) cmbBand.getSelectedItem()).getIndex() > -1) { |
147 |
// ColorTable adaptedColorTable = adaptToTheBand(colorTable);
|
148 |
colorTable = adaptToTheBand(colorTable); |
149 |
setEditorColorTable(); |
150 |
updatePreviewer(); |
151 |
} |
152 |
} |
153 |
}); |
154 |
} |
155 |
}, "Selector changed");
|
156 |
|
157 |
thread.setDaemon(false);
|
158 |
thread.start(); |
159 |
} |
160 |
}); |
161 |
splPanel.setDividerLocation(0.75);
|
162 |
|
163 |
cmbBand.setRenderer(new ComboBandsCellRenderer());
|
164 |
cmbBand.addItemListener(new ItemListener() { |
165 |
|
166 |
@Override
|
167 |
public void itemStateChanged(ItemEvent e) { |
168 |
if(e.getStateChange()==ItemEvent.SELECTED){ |
169 |
Thread thread = new Thread(new Runnable() { |
170 |
@Override
|
171 |
public void run() { |
172 |
SwingUtilities.invokeLater(new Runnable() { |
173 |
@Override
|
174 |
public void run() { |
175 |
updatePreviewer(); |
176 |
} |
177 |
}); |
178 |
} |
179 |
}, "Palette band changed");
|
180 |
|
181 |
thread.setDaemon(false);
|
182 |
thread.start(); |
183 |
} |
184 |
} |
185 |
}); |
186 |
|
187 |
// editor.addInterpolatedItemListener(new ItemListener() {
|
188 |
//
|
189 |
// @Override
|
190 |
// public void itemStateChanged(ItemEvent e) {
|
191 |
// updatePreviewer();
|
192 |
// }
|
193 |
// });
|
194 |
} |
195 |
|
196 |
private OneLayerPreviewer getPreviewer() {
|
197 |
if(previewer == null){ |
198 |
previewer = MapContextRasterSwingLocator.getSwingManager().createOneLayerPreviewer(); |
199 |
} |
200 |
return this.previewer; |
201 |
|
202 |
// if(this.previewerMapControl == null) {
|
203 |
// try {
|
204 |
// MapContext mapContext = MapContextLocator.getMapContextManager().createMapContext();
|
205 |
// this.previewerMapControl = MapControlLocator.getMapControlManager().createJMapControlPanel(mapContext);
|
206 |
// } catch (MapControlCreationException | LocatorException e) {
|
207 |
// LOG.warn("Can't create mapControl to preview.", e);
|
208 |
// }
|
209 |
// this.previewerMapControl.setPreferredSize(new Dimension(300, 200));
|
210 |
//
|
211 |
// }
|
212 |
// return this.previewerMapControl;
|
213 |
} |
214 |
|
215 |
private void initializePreviewer(){ |
216 |
|
217 |
// MapControl mapControl = getPreviewer();
|
218 |
// MapContext mapContext = mapControl.getMapContext();
|
219 |
// mapContext.setProjection(this.rasterLayer.getProjection());
|
220 |
// mapContext.getLayers().add(this.clonedLayerForPreview);
|
221 |
// try {
|
222 |
// mapControl.getViewPort().setEnvelope(this.rasterLayer.getFullEnvelope());
|
223 |
// } catch (ReadException e) {
|
224 |
// LOG.warn("Can't get the full envelope of the layer.", e);
|
225 |
// }
|
226 |
//
|
227 |
getPreviewer().setLayer(this.clonedLayerForPreview);
|
228 |
|
229 |
|
230 |
|
231 |
splPanelRight.setTopComponent(previewer.asJComponent()); |
232 |
// splPanelRight.getTopComponent().setSize(previewer.asJComponent().getPreferredSize());
|
233 |
|
234 |
// pan
|
235 |
// PanListener panListener = new PanListener(mapControl);
|
236 |
// mapControl.addBehavior("pan", new MoveBehavior(panListener,Behavior.BUTTON_LEFT));
|
237 |
// mapControl.setTool("pan");
|
238 |
|
239 |
} |
240 |
|
241 |
private void updatePreviewer(){ |
242 |
if (this.initialized) { |
243 |
// MapControl mapControl = getPreviewer();
|
244 |
ColorInterpretation colorInterpretation = this.clonedLayerForPreview.getLegend().getColorInterpretation();
|
245 |
updateColorInterpretation(colorInterpretation); |
246 |
// MapContext mapContext = mapControl.getMapContext();
|
247 |
// mapContext.invalidate();
|
248 |
// mapControl.paintImmediately(new Rectangle(mapControl.getSize()));
|
249 |
} |
250 |
} |
251 |
|
252 |
protected ColorTable adaptToTheBand(ColorTable colorTable) {
|
253 |
ColorTable cloned = RasterLegendLocator.getRasterLegendManager().createColorTable(); |
254 |
cloned.copyFrom(colorTable); |
255 |
|
256 |
List<ColorTableClass> classes = cloned.getClasses();
|
257 |
double minClassValue = Double.POSITIVE_INFINITY; |
258 |
double maxClassValue = Double.NEGATIVE_INFINITY; |
259 |
for (ColorTableClass colorTableClass : classes) {
|
260 |
double value = colorTableClass.getValue();
|
261 |
if(value < minClassValue){
|
262 |
minClassValue = value; |
263 |
} |
264 |
if(value > maxClassValue){
|
265 |
maxClassValue = value; |
266 |
} |
267 |
} |
268 |
|
269 |
Statistics statistics = this.rasterLayer.getStatistics(null); |
270 |
int bandIndex = ((BandItem)cmbBand.getSelectedItem()).getIndex();
|
271 |
double minBandValue = statistics.getMin()[bandIndex];
|
272 |
double maxBandValue = statistics.getMax()[bandIndex];
|
273 |
|
274 |
double ratio = (maxBandValue-minBandValue)/(maxClassValue-minClassValue);
|
275 |
|
276 |
for (ColorTableClass colorTableClass : classes) {
|
277 |
double value = colorTableClass.getValue();
|
278 |
double adaptedValue = ((value-minClassValue)*ratio)+minBandValue;
|
279 |
colorTableClass.setValue(adaptedValue); |
280 |
} |
281 |
|
282 |
return cloned;
|
283 |
} |
284 |
|
285 |
private void translate() { |
286 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
287 |
lblBand.setText(i18nManager.getTranslation(lblBand.getText())); |
288 |
lblBand.setToolTipText(i18nManager.getTranslation(lblBand.getToolTipText())); |
289 |
cmbBand.setToolTipText(i18nManager.getTranslation(cmbBand.getToolTipText())); |
290 |
} |
291 |
|
292 |
/**
|
293 |
* @param locale
|
294 |
*
|
295 |
*/
|
296 |
public void setLocate(Locale locale) { |
297 |
Locale l = super.getLocale(); |
298 |
if (!l.equals(locale)) {
|
299 |
translate(); |
300 |
} |
301 |
super.setLocale(locale);
|
302 |
} |
303 |
|
304 |
|
305 |
/* (non-Javadoc)
|
306 |
* @see org.gvsig.tools.swing.api.Component#asJComponent()
|
307 |
*/
|
308 |
@Override
|
309 |
public JComponent asJComponent() { |
310 |
return this; |
311 |
} |
312 |
|
313 |
/* (non-Javadoc)
|
314 |
* @see org.gvsig.raster.app.mainplugin.colortable.ColorTablePagePanel#fetch(org.gvsig.fmap.mapcontext.layers.FLayer)
|
315 |
*/
|
316 |
@Override
|
317 |
public boolean fetch(FLayer layer) { |
318 |
RasterLayer rasterLayer = (RasterLayer) layer; |
319 |
RasterLegend rasterLegend = (RasterLegend) rasterLayer.getLegend(); |
320 |
ColorInterpretation colorInterpretation = rasterLegend.getColorInterpretation(); |
321 |
// updateColorInterpretation(colorInterpretation);
|
322 |
int paletteBand = ((BandItem)cmbBand.getSelectedItem()).getIndex();
|
323 |
colorInterpretation.setPaletteBand(paletteBand); |
324 |
if (paletteBand>=0) { |
325 |
try {
|
326 |
colorInterpretation.setPalette((ColorTable)this.editor.getColorTable().clone());
|
327 |
} catch (CloneNotSupportedException e) { |
328 |
String message = "Can't clone ColorTable."; |
329 |
LOG.warn(message, e); |
330 |
throw new RuntimeException(message, e); |
331 |
} |
332 |
} |
333 |
updateCmbBands(rasterLayer); |
334 |
return true; |
335 |
} |
336 |
|
337 |
/**
|
338 |
* @param colorInterpretation
|
339 |
*/
|
340 |
private void updateColorInterpretation(ColorInterpretation colorInterpretation) { |
341 |
int paletteBand = ((BandItem)cmbBand.getSelectedItem()).getIndex();
|
342 |
colorInterpretation.setPaletteBand(paletteBand); |
343 |
if (paletteBand>=0) { |
344 |
colorInterpretation.setPalette(this.editor.getColorTable());
|
345 |
} |
346 |
} |
347 |
|
348 |
/* (non-Javadoc)
|
349 |
* @see org.gvsig.raster.app.mainplugin.colortable.ColorTablePagePanel#set(org.gvsig.fmap.mapcontext.layers.FLayer)
|
350 |
*/
|
351 |
@Override
|
352 |
public void set(FLayer layer) { |
353 |
this.rasterLayer = (RasterLayer) layer;
|
354 |
try {
|
355 |
this.clonedLayerForPreview = (RasterLayer) this.rasterLayer.cloneLayer(); |
356 |
} catch (Exception e1) { |
357 |
LOG.warn("Can't clone the raster layer to preview.", e1);
|
358 |
return;
|
359 |
} |
360 |
init(); |
361 |
} |
362 |
|
363 |
/**
|
364 |
*
|
365 |
*/
|
366 |
private void init() { |
367 |
updateCmbBands(this.clonedLayerForPreview);
|
368 |
RasterLegend legend = null;
|
369 |
legend = (RasterLegend) this.clonedLayerForPreview.getLegend();
|
370 |
|
371 |
ColorInterpretation colorInterpretation = legend.getColorInterpretation(); |
372 |
|
373 |
if (colorInterpretation.isPalette()) {
|
374 |
splPanel.getLeftComponent().setEnabled(true);
|
375 |
colorTable = colorInterpretation.getPalette(); |
376 |
} else {
|
377 |
splPanel.getLeftComponent().setEnabled(false);
|
378 |
} |
379 |
setEditorColorTable(); |
380 |
|
381 |
initializePreviewer(); |
382 |
this.initialized = true; |
383 |
} |
384 |
|
385 |
/**
|
386 |
* @return
|
387 |
*/
|
388 |
private TableModelListener getEditorTableModelListener() { |
389 |
if (editorTableModelListener == null) { |
390 |
editorTableModelListener = new TableModelListener() { |
391 |
|
392 |
@Override
|
393 |
public void tableChanged(TableModelEvent e) { |
394 |
updatePreviewer(); |
395 |
} |
396 |
}; |
397 |
} |
398 |
return editorTableModelListener;
|
399 |
} |
400 |
|
401 |
private void updateCmbBands(RasterLayer layer){ |
402 |
RasterStore store = layer.getRasterStore(); |
403 |
|
404 |
DefaultComboBoxModel<BandItem> bandsModel = new DefaultComboBoxModel<BandItem>(); |
405 |
|
406 |
for (int i = -1; i < store.getBands(); i++) { |
407 |
BandDescriptor bandDescriptor = null;
|
408 |
if(i>=0){ |
409 |
bandDescriptor = store.getBandDescriptor(i); |
410 |
} |
411 |
|
412 |
bandsModel.addElement(new BandItem(i,bandDescriptor));
|
413 |
} |
414 |
cmbBand.setModel(bandsModel); |
415 |
|
416 |
ColorInterpretation colorInterpretation = layer.getLegend().getColorInterpretation(); |
417 |
if(colorInterpretation.isPalette()) {
|
418 |
cmbBand.setSelectedIndex(colorInterpretation.getPaletteBand()+1);
|
419 |
} else {
|
420 |
cmbBand.setSelectedIndex(0);
|
421 |
} |
422 |
} |
423 |
|
424 |
private static class BandItem{ |
425 |
|
426 |
private int index; |
427 |
private BandDescriptor descriptor;
|
428 |
|
429 |
public BandItem(int index, BandDescriptor descriptor) { |
430 |
this.setIndex(index);
|
431 |
this.setDescriptor(descriptor);
|
432 |
} |
433 |
|
434 |
/**
|
435 |
* @return the index
|
436 |
*/
|
437 |
public int getIndex() { |
438 |
return index;
|
439 |
} |
440 |
|
441 |
/**
|
442 |
* @param index the index to set
|
443 |
*/
|
444 |
public void setIndex(int index) { |
445 |
this.index = index;
|
446 |
} |
447 |
|
448 |
/**
|
449 |
* @return the descriptor
|
450 |
*/
|
451 |
public BandDescriptor getDescriptor() {
|
452 |
return descriptor;
|
453 |
} |
454 |
|
455 |
/**
|
456 |
* @param descriptor the descriptor to set
|
457 |
*/
|
458 |
public void setDescriptor(BandDescriptor descriptor) { |
459 |
this.descriptor = descriptor;
|
460 |
} |
461 |
} |
462 |
|
463 |
private static class ComboBandsCellRenderer extends javax.swing.JLabel implements ListCellRenderer<BandItem> { |
464 |
|
465 |
/**
|
466 |
*
|
467 |
*/
|
468 |
private static final long serialVersionUID = 7763209775607437499L; |
469 |
|
470 |
@Override
|
471 |
public Component getListCellRendererComponent(JList<? extends BandItem> list, BandItem value, int index, |
472 |
boolean isSelected, boolean cellHasFocus) { |
473 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
474 |
if (value.getIndex()<0) { |
475 |
setHorizontalAlignment(JLabel.CENTER);
|
476 |
setText("\u2015");
|
477 |
} else {
|
478 |
setHorizontalAlignment(JLabel.LEFT);
|
479 |
setText(i18nManager.getTranslation("_band")+": "+(value.getIndex())+" - '"+value.getDescriptor().getName()+"'"); |
480 |
} |
481 |
return this; |
482 |
} |
483 |
|
484 |
} |
485 |
|
486 |
@Override
|
487 |
public void setVisible(boolean aFlag) { |
488 |
super.setVisible(aFlag);
|
489 |
splPanel.setDividerLocation(0.60);
|
490 |
splPanelRight.setDividerLocation(0.40);
|
491 |
} |
492 |
|
493 |
/**
|
494 |
*
|
495 |
*/
|
496 |
private void setEditorColorTable() { |
497 |
if (colorTable != null) { |
498 |
editor.set(colorTable); |
499 |
if (editorTableModelListener == null) { |
500 |
editor.addTableModelListener(getEditorTableModelListener()); |
501 |
} |
502 |
} |
503 |
} |
504 |
|
505 |
|
506 |
} |