1 11 package org.eclipse.jdt.internal.ui.viewsupport; 12 13 import java.io.IOException ; 14 import java.io.Reader ; 15 import java.io.StringReader ; 16 import java.io.StringWriter ; 17 import java.util.HashSet ; 18 import java.util.Set ; 19 20 import org.eclipse.core.runtime.Assert; 21 22 import org.eclipse.core.resources.IProject; 23 import org.eclipse.core.resources.ProjectScope; 24 25 import org.eclipse.jface.text.templates.Template; 26 import org.eclipse.jface.text.templates.persistence.TemplatePersistenceData; 27 import org.eclipse.jface.text.templates.persistence.TemplateReaderWriter; 28 import org.eclipse.jface.text.templates.persistence.TemplateStore; 29 30 import org.eclipse.ui.preferences.ScopedPreferenceStore; 31 32 import org.eclipse.jdt.ui.JavaUI; 33 34 import org.eclipse.jdt.internal.ui.JavaPlugin; 35 36 39 public final class ProjectTemplateStore { 40 41 private static final String KEY= "org.eclipse.jdt.ui.text.custom_code_templates"; 43 private final TemplateStore fInstanceStore; 44 private final TemplateStore fProjectStore; 45 46 public ProjectTemplateStore(IProject project) { 47 fInstanceStore= JavaPlugin.getDefault().getCodeTemplateStore(); 48 if (project == null) { 49 fProjectStore= null; 50 } else { 51 final ScopedPreferenceStore projectSettings= new ScopedPreferenceStore(new ProjectScope(project), JavaUI.ID_PLUGIN); 52 fProjectStore= new TemplateStore(projectSettings, KEY) { 53 57 public void add(TemplatePersistenceData data) { 58 internalAdd(data); 59 } 60 61 public void save() throws IOException { 62 63 StringWriter output= new StringWriter (); 64 TemplateReaderWriter writer= new TemplateReaderWriter(); 65 writer.save(getTemplateData(false), output); 66 67 projectSettings.setValue(KEY, output.toString()); 68 projectSettings.save(); 69 } 70 }; 71 } 72 } 73 74 public static boolean hasProjectSpecificTempates(IProject project) { 75 String pref= new ProjectScope(project).getNode(JavaUI.ID_PLUGIN).get(KEY, null); 76 if (pref != null && pref.trim().length() > 0) { 77 Reader input= new StringReader (pref); 78 TemplateReaderWriter reader= new TemplateReaderWriter(); 79 TemplatePersistenceData[] datas; 80 try { 81 datas= reader.read(input); 82 return datas.length > 0; 83 } catch (IOException e) { 84 } 86 } 87 return false; 88 } 89 90 91 public TemplatePersistenceData[] getTemplateData() { 92 if (fProjectStore != null) { 93 return fProjectStore.getTemplateData(true); 94 } else { 95 return fInstanceStore.getTemplateData(true); 96 } 97 } 98 99 public Template findTemplateById(String id) { 100 Template template= null; 101 if (fProjectStore != null) 102 template= fProjectStore.findTemplateById(id); 103 if (template == null) 104 template= fInstanceStore.findTemplateById(id); 105 106 return template; 107 } 108 109 public void load() throws IOException { 110 if (fProjectStore != null) { 111 fProjectStore.load(); 112 113 Set datas= new HashSet (); 114 TemplatePersistenceData[] data= fProjectStore.getTemplateData(false); 115 for (int i= 0; i < data.length; i++) { 116 datas.add(data[i].getId()); 117 } 118 119 data= fInstanceStore.getTemplateData(false); 120 for (int i= 0; i < data.length; i++) { 121 TemplatePersistenceData orig= data[i]; 122 if (!datas.contains(orig.getId())) { 123 TemplatePersistenceData copy= new TemplatePersistenceData(new Template(orig.getTemplate()), orig.isEnabled(), orig.getId()); 124 fProjectStore.add(copy); 125 copy.setDeleted(true); 126 } 127 } 128 } 129 } 130 131 public boolean isProjectSpecific(String id) { 132 if (id == null) { 133 return false; 134 } 135 136 if (fProjectStore == null) 137 return false; 138 139 return fProjectStore.findTemplateById(id) != null; 140 } 141 142 143 public void setProjectSpecific(String id, boolean projectSpecific) { 144 Assert.isNotNull(fProjectStore); 145 146 TemplatePersistenceData data= fProjectStore.getTemplateData(id); 147 if (data == null) { 148 return; } else { 150 data.setDeleted(!projectSpecific); 151 } 152 } 153 154 public void restoreDefaults() { 155 if (fProjectStore == null) { 156 fInstanceStore.restoreDefaults(); 157 } else { 158 fProjectStore.restoreDefaults(); 159 } 160 } 161 162 public void save() throws IOException { 163 if (fProjectStore == null) { 164 fInstanceStore.save(); 165 } else { 166 fProjectStore.save(); 167 } 168 } 169 170 public void revertChanges() throws IOException { 171 if (fProjectStore != null) { 172 } else { 174 fInstanceStore.load(); 175 } 176 } 177 } 178 | Popular Tags |