1 10 11 package org.enhydra.jawe.xml.panels; 12 13 import org.enhydra.jawe.xml.*; 14 15 import java.util.*; 16 import javax.swing.*; 17 import java.awt.*; 18 import java.awt.event.*; 19 20 23 public class XMLComboPanel extends XMLPanel { 24 25 private Dimension comboBoxDimension=new Dimension(200,20); 26 27 public XMLComboPanel (XMLChoice myOwner) { 28 this(myOwner,XMLPanel.BOX_LAYOUT); 29 } 30 31 public XMLComboPanel (XMLElement myOwner,Object [] choices) { 32 this(myOwner,choices,XMLPanel.BOX_LAYOUT,false,false); 33 } 34 35 public XMLComboPanel (XMLChoice myOwner,int layout) { 36 this(myOwner,layout,false); 37 } 38 39 public XMLComboPanel (XMLChoice myOwner,int layout,boolean isVertical) { 40 this(myOwner,null,layout,isVertical,false); 41 } 42 43 public XMLComboPanel (XMLElement myOwner,Object [] choices, 44 int layout,boolean isVertical,boolean isEditable) { 45 46 super(myOwner,2,"",layout,isVertical,false); 47 48 JLabel jl=new JLabel(myOwner.toLabel()+": "); 49 jl.setAlignmentX(Component.LEFT_ALIGNMENT); 50 jl.setAlignmentY(Component.BOTTOM_ALIGNMENT); 51 jl.setHorizontalAlignment(SwingConstants.RIGHT); 52 final JComboBox jcb; 54 Dimension toSet; 55 if ((myOwner instanceof XMLChoice) && ((XMLChoice)myOwner).getChoices()!=null) { 56 Object [] chs=((XMLChoice)myOwner).getChoices(); 57 jcb=new JComboBox(XMLComboPanel.sortComboEntries(chs)); 59 61 jcb.setEditable(isEditable); 62 jcb.setSelectedItem(((XMLChoice)myOwner).getChoosen()); 63 toSet=getComboDimension(chs); 64 } else { 65 jcb=new JComboBox(XMLComboPanel.sortComboEntries(choices)); 67 68 jcb.setEditable(isEditable); 69 jcb.setSelectedItem(myOwner.toValue()); 70 toSet=getComboDimension(choices); 71 } 72 73 jcb.setAlignmentX(Component.LEFT_ALIGNMENT); 74 jcb.setAlignmentY(Component.BOTTOM_ALIGNMENT); 75 jcb.setMinimumSize(new Dimension(toSet)); 76 jcb.setMaximumSize(new Dimension(toSet)); 77 jcb.setPreferredSize(new Dimension(toSet)); 78 79 jcb.setEnabled(!myOwner.isReadOnly()); 80 81 add(Box.createHorizontalGlue()); 82 add(jl); 83 add(jcb); 84 85 } 86 87 public boolean checkRequired () { 88 if ((getSelectedItem()==null || getSelectedItem().toString().trim().equals("")) && 89 getOwner().isRequired() && !getOwner().isReadOnly()) { 90 91 XMLPanel.defaultErrorMessage(this.getDialog(),((JLabel)getComponent(1)).getText()); 92 ((JComboBox)getComponent(2)).requestFocus(); 93 return false; 94 } 95 return true; 96 } 97 98 public void setElements () { 99 getOwner().setValue(getSelectedItem()); 100 } 101 102 public JComboBox getComboBox () { 103 return (JComboBox)getComponent(2); 104 } 105 106 public Object getSelectedItem() { 107 return getComboBox().getSelectedItem(); 108 } 109 110 public Dimension getComboDimension (Object [] choices) { 111 Dimension d=new Dimension(comboBoxDimension); 112 double w=0; 113 if (choices!=null) { 114 double longest=0; 115 for (int i=0;i<choices.length; i++) { 116 String n=choices[i].toString(); 117 try { 118 w=getFontMetrics(getFont()).stringWidth(choices[i].toString()); 119 if (w>longest) longest=w; 120 } catch(Exception ex) {} 121 } 122 123 w=longest+25; 124 } 125 if (w<comboBoxDimension.width) w=comboBoxDimension.width; 126 return new Dimension((int)w,comboBoxDimension.height); 127 128 } 129 130 public static Vector sortComboEntries (Object [] ces) { 131 java.util.List l=new ArrayList(); 133 if (ces!=null) { 134 for (int i=0; i<ces.length; i++){ 135 l.add(ces[i]); 136 } 137 } 138 139 Collections.sort(l,new ComboEntryComparator()); 140 return new Vector(l); 141 } 142 143 private static class ComboEntryComparator implements Comparator { 144 public int compare(Object o1, Object o2) { 145 String p1=o1.toString(); 146 String p2=o2.toString(); 147 148 return p1.compareTo(p2); 149 } 150 } 151 } 152 153 | Popular Tags |