Revision 176

View differences:

org.gvsig.raster.app/trunk/org.gvsig.raster.app/org.gvsig.raster.app.tools/src/main/java/org/gvsig/raster/app/extension/tool/GenericToolBarModule.java
34 34
import org.gvsig.app.project.documents.view.gui.AbstractViewPanel;
35 35
import org.gvsig.fmap.mapcontext.MapContext;
36 36
import org.gvsig.raster.app.extension.tool.clip.ClippingTocMenuEntry;
37
import org.gvsig.raster.app.extension.tool.colortable.ColorTableTocMenuEntry;
37 38
import org.gvsig.raster.app.extension.tool.enhanced.EnhancedTocMenuEntry;
38 39
import org.gvsig.raster.app.extension.tool.filter.FilterTocMenuEntry;
39 40
import org.gvsig.raster.app.extension.tool.generictoolbar.GenericToolBarMenuItem;
......
130 131
		point.append("RasterProperties", "", RasterPropertiesTocMenuEntry.getSingleton());
131 132
		point.append("SelectLayer", "", SelectLayerTocMenuEntry.getSingleton());
132 133
		//point.append("HistogramPanel", "", HistogramTocMenuEntry.getSingleton());
133
		//point.append("ViewColorTable", "",ColorTableTocMenuEntry.getSingleton());
134
		point.append("ViewColorTable", "",ColorTableTocMenuEntry.getSingleton());
134 135
		point.append("Overviews", "", OverviewsTocMenuEntry.getSingleton());
135 136
		point.append("RoisManager", "", ROIManagerTocMenuEntry.getSingleton());
136 137
		//point.append("ViewRasterAnalysis", "", ViewRasterAnalysisTocMenuEntry.getSingleton());
org.gvsig.raster.app/trunk/org.gvsig.raster.app/org.gvsig.raster.app.tools/src/main/java/org/gvsig/raster/app/extension/tool/colortable/ui/library/ColorTableLibraryPanel.java
1
/* gvSIG. Geographic Information System of the Valencian Government
2
*
3
* Copyright (C) 2007-2008 Infrastructures and Transports Department
4
* of the Valencian Government (CIT)
5
* 
6
* This program is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU General Public License
8
* as published by the Free Software Foundation; either version 2
9
* of the License, or (at your option) any later version.
10
* 
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
* GNU General Public License for more details.
15
* 
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
19
* MA  02110-1301, USA.
20
* 
21
*/
22
package org.gvsig.raster.app.extension.tool.colortable.ui.library;
23

  
24
import java.awt.BorderLayout;
25
import java.awt.Dimension;
26
import java.awt.FlowLayout;
27
import java.awt.event.ActionEvent;
28
import java.awt.event.ActionListener;
29
import java.io.File;
30
import java.io.IOException;
31
import java.util.ArrayList;
32
import java.util.EventObject;
33
import java.util.Iterator;
34

  
35
import javax.swing.ImageIcon;
36
import javax.swing.JButton;
37
import javax.swing.JFileChooser;
38
import javax.swing.JOptionPane;
39
import javax.swing.JPanel;
40
import javax.swing.JScrollPane;
41

  
42
import org.gvsig.andami.Launcher;
43
import org.gvsig.andami.PluginServices;
44
import org.gvsig.fmap.dal.coverage.RasterLocator;
45
import org.gvsig.fmap.dal.coverage.RasterManager;
46
import org.gvsig.fmap.dal.coverage.datastruct.ColorItem;
47
import org.gvsig.fmap.dal.coverage.datastruct.ColorTableLibrary;
48
import org.gvsig.fmap.dal.coverage.exception.RasterLegendIONotFound;
49
import org.gvsig.fmap.dal.coverage.store.props.ColorTable;
50
import org.gvsig.gui.beans.listview.ListViewComponent;
51
import org.gvsig.gui.beans.listview.ListViewItem;
52
import org.gvsig.gui.beans.listview.ListViewListener;
53
import org.gvsig.raster.app.extension.tool.colortable.data.ColorTableData;
54
import org.gvsig.raster.util.BasePanel;
55
import org.gvsig.raster.util.ExtendedFileFilter;
56
import org.gvsig.raster.util.RasterToolsUtil;
57
/**
58
 * Panel que aparece en la parte inferior derecha que contiene la lista
59
 * de librerias disponibles para las tablas de color, con sus botones correspondientes
60
 * para importar, exportar, borrar y a�adir librerias.
61
 * 
62
 * 19/02/2008
63
 * @author BorSanZa - Borja S�nchez Zamorano (borja.sanchez@iver.es)
64
 */
65
public class ColorTableLibraryPanel extends BasePanel implements ActionListener, ListViewListener {
66
	private static final long serialVersionUID       = 1L;
67
	private JPanel            panelButtons           = null;
68
	private JButton           buttonAdd              = null;
69
	private JButton           buttonDel              = null;
70
	private JButton           buttonImport           = null;
71
	private JButton           buttonExport           = null;
72
	private ListViewComponent listViewComponent      = null;
73
	private boolean           hasChanged             = false;
74
	private ArrayList<ColorTableLibraryListener>         
75
	                          actionCommandListeners = new ArrayList<ColorTableLibraryListener>();
76
	private ColorTableData    colorTableData         = null;
77
	private ColorTable        currentColorTable      = null;
78
	public static String      palettesPath           = Launcher.getAppHomeDir() + "colortable";
79
	private ColorTableLibrary tableLib               = null;
80
	private RasterManager     rManager               = null;
81
	
82
	/**
83
	 *Inicializa componentes gr?ficos y traduce
84
	 */
85
	public ColorTableLibraryPanel() {
86
		init();
87
		translate();
88
	}
89
	
90
	public ColorTableLibraryPanel(ColorTableData colorTableData){
91
		this.colorTableData = colorTableData;
92
		init();
93
		translate();
94
	}
95
	
96
	/*
97
	 * (non-Javadoc)
98
	 * @see org.gvsig.raster.util.BasePanel#init()
99
	 */
100
	protected void init() {
101
		rManager = RasterLocator.getManager();
102
		tableLib = rManager.getDataStructFactory().getColorTableLibrary();
103
		setLayout(new BorderLayout());
104
		
105
		JPanel panel = new JPanel();
106
		panel.setLayout(new BorderLayout());
107
		panel.setBorder(javax.swing.BorderFactory.createEmptyBorder(8, 8, 8, 8));
108
		
109
		JScrollPane jScrollPane = new JScrollPane();
110
		jScrollPane.setViewportView(getListViewComponent());
111
		jScrollPane.setAutoscrolls(true);
112
		
113
		panel.add(jScrollPane, BorderLayout.CENTER);
114
		add(panel, BorderLayout.CENTER);
115
		add(getPanelButtons(), BorderLayout.SOUTH);
116
		setPreferredSize(new Dimension(0, 192));
117

  
118
		loadDiskLibrary();
119
	}
120

  
121
	/*
122
	 * (non-Javadoc)
123
	 * @see org.gvsig.raster.util.BasePanel#translate()
124
	 */
125
	protected void translate() {
126
	}
127
	
128
	private void loadDiskLibrary() {
129
		ArrayList<String> fileList = tableLib.getPaletteFileList(palettesPath);
130
		for (int i = 0; i < fileList.size(); i++) {
131
			ArrayList<ColorItem> paletteItems = new ArrayList<ColorItem>();
132
			String paletteName = tableLib.loadPalette(palettesPath, (String) fileList.get(i), paletteItems);
133

  
134
			if (paletteItems.size() <= 0)
135
				continue;
136

  
137
			ColorTable colorTable = tableLib.createColorTable();
138
			colorTable.setName(paletteName);
139
			colorTable.createPaletteFromColorItems(paletteItems, true);
140
			colorTable.setInterpolated(true);
141

  
142
			ListViewItem item = new ListViewItem(new ColorTableIconPainter(colorTable), paletteName);
143
			item.setTag(fileList.get(i));
144
			getListViewComponent().addItem(item);
145
		}
146
		getListViewComponent().setSelectedIndex(0);
147
		callColorTableChanged();		
148
	}
149
	
150
	/**
151
	 * Devuelve el jButtonAdd
152
	 * @return
153
	 */
154
	private JButton getButtonAdd() {
155
		if (buttonAdd == null) {
156
			ImageIcon icon = null;
157
			try {
158
				icon = PluginServices.getIconTheme().get("addlayer-icon");
159
			} catch (NullPointerException n) {
160
				// No ha encontrado el icono, no lo muestra.
161
			}
162

  
163
			buttonAdd = new JButton(icon);
164
			buttonAdd.setPreferredSize(new Dimension(22, 19));
165
			buttonAdd.addActionListener(this);
166
			buttonAdd.setToolTipText(getText(this, "nueva_libreria_title"));
167
		}
168
		return buttonAdd;
169
	}
170

  
171
	/**
172
	 * Devuelve el jButtonDel
173
	 * @return
174
	 */
175
	private JButton getButtonDel() {
176
		if (buttonDel == null) {
177
			ImageIcon icon = null;
178
			try {
179
				icon = PluginServices.getIconTheme().get("delall-icon");
180
			} catch (NullPointerException n) {
181
				// No ha encontrado el icono, no lo muestra.
182
			}
183
			buttonDel = new JButton(icon);
184
			buttonDel.setPreferredSize(new Dimension(22, 19));
185
			buttonDel.addActionListener(this);
186
			buttonDel.setToolTipText(getText(this, "borrar_libreria"));
187
		}
188
		return buttonDel;
189
	}
190

  
191
	/**
192
	 * Devuelve el jButtonDel
193
	 * @return
194
	 */
195
	private JButton getButtonImport() {
196
		if (buttonImport == null) {
197
			ImageIcon icon = null;
198
			try {
199
				icon = PluginServices.getIconTheme().get("import-icon");
200
			} catch (NullPointerException n) {
201
				// No ha encontrado el icono, no lo muestra.
202
			}
203
			buttonImport = new JButton(icon);
204
			buttonImport.setPreferredSize(new Dimension(22, 19));
205
			buttonImport.addActionListener(this);
206
			buttonImport.setToolTipText(getText(this, "import_libreria"));
207
		}
208
		return buttonImport;
209
	}
210

  
211
	/**
212
	 * Devuelve el jButtonDel
213
	 * @return
214
	 */
215
	private JButton getButtonExport() {
216
		if (buttonExport == null) {
217
			ImageIcon icon = null;
218
			try {
219
				icon = PluginServices.getIconTheme().get("export-icon");
220
			} catch (NullPointerException n) {
221
				// No ha encontrado el icono, no lo muestra.
222
			}
223
			buttonExport = new JButton(icon);
224
			buttonExport.setPreferredSize(new Dimension(22, 19));
225
			buttonExport.addActionListener(this);
226
			buttonExport.setToolTipText(getText(this, "export_libreria"));
227
		}
228
		return buttonExport;
229
	}
230
	
231
	/**
232
	 * Devuelve el panel de botones de la libreria de a�adir y borrar
233
	 * @return
234
	 */
235
	private JPanel getPanelButtons() {
236
		if (panelButtons == null) {
237
			panelButtons = new JPanel();
238
			panelButtons.setPreferredSize(new Dimension(0, 21));
239

  
240
			FlowLayout flowLayout5 = new FlowLayout();
241
			flowLayout5.setHgap(5);
242
			flowLayout5.setVgap(0);
243
			flowLayout5.setAlignment(java.awt.FlowLayout.CENTER);
244
			panelButtons.setLayout(flowLayout5);
245

  
246
			panelButtons.add(getButtonAdd(), null);
247
			panelButtons.add(getButtonExport(), null);
248
			panelButtons.add(getButtonImport(), null);
249
			panelButtons.add(getButtonDel(), null);
250
		}
251
		return panelButtons;
252
	}
253

  
254
	private ListViewComponent getListViewComponent() {
255
		if (listViewComponent == null) {
256
			listViewComponent = new ListViewComponent();
257
			listViewComponent.addListSelectionListener(this);
258
			listViewComponent.setEditable(true);
259
		}
260
		return listViewComponent;
261
	}
262

  
263
	private void callColorTableChanged() {
264
		Iterator<ColorTableLibraryListener> acIterator = actionCommandListeners.iterator();
265
		while (acIterator.hasNext()) {
266
			ColorTableLibraryListener listener = (ColorTableLibraryListener) acIterator.next();
267
			listener.actionColorTableChanged(new ColorTableLibraryEvent(this));
268
		}
269
	}
270
	
271
	/**
272
	 * Accion que se ejecuta cuando se disparan los siguientes eventos:
273
	 *  - Checkbox de interpolacion.
274
	 *  - Checkbox de habilitacion de colortables.
275
	 *  - Boton de a�adir un colortable nuevo.
276
	 *  - Boton de borrar un colortable seleccionado.
277
	 */
278
	public void actionPerformed(ActionEvent e) {
279
		if (e.getSource() == getButtonAdd()) {
280
			AddLibraryWindow addLibrary = new AddLibraryWindow();
281
			if (addLibrary.showConfirm(this) == JOptionPane.OK_OPTION) {
282
				ColorTable colorTable = addLibrary.getColorTable();
283
				if (colorTable != null) {
284
					ListViewItem item = new ListViewItem(new ColorTableIconPainter(colorTable), colorTable.getName());
285
					item.setTag(item.getName() + ".xml");
286
					getListViewComponent().addItem(item);
287
					getListViewComponent().setSelectedIndex(getListViewComponent().getItems().size() - 1);
288
					((ColorTableIconPainter) item.getIcon()).getColorTable().setName(item.getName());
289
					colorTable.setName(((ColorTableIconPainter) item.getIcon()).getColorTable().getName());
290
					tableLib.save_to_1_1(palettesPath, colorTable);
291
					callColorTableChanged();
292
				}
293
			}
294
		}
295
		
296
		if (e.getSource() == getButtonExport()) {
297
			JFileChooser chooser = new JFileChooser();
298
			chooser.setAcceptAllFileFilterUsed(false);
299

  
300
			String[] formats = tableLib.getFormats();
301
			ExtendedFileFilter fileFilter = null;
302
			ExtendedFileFilter firstFileFilter = null;
303
			for (int i = 0; i < formats.length; i++) {
304
				fileFilter = new ExtendedFileFilter();
305
				fileFilter.addExtension(formats[i]);
306

  
307
				try {
308
					String s = RasterLocator.getManager().getFileUtils().getExtensionFromFileName(formats[i]);
309
					String desc = rManager.getRasterLegendIO(s).getDescription();
310
					if (desc != null)
311
						fileFilter.setDescription(desc);
312
				} catch (RasterLegendIONotFound e1) {
313
					// Si no puedo pillar la descripcion no pasa nada
314
				}
315
				if (firstFileFilter == null)
316
					firstFileFilter = fileFilter;
317

  
318
				chooser.addChoosableFileFilter(fileFilter);
319
			}
320
			if (firstFileFilter != null)
321
				chooser.setFileFilter(firstFileFilter);
322

  
323
			int returnVal = chooser.showSaveDialog(this);
324
			if (returnVal == JFileChooser.APPROVE_OPTION) {
325
				try {
326
					if (colorTableData != null){	
327
						tableLib.save_to_1_1(ColorTableLibraryPanel.palettesPath, colorTableData.getColorTable());
328
					}
329
					ExtendedFileFilter filter = (ExtendedFileFilter) chooser.getFileFilter();
330
					String file = filter.getNormalizedFilename(chooser.getSelectedFile());
331
					
332
					String s = RasterLocator.getManager().getFileUtils().getExtensionFromFileName(file);
333
					rManager.getRasterLegendIO(s).write(getColorTableSelected(), new File(file));
334
				} catch (IOException e1) {
335
					e1.printStackTrace();
336
				} catch (RasterLegendIONotFound e1) {
337
					e1.printStackTrace();
338
				}
339
			}
340
		}
341

  
342
		if (e.getSource() == getButtonImport()) {
343
			JFileChooser chooser = new JFileChooser();
344
			chooser.setAcceptAllFileFilterUsed(false);
345
			chooser.setMultiSelectionEnabled(true);
346
			
347
			String[] formats = tableLib.getFormats();
348
			ExtendedFileFilter allFilters = new ExtendedFileFilter();
349
			ExtendedFileFilter fileFilter = null;
350
			for (int i = 0; i < formats.length; i++) {
351
				fileFilter = new ExtendedFileFilter();
352
				fileFilter.addExtension(formats[i]);
353
				allFilters.addExtension(formats[i]);
354

  
355
				try {
356
					String s = RasterLocator.getManager().getFileUtils().getExtensionFromFileName((formats[i]));
357
					String desc = rManager.getRasterLegendIO(s).getDescription();
358
					if (desc != null)
359
						fileFilter.setDescription(desc);
360
				} catch (RasterLegendIONotFound e1) {
361
					// Si no puedo pillar la descripcion no pasa nada
362
				}
363

  
364
				chooser.addChoosableFileFilter(fileFilter);
365
			}
366
			allFilters.setDescription(getText(this, "todos_soportados"));
367
			chooser.addChoosableFileFilter(allFilters);
368
			chooser.setFileFilter(allFilters);
369

  
370
			int returnVal = chooser.showOpenDialog(this);
371

  
372
			if (returnVal == JFileChooser.APPROVE_OPTION) {
373
				try {
374
					for (int file = 0; file<chooser.getSelectedFiles().length; file++) {
375
						fileFilter = null;
376
						for (int i = 0; i < formats.length; i++) {
377
							fileFilter = new ExtendedFileFilter();
378
							fileFilter.addExtension(formats[i]);
379
							if (fileFilter.accept(chooser.getSelectedFiles()[file])) {
380
								ColorTable colorTable = rManager.getRasterLegendIO(formats[i]).read(chooser.getSelectedFiles()[file]);
381

  
382
								if (colorTable != null){
383
									colorTable.setInterpolated(true);
384

  
385
									ListViewItem item = new ListViewItem(new ColorTableIconPainter(colorTable), colorTable.getName());
386
									item.setTag(item.getName() + ".xml");
387
									getListViewComponent().addItem(item);
388
									getListViewComponent().setSelectedIndex(getListViewComponent().getItems().size() - 1);
389
									((ColorTableIconPainter) item.getIcon()).getColorTable().setName(item.getName());
390
									colorTable.setName(((ColorTableIconPainter) item.getIcon()).getColorTable().getName());
391
									tableLib.save_to_1_1(palettesPath, colorTable);
392
									callColorTableChanged();
393
								} else {
394
									RasterToolsUtil.messageBoxError("error_importando_tabla_color_rmf", this);
395
								}
396
								break;
397
							}
398
						}
399
					}
400
				} catch (IOException e1) {
401
					e1.printStackTrace();
402
					RasterToolsUtil.messageBoxError("error_importando_tabla_color_rmf", this);
403
				} catch (RasterLegendIONotFound e1) {
404
					e1.printStackTrace();
405
				}
406
			}
407
		}
408

  
409
		if (e.getSource() == getButtonDel()) {
410
			if (getListViewComponent().getSelectedIndices().length > 0) {
411
				if (RasterToolsUtil.messageBoxYesOrNot("desea_borrar_librerias", this)) {
412
					File oldFile = new File(palettesPath + File.separator + getListViewComponent().getSelectedValue().getTag());
413
					oldFile.delete();
414
					int pos = getListViewComponent().getSelectedIndices()[0];
415
					getListViewComponent().removeSelecteds();
416
					
417
					if (getListViewComponent().getItems().size() == 0) {
418
						loadDiskLibrary();
419
					} else {
420
						getListViewComponent().setSelectedIndex(pos - 1);
421
						callColorTableChanged();
422
					}
423
				}
424
			}
425
			return;
426
		}
427
	}
428

  
429
	/*
430
	 * (non-Javadoc)
431
	 * @see org.gvsig.gui.beans.listview.ListViewListener#actionItemNameChanged(java.util.EventObject, java.lang.String, org.gvsig.gui.beans.listview.ListViewItem)
432
	 */
433
	public void actionItemNameChanged(EventObject e, String oldName, ListViewItem item) {
434
		File oldFile = new File(palettesPath + File.separator + oldName + ".xml");
435
		oldFile.delete();
436

  
437
		getColorTableSelected().setName(item.getName());
438
		item.setTag(item.getName() + ".xml");
439

  
440
		tableLib.save_to_1_1(palettesPath, getColorTableSelected());
441
		
442
		callColorTableChanged();
443
	}
444

  
445
	/**
446
	 * Comprueba si el usuario ha cambiado los valores de la tabla de colores,
447
	 * si es as�, le pregunta al usuario si quiere guardar los datos, y seg�n la
448
	 * selecci�n, restaura los datos antiguos o los machaca.
449
	 */
450
	private void testLibraryChanged() {
451
		if (hasChanged) {
452
			if (RasterToolsUtil.messageBoxYesOrNot("desea_guardar_cambios", this)) {
453
				tableLib.save_to_1_1(palettesPath, getColorTableSelected());
454
				getListViewComponent().repaint();
455
			}
456
			hasChanged = false;
457
		}
458
	}
459
	
460
	/**
461
	 * Selecciona la tabla de color por defecto.
462
	 */
463
	public void selectDefault() {
464
		int selected = 0;
465
		for (int i = 0; i < getListViewComponent().getItems().size(); i++) {
466
			if (((ListViewItem) getListViewComponent().getItems().get(i)).getName().equals("Default")) {
467
				selected = i;
468
				break;
469
			}
470
		}
471
		getListViewComponent().setSelectedIndex(selected);
472
		callColorTableChanged();
473
	}
474
	
475
	/**
476
	 * Inserta una tabla de color en la posicion especificada y selecciona.
477
	 */
478
	public void addColorTable(int pos, ColorTable colorTable) {
479
		ListViewItem item = new ListViewItem(new ColorTableIconPainter(colorTable), getText(this, "tabla_actual"));
480
		getListViewComponent().addItem(pos, item);
481
		getListViewComponent().setSelectedIndex(pos);
482
		callColorTableChanged();
483
	}
484
	
485
	/**
486
	 * Accion que se ejecuta cuando cambia la seleccion de un item del
487
	 * ListViewComponent
488
	 */
489
	public void actionValueChanged(EventObject e) {
490
		currentColorTable = ((ColorTableIconPainter) getListViewComponent().getSelectedValue().getIcon()).getColorTable();
491
		testLibraryChanged();
492
		callColorTableChanged();
493
	}
494
	
495
	/**
496
	 * Define si se visualizara el componente con interpolacion o no.
497
	 * @param enabled
498
	 */
499
	public void setInterpolated(boolean enabled) {
500
		for (int i = 0; i < getListViewComponent().getItems().size(); i++)
501
			((ColorTableIconPainter) ((ListViewItem) getListViewComponent().getItems().get(i)).getIcon()).setInterpolated(enabled);
502
		getListViewComponent().repaint();
503
	}
504
	
505
	/**
506
	 * A�adir un listener a la lista de eventos
507
	 * @param listener
508
	 */
509
	public void addColorTableLibraryListener(ColorTableLibraryListener listener) {
510
		if (!actionCommandListeners.contains(listener))
511
			actionCommandListeners.add(listener);
512
	}
513

  
514
	/**
515
	 * Borrar un listener de la lista de eventos
516
	 * @param listener
517
	 */
518
	public void removeColorTableLibraryListener(ColorTableLibraryListener listener) {
519
		actionCommandListeners.remove(listener);
520
	}
521

  
522
	/**
523
	 * Devuelve la tabla de color que hay seleccionada en ese momento
524
	 * @return
525
	 */
526
	public ColorTable getColorTableSelected() {
527
		if (getListViewComponent().getSelectedValue() != null){
528
			currentColorTable = ((ColorTableIconPainter) getListViewComponent().getSelectedValue().getIcon()).getColorTable();
529
		}
530
		return currentColorTable;
531
	}
532
	
533
	/**
534
	 * Reescribe la tabla de color en la posicion seleccionada
535
	 * @param colorTable
536
	 */
537
	public void setColorTableSelected(ColorTable colorTable) {
538
		hasChanged = true;
539
		((ColorTableIconPainter) getListViewComponent().getSelectedValue().getIcon()).getColorTable().createPaletteFromColorItems(colorTable.getColorItems(), false);
540
		getListViewComponent().repaint();
541
	}
542

  
543
}
0 544

  
org.gvsig.raster.app/trunk/org.gvsig.raster.app/org.gvsig.raster.app.tools/src/main/java/org/gvsig/raster/app/extension/tool/colortable/ui/library/AddLibraryPanel.java
1
/* gvSIG. Geographic Information System of the Valencian Government
2
*
3
* Copyright (C) 2007-2008 Infrastructures and Transports Department
4
* of the Valencian Government (CIT)
5
* 
6
* This program is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU General Public License
8
* as published by the Free Software Foundation; either version 2
9
* of the License, or (at your option) any later version.
10
* 
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
* GNU General Public License for more details.
15
* 
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
19
* MA  02110-1301, USA.
20
* 
21
*/
22
package org.gvsig.raster.app.extension.tool.colortable.ui.library;
23

  
24
import java.awt.GridBagConstraints;
25
import java.awt.GridBagLayout;
26
import java.awt.Insets;
27

  
28
import javax.swing.JLabel;
29
import javax.swing.JPanel;
30
import javax.swing.JTextField;
31

  
32
import org.gvsig.raster.app.extension.raster.bean.stretchselector.StretchSelectorPanel;
33
import org.gvsig.raster.util.BasePanel;
34
import org.gvsig.raster.util.RasterToolsUtil;
35

  
36
/**
37
 * Ventana que contiene el panel de a?adir librer?a de tablas de color.
38
 * 
39
 * 06/08/2008
40
 * @author Nacho Brodin nachobrodin@gmail.com
41
 */
42
public class AddLibraryPanel extends BasePanel {
43
	private static final long serialVersionUID  = 1L;
44
	private StretchSelectorPanel  stretchListPanel  = null;
45
	private JPanel            libraryName       = null;
46
	private JTextField        jTextName         = null;
47
	private JLabel            jLabelName        = null;
48
	
49
	/**
50
	 * Constructor. Llama al inicializador de componentes gr?ficos
51
	 */
52
	public AddLibraryPanel() {
53
		init();
54
		translate();
55
	}
56
	
57
	/**
58
	 * Inicializa los componentes gr?ficos
59
	 */
60
	protected void init() {
61
		GridBagConstraints gridBagConstraints = new GridBagConstraints();
62
	    setLayout(new GridBagLayout());
63
	    gridBagConstraints.weightx = 1;
64
	    gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
65
	    add(getLibraryNamePanel(), gridBagConstraints);
66
	    gridBagConstraints.gridy = 1;
67
	    add(getStretchListPanel(), gridBagConstraints);
68
	}
69
	
70
	/*
71
	 * (non-Javadoc)
72
	 * @see org.gvsig.raster.util.BasePanel#translate()
73
	 */
74
	protected void translate() {
75
	}
76
	
77
	/**
78
	 * Obtiene el componente para introducir tramos
79
	 * @return StretchListPanel
80
	 */
81
	public StretchSelectorPanel getStretchListPanel() {
82
		if(stretchListPanel == null)
83
			stretchListPanel = new StretchSelectorPanel();
84
		return stretchListPanel;
85
	}
86
	
87
	/**
88
	 * Obtiene el panel con el nombre de la librer?a.
89
	 * @return JPanel
90
	 */
91
	private JPanel getLibraryNamePanel() {
92
		if(libraryName == null) {
93
			libraryName = new JPanel();
94
			libraryName.setLayout(new GridBagLayout());
95
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
96
			gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
97
			gridBagConstraints.insets = new Insets(5, 5, 2, 2);
98
			libraryName.add(getLabelName(), gridBagConstraints);
99

  
100
			gridBagConstraints.weightx = 1;
101
			gridBagConstraints.insets = new Insets(5, 2, 2, 5);
102
			libraryName.add(getLibraryName(), gridBagConstraints);
103
		}
104
		return libraryName;
105
	}
106
	
107
	/**
108
	 * Obtiene la etiqueta con el nombre de librer?a
109
	 * @return JLabel
110
	 */
111
	private JLabel getLabelName() {
112
		if(jLabelName == null) 
113
			jLabelName = new JLabel(RasterToolsUtil.getText(this, "nombre") + ":");
114
		return jLabelName;
115
	}
116
	
117
	/**
118
	 * Obtiene el nombre de la librer?a
119
	 * @return JTextField
120
	 */
121
	public JTextField getLibraryName() {
122
		if(jTextName == null)
123
			jTextName = new JTextField(RasterToolsUtil.getText(this, "nueva_libreria"));
124
		return jTextName;
125
	}
126
}
0 127

  
org.gvsig.raster.app/trunk/org.gvsig.raster.app/org.gvsig.raster.app.tools/src/main/java/org/gvsig/raster/app/extension/tool/colortable/ui/library/ColorTableLibraryEvent.java
1
/* gvSIG. Geographic Information System of the Valencian Government
2
*
3
* Copyright (C) 2007-2008 Infrastructures and Transports Department
4
* of the Valencian Government (CIT)
5
* 
6
* This program is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU General Public License
8
* as published by the Free Software Foundation; either version 2
9
* of the License, or (at your option) any later version.
10
* 
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
* GNU General Public License for more details.
15
* 
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
19
* MA  02110-1301, USA.
20
* 
21
*/
22
package org.gvsig.raster.app.extension.tool.colortable.ui.library;
23

  
24
import java.util.EventObject;
25
/**
26
 * Eventos para ColorTableLibrary
27
 *
28
 * @version 07/08/2007
29
 * @author BorSanZa - Borja S?nchez Zamorano (borja.sanchez@iver.es)
30
 */
31
public class ColorTableLibraryEvent extends EventObject {
32
	private static final long serialVersionUID = -1649548367781607532L;
33

  
34
	public ColorTableLibraryEvent(Object source) {
35
		super(source);
36
	}
37
}
0 38

  
org.gvsig.raster.app/trunk/org.gvsig.raster.app/org.gvsig.raster.app.tools/src/main/java/org/gvsig/raster/app/extension/tool/colortable/ui/library/ColorTableLibraryListener.java
1
/* gvSIG. Geographic Information System of the Valencian Government
2
*
3
* Copyright (C) 2007-2008 Infrastructures and Transports Department
4
* of the Valencian Government (CIT)
5
* 
6
* This program is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU General Public License
8
* as published by the Free Software Foundation; either version 2
9
* of the License, or (at your option) any later version.
10
* 
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
* GNU General Public License for more details.
15
* 
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
19
* MA  02110-1301, USA.
20
* 
21
*/
22
package org.gvsig.raster.app.extension.tool.colortable.ui.library;
23

  
24
import java.util.EventListener;
25
/**
26
 * Personalizacion del evento listener para el ColorTableLibrary
27
 *
28
 * @version 07/08/2007
29
 * @author BorSanZa - Borja S?nchez Zamorano (borja.sanchez@iver.es)
30
 */
31
public interface ColorTableLibraryListener extends EventListener {
32
	/**
33
	 * Evento que se dispara cuando cambia el valor del componente.
34
	 * @param e
35
	 */
36
	public void actionColorTableChanged(ColorTableLibraryEvent e);
37
}
0 38

  
org.gvsig.raster.app/trunk/org.gvsig.raster.app/org.gvsig.raster.app.tools/src/main/java/org/gvsig/raster/app/extension/tool/colortable/ui/library/ColorTableIconPainter.java
1
/* gvSIG. Geographic Information System of the Valencian Government
2
*
3
* Copyright (C) 2007-2008 Infrastructures and Transports Department
4
* of the Valencian Government (CIT)
5
* 
6
* This program is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU General Public License
8
* as published by the Free Software Foundation; either version 2
9
* of the License, or (at your option) any later version.
10
* 
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
* GNU General Public License for more details.
15
* 
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
19
* MA  02110-1301, USA.
20
* 
21
*/
22
package org.gvsig.raster.app.extension.tool.colortable.ui.library;
23

  
24
import java.awt.Color;
25
import java.awt.Graphics2D;
26
import java.awt.Rectangle;
27

  
28
import org.gvsig.fmap.dal.coverage.datastruct.ColorItem;
29
import org.gvsig.fmap.dal.coverage.store.props.ColorTable;
30
import org.gvsig.gui.beans.listview.IIconPaint;
31
/**
32
 * Clase para dibujar los iconos del ListViewComponent del panel de color. Se
33
 * puede indicar si la paleta esta seleccionada y si se dibuja con
34
 * interpolaciones.
35
 * 
36
 * @version 29/06/2007
37
 * @author BorSanZa - Borja S?nchez Zamorano (borja.sanchez@iver.es)
38
 */
39
public class ColorTableIconPainter implements IIconPaint {
40
	private ColorTable colorTable;
41

  
42
	/**
43
	 * Construye un ColorTablePaint con una tabla de color pasada por parametro
44
	 * @param colorTable
45
	 */
46
	public ColorTableIconPainter(ColorTable colorTable) {
47
		this.colorTable = colorTable;
48
	}
49

  
50
	/**
51
	 * Define si los valores estan interpolados o no entre si
52
	 * @param value
53
	 */
54
	public void setInterpolated(boolean value) {
55
		colorTable.setInterpolated(value);
56
	}
57

  
58
	/**
59
	 * Obtiene el ColorTable
60
	 * @return
61
	 */
62
	public ColorTable getColorTable() {
63
		return colorTable;
64
	}
65

  
66
	/**
67
	 * Metodo de pintado de la tabla de color
68
	 * @param g
69
	 * @param isSelected
70
	 */
71
	public void paint(Graphics2D g, boolean isSelected) {
72
		Rectangle area = g.getClipBounds();
73

  
74
		int x1 = area.x;
75
		int x2 = area.x + area.width - 1;
76

  
77
		Color bgColor = new Color(224, 224, 224);
78
		for (int i = 0; (i * 4) <= area.width; i++) {
79
			for (int j = 0; (j * 4) <= area.height; j++) {
80
				if ((i + j) % 2 == 0)
81
					g.setColor(Color.white);
82
				else
83
					g.setColor(bgColor);
84
				g.fillRect(area.x + 1 + i * 4, area.y + 1 + j * 4, 4, 4);
85
			}
86
		}		
87
		
88
		if (colorTable.getColorItems().size() >= 1) {
89
			double min = ((ColorItem) colorTable.getColorItems().get(0)).getValue();
90
			double max = ((ColorItem) colorTable.getColorItems().get(colorTable.getColorItems().size() - 1)).getValue();
91
			for (int i = area.x; i < (area.x + area.width); i++) {
92
				double pos = min + (((max - min) * (i - area.x)) / (area.width - 2));
93

  
94
				byte[] col3 = colorTable.getRGBAByBand(pos);
95
				g.setColor(new Color(col3[0] & 0xff, col3[1] & 0xff, col3[2] & 0xff, col3[3] & 0xff));
96
				g.drawLine(i, area.y, i, area.y + area.height);
97
			}
98
		} else {
99
			g.setColor(new Color(224, 224, 224));
100
			g.fillRect(x1, area.y, x2 - x1, area.height - 1);
101
		}
102
		if (isSelected)
103
			g.setColor(Color.black);
104
		else
105
			g.setColor(new Color(96, 96, 96));
106
		g.drawRect(x1, area.y, x2 - x1, area.height - 1);
107
	}	
108
}
0 109

  
org.gvsig.raster.app/trunk/org.gvsig.raster.app/org.gvsig.raster.app.tools/src/main/java/org/gvsig/raster/app/extension/tool/colortable/ui/library/AddLibraryWindow.java
1
/* gvSIG. Geographic Information System of the Valencian Government
2
*
3
* Copyright (C) 2007-2008 Infrastructures and Transports Department
4
* of the Valencian Government (CIT)
5
* 
6
* This program is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU General Public License
8
* as published by the Free Software Foundation; either version 2
9
* of the License, or (at your option) any later version.
10
* 
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
* GNU General Public License for more details.
15
* 
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
19
* MA  02110-1301, USA.
20
* 
21
*/
22
package org.gvsig.raster.app.extension.tool.colortable.ui.library;
23

  
24
import java.awt.Component;
25
import java.util.ArrayList;
26

  
27
import javax.swing.JOptionPane;
28

  
29
import org.gvsig.andami.PluginServices;
30
import org.gvsig.fmap.dal.coverage.RasterLocator;
31
import org.gvsig.fmap.dal.coverage.datastruct.ColorItem;
32
import org.gvsig.fmap.dal.coverage.datastruct.ColorTableLibrary;
33
import org.gvsig.fmap.dal.coverage.store.props.ColorTable;
34
import org.gvsig.raster.app.extension.raster.bean.stretchselector.StretchSelectorData;
35

  
36
/**
37
 * Ventana para a?adir una nueva libreria. En ella se especificara si queremos
38
 * una tabla de color por intervalos o el numero de cortes. La generar? con el
39
 * nombre seleccionado y todos los colores seran negros inicialmente.
40
 * 
41
 * @version 01/10/2007
42
 * @author BorSanZa - Borja S?nchez Zamorano (borja.sanchez@iver.es)
43
 */
44
public class AddLibraryWindow extends JOptionPane {
45
	private static final long   serialVersionUID  = 1L;
46
	private AddLibraryPanel     libraryPanel      = null;
47
	/**
48
	 * Si supera el numero de particiones estimado, se le preguntar? al usuario si
49
	 * quiere continuar
50
	 */
51
	private int                 limit_question    = 500;
52
	
53
	/**
54
	 * Devuelve el componente interno del JOptionPane
55
	 * @return
56
	 */
57
	private Object getMessageComponent() {
58
		return getLibraryPanel();
59
	}
60
	
61
	/**
62
	 * Obtiene el panel AddLibraryPanel
63
	 * @return
64
	 */
65
	public AddLibraryPanel getLibraryPanel() {
66
		if(libraryPanel == null)
67
			libraryPanel = new AddLibraryPanel();
68
		return libraryPanel;
69
	}
70

  
71
	/**
72
	 * Muestra el di?logo de la creaci?n de la tabla de color. Devolver?
73
	 * JOptionPane.OK_OPTION si el usuario le ha dado a aceptar y otro valor en
74
	 * caso contrario.
75
	 * @param parentComponent
76
	 * @return
77
	 */
78
	public int showConfirm(Component parentComponent) {
79
		return showConfirmDialog(
80
				parentComponent,
81
				getMessageComponent(),
82
				PluginServices.getText(this, "nueva_libreria_title"),
83
				JOptionPane.OK_CANCEL_OPTION,
84
				JOptionPane.PLAIN_MESSAGE);
85
	}
86

  
87
	/**
88
	 * Devuelve la tabla de color creada segun los parametros especificados en el
89
	 * panel.
90
	 * @return
91
	 */
92
	public ColorTable getColorTable() {
93
		ColorTableLibrary lib = RasterLocator.getManager().getDataStructFactory().getColorTableLibrary();
94
		ColorTable colorTable = lib.createColorTable();
95
		colorTable.setName(getLibraryPanel().getLibraryName().getText());
96
		ArrayList<ColorItem> items = new ArrayList<ColorItem>();
97

  
98
		StretchSelectorData stretchListData = getLibraryPanel().getStretchListPanel().getData(); 
99
		double start = stretchListData.getMinimum();
100
		double end = stretchListData.getMaximum();
101

  
102
		if (start > end) {
103
			double aux = start;
104
			start = end;
105
			end = aux;
106
		}
107

  
108
		double num = getLibraryPanel().getStretchListPanel().getInterval();
109
		boolean quest = false;
110
		if (getLibraryPanel().getStretchListPanel().getIntervalNumber().isSelected())
111
			quest = (num > limit_question);
112
		else
113
			quest = (((end - start) / num) > limit_question);
114
		if (quest && JOptionPane.showConfirmDialog(this, PluginServices.getText(this, "addlibrary_supera_limite"), PluginServices.getText(this, "confirmar"), JOptionPane.YES_NO_OPTION) == NO_OPTION)
115
			return null;
116
		if (getLibraryPanel().getStretchListPanel().getIntervalNumber().isSelected()) {
117
			for (double i = 0; i <= num; i++) {
118
				ColorItem colorItem = lib.createColorItem();
119
				colorItem.setValue(start + (((end - start) / num) * i));
120
				items.add(colorItem);
121
			}
122
		} else {
123
			for (double i = start; i <= end; i += num) {
124
				ColorItem colorItem = lib.createColorItem();
125
				colorItem.setValue(i);
126
				items.add(colorItem);
127
			}
128
		}
129

  
130
		colorTable.createPaletteFromColorItems(items, false);
131
		return colorTable;
132
	}
133
}
0 134

  
org.gvsig.raster.app/trunk/org.gvsig.raster.app/org.gvsig.raster.app.tools/src/main/java/org/gvsig/raster/app/extension/tool/colortable/ui/ColorTableDialog.java
1
/* gvSIG. Geographic Information System of the Valencian Government
2
*
3
* Copyright (C) 2007-2008 Infrastructures and Transports Department
4
* of the Valencian Government (CIT)
5
* 
6
* This program is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU General Public License
8
* as published by the Free Software Foundation; either version 2
9
* of the License, or (at your option) any later version.
10
* 
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
* GNU General Public License for more details.
15
* 
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
19
* MA  02110-1301, USA.
20
* 
21
*/
22
package org.gvsig.raster.app.extension.tool.colortable.ui;
23

  
24
import java.awt.BorderLayout;
25
import java.awt.Dimension;
26

  
27
import javax.swing.JPanel;
28

  
29
import org.gvsig.andami.PluginServices;
30
import org.gvsig.andami.ui.mdiManager.IWindow;
31
import org.gvsig.andami.ui.mdiManager.IWindowListener;
32
import org.gvsig.andami.ui.mdiManager.WindowInfo;
33
import org.gvsig.raster.fmap.layers.FLyrRaster;
34
/**
35
* <code>ColorTableDialog</code>. Creaci?n de la ventana de ColorTable para gvSIG.
36
*
37
* @version 17/04/2007
38
* @author BorSanZa - Borja S?nchez Zamorano (borja.sanchez@iver.es)
39
*/
40
public class ColorTableDialog extends JPanel implements IWindow, IWindowListener {
41
	private static final long serialVersionUID = -5374834293534046986L;
42
	private FLyrRaster layer = null;
43

  
44
	/**
45
	 * Panel de recortado de imagen que est? en la libreria raster
46
	 */
47
	private ColorTablePanel colorTablePanel = null;
48
	
49
	/**
50
	 * Constructor
51
	 * @param width Ancho
52
	 * @param height Alto
53
	 */
54
	public ColorTableDialog(FLyrRaster layer, int width, int height) {
55
		this.layer = layer;
56
		setPreferredSize(new Dimension(width, height));
57
		setSize(width, height);
58
		setLayout(new BorderLayout(5, 5));
59
		add(getColorTablePanel(), java.awt.BorderLayout.CENTER);
60
	}
61

  
62
	/**
63
	 * Obtiene el panel con el histograma
64
	 * @return HistogramPanel
65
	 */
66
	private ColorTablePanel getColorTablePanel() {
67
		if (colorTablePanel == null) {
68
			colorTablePanel = new ColorTablePanel(layer, this);
69
		}
70
		return colorTablePanel;
71
	}
72

  
73
	/*
74
	 * (non-Javadoc)
75
	 * @see com.iver.andami.ui.mdiManager.IWindow#getWindowInfo()
76
	 */
77
	public WindowInfo getWindowInfo() {
78
		WindowInfo m_viewinfo = new WindowInfo(WindowInfo.MODELESSDIALOG | WindowInfo.RESIZABLE | WindowInfo.MAXIMIZABLE);
79
		if(getColorTablePanel().getLayer() != null)
80
			m_viewinfo.setAdditionalInfo(getColorTablePanel().getLayer().getName());
81
		m_viewinfo.setTitle(PluginServices.getText(this, "tablas_color"));
82
		m_viewinfo.setHeight(this.getHeight());
83
		m_viewinfo.setWidth(this.getWidth());
84
		return m_viewinfo;
85
	}
86

  
87
	/*
88
	 * (non-Javadoc)
89
	 * @see com.iver.andami.ui.mdiManager.IWindowListener#windowClosed()
90
	 */
91
	public void windowClosed() {
92
		getColorTablePanel().windowClosed();
93
	}
94

  
95
	/*
96
	 * (non-Javadoc)
97
	 * @see com.iver.andami.ui.mdiManager.IWindowListener#windowActivated()
98
	 */
99
	public void windowActivated() {}
100

  
101
	public Object getWindowProfile() {
102
		return WindowInfo.PROPERTIES_PROFILE;
103
	}
104
}
0 105

  
org.gvsig.raster.app/trunk/org.gvsig.raster.app/org.gvsig.raster.app.tools/src/main/java/org/gvsig/raster/app/extension/tool/colortable/ui/ColorTablePanel.java
1
/* gvSIG. Geographic Information System of the Valencian Government
2
*
3
* Copyright (C) 2007-2008 Infrastructures and Transports Department
4
* of the Valencian Government (CIT)
5
* 
6
* This program is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU General Public License
8
* as published by the Free Software Foundation; either version 2
9
* of the License, or (at your option) any later version.
10
* 
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
* GNU General Public License for more details.
15
* 
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
19
* MA  02110-1301, USA.
20
* 
21
*/
22
package org.gvsig.raster.app.extension.tool.colortable.ui;
23

  
24
import java.awt.BorderLayout;
25
import java.io.IOException;
26
import java.util.ArrayList;
27

  
28
import javax.swing.JPanel;
29
import javax.swing.event.ChangeEvent;
30
import javax.swing.event.ChangeListener;
31

  
32
import org.gvsig.andami.PluginServices;
33
import org.gvsig.fmap.dal.coverage.RasterLocator;
34
import org.gvsig.fmap.dal.coverage.RasterManager;
35
import org.gvsig.fmap.dal.coverage.exception.RmfSerializerException;
36
import org.gvsig.fmap.dal.coverage.grid.GridTransparency;
37
import org.gvsig.fmap.dal.coverage.grid.RasterFilter;
38
import org.gvsig.fmap.dal.coverage.grid.RasterFilterList;
39
import org.gvsig.fmap.dal.coverage.grid.render.Render;
40
import org.gvsig.fmap.dal.coverage.store.RasterDataStore;
41
import org.gvsig.fmap.dal.coverage.store.props.ColorTable;
42
import org.gvsig.gui.beans.buttonspanel.ButtonsPanel;
43
import org.gvsig.gui.beans.buttonspanel.ButtonsPanelEvent;
44
import org.gvsig.gui.beans.buttonspanel.ButtonsPanelListener;
45
import org.gvsig.raster.app.extension.raster.bean.previewbase.PreviewBasePanel;
46
import org.gvsig.raster.app.extension.tool.colortable.data.ColorTableData;
47
import org.gvsig.raster.app.extension.tool.colortable.ui.library.ColorTableLibraryEvent;
48
import org.gvsig.raster.app.extension.tool.colortable.ui.library.ColorTableLibraryListener;
49
import org.gvsig.raster.app.extension.tool.colortable.ui.library.ColorTableLibraryPanel;
50
import org.gvsig.raster.app.extension.tool.colortable.ui.tabs.IColorTableUI;
51
import org.gvsig.raster.app.extension.tool.colortable.ui.tabs.TabInterpolated;
52
import org.gvsig.raster.app.extension.tool.colortable.ui.tabs.TabTable;
53
import org.gvsig.raster.fmap.layers.FLyrRaster;
54
import org.gvsig.raster.util.RasterToolsUtil;
55
/**
56
 * ColorTablePanel es el panel padre que contendra todos los paneles del interfaz
57
 * de tablas de color.
58
 *
59
 * @version 26/06/2007
60
 * @author BorSanZa - Borja S�nchez Zamorano (borja.sanchez@iver.es)
61
 */
62
public class ColorTablePanel extends JPanel implements ChangeListener, ButtonsPanelListener, ColorTableLibraryListener {
63
	private static final long serialVersionUID = 6028780107787443656L;
64

  
65
	private ColorTableListener      colorTableListener = null;
66
	private FLyrRaster              fLayer             = null;
67
	private ArrayList<RasterFilter> filterStatus       = null;
68
	private ColorTableData          colorTableData     = null;
69

  
70
	private IColorTableUI           tabTable           = null;
71
	private IColorTableUI           tabInterpolated    = null;
72
	
73
	private ColorTableLibraryPanel  colorTableLibraryPanel = null;
74

  
75
	private JPanel                  jPanelListView     = null;
76
	private ColorTableGlobalPanel   panelGeneral       = null;
77
	private ColorTableDialog        colorTableDialog   = null;
78
	private PreviewBasePanel        previewBasePanel   = null;
79

  
80
	/**
81
	 * Controla si se ha presionado el boton aceptar para el cerrado de la ventana
82
	 */
83
	boolean accepted = false;
84

  
85
	/**
86
	 * Construir un nuevo ColorTablePanel
87
	 */
88
	public ColorTablePanel(FLyrRaster layer, ColorTableDialog colorTableDialog) {
89
		this.colorTableDialog = colorTableDialog;
90
		fLayer = layer;
91
		initialize();
92
		setLayer(layer);
93
	}
94
	
95
	public PreviewBasePanel getPreviewBasePanel() {
96
		if (previewBasePanel == null) {
97
			ArrayList<IColorTableUI> list = new ArrayList<IColorTableUI>();
98
			list.add(getTabTable());
99
			list.add(getTabInterpolated());
100
			getColorTableListener().setLastColorTableUI(getTabTable());
101
			
102
			previewBasePanel = new PreviewBasePanel(ButtonsPanel.BUTTONS_NONE, list, (JPanel) getGeneralPanel(), getPanelListView(), getColorTableListener(), (FLyrRaster) fLayer);
103
			previewBasePanel.getTabbedPane().addChangeListener(this);
104
			previewBasePanel.getButtonsPanel().addButton(RasterToolsUtil.getText(this, "equidistar"), ButtonsPanel.BUTTON_LAST + 3);
105
			previewBasePanel.getButtonsPanel().addButton(RasterToolsUtil.getText(this, "guardar_predeterminado"), ButtonsPanel.BUTTON_LAST + 2);
106
			previewBasePanel.getButtonsPanel().addApply();
107
			previewBasePanel.getButtonsPanel().addAccept();
108
			previewBasePanel.getButtonsPanel().addCancel();
109
			previewBasePanel.addButtonPressedListener(this);
110
		}
111
		return previewBasePanel;
112
	}
113
	
114

  
115
	private void initialize() {
116
		setLayout(new BorderLayout());
117
		add(getPreviewBasePanel(), BorderLayout.CENTER);
118
	}
119

  
120
	private IColorTableUI getTabTable() {
121
		if (tabTable == null) {
122
			tabTable = new TabTable();
123
			tabTable.addColorTableUIChangedListener(getColorTableListener());
124
		}
125
		return tabTable;
126
	}
127

  
128

  
129
	private IColorTableUI getTabInterpolated() {
130
		if (tabInterpolated == null) {
131
			tabInterpolated = new TabInterpolated();
132
			tabInterpolated.addColorTableUIChangedListener(getColorTableListener());
133
		}
134
		return tabInterpolated;
135
	}
136

  
137
	public JPanel getPanelListView() {
138
		if (jPanelListView == null) {
139
			jPanelListView = new JPanel();
140
			jPanelListView.setBorder(javax.swing.BorderFactory.createTitledBorder(null, RasterToolsUtil.getText(this, "libreria"), javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null));
141
			jPanelListView.setLayout(new BorderLayout());
142
			
143
			jPanelListView.add(getColorTableLibraryPanel());
144
		}
145
		return jPanelListView;
146
	}
147
	
148
	public ColorTableLibraryPanel getColorTableLibraryPanel() {
149
		if (colorTableLibraryPanel == null) {
150
			colorTableLibraryPanel = new ColorTableLibraryPanel(getColorTableData());
151
			colorTableLibraryPanel.addColorTableLibraryListener(this);
152
			colorTableLibraryPanel.selectDefault();
153
		}
154
		return colorTableLibraryPanel;
155
	}
156

  
157
	/**
158
	 * Obtener el componente que hara la carga pesada de los listeners
159
	 * @return
160
	 */
161
	private ColorTableListener getColorTableListener() {
162
		if (colorTableListener == null) {
163
			colorTableListener = new ColorTableListener(this, getColorTableData());
164
		}
165
		return colorTableListener;
166
	}
167

  
168
	public ColorTableGlobalPanel getGeneralPanel() {
169
		if (panelGeneral == null) {
170
			panelGeneral = new ColorTableGlobalPanel(getColorTableData());
171
		}
172
		return panelGeneral;
173
	}
174

  
175
	/**
176
	 * Volvemos todo a la normalidad cuando se cancela
177
	 */
178
	private boolean restoreFilters() {
179
		if (getLayer() != null) {
180
			Render rendering = getLayer().getRender();
181
			if(rendering.getFilterList() == null)
182
				return false;
183
			rendering.getFilterList().setStatus(getFilterStatus());
184
			getLayer().getRender().setFilterList(rendering.getFilterList());
185
		}
186
		return true;
187
	}
188

  
189
	/**
190
	 * Devuelve el arrayList de filtros inicial
191
	 * @return
192
	 */
193
	private ArrayList<RasterFilter> getFilterStatus() {
194
		return filterStatus;
195
	}
196

  
197
	/**
198
	 * Carga la tabla de color que hay seleccionada en el componente
199
	 * ListViewComponent
200
	 */
201
	public void reloadPanelsFromLibraryPanel() {
202
		ColorTable colorTable = getColorTableLibraryPanel().getColorTableSelected();
203
		
204
		getColorTableData().setColorTable((ColorTable) colorTable.clone());
205
		if (colorTableData.isLimitsEnabled()) {
206
			double min = getColorTableData().getMinim();
207
			double max = getColorTableData().getMaxim();
208
			getColorTableData().getColorTable().createColorTableInRange(min, max, false);
209
		}
210
		getColorTableListener().refreshItems(true);
211
		getColorTableData().refreshPreview();
212
	}
213

  
214
	/**
215
	 * Salva la tabla de color al fichero rmf.
216
	 * @param fName
217
	 * @throws IOException
218
	 */
219
	private void saveColorTable() {
220
		RasterDataStore dataset = getLayer().getDataStore();
221
		try {
222
			if (getColorTableData().isEnabled())
223
				dataset.saveColorTableToRmf(getColorTableData().getColorTable());
224
			else
225
				dataset.saveColorTableToRmf(null);
226
		} catch (RmfSerializerException e) {
227
			RasterToolsUtil.messageBoxError("error_salvando_rmf", this, e);
228
		}
229
	}
230

  
231
	/**
232
	 * Restaura el estado de los filtros como estaban al abrir la ventana y quita
233
	 * la leyenda del TOC
234
	 */
235
	private void restoreSettings() {
236
		if (getLayer() == null)
237
			return;
238

  
239
		// Restauramos la vista con los filtros originales antes de cualquier acci�n
240
		if(!restoreFilters())
241
			return;
242

  
243
		getLayer().setLastLegend(null);
244

  
245
		RasterFilterList rasterFilterList = getLayer().getRender().getFilterList();
246
		RasterFilter colorTableFilter = rasterFilterList.getByName("colortable");
247
		if (colorTableFilter != null) {
248
			ColorTable colorTable = (ColorTable)colorTableFilter.getParam("colorTable");
249
			getLayer().setLastLegend(colorTable);
250
		}
251

  
252
		// Quitamos la banda de transparencia por si el filtro de color la ha activado
253
		try {
254
			GridTransparency transparency = getLayer().getRender().getLastTransparency();
255
			transparency.setTransparencyBand(-1);
256
			transparency.activeTransparency();
257
			transparency.setAlphaBand(null);
258
		} catch (NullPointerException ex) {
259
			//No quitamos la banda de transparencia
260
		}
261
	}
262

  
263
	/**
264
	 * Devuelve el componente que trata los datos
265
	 * @return
266
	 */
267
	private ColorTableData getColorTableData() {
268
		if (colorTableData == null) {
269
			colorTableData = new ColorTableData();
270
		}
271
		return colorTableData;
272
	}
273

  
274
	/**
275
	 * Especificar el layer para la tabla de color
276
	 * @param layer
277
	 */
278
	private void setLayer(FLyrRaster fLayer) {
279
		this.fLayer = fLayer;
280
		RasterManager rManager = RasterLocator.getManager();
281
		
282
		getGeneralPanel().setLayer(fLayer);
283
		
284
		getColorTableListener().setLayer(fLayer);
285

  
286
		RasterFilterList rasterFilterList = getLayer().getRender().getFilterList();
287

  
288
		filterStatus = rasterFilterList.getStatusCloned();
289

  
290
		RasterFilter colorTableFilter = rasterFilterList.getByName("colortable");
291
		if (colorTableFilter != null) {
292
			ColorTable colorTable = (ColorTable)colorTableFilter.getParam("colorTable");
293
			if (colorTable.isCompressible()) {
294
				if (RasterToolsUtil.messageBoxYesOrNot("comprimir_paleta", this)) {
295
					colorTable.compressPalette();
296
					colorTable.setInterpolated(true);
297
				}
298
			}
299

  
300
			ColorTable ct = rManager.getDataStructFactory().getColorTableLibrary().createColorTable();
301
			ct.setName(RasterToolsUtil.getText(this, "tabla_actual"));
302
			ct.createPaletteFromColorItems(colorTable.getColorItems(), false);
303
			ct.setInterpolated(colorTable.isInterpolated());
304
			getGeneralPanel().setCheckBoxInterpolated(colorTable.isInterpolated());
305

  
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff