1 19 package org.openharmonise.him.metadata.range.swing.stringhandling; 20 21 import java.awt.Color ; 22 import java.awt.Component ; 23 import java.awt.Container ; 24 import java.awt.Dimension ; 25 import java.awt.Font ; 26 import java.awt.LayoutManager ; 27 import java.awt.event.ActionEvent ; 28 import java.awt.event.KeyEvent ; 29 import java.awt.event.KeyListener ; 30 import java.util.Iterator ; 31 import java.util.List ; 32 33 import javax.swing.AbstractAction ; 34 import javax.swing.Action ; 35 import javax.swing.BorderFactory ; 36 import javax.swing.JComponent ; 37 import javax.swing.JLabel ; 38 import javax.swing.JPanel ; 39 import javax.swing.JScrollPane ; 40 import javax.swing.JTextArea ; 41 import javax.swing.JTextField ; 42 import javax.swing.KeyStroke ; 43 import javax.swing.border.EtchedBorder ; 44 import javax.swing.text.JTextComponent ; 45 46 import org.openharmonise.him.metadata.range.swing.*; 47 import org.openharmonise.vfs.*; 48 import org.openharmonise.vfs.gui.*; 49 import org.openharmonise.vfs.metadata.*; 50 import org.openharmonise.vfs.metadata.range.*; 51 52 53 57 public class StringValuePanel 58 extends AbstractRangeDisplay 59 implements RangeDisplay, KeyListener , LayoutManager { 60 61 private int m_nHeight = 30; 62 63 private int m_nMinOccurs = 0; 64 private int m_nMaxOccurs = 1000; 65 66 private JTextComponent m_textComp = null; 67 private Counter m_counter = null; 68 69 private WarningsLabel m_warnings = null; 70 71 private JLabel m_errorLabel = null; 72 73 private String m_sValue = ""; 74 75 private StringRangeDisplay m_display = null; 76 77 private JScrollPane m_textScroller = null; 78 79 private int m_nValueHeight = 20; 80 81 84 public StringValuePanel(StringRangeDisplay display, PropertyInstance propInstance, String sValue) { 85 super(propInstance); 86 this.m_display = display; 87 this.m_sValue = sValue; 88 this.setup(); 89 } 90 91 94 private void setup() { 95 this.setLayout(this); 96 97 StringRange range = 98 (StringRange) this.getPropertyInstance().getDefinition().getRange(); 99 100 List domains = this.getPropertyInstance().getDefinition().getDomains(); 101 Iterator itor = domains.iterator(); 102 103 while(itor.hasNext()) { 104 Domain domain = (Domain)itor.next(); 105 boolean bApplicableDomain = false; 106 String sFilePath = ((VersionedVirtualFile)this.getPropertyInstance().getVirtualFile()).getLogicalPath(); 107 Iterator itor2 = domain.getPaths().iterator(); 108 while (itor2.hasNext()) { 109 String sDomainPath = (String ) itor2.next(); 110 if(sFilePath.startsWith(sDomainPath)) { 111 bApplicableDomain=true; 112 } 113 } 114 115 if(bApplicableDomain) { 116 if(this.m_nMinOccurs<domain.getMinOccurs()) { 117 this.m_nMinOccurs = domain.getMinOccurs(); 118 } 119 if(this.m_nMaxOccurs>domain.getMaxOccurs()) { 120 this.m_nMaxOccurs = domain.getMaxOccurs(); 121 } 122 } 123 124 } 125 126 this.m_errorLabel = new JLabel (); 127 this.m_errorLabel.setIcon( 128 IconManager.getInstance().getIcon("16-message-confirm.gif")); 129 this.add(this.m_errorLabel); 130 131 String sWarning = ""; 132 if (range.getMinLength() != -1 && range.getMaxLength() != -1) { 133 sWarning = 134 "Please type text between {" 135 + range.getMinLength() 136 + "} and {" 137 + range.getMaxLength() 138 + "} characters."; 139 } else if (range.getMinLength() != -1) { 140 sWarning = 141 "Please type text with more than {" 142 + range.getMinLength() 143 + "} characters."; 144 } else if (range.getMaxLength() != -1) { 145 sWarning = 146 "Please type text with less than {" 147 + range.getMaxLength() 148 + "} characters."; 149 } else { 150 sWarning = "Please type some text."; 151 } 152 153 m_warnings = new WarningsLabel(sWarning); 154 this.add(m_warnings); 155 156 if (range.getMaxLength() <= StringRange.SIZE_BORDER) { 157 m_textComp = new JTextField (this.m_sValue); 158 m_textComp.setBorder(BorderFactory.createEtchedBorder()); 159 m_textComp.setPreferredSize(new Dimension (200, 20)); 160 this.m_textComp.addKeyListener(this); 161 this.add(m_textComp); 162 m_counter = 163 new Counter( 164 m_textComp, 165 (StringRange) this 166 .getPropertyInstance() 167 .getDefinition() 168 .getRange()); 169 } else { 170 JTextArea textComp = new JTextArea (this.m_sValue); 171 this.m_textComp = textComp; 172 textComp.setWrapStyleWord(true); 173 textComp.setLineWrap(true); 174 175 m_textScroller = new JScrollPane (textComp, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); 176 m_textScroller.setPreferredSize(new Dimension (400, 100)); 177 m_textScroller.setFocusable(false); 178 m_nValueHeight = 100; 179 textComp.addKeyListener(this); 180 this.add(m_textScroller); 181 m_counter = 182 new Counter( 183 textComp, 184 (StringRange) this 185 .getPropertyInstance() 186 .getDefinition() 187 .getRange()); 188 textComp.getInputMap(JComponent.WHEN_FOCUSED).put( 189 KeyStroke.getKeyStroke("TAB"), nextFocusAction.getValue(Action.NAME)); 190 textComp.getInputMap(JComponent.WHEN_FOCUSED).put( 191 KeyStroke.getKeyStroke("shift TAB"), prevFocusAction.getValue(Action.NAME)); 192 textComp.getActionMap().put(nextFocusAction.getValue(Action.NAME),nextFocusAction); 193 textComp.getActionMap().put(prevFocusAction.getValue(Action.NAME),prevFocusAction); 194 } 195 196 this.m_textComp.setFont(new Font ("Arial Unicode MS", Font.PLAIN, 11)); 197 198 this.add(m_counter); 199 200 this.keyReleased(null); 201 202 if(this.m_display.isReadOnly()){ 203 m_textComp.setFocusable(false); 204 } 205 } 206 public Action nextFocusAction = new AbstractAction ("Move Focus Forwards") { 208 public void actionPerformed(ActionEvent evt) { 209 ((Component )evt.getSource()).transferFocus(); 210 } 211 }; 212 public Action prevFocusAction = new AbstractAction ("Move Focus Backwards") { 213 public void actionPerformed(ActionEvent evt) { 214 ((Component )evt.getSource()).transferFocusBackward(); 215 } 216 }; 217 218 221 public Dimension getPreferredSize() { 222 this.layoutContainer(null); 223 int nWidth = this.getParent().getWidth(); 224 int nHeight = 10; 225 nHeight = nHeight + m_nValueHeight; 226 return new Dimension (nWidth, nHeight); 227 } 228 229 232 public void keyPressed(KeyEvent arg0) { 233 } 235 236 239 public void keyReleased(KeyEvent arg0) { 240 241 this.m_errorLabel.setIcon( 242 IconManager.getInstance().getIcon("16-message-confirm.gif")); 243 if(this.m_textScroller!=null) { 244 m_textScroller.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED)); 245 } else { 246 m_textComp.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED)); 247 } 248 249 StringRange range = 250 (StringRange) this.getPropertyInstance().getDefinition().getRange(); 251 int nLength = this.m_textComp.getText().length(); 252 if (range.getMinLength() != -1 && range.getMaxLength() != -1) { 253 if (nLength < range.getMinLength()) { 254 this.m_warnings.setHighlight( 255 Integer.toString(range.getMinLength()), 256 true); 257 this.m_errorLabel.setIcon( 258 IconManager.getInstance().getIcon("16-message-error.gif")); 259 if(this.m_textScroller!=null) { 260 m_textScroller.setBorder(BorderFactory.createLineBorder(Color.RED)); 261 } else { 262 m_textComp.setBorder(BorderFactory.createLineBorder(Color.RED)); 263 } 264 } else { 265 this.m_warnings.setHighlight( 266 Integer.toString(range.getMinLength()), 267 false); 268 } 269 if (nLength > range.getMaxLength()) { 270 this.m_warnings.setHighlight( 271 Integer.toString(range.getMaxLength()), 272 true); 273 this.m_errorLabel.setIcon( 274 IconManager.getInstance().getIcon("16-message-error.gif")); 275 if(this.m_textScroller!=null) { 276 m_textScroller.setBorder(BorderFactory.createLineBorder(Color.RED)); 277 } else { 278 m_textComp.setBorder(BorderFactory.createLineBorder(Color.RED)); 279 } 280 } else { 281 this.m_warnings.setHighlight( 282 Integer.toString(range.getMaxLength()), 283 false); 284 } 285 } else if (range.getMinLength() != -1) { 286 if (nLength < range.getMinLength()) { 287 this.m_warnings.setHighlight( 288 Integer.toString(range.getMinLength()), 289 true); 290 this.m_errorLabel.setIcon( 291 IconManager.getInstance().getIcon("16-message-error.gif")); 292 if(this.m_textScroller!=null) { 293 m_textScroller.setBorder(BorderFactory.createLineBorder(Color.RED)); 294 } else { 295 m_textComp.setBorder(BorderFactory.createLineBorder(Color.RED)); 296 } 297 } else { 298 this.m_warnings.setHighlight( 299 Integer.toString(range.getMinLength()), 300 false); 301 } 302 } else if (range.getMaxLength() != -1) { 303 if (nLength > range.getMaxLength()) { 304 this.m_warnings.setHighlight( 305 Integer.toString(range.getMaxLength()), 306 true); 307 this.m_errorLabel.setIcon( 308 IconManager.getInstance().getIcon("16-message-error.gif")); 309 if(this.m_textScroller!=null) { 310 m_textScroller.setBorder(BorderFactory.createLineBorder(Color.RED)); 311 } else { 312 m_textComp.setBorder(BorderFactory.createLineBorder(Color.RED)); 313 } 314 } else { 315 this.m_warnings.setHighlight( 316 Integer.toString(range.getMaxLength()), 317 false); 318 } 319 } 320 if(this.m_nMinOccurs>0 && this.m_textComp.getText().trim().length()<1) { 321 this.m_errorLabel.setIcon( 322 IconManager.getInstance().getIcon("16-message-error.gif")); 323 if(this.m_textScroller!=null) { 324 m_textScroller.setBorder(BorderFactory.createLineBorder(Color.RED)); 325 } else { 326 m_textComp.setBorder(BorderFactory.createLineBorder(Color.RED)); 327 } 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 339 public void keyTyped(KeyEvent arg0) { 340 } 342 343 346 public void removeLayoutComponent(Component arg0) { 347 } 348 349 352 public void layoutContainer(Container arg0) { 353 m_nHeight = 30; 354 int nWidth = this.getParent().getWidth() - 18; 355 Dimension dim = null; 356 if(this.m_textScroller!=null) { 357 dim = this.m_textScroller.getPreferredSize(); 358 this.m_textScroller.setPreferredSize( 359 new Dimension (nWidth - 105, dim.height)); 360 } else { 361 dim = this.m_textComp.getPreferredSize(); 362 this.m_textComp.setPreferredSize( 363 new Dimension (nWidth - 105, dim.height)); 364 } 365 366 this.m_counter.setSize(this.m_counter.getPreferredSize()); 367 368 if(this.m_textScroller==null) { 369 this.m_textComp.setSize(this.m_textComp.getPreferredSize()); 370 this.m_textComp.setLocation(20, 0); 371 this.m_nHeight = this.m_nHeight + m_nValueHeight; 372 this.m_counter.setLocation( 373 this.m_textComp.getLocation().x 374 + this.m_textComp.getSize().width 375 + 5, 376 this.m_textComp.getLocation().y); 377 } else { 378 this.m_textScroller.setSize(this.m_textScroller.getPreferredSize()); 379 this.m_textScroller.setLocation(20, 0); 380 this.m_nHeight = this.m_nHeight + m_nValueHeight; 381 this.m_counter.setLocation( 382 this.m_textScroller.getLocation().x 383 + this.m_textScroller.getSize().width 384 + 5, 385 this.m_textScroller.getLocation().y); 386 } 387 388 this.m_errorLabel.setLocation(0,0); 389 this.m_errorLabel.setSize(15, 15); 390 this.repaint(); 391 } 392 393 396 public void addLayoutComponent(String arg0, Component arg1) { 397 } 398 399 402 public Dimension minimumLayoutSize(Container arg0) { 403 return this.getPreferredSize(); 404 } 405 406 409 public Dimension preferredLayoutSize(Container arg0) { 410 return this.getPreferredSize(); 411 } 412 413 416 public JPanel getPanel() { 417 return null; 418 } 419 420 protected String getValue() { 421 return this.m_textComp.getText().trim(); 422 } 423 424 427 public boolean isMetadataValid() { 428 return !this.m_errorLabel.getIcon().toString().equals(IconManager.getInstance().getIcon("16-message-error.gif").toString()); 429 } 430 431 432 433 436 public void setEnabled(boolean bEnabled) { 437 super.setEnabled(bEnabled); 438 if(this.m_textComp instanceof JTextArea ) { 439 ((JTextArea )this.m_textComp).setEditable(bEnabled); 440 } 441 } 442 443 } 444 | Popular Tags |