?? color4jgui.java
字號(hào):
package jove.color4j;
import java.awt.*;
import java.awt.datatransfer.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import snoozesoft.systray4j.*;
/**
* @author Jove
*/
public class Color4jGUI extends JFrame implements SysTrayMenuListener {
public static void main(String[] args) {
/*
使用exe4j來(lái)保證單實(shí)例
File f = new File("file.lock");
if (f.exists()) {
showMessage("Warning: A instance has existed.");
System.exit(0);
}
try {
f.createNewFile();
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
f.deleteOnExit();
*/
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {}
new Color4jGUI().show();
}
private static void showMessage(String msg) {
JFrame dummy = new JFrame("Color4j");
dummy.setLocation(400, 300);
dummy.show();
JOptionPane.showMessageDialog(dummy, msg);
dummy.dispose();
}
private CheckableMenuItem checkMenuUbb, checkMenuAnsi;
private Color4j color4j;
private SysTrayMenuIcon icon;
private Color[] labelColor = { Color.RED, Color.BLUE };
private String[] labelText =
{
" Press F9 to convert and save result to clipboard",
" Press F8 to read data from clipboard and convert" };
private boolean labelTextFlag = true;
private SysTrayMenu menu;
private JRadioButton radioUbb, radioAnsi;
private Color4jGUI() {
super("Java Code Formatter");
setBounds(100, 100, 500, 400);
addWindowListener(new WindowListener() {
public void windowActivated(WindowEvent e) {}
public void windowClosed(WindowEvent e) {}
public void windowClosing(WindowEvent e) {
//hide();
dispose();
System.exit(0);
}
public void windowDeactivated(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowIconified(WindowEvent e) {
hide();
}
public void windowOpened(WindowEvent e) {}
});
Container con = getContentPane();
Box box = Box.createHorizontalBox();
final JLabel label = new JLabel(labelText[0]);
label.setForeground(labelColor[0]);
box.add(label);
Timer timer = new Timer(5000, new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (labelTextFlag) {
label.setText(labelText[1]);
label.setForeground(labelColor[1]);
} else {
label.setText(labelText[0]);
label.setForeground(labelColor[0]);
}
labelTextFlag = !labelTextFlag;
}
});
timer.start();
box.add(Box.createHorizontalGlue());
radioUbb = new JRadioButton("UBB", true);
radioAnsi = new JRadioButton("ANSI");
ChangeListener cListener = new ChangeListener() {
public void stateChanged(ChangeEvent e) {
boolean isUbb = radioUbb.isSelected();
checkMenuUbb.setState(isUbb);
checkMenuAnsi.setState(!isUbb);
}
};
radioUbb.addChangeListener(cListener);
radioAnsi.addChangeListener(cListener);
box.add(radioUbb);
box.add(radioAnsi);
box.add(Box.createHorizontalStrut(5));
ButtonGroup group = new ButtonGroup();
group.add(radioUbb);
group.add(radioAnsi);
con.add(box, BorderLayout.SOUTH);
final JTextArea textSource = new JTextArea();
JScrollPane sp = new JScrollPane(textSource);
JPanel p = new JPanel();
p.setLayout(new BorderLayout());
p.setBorder(BorderFactory.createTitledBorder("Source Code"));
p.add(sp);
con.add(p);
color4j = new Color4j();
KeyListener listener = new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_F9) {
convert(textSource.getText());
} else if (e.getKeyCode() == KeyEvent.VK_F8) {
convertFromCB();
}
}
};
textSource.addKeyListener(listener);
radioAnsi.addKeyListener(listener);
radioUbb.addKeyListener(listener);
icon = new SysTrayMenuIcon(getClass().getResource("icon.ico"));
icon.addSysTrayMenuListener(this);
createMenu();
}
private void convert(String code) {
String oldTitle=getTitle();
setTitle(oldTitle+" <converting>");
if (radioUbb.isSelected()) {
color4j.setStyleSet(StyleSet.UBB);
} else {
color4j.setStyleSet(StyleSet.ANSI);
}
code = color4j.convert(code);
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(
new StringSelection(code),
null);
//不再顯示Message
//showMessage("data have been sent to system clipboard.");
setTitle(oldTitle);
}
private void convertFromCB() {
Transferable clip =
Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);
String code = null;
try {
code = (String) clip.getTransferData(DataFlavor.stringFlavor);
} catch (Exception e) {
e.printStackTrace();
}
convert(code);
}
private void createMenu() {
// create an exit item
SysTrayMenuItem itemExit = new SysTrayMenuItem("Exit", "exit");
itemExit.addSysTrayMenuListener(this);
// create an about item
SysTrayMenuItem itemAbout = new SysTrayMenuItem("About...", "about");
itemAbout.addSysTrayMenuListener(this);
SysTrayMenuItem itemConvert =
new SysTrayMenuItem("Convert code from clipboard", "convert");
itemConvert.addSysTrayMenuListener(this);
SubMenu subMenu = new SubMenu("Type");
checkMenuUbb = new CheckableMenuItem("UBB", "ubb");
checkMenuUbb.addSysTrayMenuListener(this);
checkMenuUbb.setState(true);
checkMenuAnsi = new CheckableMenuItem("Ansi", "ansi");
checkMenuAnsi.addSysTrayMenuListener(this);
subMenu.addItem(checkMenuAnsi);
subMenu.addItem(checkMenuUbb);
menu = new SysTrayMenu(icon, "Color4j");
menu.addItem(itemExit);
menu.addSeparator();
menu.addItem(itemAbout);
menu.addSeparator();
menu.addItem(itemConvert);
menu.addItem(subMenu);
}
public void iconLeftClicked(SysTrayMenuEvent e) {
if (isVisible()) {
hide();
} else {
show();
setState(Frame.NORMAL);
toFront();
}
}
public void iconLeftDoubleClicked(SysTrayMenuEvent e) {}
public void menuItemSelected(SysTrayMenuEvent e) {
if (e.getActionCommand().equals("exit"))
System.exit(0);
else if (e.getActionCommand().equals("about")) {
JOptionPane.showMessageDialog(
this,
"Color4j. \nWith SysTray for Java v" + SysTrayMenu.VERSION);
} else if (e.getActionCommand().equals("ubb")) {
checkMenuUbb.setState(true);
checkMenuAnsi.setState(false);
radioUbb.setSelected(true);
} else if (e.getActionCommand().equals("ansi")) {
checkMenuUbb.setState(false);
checkMenuAnsi.setState(true);
radioAnsi.setSelected(true);
} else if (e.getActionCommand().equals("convert")) {
convertFromCB();
} else
JOptionPane.showMessageDialog(this, e.getActionCommand());
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -