1 19 20 package org.netbeans.modules.project.ui.groups; 21 22 import java.util.Arrays ; 23 import java.util.Collection ; 24 import java.util.HashSet ; 25 import java.util.Set ; 26 import java.util.TreeSet ; 27 import java.util.logging.Level ; 28 import java.util.logging.Logger ; 29 import java.util.prefs.Preferences ; 30 import org.netbeans.api.progress.ProgressHandle; 31 import org.netbeans.api.project.Project; 32 import org.netbeans.api.project.ui.OpenProjects; 33 import org.openide.filesystems.FileStateInvalidException; 34 35 39 public class AdHocGroup extends Group { 40 41 private static final Logger LOG = Logger.getLogger(AdHocGroup.class.getName()); 42 43 44 private static final String KEY_AUTO_SYNCH = "autoSynch"; 46 static final String KIND = "adHoc"; 48 51 public static AdHocGroup create(String name, boolean autoSynch) { 52 String id = sanitizeNameAndUniquifyForId(name); 53 LOG.log(Level.FINE, "Creating: {0}", id); 54 Preferences p = NODE.node(id); 55 p.put(KEY_NAME, name); 56 p.put(KEY_KIND, KIND); 57 p.putBoolean(KEY_AUTO_SYNCH, autoSynch); 58 return new AdHocGroup(id); 59 } 60 61 AdHocGroup(String id) { 62 super(id); 63 } 64 65 protected void findProjects(Set <Project> projects, ProgressHandle h, int start, int end) { 66 String paths = prefs().get(KEY_PATH, ""); 67 if (paths.length() > 0) { String [] items = paths.split(" "); 69 for (String path : items) { 70 Project p = projectForPath(path); 71 if (p != null) { 72 if (h != null) { 73 h.progress(progressMessage(p), start += ((end - start) / items.length)); 74 } 75 projects.add(p); 76 } 77 } 78 } 79 } 80 81 84 public void setProjects(Set <Project> projects) { 85 Set <String > projectPaths = new TreeSet <String >(); 86 for (Project prj : projects) { 87 try { 88 projectPaths.add(prj.getProjectDirectory().getURL().toExternalForm()); 89 } catch (FileStateInvalidException x) { 90 LOG.log(Level.WARNING, null, x); 91 } 92 } 93 prefs().put(KEY_PATH, joinPaths(projectPaths)); 94 LOG.log(Level.FINE, "updating projects for {0} to {1}", new Object [] {id, projects}); 95 } 96 97 private static String joinPaths(Collection <String > paths) { 98 StringBuilder b = new StringBuilder (); 99 for (String p : paths) { 100 if (b.length() > 0) { 101 b.append(' '); 102 } 103 b.append(p); 104 } 105 return b.toString(); 106 } 107 108 111 public boolean isAutoSynch() { 112 return prefs().getBoolean(KEY_AUTO_SYNCH, false); 113 } 114 115 118 public void setAutoSynch(boolean b) { 119 prefs().putBoolean(KEY_AUTO_SYNCH, b); 120 } 121 122 125 public void synch() { 126 OpenProjects op = OpenProjects.getDefault(); 127 setProjects(new HashSet <Project>(Arrays.asList(op.getOpenProjects()))); 128 setMainProject(op.getMainProject()); 129 } 130 131 public GroupEditPanel createPropertiesPanel() { 132 return Group.isAdvancedMode() ? new AdHocGroupEditPanel(this) : new AdHocGroupEditPanelBasic(this); 133 } 134 135 @Override 136 protected void closed() { 137 if (isAutoSynch()) { 138 setProjects(new HashSet <Project>(Arrays.asList(OpenProjects.getDefault().getOpenProjects()))); 139 } 140 super.closed(); 142 } 143 144 @Override 145 public boolean isPristine() { 146 if (isAutoSynch()) { 147 return true; 148 } else { 149 return super.isPristine(); 150 } 151 } 152 153 @Override 154 protected String toString(boolean scrubPersonalInfo) { 155 return super.toString(scrubPersonalInfo) + (isAutoSynch() ? "" : "[!autoSynch]"); 156 } 157 158 } 159 | Popular Tags |