1 28 29 package com.idaremedia.antx.print; 30 31 import org.apache.tools.ant.BuildException; 32 import org.apache.tools.ant.Project; 33 import org.apache.tools.ant.types.Reference; 34 35 import com.idaremedia.antx.AntX; 36 import com.idaremedia.antx.AssertableLibDefinition; 37 import com.idaremedia.antx.FixtureInitializer; 38 import com.idaremedia.antx.helpers.Strings; 39 import com.idaremedia.antx.helpers.Tk; 40 41 75 76 public final class ManagePrintersTask extends AssertableLibDefinition 77 implements FixtureInitializer 78 { 79 82 public ManagePrintersTask() 83 { 84 super(AntX.print+"ManagePrintersTask:"); 85 } 86 87 88 89 92 public void setRegistryId(Reference ref) 93 { 94 require_(ref!=null,"setRegId- nonzro ref"); 95 m_registryReference = ref; 96 } 97 98 99 100 104 public void setAction(String op) 105 { 106 m_Op = Tk.lowercaseFrom(op); 107 } 108 109 110 111 117 public void setOverwrite(boolean overwrite) 118 { 119 m_willSkip = overwrite ? Boolean.FALSE : Boolean.TRUE; 120 } 121 122 123 124 128 public final Reference getRegistryReference() 129 { 130 return m_registryReference; 131 } 132 133 134 135 140 protected final PrinterRegistry getPrinterRegistry() 141 { 142 return (PrinterRegistry)getProject().getReference(getRegistryReference().getRefId()); 143 } 144 145 146 147 150 private boolean isInstall() 151 { 152 return m_Op==null || Strings.INSTALL.equals(m_Op); 153 } 154 155 156 157 162 public void verifyCanExecute_(String calr) throws BuildException 163 { 164 super.verifyCanExecute_(calr); 165 Reference r = getRegistryReference(); 166 167 String ermsg = null; 168 if (r==null || r.getRefId()==null) { 169 if (isInstall()) { 170 ermsg = uistrs().get("task.needs.this.attr",getTaskName(),"registryid"); 171 } 172 } else { 173 Object object = getProject().getReference(r.getRefId()); 174 if (!(object instanceof PrinterRegistry)) { 175 ermsg = uistrs().get("initprint.bad.registryid",r.getRefId()); 176 } 177 } 178 if (ermsg!=null) { 179 log(ermsg,Project.MSG_ERR); 180 throw new BuildException(ermsg,getLocation()); 181 } 182 } 183 184 185 186 191 public void execute() throws BuildException 192 { 193 verifyCanExecute_("execute"); 194 195 if (isInstall()) { 197 PrinterRegistry newpr = getPrinterRegistry(); 198 PrinterRegistry oldpr = PrinterRegistryContext.setDefaultPrinterRegistry(newpr); 199 200 if (oldpr!=DFLT_PR && oldpr!=null && oldpr!=newpr) { 201 String warning = uistrs().get("initprint.err.too.many"); 202 log(warning,Project.MSG_WARN); 203 } else { 204 log("Installed default PrinterRegistry with id=\""+ 205 getRegistryReference().getRefId()+"\"", Project.MSG_DEBUG); 206 } 207 } 208 else if (Strings.UNINSTALL.equals(m_Op)) { 210 Reference r = getRegistryReference(); 211 212 if (r!=null && r.getRefId()!=null) { 213 PrinterRegistry expectedpr = getPrinterRegistry(); 214 if (PrinterRegistryContext.getDefaultPrinterRegistry()!=expectedpr) { 215 String warning = uistrs().get("initprint.mismatch.installed",r.getRefId()); 216 log(warning,Project.MSG_WARN); 217 } else { 218 PrinterRegistryContext.unsetDefaultPrinterRegistry(); 219 log("Uninstalled previously installed default PrinterRegistry with id=\""+ 220 expectedpr.getId()+"\"", Project.MSG_DEBUG); 221 } 222 } else { 223 PrinterRegistry oldpr = PrinterRegistryContext.unsetDefaultPrinterRegistry(); 224 if (oldpr!=DFLT_PR && oldpr!=null) { 225 log("Uninstalled default PrinterRegistry with id=\""+oldpr.getId()+"\"", 226 Project.MSG_DEBUG); 227 } 228 } 229 } 230 else { 232 String warning = uistrs().get("task.manager.err.unknown.operation",m_Op); 233 log(warning,Project.MSG_WARN); 234 } 235 } 236 237 238 239 240 private static final PrinterRegistry DFLT_PR= 241 PrinterRegistryContext.getEmptyPrinterRegistry(); 242 243 private Reference m_registryReference; 244 private String m_Op; private Boolean m_willSkip; 246 } 247 248 249 | Popular Tags |