Purpose: change the value of the selected node.
Solution:
Object selectedNode = tree.getValue(); tree.setItemCaption(selectedNode, "new value");
Make good use of it :)
Object selectedNode = tree.getValue(); tree.setItemCaption(selectedNode, "new value");
GregorianCalendar cal = new GregorianCalendar(); cal.setTime(new Date()); int numDays = cal.getActualMaximum(Calendar.DAY_OF_MONTH); int month = cal.get(Calendar.MONTH); int year = cal.get(Calendar.YEAR);
for (int date = 1; date <= numDays; date++) { cal.set(year, month, date); String dayName = dayName(cal.get(Calendar.DAY_OF_WEEK)); System.out.println(date + "-" + month + "-" + year + " " + dayName); } String dayName(int day) { switch (day) { case Calendar.MONDAY: return "Monday"; case Calendar.TUESDAY: return "Tuesday"; case Calendar.WEDNESDAY:return "Wednesday"; case Calendar.THURSDAY: return "Thursday"; case Calendar.FRIDAY: return "Friday"; case Calendar.SATURDAY: return "Saturday"; case Calendar.SUNDAY: return "Sunday"; default: return ""; } }
1-6-2013 Monday 2-6-2013 Tuesday 3-6-2013 Wednesday 4-6-2013 Thursday 5-6-2013 Friday 6-6-2013 Saturday 7-6-2013 Sunday 8-6-2013 Monday 9-6-2013 Tuesday 10-6-2013 Wednesday .......
String[] newArray = new String[firstArray.length + secondArray.length]; int index = 0; for(int i = 0; i < firstArray.length: i++){ newArray[index++] = firstArray[i]; } for(int i = 0; i < secondArray.length: i++){ newArray[index++] = secondArray[i]; }This code is simple but slow, the best way to do this is to use system methods, as follow:
String[] newArray = Arrays.copyOf(firstArray, firstArray.length + secondArray.length); System.arraycopy(secondArray, 0, newArray, firstArray.length, secondArray.length);This algorithm is three times faster and short.
public interface IBean { public String getField(); public void setField(String field); }
public class Generate { // If a program is running on a web application server such as JBoss and Tomcat, // the ClassPool object may not be able to find user classes. // In that case, an additional class path must be registered to the ClassPool. static{ // ClassPool initialization ClassPool.getDefault().insertClassPath(new ClassClassPath(IBean.class)); } public static IBean bean(){ ClassPool cp = ClassPool.getDefault(); // creation of the class CtClass Bean = cp.makeClass("Bean"); // addition of the interface Bean.addInterface(cp.get(IBean.class.getName())); // creation of an empty constructor CtConstructor ctConstructor = new CtConstructor(new CtClass[]{}, Bean); // its body ctConstructor.setBody(";"); // addition of the constructor Bean.addConstructor(ctConstructor); // addition of the String field Bean.addField(new CtField(cp.get(String.class.getName()), "field", Bean)); // creation of getField method CtMethod getField = CtNewMethod.make("public String getField(){ return field; }",Bean); // add method to CtClass Bean.addMethod(getField); // creation of setField method CtMethod setField = CtNewMethod.make("public void setField(String field){ this.field = field; }",Bean); // add method to CtClass Bean.addMethod(setField); return (IBean) Bean.toClass().newInstance(); } }
IBean bean = Generate.bean(); bean.setField("example"); assertEqual("example",bean.getField());
class Bean{ Integer id; String description; String other; // getter and setter.. }The scope is to include in the HQL query only the valorized properties of this bean. then we immediately think of a cascade of if:
String hql = "Select Bean from Bean"; boolean isWhere = true; if(bean.getId() != null) { if(isWhere){ hql += " Where "; isWhere = false; }else{ hql += " and "; } hql += "Bean.id = " + bean.getId(); } if(bean.getDescription() != null) { if(isWhere){ hql += " Where "; isWhere = false; }else{ hql += " and "; } hql += "Bean.id = " + bean.getDescription(); } // etc..The result is a HQL query generated at runtime, but the code written isn't generic and you must in all cases write custom code. Follows an algorithm created by me, a recursive function that permits to write hql query in every situation:
import java.lang.reflect.Field; import java.util.ArrayList; import java.util.Date; class Generate { private boolean isWhere; public String hql(Object obj){ isWhere = true; String alias = ob.getClass().getSimpleName().toLowerCase(); return hql(obj, alias, true); } private String hql(Object ob, String alias, boolean isFirst){ Class<? extends Object> obClass = ob.getClass(); StringBuilder sb = new StringBuilder(); if (isFirst) sb.append("SELECT "+alias+" FROM "+obClass.getSimpleName()+" as "+alias); try { for (Field field : obClass.getDeclaredFields()) { Class<?> fieldType = field.getType(); String fieldName = field.getName(); if(!isCollection(fieldType) && !isSerialVersionUID(fieldName)){ Object fieldValue = null; try{ fieldValue = obClass.getMethod(mGet(fieldName)).invoke(ob); }catch(Exception e){ // some fields don't have get method // for example serialVersionUID } if (fieldValue != null) // if the field is complex type it goes into introspection if (isFk(fieldType)) sb.append(hql(fieldValue, alias+"."+fieldName, false)); else { if (isWhere) { sb.append(" WHERE "); isWhere = false; } else sb.append(" AND "); sb.append(alias+"."+fieldName+" = "+getValue(fieldValue, fieldType)); } } } } catch (Exception e) { /** to handle **/ } return sb.toString(); } private String getValue(Object value, Class<?> field) { StringBuilder sb = new StringBuilder(); try { if (field.equals(String.class) || field.equals(Character.class) || field.equals(char.class)) sb.append("'"+value+"'"); else if(field.equals(Date.class)) sb.append("to_timestamp('"+value+"')"); else sb.append(value); } catch (Exception e) { /** to handle **/ } return sb.toString(); } public static final ArrayListThe exceptions aren't handled so to focalize the attention on the algorithm. An example of use:basicTypes = new ArrayList (){ private static final long serialVersionUID = -5567529960231273742L; { add(byte.class.getName()); add(short.class.getName()); add(int.class.getName()); add(long.class.getName()); add(float.class.getName()); add(double.class.getName()); add(char.class.getName()); add(boolean.class.getName()); add(Byte.class.getName()); add(Short.class.getName()); add(Integer.class.getName()); add(Long.class.getName()); add(Float.class.getName()); add(Double.class.getName()); add(Character.class.getName()); add(Boolean.class.getName()); add(String.class.getName()); add(Date.class.getName()); } }; private boolean isCollection(Class<?> aClass){ return Collection.class.isAssignableFrom(aClass); } private String mGet(String s) { return "get" + s.substring(0, 1).toUpperCase() + s.substring(1); } private boolean isSerialVersionUID(String field) { return field.equals("serialVersionUID"); } private boolean isFk(Class<?> field) { return !basicTypes.contains(field.getName()); } }
Bean bean = new Bean(2013,"description"); String hql = new Generate().hql(bean); assertEqual("SELECT bean FROM Bean as bean WHERE bean.id = 2013 and bean.description = 'description'", hql);