1 19 20 package org.netbeans.core; 21 22 import java.awt.Dimension ; 23 import java.beans.BeanInfo ; 24 import java.util.Iterator ; 25 import java.util.ResourceBundle ; 26 import javax.swing.BorderFactory ; 27 import javax.swing.DefaultListModel ; 28 import javax.swing.ImageIcon ; 29 import javax.swing.JButton ; 30 import javax.swing.JLabel ; 31 import javax.swing.JList ; 32 import javax.swing.JPanel ; 33 import javax.swing.JScrollPane ; 34 import javax.swing.ListCellRenderer ; 35 import javax.swing.SwingUtilities ; 36 import javax.swing.UIManager ; 37 import javax.swing.border.Border ; 38 import javax.swing.border.LineBorder ; 39 import org.openide.DialogDescriptor; 40 import org.openide.NotifyDescriptor; 41 import org.openide.awt.Mnemonics; 42 import org.openide.cookies.SaveCookie; 43 import org.openide.loaders.DataObject; 44 import org.openide.nodes.Node; 45 import org.openide.util.Exceptions; 46 import org.openide.util.NbBundle; 47 48 52 53 public class ExitDialog extends JPanel implements java.awt.event.ActionListener { 54 private final static boolean isAqua = "Aqua".equals(UIManager.getLookAndFeel().getID()); 55 56 private static Object [] exitOptions; 57 58 59 private static java.awt.Dialog exitDialog; 60 61 62 private static boolean result = false; 63 64 JList list; 65 DefaultListModel listModel; 66 67 static final long serialVersionUID = 6039058107124767512L; 68 69 70 public ExitDialog () { 71 setLayout (new java.awt.BorderLayout ()); 72 73 listModel = new DefaultListModel (); 74 Iterator iter = DataObject.getRegistry ().getModifiedSet ().iterator(); 75 while (iter.hasNext()) { 76 DataObject obj = (DataObject) iter.next(); 77 listModel.addElement(obj); 78 } 79 draw (); 80 } 81 82 84 private void draw () { 85 list = new JList (listModel); 86 list.addListSelectionListener (new javax.swing.event.ListSelectionListener () { 87 public void valueChanged (javax.swing.event.ListSelectionEvent evt) { 88 updateSaveButton (); 89 } 90 } 91 ); 92 if (!listModel.isEmpty ()) { 94 list.setSelectedIndex (0); 95 } else { 96 updateSaveButton (); 97 } 98 JScrollPane scroll = new JScrollPane (list); 99 setBorder(BorderFactory.createEmptyBorder( 12, 12, 11, 12)); 100 add(scroll, java.awt.BorderLayout.CENTER); 101 list.setCellRenderer(new ExitDlgListCellRenderer()); 102 list.getAccessibleContext().setAccessibleName((NbBundle.getBundle(ExitDialog.class)).getString("ACSN_ListOfChangedFiles")); 103 list.getAccessibleContext().setAccessibleDescription((NbBundle.getBundle(ExitDialog.class)).getString("ACSD_ListOfChangedFiles")); 104 this.getAccessibleContext().setAccessibleDescription((NbBundle.getBundle(ExitDialog.class)).getString("ACSD_ExitDialog")); 105 } 106 107 private void updateSaveButton () { 108 ((JButton )exitOptions [0]).setEnabled (list.getSelectedIndex () != -1); 109 } 110 111 112 public Dimension getPreferredSize() { 113 Dimension prev = super.getPreferredSize(); 114 return new Dimension (Math.max(300, prev.width), Math.max(150, prev.height)); 115 } 116 117 119 public void actionPerformed(final java.awt.event.ActionEvent evt ) { 120 if (exitOptions[0].equals (evt.getSource ())) { 121 save(false); 122 } else if (exitOptions[1].equals (evt.getSource ())) { 123 save(true); 124 } else if (exitOptions[2].equals (evt.getSource ())) { 125 theEnd(); 126 } else if (NotifyDescriptor.CANCEL_OPTION.equals (evt.getSource ())) { 127 exitDialog.setVisible (false); 128 } 129 } 130 131 134 private void save(boolean all) { 135 Object array[] = ((all) ? listModel.toArray() : list.getSelectedValues()); 136 int i, count = ((array == null) ? 0 : array.length); 137 int index = 0; 139 for (i = 0; i < count; i++) { 140 DataObject nextObject = (DataObject)array[i]; 141 index = listModel.indexOf(nextObject); 142 save(nextObject); 143 } 144 145 if (listModel.isEmpty()) 146 theEnd(); 147 else { if (index < 0) 149 index = 0; 150 else if (index > listModel.size() - 1) { 151 index = listModel.size() - 1; 152 } 153 list.setSelectedIndex(index); 154 } 155 } 156 157 160 private void save (DataObject dataObject) { 161 try { 162 SaveCookie sc = (SaveCookie)dataObject.getCookie(SaveCookie.class); 163 if (sc != null) { 164 sc.save(); 165 } 166 listModel.removeElement(dataObject); 168 } catch (java.io.IOException exc) { 169 Throwable t = exc; 170 if (Exceptions.findLocalizedMessage(exc) == null) { 171 t = Exceptions.attachLocalizedMessage(exc, 172 NbBundle.getBundle(ExitDialog.class).getString("EXC_Save")); 173 } 174 Exceptions.printStackTrace(t); 175 } 176 } 177 178 180 private void theEnd() { 181 184 for (int i = listModel.size() - 1; i >= 0; i--) { 185 DataObject obj = (DataObject) listModel.getElementAt(i); 186 obj.setModified(false); 187 } 188 189 result = true; 190 exitDialog.setVisible (false); 191 exitDialog.dispose(); 192 } 193 194 197 public static boolean showDialog() { 198 return innerShowDialog(); 199 } 200 201 204 private static boolean innerShowDialog() { 205 java.util.Set set = org.openide.loaders.DataObject.getRegistry ().getModifiedSet (); 206 if (!set.isEmpty()) { 207 208 exitDialog = null; 214 215 if (exitDialog == null) { 216 ResourceBundle bundle = NbBundle.getBundle(ExitDialog.class); 217 JButton buttonSave = new JButton (); 218 buttonSave.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_Save")); 219 JButton buttonSaveAll = new JButton (); 220 buttonSaveAll.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_SaveAll")); 221 JButton buttonDiscardAll = new JButton (); 222 buttonDiscardAll.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_DiscardAll")); 223 224 Mnemonics.setLocalizedText(buttonSave, bundle.getString("CTL_Save")); 227 Mnemonics.setLocalizedText(buttonSaveAll, bundle.getString("CTL_SaveAll")); 228 Mnemonics.setLocalizedText(buttonDiscardAll, bundle.getString("CTL_DiscardAll")); 229 230 exitOptions = new Object [] { 231 buttonSave, 232 buttonSaveAll, 233 buttonDiscardAll, 234 }; 235 ExitDialog exitComponent = new ExitDialog (); 236 DialogDescriptor exitDlgDescriptor = new DialogDescriptor ( 237 exitComponent, bundle.getString("CTL_ExitTitle"), true, exitOptions, NotifyDescriptor.CANCEL_OPTION, DialogDescriptor.RIGHT_ALIGN, null, exitComponent ); 246 exitDlgDescriptor.setAdditionalOptions (new Object [] {NotifyDescriptor.CANCEL_OPTION}); 247 exitDialog = org.openide.DialogDisplayer.getDefault ().createDialog (exitDlgDescriptor); 248 } 249 250 result = false; 251 exitDialog.setVisible(true); return result; 253 254 } 255 else 256 return true; 257 } 258 259 261 private class ExitDlgListCellRenderer extends JLabel implements ListCellRenderer { 262 263 static final long serialVersionUID = 1877692790854373689L; 264 265 protected Border hasFocusBorder; 266 protected Border noFocusBorder; 267 268 public ExitDlgListCellRenderer() { 269 this.setOpaque(true); 270 this.setBorder(noFocusBorder); 271 if (isAqua) { 272 hasFocusBorder = BorderFactory.createEmptyBorder(1, 1, 1, 1); 273 } else { 274 hasFocusBorder = new LineBorder (UIManager.getColor("List.focusCellHighlight")); } 276 noFocusBorder = BorderFactory.createEmptyBorder(1, 1, 1, 1); 277 } 278 279 public java.awt.Component getListCellRendererComponent(JList list, 280 Object value, int index, boolean isSelected, boolean cellHasFocus) { 285 final DataObject obj = (DataObject)value; 286 if (!obj.isValid()) { 287 SwingUtilities.invokeLater(new Runnable () { 290 public void run() { 291 listModel.removeElement(obj); 292 } 293 }); 294 setText(""); 295 return this; 296 } 297 298 Node node = obj.getNodeDelegate(); 299 300 ImageIcon icon = new ImageIcon (node.getIcon(BeanInfo.ICON_COLOR_16x16)); 301 super.setIcon(icon); 302 303 setText(node.getDisplayName()); 304 if (isSelected){ 305 this.setBackground(UIManager.getColor("List.selectionBackground")); this.setForeground(UIManager.getColor("List.selectionForeground")); } 308 else { 309 this.setBackground(list.getBackground()); 310 this.setForeground(list.getForeground()); 311 } 312 313 this.setBorder(cellHasFocus ? hasFocusBorder : noFocusBorder); 314 315 return this; 316 } 317 } 318 } 319 | Popular Tags |