KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > ExitDialog


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.core;
21
22 import java.awt.Dimension JavaDoc;
23 import java.beans.BeanInfo JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.ResourceBundle JavaDoc;
26 import javax.swing.BorderFactory JavaDoc;
27 import javax.swing.DefaultListModel JavaDoc;
28 import javax.swing.ImageIcon JavaDoc;
29 import javax.swing.JButton JavaDoc;
30 import javax.swing.JLabel JavaDoc;
31 import javax.swing.JList JavaDoc;
32 import javax.swing.JPanel JavaDoc;
33 import javax.swing.JScrollPane JavaDoc;
34 import javax.swing.ListCellRenderer JavaDoc;
35 import javax.swing.SwingUtilities JavaDoc;
36 import javax.swing.UIManager JavaDoc;
37 import javax.swing.border.Border JavaDoc;
38 import javax.swing.border.LineBorder JavaDoc;
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 /** Dialog which lets the user select which open files to close.
49  *
50  * @author Ian Formanek, Petr Hrebejk
51  */

52
53 public class ExitDialog extends JPanel JavaDoc implements java.awt.event.ActionListener JavaDoc {
54     private final static boolean isAqua = "Aqua".equals(UIManager.getLookAndFeel().getID());
55
56     private static Object JavaDoc[] exitOptions;
57
58     /** The dialog */
59     private static java.awt.Dialog JavaDoc exitDialog;
60
61     /** Result of the dialog */
62     private static boolean result = false;
63
64     JList JavaDoc list;
65     DefaultListModel JavaDoc listModel;
66
67     static final long serialVersionUID = 6039058107124767512L;
68
69     /** Constructs new dlg */
70     public ExitDialog () {
71         setLayout (new java.awt.BorderLayout JavaDoc ());
72
73         listModel = new DefaultListModel JavaDoc();
74         Iterator JavaDoc 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     /** Constructs rest of dialog.
83     */

84     private void draw () {
85         list = new JList JavaDoc(listModel);
86         list.addListSelectionListener (new javax.swing.event.ListSelectionListener JavaDoc () {
87                                            public void valueChanged (javax.swing.event.ListSelectionEvent JavaDoc evt) {
88                                                updateSaveButton ();
89                                            }
90                                        }
91                                       );
92         // bugfix 37941, select first item in list
93
if (!listModel.isEmpty ()) {
94             list.setSelectedIndex (0);
95         } else {
96             updateSaveButton ();
97         }
98         JScrollPane JavaDoc scroll = new JScrollPane JavaDoc (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 JavaDoc)exitOptions [0]).setEnabled (list.getSelectedIndex () != -1);
109     }
110
111     /** @return preffered size */
112     public Dimension JavaDoc getPreferredSize() {
113         Dimension JavaDoc prev = super.getPreferredSize();
114         return new Dimension JavaDoc(Math.max(300, prev.width), Math.max(150, prev.height));
115     }
116
117     /** This method is called when is any of buttons pressed
118     */

119     public void actionPerformed(final java.awt.event.ActionEvent JavaDoc 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     /** Save the files from the listbox
132     * @param all true- all files, false - just selected
133     */

134     private void save(boolean all) {
135         Object JavaDoc array[] = ((all) ? listModel.toArray() : list.getSelectedValues());
136         int i, count = ((array == null) ? 0 : array.length);
137         int index = 0; // index of last removed item
138

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 { // reset selection to new item at the same index if available
148
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     /** Tries to save given data object using its save cookie.
158      * Notifies user if excetions appear.
159      */

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             // only remove the object if the save succeeded
167
listModel.removeElement(dataObject);
168         } catch (java.io.IOException JavaDoc exc) {
169             Throwable JavaDoc 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     /** Exit the IDE
179     */

180     private void theEnd() {
181         // XXX(-ttran) result must be set before calling setVisible(false)
182
// because this will unblock the thread which called Dialog.show()
183

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     /** Opens the ExitDialog and blocks until it's closed. If dialog doesm't
195      * exists it creates new one. Returns true if the IDE should be closed.
196      */

197     public static boolean showDialog() {
198         return innerShowDialog();
199     }
200
201     /**
202      * Opens the ExitDialog.
203      */

204     private static boolean innerShowDialog() {
205         java.util.Set JavaDoc set = org.openide.loaders.DataObject.getRegistry ().getModifiedSet ();
206         if (!set.isEmpty()) {
207
208             // XXX(-ttran) caching this dialog is fatal. If the user
209
// cancels the Exit action, modifies some more files and tries to
210
// Exit again the list of modified DataObject's is not updated,
211
// changes made by the user after the first aborted Exit will be
212
// lost.
213
exitDialog = null;
214             
215             if (exitDialog == null) {
216                 ResourceBundle JavaDoc bundle = NbBundle.getBundle(ExitDialog.class);
217                 JButton JavaDoc buttonSave = new JButton JavaDoc();
218                 buttonSave.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_Save"));
219                 JButton JavaDoc buttonSaveAll = new JButton JavaDoc();
220                 buttonSaveAll.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_SaveAll"));
221                 JButton JavaDoc buttonDiscardAll = new JButton JavaDoc();
222                 buttonDiscardAll.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_DiscardAll"));
223
224                 // special handling to handle a button title with mnemonic
225
// and to allow enable/disable control of the option
226
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 JavaDoc[] {
231                                   buttonSave,
232                                   buttonSaveAll,
233                                   buttonDiscardAll,
234                               };
235                 ExitDialog exitComponent = new ExitDialog ();
236                 DialogDescriptor exitDlgDescriptor = new DialogDescriptor (
237                                                          exitComponent, // inside component
238
bundle.getString("CTL_ExitTitle"), // title
239
true, // modal
240
exitOptions, // options
241
NotifyDescriptor.CANCEL_OPTION, // initial value
242
DialogDescriptor.RIGHT_ALIGN, // option align
243
null, // HelpCtx
244
exitComponent // Action Listener
245
);
246                 exitDlgDescriptor.setAdditionalOptions (new Object JavaDoc[] {NotifyDescriptor.CANCEL_OPTION});
247                 exitDialog = org.openide.DialogDisplayer.getDefault ().createDialog (exitDlgDescriptor);
248             }
249
250             result = false;
251             exitDialog.setVisible(true); // Show the modal Save dialog
252
return result;
253
254         }
255         else
256             return true;
257     }
258
259     /** Renderer used in list box of exit dialog
260      */

261     private class ExitDlgListCellRenderer extends JLabel JavaDoc implements ListCellRenderer JavaDoc {
262         /** generated Serialized Version UID */
263         static final long serialVersionUID = 1877692790854373689L;
264
265         protected Border JavaDoc hasFocusBorder;
266         protected Border JavaDoc 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 JavaDoc(UIManager.getColor("List.focusCellHighlight")); // NOI18N
275
}
276             noFocusBorder = BorderFactory.createEmptyBorder(1, 1, 1, 1);
277         }
278
279         public java.awt.Component JavaDoc getListCellRendererComponent(JList JavaDoc list,
280                 Object JavaDoc value, // value to display
281
int index, // cell index
282
boolean isSelected, // is the cell selected
283
boolean cellHasFocus) // the list and the cell have the focus
284
{
285             final DataObject obj = (DataObject)value;
286             if (!obj.isValid()) {
287                 // #17059: it might be invalid already.
288
// #18886: but if so, remove it later, otherwise BasicListUI gets confused.
289
SwingUtilities.invokeLater(new Runnable JavaDoc() {
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 JavaDoc icon = new ImageIcon JavaDoc(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")); // NOI18N
306
this.setForeground(UIManager.getColor("List.selectionForeground")); // NOI18N
307
}
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