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