1 19 package org.netbeans.modules.subversion.ui.search; 20 21 import java.awt.BorderLayout ; 22 import java.awt.EventQueue ; 23 import java.awt.event.ActionEvent ; 24 import java.awt.event.ActionListener ; 25 import java.text.DateFormat ; 26 import java.text.ParseException ; 27 import java.text.SimpleDateFormat ; 28 import java.util.ArrayList ; 29 import java.util.Date ; 30 import java.util.List ; 31 import javax.swing.JPanel ; 32 import javax.swing.event.DocumentEvent ; 33 import javax.swing.event.DocumentListener ; 34 import javax.swing.event.ListSelectionListener ; 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 53 public class SvnSearch implements ActionListener , DocumentListener { 54 55 private static final String DATE_FROM = "svnSearch.dateFrom"; 56 private static final DateFormat DATE_FORMAT = new SimpleDateFormat ("yyyy-MM-dd"); 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 date = DATE_FORMAT.format(new Date (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 ()); 76 panel.listPanel.add(searchView.getComponent()); 77 78 noContentPanel = new NoContentPanel(); 79 panel.noContentPanel.setLayout(new BorderLayout ()); 80 panel.noContentPanel.add(noContentPanel); 81 noContentPanel.setLabel(org.openide.util.NbBundle.getMessage(SvnSearch.class, "LBL_NoResults_SearchNotPerformed")); 83 panel.listPanel.setVisible(false); 84 panel.noContentPanel.setVisible(true); 85 } 86 87 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")); 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")); errorNode.setShortDescription(ex.getLocalizedMessage()); 122 return; 123 } 124 125 if(isCanceled()) { 126 return; 127 } 128 129 final List <ISVNLogMessage> msgList = new ArrayList <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 () { 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")); } finally { 151 support = null; 153 } 154 } 155 156 public JPanel getSearchPanel() { 157 return panel; 158 } 159 160 public SVNRevision getSelectedRevision() { 161 return searchView.getSelectedValue(); 162 } 163 164 public void addListSelectionListener(ListSelectionListener listener) { 165 searchView.addListSelectionListener(listener); 166 } 167 168 public void removeListSelectionListener(ListSelectionListener listener) { 169 searchView.removeListSelectionListener(listener); 170 } 171 172 public void actionPerformed(ActionEvent e) { 173 if(e.getSource()==panel.listButton) { 174 listLogEntries(); 175 } 176 } 177 178 private SVNRevision getRevisionFrom() { 179 String 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 ex) { 186 return null; } 188 } 189 190 public void insertUpdate(DocumentEvent e) { 191 validateUserInput(); 192 } 193 194 public void removeUpdate(DocumentEvent e) { 195 validateUserInput(); 196 } 197 198 public void changedUpdate(DocumentEvent e) { 199 validateUserInput(); 200 } 201 202 private void validateUserInput() { 203 boolean isValid = false; 204 String dateString = panel.dateFromTextField.getText(); 205 if(dateString.equals("")) { isValid = true; 207 } else { 208 try { 209 DATE_FORMAT.parse(panel.dateFromTextField.getText()); 210 isValid = true; 211 } catch (ParseException ex) { 212 } 214 } 215 panel.listButton.setEnabled(isValid); 216 } 217 218 } 219 | Popular Tags |