Retired Document
Important: This sample code may not represent best practices for current development. The project may use deprecated symbols and illustrate technologies and techniques that are no longer recommended.
Source/AESend/AESendFrame.java
/* |
File: AESendFrame.java |
Description: Sample showing how to send and receive AppleEvents using JDirect 3. |
Author: EPJ, LB, MH |
Copyright: © Copyright 1999 - 2002 Apple Computer, Inc. All rights reserved. |
Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Computer, Inc. |
("Apple") in consideration of your agreement to the following terms, and your |
use, installation, modification or redistribution of this Apple software |
constitutes acceptance of these terms. If you do not agree with these terms, |
please do not use, install, modify or redistribute this Apple software. |
In consideration of your agreement to abide by the following terms, and subject |
to these terms, Apple grants you a personal, non-exclusive license, under AppleÕs |
copyrights in this original Apple software (the "Apple Software"), to use, |
reproduce, modify and redistribute the Apple Software, with or without |
modifications, in source and/or binary forms; provided that if you redistribute |
the Apple Software in its entirety and without modifications, you must retain |
this notice and the following text and disclaimers in all such redistributions of |
the Apple Software. Neither the name, trademarks, service marks or logos of |
Apple Computer, Inc. may be used to endorse or promote products derived from the |
Apple Software without specific prior written permission from Apple. Except as |
expressly stated in this notice, no other rights or licenses, express or implied, |
are granted by Apple herein, including but not limited to any patent rights that |
may be infringed by your derivative works or by other works in which the Apple |
Software may be incorporated. |
The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO |
WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED |
WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN |
COMBINATION WITH YOUR PRODUCTS. |
IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR |
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION |
OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT |
(INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN |
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Change History (most recent first): |
* version 1.0 |
* 4/15/1999 Shipped as 'AppleEvent Send and Receive' sample. |
* version 2.0 |
* 4/2/2002 Updated to Swing |
* This class contains this sample's user interface for sending AppleEvents. |
*/ |
package com.apple.dts.samplecode.aesendandreceive.aesend; |
import java.awt.Color; |
import java.awt.Insets; |
import java.awt.Font; |
import java.awt.Toolkit; |
import java.awt.GridBagLayout; |
import java.awt.GridBagConstraints; |
import java.awt.event.KeyEvent; |
import java.awt.event.ActionEvent; |
import java.awt.event.ActionListener; |
import javax.swing.JFrame; |
import javax.swing.JButton; |
import com.apple.dts.samplecode.aesendandreceive.shared.*; |
public class AESendFrame extends JFrame |
{ |
/** |
* Creates the UI for the AESend example. |
*/ |
public AESendFrame() throws NativeException |
{ |
//{{INIT_CONTROLS |
GridBagLayout gridBagLayout; |
gridBagLayout = new GridBagLayout(); |
getContentPane().setLayout(gridBagLayout); |
setVisible(false); |
setSize(466,90); |
setBackground(new Color(-1052689)); |
divider1 = new Divider(); |
divider1.setBounds(5,51,456,2); |
GridBagConstraints gbc; |
gbc = new GridBagConstraints(); |
gbc.gridx = 1; |
gbc.gridy = 4; |
gbc.gridwidth = 2; |
gbc.weightx = 0.5; |
gbc.fill = GridBagConstraints.HORIZONTAL; |
gbc.insets = new Insets(0,5,0,5); |
((GridBagLayout)getContentPane().getLayout()).setConstraints(divider1, gbc); |
getContentPane().add(divider1); |
label1 = new javax.swing.JLabel("Message to Send:"); |
label1.setBounds(5,4,94,21); |
label1.setFont(new Font("SansSerif", Font.BOLD, 9)); |
gbc = new GridBagConstraints(); |
gbc.gridx = 1; |
gbc.gridy = 1; |
gbc.anchor = GridBagConstraints.WEST; |
gbc.fill = GridBagConstraints.NONE; |
gbc.insets = new Insets(0,5,0,0); |
((GridBagLayout)getContentPane().getLayout()).setConstraints(label1, gbc); |
getContentPane().add(label1); |
sendButton = new javax.swing.JButton(); |
sendButton.setText("Send Event"); |
sendButton.setBounds(376,25,85,20); |
gbc = new GridBagConstraints(); |
gbc.gridx = 2; |
gbc.gridy = 2; |
gbc.fill = GridBagConstraints.NONE; |
gbc.insets = new Insets(0,0,0,5); |
((GridBagLayout)getContentPane().getLayout()).setConstraints(sendButton, gbc); |
getContentPane().add(sendButton); |
textField1 = new javax.swing.JTextField(); |
textField1.setText("Hello there, other Java applicaion!"); |
textField1.setBounds(10,25,361,21); |
textField1.setFont(new Font("SansSerif", Font.PLAIN, 9)); |
textField1.setBackground(new Color(16777215)); |
gbc = new GridBagConstraints(); |
gbc.gridx = 1; |
gbc.gridy = 2; |
gbc.weightx = 0.5; |
gbc.anchor = GridBagConstraints.WEST; |
gbc.fill = GridBagConstraints.HORIZONTAL; |
gbc.insets = new Insets(0,10,5,5); |
((GridBagLayout)getContentPane().getLayout()).setConstraints(textField1, gbc); |
getContentPane().add(textField1); |
panel1 = new javax.swing.JPanel(); |
gridBagLayout = new GridBagLayout(); |
panel1.setLayout(gridBagLayout); |
panel1.setBounds(0,53,466,21); |
gbc = new GridBagConstraints(); |
gbc.gridx = 1; |
gbc.gridy = 5; |
gbc.gridwidth = 2; |
gbc.weightx = 0.5; |
gbc.anchor = GridBagConstraints.WEST; |
gbc.fill = GridBagConstraints.HORIZONTAL; |
gbc.insets = new Insets(0,0,0,0); |
((GridBagLayout)getContentPane().getLayout()).setConstraints(panel1, gbc); |
getContentPane().add(panel1); |
statusLabel = new javax.swing.JLabel("Idle"); |
statusLabel.setBounds(50,0,416,21); |
statusLabel.setFont(new Font("SansSerif", Font.PLAIN, 9)); |
gbc = new GridBagConstraints(); |
gbc.gridx = 2; |
gbc.gridy = 1; |
gbc.weightx = 0.5; |
gbc.anchor = GridBagConstraints.WEST; |
gbc.fill = GridBagConstraints.HORIZONTAL; |
gbc.insets = new Insets(0,0,0,0); |
((GridBagLayout)panel1.getLayout()).setConstraints(statusLabel, gbc); |
panel1.add(statusLabel); |
label2 = new javax.swing.JLabel("Status:"); |
label2.setBounds(5,0,40,21); |
label2.setFont(new Font("SansSerif", Font.BOLD, 9)); |
gbc = new GridBagConstraints(); |
gbc.gridx = 1; |
gbc.gridy = 1; |
gbc.anchor = GridBagConstraints.WEST; |
gbc.fill = GridBagConstraints.NONE; |
gbc.insets = new Insets(0,5,0,5); |
((GridBagLayout)panel1.getLayout()).setConstraints(label2, gbc); |
panel1.add(label2); |
panel2 = new javax.swing.JPanel(); |
gridBagLayout = new GridBagLayout(); |
panel2.setLayout(gridBagLayout); |
panel2.setBounds(0,53,466,21); |
gbc = new GridBagConstraints(); |
gbc.gridx = 1; |
gbc.gridy = 3; |
gbc.gridwidth = 2; |
gbc.weightx = 0.5; |
gbc.anchor = GridBagConstraints.WEST; |
gbc.fill = GridBagConstraints.HORIZONTAL; |
gbc.insets = new Insets(0,0,0,0); |
((GridBagLayout)getContentPane().getLayout()).setConstraints(panel2, gbc); |
getContentPane().add(panel2); |
replyLabel = new javax.swing.JLabel("Idle"); |
replyLabel.setBounds(50,0,416,21); |
replyLabel.setFont(new Font("SansSerif", Font.PLAIN, 9)); |
gbc = new GridBagConstraints(); |
gbc.gridx = 2; |
gbc.gridy = 1; |
gbc.weightx = 0.5; |
gbc.anchor = GridBagConstraints.WEST; |
gbc.fill = GridBagConstraints.HORIZONTAL; |
gbc.insets = new Insets(0,0,0,0); |
((GridBagLayout)panel2.getLayout()).setConstraints(replyLabel, gbc); |
panel2.add(replyLabel); |
label4 = new javax.swing.JLabel("Reply:"); |
label4.setBounds(5,0,40,21); |
label4.setFont(new Font("SansSerif", Font.BOLD, 9)); |
gbc = new GridBagConstraints(); |
gbc.gridx = 1; |
gbc.gridy = 1; |
gbc.anchor = GridBagConstraints.WEST; |
gbc.fill = GridBagConstraints.NONE; |
gbc.insets = new Insets(0,5,0,5); |
((GridBagLayout)panel2.getLayout()).setConstraints(label4, gbc); |
panel2.add(label4); |
setTitle("AESend"); |
//}} |
//{{INIT_MENUS |
//}} |
//{{REGISTER_LISTENERS |
SymWindow aSymWindow = new SymWindow(); |
this.addWindowListener(aSymWindow); |
SymAction lSymAction = new SymAction(); |
sendButton.addActionListener(lSymAction); |
textField1.addActionListener(lSymAction); |
//}} |
//Create a new timer to handle status messages. |
statusTimer = new Timer(kEraseSleep, new TimerCallback() |
{ |
public void timeIsUp() |
{ |
isEraseStatus = false; |
try |
{ |
setStatus("Idle"); |
} |
finally |
{ |
isEraseStatus = true; |
} |
} |
}); |
isEraseStatus = true; |
//Create a new timer to handle reply messages. |
replyTimer = new Timer(kEraseSleep, new TimerCallback() |
{ |
public void timeIsUp() |
{ |
isEraseReply = false; |
try |
{ |
setReply("Idle"); |
} |
finally |
{ |
isEraseReply = true; |
} |
} |
}); |
isEraseReply = true; |
//Create a new AESend object so we can send our event later. |
aeSend = new AESend(); |
//Register a new instance of our ActionListener with the AEReceive object |
//so we get notified when an AppleEvent is received. |
aeSend.addActionListener(new Action()); |
} |
/** |
* Shows or hides the component depending on the boolean flag b. |
* param b if true, show the component; otherwise, hide the component. |
* see java.awt.Component#isVisible |
*/ |
public void setVisible(boolean b) |
{ |
if(b) |
{ |
setLocation(50, 50); |
} |
super.setVisible(b); |
} |
/** |
* The entry point to our application |
*/ |
static public void main(String args[]) |
{ |
try { |
(new AESendFrame()).setVisible(true); |
} catch (NativeException e) { |
e.printStackTrace(); |
} |
} |
//{{DECLARE_CONTROLS |
Divider divider1; |
javax.swing.JLabel label1; |
javax.swing.JButton sendButton; |
javax.swing.JTextField textField1; |
javax.swing.JPanel panel1; |
javax.swing.JLabel statusLabel; |
javax.swing.JLabel label2; |
javax.swing.JPanel panel2; |
javax.swing.JLabel replyLabel; |
javax.swing.JLabel label4; |
//}} |
//{{DECLARE_MENUS |
//}} |
/** |
* Set the text of the statusLabel |
* param the message to use. |
*/ |
protected void setStatus(String message) |
{ |
statusLabel.setText(message); |
if (isEraseStatus) |
statusTimer.start(); |
} |
/** |
* Set the text of the message replyLabel |
* param the message to use. |
*/ |
protected void setReply(String message) |
{ |
replyLabel.setText(message); |
if (isEraseReply) |
replyTimer.start(); |
} |
/** |
* A function to simulate a click on the target button. |
* This will make the button draw as if it had been pressed and |
* released, and the button will fire an Action event as if |
* the button were pressed. |
* For use with the Apple MRJ 2.1 EA3 and later. |
*/ |
protected void simulateClick(JButton target) |
{ |
if (target != null) |
{ |
KeyEvent keyEvent = new KeyEvent(target, KeyEvent.KEY_PRESSED, |
System.currentTimeMillis(), 0, KeyEvent.VK_ENTER, |
(char)KeyEvent.VK_ENTER); |
target.dispatchEvent(keyEvent); |
} |
} |
/** |
* An inner class designed to listen for ActionEvents |
* from the AESend class. |
*/ |
class Action implements ActionListener |
{ |
public void actionPerformed(ActionEvent event) |
{ |
setReply(event.getActionCommand()); |
} |
} |
class SymWindow extends java.awt.event.WindowAdapter |
{ |
public void windowClosing(java.awt.event.WindowEvent event) |
{ |
Object object = event.getSource(); |
if (object == AESendFrame.this) |
AESendFrame_WindowClosing(event); |
} |
} |
void AESendFrame_WindowClosing(java.awt.event.WindowEvent event) |
{ |
setVisible(false); // hide the AESendFrame |
dispose(); // free the system resources |
System.exit(0); // close the application |
} |
class SymAction implements java.awt.event.ActionListener |
{ |
public void actionPerformed(java.awt.event.ActionEvent event) |
{ |
Object object = event.getSource(); |
if (object == sendButton) |
sendButton_ActionPerformed(event); |
else if (object == textField1) |
textField1_EnterHit(event); |
} |
} |
void sendButton_ActionPerformed(java.awt.event.ActionEvent event) |
{ |
String text = textField1.getText(); |
if (text != null && !text.equals("")) |
{ |
setStatus("Sending AppleEvent..."); |
try |
{ |
aeSend.sendAppleEvent(text); |
} |
catch (NativeException exc) |
{ |
Toolkit.getDefaultToolkit().beep(); |
setStatus(exc.getMessage() + " (#" + exc.getErrNum() + ")"); |
return; |
} |
setStatus("AppleEvent Sent"); |
} |
} |
void textField1_EnterHit(java.awt.event.ActionEvent event) |
{ |
simulateClick(sendButton); |
} |
/** |
* The time in milliseconds to wait before removing the previous status message. |
*/ |
protected static final int kEraseSleep = 1250; |
/** |
* Our AESend object which allows us to send the AppleEvent |
*/ |
protected AESend aeSend; |
/** |
* An internal flag to prevent recursion in the status message handling code. |
*/ |
protected boolean isEraseStatus; |
/** |
* An internal flag to prevent recursion in the reply message handling code. |
*/ |
protected boolean isEraseReply; |
/** |
* A timer to keep track of displaying the status message. |
*/ |
protected Timer statusTimer; |
/** |
* A timer to keep track of displaying the reply message. |
*/ |
protected Timer replyTimer; |
} |
Copyright © 2003 Apple Computer, Inc. All Rights Reserved. Terms of Use | Privacy Policy | Updated: 2003-01-14