package sse.ustc;
import java.awt.event.ActionEvent;
import java.io.File;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ActionMap;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.InputMap;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.table.TableModel;
import filescanner.FileMonitor;
public class MainFrame {
public static void main(String[] args) {
JFrame main = new JFrame();
ActionFrame frame = new ActionFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
/**
* A frame with a panel that demonstrates color change actions.
*/
class ActionFrame extends JFrame {
public ActionFrame() {
setTitle("SearchMusicServer");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
// add panel to frame
ActionPanel panel = new ActionPanel();
add(panel);
}
public static final int DEFAULT_WIDTH = 400;
public static final int DEFAULT_HEIGHT = 400;
}
/**
* A panel with buttons and keyboard shortcuts to change the background color.
*/
class ActionPanel extends JPanel {
BuildhMusicIndexFileThread buildIndexThread = null;
ServerThread serverThread = null;
FileServerThread fileThread = null;
public ActionPanel() {
// define actions
Action startAction = new MyAction("Start", new ImageIcon(
"yellow-ball.gif"));
Action stopAction = new MyAction("Stop", new ImageIcon("blue-ball.gif"));
add(new JButton(startAction));
add(new JButton(stopAction));
InputMap imap = getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
imap.put(KeyStroke.getKeyStroke("ctrl S"), "panel.start");
imap.put(KeyStroke.getKeyStroke("ctrl Q"), "panel.stop");
// associate the names with actions
ActionMap amap = getActionMap();
amap.put("panel.start", startAction);
amap.put("panel.stop", stopAction);
buildIndexThread = new BuildhMusicIndexFileThread();
serverThread = new ServerThread();
fileThread = new FileServerThread();
}
public class MyAction extends AbstractAction {
/**
* Constructs a color action.
*
* @param name
* the name to show on the button
* @param icon
* the icon to display on the button
* @param c
* the background color
*/
public MyAction(String name, Icon icon) {
putValue(Action.NAME, name);
putValue(Action.SMALL_ICON, icon);
putValue(Action.SHORT_DESCRIPTION, "Set panel color to "
+ name.toLowerCase());
}
public void actionPerformed(ActionEvent event) {
String name = (String)event.getActionCommand();
if("Start".equals(name)){
if(serverThread ==null || buildIndexThread ==null||fileThread ==null){
serverThread = new ServerThread();
buildIndexThread = new BuildhMusicIndexFileThread();
fileThread = new FileServerThread();
}
serverThread.start();
fileThread.start();
buildIndexThread.start();
}
if ("Stop".equals(name)) {
serverThread.setStop(true);
serverThread = null;
fileThread.setStop(true);
fileThread = null;
buildIndexThread = null;
}
}
}
}
|