Revision 36719 branches/v2_0_0_prep/applications/appgvSIG/src/org/gvsig/app/extension/FiltroExtension.java

View differences:

FiltroExtension.java
67 67

  
68 68
/**
69 69
 * Extensi?n que abre un di?logo para poder hacer un filtro de una capa o tabla.
70
 *
70
 * 
71 71
 * @author Vicente Caballero Navarro
72 72
 */
73 73
public class FiltroExtension extends Extension implements ExpressionListener {
74
	protected FeatureStore featureStore = null;
75
	private String filterTitle;
76 74

  
77
	/**
78
	 * DOCUMENT ME!
79
	 */
80
	public void initialize() {
81
		registerIcons();
82
	}
75
    protected FeatureStore featureStore = null;
76
    private String filterTitle;
83 77

  
84
	private void registerIcons(){
85
		PluginServices.getIconTheme().registerDefault(
86
				"table-filter",
87
				this.getClass().getClassLoader().getResource("images/Filtro.png")
88
		);
89
	}
78
    /**
79
     * DOCUMENT ME!
80
     */
81
    public void initialize() {
82
        registerIcons();
83
    }
90 84

  
91
	/**
92
	 * DOCUMENT ME!
93
	 *
94
	 * @param actionCommand DOCUMENT ME!
95
	 */
96
	public void execute(String actionCommand) {
97
		if ("FILTRO".equals(actionCommand)) {
98
//			try {
99
				IWindow v = PluginServices.getMDIManager().getActiveWindow();
85
    private void registerIcons() {
86
        PluginServices.getIconTheme().registerDefault("table-filter",
87
            this.getClass().getClassLoader().getResource("images/Filtro.png"));
88
    }
100 89

  
90
    /**
91
     * DOCUMENT ME!
92
     * 
93
     * @param actionCommand
94
     *            DOCUMENT ME!
95
     */
96
    public void execute(String actionCommand) {
97
        if ("FILTRO".equals(actionCommand)) {
98
            // try {
99
            IWindow v = PluginServices.getMDIManager().getActiveWindow();
100

  
101 101
            if (v instanceof org.gvsig.app.project.documents.view.gui.DefaultViewPanel) {
102
					ViewDocument pv = ((org.gvsig.app.project.documents.view.gui.DefaultViewPanel) v).getModel();
103
					filterTitle = ((org.gvsig.app.project.documents.view.gui.DefaultViewPanel) v).getModel().getName();
104
					FLayer layer = pv.getMapContext()
105
					.getLayers().getActives()[0];
106
					featureStore = ((FLyrVect)layer).getFeatureStore();//pv.getProject().getDataSourceByLayer(layer);
107
					((AbstractDocument)pv).setModified(true);
108
				}
109
//			}  catch (ReadException e) {
110
//				NotificationManager.addError("Error filtrando", e);
111
//			}
102
                ViewDocument pv =
103
                    ((org.gvsig.app.project.documents.view.gui.DefaultViewPanel) v)
104
                        .getModel();
105
                filterTitle =
106
                    ((org.gvsig.app.project.documents.view.gui.DefaultViewPanel) v)
107
                        .getModel().getName();
108
                FLayer layer = pv.getMapContext().getLayers().getActives()[0];
109
                featureStore = ((FLyrVect) layer).getFeatureStore();// pv.getProject().getDataSourceByLayer(layer);
110
                ((AbstractDocument) pv).setModified(true);
111
            }
112
            // } catch (ReadException e) {
113
            // NotificationManager.addError("Error filtrando", e);
114
            // }
112 115

  
113
			doExecute();
114
		}
115
	}
116
            doExecute();
117
        }
118
    }
116 119

  
117
	/**
118
	 * "execute" method action.
119
	 *
120
	 */
121
	protected void doExecute(){
122
		//		DefaultExpressionDataSource ds = new DefaultExpressionDataSource();
123
		//		ds.setTable(featureStore);
124
		FilterDialog dlg = new FilterDialog(filterTitle);
125
		dlg.addExpressionListener(this);
126
		dlg.addExceptionListener(new ExceptionListener() {
127
			public void exceptionThrown(Throwable t) {
128
				NotificationManager.addError(t.getMessage(), t);
129
			}
130
		});
131
		dlg.setModel(featureStore);
132
		PluginServices.getMDIManager().addWindow(dlg);
133
	}
120
    /**
121
     * "execute" method action.
122
     * 
123
     */
124
    protected void doExecute() {
125
        // DefaultExpressionDataSource ds = new DefaultExpressionDataSource();
126
        // ds.setTable(featureStore);
127
        FilterDialog dlg = new FilterDialog(filterTitle);
128
        dlg.addExpressionListener(this);
129
        dlg.addExceptionListener(new ExceptionListener() {
134 130

  
135
	/**
136
	 * DOCUMENT ME!
137
	 *
138
	 * @return DOCUMENT ME!
139
	 */
140
	public boolean isEnabled() {
141
		IWindow v = PluginServices.getMDIManager().getActiveWindow();
131
            public void exceptionThrown(Throwable t) {
132
                NotificationManager.addError(t.getMessage(), t);
133
            }
134
        });
135
        dlg.setModel(featureStore);
136
        PluginServices.getMDIManager().addWindow(dlg);
137
    }
142 138

  
139
    /**
140
     * DOCUMENT ME!
141
     * 
142
     * @return DOCUMENT ME!
143
     */
144
    public boolean isEnabled() {
145
        IWindow v = PluginServices.getMDIManager().getActiveWindow();
146

  
143 147
        if (v == null) {
144 148
            return false;
145 149
        }
......
153 157

  
154 158
            if (seleccionadas.length == 1) {
155 159
                if (seleccionadas[0].isAvailable()
156
                    && seleccionadas[0] instanceof FLyrVect) {
160
                    && (seleccionadas[0] instanceof FLyrVect)) {
157 161
                    return true;
158 162
                }
159 163
            }
160 164
        }
161 165
        return false;
162
	}
166
    }
163 167

  
164
	/**
165
	 * DOCUMENT ME!
166
	 *
167
	 * @return DOCUMENT ME!
168
	 */
169
	public boolean isVisible() {
168
    /**
169
     * DOCUMENT ME!
170
     * 
171
     * @return DOCUMENT ME!
172
     */
173
    public boolean isVisible() {
170 174
        IWindow v = PluginServices.getMDIManager().getActiveWindow();
171 175

  
172 176
        if (v == null) {
......
188 192
        }
189 193

  
190 194
        return false;
191
	}
195
    }
192 196

  
193
	/**
194
	 * DOCUMENT ME!
195
	 *
196
	 * @param expression DOCUMENT ME!
197
	 */
198
	// By Pablo: if no filter expression -> no element selected
199
	public void newSet(String expression) throws DataException {
200
		if (! this.filterExpressionFromWhereIsEmpty(expression)) {
201
			FeatureSet set = null;
202
			try {
203
				set = doSet(expression);
197
    /**
198
     * DOCUMENT ME!
199
     * 
200
     * @param expression
201
     *            DOCUMENT ME!
202
     */
203
    // By Pablo: if no filter expression -> no element selected
204
    public void newSet(String expression) throws DataException {
205
        if (!this.filterExpressionFromWhereIsEmpty(expression)) {
206
            FeatureSet set = null;
207
            try {
208
                set = doSet(expression);
204 209

  
205
				if (set == null) {
206
					//throw new RuntimeException("Not a 'where' clause?");
207
					return;
208
				}
209
				featureStore.setSelection(set);
210
                if (set == null) {
211
                    // throw new RuntimeException("Not a 'where' clause?");
212
                    return;
213
                }
214
                featureStore.setSelection(set);
210 215

  
211
			}catch(Exception e){
212
				JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(), "Asegurate de que la consulta es correcta.");
213
			}
214
			finally {
215
				if (set != null) {
216
					set.dispose();
217
				}
218
			}
219
		}
220
		else {
221
			// By Pablo: if no expression -> no element selected
222
			featureStore.getFeatureSelection().deselectAll();
223
		}
224
	}
216
            } catch (Exception e) {
217
                JOptionPane.showMessageDialog((Component) PluginServices
218
                    .getMainFrame(),
219
                    "Asegurate de que la consulta es correcta.");
220
            } finally {
221
                if (set != null) {
222
                    set.dispose();
223
                }
224
            }
225
        } else {
226
            // By Pablo: if no expression -> no element selected
227
            featureStore.getFeatureSelection().deselectAll();
228
        }
229
    }
225 230

  
226
	/**
227
	 * @throws DataException
228
	 * @see org.gvsig.app.gui.filter.ExpressionListener#newSet(java.lang.String)
229
	 */
230
	private FeatureSet doSet(String expression) throws DataException {
231
		FeatureQuery query = featureStore.createFeatureQuery();
232
		DataManager manager = DALLocator.getDataManager();
233
		query.setFilter(manager.createExpresion(expression));
234
		return featureStore.getFeatureSet(query);
235
		//		try {
236
		//			DataSource ds = LayerFactory.getDataSourceFactory().executeSQL(expression,
237
		//					DataSourceFactory.MANUAL_OPENING);
238
		//
239
		//			return ds.getWhereFilter();
240
		//		} catch (DriverLoadException e) {
241
		//			JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"driver_error")+"\n"+e.getMessage());
242
		//		} catch (ReadDriverException e) {
243
		//			JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"driver_error")+"\n"+e.getMessage());
244
		//		} catch (ParseException e) {
245
		//			JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"parse_expresion_error")+"\n"+e.getMessage());
246
		//		} catch (SemanticException e) {
247
		//			JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"semantic_expresion_error")+"\n"+e.getMessage());
248
		//		} catch (IOException e) {
249
		//			JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"input_output_error")+"\n"+e.getMessage());
250
		//		} catch (EvaluationException e) {
251
		//			JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"parse_expresion_error")+"\n"+e.getMessage());
252
		//		} catch (com.hardcode.gdbms.parser.TokenMgrError e) {
253
		//			JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"expresion_error")+"\n"+e.getMessage());
254
		//		}
255
		//		return null;
256
	}
231
    /**
232
     * @throws DataException
233
     * @see org.gvsig.app.gui.filter.ExpressionListener#newSet(java.lang.String)
234
     */
235
    private FeatureSet doSet(String expression) throws DataException {
236
        FeatureQuery query = featureStore.createFeatureQuery();
237
        DataManager manager = DALLocator.getDataManager();
238
        query.setFilter(manager.createExpresion(expression));
239
        return featureStore.getFeatureSet(query);
240
        // try {
241
        // DataSource ds =
242
        // LayerFactory.getDataSourceFactory().executeSQL(expression,
243
        // DataSourceFactory.MANUAL_OPENING);
244
        //
245
        // return ds.getWhereFilter();
246
        // } catch (DriverLoadException e) {
247
        // JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"driver_error")+"\n"+e.getMessage());
248
        // } catch (ReadDriverException e) {
249
        // JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"driver_error")+"\n"+e.getMessage());
250
        // } catch (ParseException e) {
251
        // JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"parse_expresion_error")+"\n"+e.getMessage());
252
        // } catch (SemanticException e) {
253
        // JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"semantic_expresion_error")+"\n"+e.getMessage());
254
        // } catch (IOException e) {
255
        // JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"input_output_error")+"\n"+e.getMessage());
256
        // } catch (EvaluationException e) {
257
        // JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"parse_expresion_error")+"\n"+e.getMessage());
258
        // } catch (com.hardcode.gdbms.parser.TokenMgrError e) {
259
        // JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"expresion_error")+"\n"+e.getMessage());
260
        // }
261
        // return null;
262
    }
257 263

  
258
	/**
259
	 * DOCUMENT ME!
260
	 *
261
	 * @param expression
262
	 *            DOCUMENT ME!
263
	 * @throws DataException
264
	 */
265
	public void addToSet(String expression) throws DataException {
266
		// By Pablo: if no filter expression -> don't add more elements to set
267
		if (! this.filterExpressionFromWhereIsEmpty(expression)) {
268
			FeatureSet set = null;
269
			try {
270
				set = doSet(expression);
264
    /**
265
     * DOCUMENT ME!
266
     * 
267
     * @param expression
268
     *            DOCUMENT ME!
269
     * @throws DataException
270
     */
271
    public void addToSet(String expression) throws DataException {
272
        // By Pablo: if no filter expression -> don't add more elements to set
273
        if (!this.filterExpressionFromWhereIsEmpty(expression)) {
274
            FeatureSet set = null;
275
            try {
276
                set = doSet(expression);
271 277

  
272
				if (set == null) {
273
					// throw new RuntimeException("Not a 'where' clause?");
274
					return;
275
				}
276
				featureStore.getFeatureSelection().select(set);
277
			} finally {
278
				if (set != null) {
279
					set.dispose();
280
				}
281
			}
278
                if (set == null) {
279
                    // throw new RuntimeException("Not a 'where' clause?");
280
                    return;
281
                }
282
                featureStore.getFeatureSelection().select(set);
283
            } finally {
284
                if (set != null) {
285
                    set.dispose();
286
                }
287
            }
282 288

  
283
			//			FBitSet selection = new FBitSet();
284
			//
285
			//			for (int i = 0; i < sel.length; i++) {
286
			//				selection.set((int) sel[i]);
287
			//			}
288
			//
289
			//			FBitSet fbs = featureStore.getSelection();
290
			//			fbs.or(selection);
291
			//			featureStore.setSelection(fbs);
292
		}
293
	}
289
            // FBitSet selection = new FBitSet();
290
            //
291
            // for (int i = 0; i < sel.length; i++) {
292
            // selection.set((int) sel[i]);
293
            // }
294
            //
295
            // FBitSet fbs = featureStore.getSelection();
296
            // fbs.or(selection);
297
            // featureStore.setSelection(fbs);
298
        }
299
    }
294 300

  
295
	/**
296
	 * DOCUMENT ME!
297
	 *
298
	 * @param expression DOCUMENT ME!
299
	 */
300
	public void fromSet(String expression) throws DataException {
301
		// By Pablo: if no filter expression -> no element selected
302
		try{
303
			if (! this.filterExpressionFromWhereIsEmpty(expression)) {
304
//				NotificationManager.showMessageInfo("Falta por implementar",
305
//						null);
301
    /**
302
     * DOCUMENT ME!
303
     * 
304
     * @param expression
305
     *            DOCUMENT ME!
306
     */
307
    public void fromSet(String expression) throws DataException {
308
        // By Pablo: if no filter expression -> no element selected
309
        try {
310
            if (!this.filterExpressionFromWhereIsEmpty(expression)) {
311
                // NotificationManager.showMessageInfo("Falta por implementar",
312
                // null);
306 313

  
307 314
                FeatureSet set = null;
308 315
                set = doSet(expression);
......
326 333
                featureStore.setSelection(newSelection);
327 334
                set.dispose();
328 335

  
329
			} else {
330
				// By Pablo: if no expression -> no element selected
331
				featureStore.getFeatureSelection().deselectAll();
332
				;
333
			}
334
		} catch (DataException e) {
335
			NotificationManager.addError(e);
336
		}
336
            } else {
337
                // By Pablo: if no expression -> no element selected
338
                featureStore.getFeatureSelection().deselectAll();
339
                ;
340
            }
341
        } catch (DataException e) {
342
            NotificationManager.addError(e);
343
        }
337 344

  
338
	}
345
    }
339 346

  
340
	/**
341
	 * Returns true if the WHERE subconsultation of the filterExpression is empty ("")
342
	 *
343
	 * @author Pablo Piqueras Bartolom? (p_queras@hotmail.com)
344
	 * @param expression An string
345
	 * @return A boolean value
346
	 */
347
	private boolean filterExpressionFromWhereIsEmpty(String expression) {
348
		String subExpression = expression.trim();
349
		int pos;
347
    /**
348
     * Returns true if the WHERE subconsultation of the filterExpression is
349
     * empty ("")
350
     * 
351
     * @author Pablo Piqueras Bartolom? (p_queras@hotmail.com)
352
     * @param expression
353
     *            An string
354
     * @return A boolean value
355
     */
356
    private boolean filterExpressionFromWhereIsEmpty(String expression) {
357
        String subExpression = expression.trim();
358
        int pos;
350 359

  
351
		// Remove last ';' if exists
352
		if (subExpression.charAt(subExpression.length() -1) == ';') {
353
			subExpression = subExpression.substring(0, subExpression.length() -1).trim();
354
		}
360
        // Remove last ';' if exists
361
        if (subExpression.charAt(subExpression.length() - 1) == ';') {
362
            subExpression =
363
                subExpression.substring(0, subExpression.length() - 1).trim();
364
        }
355 365

  
356
		// If there is no 'where' clause
357
		if ((pos = subExpression.indexOf("where")) == -1) {
358
			return false;
359
		}
366
        // If there is no 'where' clause
367
        if ((pos = subExpression.indexOf("where")) == -1) {
368
            return false;
369
        }
360 370

  
361
		// If there is no subexpression in the WHERE clause -> true
362
		subExpression = subExpression.substring(pos + 5, subExpression.length()).trim(); // + 5 is the length of 'where'
363
		if ( subExpression.length() == 0 ) {
364
			return true;
365
		} else {
366
			return false;
367
		}
368
	}
371
        // If there is no subexpression in the WHERE clause -> true
372
        subExpression =
373
            subExpression.substring(pos + 5, subExpression.length()).trim(); // +
374
        // 5
375
        // is
376
        // the
377
        // length
378
        // of
379
        // 'where'
380
        if (subExpression.length() == 0) {
381
            return true;
382
        } else {
383
            return false;
384
        }
385
    }
369 386
}

Also available in: Unified diff