1 22 23 package org.gjt.sp.jedit.gui; 24 25 import javax.swing.border.*; 27 import javax.swing.event.*; 28 import javax.swing.*; 29 import java.awt.event.*; 30 import java.awt.*; 31 import org.gjt.sp.jedit.bufferio.BufferIORequest; 32 import org.gjt.sp.jedit.io.*; 33 import org.gjt.sp.jedit.*; 34 36 public class CloseDialog extends EnhancedDialog 37 { 38 public CloseDialog(View view) 40 { 41 super(view,jEdit.getProperty("close.title"),true); 42 43 this.view = view; 44 45 JPanel content = new JPanel(new BorderLayout(12,12)); 46 content.setBorder(new EmptyBorder(12,12,12,12)); 47 setContentPane(content); 48 49 Box iconBox = new Box(BoxLayout.Y_AXIS); 50 iconBox.add(new JLabel(UIManager.getIcon("OptionPane.warningIcon"))); 51 iconBox.add(Box.createGlue()); 52 content.add(BorderLayout.WEST,iconBox); 53 54 JPanel centerPanel = new JPanel(new BorderLayout()); 55 56 JLabel label = new JLabel(jEdit.getProperty("close.caption")); 57 label.setBorder(new EmptyBorder(0,0,6,0)); 58 centerPanel.add(BorderLayout.NORTH,label); 59 60 bufferList = new JList(bufferModel = new DefaultListModel()); 61 bufferList.setVisibleRowCount(10); 62 bufferList.addListSelectionListener(new ListHandler()); 63 64 Buffer[] buffers = jEdit.getBuffers(); 65 for(int i = 0; i < buffers.length; i++) 66 { 67 Buffer buffer = buffers[i]; 68 if(buffer.isDirty()) 69 { 70 bufferModel.addElement(buffer.getPath()); 71 } 72 } 73 74 centerPanel.add(BorderLayout.CENTER,new JScrollPane(bufferList)); 75 76 content.add(BorderLayout.CENTER,centerPanel); 77 78 ActionHandler actionListener = new ActionHandler(); 79 80 Box buttons = new Box(BoxLayout.X_AXIS); 81 buttons.add(Box.createGlue()); 82 buttons.add(selectAll = new JButton(jEdit.getProperty("close.selectAll"))); 83 selectAll.setMnemonic(jEdit.getProperty("close.selectAll.mnemonic").charAt(0)); 84 selectAll.addActionListener(actionListener); 85 buttons.add(Box.createHorizontalStrut(6)); 86 buttons.add(save = new JButton(jEdit.getProperty("close.save"))); 87 save.setMnemonic(jEdit.getProperty("close.save.mnemonic").charAt(0)); 88 save.addActionListener(actionListener); 89 buttons.add(Box.createHorizontalStrut(6)); 90 buttons.add(discard = new JButton(jEdit.getProperty("close.discard"))); 91 discard.setMnemonic(jEdit.getProperty("close.discard.mnemonic").charAt(0)); 92 discard.addActionListener(actionListener); 93 buttons.add(Box.createHorizontalStrut(6)); 94 buttons.add(cancel = new JButton(jEdit.getProperty("common.cancel"))); 95 cancel.addActionListener(actionListener); 96 buttons.add(Box.createGlue()); 97 98 bufferList.setSelectedIndex(0); 99 100 content.add(BorderLayout.SOUTH,buttons); 101 102 GUIUtilities.requestFocus(this,bufferList); 103 104 pack(); 105 setLocationRelativeTo(view); 106 setVisible(true); 107 } 109 public boolean isOK() 111 { 112 return ok; 113 } 115 public void ok() 117 { 118 } 121 public void cancel() 123 { 124 dispose(); 125 } 127 private View view; 129 private JList bufferList; 130 private DefaultListModel bufferModel; 131 private JButton selectAll; 132 private JButton save; 133 private JButton discard; 134 private JButton cancel; 135 136 private boolean ok; 138 boolean selectAllFlag; 139 140 private void updateButtons() 141 { 142 int index = bufferList.getSelectedIndex(); 143 save.getModel().setEnabled(index != -1); 144 discard.getModel().setEnabled(index != -1); 145 } 147 class ActionHandler implements ActionListener 149 { 150 public void actionPerformed(ActionEvent evt) 151 { 152 Object source = evt.getSource(); 153 if(source == selectAll) 154 { 155 try 158 { 159 selectAllFlag = true; 160 161 bufferList.setSelectionInterval(0, 162 bufferModel.getSize() - 1); 163 } 164 finally 165 { 166 selectAllFlag = false; 167 } 168 bufferList.requestFocus(); 169 } 170 else if(source == save) 171 { 172 Object [] paths = bufferList.getSelectedValues(); 173 174 for(int i = 0; i < paths.length; i++) 175 { 176 String path = (String )paths[i]; 177 Buffer buffer = jEdit.getBuffer(path); 178 if(!buffer.save(view,null,true)) 179 return; 180 VFSManager.waitForRequests(); 181 if(buffer.getBooleanProperty(BufferIORequest 182 .ERROR_OCCURRED)) 183 return; 184 jEdit._closeBuffer(view,buffer); 185 bufferModel.removeElement(path); 186 } 187 188 if(bufferModel.getSize() == 0) 189 { 190 ok = true; 191 dispose(); 192 } 193 else 194 { 195 bufferList.setSelectedIndex(0); 196 bufferList.requestFocus(); 197 } 198 } 199 else if(source == discard) 200 { 201 Object [] paths = bufferList.getSelectedValues(); 202 203 for(int i = 0; i < paths.length; i++) 204 { 205 String path = (String )paths[i]; 206 Buffer buffer = jEdit.getBuffer(path); 207 jEdit._closeBuffer(view,buffer); 208 bufferModel.removeElement(path); 209 } 210 211 if(bufferModel.getSize() == 0) 212 { 213 ok = true; 214 dispose(); 215 } 216 else 217 { 218 bufferList.setSelectedIndex(0); 219 bufferList.requestFocus(); 220 } 221 } 222 else if(source == cancel) 223 cancel(); 224 } 225 } 227 class ListHandler implements ListSelectionListener 229 { 230 public void valueChanged(ListSelectionEvent evt) 231 { 232 if(selectAllFlag) 233 return; 234 235 int index = bufferList.getSelectedIndex(); 236 if(index != -1) 237 view.goToBuffer(jEdit.getBuffer((String ) 238 bufferModel.getElementAt(index))); 239 240 updateButtons(); 241 } 242 } } 244 | Popular Tags |