1 package discRack.presentation.dpanels; 2 3 import discRack.presentation.*; 4 import discRack.presentation.delements.*; 5 6 import java.util.*; 7 import javax.swing.*; 8 import javax.swing.table.*; 9 import java.awt.*; 10 import java.awt.event.*; 11 12 import org.enhydra.dods.exceptions.AssertionDataObjectException; 13 19 public class DTableControlPanel extends DControlPanel { 20 DButton buttonNew; 21 DButton buttonEdit; 22 DButton buttonDelete; 23 24 public DTableControlPanel (DCollection myOwner,String title, 25 boolean isVertical,boolean hasBorder) { 26 27 super(myOwner,title,isVertical,hasBorder); 28 29 controlledPanel=((DCollection)myOwner).getControlledPanel(); 30 31 String n="New"; 32 String e="Edit"; 33 String d="Delete"; 34 35 Dimension dim=getPreferredDimension(new String []{n,e,d}); 36 37 buttonNew=new DButton(n,"NewSmall",dim); 38 buttonEdit=new DButton(e,"EditSmall",dim); 39 buttonDelete=new DButton(d,"DeleteSmall",dim); 40 if (isVertical) { 41 add(Box.createRigidArea(new Dimension(0,10))); 42 } 43 add(buttonNew); 44 add(Box.createRigidArea(new Dimension((isVertical)? 0:5,(isVertical)? 5:0))); 45 add(buttonEdit); 46 add(Box.createRigidArea(new Dimension((isVertical)? 0:5,(isVertical)? 5:0))); 47 add(buttonDelete); 48 if (isVertical) { 49 add(Box.createVerticalGlue()); 50 } 51 52 buttonNew.setEnabled(!myOwner.isReadOnly()); 53 buttonDelete.setEnabled(!myOwner.isReadOnly()); 54 55 buttonNew.getInputMap(JComponent.WHEN_FOCUSED). 56 put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,0,false),"edit"); 57 buttonNew.getActionMap().put("edit",new AbstractAction() { 58 public void actionPerformed(ActionEvent e) { 59 if (buttonNew.hasFocus()) { 60 buttonNew.doClick(); 61 } 62 } 63 }); 64 65 buttonEdit.getInputMap(JComponent.WHEN_FOCUSED). 66 put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,0,false),"edit"); 67 buttonEdit.getActionMap().put("edit",new AbstractAction() { 68 public void actionPerformed(ActionEvent e) { 69 if (buttonEdit.hasFocus()) { 70 buttonEdit.doClick(); 71 } 72 } 73 }); 74 75 buttonDelete.getInputMap(JComponent.WHEN_FOCUSED). 76 put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,0,false),"edit"); 77 buttonDelete.getActionMap().put("edit",new AbstractAction() { 78 public void actionPerformed(ActionEvent e) { 79 if (buttonDelete.hasFocus()) { 80 buttonDelete.doClick(); 81 } 82 } 83 }); 84 85 buttonNew.addActionListener(new ActionListener() { 87 public void actionPerformed(ActionEvent ae) { 88 DSimpleElement newElement=((DCollection)getOwner()).generateNewElement(); 89 Window w=getWindow(); 90 DElementDialog de; 91 if (w instanceof JFrame) { 92 de=new DElementDialog((JFrame)w,newElement.toName()+" - Enter new"); 93 } else { 94 de=new DElementDialog((JDialog)w,newElement.toName()+" - Enter new"); 95 } 96 de.editDElement(newElement.getPanel(),true); 97 if (!de.isCanceled()) { 99 addElement((DCollectionElement)newElement); 100 ((DCollection)getOwner()).onElementCreated(newElement); 101 } else { 102 ((DCollection)getOwner()).onActionCanceled(newElement,0); 103 } 104 105 } 106 }); 107 108 buttonEdit.addActionListener(new ActionListener() { 110 public void actionPerformed(ActionEvent ae) { 111 DCollectionElement editElement=getSelectedElement(); 112 if (editElement != null) { Window w=getWindow(); 114 DElementDialog de; 115 if (w instanceof JFrame) { 116 de=new DElementDialog((JFrame)w,editElement.toName()+" Editing"); 117 } else { 118 de=new DElementDialog((JDialog)w,editElement.toName()+" Editing"); 119 } 120 121 de.editDElement(editElement.getPanel(),true); 122 if (!de.isCanceled()) { 123 modifyElement(editElement); 124 ((DCollection)getOwner()).onElementModified(editElement); 125 } else { 126 ((DCollection)getOwner()).onActionCanceled(editElement,1); 127 } 128 129 } else { 130 complainLoudly(); 131 } 132 ((DTablePanel)controlledPanel).getTable().requestFocus(); 133 } 134 }); 135 136 buttonDelete.addActionListener(new ActionListener() { 138 public void actionPerformed(ActionEvent ae) { 139 DCollectionElement deleteElement=getSelectedElement(); 140 if (deleteElement == null) { 141 complainLoudly(); 142 } else { 143 int yn=JOptionPane.showConfirmDialog(getWindow(), 144 "Do you really want to delete selected item?", 145 deleteElement.toName()+" Deleting", 146 JOptionPane.YES_NO_OPTION); 147 if (yn==JOptionPane.YES_OPTION) { 148 try { 149 ((DCollection)getOwner()).onElementDeleted(deleteElement); 150 removeElement(deleteElement); 151 } catch (AssertionDataObjectException ex) { 152 JOptionPane.showMessageDialog(getWindow(), 153 "Read-only cache: delete not allowed.","Disc error", 154 JOptionPane.ERROR_MESSAGE); 155 } catch (Exception ex) { 156 JOptionPane.showMessageDialog(getWindow(), 157 "Something went wrong, please try again !","Disc error", 158 JOptionPane.ERROR_MESSAGE); 159 } 160 } 161 } 162 } 163 }); 164 } 165 166 public DCollectionElement getSelectedElement() { 167 return ((DTablePanel)controlledPanel).getSelectedElement(); 168 } 169 170 private void addElement(DCollectionElement e) { 171 ((DCollection)getOwner()).add(e); 172 JTable t=((DTablePanel)controlledPanel).getTable(); 173 DefaultTableModel dtm=(DefaultTableModel)t.getModel(); 174 Vector v=new Vector(((DCollectionElement)e).toComplexTypeValues()); 175 dtm.addRow(v); 176 try { 177 t.setRowSelectionInterval(t.getRowCount()-1,t.getRowCount()-1); 178 } catch (Exception ex) {} 179 t.requestFocus(); 180 } 181 182 private void modifyElement(DCollectionElement e) { 183 ((DTablePanel)controlledPanel).modifyElement(e); 184 } 185 186 private void removeElement(DCollectionElement e) { 187 ((DTablePanel)controlledPanel).removeElement(e); 188 } 189 190 193 public void complainLoudly() { 194 JOptionPane.showMessageDialog(getWindow(), 195 "Please select valid element", 196 "DiscRack", 197 JOptionPane.WARNING_MESSAGE); 198 } 199 } 200 201 | Popular Tags |