KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > localhistory > ui > revert > RevertFolderChanges


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 package org.netbeans.modules.localhistory.ui.revert;
20
21 import java.io.File JavaDoc;
22 import java.io.FileFilter JavaDoc;
23 import java.text.DateFormat JavaDoc;
24 import java.text.ParseException JavaDoc;
25 import java.text.SimpleDateFormat JavaDoc;
26 import java.util.Date JavaDoc;
27 import javax.swing.event.DocumentEvent JavaDoc;
28 import javax.swing.event.DocumentListener JavaDoc;
29 import org.netbeans.modules.localhistory.LocalHistory;
30 import org.netbeans.modules.localhistory.store.StoreEntry;
31 import org.netbeans.modules.localhistory.utils.Utils;
32 import org.netbeans.modules.versioning.util.FlatFolder;
33
34 /**
35  *
36  * @author Tomas Stupka
37  */

38 public class RevertFolderChanges extends RevertChanges implements DocumentListener JavaDoc {
39     
40     public final static DateFormat JavaDoc TS_FORMAT = new SimpleDateFormat JavaDoc("yyyy-MM-dd HH:mm");
41     private RevertFolderPanel panel;
42     
43     void show(File JavaDoc root) {
44         panel = new RevertFolderPanel();
45         panel.dateTextField.setText(TS_FORMAT.format(new Date JavaDoc(System.currentTimeMillis())));
46         panel.dateTextField.getDocument().addDocumentListener(this);
47         if(show(panel)) {
48             // in case of a folder it may be only one file
49
revertFolder(root, getTimeStamp());
50         }
51     }
52     
53     private void revertFolder(File JavaDoc root, long ts) {
54         // revert all files in the folder
55
File JavaDoc[] files = root.listFiles();
56         StoreEntry[] entries = LocalHistory.getInstance().getLocalHistoryStore().getFolderState(root, files, ts);
57         for(StoreEntry entry : entries) {
58             Utils.revert(entry);
59         }
60         
61         if(root instanceof FlatFolder) {
62             return; // only one level revert
63
}
64         
65         // get the root folders actuall children and
66
// revert also all directories among them
67
File JavaDoc[] revertedFolders = root.listFiles(new FileFilter JavaDoc() {
68             public boolean accept(File JavaDoc file) {
69                 return file.isDirectory();
70             }
71         });
72         for(File JavaDoc revertedFolder : revertedFolders) {
73             revertFolder(revertedFolder, ts);
74         }
75     }
76     
77     private long getTimeStamp() {
78         Date JavaDoc date;
79         try {
80             date = TS_FORMAT.parse(panel.dateTextField.getText());
81         } catch(ParseException JavaDoc e) {
82             // should not happen
83
return -1;
84         }
85         return date.getTime();
86     }
87     
88     public void insertUpdate(DocumentEvent JavaDoc arg0) {
89         validateUserInput();
90     }
91
92     public void removeUpdate(DocumentEvent JavaDoc arg0) {
93         validateUserInput();
94     }
95
96     public void changedUpdate(DocumentEvent JavaDoc arg0) {
97         validateUserInput();
98     }
99
100     private void validateUserInput() {
101         setValid(panel.dateTextField.isEditValid());
102     }
103 }
104
Popular Tags