KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > subversion > ui > search > SvnSearch


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.subversion.ui.search;
20
21 import java.awt.BorderLayout JavaDoc;
22 import java.awt.EventQueue JavaDoc;
23 import java.awt.event.ActionEvent JavaDoc;
24 import java.awt.event.ActionListener JavaDoc;
25 import java.text.DateFormat JavaDoc;
26 import java.text.ParseException JavaDoc;
27 import java.text.SimpleDateFormat JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.Date JavaDoc;
30 import java.util.List JavaDoc;
31 import javax.swing.JPanel JavaDoc;
32 import javax.swing.event.DocumentEvent JavaDoc;
33 import javax.swing.event.DocumentListener JavaDoc;
34 import javax.swing.event.ListSelectionListener JavaDoc;
35 import org.netbeans.modules.subversion.RepositoryFile;
36 import org.netbeans.modules.subversion.Subversion;
37 import org.netbeans.modules.subversion.client.SvnClient;
38 import org.netbeans.modules.subversion.client.SvnProgressSupport;
39 import org.netbeans.modules.subversion.SvnModuleConfig;
40 import org.netbeans.modules.versioning.util.NoContentPanel;
41 import org.openide.nodes.AbstractNode;
42 import org.openide.nodes.Children;
43 import org.openide.util.RequestProcessor;
44 import org.tigris.subversion.svnclientadapter.ISVNLogMessage;
45 import org.tigris.subversion.svnclientadapter.SVNClientException;
46 import org.tigris.subversion.svnclientadapter.SVNRevision;
47
48 /**
49  * Handles the UI for revision search.
50  *
51  * @author Tomas Stupka
52  */

53 public class SvnSearch implements ActionListener JavaDoc, DocumentListener JavaDoc {
54     
55     private static final String JavaDoc DATE_FROM = "svnSearch.dateFrom";
56     private static final DateFormat JavaDoc DATE_FORMAT = new SimpleDateFormat JavaDoc("yyyy-MM-dd"); // NOI18N
57
private final SvnSearchPanel panel;
58     
59     private RepositoryFile repositoryRoot;
60     private SvnSearchView searchView ;
61     private SvnProgressSupport support;
62     private NoContentPanel noContentPanel;
63
64     public SvnSearch(RepositoryFile repositoryRoot) {
65         this.repositoryRoot = repositoryRoot;
66         panel = new SvnSearchPanel();
67         panel.listButton.addActionListener(this);
68         panel.dateFromTextField.getDocument().addDocumentListener(this);
69         
70         String JavaDoc date = DATE_FORMAT.format(new Date JavaDoc(System.currentTimeMillis() - 1000 * 60 * 60 * 24 * 7));
71         panel.dateFromTextField.setText(SvnModuleConfig.getDefault().getPreferences().get(DATE_FROM, date));
72         
73         searchView = new SvnSearchView();
74         
75         panel.listPanel.setLayout(new BorderLayout JavaDoc());
76         panel.listPanel.add(searchView.getComponent());
77
78         noContentPanel = new NoContentPanel();
79         panel.noContentPanel.setLayout(new BorderLayout JavaDoc());
80         panel.noContentPanel.add(noContentPanel);
81         noContentPanel.setLabel(org.openide.util.NbBundle.getMessage(SvnSearch.class, "LBL_NoResults_SearchNotPerformed")); // NOI18N
82

83         panel.listPanel.setVisible(false);
84         panel.noContentPanel.setVisible(true);
85     }
86
87     /**
88      * Cancels all running tasks
89      */

90     public void cancel() {
91         if(support != null) {
92             support.cancel();
93         }
94     }
95     
96     private void listLogEntries() {
97                 
98         noContentPanel.setLabel(org.openide.util.NbBundle.getMessage(SvnSearch.class, "LBL_NoResults_SearchInProgress")); // NOI18N
99
panel.listPanel.setVisible(false);
100         panel.noContentPanel.setVisible(true);
101         
102         final SVNRevision revisionFrom = getRevisionFrom();
103         if(revisionFrom instanceof SVNRevision.DateSpec) {
104             SvnModuleConfig.getDefault().getPreferences().put(DATE_FROM, panel.dateFromTextField.getText().trim());
105         }
106         
107         RequestProcessor rp = Subversion.getInstance().getRequestProcessor(this.repositoryRoot.getRepositoryUrl());
108         try {
109             support = new SvnProgressSupport() {
110                 protected void perform() {
111                     SvnClient client;
112                     ISVNLogMessage[] lm;
113                     try {
114                         client = Subversion.getInstance().getClient(SvnSearch.this.repositoryRoot.getRepositoryUrl(), this);
115                         lm = client.getLogMessages(repositoryRoot.getRepositoryUrl(),
116                                                    SVNRevision.HEAD,
117                                                    revisionFrom);
118                     } catch (SVNClientException ex) {
119                         AbstractNode errorNode = new AbstractNode(Children.LEAF);
120                         errorNode.setDisplayName(org.openide.util.NbBundle.getMessage(SvnSearch.class, "LBL_Error")); // NOI18N
121
errorNode.setShortDescription(ex.getLocalizedMessage());
122                         return;
123                     }
124
125                     if(isCanceled()) {
126                         return;
127                     }
128
129                     final List JavaDoc<ISVNLogMessage> msgList = new ArrayList JavaDoc<ISVNLogMessage>(lm.length);
130                     if(revisionFrom instanceof SVNRevision.DateSpec) {
131                         long timeFrom = ((SVNRevision.DateSpec)revisionFrom).getDate().getTime();
132                         for (int i = 0; i < lm.length; i++) {
133                             if(lm[i].getDate().getTime() >= timeFrom) {
134                                 msgList.add(lm[i]);
135                             }
136                         }
137                         lm = msgList.toArray(new ISVNLogMessage[msgList.size()]);
138                     }
139                     final ISVNLogMessage[] msgRet = lm;
140                     EventQueue.invokeLater(new Runnable JavaDoc() {
141                         public void run() {
142                             panel.listPanel.setVisible(true);
143                             panel.noContentPanel.setVisible(false);
144                             searchView.setResults(msgRet);
145                         }
146                     });
147                 }
148             };
149             support.start(rp, SvnSearch.this.repositoryRoot.getRepositoryUrl(), org.openide.util.NbBundle.getMessage(SvnSearch.class, "LBL_Search_Progress")); // NOI18N
150
} finally {
151             // XXX and how is this supposed to work?
152
support = null;
153         }
154     }
155     
156     public JPanel JavaDoc getSearchPanel() {
157         return panel;
158     }
159     
160     public SVNRevision getSelectedRevision() {
161         return searchView.getSelectedValue();
162     }
163
164     public void addListSelectionListener(ListSelectionListener JavaDoc listener) {
165         searchView.addListSelectionListener(listener);
166     }
167     
168     public void removeListSelectionListener(ListSelectionListener JavaDoc listener) {
169         searchView.removeListSelectionListener(listener);
170     }
171
172     public void actionPerformed(ActionEvent JavaDoc e) {
173         if(e.getSource()==panel.listButton) {
174             listLogEntries();
175         }
176     }
177     
178     private SVNRevision getRevisionFrom() {
179         String JavaDoc value = panel.dateFromTextField.getText().trim();
180         if(value.equals("")) {
181             return new SVNRevision.Number(1);
182         }
183         try {
184             return new SVNRevision.DateSpec(DATE_FORMAT.parse(value));
185         } catch (ParseException JavaDoc ex) {
186             return null; // should not happen
187
}
188     }
189
190     public void insertUpdate(DocumentEvent JavaDoc e) {
191          validateUserInput();
192     }
193
194     public void removeUpdate(DocumentEvent JavaDoc e) {
195          validateUserInput();
196     }
197
198     public void changedUpdate(DocumentEvent JavaDoc e) {
199          validateUserInput();
200     }
201
202     private void validateUserInput() {
203         boolean isValid = false;
204         String JavaDoc dateString = panel.dateFromTextField.getText();
205         if(dateString.equals("")) { // NOI18N
206
isValid = true;
207         } else {
208             try {
209                 DATE_FORMAT.parse(panel.dateFromTextField.getText());
210                 isValid = true;
211             } catch (ParseException JavaDoc ex) {
212                 // ignore
213
}
214         }
215         panel.listButton.setEnabled(isValid);
216     }
217     
218 }
219
Popular Tags