1 19 package org.openharmonise.him.metadata.range.swing.integerhandling; 20 21 import java.awt.*; 22 import java.awt.event.*; 23 import java.util.*; 24 import java.util.List ; 25 26 import javax.swing.*; 27 import javax.swing.text.*; 28 29 import org.openharmonise.him.metadata.range.swing.*; 30 import org.openharmonise.vfs.*; 31 import org.openharmonise.vfs.gui.*; 32 import org.openharmonise.vfs.metadata.*; 33 import org.openharmonise.vfs.metadata.range.*; 34 35 43 public class IntegerValuePanel 44 extends AbstractRangeDisplay 45 implements RangeDisplay, LayoutManager, KeyListener { 46 47 protected int m_nHeight = 10; 48 49 private int m_nMinOccurs = 0; 50 private int m_nMaxOccurs = 1000; 51 52 protected JTextComponent m_textComp = null; 53 54 private String m_sPreviousText = ""; 55 56 protected WarningsLabel m_warnings = null; 57 58 protected JLabel m_errorLabel = null; 59 60 private String m_sValue = ""; 61 62 private IntegerRangeDisplay m_display = null; 63 64 67 public IntegerValuePanel(IntegerRangeDisplay display, PropertyInstance propInstance, String sValue) { 68 super(propInstance); 69 this.m_display = display; 70 this.m_sValue = sValue; 71 this.setup(); 72 } 73 74 77 public void setup() { 78 this.setLayout(this); 79 IntegerRange range = 80 (IntegerRange) this 81 .getPropertyInstance() 82 .getDefinition() 83 .getRange(); 84 85 List domains = this.getPropertyInstance().getDefinition().getDomains(); 86 Iterator itor = domains.iterator(); 87 88 while(itor.hasNext()) { 89 Domain domain = (Domain)itor.next(); 90 boolean bApplicableDomain = false; 91 String sFilePath = ((VersionedVirtualFile)this.getPropertyInstance().getVirtualFile()).getLogicalPath(); 92 Iterator itor2 = domain.getPaths().iterator(); 93 while (itor2.hasNext()) { 94 String sDomainPath = (String ) itor2.next(); 95 if(sFilePath.startsWith(sDomainPath)) { 96 bApplicableDomain=true; 97 } 98 } 99 100 if(bApplicableDomain) { 101 if(this.m_nMinOccurs<domain.getMinOccurs()) { 102 this.m_nMinOccurs = domain.getMinOccurs(); 103 } 104 if(this.m_nMaxOccurs>domain.getMaxOccurs()) { 105 this.m_nMaxOccurs = domain.getMaxOccurs(); 106 } 107 } 108 109 } 110 111 this.m_errorLabel = new JLabel(); 112 this.m_errorLabel.setIcon( 113 IconManager.getInstance().getIcon("16-message-confirm.gif")); 114 this.add(this.m_errorLabel); 115 116 String sWarning = ""; 117 118 if (range.getMinimum() != null && range.getMaximum() != null) { 119 sWarning = 120 "Please type a number between {" 121 + range.getMinimum() 122 + "} and {" 123 + range.getMaximum() 124 + "}."; 125 } else if (range.getMaximum() != null) { 126 sWarning = 127 "Please type a number less than {" + range.getMaximum() + "}."; 128 } else if (range.getMinimum() != null) { 129 sWarning = 130 "Please type a number greater than {" 131 + range.getMinimum() 132 + "}."; 133 } else { 134 sWarning = "Please type a number."; 135 } 136 137 m_warnings = new WarningsLabel(sWarning); 138 this.add(m_warnings); 139 140 m_textComp = new JTextField(this.m_sValue); 141 this.m_sPreviousText = this.m_sValue; 142 m_textComp.addKeyListener(this); 143 m_textComp.setBorder(BorderFactory.createEtchedBorder()); 144 int nHeight = 30; 145 m_nHeight = nHeight + 20; 146 m_textComp.setPreferredSize(new Dimension(200, 20)); 147 this.add(m_textComp); 148 149 this.keyReleased(null); 150 } 151 152 155 public Dimension getPreferredSize() { 156 this.layoutContainer(null); 157 int nWidth = this.getParent().getWidth(); 158 int nHeight = 10; 159 nHeight = nHeight + this.m_textComp.getSize().height; 160 return new Dimension(nWidth, nHeight); 161 } 162 163 166 public void removeLayoutComponent(Component arg0) { 167 } 169 170 173 public void layoutContainer(Container arg0) { 174 int nWidth = this.getParent().getWidth() - 18; 175 176 Dimension dim = m_textComp.getPreferredSize(); 177 m_textComp.setPreferredSize(new Dimension(nWidth - 85, dim.height)); 178 m_textComp.setSize(m_textComp.getPreferredSize()); 179 m_textComp.setLocation(20, 0); 180 181 this.m_errorLabel.setLocation(0,0); 182 this.m_errorLabel.setSize(15, 15); 183 this.repaint(); 184 } 185 186 189 public void addLayoutComponent(String arg0, Component arg1) { 190 } 192 193 196 public Dimension minimumLayoutSize(Container arg0) { 197 return this.getPreferredSize(); 198 } 199 200 203 public Dimension preferredLayoutSize(Container arg0) { 204 return this.getPreferredSize(); 205 } 206 207 210 public void keyPressed(KeyEvent arg0) { 211 } 213 214 217 public void keyReleased(KeyEvent ke) { 218 if (ke != null) { 219 char key = ke.getKeyChar(); 220 221 this.m_errorLabel.setIcon( 222 IconManager.getInstance().getIcon("16-message-confirm.gif")); 223 m_textComp.setBorder(BorderFactory.createEtchedBorder()); 224 225 if (!(key == '0' 226 || key == '1' 227 || key == '2' 228 || key == '3' 229 || key == '4' 230 || key == '5' 231 || key == '6' 232 || key == '7' 233 || key == '8' 234 || key == '9' 235 || key == '-' 236 || ke.getKeyCode() == KeyEvent.VK_RIGHT 237 || ke.getKeyCode() == KeyEvent.VK_LEFT 238 || ke.getKeyCode() == KeyEvent.VK_DELETE 239 || ke.getKeyCode() == KeyEvent.VK_BACK_SPACE) 240 || ke.getKeyCode() == KeyEvent.VK_SPACE) { 241 this.m_textComp.setText(this.m_sPreviousText); 242 } else if (key == '-') { 243 String sTemp = this.m_textComp.getText(); 244 if (sTemp.lastIndexOf("-") > 0) { 245 this.m_textComp.setText(this.m_sPreviousText); 246 } else { 247 this.m_sPreviousText = this.m_textComp.getText(); 248 } 249 } else { 250 this.m_sPreviousText = this.m_textComp.getText(); 251 } 252 } 253 254 if (!this.m_textComp.getText().equals("")) { 255 int fVal = Integer.parseInt(this.m_textComp.getText()); 256 IntegerRange range = 257 (IntegerRange) this 258 .getPropertyInstance() 259 .getDefinition() 260 .getRange(); 261 262 if (range.getMinimum() != null && range.getMaximum() != null) { 263 if (fVal < range.getMinimum().intValue()) { 264 this.m_warnings.setHighlight( 265 range.getMinimum().toString(), 266 true); 267 this.m_errorLabel.setIcon( 268 IconManager.getInstance().getIcon( 269 "16-message-error.gif")); 270 m_textComp.setBorder( 271 BorderFactory.createLineBorder(Color.RED)); 272 } else { 273 this.m_warnings.setHighlight( 274 range.getMinimum().toString(), 275 false); 276 } 277 if (fVal > range.getMaximum().intValue()) { 278 this.m_warnings.setHighlight( 279 range.getMaximum().toString(), 280 true); 281 this.m_errorLabel.setIcon( 282 IconManager.getInstance().getIcon( 283 "16-message-error.gif")); 284 m_textComp.setBorder( 285 BorderFactory.createLineBorder(Color.RED)); 286 } else { 287 this.m_warnings.setHighlight( 288 range.getMaximum().toString(), 289 false); 290 } 291 } else if (range.getMinimum() != null) { 292 if (fVal < range.getMinimum().intValue()) { 293 this.m_warnings.setHighlight( 294 range.getMinimum().toString(), 295 true); 296 this.m_errorLabel.setIcon( 297 IconManager.getInstance().getIcon( 298 "16-message-error.gif")); 299 m_textComp.setBorder( 300 BorderFactory.createLineBorder(Color.RED)); 301 } else { 302 this.m_warnings.setHighlight( 303 range.getMinimum().toString(), 304 false); 305 } 306 } else if (range.getMaximum() != null) { 307 if (fVal > range.getMaximum().intValue()) { 308 this.m_warnings.setHighlight( 309 range.getMaximum().toString(), 310 true); 311 this.m_errorLabel.setIcon( 312 IconManager.getInstance().getIcon( 313 "16-message-error.gif")); 314 m_textComp.setBorder( 315 BorderFactory.createLineBorder(Color.RED)); 316 } else { 317 this.m_warnings.setHighlight( 318 range.getMaximum().toString(), 319 false); 320 } 321 } 322 } else if(this.m_nMinOccurs>0) { 323 this.m_errorLabel.setIcon( 324 IconManager.getInstance().getIcon( 325 "16-message-error.gif")); 326 m_textComp.setBorder( 327 BorderFactory.createLineBorder(Color.RED)); 328 } 329 330 if(!this.m_errorLabel.getIcon().toString().equals(IconManager.getInstance().getIcon("16-message-error.gif").toString()) && this.getParent()!=null) { 331 this.m_display.valueChanged(this.m_textComp.getText().trim()); 332 } 333 super.validateTab(); 334 335 } 336 337 340 public void keyTyped(KeyEvent arg0) { 341 } 343 344 347 public JPanel getPanel() { 348 return null; 349 } 350 351 protected String getValue() { 352 return this.m_textComp.getText().trim(); 353 } 354 355 358 public boolean isMetadataValid() { 359 return !this.m_errorLabel.getIcon().toString().equals(IconManager.getInstance().getIcon("16-message-error.gif").toString()); 360 } 361 362 } 363 | Popular Tags |