1 19 package org.openharmonise.him.metadata.range.swing.floathandling; 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 36 40 public class FloatValuePanel 41 extends AbstractRangeDisplay 42 implements RangeDisplay, LayoutManager, KeyListener { 43 44 protected int m_nHeight = 10; 45 46 private int m_nMinOccurs = 0; 47 private int m_nMaxOccurs = 1000; 48 49 protected JTextComponent m_textComp = null; 50 51 private String m_sPreviousText = ""; 52 53 protected WarningsLabel m_warnings = null; 54 55 protected JLabel m_errorLabel = null; 56 57 private String m_sValue = ""; 58 59 private FloatRangeDisplay m_display = null; 60 61 64 public FloatValuePanel(FloatRangeDisplay display, PropertyInstance propInstance, String sValue) { 65 super(propInstance); 66 this.m_display = display; 67 this.m_sValue = sValue; 68 this.setup(); 69 } 70 71 74 public JPanel getPanel() { 75 return null; 76 } 77 78 public void setup() { 79 this.setLayout(this); 80 FloatRange range = 81 (FloatRange) this.getPropertyInstance().getDefinition().getRange(); 82 83 List domains = this.getPropertyInstance().getDefinition().getDomains(); 84 Iterator itor = domains.iterator(); 85 86 while(itor.hasNext()) { 87 Domain domain = (Domain)itor.next(); 88 boolean bApplicableDomain = false; 89 String sFilePath = ((VersionedVirtualFile)this.getPropertyInstance().getVirtualFile()).getLogicalPath(); 90 Iterator itor2 = domain.getPaths().iterator(); 91 while (itor2.hasNext()) { 92 String sDomainPath = (String ) itor2.next(); 93 if(sFilePath.startsWith(sDomainPath)) { 94 bApplicableDomain=true; 95 } 96 } 97 98 if(bApplicableDomain) { 99 if(this.m_nMinOccurs<domain.getMinOccurs()) { 100 this.m_nMinOccurs = domain.getMinOccurs(); 101 } 102 if(this.m_nMaxOccurs>domain.getMaxOccurs()) { 103 this.m_nMaxOccurs = domain.getMaxOccurs(); 104 } 105 } 106 107 } 108 109 String sWarning = ""; 110 111 this.m_errorLabel = new JLabel(); 112 this.m_errorLabel.setIcon( IconManager.getInstance().getIcon("16-message-confirm.gif") ); 113 this.add(this.m_errorLabel); 114 115 if(range.getMinimum()!=null && range.getMaximum()!=null) { 116 sWarning = "Please type a number between {" + range.getMinimum() + "} and {" + range.getMaximum() + "}."; 117 } else if(range.getMaximum()!=null) { 118 sWarning = "Please type a number less than {" + range.getMaximum() + "}."; 119 } else if(range.getMinimum()!=null) { 120 sWarning = "Please type a number greater than {" + range.getMinimum() + "}."; 121 } else { 122 sWarning = "Please type a number."; 123 } 124 125 m_warnings = new WarningsLabel(sWarning); 126 this.add(m_warnings); 127 128 m_textComp = new JTextField(this.m_sValue); 129 this.m_sPreviousText = this.m_sValue; 130 m_textComp.addKeyListener(this); 131 m_textComp.setBorder(BorderFactory.createEtchedBorder()); 132 int nHeight = 30; 133 m_nHeight = nHeight + 20; 134 m_textComp.setPreferredSize(new Dimension(200, 20)); 135 this.add(m_textComp); 136 137 this.keyReleased(null); 138 } 139 140 143 public Dimension getPreferredSize() { 144 this.layoutContainer(null); 145 int nWidth = this.getParent().getWidth(); 146 int nHeight = 10; 147 nHeight = nHeight + this.m_textComp.getSize().height; 148 return new Dimension(nWidth, nHeight); 149 } 150 151 154 public void removeLayoutComponent(Component arg0) { 155 } 157 158 161 public void layoutContainer(Container arg0) { 162 int nWidth = this.getParent().getWidth() - 18; 163 Dimension dim = m_textComp.getPreferredSize(); 164 m_textComp.setPreferredSize(new Dimension(nWidth - 85, dim.height)); 165 m_textComp.setSize(m_textComp.getPreferredSize()); 166 m_textComp.setLocation(20, 0); 167 168 this.m_errorLabel.setLocation(0, 0); 169 this.m_errorLabel.setSize(15,15); 170 this.repaint(); 171 } 172 173 176 public void addLayoutComponent(String arg0, Component arg1) { 177 } 179 180 183 public Dimension minimumLayoutSize(Container arg0) { 184 return this.getPreferredSize(); 185 } 186 187 190 public Dimension preferredLayoutSize(Container arg0) { 191 return this.getPreferredSize(); 192 } 193 194 197 public void keyPressed(KeyEvent arg0) { 198 } 200 201 204 public void keyReleased(KeyEvent ke) { 205 if(ke!=null) { 206 char key = ke.getKeyChar(); 207 208 this.m_errorLabel.setIcon(IconManager.getInstance().getIcon("16-message-confirm.gif")); 209 m_textComp.setBorder(BorderFactory.createEtchedBorder()); 210 211 if (!(key == '0' 212 || key == '1' 213 || key == '2' 214 || key == '3' 215 || key == '4' 216 || key == '5' 217 || key == '6' 218 || key == '7' 219 || key == '8' 220 || key == '9' 221 || key == '.' 222 || key == '-' 223 || ke.getKeyCode() == KeyEvent.VK_RIGHT 224 || ke.getKeyCode() == KeyEvent.VK_LEFT 225 || ke.getKeyCode() == KeyEvent.VK_DELETE 226 || ke.getKeyCode() == KeyEvent.VK_BACK_SPACE) 227 || ke.getKeyCode() == KeyEvent.VK_SPACE) { 228 this.m_textComp.setText(this.m_sPreviousText); 229 } else if (key == '-') { 230 String sTemp = this.m_textComp.getText(); 231 if (sTemp.lastIndexOf("-") > 0) { 232 this.m_textComp.setText(this.m_sPreviousText); 233 } else { 234 this.m_sPreviousText = this.m_textComp.getText(); 235 } 236 } else if (key == '.') { 237 char[] cTemp = this.m_textComp.getText().toCharArray(); 238 int nCount = 0; 239 for (int i = 0; i < cTemp.length; i++) { 240 if (cTemp[i] == '.') { 241 nCount++; 242 } 243 if (nCount > 1) { 244 this.m_textComp.setText(this.m_sPreviousText); 245 break; 246 } 247 } 248 if (nCount < 2) { 249 this.m_sPreviousText = this.m_textComp.getText(); 250 } 251 } else { 252 this.m_sPreviousText = this.m_textComp.getText(); 253 } 254 } 255 256 FloatRange range = 257 (FloatRange) this.getPropertyInstance().getDefinition().getRange(); 258 259 float fVal = 0; 260 try { 261 fVal = Float.parseFloat(this.m_textComp.getText()); 262 } catch (NumberFormatException e) { 263 if(range.getMinimum()!=null) { 264 this.m_warnings.setHighlight(range.getMinimum().toString(), true); 265 } 266 if(range.getMaximum()!=null) { 267 this.m_warnings.setHighlight(range.getMaximum().toString(), true); 268 } 269 this.m_errorLabel.setIcon(IconManager.getInstance().getIcon("16-message-error.gif")); 270 m_textComp.setBorder( BorderFactory.createLineBorder(Color.RED) ); 271 } 272 273 if(range.getMinimum()!=null && range.getMaximum()!=null) { 274 if(fVal<range.getMinimum().floatValue()) { 275 this.m_warnings.setHighlight(range.getMinimum().toString(), true); 276 this.m_errorLabel.setIcon(IconManager.getInstance().getIcon("16-message-error.gif")); 277 m_textComp.setBorder( BorderFactory.createLineBorder(Color.RED) ); 278 } else { 279 this.m_warnings.setHighlight(range.getMinimum().toString(), false); 280 } 281 if(fVal>range.getMaximum().floatValue()) { 282 this.m_warnings.setHighlight(range.getMaximum().toString(), true); 283 this.m_errorLabel.setIcon(IconManager.getInstance().getIcon("16-message-error.gif")); 284 m_textComp.setBorder( BorderFactory.createLineBorder(Color.RED) ); 285 } else { 286 this.m_warnings.setHighlight(range.getMaximum().toString(), false); 287 } 288 } else if(range.getMinimum()!=null) { 289 if(fVal<range.getMinimum().floatValue()) { 290 this.m_warnings.setHighlight(range.getMinimum().toString(), true); 291 this.m_errorLabel.setIcon(IconManager.getInstance().getIcon("16-message-error.gif")); 292 m_textComp.setBorder( BorderFactory.createLineBorder(Color.RED) ); 293 } else { 294 this.m_warnings.setHighlight(range.getMinimum().toString(), false); 295 } 296 } else if(range.getMaximum()!=null) { 297 if(fVal>range.getMaximum().floatValue()) { 298 this.m_warnings.setHighlight(range.getMaximum().toString(), true); 299 this.m_errorLabel.setIcon(IconManager.getInstance().getIcon("16-message-error.gif")); 300 m_textComp.setBorder( BorderFactory.createLineBorder(Color.RED) ); 301 } else { 302 this.m_warnings.setHighlight(range.getMaximum().toString(), false); 303 } 304 } 305 306 if(!this.m_errorLabel.getIcon().toString().equals(IconManager.getInstance().getIcon("16-message-error.gif").toString()) && this.getParent()!=null) { 307 this.m_display.valueChanged(this.m_textComp.getText().trim()); 308 } 309 super.validateTab(); 310 } 311 312 315 public void keyTyped(KeyEvent arg0) { 316 } 318 319 protected String getValue() { 320 return this.m_textComp.getText().trim(); 321 } 322 323 326 public boolean isMetadataValid() { 327 return !this.m_errorLabel.getIcon().toString().equals(IconManager.getInstance().getIcon("16-message-error.gif").toString()); 328 } 329 330 } 331 | Popular Tags |