root / branches / v2_0_0_prep / extensions / extExpressionField / src / org / gvsig / expressionfield / project / documents / table / operators / DateToString.java @ 29628
History | View | Annotate | Download (2.94 KB)
1 |
package org.gvsig.expressionfield.project.documents.table.operators; |
---|---|
2 |
|
3 |
import org.apache.bsf.BSFException; |
4 |
import org.apache.bsf.BSFManager; |
5 |
import org.gvsig.andami.PluginServices; |
6 |
import org.gvsig.expressionfield.ExpressionFieldExtension; |
7 |
import org.gvsig.expressionfield.project.documents.table.AbstractOperator; |
8 |
import org.gvsig.expressionfield.project.documents.table.IOperator; |
9 |
|
10 |
|
11 |
/**
|
12 |
* @author fdiaz
|
13 |
*/
|
14 |
public class DateToString extends AbstractOperator{ |
15 |
|
16 |
public String addText(String s) { |
17 |
return toString()+"("+s+")"; |
18 |
} |
19 |
public String toString() { |
20 |
return "dateToString"; |
21 |
} |
22 |
public void eval(BSFManager interpreter) throws BSFException { |
23 |
// interpreter.eval(ExpressionFieldExtension.BEANSHELL,null,-1,-1,"String toString(java.lang.Object value){" +
|
24 |
// "if (value instanceof java.util.Date)" +
|
25 |
// "return ((java.util.Date)value).toString();" +
|
26 |
// "return String.valueOf(value);};");
|
27 |
interpreter.exec(ExpressionFieldExtension.JYTHON,null,-1,-1, |
28 |
"import java.util.Date as Date\n"+
|
29 |
"import java.text.DateFormat as DateFormat\n"+
|
30 |
"import java.text.SimpleDateFormat as SimpleDateFormat\n" +
|
31 |
"import java.text.NumberFormat as NumberFormat\n"+
|
32 |
"dateFormat = DateFormat.getInstance()\n"+
|
33 |
"myDateFormat = SimpleDateFormat()\n" +
|
34 |
"def dateToString(value, format=None):\n" +
|
35 |
" if value == None:\n"+
|
36 |
" return None\n"+
|
37 |
" if value == '':\n"+
|
38 |
" return ''\n"+
|
39 |
" if isinstance(value,Date):\n"+
|
40 |
" if format != None:\n"+
|
41 |
" myDateFormat.applyPattern(format)\n"+
|
42 |
" return myDateFormat.format(value)\n"+
|
43 |
" else:\n"+
|
44 |
" return dateFormat.format(value)\n"+
|
45 |
" else:\n"+
|
46 |
" raise InputError\n"+
|
47 |
" return str(value)");
|
48 |
} |
49 |
public boolean isEnable() { |
50 |
return (getType()==IOperator.DATE);
|
51 |
} |
52 |
|
53 |
public String getTooltip(){ |
54 |
return PluginServices.getText(this,"operator")+": "+toString()+"("+PluginServices.getText(this,"parameter")+"[,"+PluginServices.getText(this,"format")+"])"+"\n"+getDescription(); |
55 |
} |
56 |
|
57 |
public String getDescription() { |
58 |
return PluginServices.getText(this, "parameter") + ": " + |
59 |
PluginServices.getText(this, "date_value") + "\n" + |
60 |
PluginServices.getText(this, "format") + " ("+PluginServices.getText(this, "optional")+"): " + |
61 |
PluginServices.getText(this, "string_value") + "\n" + |
62 |
PluginServices.getText(this, "returns") + ": " + |
63 |
PluginServices.getText(this, "string_value") + "\n" + |
64 |
PluginServices.getText(this, "description") + ": " + |
65 |
"Returns the string representation of the Object date parameter\n" +
|
66 |
"formatted according to the parameter format, if it is supplied.\n\n"+
|
67 |
"The format should follow the specifications of\n" +
|
68 |
"'http://java.sun.com/j2se/1.4.2/docs/api/java/text/SimpleDateFormat.html'.\n\n"+
|
69 |
"Examples: (for the date of December 23, 2008)\n"+
|
70 |
" 'dd/MM/yy' 23/12/08\n"+
|
71 |
" 'dd/MM/yyyy' 23/12/2008\n"+
|
72 |
" 'dd/MMM/yyyy' 23/dec/2008\n"+
|
73 |
" 'dd/MMMM/yyyy' 23/december/2008\n";
|
74 |
} |
75 |
} |