345449.vhj5l3oj7.asiaButton.java2004-03-24T16:00:00Z2004-03-24T16:00:00Z<br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.command.java;
/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* This file is part of the design patterns project at UBC
*
* The contents of this file are subject to the Mozilla Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/.
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is ca.ubc.cs.spl.pattern.
*
* Contributor(s):
*/
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
/**
* Implements a simple extension of JButton that supplies its own
* ActionListener and calls its own <code>clicked()</code> method
* whenever the button is pressed. This method calls the <code>
* executeCommand()</code> method on the button's associate <i>Command</i>
* object.
*
* @author Jan Hannemann
* @author Gregor Kiczales
* @version 1.0, 07/13/02
*/
public class Button extends JButton {
/**
* the command object associated with this button
*/
protected Command command;
/**
* Creates a new button with the provided label
*
* @param name the label of the button
*/
public Button(String name) {
super(name);
this.setActionCommand(name);
this.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
clicked();
}
});
}
/**
* Calls <code>ececuteCommand()</code> on the associated
* command object. This method gets called whenever the
* button is pressed.
*/
public void clicked() {
if (command != null) {
command.executeCommand();
}
}
/**
* Sets the associated command object for this button
*
* @param command the new <i>Command</i> object.
*/
public void setCommand(Command command) {
this.command = command;
}
}</TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>2004-03-24T16:00:00ZButtonCommand.java2004-03-24T16:00:00Z2004-03-24T16:00:00Z<br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.command.java;
/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* This file is part of the design patterns project at UBC
*
* The contents of this file are subject to the Mozilla Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/.
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is ca.ubc.cs.spl.pattern.
*
* Contributor(s):
*/
/**
* Implements a sample command. This one prints a short message to
* <code>System.out</code> whenever it executes. The message is
* <quote>"ButtonCommand executed"</quote>.
*
* @author Jan Hannemann
* @author Gregor Kiczales
* @version 1.0, 07/13/02
*/
public class ButtonCommand implements Command {
/**
* Implements a sample command. This one prints a short message to
* <code>System.out</code> whenever it executes. The message is
* <quote>"ButtonCommand executed"</quote>.
*/
public void executeCommand() {
System.out.println("ButtonCommand executed");
}
}
</TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>2004-03-24T16:00:00ZButtonCommand2.java2004-03-24T16:00:00Z2004-03-24T16:00:00Z<br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.command.java;
/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* This file is part of the design patterns project at UBC
*
* The contents of this file are subject to the Mozilla Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/.
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is ca.ubc.cs.spl.pattern.
*
* Contributor(s):
*/
/**
* Implements a sample command. This one prints a short message to
* <code>System.out</code> whenever it executes. The message is
* <quote>"ButtonCommand number 2 executed"</quote>.
*
* @author Jan Hannemann
* @author Gregor Kiczales
* @version 1.0, 07/13/02
*/
public class ButtonCommand2 implements Command {
/**
* Implements a sample command. This one prints a short message to
* <code>System.out</code> whenever it executes. The message is
* <quote>"ButtonCommand number 2 executed"</quote>.
*/
public void executeCommand() {
System.out.println("ButtonCommand number 2 executed");
}
}
</TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>2004-03-24T16:00:00ZCommand.java2004-03-24T16:00:00Z2004-03-24T16:00:00Z<br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.command.java;
/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* This file is part of the design patterns project at UBC
*
* The contents of this file are subject to the Mozilla Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/.
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is ca.ubc.cs.spl.pattern.
*
* Contributor(s):
*/
/**
* Defines the interface for <i>Command</i> objects.
*
* @author Jan Hannemann
* @author Gregor Kiczales
* @version 1.0, 07/13/02
*/
public interface Command {
/**
* Executes the command.
*/
public void executeCommand();
}
</TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>2004-03-24T16:00:00ZMain.java2004-03-24T16:00:00Z2004-03-24T16:00:00Z<br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.command.java;
/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* This file is part of the design patterns project at UBC
*
* The contents of this file are subject to the Mozilla Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/.
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is ca.ubc.cs.spl.pattern.
*
* Contributor(s):
*/
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
/**
* Implements the driver for the command design pattern example.<p>
*
* Intent: <i>Encapsulate a request as an object, thereby letting you
* parameterize clients with different requests, queueu or log requests,
* and support undoable operations.</i><p>
*
* Participatng objects are <code>Button</code>s as <i>Invoker</i>s,
* and a <code>ButtonCommand</code> and <code>ButtonCommand2</code> as
* two concrete <i>Command</i>s.
*
* This example creates a simple GUI with three buttons. Each button has a
* command associated with it that is executed when the button is pressed.
* Button1 and button3 have the same command, button2 has a different one.
*
* <p><i>This is the Java version.</i><p>
*
* Both commands and invoker have to know of the pattern.
*
* @author Jan Hannemann
* @author Gregor Kiczales
* @version 1.0, 05/13/02
*
* @see Button
* @see Command
* @see ButtonCommand
* @see Buttoncommand2
*/
public class Main {
/**
* This example creates a simple GUI with three buttons. Each button has a
* command associated with it that is executed when the button is pressed.
* Button1 and button3 have the same command, button2 has a different one.
*/
public static void main(String[] args) {
Button button1 = new Button("Button1");
Button button2 = new Button("Button2");
Button button3 = new Button("Button3");
Command com1 = new ButtonCommand();
Command com2 = new ButtonCommand2();
JPanel pane = new JPanel();
pane.add(button1);
button1.setCommand(com1);
pane.add(button2);
button2.setCommand(com2);
pane.add(button3);
button3.setCommand(com1);
// Note: Can not have two commands.
// That is within the pattern specification
JFrame frame = new JFrame("Testing: Command Pattern");
frame.getContentPane().add(pane);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);}
});
frame.pack();
frame.setVisible(true);
}
}
</TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>2004-03-24T16:00:00Zfiles.lst2004-03-24T16:00:00Z2004-03-24T16:00:00Z<br/><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>2004-03-24T16:00:00Z