love fish大鹏一曰同风起,扶摇直上九万里

常用链接

统计

积分与排名

friends

link

最新评论

反射妙用:超强通用年月日树 (轉)

在实际项目中,大家都要都有很多需要通过日期进行管理的数据,比如单据,考勤,日志等等。。。    这些数据对象都有一个共同的特点就是有一个字段是日期型。
     于是我就利用JAVA的反射机制,写了一个通用年月日树。
是用在RCP插件项目中的。稍加改造也能用到WEB项目中,核心算法不变。

  1 import java.lang.reflect.Field;
  2 import java.util.Calendar;
  3 import java.util.Date;
  4 import java.util.List;
  5 import org.eclipse.swt.SWT;
  6 import org.eclipse.swt.graphics.Image;
  7 import org.eclipse.swt.widgets.Tree;
  8 import org.eclipse.swt.widgets.TreeItem;
  9 
 10 public class GetYMDTree {
 11 
 12     private static Image nodeImg = Activator.getImageDescriptor("icons/node.png").createImage();
 13 
 14     private static Image yearImg = Activator.getImageDescriptor("icons/year.png").createImage();
 15 
 16     private static Image monthImg = Activator.getImageDescriptor("icons/month.png").createImage();
 17 
 18     private static Image dayImg = Activator.getImageDescriptor("icons/day.png").createImage();
 19 
 20     
 21 
 22     public static void buildTree(List list, TreeItem tree, String dateField, String nameField) {
 23         tree.removeAll();
 24         if (list != null && list.size() > 0) {
 25             String year = "";
 26             String month = "";
 27             String day = "";
 28             Object objt = list.get(0);
 29             Field dateF = null;
 30             Field nameF = null;
 31             try {
 32                 dateF = objt.getClass().getDeclaredField(dateField);
 33                 dateF.setAccessible(true);
 34                 nameF = objt.getClass().getDeclaredField(nameField);
 35                 nameF.setAccessible(true);
 36             } catch (SecurityException e) {
 37                 e.printStackTrace();
 38                 tree.removeAll();
 39                 return;
 40             } catch (NoSuchFieldException e) {
 41                 e.printStackTrace();
 42                 tree.removeAll();
 43                 return;
 44             }
 45             for (Object obj : list) {
 46                 Calendar calendar = Calendar.getInstance();
 47                 String name = null;
 48                 try {
 49                     calendar.setTime((Date) dateF.get(obj));
 50                     name = (String) nameF.get(obj);
 51                 } catch (IllegalArgumentException e) {
 52                     e.printStackTrace();
 53                     tree.removeAll();
 54                     return;
 55                 } catch (IllegalAccessException e) {
 56                     e.printStackTrace();
 57                     tree.removeAll();
 58                     return;
 59                 }
 60                 if (year.equals(Integer.toString(calendar.get(Calendar.YEAR)))) {
 61                     if (month.equals(Integer.toString(calendar.get(Calendar.MONTH) + 1))) {
 62                         if (day.equals(Integer.toString(calendar.get(Calendar.DATE)))) {
 63                             TreeItem et = new TreeItem(tree.getItem(tree.getItemCount() - 1).getItem(
 64                                     tree.getItem(tree.getItemCount() - 1).getItemCount() - 1)
 65                                     .getItem(
 66                                             tree.getItem(tree.getItemCount() - 1).getItem(
 67                                                     tree.getItem(tree.getItemCount() - 1).getItemCount() - 1)
 68                                                     .getItemCount() - 1), SWT.NONE);
 69                             et.setText(name);
 70                             et.setData(obj);
 71                             et.setImage(nodeImg);
 72                         } else {
 73                             day = Integer.toString(calendar.get(Calendar.DATE));
 74                             TreeItem dt = new TreeItem(tree.getItem(tree.getItemCount() - 1).getItem(
 75                                     tree.getItem(tree.getItemCount() - 1).getItemCount() - 1), SWT.NONE);
 76                             dt.setText(day);
 77                             dt.setImage(dayImg);
 78                             dt.setData(day);
 79                             TreeItem et = new TreeItem(dt, SWT.NONE);
 80                             et.setText(name);
 81                             et.setData(obj);
 82                             et.setImage(nodeImg);
 83                         }
 84                     } else {
 85                         month = Integer.toString(calendar.get(Calendar.MONTH) + 1);
 86                         day = Integer.toString(calendar.get(Calendar.DATE));
 87                         TreeItem mt = new TreeItem(tree.getItem(tree.getItemCount() - 1), SWT.NONE);
 88                         mt.setText(month);
 89                         mt.setImage(monthImg);
 90                         mt.setData(month);
 91                         mt.setExpanded(true);
 92                         TreeItem dt = new TreeItem(mt, SWT.NONE);
 93                         dt.setText(day);
 94                         dt.setImage(dayImg);
 95                         dt.setData(day);
 96                         TreeItem et = new TreeItem(dt, SWT.NONE);
 97                         et.setText(name);
 98                         et.setData(obj);
 99                         et.setImage(nodeImg);
100                     }
101                 } else {
102                     year = Integer.toString(calendar.get(Calendar.YEAR));
103                     month = Integer.toString(calendar.get(Calendar.MONTH) + 1);
104                     day = Integer.toString(calendar.get(Calendar.DATE));
105                     TreeItem yt = new TreeItem(tree, SWT.NONE);
106                     yt.setText(year);
107                     yt.setImage(yearImg);
108                     yt.setData(year);
109                     TreeItem mt = new TreeItem(yt, SWT.NONE);
110                     mt.setText(month);
111                     mt.setImage(monthImg);
112                     mt.setData(month);
113                     mt.setExpanded(true);
114                     TreeItem dt = new TreeItem(mt, SWT.NONE);
115                     dt.setText(day);
116                     dt.setImage(dayImg);
117                     dt.setData(day);
118                     TreeItem et = new TreeItem(dt, SWT.NONE);
119                     et.setText(name);
120                     et.setData(obj);
121                     et.setImage(nodeImg);
122                     yt.setExpanded(true);
123                 }
124             }
125         }
126     }
127 
128 
129 
130     public static Image getDayImg() {
131         return dayImg;
132     }
133 
134 
135 
136     public static void setDayImg(Image dayImg) {
137         GetYMDTree.dayImg = dayImg;
138     }
139 
140 
141 
142     public static Image getMonthImg() {
143         return monthImg;
144     }
145 
146 
147 
148     public static void setMonthImg(Image monthImg) {
149         GetYMDTree.monthImg = monthImg;
150     }
151 
152 
153 
154     public static Image getNodeImg() {
155         return nodeImg;
156     }
157 
158 
159 
160     public static void setNodeImg(Image nodeImg) {
161         GetYMDTree.nodeImg = nodeImg;
162     }
163 
164 
165 
166     public static Image getYearImg() {
167         return yearImg;
168     }
169 
170 
171 
172     public static void setYearImg(Image yearImg) {
173         GetYMDTree.yearImg = yearImg;
174     }
175 }
176 

 

posted on 2007-01-12 14:44 liaojiyong 阅读(283) 评论(0)  编辑  收藏 所属分类: Java


只有注册用户登录后才能发表评论。


网站导航:
博客园   IT新闻   Chat2DB   C++博客   博问