1 19 20 package org.netbeans.modules.versioning.system.cvss.ui.selectors; 21 22 import org.netbeans.lib.cvsclient.CVSRoot; 23 import org.netbeans.lib.cvsclient.Client; 24 import org.netbeans.lib.cvsclient.connection.*; 25 import org.netbeans.lib.cvsclient.event.*; 26 import org.netbeans.lib.cvsclient.command.checkout.CheckoutCommand; 27 import org.netbeans.lib.cvsclient.command.GlobalOptions; 28 import org.netbeans.lib.cvsclient.command.CommandException; 29 import org.netbeans.lib.cvsclient.command.log.LogCommand; 30 import org.netbeans.lib.cvsclient.command.log.LogInformation; 31 import org.netbeans.modules.versioning.system.cvss.CvsVersioningSystem; 32 import org.netbeans.modules.versioning.system.cvss.FileInformation; 33 import org.netbeans.modules.versioning.system.cvss.util.Utils; 34 import org.openide.util.RequestProcessor; 35 import org.openide.util.UserCancelException; 36 import org.openide.util.HelpCtx; 37 import org.openide.ErrorManager; 38 import org.openide.nodes.Node; 39 import org.openide.nodes.AbstractNode; 40 import org.openide.nodes.NodeAcceptor; 41 42 import java.util.*; 43 import java.util.List ; 44 import java.io.File ; 45 import java.io.IOException ; 46 47 52 public final class BranchSelector implements Runnable { 53 54 private CVSRoot root; 55 56 private String module; 57 58 private File file; 59 60 private Node rootNode; 61 private BranchNodeChildren rootKids; 62 63 69 public String selectTag(File file) { 70 71 this.file = file; 72 73 return showSelector(); 74 } 75 76 87 public String selectTag(CVSRoot root, String module) { 88 89 this.root = root; 90 this.module = module; 91 92 return showSelector(); 93 } 94 95 private String showSelector() { 96 97 rootKids = new BranchNodeChildren(); 98 rootNode = new AbstractNode(rootKids); 99 100 RequestProcessor.getDefault().post(this); 102 103 try { 104 NodeOperation2 op = new NodeOperation2(); 105 op.setIconsVisible(false); 106 op.setRootVisible(false); 107 op.setHelpCtx(new HelpCtx(BranchSelector.class)); 108 Node[] selected = op.select(org.openide.util.NbBundle.getMessage(BranchSelector.class, "BK2012"), 109 org.openide.util.NbBundle.getMessage(BranchSelector.class, "BK2013"), 110 org.openide.util.NbBundle.getMessage(BranchSelector.class, "ACSD_BranchSelect"), 111 rootNode, 112 org.openide.util.NbBundle.getMessage(BranchSelector.class, "ACSN_BranchesTree"), 113 org.openide.util.NbBundle.getMessage(BranchSelector.class, "ACSD_BranchesTree"), 114 new NodeAcceptor() { 115 public boolean acceptNodes(Node[] nodes) { 116 if (nodes.length != 1) return false; 117 return nodes[0].getLookup().lookup(String .class) != null; 118 } 119 }); 120 121 Node node = selected[0]; 122 String branch = (String ) node.getLookup().lookup(String .class); 123 return branch; 124 } catch (UserCancelException e) { 125 return null; 126 } 127 } 128 129 130 public void run() { 131 132 GlobalOptions gtx = CvsVersioningSystem.createGlobalOptions(); 133 if (root != null) { 134 gtx.setCVSRoot(root.toString()); } 136 File checkoutFolder = null; 137 try { 138 139 File [] files; 140 File localPath; 141 if (file == null) { 142 144 checkoutFolder = Kit.createTmpFolder(); 145 if (checkoutFolder == null) { 146 error(org.openide.util.NbBundle.getMessage(BranchSelector.class, "BK2015")); 147 return; 148 } 149 150 CheckoutCommand checkout = new CheckoutCommand(); 151 checkout.setRecursive(false); 152 153 if (".".equals(module)) { Client client = Kit.createClient(root); 157 List l = ModuleSelector.listRepositoryPath(client, root, ""); Iterator it = l.iterator(); 159 int max = l.size(); 160 int counter = max; 161 Random random = new Random(); 162 while (counter-- > 0) { 163 int rnd = random.nextInt(max); 164 String path = (String ) l.get(rnd); 165 if ("CVSROOT".equals(path)) continue; module = path; 167 break; 168 } 169 } 170 checkout.setModule(module); 171 File [] checkoutFiles = new File [] {checkoutFolder}; 172 checkout.setFiles(checkoutFiles); 173 174 Client client = Kit.createClient(root); 175 client.setLocalPath(checkoutFolder.getAbsolutePath()); 176 client.executeCommand(checkout, gtx); 177 178 files = checkoutFolder.listFiles(); 179 localPath = checkoutFolder; 180 181 for (int i = 0; i<files.length; i++) { 183 if (files[i].isFile()) continue; 184 if ("CVSROOT".equals(files[i].getName())) continue; files = files[i].listFiles(); 186 break; 187 } 188 189 } else { 190 if (file.isDirectory()) { 191 files = file.listFiles(); 192 localPath = file; 193 } else { 194 files = new File [] {file}; 195 localPath = file.getParentFile(); 196 } 197 } 198 199 List logFiles = new ArrayList(files.length); 200 fillLogFiles(files, logFiles); 201 if (logFiles.isEmpty()) { 202 tagsLoaded(Collections.EMPTY_SET, Collections.EMPTY_SET); 203 return; 204 } 205 206 LogCommand log = new LogCommand(); 208 log.setHeaderOnly(true); 209 File [] cmdFiles = (File []) logFiles.toArray(new File [logFiles.size()]); 210 log.setFiles(cmdFiles); 211 if (root == null) { 212 for (int i = 0; i<cmdFiles.length; i++) { 213 try { 214 root = CVSRoot.parse(Utils.getCVSRootFor(cmdFiles[i])); break; 216 } catch (IOException e) { 217 ErrorManager err = ErrorManager.getDefault(); 218 err.annotate(e, "Can not find CVSROOT for " + cmdFiles[i]); err.notify(ErrorManager.INFORMATIONAL, e); 220 } 221 } 222 } 223 Client client = Kit.createClient(root); 224 225 final Set tags = new TreeSet(); 226 final Set branches = new TreeSet(); 227 EventManager mgr = client.getEventManager(); 228 mgr.addCVSListener(new CVSListener() { 229 230 public void messageSent(MessageEvent e) { 231 } 232 public void messageSent(BinaryMessageEvent e) { 233 } 234 public void fileAdded(FileAddedEvent e) { 235 } 236 public void fileToRemove(FileToRemoveEvent e) { 237 } 238 public void fileRemoved(FileRemovedEvent e) { 239 } 240 public void fileUpdated(FileUpdatedEvent e) { 241 } 242 public void fileInfoGenerated(FileInfoEvent e) { 243 LogInformation info = (LogInformation) e.getInfoContainer(); 244 List symNames = info.getAllSymbolicNames(); 245 Iterator it = symNames.iterator(); 246 while (it.hasNext()) { 247 LogInformation.SymName name = (LogInformation.SymName) it.next(); 248 if (name.isBranch()) { 249 branches.add(name.getName()); 250 } else { 251 tags.add(name.getName()); 252 } 253 } 254 } 255 public void commandTerminated(TerminationEvent e) { 256 } 257 public void moduleExpanded(ModuleExpansionEvent e) { 258 } 259 }); 260 261 client.setLocalPath(localPath.getAbsolutePath()); 262 client.executeCommand(log, gtx); 263 tagsLoaded(branches, tags); 264 Kit.deleteRecursively(checkoutFolder); 265 } catch (CommandException e) { 266 error(org.openide.util.NbBundle.getMessage(BranchSelector.class, "BK2016")); 267 ErrorManager err = ErrorManager.getDefault(); 268 err.annotate(e, org.openide.util.NbBundle.getMessage(BranchSelector.class, "BK2016")); 269 err.notify(e); 270 } catch (AuthenticationException e) { 271 error(org.openide.util.NbBundle.getMessage(BranchSelector.class, "BK2016")); 272 ErrorManager err = ErrorManager.getDefault(); 273 err.annotate(e, org.openide.util.NbBundle.getMessage(BranchSelector.class, "BK2016")); 274 err.notify(e); 275 } finally { 276 Kit.deleteRecursively(checkoutFolder); 277 } 278 } 279 280 283 private void fillLogFiles(File [] files, List logFiles) { 284 285 if (files == null) { 286 return; 287 } 288 289 if (logFiles.isEmpty() == false) { 290 return; 291 } 292 293 for (int i = 0; i<files.length; i++) { 294 if (files[i].isFile()) { 295 FileInformation info = CvsVersioningSystem.getInstance().getStatusCache().getStatus(files[i]); 296 if ((info.getStatus() & FileInformation.STATUS_IN_REPOSITORY) != 0) { 297 logFiles.add(files[i]); 298 } 299 } 300 } 301 302 for (int i = 0; i<files.length; i++) { 303 if (files[i].isDirectory()) { 304 fillLogFiles(files[i].listFiles(), logFiles); } 306 } 307 308 } 309 310 313 private void tagsLoaded(Collection branches, Collection tags) { 314 rootKids.setBranches(branches); 315 rootKids.setTags(tags); 316 } 317 318 private void error(String msg) { 319 rootNode.setDisplayName(msg); 320 } 321 322 } 323 | Popular Tags |