1 10 11 12 package org.enhydra.jawe.xml.panels; 13 14 import org.enhydra.jawe.xml.*; 15 16 import java.util.*; 17 import javax.swing.*; 18 import javax.swing.event.*; 19 import java.awt.*; 20 import java.awt.event.*; 21 22 26 public class XMLListChoicePanel extends XMLPanel { 27 28 XMLPanel prevPanel=null; 29 30 boolean errorFlag=false; 31 32 public XMLListChoicePanel (XMLCollection myOwner) { 33 this(myOwner,""); 34 } 35 36 public XMLListChoicePanel (XMLCollection myOwner,String title) { 37 this(myOwner,title,XMLPanel.BOX_LAYOUT); 38 } 39 40 public XMLListChoicePanel (XMLCollection myOwner,String title, 41 int layout) { 42 this(myOwner,title,layout,true); 43 } 44 45 public XMLListChoicePanel (XMLCollection myOwner,String title, 46 int layout,boolean isVertical) { 47 this(myOwner,title,layout,isVertical,true); 48 } 49 50 public XMLListChoicePanel (XMLCollection myOwner,String title, 51 int layout,boolean isVertical,boolean hasBorder) { 52 53 super(myOwner,3,title,layout,isVertical,hasBorder); 54 55 final XMLListPanel pList=new XMLListPanel(myOwner,"",false,false,true); 56 add(pList); 57 final JScrollPane jsp=new JScrollPane(); 58 jsp.setAlignmentX(Component.LEFT_ALIGNMENT); 59 jsp.setAlignmentY(Component.TOP_ALIGNMENT); 60 jsp.setMinimumSize(new Dimension(300,225)); 61 add(jsp); 63 64 final JList jl=pList.getList(); 65 jl.addListSelectionListener(new ListSelectionListener() { 66 public void valueChanged(ListSelectionEvent e) { 67 if (pList.isItemChangingPosition() || errorFlag) return; 68 if (prevPanel!=null) { 69 if (canApplyChanges(prevPanel)) { 70 prevPanel.setElements(); 71 if (!getOwner().isReadOnly()) { 72 ((XMLElementDialog)prevPanel.getDialog()). 73 notifyListeners(prevPanel.getOwner()); 74 } 75 }else { 76 errorFlag=true; 77 pList.setChanging(); 78 jl.setSelectedIndex(e.getFirstIndex()); 79 errorFlag=false; 80 return; 81 } 82 } 83 84 XMLElement choosen=null; 85 try { 86 choosen=(XMLElement)jl.getSelectedValue(); 87 } catch (Exception ex) {} 88 89 if (choosen!=null) { 90 prevPanel=choosen.getPanel(); 91 jsp.setViewportView(prevPanel); 92 } else { 93 jsp.setViewportView(new XMLPanel()); 94 } 95 } 96 }); 97 jl.setSelectedIndex(0); 98 if (layout==XMLPanel.BOX_LAYOUT) { 99 if (isVertical) { 100 add(Box.createVerticalGlue()); 101 } 102 else { 103 add(Box.createHorizontalGlue()); 104 } 105 } 106 } 107 108 protected boolean canApplyChanges (XMLPanel panelToEdit) { 109 if (panelToEdit.checkRequired()) { 110 if (!panelToEdit.getOwner().isValidEnter(panelToEdit)) { 111 return false; 112 } 113 if (panelToEdit.getOwner() instanceof XMLCollectionElement) { 114 if (((XMLCollectionElement)panelToEdit.getOwner()). 115 isIDUniqueAndValid(panelToEdit)) { 116 return true; 117 } else { 118 return false; 119 } 120 } else { 121 return true; 122 } 123 } else { 124 return false; 125 } 126 } 127 128 public JList getList () { 129 return ((XMLListPanel)getComponent(0)).getList(); 130 } 131 132 public XMLPanel getDisplayedPanel () { 133 return prevPanel; 134 } 135 136 public void setElements () { 137 ((XMLPanel)getComponent(0)).setElements(); 140 JScrollPane jsp=(JScrollPane)getComponent(1); 142 JViewport jvp=(JViewport)jsp.getComponent(0); 143 ((XMLPanel)jvp.getComponent(0)).setElements(); 144 } 146 147 } 148 | Popular Tags |