32 |
32 |
import java.util.Comparator;
|
33 |
33 |
import java.util.Date;
|
34 |
34 |
import java.util.Iterator;
|
|
35 |
import java.util.List;
|
35 |
36 |
import java.util.TreeSet;
|
36 |
37 |
import java.util.regex.Matcher;
|
37 |
38 |
import java.util.regex.Pattern;
|
|
39 |
import javax.swing.JOptionPane;
|
38 |
40 |
|
39 |
41 |
import javax.swing.tree.DefaultMutableTreeNode;
|
40 |
42 |
import javax.swing.tree.DefaultTreeModel;
|
|
43 |
import org.apache.commons.lang.StringEscapeUtils;
|
|
44 |
import org.apache.commons.lang3.StringUtils;
|
41 |
45 |
|
42 |
46 |
import org.slf4j.Logger;
|
43 |
47 |
import org.slf4j.LoggerFactory;
|
44 |
48 |
|
45 |
49 |
import org.gvsig.andami.PluginServices;
|
46 |
|
import org.gvsig.andami.messages.NotificationManager;
|
47 |
50 |
import org.gvsig.andami.ui.mdiManager.IWindow;
|
48 |
51 |
import org.gvsig.andami.ui.mdiManager.IWindowListener;
|
49 |
52 |
import org.gvsig.andami.ui.mdiManager.WindowInfo;
|
|
53 |
import org.gvsig.app.ApplicationLocator;
|
|
54 |
import org.gvsig.app.ApplicationManager;
|
50 |
55 |
import org.gvsig.fmap.dal.exception.DataException;
|
51 |
56 |
import org.gvsig.fmap.dal.feature.Feature;
|
52 |
57 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
|
... | ... | |
54 |
59 |
import org.gvsig.fmap.dal.feature.FeatureSet;
|
55 |
60 |
import org.gvsig.fmap.dal.feature.FeatureStore;
|
56 |
61 |
import org.gvsig.gui.beans.filterPanel.tableFilterQueryPanel.TableFilterQueryJPanel;
|
|
62 |
import org.gvsig.tools.ToolsLocator;
|
57 |
63 |
import org.gvsig.tools.dispose.DisposableIterator;
|
58 |
64 |
import org.gvsig.tools.dispose.DisposeUtils;
|
|
65 |
import org.gvsig.tools.i18n.I18nManager;
|
59 |
66 |
import org.gvsig.utils.DefaultCharSet;
|
60 |
67 |
import org.gvsig.utils.StringUtilities;
|
61 |
68 |
import org.gvsig.utils.exceptionHandling.ExceptionHandlingSupport;
|
... | ... | |
76 |
83 |
*
|
77 |
84 |
*/
|
78 |
85 |
private static final long serialVersionUID = -149317534873551735L;
|
79 |
|
private static final Logger logger =
|
80 |
|
LoggerFactory.getLogger(FilterDialog.class);
|
|
86 |
private static final Logger logger = LoggerFactory.getLogger(FilterDialog.class);
|
|
87 |
|
81 |
88 |
private FeatureStore model = null;
|
82 |
|
private ArrayList expressionListeners = new ArrayList();
|
83 |
|
private ExceptionHandlingSupport exceptionHandlingSupport =
|
84 |
|
new ExceptionHandlingSupport();
|
85 |
|
private NumberFormat nf = NumberFormat.getNumberInstance();
|
|
89 |
|
|
90 |
private final List<ExpressionListener> expressionListeners = new ArrayList<>();
|
|
91 |
private final ExceptionHandlingSupport exceptionHandlingSupport = new ExceptionHandlingSupport();
|
|
92 |
private final NumberFormat nf = NumberFormat.getNumberInstance();
|
86 |
93 |
|
87 |
94 |
private String title;
|
88 |
95 |
|
... | ... | |
96 |
103 |
|
97 |
104 |
/**
|
98 |
105 |
* This is the default constructor
|
|
106 |
* @param _title
|
99 |
107 |
*/
|
100 |
108 |
public FilterDialog(String _title) {
|
101 |
109 |
super();
|
... | ... | |
134 |
142 |
// Listener for "btnAdd"
|
135 |
143 |
// Adds more elements to the current set
|
136 |
144 |
getBtnAddToCurrentSet().addActionListener(
|
137 |
|
new java.awt.event.ActionListener() {
|
|
145 |
new java.awt.event.ActionListener() {
|
138 |
146 |
|
139 |
|
/*
|
140 |
|
* (non-Javadoc)
|
141 |
|
*
|
142 |
|
* @see
|
143 |
|
* java.awt.event.ActionListener#actionPerformed(java.awt.event
|
144 |
|
* .ActionEvent)
|
145 |
|
*/
|
146 |
|
public void actionPerformed(java.awt.event.ActionEvent e) {
|
147 |
|
// final String expr = "select * from '" +
|
148 |
|
// model.getName() + "' where " +
|
149 |
|
// getTxtExpression().getText() + ";";
|
150 |
|
final String expr = getTxtExpression().getText();
|
151 |
|
logger.debug(expr);
|
|
147 |
@Override
|
|
148 |
public void actionPerformed(java.awt.event.ActionEvent e) {
|
|
149 |
final String expr = getTxtExpression().getText();
|
|
150 |
if (!validateQuotes(expr) || !validateLike(expr)) {
|
|
151 |
return;
|
|
152 |
}
|
152 |
153 |
|
153 |
|
PluginServices.backgroundExecution(new Runnable() {
|
|
154 |
PluginServices.backgroundExecution(new Runnable() {
|
154 |
155 |
|
155 |
|
public void run() {
|
156 |
|
for (int i = 0; i < expressionListeners.size(); i++) {
|
157 |
|
ExpressionListener l =
|
158 |
|
(ExpressionListener) expressionListeners
|
159 |
|
.get(i);
|
160 |
|
try {
|
161 |
|
l.addToSet(expr);
|
162 |
|
} catch (DataException e) {
|
163 |
|
e.printStackTrace();
|
|
156 |
@Override
|
|
157 |
public void run() {
|
|
158 |
for (ExpressionListener l : expressionListeners) {
|
|
159 |
try {
|
|
160 |
l.addToSet(expr);
|
|
161 |
} catch (DataException e) {
|
|
162 |
logger.warn("Can't add expression '"+expr+"' to set.", e);
|
|
163 |
}
|
164 |
164 |
}
|
165 |
165 |
}
|
166 |
|
}
|
167 |
|
});
|
168 |
|
}
|
169 |
|
});
|
|
166 |
});
|
|
167 |
}
|
|
168 |
});
|
170 |
169 |
|
171 |
170 |
// Listener for "btnNuevo"
|
172 |
171 |
// Adds a new set
|
173 |
172 |
getBtnNewSet().addActionListener(new java.awt.event.ActionListener() {
|
174 |
173 |
|
|
174 |
@Override
|
175 |
175 |
public void actionPerformed(java.awt.event.ActionEvent e) {
|
176 |
|
// final String expr = "select * from '" +
|
177 |
|
// model.getName() + "' where " +
|
178 |
|
// getTxtExpression().getText() + ";";
|
179 |
176 |
final String expr = getTxtExpression().getText();
|
|
177 |
if (!validateQuotes(expr) || !validateLike(expr)) {
|
|
178 |
return;
|
|
179 |
}
|
180 |
180 |
|
181 |
|
logger.debug(expr);
|
182 |
|
|
183 |
181 |
PluginServices.backgroundExecution(new Runnable() {
|
184 |
182 |
|
|
183 |
@Override
|
185 |
184 |
public void run() {
|
186 |
|
for (int i = 0; i < expressionListeners.size(); i++) {
|
187 |
|
ExpressionListener l =
|
188 |
|
(ExpressionListener) expressionListeners.get(i);
|
|
185 |
for (ExpressionListener l : expressionListeners) {
|
189 |
186 |
try {
|
190 |
187 |
l.newSet(expr);
|
191 |
188 |
} catch (DataException e) {
|
192 |
|
// TODO Auto-generated catch block
|
193 |
|
NotificationManager.addError(e);
|
|
189 |
logger.warn("Can't create set with expression '"+expr+"'.", e);
|
194 |
190 |
}
|
195 |
191 |
}
|
196 |
192 |
}
|
... | ... | |
202 |
198 |
// Selects elements from the current filtered selection
|
203 |
199 |
getBtnFromSet().addActionListener(new java.awt.event.ActionListener() {
|
204 |
200 |
|
|
201 |
@Override
|
205 |
202 |
public void actionPerformed(java.awt.event.ActionEvent e) {
|
206 |
|
// final String expr = "select * from '" +
|
207 |
|
// model.getName() + "' where " +
|
208 |
|
// getTxtExpression().getText() + ";";
|
209 |
203 |
final String expr = getTxtExpression().getText();
|
210 |
|
logger.debug(expr);
|
|
204 |
if (!validateQuotes(expr) || !validateLike(expr) ) {
|
|
205 |
return;
|
|
206 |
}
|
211 |
207 |
|
212 |
208 |
PluginServices.backgroundExecution(new Runnable() {
|
213 |
209 |
|
|
210 |
@Override
|
214 |
211 |
public void run() {
|
215 |
|
for (int i = 0; i < expressionListeners.size(); i++) {
|
216 |
|
ExpressionListener l =
|
217 |
|
(ExpressionListener) expressionListeners.get(i);
|
|
212 |
for (ExpressionListener l : expressionListeners) {
|
218 |
213 |
try {
|
219 |
214 |
l.fromSet(expr);
|
220 |
215 |
} catch (DataException e) {
|
221 |
|
NotificationManager.addError(e);
|
|
216 |
logger.warn("Can't create expression '"+expr+"' from set.", e);
|
222 |
217 |
}
|
223 |
218 |
}
|
224 |
219 |
}
|
... | ... | |
229 |
224 |
// Listener for "fieldsJTree"
|
230 |
225 |
getFieldsJTree().addMouseListener(new MouseAdapter() {
|
231 |
226 |
|
232 |
|
/*
|
233 |
|
* (non-Javadoc)
|
234 |
|
*
|
235 |
|
* @see
|
236 |
|
* java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent
|
237 |
|
* )
|
238 |
|
*/
|
239 |
227 |
@Override
|
240 |
228 |
public void mouseClicked(MouseEvent e) {
|
241 |
229 |
int row = fieldsJTree.getRowForLocation(e.getX(), e.getY());
|
242 |
|
|
243 |
230 |
if (row > -1) {
|
244 |
231 |
switch (e.getClickCount()) {
|
245 |
|
case 1:
|
246 |
|
fillValues(row);
|
247 |
|
break;
|
248 |
|
case 2:
|
249 |
|
putSymbol(jtreeRoot.getChildAt(row).toString());
|
250 |
|
break;
|
|
232 |
case 1:
|
|
233 |
fillValues(row);
|
|
234 |
break;
|
|
235 |
case 2:
|
|
236 |
putSymbol(jtreeRoot.getChildAt(row).toString());
|
|
237 |
break;
|
251 |
238 |
}
|
252 |
239 |
}
|
253 |
240 |
}
|
... | ... | |
256 |
243 |
// Listener for "valuesJList"
|
257 |
244 |
getValuesJList().addMouseListener(new MouseAdapter() {
|
258 |
245 |
|
259 |
|
/*
|
260 |
|
* (non-Javadoc)
|
261 |
|
*
|
262 |
|
* @see
|
263 |
|
* java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent
|
264 |
|
* )
|
265 |
|
*/
|
266 |
246 |
@Override
|
267 |
247 |
public void mouseClicked(MouseEvent e) {
|
268 |
248 |
if (e.getClickCount() == 2) {
|
269 |
|
Object valor =
|
270 |
|
valuesListModel.getElementAt(getValuesJList()
|
|
249 |
Object valor = valuesListModel.getElementAt(getValuesJList()
|
271 |
250 |
.getSelectedIndex());
|
272 |
|
|
273 |
|
if (valor instanceof Date) {
|
|
251 |
if (valor == null) {
|
|
252 |
putSymbol("null");
|
|
253 |
} else if (valor instanceof Date) {
|
274 |
254 |
putSymbol("date('" + valor + "')");
|
275 |
|
} else
|
276 |
|
if (valor instanceof Boolean) {
|
277 |
|
putSymbol(valor.toString());
|
278 |
|
} else
|
279 |
|
if (valor instanceof String) {
|
280 |
|
putSymbol("'" + valor + "'");
|
281 |
|
} else {
|
282 |
|
putSymbol(valor.toString());
|
283 |
|
}
|
|
255 |
} else if (valor instanceof Boolean) {
|
|
256 |
putSymbol(valor.toString());
|
|
257 |
} else if (valor instanceof String) {
|
|
258 |
// putSymbol("'" + StringEscapeUtils.escapeSql((String) valor) + "'");
|
|
259 |
putSymbol("'" + ((String)valor).replace('\'', '?') + "'");
|
|
260 |
} else {
|
|
261 |
putSymbol(StringEscapeUtils.escapeSql(valor.toString()));
|
|
262 |
}
|
284 |
263 |
}
|
285 |
264 |
}
|
286 |
265 |
});
|
287 |
266 |
}
|
288 |
267 |
|
|
268 |
private boolean validateQuotes(String expr) {
|
|
269 |
if ((StringUtils.countMatches(expr, "'") % 2) != 0) {
|
|
270 |
ApplicationManager application = ApplicationLocator.getManager();
|
|
271 |
I18nManager i18n = ToolsLocator.getI18nManager();
|
|
272 |
String msg = i18n.getTranslation("_The_number_of_quotes_is_doubtful_It_could_be_that_some_of_them_should_escape_Do_you_want_to_continue_the_operation_anyway");
|
|
273 |
int resp = application.confirmDialog(
|
|
274 |
msg,
|
|
275 |
i18n.getTranslation("_Caution"),
|
|
276 |
JOptionPane.YES_NO_OPTION,
|
|
277 |
JOptionPane.QUESTION_MESSAGE,
|
|
278 |
"Doubtless number of quotes in the filter dialog"
|
|
279 |
);
|
|
280 |
if (resp == JOptionPane.NO_OPTION) {
|
|
281 |
return false;
|
|
282 |
}
|
|
283 |
}
|
|
284 |
return true;
|
|
285 |
|
|
286 |
}
|
|
287 |
|
|
288 |
private boolean validateLike(String expr) {
|
|
289 |
if( (expr.toLowerCase().contains("?") ||
|
|
290 |
expr.toLowerCase().contains("%")) &&
|
|
291 |
!expr.toLowerCase().contains("like") )
|
|
292 |
{
|
|
293 |
ApplicationManager application = ApplicationLocator.getManager();
|
|
294 |
I18nManager i18n = ToolsLocator.getI18nManager();
|
|
295 |
String msg = i18n.getTranslation("_It_looks_like_youre_using_percent_o_question_and_the_like_operator_is_not_being_used_I_should_probably_keep_the_equal_operator_by_like_Do_you_want_to_continue_anyway");
|
|
296 |
int resp = application.confirmDialog(
|
|
297 |
msg,
|
|
298 |
i18n.getTranslation("_Caution"),
|
|
299 |
JOptionPane.YES_NO_OPTION,
|
|
300 |
JOptionPane.QUESTION_MESSAGE,
|
|
301 |
"You may need the like operator in the filter dialog"
|
|
302 |
);
|
|
303 |
if (resp == JOptionPane.NO_OPTION) {
|
|
304 |
return false;
|
|
305 |
}
|
|
306 |
}
|
|
307 |
return true;
|
|
308 |
|
|
309 |
}
|
289 |
310 |
/**
|
290 |
311 |
* Rellena la lista con los valores del campo seleccionado
|
291 |
312 |
*/
|