1 19 package org.openharmonise.him.metadata.range.swing.datehandling; 20 21 import java.awt.*; 22 import java.text.*; 23 import java.util.*; 24 import java.util.List ; 25 26 import javax.swing.*; 27 28 import org.openharmonise.him.metadata.range.swing.*; 29 import org.openharmonise.swing.datefield.*; 30 import org.openharmonise.vfs.gui.*; 31 import org.openharmonise.vfs.metadata.*; 32 import org.openharmonise.vfs.metadata.range.*; 33 34 35 42 public class DateValuePanel 43 extends AbstractRangeDisplay 44 implements RangeDisplay, LayoutManager, DateFieldListener { 45 46 49 private static final String XSD_DATE_FORMAT = "yyyy-MM-dd G"; 50 51 54 private SimpleDateFormat df = 55 new java.text.SimpleDateFormat (XSD_DATE_FORMAT); 56 57 60 protected int m_nHeight = 10; 61 62 65 private JDateField m_dateField = null; 66 67 70 private String m_sPreviousText = ""; 71 72 75 protected WarningsLabel m_warnings = null; 76 77 80 protected JLabel m_errorLabel = null; 81 82 85 private String m_sValue = ""; 86 87 90 private DateRangeDisplay m_display = null; 91 92 95 private boolean m_bNoValueAllowed = true; 96 97 100 public DateValuePanel(DateRangeDisplay display, PropertyInstance propInstance, String sValue, boolean bNoValueAllowed) { 101 super(propInstance); 102 this.m_display = display; 103 this.m_sValue = sValue; 104 setNoValueAllowed(bNoValueAllowed); 105 this.setup(); 106 } 107 108 protected void setNoValueAllowed(boolean bNoValueAllowed) { 109 m_bNoValueAllowed = bNoValueAllowed; 110 } 111 112 115 public JPanel getPanel() { 116 return null; 117 } 118 119 public void setup() { 120 this.setLayout(this); 121 DateRange range = 122 (DateRange) this.getPropertyInstance().getDefinition().getRange(); 123 124 List domains = this.getPropertyInstance().getDefinition().getDomains(); 125 Iterator itor = domains.iterator(); 126 127 128 String sWarning = ""; 129 130 this.m_errorLabel = new JLabel(); 131 this.m_errorLabel.setIcon( 132 IconManager.getInstance().getIcon("16-message-confirm.gif")); 133 this.add(this.m_errorLabel); 134 135 if (range.getMinimum() != null && range.getMaximum() != null) { 136 sWarning = 137 "Please enter a date (ccyy/MM/dd) between {" 138 + this.df.format(range.getMinimum()) 139 + "} and {" 140 + this.df.format(range.getMaximum()) 141 + "}."; 142 } else if (range.getMaximum() != null) { 143 sWarning = 144 "Please enter a date (ccyy/MM/dd) before {" 145 + this.df.format(range.getMaximum()) 146 + "}."; 147 } else if (range.getMinimum() != null) { 148 sWarning = 149 "Please enter a date (ccyy/MM/dd) after {" 150 + this.df.format(range.getMinimum()) 151 + "}."; 152 } else { 153 sWarning = "Please enter a date (ccyy/MM/dd)."; 154 } 155 156 m_warnings = new WarningsLabel(sWarning); 157 this.add(m_warnings); 158 159 this.m_dateField = new JDateField("yyyy/MM/dd G"); 160 this.m_dateField.setBorder( BorderFactory.createEtchedBorder() ); 161 this.m_dateField.addDateFieldListener(this); 162 this.m_dateField.setMinimumDate(range.getMinimum()); 163 this.m_dateField.setMaximumDate(range.getMaximum()); 164 if(this.m_sValue!=null && !this.m_sValue.equals("")) { 165 Date dtValue = null; 166 try { 167 dtValue = df.parse(this.m_sValue); 168 } catch (Exception e) { 169 } 171 if(dtValue!=null) { 172 this.m_dateField.setDate(dtValue); 173 } 174 } 175 this.add(this.m_dateField); 176 177 } 178 179 182 public Dimension getPreferredSize() { 183 this.layoutContainer(null); 184 int nWidth = this.m_warnings.getSize().width + this.m_warnings.getLocation().x; 185 if(this.m_dateField.getSize().width>nWidth) { 186 nWidth =this.m_dateField.getSize().width; 187 } 188 nWidth = nWidth + 60; 189 int nHeight = 10; 190 191 nHeight = nHeight + this.m_dateField.getSize().height; 192 return new Dimension(nWidth, nHeight); 193 } 194 195 198 public void removeLayoutComponent(Component arg0) { 199 } 201 202 205 public void layoutContainer(Container arg0) { 206 int nWidth = this.getParent().getWidth() - 18; 207 208 int nXpos = 20; 209 210 this.m_dateField.setSize(this.m_dateField.getPreferredSize()); 211 this.m_dateField.setLocation(nXpos, 0); 212 nXpos = nXpos + this.m_dateField.getSize().width + 10; 213 214 this.m_errorLabel.setLocation(0,0); 215 this.m_errorLabel.setSize(15, 15); 216 this.repaint(); 217 } 218 219 222 public void addLayoutComponent(String arg0, Component arg1) { 223 } 225 226 229 public Dimension minimumLayoutSize(Container arg0) { 230 return this.getPreferredSize(); 231 } 232 233 236 public Dimension preferredLayoutSize(Container arg0) { 237 return this.getPreferredSize(); 238 } 239 240 protected String getValue() { 241 Date dtValue = this.m_dateField.getDate(); 242 String sValue = ""; 243 try { 244 if(dtValue != null) { 245 sValue = this.df.format(dtValue); 246 } 247 } catch(Exception e) { 248 e.printStackTrace(); 249 } 250 251 return sValue; 252 } 253 254 257 public boolean isMetadataValid() { 258 if(!this.m_dateField.hasValue()) { 259 return true; 260 } else { 261 PropertyInstance propInst = getPropertyInstance(); 262 Property prop = getPropertyInstance().getDefinition(); 263 264 DateRange range = (DateRange)prop.getRange(); 265 266 return range.validate(propInst.getValues()).isValid(); 267 } 268 } 269 270 273 public void validationFailed(JDateField source, int nReason) { 274 this.m_errorLabel.setIcon( 275 IconManager.getInstance().getIcon("16-message-error.gif")); 276 super.validateTab(); 277 } 278 279 282 public void valueChanged(JDateField source) { 283 this.m_errorLabel.setIcon( 284 IconManager.getInstance().getIcon("16-message-confirm.gif")); 285 this.m_display.valueChanged(this.getValue().trim()); 286 super.validateTab(); 287 } 288 289 } 290 | Popular Tags |