root / trunk / extensions / extgvSIGPiloto-Raster / src / com / iver / cit / gvsig / project / documents / raster / gui / dialogs / RasterFilterDialog.java @ 8999
History | View | Annotate | Download (11.2 KB)
1 |
/*
|
---|---|
2 |
* Created on 25-jul-2006
|
3 |
*
|
4 |
* To change the template for this generated file go to
|
5 |
* Window>Preferences>Java>Code Generation>Code and Comments
|
6 |
*/
|
7 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
8 |
*
|
9 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
10 |
*
|
11 |
* This program is free software; you can redistribute it and/or
|
12 |
* modify it under the terms of the GNU General Public License
|
13 |
* as published by the Free Software Foundation; either version 2
|
14 |
* of the License, or (at your option) any later version.
|
15 |
*
|
16 |
* This program is distributed in the hope that it will be useful,
|
17 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
18 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
19 |
* GNU General Public License for more details.
|
20 |
*
|
21 |
* You should have received a copy of the GNU General Public License
|
22 |
* along with this program; if not, write to the Free Software
|
23 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
24 |
*/
|
25 |
package com.iver.cit.gvsig.project.documents.raster.gui.dialogs; |
26 |
|
27 |
import java.awt.GridBagConstraints; |
28 |
import java.awt.GridBagLayout; |
29 |
import java.awt.event.ActionEvent; |
30 |
import java.awt.event.ActionListener; |
31 |
import java.awt.event.ComponentEvent; |
32 |
import java.awt.event.ComponentListener; |
33 |
|
34 |
import javax.swing.JButton; |
35 |
import javax.swing.JPanel; |
36 |
import javax.swing.ListModel; |
37 |
|
38 |
import org.cresques.filter.RasterFilter; |
39 |
import org.cresques.filter.RasterFilterStack; |
40 |
import org.cresques.filter.RasterFilterStackManager; |
41 |
import org.cresques.filter.RasterFilter.Kernel; |
42 |
import org.cresques.filter.convolution.ConvolutionStackManager; |
43 |
import org.cresques.filter.correction.MedianStackManager; |
44 |
import org.cresques.filter.segmentation.FirstDerivativeFilter; |
45 |
import org.cresques.filter.segmentation.FirstDerivativeStackManager; |
46 |
import org.cresques.ui.filter.FirstDerivativePanel; |
47 |
import org.cresques.ui.filter.KernelFiltersPanel; |
48 |
import org.gvsig.gui.beans.treelist.TreeListContainer; |
49 |
|
50 |
import com.iver.andami.PluginServices; |
51 |
import com.iver.andami.ui.mdiManager.IWindow; |
52 |
import com.iver.andami.ui.mdiManager.WindowInfo; |
53 |
import com.iver.cit.gvsig.fmap.layers.FLayer; |
54 |
import com.iver.cit.gvsig.gui.panels.RasterFilterPanel; |
55 |
|
56 |
/**
|
57 |
* Dialogo para los filtros de raster.
|
58 |
*
|
59 |
* @author Nacho Brodin <brodin_ign@gva.es>
|
60 |
*/
|
61 |
public class RasterFilterDialog extends JPanel implements IWindow, ActionListener, ComponentListener{ |
62 |
private final int MARGIN = 4; |
63 |
|
64 |
private int width = 0; |
65 |
private int height = 0; |
66 |
private RasterFilterPanel filterPanel = null; |
67 |
private JPanel pButtons = null; |
68 |
private JButton bClose = null; |
69 |
private RasterFilterStackManager stackManager = null; |
70 |
private FLayer fLayer = null; |
71 |
|
72 |
/**
|
73 |
* Constructor
|
74 |
* @param width Ancho del panel
|
75 |
* @param height Alto del panel
|
76 |
* @param stackManager Gestor de la pila de filtros
|
77 |
*/
|
78 |
public RasterFilterDialog(FLayer layer, int width, int height, RasterFilterStackManager stackManager){ |
79 |
this.width = width;
|
80 |
this.height = height;
|
81 |
this.fLayer = layer;
|
82 |
this.stackManager = stackManager;
|
83 |
|
84 |
GridBagConstraints gridBagConstraints = new GridBagConstraints(); |
85 |
gridBagConstraints.gridx = 0;
|
86 |
gridBagConstraints.gridy = 0;
|
87 |
this.setLayout(new GridBagLayout()); |
88 |
this.add(getPFilter(), gridBagConstraints);
|
89 |
this.addComponentListener(this); |
90 |
} |
91 |
|
92 |
/**
|
93 |
* Obtiene el panel de filtros
|
94 |
* @return HistogramPanel
|
95 |
*/
|
96 |
public RasterFilterPanel getPFilter(){
|
97 |
if(filterPanel == null){ |
98 |
filterPanel = new RasterFilterPanel(width, height);
|
99 |
filterPanel.getBAccept().addActionListener(this);
|
100 |
filterPanel.getBApply().addActionListener(this);
|
101 |
filterPanel.getBClose().addActionListener(this);
|
102 |
} |
103 |
return filterPanel;
|
104 |
} |
105 |
|
106 |
/**
|
107 |
* @see com.iver.mdiApp.ui.MDIManager.View#getViewInfo()
|
108 |
*/
|
109 |
public WindowInfo getWindowInfo() {
|
110 |
WindowInfo m_viewinfo=new WindowInfo(WindowInfo.MODALDIALOG /*| WindowInfo.RESIZABLE*/); |
111 |
m_viewinfo.setTitle(PluginServices.getText(this, "Filtros")); |
112 |
m_viewinfo.setWidth(width + MARGIN); |
113 |
m_viewinfo.setHeight(height + MARGIN); |
114 |
return m_viewinfo;
|
115 |
} |
116 |
|
117 |
/**
|
118 |
* Acciones realizadas al pulsar los botones de Aceptar, Aplicar o Cancelar del dialogo general
|
119 |
* de filtros.
|
120 |
*/
|
121 |
public void actionPerformed(ActionEvent e) { |
122 |
|
123 |
//Al pulsar Aceptar o aplicar se a?aden los filtros
|
124 |
if(e.getSource() == getPFilter().getBApply() || e.getSource() == getPFilter().getBAccept()){
|
125 |
RasterFilterStack stack = stackManager.getFilterStack(); |
126 |
|
127 |
// Quitamos lo filtros de la pila que se han eliminado de la lista
|
128 |
removeFiltersFromStack(stack); |
129 |
|
130 |
ListModel model = getSelector().getList().getModel();
|
131 |
for(int iListFilter = 0; iListFilter < model.getSize(); iListFilter ++){ |
132 |
String filterName = getSelector().getMap().get(model.getElementAt(iListFilter).toString()).toString();
|
133 |
addFilterByName(filterName); |
134 |
} |
135 |
|
136 |
fLayer.getMapContext().invalidate(); |
137 |
} |
138 |
|
139 |
//Al pulsar Cerrar o Aceptar la ventana se cierra
|
140 |
if(e.getSource() == getPFilter().getBClose() || e.getSource() == getPFilter().getBAccept()){
|
141 |
try{
|
142 |
PluginServices.getMDIManager().closeWindow(RasterFilterDialog.this); |
143 |
}catch(ArrayIndexOutOfBoundsException ex){ |
144 |
//Si la ventana no se puede eliminar no hacemos nada
|
145 |
} |
146 |
} |
147 |
|
148 |
} |
149 |
|
150 |
/**
|
151 |
* A?ade un filtro desde el dialogo en la pila de filtros a partir de los par?metros
|
152 |
* del panel.
|
153 |
* @param filterName Nombre del filtro.
|
154 |
*/
|
155 |
private void addFilterByName(String filterName){ |
156 |
if(filterName.equals("sobel")||filterName.equals("roberts")||filterName.equals("prewitt")||filterName.equals("frei_chen")){ |
157 |
//stackManager.removeFilter(FirstDerivativeStackManager.firstDerivative);
|
158 |
FirstDerivativePanel panel = (FirstDerivativePanel)RasterFilterPanel.getPanel(PluginServices.getText(this,filterName));
|
159 |
int umbral = 0; |
160 |
try{
|
161 |
if (panel.getCbUmbralizar().isSelected())
|
162 |
umbral = Integer.valueOf(panel.getTUmbral().getText()).intValue();
|
163 |
}catch(NumberFormatException ex){ |
164 |
//No se ha podido leer el umbral y no hacemos nada. Tendr? el valor por defecto
|
165 |
} |
166 |
boolean compare = panel.getCbUmbralizar().isSelected();
|
167 |
FirstDerivativeStackManager manager = (FirstDerivativeStackManager)stackManager.getManagerByClass(FirstDerivativeStackManager.class); |
168 |
|
169 |
int operator = -1; |
170 |
if (filterName.equals("sobel")) operator = FirstDerivativeFilter.TYPE_SOBEL; |
171 |
else if (filterName.equals("roberts"))operator = FirstDerivativeFilter.TYPE_ROBERTS; |
172 |
else if (filterName.equals("prewitt"))operator = FirstDerivativeFilter.TYPE_PREWITT; |
173 |
else if (filterName.equals("frei_chen"))operator = FirstDerivativeFilter.TYPE_FREICHEN; |
174 |
|
175 |
manager.addFirstDerivativeFilter(umbral, operator ,compare,filterName); |
176 |
|
177 |
}else if(filterName.equals("laplace")){ |
178 |
KernelFiltersPanel panel = (KernelFiltersPanel)RasterFilterPanel.getPanel(PluginServices.getText(this,filterName));
|
179 |
ConvolutionStackManager manager = (ConvolutionStackManager)stackManager.getManagerByClass(ConvolutionStackManager.class); |
180 |
double k[][] = null; |
181 |
double k3[][]= {{-1,-1,-1},{-1,8,-1},{-1,-1,-1}}; |
182 |
double k5[][]= {{0,0,-1,0,0},{0,-1,-2,-1,0},{-1,-2,16,-2,-1},{0,-1,-2,-1,0},{0,0,-1,0,0}}; |
183 |
switch(Integer.valueOf(panel.getCbKernel().getSelectedItem().toString()).intValue()){ |
184 |
case 3: |
185 |
k = k3; |
186 |
break;
|
187 |
case 5: |
188 |
k = k5; |
189 |
break;
|
190 |
} |
191 |
|
192 |
manager.addConvolutionFilter(new Kernel(k),0,filterName); |
193 |
|
194 |
}else if(filterName.equals("gauss")){ |
195 |
KernelFiltersPanel panel = (KernelFiltersPanel)RasterFilterPanel.getPanel(PluginServices.getText(this,filterName));
|
196 |
ConvolutionStackManager manager = (ConvolutionStackManager)stackManager.getManagerByClass(ConvolutionStackManager.class); |
197 |
double k[][] = null; |
198 |
double k3[][]= {{1,4,1},{4,12,4},{1,4,1}}; |
199 |
double k5[][]= {{1,2,3,2,1},{2,7,11,7,2},{3,11,17,11,3},{2,7,11,7,2},{1,2,3,2,1}}; |
200 |
switch(Integer.valueOf(panel.getCbKernel().getSelectedItem().toString()).intValue()){ |
201 |
case 3: |
202 |
k = k3; |
203 |
break;
|
204 |
case 5: |
205 |
k = k5; |
206 |
break;
|
207 |
} |
208 |
|
209 |
manager.addConvolutionFilter(new Kernel(k),0,filterName); |
210 |
|
211 |
}else if(filterName.equals("media")){ |
212 |
KernelFiltersPanel panel = (KernelFiltersPanel)RasterFilterPanel.getPanel(PluginServices.getText(this,filterName));
|
213 |
int ladoVentana = Integer.valueOf(panel.getCbKernel().getSelectedItem().toString()).intValue(); |
214 |
ConvolutionStackManager manager = (ConvolutionStackManager)stackManager.getManagerByClass(ConvolutionStackManager.class); |
215 |
|
216 |
double k[][]= new double[ladoVentana][ladoVentana]; |
217 |
for (int i=0; i<ladoVentana; i++) |
218 |
for (int j=0; j<ladoVentana; j++) |
219 |
k[i][j]=1;
|
220 |
manager.addConvolutionFilter(new Kernel(k),0,filterName); |
221 |
|
222 |
}else if(filterName.equals("mediana")){ |
223 |
KernelFiltersPanel panel = (KernelFiltersPanel)RasterFilterPanel.getPanel(PluginServices.getText(this,filterName));
|
224 |
MedianStackManager manager = (MedianStackManager)stackManager.getManagerByClass(MedianStackManager.class); |
225 |
manager.addMedianFilter(Integer.valueOf(panel.getCbKernel().getSelectedItem().toString()).intValue());
|
226 |
|
227 |
}else if(filterName.equals("paso_bajo")){ |
228 |
ConvolutionStackManager manager = (ConvolutionStackManager)stackManager.getManagerByClass(ConvolutionStackManager.class); |
229 |
double k[][]= {{0,1,0},{1,6,1},{0,1,0}}; |
230 |
manager.addConvolutionFilter(new Kernel(k),0,filterName); |
231 |
} |
232 |
|
233 |
} |
234 |
|
235 |
/**
|
236 |
*Quita los filtros de la pila que no est?n en la lista. Esto solo tiene efecto para aquellos filtros con nombre
|
237 |
*ya que son los filtros de usuario. Los filtros que no tienen nombre (filterName) no se visualizan en la lista por
|
238 |
*lo que no son eliminados de esta.
|
239 |
*@param stack Pila de filtros.
|
240 |
*/
|
241 |
private void removeFiltersFromStack(RasterFilterStack stack){ |
242 |
//Recorremos la pila de filtros y eliminamos los que est?n "registrados" en el interfaz de filtros.
|
243 |
for(int iFilter = 0; iFilter < stack.lenght(); iFilter ++){ |
244 |
RasterFilter rf = stack.get(iFilter); |
245 |
if(!rf.getFilterName().equals("") && getSelector().getMap().containsValue(rf.getFilterName())){ |
246 |
stack.removeFilter(rf); |
247 |
iFilter--; |
248 |
} |
249 |
} |
250 |
} |
251 |
|
252 |
/**
|
253 |
* Obtiene el panel con el selector de filtros
|
254 |
* @return TreeListContainer
|
255 |
*/
|
256 |
public TreeListContainer getSelector(){
|
257 |
return filterPanel.getPSelector();
|
258 |
} |
259 |
|
260 |
//-------------------------------------------
|
261 |
//M?TODOS DEL LISTENER
|
262 |
|
263 |
public void componentHidden(ComponentEvent e) { |
264 |
// TODO Auto-generated method stub
|
265 |
|
266 |
} |
267 |
|
268 |
public void componentMoved(ComponentEvent e) { |
269 |
// TODO Auto-generated method stub
|
270 |
|
271 |
} |
272 |
|
273 |
public void componentResized(ComponentEvent e) { |
274 |
this.width = this.getWidth(); |
275 |
this.height = this.getHeight(); |
276 |
getPFilter().setSizeComponent(width - MARGIN, height - MARGIN); |
277 |
} |
278 |
|
279 |
public void componentShown(ComponentEvent e) { |
280 |
// TODO Auto-generated method stub
|
281 |
} |
282 |
|
283 |
//-------------------------------------------
|
284 |
//M?TODOS DEL COMPONENTE
|
285 |
|
286 |
/**
|
287 |
* Asigna el panel visible en el dialogo
|
288 |
* @param pos Posici?n del panel
|
289 |
*/
|
290 |
public void setVisiblePanel(int pos){ |
291 |
getPFilter().setVisiblePanel(pos); |
292 |
} |
293 |
|
294 |
/**
|
295 |
* Obtiene el panel visible
|
296 |
* @return panel visible
|
297 |
*/
|
298 |
public JPanel getVisiblePanel(){ |
299 |
return getPFilter().getVisiblePanel();
|
300 |
} |
301 |
|
302 |
/**
|
303 |
* Obtiene el panel de la posici?n solicitada
|
304 |
* @param pos Posici?n del panel solicitado
|
305 |
* @return panel solicitado
|
306 |
*/
|
307 |
public JPanel getPanel(int pos){ |
308 |
return RasterFilterPanel.getPanel(pos);
|
309 |
} |
310 |
|
311 |
|
312 |
} |
313 |
|