1 19 package org.openharmonise.him.metadata.range.swing.booleanhandling; 20 21 import java.awt.*; 22 import java.awt.event.*; 23 import java.util.*; 24 import java.util.List ; 25 26 import javax.swing.*; 27 28 import org.openharmonise.him.metadata.range.swing.*; 29 import org.openharmonise.him.metadata.swing.*; 30 import org.openharmonise.vfs.gui.*; 31 import org.openharmonise.vfs.metadata.*; 32 import org.openharmonise.vfs.metadata.range.*; 33 import org.openharmonise.vfs.metadata.value.*; 34 35 36 43 public class BooleanRangeDisplay 44 extends AbstractRangeDisplay 45 implements RangeDisplay, LayoutManager, ActionListener { 46 47 50 private int m_nHeight = 10; 51 52 55 private int m_nMinOccurs = 1; 56 57 60 private int m_nMaxOccurs = 1; 61 62 65 private JButton m_addButton = null; 66 67 70 private ArrayList m_valuePanels = new ArrayList(); 71 72 75 private ArrayList m_removeButtons = new ArrayList(); 76 77 80 private boolean m_bShowingDelButtons = true; 81 82 87 public BooleanRangeDisplay(PropertyInstance propInstance) { 88 super(propInstance); 89 this.setup(); 90 } 91 92 96 private void setup() { 97 this.setLayout(this); 98 } 99 100 103 public JPanel getPanel() { 104 BooleanRange range = 105 (BooleanRange) this 106 .getPropertyInstance() 107 .getDefinition() 108 .getRange(); 109 List domains = this.getPropertyInstance().getDefinition().getDomains(); 110 Iterator itor = domains.iterator(); 111 112 while (itor.hasNext()) { 113 Domain domain = (Domain) itor.next(); 114 if (this.m_nMinOccurs < domain.getMinOccurs()) { 115 this.m_nMinOccurs = domain.getMinOccurs(); 116 } 117 118 } 119 120 PropertyInstance propInst = this.getPropertyInstance(); 121 List vals = propInst.getValues(); 122 123 int nCountMax=this.m_nMinOccurs; 124 if(vals.size()>nCountMax) { 125 nCountMax=vals.size(); 126 } 127 128 for(int i=0; i<nCountMax; i++) { 129 130 if (i < vals.size()) { 131 BooleanValuePanel valPanel = 132 new BooleanValuePanel(this, 133 this.getPropertyInstance(), 134 Boolean.toString( ((BooleanValue) vals.get(i)).getValue() ) ); 135 this.add(valPanel); 136 this.m_valuePanels.add(valPanel); 137 this.m_nHeight = 138 this.m_nHeight + valPanel.getPreferredSize().height + 5; 139 } else { 140 BooleanValuePanel valPanel = 141 new BooleanValuePanel(this,this.getPropertyInstance(), ""); 142 this.add(valPanel); 143 this.m_valuePanels.add(valPanel); 144 this.m_nHeight = 145 this.m_nHeight + valPanel.getPreferredSize().height + 5; 146 } 147 JButton removeButton = new JButton(); 148 String fontName = "Dialog"; 149 int fontSize = 13; 150 Font font = new Font(fontName, Font.BOLD, fontSize); 151 removeButton.setFont(font); 152 removeButton.setIcon(IconManager.getInstance().getIcon("16-command-value-minus.gif")); 153 removeButton.setActionCommand("REMOVE"); 154 removeButton.addActionListener(this); 155 removeButton.setMargin( new Insets(2,2,2,2) ); 156 this.add(removeButton); 157 this.m_removeButtons.add(removeButton); 158 159 } 160 161 if(this.m_valuePanels.size()<this.m_nMaxOccurs) { 162 this.m_addButton = new JButton(); 163 String fontName = "Dialog"; 164 int fontSize = 13; 165 Font font = new Font(fontName, Font.BOLD, fontSize); 166 this.m_addButton.setFont(font); 167 this.m_addButton.setIcon(IconManager.getInstance().getIcon("16-command-value-plus.gif")); 168 this.m_addButton.setActionCommand("ADD"); 169 this.m_addButton.setMargin( new Insets(2,2,2,2) ); 170 171 this.add(this.m_addButton); 172 this.m_addButton.addActionListener(this); 173 this.m_nHeight = this.m_nHeight + 25; 174 } 175 176 this.checkButtonVisibility(); 177 178 return this; 179 } 180 181 184 public Dimension getPreferredSize() { 185 this.layoutContainer(null); 186 int nWidth = this.getParent().getWidth()-25; 187 188 return new Dimension(nWidth, this.m_nHeight); 189 190 } 191 192 195 public void removeLayoutComponent(Component arg0) { 196 } 198 199 202 public void layoutContainer(Container arg0) { 203 int nWidth = this.getParent().getWidth()-50; 204 205 int nYPos = 0; 206 207 int nXPos = 0; 208 209 Iterator itor = this.m_valuePanels.iterator(); 210 int nCount = 0; 211 while(itor.hasNext()) { 212 BooleanValuePanel valuePanel = (BooleanValuePanel) itor.next(); 213 valuePanel.setSize(valuePanel.getPreferredSize().width-50, valuePanel.getPreferredSize().height); 214 valuePanel.setLocation(10, nYPos); 215 nXPos = valuePanel.getLocation().x + valuePanel.getSize().width; 216 217 nYPos = nYPos + valuePanel.getSize().height; 218 219 JButton removeButton = (JButton)this.m_removeButtons.get(nCount); 220 removeButton.setSize(20, 20); 221 removeButton.setLocation(nXPos - removeButton.getPreferredSize().width - 50, nYPos); 222 223 nCount++; 224 } 225 226 if(this.m_addButton!=null) { 227 this.m_addButton.setSize(20, 20); 228 this.m_addButton.setLocation(nXPos - (m_addButton.getPreferredSize().width*2) - 55, nYPos-3); 229 230 } 231 232 this.m_nHeight = nYPos; 234 this.repaint(); 235 } 236 237 240 public void addLayoutComponent(String arg0, Component arg1) { 241 } 243 244 247 public Dimension minimumLayoutSize(Container arg0) { 248 return this.getPreferredSize(); 249 } 250 251 254 public Dimension preferredLayoutSize(Container arg0) { 255 return this.getPreferredSize(); 256 } 257 258 261 public void actionPerformed(ActionEvent ae) { 262 PropertyInstance propInst = this.getPropertyInstance(); 263 264 if (ae.getActionCommand().equals("REMOVE") 265 && this.m_valuePanels.size() > this.m_nMinOccurs) { 266 int nIndex = this.m_removeButtons.indexOf(ae.getSource()); 267 this.remove((Component) this.m_removeButtons.get(nIndex)); 268 this.m_removeButtons.remove(nIndex); 269 this.m_nHeight = 270 this.m_nHeight 271 - ((Component) this.m_valuePanels.get(nIndex)) 272 .getSize() 273 .height; 274 this.remove((Component) this.m_valuePanels.get(nIndex)); 275 this.m_valuePanels.remove(nIndex); 276 } else if ( 277 ae.getActionCommand().equals("ADD") 278 && this.m_valuePanels.size() < this.m_nMaxOccurs) { 279 BooleanValuePanel valPanel = 280 new BooleanValuePanel(this,this.getPropertyInstance(), ""); 281 this.add(valPanel); 282 this.m_valuePanels.add(valPanel); 283 this.m_nHeight = 284 this.m_nHeight + valPanel.getPreferredSize().height; 285 JButton removeButton = new JButton(); 286 String fontName = "Dialog"; 287 int fontSize = 13; 288 Font font = new Font(fontName, Font.BOLD, fontSize); 289 removeButton.setFont(font); 290 removeButton.setIcon(IconManager.getInstance().getIcon("16-command-value-minus.gif")); 291 removeButton.setActionCommand("REMOVE"); 292 removeButton.addActionListener(this); 293 removeButton.setMargin( new Insets(2,2,2,2) ); 294 this.add(removeButton); 295 this.m_removeButtons.add(removeButton); 296 } 297 298 this.checkButtonVisibility(); 299 300 Component comp = this.getParent(); 301 while (!(comp instanceof MetadataTabs)) { 302 if (comp != null) { 303 comp = comp.getParent(); 304 } 305 } 306 if (comp != null) { 307 comp.validate(); 308 } 309 } 310 311 316 private void checkButtonVisibility() { 317 if (this.m_bShowingDelButtons 318 && this.m_removeButtons.size() <= this.m_nMinOccurs) { 319 Iterator itor = this.m_removeButtons.iterator(); 320 while (itor.hasNext()) { 321 JButton button = (JButton) itor.next(); 322 button.setVisible(false); 323 } 324 this.m_bShowingDelButtons = false; 325 } else if ( 326 !this.m_bShowingDelButtons 327 && this.m_removeButtons.size() > this.m_nMinOccurs) { 328 Iterator itor = this.m_removeButtons.iterator(); 329 while (itor.hasNext()) { 330 JButton button = (JButton) itor.next(); 331 button.setVisible(true); 332 } 333 this.m_bShowingDelButtons = true; 334 } 335 if (this.m_addButton != null 336 && this.m_addButton.isVisible() 337 && this.m_valuePanels.size() >= this.m_nMaxOccurs) { 338 this.m_addButton.setVisible(false); 339 this.m_nHeight = this.m_nHeight - this.m_addButton.getSize().height; 340 } else if ( 341 this.m_addButton != null 342 && !this.m_addButton.isVisible() 343 && this.m_valuePanels.size() < this.m_nMaxOccurs) { 344 this.m_addButton.setVisible(true); 345 this.m_nHeight = this.m_nHeight + this.m_addButton.getSize().height; 346 } 347 348 } 349 350 355 protected void valueChanged(String sValue) { 356 ArrayList aValues = new ArrayList(); 357 358 Iterator itor = this.m_valuePanels.iterator(); 359 while(itor.hasNext()) { 360 String sVal = ((BooleanValuePanel)itor.next()).getValue(); 361 if(!sVal.equals("")) { 362 BooleanValue val = (BooleanValue)this.getPropertyInstance().getNewValueInstance(); 363 val.setValue( Boolean.valueOf( sVal ).booleanValue() ); 364 aValues.add( val ); 365 } 366 } 367 if(this.getPropertyInstance().getValues().size()!=aValues.size() || !this.getPropertyInstance().getValues().containsAll(aValues)) { 368 this.getPropertyInstance().setValues(aValues); 369 } 370 371 super.validateTab(); 372 } 373 374 377 public boolean isMetadataValid() { 378 boolean bValid = true; 379 if(this.m_nMinOccurs>0) { 380 for (int i = 0; i < this.getComponentCount(); i++) { 381 Component comp = this.getComponent(i); 382 if(comp instanceof AbstractRangeDisplay) { 383 if(!((AbstractRangeDisplay)comp).isMetadataValid()) { 384 bValid=false; 385 } 386 } 387 } 388 } 389 return bValid; 390 } 391 } | Popular Tags |