1 19 package org.openharmonise.him.metadata.range.swing.relatedevents; 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 42 public class RelatedEventRangeDisplay 43 extends AbstractRangeDisplay 44 implements RangeDisplay, LayoutManager, ActionListener { 45 46 private int m_nHeight = 0; 47 private int m_nWidth = 100; 48 49 private int m_nMinOccurs = 0; 50 private int m_nMaxOccurs = 1000; 51 52 private JButton m_addButton = null; 53 54 private JScrollPane m_scrollPane = null; 55 56 private ArrayList m_valuePanels = new ArrayList(); 57 private ArrayList m_removeButtons = new ArrayList(); 58 59 private boolean m_bShowingDelButtons = true; 60 61 private RelatedEventHeadingPanel m_heading; 62 63 66 public RelatedEventRangeDisplay(PropertyInstance propInstance) { 67 super(propInstance); 68 this.setup(); 69 } 70 71 74 public boolean isMetadataValid() { 75 boolean bValid = true; 76 for (int i = 0; i < this.getComponentCount(); i++) { 77 Component comp = this.getComponent(i); 78 if (comp instanceof AbstractRangeDisplay) { 79 if (!((AbstractRangeDisplay) comp).isMetadataValid()) { 80 bValid = false; 81 } 82 } 83 } 84 return bValid; 85 } 86 87 90 public JPanel getPanel() { 91 PropertyRange range = 92 (PropertyRange) this 93 .getPropertyInstance() 94 .getDefinition() 95 .getRange(); 96 List domains = this.getPropertyInstance().getDefinition().getDomains(); 97 Iterator itor = domains.iterator(); 98 99 while (itor.hasNext()) { 100 Domain domain = (Domain) itor.next(); 101 if (this.m_nMinOccurs < domain.getMinOccurs()) { 102 this.m_nMinOccurs = domain.getMinOccurs(); 103 } 104 if (domain.getMaxOccurs() != -1 105 && this.m_nMaxOccurs > domain.getMaxOccurs()) { 106 this.m_nMaxOccurs = domain.getMaxOccurs(); 107 } 108 109 } 110 111 PropertyInstance propInst = this.getPropertyInstance(); 112 List vals = propInst.getValues(); 113 114 int nCountMax = this.m_nMinOccurs; 115 if (vals.size() > nCountMax) { 116 nCountMax = vals.size(); 117 } 118 119 RelatedEventValueDisplay valPanel = null; 120 121 for (int i = 0; i < nCountMax; i++) { 122 try { 123 if (i < vals.size()) { 124 valPanel = 125 new RelatedEventValueDisplay( 126 this, 127 this.getPropertyInstance(), 128 (PropertyValue) vals.get(i)); 129 this.add(valPanel.getPanel()); 130 this.m_valuePanels.add(valPanel); 131 this.m_nHeight = 132 this.m_nHeight + valPanel.getPreferredSize().height + 5; 133 134 } 135 136 JButton removeButton = new JButton(); 137 String fontName = "Dialog"; 138 int fontSize = 13; 139 Font font = new Font(fontName, Font.BOLD, fontSize); 140 removeButton.setFont(font); 141 removeButton.setIcon( 142 IconManager.getInstance().getIcon( 143 "16-command-value-minus.gif")); 144 removeButton.setActionCommand("REMOVE"); 145 removeButton.addActionListener(this); 146 removeButton.setMargin(new Insets(2, 2, 2, 2)); 147 this.add(removeButton); 148 this.m_removeButtons.add(removeButton); 149 } catch (ClassCastException cce) { 150 151 } 152 } 153 154 if (this.m_nMaxOccurs == -1 155 || this.m_valuePanels.size() < this.m_nMaxOccurs) { 156 this.m_addButton = new JButton(); 157 String fontName = "Dialog"; 158 int fontSize = 13; 159 Font font = new Font(fontName, Font.BOLD, fontSize); 160 this.m_addButton.setFont(font); 161 this.m_addButton.setIcon( 162 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 if(valPanel != null){ 171 m_heading = 172 new RelatedEventHeadingPanel( 173 valPanel.getDateSize().width, 174 valPanel.getRelationshipSize().width, 175 valPanel.getTextSize().width); 176 177 this.add(m_heading); 178 m_nHeight = m_nHeight + m_heading.getPreferredSize().height + 5; 179 } 180 this.checkButtonVisibility(); 181 182 return this; 183 } 184 185 188 private void checkButtonVisibility() { 189 if (this.m_bShowingDelButtons 190 && this.m_removeButtons.size() <= this.m_nMinOccurs) { 191 Iterator itor = this.m_removeButtons.iterator(); 192 while (itor.hasNext()) { 193 JButton button = (JButton) itor.next(); 194 button.setVisible(false); 195 } 196 this.m_bShowingDelButtons = false; 197 } else if ( 198 !this.m_bShowingDelButtons 199 && this.m_removeButtons.size() > this.m_nMinOccurs) { 200 Iterator itor = this.m_removeButtons.iterator(); 201 while (itor.hasNext()) { 202 JButton button = (JButton) itor.next(); 203 button.setVisible(true); 204 } 205 this.m_bShowingDelButtons = true; 206 } 207 208 if (this.m_addButton != null 209 && this.m_addButton.isVisible() 210 && this.m_valuePanels.size() >= this.m_nMaxOccurs) { 211 this.m_addButton.setVisible(false); 212 this.m_nHeight = this.m_nHeight - this.m_addButton.getSize().height; 213 } else if ( 214 this.m_addButton != null 215 && this.m_valuePanels.size() < this.m_nMaxOccurs) { 216 this.m_addButton.setVisible(true); 217 this.m_nHeight = this.m_nHeight + this.m_addButton.getSize().height; 218 } 219 220 } 221 222 225 public void removeLayoutComponent(Component comp) { 226 } 227 228 231 public void layoutContainer(Container parent) { 232 this.checkButtonVisibility(); 233 234 int nYPos = 0; 235 236 int nXPos = 0; 237 238 if(m_heading != null){ 239 m_heading.setSize(m_heading.getPreferredSize()); 240 m_heading.setLocation(60, nYPos); 241 nYPos = nYPos + m_heading.getSize().height; 242 } 243 244 Iterator itor = this.m_valuePanels.iterator(); 245 int nCount = 0; 246 while (itor.hasNext()) { 247 RelatedEventValueDisplay valuePanel = 248 (RelatedEventValueDisplay) itor.next(); 249 Dimension dim = valuePanel.getPreferredSize(); 250 valuePanel.setSize(dim.width, dim.height); 251 252 if(m_nWidth < dim.width) { 253 m_nWidth = dim.width + 70; 254 } 255 256 valuePanel.setLocation(60, 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(5, nYPos+16); 263 } 264 265 nYPos = nYPos + valuePanel.getSize().height; 266 267 nCount++; 268 } 269 270 if (this.m_addButton != null && this.m_addButton.isVisible()) { 271 this.m_addButton.setSize(20, 20); 272 this.m_addButton.setLocation(5, nYPos+5); 273 nYPos = nYPos + 50; 274 } 275 276 this.m_nHeight = nYPos; 277 278 this.repaint(); 279 280 } 281 282 285 public void addLayoutComponent(String name, Component comp) { 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.getPropertyInstance().clearAllValues(); 320 this.m_valuePanels.remove(nIndex); 321 Iterator itor = this.m_valuePanels.iterator(); 322 while (itor.hasNext()) { 323 RelatedEventValueDisplay propValuePanel = 324 (RelatedEventValueDisplay) itor.next(); 325 this.getPropertyInstance().addValue(propValuePanel.getValue()); 326 } 327 328 } else if ( 329 ae.getActionCommand().equals("ADD") 330 && this.m_valuePanels.size() < this.m_nMaxOccurs) { 331 PropertyValue newValue = 332 (PropertyValue) this 333 .getPropertyInstance() 334 .getNewValueInstance(); 335 this.getPropertyInstance().addValue(newValue); 336 RelatedEventValueDisplay valPanel = 337 new RelatedEventValueDisplay( 338 this, 339 this.getPropertyInstance(), 340 newValue); 341 this.add(valPanel); 342 this.m_valuePanels.add(valPanel.getPanel()); 343 this.m_nHeight = 344 this.m_nHeight + valPanel.getPreferredSize().height; 345 JButton removeButton = new JButton(); 346 String fontName = "Dialog"; 347 int fontSize = 13; 348 Font font = new Font(fontName, Font.BOLD, fontSize); 349 removeButton.setFont(font); 350 removeButton.setIcon( 351 IconManager.getInstance().getIcon( 352 "16-command-value-minus.gif")); 353 removeButton.setActionCommand("REMOVE"); 354 removeButton.addActionListener(this); 355 removeButton.setMargin(new Insets(2, 2, 2, 2)); 356 this.add(removeButton); 357 this.m_removeButtons.add(removeButton); 358 if(m_heading == null){ 359 m_heading = 360 new RelatedEventHeadingPanel( 361 valPanel.getDateSize().width, 362 valPanel.getRelationshipSize().width, 363 valPanel.getTextSize().width); 364 365 this.add(m_heading); 366 m_nHeight = m_nHeight + m_heading.getPreferredSize().height + 5; 367 } 368 } 369 370 this.checkButtonVisibility(); 371 372 Component comp = this.getParent(); 373 while (!(comp instanceof MetadataTabs)) { 374 if (comp != null) { 375 comp = comp.getParent(); 376 } 377 } 378 if (comp != null) { 379 comp.validate(); 380 } 381 super.validateTab(); 382 383 } 384 385 388 public Dimension getPreferredSize() { 389 return new Dimension(m_nWidth, this.m_nHeight); 390 } 391 392 private void setup() { 393 BoxLayout layout = new BoxLayout(this, BoxLayout.Y_AXIS); 394 this.setLayout(this); 395 } 396 397 400 public boolean isResizeWidthEnabled() { 401 return false; 402 } 403 404 } 405 | Popular Tags |