package com.vci.client.uif.engine.client.controls; import java.awt.Color; import java.awt.Cursor; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Image; import java.awt.event.MouseEvent; import java.util.List; import java.util.Map; import com.vci.client.portal.custom.ICustomDefine; import com.vci.client.portal.utility.PRMItem; import com.vci.client.ui.swing.VCISwingUtil; import com.vci.client.ui.swing.components.VCIJTextField; import com.vci.client.uif.engine.client.custom.ICustomAttributeInteceptor; import com.vci.client.uif.engine.client.renderer.UserNameInTableCellRenderer; import com.vci.corba.common.VCIError; /** * txt带search的公用选择控制模块。 *

Description:

* @author caicong * @time 2014-5-8 */ public class BaseCustomControl extends AbstractCustomControl implements ICustomDefine{ private static final long serialVersionUID = -2815707317189936866L; private final Image img = VCISwingUtil.createImageIcon("search.png").getImage(); private String value = ""; private boolean editable = true; private ICustomAttributeInteceptor inteceptor = null ; private PRMItem item; public BaseCustomControl(){ super(); } public BaseCustomControl(PRMItem item) { super(item); this.item = item; // 1:只读 0:非只读 editable = item.getItemIsEditable().equals("1") ? false:true; init(); setEditable(editable); if(editable){ txt.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); } } @SuppressWarnings("serial") private final VCIJTextField txt = new VCIJTextField(""){ @Override public void paintComponent(Graphics g){ super.paintComponent(g); Graphics2D g2 = (Graphics2D) g; if(img != null){ g2.drawImage(img, getWidth() - img.getWidth(null), getHeight() - img.getHeight(null), img.getWidth(null), img.getHeight(null), null); } BaseCustomControl.this.drawRequiredFlag(g); } @Override public void mouseClicked(MouseEvent e) { super.mouseClicked(e); if(!editable || !isEnabled()) { return; } if (isClickImage(e)) { try { if(inteceptor == null){ inteceptor = getCustomInteceptorInstance(); } if(inteceptor != null){ //通过接口获取id-name的字符串。 String sotoreAndDisplayValue = inteceptor.getCustomAttributeSaveValue(item, getParentComponent());//父组件对象需考虑。 //将id 赋值给value,name赋值给txt。 setValue(sotoreAndDisplayValue);//待确定赋值方式。 } } catch (Exception e1) { e1.printStackTrace(); } } } private boolean isClickImage(MouseEvent e){ boolean res = false; int imgWidth = img.getWidth(null); int imgHeight = img.getHeight(null); int width = getWidth(); int height = getHeight(); int pointX = e.getX(); int pointY = e.getY(); if(width - imgWidth < pointX && pointX < width && height - imgHeight < pointY && pointY < height){ res = true; } // 只读的情况下,点击文本框任何区域,就将响应自事件处理 // 非只读时,只有点击图片区域,才响应自定义事件处理 if(!txt.isEditable()){ res = true; } return res; } }; @Override public String getValue() { return value; } @Override public void setValue(String sotoreAndDisplayValue) { String displayValue = ""; if(!sotoreAndDisplayValue.contains(ICustomAttributeInteceptor.SPLIT_CHAR)) { ICustomAttributeInteceptor inteceptor = getCustomInteceptorInstance(); if(inteceptor != null){ try { displayValue = inteceptor.getCustomAttributeDisplayValue(sotoreAndDisplayValue, getPRMItem()); } catch (VCIError e) { e.printStackTrace(); } } if(displayValue != null){ sotoreAndDisplayValue = sotoreAndDisplayValue + ICustomAttributeInteceptor.SPLIT_CHAR + displayValue; } } String oldValue = txt.getText(); String[] values = sotoreAndDisplayValue.split(ICustomAttributeInteceptor.SPLIT_CHAR); String newValue = ""; if(values.length > 0){ this.value = values[0]; this.txt.setText(values[0]); } if(values.length >= 2){ this.txt.setText(values[1]); newValue = this.txt.getText(); } if(sotoreAndDisplayValue.equals(ICustomAttributeInteceptor.SPLIT_CHAR)){ newValue = ""; this.value = newValue; this.txt.setText(this.value); } firePropertyChange("valueChange", oldValue, newValue); } protected ICustomAttributeInteceptor getCustomInteceptorInstance(){ ICustomAttributeInteceptor res = null; String className = getPRMItem().getItemStyle(); if(className == null || "".equals(className)){ className = getPRMItem().getItemValue(); } try{ if(className == null || "".equals(className)) return res; Object obj = Class.forName(className).getConstructor().newInstance(); if(obj == null) return res; if(obj instanceof ICustomAttributeInteceptor){ res = (ICustomAttributeInteceptor)obj; res.setCustomControl(this); } }catch(Exception ex){ ex.printStackTrace(); } return res; } @Override public void setEditable(boolean editable) { this.editable = editable; } protected void init(){ setLayout(new GridBagLayout()); add(txt, getGBC(0, 0, 1, 1, 1.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.BOTH, 0)); txt.setEditable(false); if (editable) { txt.setBackground(Color.WHITE); } } @Override public void setEnabled(boolean enabled) { super.setEnabled(enabled); setEditable(enabled); txt.setEnabled(enabled); } private UserNameInTableCellRenderer uncr = null; @Override public void getCustomValue(String columnName, Map customItemMap, List> datas) throws VCIError { if(uncr == null) uncr = new UserNameInTableCellRenderer(); uncr.getCustomValue(columnName, customItemMap, datas); } public VCIJTextField getTxt() { return txt; } }