1 package discRack.presentation; 2 3 import discRack.*; 4 import discRack.presentation.delements.*; 5 import discRack.presentation.dpanels.*; 6 import discRack.business.person.*; 7 8 import javax.swing.*; 9 import java.awt.*; 10 import java.awt.event.*; 11 12 import java.util.*; 13 import java.net.URL ; 14 15 27 public class DElementDialog extends JDialog { 28 private DPanel panelToEdit=new DPanel(); 29 private JButton buttonOK; 30 private JButton buttonCancel; 31 32 private WindowListener wl=new WindowAdapter() { 33 public void windowClosing(WindowEvent e) { 34 isCanceled=true; 35 dispose(); 36 } 37 }; 38 39 40 private boolean isCanceled=false; 41 42 49 public DElementDialog (JFrame parent,String title) { 50 super(parent,title,true); 51 initDialog(); 52 } 53 54 61 public DElementDialog (JDialog parent,String title) { 62 super(parent,title,true); 63 initDialog(); 64 } 65 66 70 public boolean isCanceled () { 71 return isCanceled; 72 } 73 74 85 public void editDElement (DPanel elementPanel,boolean hasCancelButton) { 86 87 Container cp=getContentPane(); 88 cp.remove(panelToEdit); 89 panelToEdit=elementPanel; 90 cp.add(panelToEdit,0); 91 pack(); 92 93 if (hasCancelButton) { 94 buttonCancel.setVisible(true); 95 } else { 96 buttonCancel.setVisible(false); 97 buttonOK.requestFocus(); 98 } 99 100 setLocationRelativeTo(getParent()); 101 pack(); 102 show(); 103 try { 104 if (panelToEdit instanceof DGroupPanel) { 105 if (panelToEdit.getComponent(0).isEnabled()) { 106 panelToEdit.getComponent(0).requestFocus(); 107 } else { 108 panelToEdit.getComponent(1).requestFocus(); 109 } 110 } 111 112 } catch (Exception ex) { 113 panelToEdit.requestFocus(); 114 } 115 116 } 117 118 private void initDialog () { 119 try { 120 JPanel buttonPanel=new JPanel(); 121 122 buttonPanel.setAlignmentX(Component.LEFT_ALIGNMENT); 123 buttonPanel.setAlignmentY(Component.TOP_ALIGNMENT); 124 125 buttonOK=new JButton("OK"); 126 URL u = ResourceManager.getResource("OKImage"); 127 if (u!=null) { 128 buttonOK.setIcon(new ImageIcon(u)); 129 } 130 131 buttonCancel=new JButton("Cancel"); 132 u = ResourceManager.getResource("CancelImage"); 133 if (u!=null) { 134 buttonCancel.setIcon(new ImageIcon(u)); 135 } 136 137 buttonPanel.add(buttonOK); 138 buttonPanel.add(buttonCancel); 139 140 Container cp=getContentPane(); 141 cp.setLayout(new BoxLayout(cp,BoxLayout.Y_AXIS)); 142 143 cp.add(panelToEdit); 144 cp.add(buttonPanel); 145 146 buttonOK.addActionListener( new ActionListener(){ 148 public void actionPerformed( ActionEvent ae ){ 149 if (canApplyChanges()) { 150 applyChanges(); 151 dispose(); 152 } 153 } 154 }); 155 156 buttonCancel.addActionListener( new ActionListener(){ 158 public void actionPerformed( ActionEvent ae ){ 159 isCanceled=true; 160 dispose(); 161 } 162 }); 163 164 165 addWindowListener(wl); 166 167 getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW) 168 .put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE,0,false),"Cancel"); 169 getRootPane().getActionMap().put("Cancel", new AbstractAction() { 170 public void actionPerformed(ActionEvent e) { 171 isCanceled=true; 172 dispose(); 173 } 174 }); 175 176 } catch (Exception e) { 177 e.printStackTrace(); 178 } 179 setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE); 180 setResizable(false); 181 setLocationRelativeTo(getParent()); 182 buttonOK.setDefaultCapable(true); 183 getRootPane().setDefaultButton(buttonOK); 184 185 } 186 187 public boolean canApplyChanges () { 188 if (panelToEdit.checkRequired()) { 189 if (!panelToEdit.getOwner().setDODSElements(panelToEdit)) { 190 return false; 191 } else { 192 return true; 193 } 194 } else { 195 return false; 196 } 197 } 198 199 public void applyChanges () { 200 panelToEdit.setElements(); 201 } 202 203 } 204 205 206 | Popular Tags |