KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > windows > actions > ResetWindowsAction


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.windows.actions;
21
22 import java.awt.event.ActionEvent JavaDoc;
23 import java.io.IOException JavaDoc;
24 import javax.swing.AbstractAction JavaDoc;
25 import javax.swing.JFrame JavaDoc;
26 import javax.swing.SwingUtilities JavaDoc;
27 import org.netbeans.core.NbTopManager;
28 import org.netbeans.core.windows.PersistenceHandler;
29 import org.netbeans.core.windows.WindowManagerImpl;
30 import org.netbeans.core.windows.persistence.PersistenceManager;
31 import org.openide.ErrorManager;
32 import org.openide.filesystems.FileObject;
33 import org.openide.filesystems.FileSystem;
34 import org.openide.filesystems.Repository;
35 import org.openide.util.Lookup;
36 import org.openide.util.NbBundle;
37 import org.openide.windows.TopComponent;
38
39 /**
40  * Resets the window system to its default state.
41  *
42  * @author S. Aubrecht
43  */

44 public class ResetWindowsAction extends AbstractAction JavaDoc {
45     
46     /** Creates a new instance of ResetWindowsAction */
47     public ResetWindowsAction() {
48         putValue(NAME, NbBundle.getMessage(CloneDocumentAction.class, "CTL_ResetWindows" ) ); // NOI18N
49
}
50
51     public void actionPerformed(ActionEvent JavaDoc e) {
52         final NbTopManager.WindowSystem ws = (NbTopManager.WindowSystem)Lookup.getDefault().lookup( NbTopManager.WindowSystem.class );
53         if( null == ws ) {
54             //unsupported window system implementation
55
//TODO log a warning
56
return;
57         }
58         
59         WindowManagerImpl wm = WindowManagerImpl.getInstance();
60         
61         wm.getMainWindow().setExtendedState( JFrame.NORMAL );
62         
63         //get a list of editor windows that should stay open even after the reset
64
final TopComponent[] editors = wm.getEditorTopComponents();
65         
66         //close all other windows just in case they hold some references to editor windows
67
wm.closeNonEditorViews();
68         
69         //hide the main window to hide some window operations before the actual reset is performed
70
wm.getMainWindow().setVisible( false );
71         
72         //find an editor window that will be activated after the reset (may be null)
73
final TopComponent activeEditor = wm.getArbitrarySelectedEditorTopComponent();
74         //make sure that componentHidden() gets called on all opened and selected editors
75
//so that they can reset their respective states and/or release some listeners
76
wm.deselectEditorTopComponents();
77         
78         SwingUtilities.invokeLater( new Runnable JavaDoc() {
79             public void run() {
80                 //find the local folder that must be deleted
81
FileSystem fs = Repository.getDefault().getDefaultFileSystem();
82                 FileObject rootFolder = fs.getRoot().getFileObject( PersistenceManager.ROOT_LOCAL_FOLDER );
83                 if( null != rootFolder ) {
84                     try {
85                         for( FileObject fo : rootFolder.getChildren() ) {
86                             if( PersistenceManager.COMPS_FOLDER.equals( fo.getName() ) )
87                                 continue; //do not delete settings files
88
fo.delete();
89                         }
90                     } catch( IOException JavaDoc ioE ) {
91                         ErrorManager.getDefault().notify( ErrorManager.INFORMATIONAL, ioE );
92                     }
93                 }
94                 
95                 //reset the window system
96
ws.hide();
97                 WindowManagerImpl.getInstance().resetModel();
98                 PersistenceManager.getDefault().reset(); //keep mappings to TopComponents created so far
99
PersistenceHandler.getDefault().clear();
100                 ws.load();
101                 ws.show();
102
103                 //re-open editor windows that were opened before the reset
104
for( int i=0; i<editors.length; i++ ) {
105                     editors[i].open();
106                 }
107                 //activate some editor window
108
if( null != activeEditor ) {
109                     SwingUtilities.invokeLater( new Runnable JavaDoc() {
110                         public void run() {
111                             activeEditor.requestActive();
112                         }
113                     });
114                 }
115             }
116         });
117     }
118 }
119
Popular Tags