001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package javax.print;
019:
020: import java.awt.Dialog;
021: import java.awt.Frame;
022: import java.awt.GraphicsConfiguration;
023: import java.awt.GraphicsEnvironment;
024: import java.awt.HeadlessException;
025: import java.awt.KeyboardFocusManager;
026: import java.awt.Window;
027: import javax.print.attribute.PrintRequestAttributeSet;
028: import org.apache.harmony.x.print.ServiceUIDialog;
029:
030: public class ServiceUI {
031: public static PrintService printDialog(GraphicsConfiguration gc,
032: int x, int y, PrintService[] services,
033: PrintService defaultService, DocFlavor flavor,
034: PrintRequestAttributeSet attributes)
035: throws HeadlessException {
036: if (GraphicsEnvironment.isHeadless()) {
037: throw new HeadlessException();
038: }
039: int initialIndex = checkServices(services, defaultService,
040: attributes);
041: Window activeWindow = KeyboardFocusManager
042: .getCurrentKeyboardFocusManager().getActiveWindow();
043: Window dialogOwner = getDialogOwner(activeWindow);
044: ServiceUIDialog dialog = new ServiceUIDialog(gc, x, y,
045: services, initialIndex, flavor, attributes, dialogOwner);
046: dialog.show();
047: if (dialogOwner != activeWindow) {
048: dialogOwner.dispose();
049: }
050: if (dialog.getResult() == ServiceUIDialog.APPROVE_PRINT) {
051: attributes.clear();
052: attributes.addAll(dialog.getAttributes());
053: return dialog.getPrintService();
054: }
055: return null;
056: }
057:
058: /**
059: * This function checks services, defaultService and attributes input
060: * parameters for printDialog(...) method.
061: *
062: * @return defaultService index in services array or 0 if defaultService is
063: * null.
064: *
065: * @throws IllegalArgumentException if services is null or empty, or
066: * attributes is null, or the initial PrintService is not in the
067: * list of browseable services
068: */
069: static int checkServices(PrintService[] services,
070: PrintService defaultService,
071: PrintRequestAttributeSet attributes) {
072: if (services == null) {
073: throw new IllegalArgumentException("Services list is null!");
074: } else if (services.length == 0) {
075: throw new IllegalArgumentException(
076: "Services list is empty!");
077: } else if (attributes == null) {
078: throw new IllegalArgumentException("Attribute set is null!");
079: }
080: int serviceIndex = 0;
081: boolean defaultServiceFound = (defaultService == null);
082: for (int i = 0; i < services.length; i++) {
083: if (services[i].equals(defaultService)) {
084: serviceIndex = i;
085: defaultServiceFound = true;
086: break;
087: }
088: }
089: if (!defaultServiceFound) {
090: throw new IllegalArgumentException(
091: "Default service is absent in the services list!");
092: }
093: return serviceIndex;
094: }
095:
096: /**
097: * This functions checks if current activeWindow can be parent for
098: * ServiceUIDialog window.
099: *
100: * @return window if the given window is Dialog or Frame, otherwise returns
101: * new Frame object.
102: */
103: private static Window getDialogOwner(Window window) {
104: return ((window instanceof Dialog) || (window instanceof Frame)) ? window
105: : new Frame();
106: }
107: } /* End of ServiceUI class */
|