1 19 package org.openharmonise.him.metadata.range.swing.datehandling; 20 21 import java.awt.Component ; 22 import java.awt.Container ; 23 import java.awt.Dimension ; 24 import java.awt.Font ; 25 import java.awt.Insets ; 26 import java.awt.LayoutManager ; 27 import java.awt.event.ActionEvent ; 28 import java.awt.event.ActionListener ; 29 import java.util.ArrayList ; 30 import java.util.Iterator ; 31 import java.util.List ; 32 33 import javax.swing.BoxLayout ; 34 import javax.swing.JButton ; 35 import javax.swing.JPanel ; 36 37 import org.openharmonise.him.metadata.range.swing.*; 38 import org.openharmonise.him.metadata.range.swing.propertyhandling.*; 39 import org.openharmonise.him.metadata.swing.*; 40 import org.openharmonise.vfs.*; 41 import org.openharmonise.vfs.gui.*; 42 import org.openharmonise.vfs.metadata.*; 43 import org.openharmonise.vfs.metadata.range.*; 44 import org.openharmonise.vfs.metadata.value.*; 45 46 47 54 public class DateRangeDisplay 55 extends AbstractRangeDisplay 56 implements RangeDisplay, LayoutManager , ActionListener { 57 58 61 private int m_nHeight = 10; 62 63 66 private int m_nMinOccurs = 0; 67 68 71 private int m_nMaxOccurs = 1000; 72 73 76 private JButton m_addButton = null; 77 78 81 private ArrayList m_valuePanels = new ArrayList (); 82 83 86 private ArrayList m_removeButtons = new ArrayList (); 87 88 91 private boolean m_bShowingDelButtons = true; 92 93 98 public DateRangeDisplay(PropertyInstance propInstance) { 99 super(propInstance); 100 this.setup(); 101 } 102 103 107 private void setup() { 108 BoxLayout layout = new BoxLayout (this, BoxLayout.Y_AXIS); 109 this.setLayout(this); 110 } 111 112 115 public JPanel getPanel() { 116 DateRange range = 117 (DateRange) this 118 .getPropertyInstance() 119 .getDefinition() 120 .getRange(); 121 List domains = this.getPropertyInstance().getDefinition().getDomains(); 122 Iterator itor = domains.iterator(); 123 124 while (itor.hasNext()) { 125 Domain domain = (Domain)itor.next(); 126 boolean bApplicableDomain = false; 127 String sFilePath = ((VersionedVirtualFile)this.getPropertyInstance().getVirtualFile()).getLogicalPath(); 128 Iterator itor2 = domain.getPaths().iterator(); 129 while (itor2.hasNext()) { 130 String sDomainPath = (String ) itor2.next(); 131 if(sFilePath.startsWith(sDomainPath)) { 132 bApplicableDomain=true; 133 } 134 } 135 136 if(bApplicableDomain) { 137 if(this.m_nMinOccurs < domain.getMinOccurs()) { 138 this.m_nMinOccurs = domain.getMinOccurs(); 139 } 140 if (domain.getMaxOccurs()!=-1 && this.m_nMaxOccurs > domain.getMaxOccurs()) { 141 this.m_nMaxOccurs = domain.getMaxOccurs(); 142 } 143 } 144 145 } 146 147 PropertyInstance propInst = this.getPropertyInstance(); 148 List vals = propInst.getValues(); 149 150 int nCountMax=this.m_nMinOccurs; 151 if(nCountMax==0) { 152 nCountMax=1; 153 } 154 155 if(vals.size()>nCountMax) { 156 nCountMax=vals.size(); 157 } 158 159 for(int i=0; i<nCountMax; i++) { 160 boolean bNoValAllowed = true; 161 162 if(i<m_nMinOccurs) { 163 bNoValAllowed = false; 164 } 165 166 if (i < vals.size()) { 167 168 DateValuePanel valPanel = 169 new DateValuePanel(this, 170 this.getPropertyInstance(), 171 ((DateValue) vals.get(i)).getValue(),bNoValAllowed); 172 this.add(valPanel); 173 this.m_valuePanels.add(valPanel); 174 this.m_nHeight = 175 this.m_nHeight + valPanel.getPreferredSize().height + 5; 176 177 178 } else { 179 DateValuePanel valPanel = 180 new DateValuePanel(this, this.getPropertyInstance(), "",bNoValAllowed); 181 this.add(valPanel); 182 this.m_valuePanels.add(valPanel); 183 this.m_nHeight = 184 this.m_nHeight + valPanel.getPreferredSize().height + 5; 185 } 186 JButton removeButton = new JButton (); 187 String fontName = "Dialog"; 188 int fontSize = 13; 189 Font font = new Font (fontName, Font.BOLD, fontSize); 190 removeButton.setFont(font); 191 removeButton.setIcon(IconManager.getInstance().getIcon("16-command-value-minus.gif")); 192 removeButton.setActionCommand("REMOVE"); 193 removeButton.addActionListener(this); 194 removeButton.setMargin( new Insets (2,2,2,2) ); 195 this.add(removeButton); 196 this.m_removeButtons.add(removeButton); 197 198 } 199 200 if(this.m_valuePanels.size()<this.m_nMaxOccurs) { 201 this.m_addButton = new JButton (); 202 String fontName = "Dialog"; 203 int fontSize = 13; 204 Font font = new Font (fontName, Font.BOLD, fontSize); 205 this.m_addButton.setFont(font); 206 this.m_addButton.setIcon(IconManager.getInstance().getIcon("16-command-value-plus.gif")); 207 this.m_addButton.setActionCommand("ADD"); 208 this.m_addButton.setMargin( new Insets (2,2,2,2) ); 209 this.add(this.m_addButton); 210 this.m_addButton.addActionListener(this); 211 this.m_nHeight = this.m_nHeight + 25; 212 } 213 214 this.checkButtonVisibility(); 215 216 return this; 217 } 218 219 222 public Dimension getPreferredSize() { 223 this.layoutContainer(null); 224 int nWidth = this.getParent().getWidth()-25; 225 226 return new Dimension (nWidth, this.m_nHeight); 227 } 228 229 232 public void removeLayoutComponent(Component arg0) { 233 } 234 235 238 public void layoutContainer(Container arg0) { 239 if(this.getParent() instanceof PropertyValuePanel) { 240 this.m_nMinOccurs=0; 241 this.m_nMaxOccurs=1; 242 } 243 244 this.checkButtonVisibility(); 245 int nWidth = this.getParent().getWidth()-50; 246 247 int nYPos = 0; 248 249 int nXPos = 0; 250 251 Iterator itor = this.m_valuePanels.iterator(); 252 int nCount = 0; 253 while(itor.hasNext()) { 254 DateValuePanel valuePanel = (DateValuePanel) itor.next(); 255 valuePanel.setSize(valuePanel.getPreferredSize().width-50, valuePanel.getPreferredSize().height); 256 valuePanel.setLocation(10, nYPos); 257 nXPos = valuePanel.getLocation().x + valuePanel.getSize().width; 258 259 JButton removeButton = (JButton )this.m_removeButtons.get(nCount); 260 if(removeButton.isVisible()) { 261 removeButton.setSize(20, 20); 262 removeButton.setLocation(nXPos, nYPos); 263 } 264 265 nYPos = nYPos + valuePanel.getSize().height; 266 267 nYPos = nYPos + 3; 268 269 nCount++; 270 } 271 272 if(this.m_addButton!=null && this.m_addButton.isVisible()) { 273 this.m_addButton.setSize(20, 20); 274 this.m_addButton.setLocation(nXPos+30, nYPos-37); 275 } 276 277 this.m_nHeight = nYPos-17; 278 279 this.repaint(); 280 } 281 282 285 public void addLayoutComponent(String arg0, Component arg1) { 286 } 287 288 291 public Dimension minimumLayoutSize(Container arg0) { 292 return this.getPreferredSize(); 293 } 294 295 298 public Dimension preferredLayoutSize(Container arg0) { 299 return this.getPreferredSize(); 300 } 301 302 305 public void actionPerformed(ActionEvent ae) { 306 PropertyInstance propInst = this.getPropertyInstance(); 307 308 if (ae.getActionCommand().equals("REMOVE") 309 && this.m_valuePanels.size() > this.m_nMinOccurs) { 310 int nIndex = this.m_removeButtons.indexOf(ae.getSource()); 311 this.remove((Component ) this.m_removeButtons.get(nIndex)); 312 this.m_removeButtons.remove(nIndex); 313 this.m_nHeight = 314 this.m_nHeight 315 - ((Component ) this.m_valuePanels.get(nIndex)) 316 .getSize() 317 .height; 318 this.remove((Component ) this.m_valuePanels.get(nIndex)); 319 this.m_valuePanels.remove(nIndex); 320 this.valueChanged(null); 321 } else if ( 322 ae.getActionCommand().equals("ADD") 323 && this.m_valuePanels.size() < this.m_nMaxOccurs) { 324 DateValuePanel valPanel = 325 new DateValuePanel(this, this.getPropertyInstance(), "",true); 326 this.add(valPanel); 327 this.m_valuePanels.add(valPanel); 328 this.m_nHeight = 329 this.m_nHeight + valPanel.getPreferredSize().height; 330 JButton removeButton = new JButton (); 331 String fontName = "Dialog"; 332 int fontSize = 13; 333 Font font = new Font (fontName, Font.BOLD, fontSize); 334 removeButton.setFont(font); 335 removeButton.setIcon(IconManager.getInstance().getIcon("16-command-value-minus.gif")); 336 removeButton.setActionCommand("REMOVE"); 337 removeButton.addActionListener(this); 338 removeButton.setMargin( new Insets (2,2,2,2) ); 339 this.add(removeButton); 340 this.m_removeButtons.add(removeButton); 341 } 342 343 this.checkButtonVisibility(); 344 345 Component comp = this.getParent(); 346 comp.validate(); 347 while (!(comp instanceof MetadataTabs)) { 348 if (comp != null) { 349 comp = comp.getParent(); 350 } 351 } 352 if (comp != null) { 353 comp.validate(); 354 } 355 } 356 357 362 private void checkButtonVisibility() { 363 if(this.m_nMinOccurs==0 && this.m_nMaxOccurs==1) { 364 Iterator itor = this.m_removeButtons.iterator(); 365 while (itor.hasNext()) { 366 JButton button = (JButton ) itor.next(); 367 button.setVisible(false); 368 } 369 if(this.m_addButton!=null) { 370 this.m_addButton.setVisible(false); 371 } 372 this.m_bShowingDelButtons = false; 373 return; 374 } 375 if (this.m_bShowingDelButtons 376 && this.m_removeButtons.size() <= this.m_nMinOccurs) { 377 Iterator itor = this.m_removeButtons.iterator(); 378 while (itor.hasNext()) { 379 JButton button = (JButton ) itor.next(); 380 button.setVisible(false); 381 } 382 this.m_bShowingDelButtons = false; 383 } else if ( 384 !this.m_bShowingDelButtons 385 && this.m_removeButtons.size() > this.m_nMinOccurs) { 386 Iterator itor = this.m_removeButtons.iterator(); 387 while (itor.hasNext()) { 388 JButton button = (JButton ) itor.next(); 389 button.setVisible(true); 390 } 391 this.m_bShowingDelButtons = true; 392 } 393 if (this.m_addButton != null 394 && this.m_addButton.isVisible() 395 && this.m_valuePanels.size() >= this.m_nMaxOccurs) { 396 this.m_addButton.setVisible(false); 397 this.m_nHeight = this.m_nHeight - this.m_addButton.getSize().height; 398 } else if ( 399 this.m_addButton != null 400 && this.m_valuePanels.size() < this.m_nMaxOccurs) { 401 this.m_addButton.setVisible(true); 402 this.m_nHeight = this.m_nHeight + this.m_addButton.getSize().height; 403 } 404 405 } 406 407 412 protected void valueChanged(String sValue) { 413 ArrayList aValues = new ArrayList (); 414 415 Iterator itor = this.m_valuePanels.iterator(); 416 while(itor.hasNext()) { 417 DateValue val = (DateValue)this.getPropertyInstance().getNewValueInstance(); 418 val.setValue(((DateValuePanel)itor.next()).getValue()); 419 aValues.add( val ); 420 } 421 if(!this.getPropertyInstance().getValues().containsAll(aValues) 422 || !aValues.containsAll(this.getPropertyInstance().getValues())) { 423 this.getPropertyInstance().setValues(aValues); 424 } 425 } 426 427 430 public boolean isMetadataValid() { 431 boolean bValid = true; 432 for (int i = 0; i < this.getComponentCount(); i++) { 433 Component comp = this.getComponent(i); 434 if(comp instanceof AbstractRangeDisplay) { 435 if(!((AbstractRangeDisplay)comp).isMetadataValid()) { 436 bValid=false; 437 } 438 } 439 } 440 441 if(bValid == true && m_nMinOccurs > 0) { 442 445 List vals = getPropertyInstance().getValues(); 446 447 if(vals.size() < m_nMinOccurs) { 448 bValid = false; 449 } 450 451 for(int i=0;i<m_nMinOccurs && bValid == true;i++) { 452 DateValue val = (DateValue) vals.get(i); 453 String sVal = val.getValue(); 454 if(sVal == null || sVal.trim().length() == 0) { 455 bValid = false; 456 } 457 } 458 } 459 460 461 return bValid; 462 } 463 } | Popular Tags |