1 19 package org.openharmonise.him.metadata.range.swing.floathandling; 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 FloatRangeDisplay 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 FloatRangeDisplay(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 FloatRange range = 85 (FloatRange) 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 FloatValuePanel valPanel = 127 new FloatValuePanel(this, 128 this.getPropertyInstance(), 129 Float.toString( ((FloatValue) 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 FloatValuePanel valPanel = 136 new FloatValuePanel(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 return new Dimension (nWidth, this.m_nHeight); 182 } 183 184 187 public void removeLayoutComponent(Component arg0) { 188 } 189 190 193 public void layoutContainer(Container arg0) { 194 if(this.getParent() instanceof PropertyValuePanel) { 195 this.m_nMinOccurs=0; 196 this.m_nMaxOccurs=1; 197 } 198 199 this.checkButtonVisibility(); 200 int nWidth = this.getParent().getWidth()-50; 201 202 int nYPos = 0; 203 204 int nXPos = 0; 205 206 Iterator itor = this.m_valuePanels.iterator(); 207 int nCount = 0; 208 while(itor.hasNext()) { 209 FloatValuePanel valuePanel = (FloatValuePanel) itor.next(); 210 valuePanel.setSize(valuePanel.getPreferredSize().width-50, valuePanel.getPreferredSize().height); 211 valuePanel.setLocation(0, nYPos); 212 nXPos = valuePanel.getLocation().x + valuePanel.getSize().width; 213 214 JButton removeButton = (JButton )this.m_removeButtons.get(nCount); 215 if(removeButton.isVisible()) { 216 removeButton.setSize(20, 20); 217 removeButton.setLocation(nXPos, nYPos); 218 } 219 220 nYPos = nYPos + valuePanel.getSize().height; 221 222 nCount++; 223 } 224 225 if(this.m_addButton!=null && this.m_addButton.isVisible()) { 226 this.m_addButton.setSize(20, 20); 227 this.m_addButton.setLocation(nXPos+30, nYPos-30); 228 } 229 230 this.m_nHeight = nYPos-10; 231 232 this.repaint(); 233 } 234 235 238 public void addLayoutComponent(String arg0, Component arg1) { 239 } 240 241 244 public Dimension minimumLayoutSize(Container arg0) { 245 return this.getPreferredSize(); 246 } 247 248 251 public Dimension preferredLayoutSize(Container arg0) { 252 return this.getPreferredSize(); 253 } 254 255 258 public void actionPerformed(ActionEvent ae) { 259 PropertyInstance propInst = this.getPropertyInstance(); 260 261 if (ae.getActionCommand().equals("REMOVE") 262 && this.m_valuePanels.size() > this.m_nMinOccurs) { 263 int nIndex = this.m_removeButtons.indexOf(ae.getSource()); 264 this.remove((Component ) this.m_removeButtons.get(nIndex)); 265 this.m_removeButtons.remove(nIndex); 266 this.m_nHeight = 267 this.m_nHeight 268 - ((Component ) this.m_valuePanels.get(nIndex)) 269 .getSize() 270 .height; 271 this.remove((Component ) this.m_valuePanels.get(nIndex)); 272 this.m_valuePanels.remove(nIndex); 273 this.valueChanged(null); 274 } else if ( 275 ae.getActionCommand().equals("ADD") 276 && this.m_valuePanels.size() < this.m_nMaxOccurs) { 277 FloatValuePanel valPanel = 278 new FloatValuePanel(this,this.getPropertyInstance(), ""); 279 this.add(valPanel); 280 this.m_valuePanels.add(valPanel); 281 this.m_nHeight = 282 this.m_nHeight + valPanel.getPreferredSize().height; 283 JButton removeButton = new JButton (); 284 String fontName = "Dialog"; 285 int fontSize = 13; 286 Font font = new Font (fontName, Font.BOLD, fontSize); 287 removeButton.setFont(font); 288 removeButton.setIcon(IconManager.getInstance().getIcon("16-command-value-minus.gif")); 289 removeButton.setActionCommand("REMOVE"); 290 removeButton.addActionListener(this); 291 removeButton.setMargin( new Insets (2,2,2,2) ); 292 this.add(removeButton); 293 this.m_removeButtons.add(removeButton); 294 } 295 296 this.checkButtonVisibility(); 297 298 Component comp = this.getParent(); 299 comp.validate(); 300 while (!(comp instanceof MetadataTabs)) { 301 if (comp != null) { 302 comp = comp.getParent(); 303 } 304 } 305 if (comp != null) { 306 comp.validate(); 307 } 308 } 309 310 private void checkButtonVisibility() { 311 if(this.m_nMinOccurs==0 && this.m_nMaxOccurs==1) { 312 Iterator itor = this.m_removeButtons.iterator(); 313 while (itor.hasNext()) { 314 JButton button = (JButton ) itor.next(); 315 button.setVisible(false); 316 } 317 this.m_addButton.setVisible(false); 318 this.m_bShowingDelButtons = false; 319 return; 320 } 321 if (this.m_bShowingDelButtons 322 && this.m_removeButtons.size() <= this.m_nMinOccurs) { 323 Iterator itor = this.m_removeButtons.iterator(); 324 while (itor.hasNext()) { 325 JButton button = (JButton ) itor.next(); 326 button.setVisible(false); 327 } 328 this.m_bShowingDelButtons = false; 329 } else if ( 330 !this.m_bShowingDelButtons 331 && this.m_removeButtons.size() > this.m_nMinOccurs) { 332 Iterator itor = this.m_removeButtons.iterator(); 333 while (itor.hasNext()) { 334 JButton button = (JButton ) itor.next(); 335 button.setVisible(true); 336 } 337 this.m_bShowingDelButtons = true; 338 } 339 if (this.m_addButton != null 340 && this.m_addButton.isVisible() 341 && this.m_valuePanels.size() >= this.m_nMaxOccurs) { 342 this.m_addButton.setVisible(false); 343 this.m_nHeight = this.m_nHeight - this.m_addButton.getSize().height; 344 } else if ( 345 this.m_addButton != null 346 && this.m_valuePanels.size() < this.m_nMaxOccurs) { 347 this.m_addButton.setVisible(true); 348 this.m_nHeight = this.m_nHeight + this.m_addButton.getSize().height; 349 } 350 351 } 352 353 protected void valueChanged(String sValue) { 354 ArrayList aValues = new ArrayList (); 355 356 Iterator itor = this.m_valuePanels.iterator(); 357 while(itor.hasNext()) { 358 String sTempValue = ((FloatValuePanel)itor.next()).getValue(); 359 if(!sTempValue.equals("")) { 360 FloatValue val = (FloatValue)this.getPropertyInstance().getNewValueInstance(); 361 val.setValue( Float.parseFloat(sTempValue) ); 362 aValues.add( val ); 363 } 364 } 365 if(!this.getPropertyInstance().getValues().containsAll(aValues) 366 || !aValues.containsAll(this.getPropertyInstance().getValues())) { 367 this.getPropertyInstance().setValues(aValues); 368 } 369 } 370 371 374 public boolean isMetadataValid() { 375 boolean bValid = true; 376 for (int i = 0; i < this.getComponentCount(); i++) { 377 Component comp = this.getComponent(i); 378 if(comp instanceof AbstractRangeDisplay) { 379 if(!((AbstractRangeDisplay)comp).isMetadataValid()) { 380 bValid=false; 381 } 382 } 383 } 384 return bValid; 385 } 386 } | Popular Tags |