1 19 20 package org.netbeans.modules.project.ui; 21 22 import java.awt.BorderLayout ; 23 import java.awt.Component ; 24 import java.awt.Dialog ; 25 import java.awt.Dimension ; 26 import java.awt.event.ActionEvent ; 27 import java.awt.event.ActionListener ; 28 import java.beans.BeanInfo ; 29 import java.io.IOException ; 30 import java.util.HashSet ; 31 import java.util.Iterator ; 32 import java.util.ResourceBundle ; 33 import java.util.Set ; 34 import javax.swing.*; 35 import javax.swing.border.Border ; 36 import javax.swing.border.CompoundBorder ; 37 import javax.swing.border.EmptyBorder ; 38 import javax.swing.border.LineBorder ; 39 import javax.swing.event.ListSelectionEvent ; 40 import javax.swing.event.ListSelectionListener ; 41 import org.openide.DialogDescriptor; 42 import org.openide.DialogDisplayer; 43 import org.openide.ErrorManager; 44 import org.openide.NotifyDescriptor; 45 import org.openide.awt.Mnemonics; 46 import org.openide.cookies.EditorCookie; 47 import org.openide.cookies.SaveCookie; 48 import org.openide.loaders.DataObject; 49 import org.openide.nodes.Node; 50 import org.openide.util.NbBundle; 51 52 53 58 final public class ExitDialog extends JPanel implements ActionListener { 59 60 static boolean SAVE_ALL_UNCONDITIONALLY = false; 61 62 private static Object [] exitOptions; 63 64 65 private static Dialog exitDialog; 66 67 68 private static boolean result = false; 69 70 JList list; 71 DefaultListModel listModel; 72 73 76 private ExitDialog (Set <DataObject> openedFiles) { 77 setLayout (new BorderLayout ()); 78 79 listModel = new DefaultListModel(); 80 81 Set <DataObject> set = getModifiedFiles (openedFiles); 82 if (!set.isEmpty ()) { 83 Iterator iter = set.iterator (); 84 while (iter.hasNext ()) { 85 DataObject obj = (DataObject) iter.next (); 86 listModel.addElement(obj); 87 } 88 draw (); 89 } 90 } 91 92 93 95 private void draw () { 96 list = new JList(listModel); 97 list.setBorder(new EmptyBorder (2, 2, 2, 2)); 98 list.addListSelectionListener (new ListSelectionListener () { 99 public void valueChanged (ListSelectionEvent evt) { 100 updateSaveButton (); 101 } 102 } 103 ); 104 if (!listModel.isEmpty ()) { 106 list.setSelectedIndex (0); 107 } else { 108 updateSaveButton (); 109 } 110 JScrollPane scroll = new JScrollPane (list); 111 scroll.setBorder (new CompoundBorder (new EmptyBorder (12, 12, 11, 0), scroll.getBorder ())); 112 add(scroll, BorderLayout.CENTER); 113 list.setCellRenderer(new ExitDlgListCellRenderer()); 114 list.getAccessibleContext().setAccessibleName((NbBundle.getBundle(ExitDialog.class)).getString("ACSN_ListOfChangedFiles")); 115 list.getAccessibleContext().setAccessibleDescription((NbBundle.getBundle(ExitDialog.class)).getString("ACSD_ListOfChangedFiles")); 116 this.getAccessibleContext().setAccessibleDescription((NbBundle.getBundle(ExitDialog.class)).getString("ACSD_ExitDialog")); 117 } 118 119 private void updateSaveButton () { 120 ((JButton)exitOptions [0]).setEnabled (list.getSelectedIndex () != -1); 121 } 122 123 124 public Dimension getPreferredSize() { 125 Dimension prev = super.getPreferredSize(); 126 return new Dimension (Math.max(300, prev.width), Math.max(150, prev.height)); 127 } 128 129 131 public void actionPerformed (final ActionEvent evt) { 132 if (exitOptions[0].equals (evt.getSource ())) { 133 save(false); 134 } else if (exitOptions[1].equals (evt.getSource ())) { 135 save(true); 136 } else if (exitOptions[2].equals (evt.getSource ())) { 137 theEnd(); 138 } else if (NotifyDescriptor.CANCEL_OPTION.equals (evt.getSource ())) { 139 exitDialog.setVisible (false); 140 } 141 } 142 143 146 private void save(boolean all) { 147 Object array[] = ((all) ? listModel.toArray() : list.getSelectedValues()); 148 int i, count = ((array == null) ? 0 : array.length); 149 int index = 0; 151 for (i = 0; i < count; i++) { 152 DataObject nextObject = (DataObject)array[i]; 153 index = listModel.indexOf(nextObject); 154 save(nextObject); 155 } 156 157 if (listModel.isEmpty()) 158 theEnd(); 159 else { if (index < 0) 161 index = 0; 162 else if (index > listModel.size() - 1) { 163 index = listModel.size() - 1; 164 } 165 list.setSelectedIndex(index); 166 } 167 } 168 169 172 private void save (DataObject dataObject) { 173 try { 174 SaveCookie sc = (SaveCookie)dataObject.getCookie(SaveCookie.class); 175 if (sc != null) { 176 sc.save(); 177 } 178 listModel.removeElement(dataObject); 179 } catch (IOException exc) { 180 ErrorManager em = ErrorManager.getDefault(); 181 Throwable t = em.annotate( 182 exc, NbBundle.getBundle(ExitDialog.class).getString("EXC_Save") 183 ); 184 em.notify(ErrorManager.EXCEPTION, t); 185 } 186 } 187 188 public static void doSave (DataObject dataObject) { 189 try { 190 SaveCookie sc = (SaveCookie)dataObject.getCookie(SaveCookie.class); 191 if (sc != null) { 192 sc.save(); 193 } 194 } catch (IOException exc) { 195 ErrorManager em = ErrorManager.getDefault(); 196 Throwable t = em.annotate( 197 exc, NbBundle.getBundle(ExitDialog.class).getString("EXC_Save") 198 ); 199 em.notify(ErrorManager.EXCEPTION, t); 200 } 201 } 202 203 205 private void theEnd() { 206 209 for (int i = listModel.size() - 1; i >= 0; i--) { 210 DataObject obj = (DataObject) listModel.getElementAt(i); 211 obj.setModified(false); 212 } 213 214 result = true; 215 exitDialog.setVisible (false); 216 exitDialog.dispose(); 217 } 218 219 223 public static boolean showDialog (Set <DataObject> openedFiles) { 224 return innerShowDialog (getModifiedFiles (openedFiles)); 225 } 226 227 private static Set <DataObject> getModifiedFiles (Set <DataObject> openedFiles) { 228 Set <DataObject> set = new HashSet <DataObject> (openedFiles.size ()); 229 for (DataObject obj: openedFiles) { 230 if (obj.isModified ()) { 231 set.add (obj); 232 } 233 } 234 return set; 235 } 236 237 238 241 private static boolean innerShowDialog (Set <DataObject> openedFiles) { 242 if (!openedFiles.isEmpty()) { 243 if (SAVE_ALL_UNCONDITIONALLY) { 244 for (DataObject d: openedFiles) { 246 doSave(d); 247 } 248 249 return true; 250 } 251 252 exitDialog = null; 258 259 if (exitDialog == null) { 260 ResourceBundle bundle = NbBundle.getBundle(ExitDialog.class); 261 JButton buttonSave = new JButton(); 262 buttonSave.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_Save")); 263 JButton buttonSaveAll = new JButton(); 264 buttonSaveAll.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_SaveAll")); 265 JButton buttonDiscardAll = new JButton(); 266 buttonDiscardAll.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_DiscardAll")); 267 268 Mnemonics.setLocalizedText(buttonSave, bundle.getString("CTL_Save")); 269 Mnemonics.setLocalizedText(buttonSaveAll, bundle.getString ("CTL_SaveAll")); 270 Mnemonics.setLocalizedText(buttonDiscardAll, bundle.getString ("CTL_DiscardAll")); 271 272 exitOptions = new Object [] { 273 buttonSave, 274 buttonSaveAll, 275 buttonDiscardAll, 276 }; 277 ExitDialog exitComponent = null; 278 exitComponent = new ExitDialog (openedFiles); 279 DialogDescriptor exitDlgDescriptor = new DialogDescriptor ( 280 exitComponent, bundle.getString("CTL_ExitTitle"), true, exitOptions, NotifyDescriptor.CANCEL_OPTION, DialogDescriptor.RIGHT_ALIGN, null, exitComponent ); 289 exitDlgDescriptor.setAdditionalOptions (new Object [] {NotifyDescriptor.CANCEL_OPTION}); 290 exitDialog = DialogDisplayer.getDefault ().createDialog (exitDlgDescriptor); 291 } 292 293 result = false; 294 exitDialog.setVisible (true); return result; 296 297 } 298 else 299 return true; 300 } 301 302 304 private class ExitDlgListCellRenderer extends JLabel implements ListCellRenderer { 305 306 protected Border hasFocusBorder; 307 protected Border noFocusBorder; 308 309 public ExitDlgListCellRenderer() { 310 this.setOpaque(true); 311 this.setBorder(noFocusBorder); 312 hasFocusBorder = new LineBorder (UIManager.getColor("List.focusCellHighlight")); noFocusBorder = new EmptyBorder (1, 1, 1, 1); 314 } 315 316 public Component getListCellRendererComponent (JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { 318 final DataObject obj = (DataObject)value; 319 if (!obj.isValid()) { 320 SwingUtilities.invokeLater(new Runnable () { 323 public void run() { 324 listModel.removeElement(obj); 325 } 326 }); 327 setText(""); 328 return this; 329 } 330 331 Node node = obj.getNodeDelegate(); 332 333 ImageIcon icon = new ImageIcon(node.getIcon(BeanInfo.ICON_COLOR_16x16)); 334 super.setIcon(icon); 335 336 setText(node.getDisplayName()); 337 if (isSelected){ 338 this.setBackground(UIManager.getColor("List.selectionBackground")); this.setForeground(UIManager.getColor("List.selectionForeground")); } 341 else { 342 this.setBackground(list.getBackground()); 343 this.setForeground(list.getForeground()); 344 } 345 346 this.setBorder(cellHasFocus ? hasFocusBorder : noFocusBorder); 347 348 return this; 349 } 350 } 351 } 352 | Popular Tags |