]> git.saurik.com Git - apple/mdnsresponder.git/blob - Clients/Java/SwingBrowseListener.java
mDNSResponder-87.tar.gz
[apple/mdnsresponder.git] / Clients / Java / SwingBrowseListener.java
1 /*
2 * Copyright (c) 2004 Apple Computer, Inc. All rights reserved.
3 *
4 * Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Computer, Inc.
5 * ("Apple") in consideration of your agreement to the following terms, and your
6 * use, installation, modification or redistribution of this Apple software
7 * constitutes acceptance of these terms. If you do not agree with these terms,
8 * please do not use, install, modify or redistribute this Apple software.
9 *
10 * In consideration of your agreement to abide by the following terms, and subject
11 * to these terms, Apple grants you a personal, non-exclusive license, under Apple's
12 * copyrights in this original Apple software (the "Apple Software"), to use,
13 * reproduce, modify and redistribute the Apple Software, with or without
14 * modifications, in source and/or binary forms; provided that if you redistribute
15 * the Apple Software in its entirety and without modifications, you must retain
16 * this notice and the following text and disclaimers in all such redistributions of
17 * the Apple Software. Neither the name, trademarks, service marks or logos of
18 * Apple Computer, Inc. may be used to endorse or promote products derived from the
19 * Apple Software without specific prior written permission from Apple. Except as
20 * expressly stated in this notice, no other rights or licenses, express or implied,
21 * are granted by Apple herein, including but not limited to any patent rights that
22 * may be infringed by your derivative works or by other works in which the Apple
23 * Software may be incorporated.
24 *
25 * The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO
26 * WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
27 * WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 * PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
29 * COMBINATION WITH YOUR PRODUCTS.
30 *
31 * IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
33 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
35 * OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
36 * (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
37 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 */
39
40
41 import javax.swing.*;
42 import com.apple.dnssd.*;
43
44
45 /** Use this to schedule BrowseListener callbacks via SwingUtilities.invokeAndWait(). */
46
47 public class SwingBrowseListener implements Runnable, BrowseListener
48 {
49 /** Create a listener for DNSSD that will call your listener on the Swing/AWT event thread. */
50 public SwingBrowseListener( BrowseListener listener)
51 { fListener = listener; fErrorCode = 0; }
52
53 /** (Clients should not call this method directly.) */
54 public void operationFailed( DNSSDService service, int errorCode)
55 {
56 fBrowser = service;
57 fErrorCode = errorCode;
58 this.schedule();
59 }
60
61 /** (Clients should not call this method directly.) */
62 public void serviceFound( DNSSDService browser, int flags, int ifIndex,
63 String serviceName, String regType, String domain)
64
65 {
66 fBrowser = browser;
67 fIsAdd = true;
68 fFlags = flags;
69 fIndex = ifIndex;
70 fService = serviceName;
71 fRegType = regType;
72 fDomain = domain;
73 this.schedule();
74 }
75
76 /** (Clients should not call this method directly.) */
77 public void serviceLost( DNSSDService browser, int flags, int ifIndex,
78 String serviceName, String regType, String domain)
79 {
80 fBrowser = browser;
81 fIsAdd = false;
82 fFlags = flags;
83 fIndex = ifIndex;
84 fService = serviceName;
85 fRegType = regType;
86 fDomain = domain;
87 this.schedule();
88 }
89
90 /** (Clients should not call this method directly.) */
91 public void run()
92 {
93 if ( fErrorCode != 0)
94 fListener.operationFailed( fBrowser, fErrorCode);
95 else if ( fIsAdd)
96 fListener.serviceFound( fBrowser, fFlags, fIndex, fService, fRegType, fDomain);
97 else
98 fListener.serviceLost( fBrowser, fFlags, fIndex, fService, fRegType, fDomain);
99 }
100
101 protected void schedule()
102 {
103 try {
104 SwingUtilities.invokeAndWait( this);
105 }
106 catch ( Exception e)
107 {
108 e.printStackTrace();
109 }
110 }
111
112 protected BrowseListener fListener;
113
114 protected boolean fIsAdd;
115 protected DNSSDService fBrowser;
116 protected int fFlags;
117 protected int fIndex;
118 protected int fErrorCode;
119 protected String fService;
120 protected String fRegType;
121 protected String fDomain;
122 }
123