1 7 8 package javax.print; 9 10 import java.awt.GraphicsConfiguration ; 11 import java.awt.GraphicsEnvironment ; 12 import java.awt.HeadlessException ; 13 import java.awt.Dialog ; 14 import java.awt.Frame ; 15 import java.awt.Window ; 16 import java.awt.KeyboardFocusManager ; 17 import javax.print.attribute.Attribute ; 18 import javax.print.attribute.AttributeSet ; 19 import javax.print.attribute.PrintRequestAttributeSet ; 20 import javax.print.attribute.standard.Destination ; 21 import javax.print.attribute.standard.Fidelity ; 22 23 import sun.print.ServiceDialog; 24 import sun.print.SunAlternateMedia; 25 26 50 51 public class ServiceUI { 52 53 54 133 public static PrintService printDialog(GraphicsConfiguration gc, 134 int x, int y, 135 PrintService [] services, 136 PrintService defaultService, 137 DocFlavor flavor, 138 PrintRequestAttributeSet attributes) 139 throws HeadlessException 140 { 141 int defaultIndex = -1; 142 143 if (GraphicsEnvironment.isHeadless()) { 144 throw new HeadlessException (); 145 } else if ((services == null) || (services.length == 0)) { 146 throw new IllegalArgumentException ("services must be non-null " + 147 "and non-empty"); 148 } else if (attributes == null) { 149 throw new IllegalArgumentException ("attributes must be non-null"); 150 } 151 152 if (defaultService != null) { 153 for (int i = 0; i < services.length; i++) { 154 if (services[i].equals(defaultService)) { 155 defaultIndex = i; 156 break; 157 } 158 } 159 160 if (defaultIndex < 0) { 161 throw new IllegalArgumentException ("services must contain " + 162 "defaultService"); 163 } 164 } else { 165 defaultIndex = 0; 166 } 167 168 boolean newFrame = false; 169 Window owner = 170 KeyboardFocusManager.getCurrentKeyboardFocusManager(). 171 getActiveWindow(); 172 173 if (!(owner instanceof Dialog || owner instanceof Frame )) { 174 owner = new Frame (); 175 newFrame = true; 176 } 177 178 ServiceDialog dialog; 179 if (owner instanceof Frame ) { 180 dialog = new ServiceDialog(gc, x, y, 181 services, defaultIndex, 182 flavor, attributes, 183 (Frame )owner); 184 } else { 185 dialog = new ServiceDialog(gc, x, y, 186 services, defaultIndex, 187 flavor, attributes, 188 (Dialog )owner); 189 } 190 dialog.show(); 191 192 if (dialog.getStatus() == ServiceDialog.APPROVE) { 193 PrintRequestAttributeSet newas = dialog.getAttributes(); 194 Class dstCategory = Destination .class; 195 Class amCategory = SunAlternateMedia.class; 196 Class fdCategory = Fidelity .class; 197 198 if (attributes.containsKey(dstCategory) && 199 !newas.containsKey(dstCategory)) 200 { 201 attributes.remove(dstCategory); 202 } 203 204 if (attributes.containsKey(amCategory) && 205 !newas.containsKey(amCategory)) 206 { 207 attributes.remove(amCategory); 208 } 209 210 attributes.addAll(newas); 211 212 Fidelity fd = (Fidelity )attributes.get(fdCategory); 213 if (fd != null) { 214 if (fd == Fidelity.FIDELITY_TRUE) { 215 removeUnsupportedAttributes(dialog.getPrintService(), 216 flavor, attributes); 217 } 218 } 219 } 220 221 if (newFrame) { 222 owner.dispose(); 223 } 224 225 return dialog.getPrintService(); 226 } 227 228 233 269 270 274 private static void removeUnsupportedAttributes(PrintService ps, 275 DocFlavor flavor, 276 AttributeSet aset) 277 { 278 AttributeSet asUnsupported = ps.getUnsupportedAttributes(flavor, 279 aset); 280 281 if (asUnsupported != null) { 282 Attribute [] usAttrs = asUnsupported.toArray(); 283 284 for (int i=0; i<usAttrs.length; i++) { 285 Class category = usAttrs[i].getCategory(); 286 287 if (ps.isAttributeCategorySupported(category)) { 288 Attribute attr = 289 (Attribute )ps.getDefaultAttributeValue(category); 290 291 if (attr != null) { 292 aset.add(attr); 293 } else { 294 aset.remove(category); 295 } 296 } else { 297 aset.remove(category); 298 } 299 } 300 } 301 } 302 } 303 | Popular Tags |