1 7 package org.openharmonise.him.metadata.range.swing.domain; 8 9 import java.awt.Color ; 10 import java.awt.Component ; 11 import java.awt.Container ; 12 import java.awt.Dimension ; 13 import java.awt.LayoutManager ; 14 import java.awt.event.ActionEvent ; 15 import java.awt.event.ActionListener ; 16 import java.awt.event.MouseEvent ; 17 import java.awt.event.MouseListener ; 18 import java.util.ArrayList ; 19 import java.util.List ; 20 21 import javax.swing.JComboBox ; 22 import javax.swing.JLabel ; 23 import javax.swing.JPanel ; 24 import javax.swing.JSpinner ; 25 import javax.swing.SpinnerModel ; 26 import javax.swing.SpinnerNumberModel ; 27 import javax.swing.event.ChangeEvent ; 28 import javax.swing.event.ChangeListener ; 29 30 import org.openharmonise.him.*; 31 import org.openharmonise.swing.FontManager; 32 import org.openharmonise.vfs.*; 33 import org.openharmonise.vfs.metadata.value.*; 34 import org.openharmonise.vfs.servers.ServerList; 35 36 37 43 public class DomainSelectionCell 44 extends JPanel 45 implements MouseListener , LayoutManager , ActionListener , ChangeListener { 46 47 private boolean m_bSelected = false; 48 49 private Color m_selectedColor = new Color (173, 169, 143); 50 51 private JLabel m_collectionLabel = null; 52 53 private JComboBox m_typeCombo = null; 54 55 private JSpinner m_minOccursSpinner = null; 56 private JSpinner m_maxOccursSpinner = null; 57 58 private DomainValue m_valueResource = null; 59 private DomainValue m_valueCollection = null; 60 61 public static String TYPE_COLLECTION = "Collections"; 62 public static String TYPE_RESOURCE = "Resources"; 63 public static String TYPE_BOTH = "Both"; 64 65 private DomainSelectionList m_list = null; 66 67 private boolean m_bListenToCombo = false; 68 69 private boolean m_bOccursSpinnersEnabled = true; 70 71 74 public DomainSelectionCell(DomainSelectionList list, DomainValue value) throws Exception { 75 super(); 76 this.m_list = list; 77 if(value.getResourceType()==DomainValue.RESOURCE) { 78 this.m_valueResource = value; 79 } else if(value.getResourceType()==DomainValue.COLLECTION) { 80 this.m_valueCollection = value; 81 } 82 this.setup(); 83 } 84 85 public void setOccursSpinnersEnabled(boolean bEnabled) { 86 m_bOccursSpinnersEnabled = bEnabled; 87 if(this.m_typeCombo.getSelectedItem().equals(DomainSelectionCell.TYPE_COLLECTION) 88 || this.m_typeCombo.getSelectedItem().equals(DomainSelectionCell.TYPE_BOTH) 89 || !bEnabled) { 90 this.m_minOccursSpinner.setEnabled(false); 91 } else if(this.m_typeCombo.getSelectedItem().equals(DomainSelectionCell.TYPE_RESOURCE)) { 92 this.m_minOccursSpinner.setEnabled(bEnabled); 93 } 94 this.m_maxOccursSpinner.setEnabled(bEnabled); 95 } 96 97 protected String getPath() { 98 if(this.m_valueResource!=null) { 99 return this.m_valueResource.getPath(); 100 } else if(this.m_valueCollection!=null) { 101 return this.m_valueCollection.getPath(); 102 } else { 103 return null; 104 } 105 } 106 107 protected void setListenToCombo(boolean bListenToCombo) { 108 this.m_bListenToCombo = bListenToCombo; 109 } 110 111 protected boolean isSelected() { 112 if(this.getBackground()==this.m_selectedColor) { 113 return true; 114 } else { 115 return false; 116 } 117 } 118 119 private void setup() throws Exception { 120 this.setLayout(this); 121 122 this.setBackground(Color.WHITE); 123 this.setOpaque(true); 124 125 AbstractVirtualFileSystem vfs = ServerList.getInstance().getHarmoniseServer().getVFS(); 126 VirtualFile vfFile = null; 127 if(this.m_valueResource!=null) { 128 vfFile = vfs.getVirtualFile(this.m_valueResource.getPath()).getResource(); 129 } else if(this.m_valueCollection!=null) { 130 vfFile = vfs.getVirtualFile(this.m_valueCollection.getPath()).getResource(); 131 } 132 133 int nMin = 0; 134 int nMax = -1; 135 136 if(vfFile==null) { 137 throw new Exception ("Domain resource is null.."); 138 } 139 this.m_collectionLabel = new JLabel (vfs.getVirtualFileSystemView().getDisplayName(vfFile)); 140 this.m_collectionLabel.setIcon( vfs.getVirtualFileSystemView().getIcon(vfFile) ); 141 this.m_collectionLabel.addMouseListener(this); 142 this.m_collectionLabel.setOpaque(false); 143 this.m_collectionLabel.setFont( FontManager.getInstance().getFont(FontManager.FONT_RESOURCE_TITLE) ); 144 this.add(m_collectionLabel); 145 146 String [] sData = new String []{DomainSelectionCell.TYPE_RESOURCE, DomainSelectionCell.TYPE_COLLECTION, DomainSelectionCell.TYPE_BOTH}; 147 this.m_typeCombo = new JComboBox (sData); 148 this.m_typeCombo.setActionCommand("TYPE"); 149 this.m_typeCombo.addActionListener(this); 150 this.m_bListenToCombo=false; 151 152 if(this.m_valueCollection!=null && this.m_valueResource==null) { 153 nMin = this.m_valueCollection.getMinOccurs(); 154 nMax = this.m_valueCollection.getMaxOccurs(); 155 this.m_typeCombo.setSelectedItem(DomainSelectionCell.TYPE_COLLECTION); 156 } else if(this.m_valueCollection==null && this.m_valueResource!=null) { 157 nMin = this.m_valueResource.getMinOccurs(); 158 nMax = this.m_valueResource.getMaxOccurs(); 159 this.m_typeCombo.setSelectedItem(DomainSelectionCell.TYPE_RESOURCE); 160 } else if(this.m_valueCollection!=null && this.m_valueResource!=null) { 161 if(this.m_valueCollection.getMinOccurs()<this.m_valueResource.getMinOccurs()) { 162 nMin = this.m_valueCollection.getMinOccurs(); 163 } else { 164 nMin = this.m_valueResource.getMinOccurs(); 165 } 166 if(this.m_valueCollection.getMaxOccurs()<this.m_valueResource.getMaxOccurs()) { 167 nMax = this.m_valueCollection.getMaxOccurs(); 168 } else { 169 nMax = this.m_valueResource.getMaxOccurs(); 170 } 171 this.m_typeCombo.setSelectedItem(DomainSelectionCell.TYPE_BOTH); 172 } 173 this.m_bListenToCombo=true; 174 this.add(this.m_typeCombo); 175 176 SpinnerModel model = new SpinnerNumberModel (nMin, 0, 1000, 1); 177 this.m_minOccursSpinner = new JSpinner (model); 178 this.m_minOccursSpinner.addChangeListener(this); 179 this.m_minOccursSpinner.setEditor(new JSpinner.NumberEditor (this.m_minOccursSpinner)); 180 this.add(this.m_minOccursSpinner); 181 182 model = new SpinnerNumberModel (nMax, -1, 1000, 1); 183 this.m_maxOccursSpinner = new JSpinner (model); 184 this.m_maxOccursSpinner.addChangeListener(this); 185 this.m_maxOccursSpinner.setEditor(new JSpinner.NumberEditor (this.m_maxOccursSpinner)); 186 this.add(this.m_maxOccursSpinner); 187 if(nMax==-1) { 188 this.setMaxSpinnerCleared(true); 189 } 190 191 if(this.m_typeCombo.getSelectedItem().equals(DomainSelectionCell.TYPE_COLLECTION) 192 || this.m_typeCombo.getSelectedItem().equals(DomainSelectionCell.TYPE_BOTH) ) { 193 this.m_minOccursSpinner.setValue( new Integer (0)); 194 195 if(this.m_valueCollection!=null) { 196 this.m_valueCollection.setMinOccurs(0); 197 } 198 if(this.m_valueResource!=null) { 199 this.m_valueResource.setMinOccurs(0); 200 } 201 202 this.m_minOccursSpinner.setEnabled(false); 203 } 204 205 } 206 207 public void setType(String sType) { 208 this.m_typeCombo.setSelectedItem(sType); 209 } 210 211 214 public void stateChanged(ChangeEvent ce) { 215 if(ce.getSource()==this.m_maxOccursSpinner) { 216 if(((Integer )this.m_maxOccursSpinner.getValue()).intValue()==-1) { 217 this.setMaxSpinnerCleared(true); 218 if(this.m_valueCollection!=null) { 219 this.m_valueCollection.setMaxOccurs(-1); 220 } 221 if(this.m_valueResource!=null) { 222 this.m_valueResource.setMaxOccurs(-1); 223 } 224 } else { 225 this.setMaxSpinnerCleared(false); 226 int nMaxVal = ((Integer )((SpinnerNumberModel )this.m_maxOccursSpinner.getModel()).getValue()).intValue(); 227 int nMinVal = ((Integer )((SpinnerNumberModel )this.m_minOccursSpinner.getModel()).getValue()).intValue(); 228 if( nMaxVal<nMinVal ) { 229 this.m_minOccursSpinner.setValue(new Integer (nMaxVal)); 230 } 231 if(this.m_valueCollection!=null) { 232 this.m_valueCollection.setMaxOccurs(nMaxVal); 233 } 234 if(this.m_valueResource!=null) { 235 this.m_valueResource.setMaxOccurs(nMaxVal); 236 } 237 } 238 this.m_list.domainValuesChanged(); 239 } else if(ce.getSource()==this.m_minOccursSpinner) { 240 this.m_list.domainValuesChanged(); 241 int nMaxVal = ((Integer )((SpinnerNumberModel )this.m_maxOccursSpinner.getModel()).getValue()).intValue(); 242 int nMinVal = ((Integer )((SpinnerNumberModel )this.m_minOccursSpinner.getModel()).getValue()).intValue(); 243 if( nMinVal>nMaxVal ) { 244 this.m_maxOccursSpinner.setValue(new Integer (nMinVal)); 245 } 246 if(this.m_valueCollection!=null) { 247 this.m_valueCollection.setMinOccurs(nMinVal); 248 } 249 if(this.m_valueResource!=null) { 250 this.m_valueResource.setMinOccurs(nMinVal); 251 } 252 } 253 } 254 255 private void setMaxSpinnerCleared(boolean bCleared) { 256 if(bCleared) { 257 ((JSpinner.DefaultEditor )((JSpinner )this.m_maxOccursSpinner).getEditor()).getTextField().setForeground(Color.WHITE); 258 ((JSpinner.DefaultEditor )((JSpinner )this.m_maxOccursSpinner).getEditor()).getTextField().setSelectedTextColor(Color.WHITE); 259 ((JSpinner.DefaultEditor )((JSpinner )this.m_maxOccursSpinner).getEditor()).getTextField().setSelectionColor(Color.WHITE); 260 } else { 261 ((JSpinner.DefaultEditor )((JSpinner )this.m_maxOccursSpinner).getEditor()).getTextField().setForeground(Color.BLACK); 262 ((JSpinner.DefaultEditor )((JSpinner )this.m_maxOccursSpinner).getEditor()).getTextField().setSelectedTextColor(Color.WHITE); 263 ((JSpinner.DefaultEditor )((JSpinner )this.m_maxOccursSpinner).getEditor()).getTextField().setSelectionColor(Color.BLUE); 264 } 265 } 266 267 270 public void mouseClicked(MouseEvent me) { 271 if(me.getSource()==this.m_collectionLabel) { 272 this.m_list.cellSelected(this); 273 } 274 } 275 276 protected void setCellSelected(boolean bSelected) { 277 if(!bSelected) { 278 this.m_bSelected = false; 279 this.setBackground(Color.WHITE); 280 this.m_collectionLabel.setBackground(Color.WHITE); 281 this.m_typeCombo.setBackground(Color.WHITE); 282 this.m_minOccursSpinner.setBackground(Color.WHITE); 283 this.m_maxOccursSpinner.setBackground(Color.WHITE); 284 } else { 285 this.m_bSelected = true; 286 this.setBackground(this.m_selectedColor); 287 this.m_collectionLabel.setBackground(this.m_selectedColor); 288 this.m_typeCombo.setBackground(this.m_selectedColor); 289 this.m_minOccursSpinner.setBackground(this.m_selectedColor); 290 this.m_maxOccursSpinner.setBackground(this.m_selectedColor); 291 } 292 this.validateTree(); 293 } 294 295 298 public void actionPerformed(ActionEvent ae) { 299 if(ae.getActionCommand().equals("TYPE") && this.m_bListenToCombo) { 300 if(this.m_typeCombo.getSelectedItem().equals(DomainSelectionCell.TYPE_COLLECTION) 301 || this.m_typeCombo.getSelectedItem().equals(DomainSelectionCell.TYPE_BOTH)) { 302 this.m_minOccursSpinner.setValue( new Integer (0)); 303 304 if(this.m_valueCollection!=null) { 305 this.m_valueCollection.setMinOccurs(0); 306 } 307 if(this.m_valueResource!=null) { 308 this.m_valueResource.setMinOccurs(0); 309 } 310 311 this.m_minOccursSpinner.setEnabled(false); 312 } else if(this.m_typeCombo.getSelectedItem().equals(DomainSelectionCell.TYPE_RESOURCE) && this.m_bOccursSpinnersEnabled) { 313 this.m_minOccursSpinner.setEnabled(true); 314 this.m_maxOccursSpinner.setEnabled(true); 315 } 316 317 String sType = (String ) this.m_typeCombo.getSelectedItem(); 318 if(sType.equals(TYPE_RESOURCE)) { 319 if(this.m_valueResource==null) { 320 this.m_valueResource = this.m_list.getNewValueInstance(); 321 this.m_valueResource.setResourceType( DomainValue.RESOURCE ); 322 if(this.m_valueCollection!=null) { 323 this.m_valueResource.setMaxOccurs(this.m_valueCollection.getMaxOccurs()); 324 this.m_valueResource.setMinOccurs(this.m_valueCollection.getMinOccurs()); 325 this.m_valueResource.setPath(this.m_valueCollection.getPath()); 326 } 327 } 328 this.m_valueCollection=null; 329 } else if(sType.equals(TYPE_COLLECTION)) { 330 if(this.m_valueCollection==null) { 331 this.m_valueCollection = this.m_list.getNewValueInstance(); 332 this.m_valueCollection.setResourceType( DomainValue.COLLECTION ); 333 if(this.m_valueResource!=null) { 334 this.m_valueCollection.setMaxOccurs(this.m_valueResource.getMaxOccurs()); 335 this.m_valueCollection.setMinOccurs(this.m_valueResource.getMinOccurs()); 336 this.m_valueCollection.setPath(this.m_valueResource.getPath()); 337 } 338 } 339 this.m_valueResource=null; 340 } else if(sType.equals(TYPE_BOTH)) { 341 if(this.m_valueResource==null) { 342 this.m_valueResource = this.m_list.getNewValueInstance(); 343 this.m_valueResource.setResourceType( DomainValue.RESOURCE ); 344 if(this.m_valueCollection!=null) { 345 this.m_valueResource.setMaxOccurs(this.m_valueCollection.getMaxOccurs()); 346 this.m_valueResource.setMinOccurs(this.m_valueCollection.getMinOccurs()); 347 this.m_valueResource.setPath(this.m_valueCollection.getPath()); 348 } 349 } 350 if(this.m_valueCollection==null) { 351 this.m_valueCollection = this.m_list.getNewValueInstance(); 352 this.m_valueCollection.setResourceType( DomainValue.COLLECTION ); 353 if(this.m_valueResource!=null) { 354 this.m_valueCollection.setMaxOccurs(this.m_valueResource.getMaxOccurs()); 355 this.m_valueCollection.setMinOccurs(this.m_valueResource.getMinOccurs()); 356 this.m_valueCollection.setPath(this.m_valueResource.getPath()); 357 } 358 } 359 } 360 this.m_list.domainValuesChanged(); 361 } 362 } 363 364 protected List getValues() { 365 ArrayList aValues = new ArrayList (); 366 367 if(this.m_valueCollection!=null) { 368 aValues.add(this.m_valueCollection); 369 } 370 if(this.m_valueResource!=null) { 371 aValues.add(this.m_valueResource); 372 } 373 374 return aValues; 375 } 376 377 380 public void layoutContainer(Container arg0) { 381 this.m_collectionLabel.setSize(100, 20); 382 this.m_collectionLabel.setLocation(0, 0); 383 384 this.m_typeCombo.setSize(98, 18); 385 this.m_typeCombo.setLocation(101, 1); 386 387 this.m_minOccursSpinner.setSize(48, 18); 388 this.m_minOccursSpinner.setLocation(201, 1); 389 390 this.m_maxOccursSpinner.setSize(48, 18); 391 this.m_maxOccursSpinner.setLocation(251, 1); 392 } 393 394 397 public Dimension getPreferredSize() { 398 return new Dimension (300, 20); 399 } 400 401 404 public void mouseEntered(MouseEvent arg0) { 405 } 406 407 410 public void mouseExited(MouseEvent arg0) { 411 } 412 413 416 public void mousePressed(MouseEvent arg0) { 417 } 418 419 422 public void mouseReleased(MouseEvent arg0) { 423 } 424 425 428 public void removeLayoutComponent(Component arg0) { 429 } 430 431 434 public void addLayoutComponent(String arg0, Component arg1) { 435 } 436 437 440 public Dimension minimumLayoutSize(Container arg0) { 441 return this.getPreferredSize(); 442 } 443 444 447 public Dimension preferredLayoutSize(Container arg0) { 448 return this.getPreferredSize(); 449 } 450 451 454 private DomainSelectionCell(boolean arg0) { 455 super(arg0); 456 } 457 458 461 private DomainSelectionCell(LayoutManager arg0) { 462 super(arg0); 463 } 464 465 469 private DomainSelectionCell(LayoutManager arg0, boolean arg1) { 470 super(arg0, arg1); 471 } 472 473 } 474 | Popular Tags |