gvsig-raster / org.gvsig.raster.tools / branches / org.gvsig.raster.tools_dataaccess_refactoring / org.gvsig.raster.tools.app.basic / src / main / java / org / gvsig / raster / tools / app / basic / tool / filter / regionalpha / RegionAlphaUI.java @ 2308
History | View | Annotate | Download (8.21 KB)
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.tools.app.basic.tool.filter.regionalpha; |
23 |
|
24 |
import java.awt.BorderLayout; |
25 |
import java.awt.Dimension; |
26 |
import java.util.ArrayList; |
27 |
import java.util.EventObject; |
28 |
import java.util.Iterator; |
29 |
import java.util.List; |
30 |
|
31 |
import javax.swing.JCheckBox; |
32 |
import javax.swing.JLabel; |
33 |
import javax.swing.JPanel; |
34 |
import javax.swing.event.ChangeEvent; |
35 |
import javax.swing.event.ChangeListener; |
36 |
import javax.swing.event.TableModelEvent; |
37 |
import javax.swing.event.TableModelListener; |
38 |
|
39 |
import org.gvsig.fmap.dal.coverage.RasterLocator; |
40 |
import org.gvsig.fmap.dal.coverage.datastruct.Params; |
41 |
import org.gvsig.fmap.dal.coverage.exception.ROIException; |
42 |
import org.gvsig.fmap.dal.coverage.grid.FilterUIListener; |
43 |
import org.gvsig.fmap.dal.coverage.grid.RegistrableFilterListener; |
44 |
import org.gvsig.gui.beans.table.TableContainer; |
45 |
import org.gvsig.gui.beans.table.exceptions.NotInitializeException; |
46 |
import org.gvsig.i18n.Messages; |
47 |
import org.gvsig.raster.fmap.layers.FLyrRaster; |
48 |
import org.gvsig.raster.roi.ROI; |
49 |
import org.gvsig.raster.tools.app.basic.RasterToolsUtil; |
50 |
/**
|
51 |
* Es el interfaz gr?fico que contiene el filtro de regiones de interes.
|
52 |
* En el se muestra una tabla con las posibles Rois a seleccionar y tambi?n
|
53 |
* un Checkbox para definir si la seleccion es normal o invertida.
|
54 |
*
|
55 |
* @author BorSanZa - Borja S?nchez Zamorano (borja.sanchez@iver.es)
|
56 |
*/
|
57 |
public class RegionAlphaUI extends JPanel implements RegistrableFilterListener, TableModelListener, ChangeListener { |
58 |
private static final long serialVersionUID = 4525736825113598035L; |
59 |
private TableContainer tableContainer = null; |
60 |
private FLyrRaster layer = null; |
61 |
private JLabel warning = null; |
62 |
private ArrayList<ROI> rois = null; |
63 |
private JCheckBox negative = null; |
64 |
private boolean lastInv = false; |
65 |
protected Params params = null; |
66 |
private ArrayList<FilterUIListener> |
67 |
actionCommandListeners = new ArrayList<FilterUIListener>(); |
68 |
|
69 |
/**
|
70 |
* Constructor de un RegionAlphaUI
|
71 |
*/
|
72 |
public RegionAlphaUI() {
|
73 |
init(null);
|
74 |
} |
75 |
|
76 |
public void init(Object obj) { |
77 |
setLayout(new BorderLayout()); |
78 |
add(getTableContainer(), BorderLayout.CENTER);
|
79 |
add(getWarning(), BorderLayout.NORTH);
|
80 |
add(getInverse(), BorderLayout.SOUTH);
|
81 |
} |
82 |
|
83 |
/**
|
84 |
* Obtiene el contenedor con la tabla.
|
85 |
* @return
|
86 |
*/
|
87 |
private TableContainer getTableContainer() {
|
88 |
if (tableContainer == null) { |
89 |
String[] columnNames = {" ", "Nombre", ""}; |
90 |
int[] columnWidths = {22, 334, 0}; |
91 |
tableContainer = new TableContainer(columnNames, columnWidths);
|
92 |
tableContainer.setPreferredSize(new Dimension(0, 130)); |
93 |
tableContainer.setModel("CheckBoxModel");
|
94 |
tableContainer.initialize(); |
95 |
tableContainer.setControlVisible(false);
|
96 |
tableContainer.setMoveRowsButtonsVisible(false);
|
97 |
tableContainer.getTable().getJTable().getColumnModel().getColumn(0).setMinWidth(22); |
98 |
tableContainer.getTable().getJTable().getColumnModel().getColumn(0).setMaxWidth(22); |
99 |
tableContainer.getTable().getJTable().getColumnModel().getColumn(2).setMinWidth(0); |
100 |
tableContainer.getTable().getJTable().getColumnModel().getColumn(2).setMaxWidth(0); |
101 |
tableContainer.getModel().addTableModelListener(this);
|
102 |
} |
103 |
return tableContainer;
|
104 |
} |
105 |
|
106 |
/**
|
107 |
* Obtiene el checkbox que informa de si selecciona lo que contiene las Rois
|
108 |
* o su inversa.
|
109 |
* @return JCheckBox
|
110 |
*/
|
111 |
private JCheckBox getInverse() { |
112 |
if (negative == null) { |
113 |
negative = new JCheckBox(); |
114 |
negative.setText(RasterToolsUtil.getText(this, "inversa")); |
115 |
negative.addChangeListener(this);
|
116 |
} |
117 |
return negative;
|
118 |
} |
119 |
|
120 |
/**
|
121 |
* Obtiene el mensaje de aviso de que no hay rois en la lista. Esta etiqueta solo
|
122 |
* es mostrada en caso en que la capa no tenga ROIs asociadas.
|
123 |
* @return JLabel Etiqueta con el mensaje de aviso.
|
124 |
*/
|
125 |
public JLabel getWarning() { |
126 |
if(warning == null) { |
127 |
warning = new JLabel(RasterToolsUtil.getText(this, "rois_needed")); |
128 |
warning.setVisible(false);
|
129 |
} |
130 |
return warning;
|
131 |
} |
132 |
|
133 |
/**
|
134 |
* Asigna la lista de regiones de inter?s.
|
135 |
* @param rois Lista de ROIs
|
136 |
*/
|
137 |
public void setRois(ArrayList<ROI> rois) { |
138 |
this.rois = rois;
|
139 |
} |
140 |
|
141 |
/**
|
142 |
* Asigna la capa.
|
143 |
* @param layer
|
144 |
*/
|
145 |
public void setLayer(FLyrRaster layer) { |
146 |
this.layer = layer;
|
147 |
if (layer == null) |
148 |
return;
|
149 |
|
150 |
List<ROI> roisArray = null; |
151 |
try {
|
152 |
roisArray = ((FLyrRaster) layer).getRois(); |
153 |
} catch (ROIException e1) {
|
154 |
RasterToolsUtil.messageBoxError(Messages.getText("problems_loading_rois"), null); |
155 |
} |
156 |
|
157 |
if(roisArray == null || roisArray.size() == 0) |
158 |
getWarning().setVisible(true);
|
159 |
|
160 |
if (roisArray != null) { |
161 |
for (int i = 0; i < roisArray.size(); i++) { |
162 |
ROI roi = (ROI) roisArray.get(i); |
163 |
|
164 |
Object row[] = {"", "", ""}; |
165 |
|
166 |
boolean active = false; |
167 |
|
168 |
if (rois != null) { |
169 |
for (int r = 0; r < rois.size(); r++) { |
170 |
if (((ROI) rois.get(r)) == roi) {
|
171 |
active = true;
|
172 |
break;
|
173 |
} |
174 |
} |
175 |
} |
176 |
|
177 |
row[0] = new Boolean(active); |
178 |
row[1] = roi.getName();
|
179 |
row[2] = new Integer(i); |
180 |
try {
|
181 |
getTableContainer().addRow(row); |
182 |
} catch (NotInitializeException e) {
|
183 |
} |
184 |
} |
185 |
} |
186 |
} |
187 |
|
188 |
/**
|
189 |
* Obtiene la lista de ROIs seleccionadas
|
190 |
* @return ArrayList con la lista de ROIs
|
191 |
*/
|
192 |
private List<ROI> getSelectedROIs() { |
193 |
if (layer == null) |
194 |
return null; |
195 |
|
196 |
List<ROI> roisArray = null; |
197 |
try {
|
198 |
roisArray = ((FLyrRaster) layer).getRois(); |
199 |
} catch (ROIException e1) {
|
200 |
} |
201 |
List<ROI> selected = new ArrayList<ROI>(); |
202 |
if (roisArray != null) { |
203 |
for (int i = 0; i < roisArray.size(); i++) { |
204 |
try {
|
205 |
if (((Boolean) tableContainer.getModel().getValueAt(i, 0)).booleanValue()) { |
206 |
selected.add(roisArray.get(i)); |
207 |
} |
208 |
} catch (ArrayIndexOutOfBoundsException e) { |
209 |
//Entra aqu? si se han a?adido ROIs con el cuadro abierto. Pasamos de hacer nada
|
210 |
} |
211 |
} |
212 |
} |
213 |
return selected;
|
214 |
} |
215 |
|
216 |
/**
|
217 |
* Sobrecargamos el m?todo getParams para que siempre devuelva
|
218 |
* algo.
|
219 |
*/
|
220 |
public Params getParams() {
|
221 |
params = RasterLocator.getManager().createParams( |
222 |
"rois",
|
223 |
getSelectedROIs(), |
224 |
-1,
|
225 |
null);
|
226 |
params.setParam("inverse",
|
227 |
new Boolean(getInverse().isSelected()), |
228 |
-1,
|
229 |
null);
|
230 |
return params;
|
231 |
} |
232 |
|
233 |
public void tableChanged(TableModelEvent e) { |
234 |
callStateChanged(); |
235 |
} |
236 |
|
237 |
/**
|
238 |
* Cambio de estado para el check de inversa
|
239 |
* @param e
|
240 |
*/
|
241 |
public void stateChanged(ChangeEvent e) { |
242 |
if (e.getSource().equals(getInverse())) {
|
243 |
if (((JCheckBox) e.getSource()).isSelected() != lastInv) { |
244 |
callStateChanged(); |
245 |
lastInv = ((JCheckBox) e.getSource()).isSelected();
|
246 |
} |
247 |
} |
248 |
} |
249 |
|
250 |
public void addFilterUIListener(FilterUIListener listener) { |
251 |
if (!actionCommandListeners.contains(listener))
|
252 |
actionCommandListeners.add(listener); |
253 |
} |
254 |
|
255 |
public void callStateChanged() { |
256 |
Iterator<FilterUIListener> acIterator = actionCommandListeners.iterator();
|
257 |
while (acIterator.hasNext()) {
|
258 |
FilterUIListener listener = (FilterUIListener) acIterator.next(); |
259 |
listener.actionValuesCompleted(new EventObject(this)); |
260 |
} |
261 |
} |
262 |
|
263 |
public void removeStateChangedListener(FilterUIListener listener) { |
264 |
actionCommandListeners.remove(listener); |
265 |
} |
266 |
} |