| 1 19 package org.openharmonise.him.metadata.range.swing.datetimehandling; 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 51 public class DateTimeRangeDisplay 52 extends AbstractRangeDisplay 53 implements RangeDisplay, LayoutManager , ActionListener { 54 55 private int m_nHeight = 10; 56 57 private int m_nMinOccurs = 1; 58 private int m_nMaxOccurs = 1000; 59 60 private JButton m_addButton = null; 61 62 private ArrayList m_valuePanels = new ArrayList (); 63 private ArrayList m_removeButtons = new ArrayList (); 64 65 private boolean m_bShowingDelButtons = true; 66 67 70 public DateTimeRangeDisplay(PropertyInstance propInstance) { 71 super(propInstance); 72 this.setup(); 73 } 74 75 private void setup() { 76 BoxLayout layout = new BoxLayout (this, BoxLayout.Y_AXIS); 77 this.setLayout(this); 78 } 79 80 83 public JPanel getPanel() { 84 DateTimeRange range = 85 (DateTimeRange) this 86 .getPropertyInstance() 87 .getDefinition() 88 .getRange(); 89 List domains = this.getPropertyInstance().getDefinition().getDomains(); 90 Iterator itor = domains.iterator(); 91 92 while (itor.hasNext()) { 93 Domain domain = (Domain)itor.next(); 94 boolean bApplicableDomain = false; 95 String sFilePath = ((VersionedVirtualFile)this.getPropertyInstance().getVirtualFile()).getLogicalPath(); 96 Iterator itor2 = domain.getPaths().iterator(); 97 while (itor2.hasNext()) { 98 String sDomainPath = (String ) itor2.next(); 99 if(sFilePath.startsWith(sDomainPath)) { 100 bApplicableDomain=true; 101 } 102 } 103 104 if(bApplicableDomain) { 105 if (this.m_nMinOccurs < domain.getMinOccurs()) { 106 this.m_nMinOccurs = domain.getMinOccurs(); 107 } 108 if (domain.getMaxOccurs()!=-1 && this.m_nMaxOccurs > domain.getMaxOccurs()) { 109 this.m_nMaxOccurs = domain.getMaxOccurs(); 110 } 111 } 112 113 } 114 115 PropertyInstance propInst = this.getPropertyInstance(); 116 List vals = propInst.getValues(); 117 118 int nCountMax=this.m_nMinOccurs; 119 if(vals.size()>nCountMax) { 120 nCountMax=vals.size(); 121 } 122 123 for(int i=0; i<nCountMax; i++) { 124 125 if (i < vals.size()) { 126 DateTimeValuePanel valPanel = 127 new DateTimeValuePanel(this, 128 this.getPropertyInstance(), 129 ((DateTimeValue) vals.get(i)).getValue()); 130 this.add(valPanel); 131 this.m_valuePanels.add(valPanel); 132 this.m_nHeight = 133 this.m_nHeight + valPanel.getPreferredSize().height + 5; 134 } else { 135 DateTimeValuePanel valPanel = 136 new DateTimeValuePanel(this, this.getPropertyInstance(), ""); 137 this.add(valPanel); 138 this.m_valuePanels.add(valPanel); 139 this.m_nHeight = 140 this.m_nHeight + valPanel.getPreferredSize().height + 5; 141 } 142 JButton removeButton = new JButton (); 143 String fontName = "Dialog"; 144 int fontSize = 13; 145 Font font = new Font (fontName, Font.BOLD, fontSize); 146 removeButton.setFont(font); 147 removeButton.setIcon(IconManager.getInstance().getIcon("16-command-value-minus.gif")); 148 removeButton.setActionCommand("REMOVE"); 149 removeButton.addActionListener(this); 150 removeButton.setMargin( new Insets (2,2,2,2) ); 151 this.add(removeButton); 152 this.m_removeButtons.add(removeButton); 153 154 } 155 156 if(this.m_valuePanels.size()<this.m_nMaxOccurs) { 157 this.m_addButton = new JButton (); 158 String fontName = "Dialog"; 159 int fontSize = 13; 160 Font font = new Font (fontName, Font.BOLD, fontSize); 161 this.m_addButton.setFont(font); 162 this.m_addButton.setIcon(IconManager.getInstance().getIcon("16-command-value-plus.gif")); 163 this.m_addButton.setActionCommand("ADD"); 164 this.m_addButton.setMargin( new Insets (2,2,2,2) ); 165 this.add(this.m_addButton); 166 this.m_addButton.addActionListener(this); 167 this.m_nHeight = this.m_nHeight + 25; 168 } 169 170 this.checkButtonVisibility(); 171 172 return this; 173 } 174 175 178 public Dimension getPreferredSize() { 179 this.layoutContainer(null); 180 int nWidth = this.getParent().getWidth()-25; 181 182 return new Dimension (nWidth, this.m_nHeight); 183 } 184 185 188 public void removeLayoutComponent(Component arg0) { 189 } 190 191 194 public void layoutContainer(Container arg0) { 195 if(this.getParent() instanceof PropertyValuePanel) { 196 this.m_nMinOccurs=0; 197 this.m_nMaxOccurs=1; 198 } 199 200 this.checkButtonVisibility(); 201 int nWidth = this.getParent().getWidth()-50; 202 203 int nYPos = 0; 204 205 int nXPos = 0; 206 207 Iterator itor = this.m_valuePanels.iterator(); 208 int nCount = 0; 209 while(itor.hasNext()) { 210 211 212 DateTimeValuePanel valuePanel = (DateTimeValuePanel) itor.next(); 213 valuePanel.setSize(valuePanel.getPreferredSize().width-50, valuePanel.getPreferredSize().height); 214 valuePanel.setLocation(10, nYPos); 215 216 nYPos = nYPos + valuePanel.getSize().height; 217 218 nYPos = nYPos + 3; 219 220 nXPos = valuePanel.getLocation().x + valuePanel.getSize().width; 221 222 JButton removeButton = (JButton )this.m_removeButtons.get(nCount); 223 if(removeButton.isVisible()) { 224 removeButton.setSize(20, 20); 225 removeButton.setLocation(nXPos, nYPos - 37); 226 } 227 228 229 230 nCount++; 231 } 232 233 if(this.m_addButton!=null && this.m_addButton.isVisible()) { 234 this.m_addButton.setSize(20, 20); 235 this.m_addButton.setLocation(nXPos+30, nYPos-37); 236 } 237 238 this.m_nHeight = nYPos-17; 239 240 this.repaint(); 241 } 242 243 246 public void addLayoutComponent(String arg0, Component arg1) { 247 } 248 249 252 public Dimension minimumLayoutSize(Container arg0) { 253 return this.getPreferredSize(); 254 } 255 256 259 public Dimension preferredLayoutSize(Container arg0) { 260 return this.getPreferredSize(); 261 } 262 263 266 public void actionPerformed(ActionEvent ae) { 267 PropertyInstance propInst = this.getPropertyInstance(); 268 269 if (ae.getActionCommand().equals("REMOVE") 270 && this.m_valuePanels.size() > this.m_nMinOccurs) { 271 int nIndex = this.m_removeButtons.indexOf(ae.getSource()); 272 this.remove((Component ) this.m_removeButtons.get(nIndex)); 273 this.m_removeButtons.remove(nIndex); 274 this.m_nHeight = 275 this.m_nHeight 276 - ((Component ) this.m_valuePanels.get(nIndex)) 277 .getSize() 278 .height; 279 this.remove((Component ) this.m_valuePanels.get(nIndex)); 280 this.m_valuePanels.remove(nIndex); 281 this.valueChanged(null); 282 } else if ( 283 ae.getActionCommand().equals("ADD") 284 && this.m_valuePanels.size() < this.m_nMaxOccurs) { 285 DateTimeValuePanel valPanel = 286 new DateTimeValuePanel(this, this.getPropertyInstance(), ""); 287 this.add(valPanel); 288 this.m_valuePanels.add(valPanel); 289 this.m_nHeight = 290 this.m_nHeight + valPanel.getPreferredSize().height; 291 JButton removeButton = new JButton (); 292 String fontName = "Dialog"; 293 int fontSize = 13; 294 Font font = new Font (fontName, Font.BOLD, fontSize); 295 removeButton.setFont(font); 296 removeButton.setIcon(IconManager.getInstance().getIcon("16-command-value-minus.gif")); 297 removeButton.setActionCommand("REMOVE"); 298 removeButton.addActionListener(this); 299 removeButton.setMargin( new Insets (2,2,2,2) ); 300 this.add(removeButton); 301 this.m_removeButtons.add(removeButton); 302 } 303 304 this.checkButtonVisibility(); 305 306 Component comp = this.getParent(); 307 comp.validate(); 308 while (!(comp instanceof MetadataTabs)) { 309 if (comp != null) { 310 comp = comp.getParent(); 311 } 312 } 313 if (comp != null) { 314 comp.validate(); 315 } 316 } 317 318 private void checkButtonVisibility() { 319 if(this.m_nMinOccurs==0 && this.m_nMaxOccurs==1) { 320 Iterator itor = this.m_removeButtons.iterator(); 321 while (itor.hasNext()) { 322 JButton button = (JButton ) itor.next(); 323 button.setVisible(false); 324 } 325 this.m_addButton.setVisible(false); 326 this.m_bShowingDelButtons = false; 327 return; 328 } 329 if (this.m_bShowingDelButtons 330 && this.m_removeButtons.size() <= this.m_nMinOccurs) { 331 Iterator itor = this.m_removeButtons.iterator(); 332 while (itor.hasNext()) { 333 JButton button = (JButton ) itor.next(); 334 button.setVisible(false); 335 } 336 this.m_bShowingDelButtons = false; 337 } else if ( 338 !this.m_bShowingDelButtons 339 && this.m_removeButtons.size() > this.m_nMinOccurs) { 340 Iterator itor = this.m_removeButtons.iterator(); 341 while (itor.hasNext()) { 342 JButton button = (JButton ) itor.next(); 343 button.setVisible(true); 344 } 345 this.m_bShowingDelButtons = true; 346 } 347 if (this.m_addButton != null 348 && this.m_addButton.isVisible() 349 && this.m_valuePanels.size() >= this.m_nMaxOccurs) { 350 this.m_addButton.setVisible(false); 351 this.m_nHeight = this.m_nHeight - this.m_addButton.getSize().height; 352 } else if ( 353 this.m_addButton != null 354 && this.m_valuePanels.size() < this.m_nMaxOccurs) { 355 this.m_addButton.setVisible(true); 356 this.m_nHeight = this.m_nHeight + this.m_addButton.getSize().height; 357 } 358 359 } 360 361 protected void valueChanged(String sValue) { 362 ArrayList aValues = new ArrayList (); 363 364 Iterator itor = this.m_valuePanels.iterator(); 365 while(itor.hasNext()) { 366 DateTimeValue val = (DateTimeValue)this.getPropertyInstance().getNewValueInstance(); 367 val.setValue(((DateTimeValuePanel)itor.next()).getValue()); 368 aValues.add( val ); 369 } 370 if(!this.getPropertyInstance().getValues().containsAll(aValues) 371 || !aValues.containsAll(this.getPropertyInstance().getValues())) { 372 this.getPropertyInstance().setValues(aValues); 373 } 374 } 375 376 379 public boolean isMetadataValid() { 380 boolean bValid = true; 381 for (int i = 0; i < this.getComponentCount(); i++) { 382 Component comp = this.getComponent(i); 383 if(comp instanceof AbstractRangeDisplay) { 384 if(!((AbstractRangeDisplay)comp).isMetadataValid()) { 385 bValid=false; 386 } 387 } 388 } 389 return bValid; 390 } 391 392 393 } | Popular Tags |