import java.awt.event.*;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import javax.swing.*;
public class SwingFrame extends JFrame implements ActionListener {
Container contentPane;
JTextField jtf_Status;
JLayeredPane desktop;
private Action actNewFile, actOpenFile, actOpenWeb, actSaveFile, actHelp, actAbout;
private JToolBar toolBar;
private String iconSize="16";
private KeyStroke keyStroke;
//Menueobjekte
JMenuBar menuBar;
JMenu menuFile, menuOpen, menuHelp;
JMenuItem jmi_New, jmi_OpenFile, jmi_OpenWeb, jmi_Close, jmi_Save, jmi_SaveAs, jmi_Help, jmi_About;
private JButton jbtn_New, jbtn_OpenFile, jbtn_OpenWeb, jbtn_Save, jbtn_Help, jbtn_About;
//Definition der ToolTip-Konstanten
private final String TTT_New = "Create new File";
private final String TTT_OpenFile = "Open File";
private final String TTT_OpenWeb = "Open Web";
private final String TTT_Save = "Save File";
private final String TTT_Help = "Help";
private final String TTT_About = "About this program";
private JEditorPane jep_EditorPane;
private FileBrowser fileBrowser;
private JTextPane jtp_HelpPane, jtp_InfoPane;
File file;
public SwingFrame() {
super("Simple Editor");
setSize(600,450);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
contentPane = this.getContentPane();
desktop = getLayeredPane();
buildMenu();
defineTooltips();
createToolBar();
//Statusfeld
jtf_Status = new JTextField("");
jtf_Status.setBackground(Color.black);
jtf_Status.setForeground(Color.white);
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.add(BorderLayout.SOUTH, jtf_Status);
jep_EditorPane = new JEditorPane();
JScrollPane jsp_ScrollPane = new JScrollPane(jep_EditorPane);
panel.add(BorderLayout.CENTER,jsp_ScrollPane);
contentPane.add(BorderLayout.CENTER, panel);
setVisible(true);
fileBrowser = new FileBrowser(jtf_Status);
}
public static void main(String[] args) {
new SwingFrame();
}
private void buildMenu() {
//Menuebar
menuBar = new JMenuBar();
setJMenuBar(menuBar);
//Dateimenue
menuFile = new JMenu("File");
menuFile.setMnemonic('F');
actNewFile = new AbstractAction("New...", new ImageIcon("images/New"+iconSize+".gif")) {
public void actionPerformed(ActionEvent ae) {
jep_EditorPane.setContentType("text/plain");
jep_EditorPane.setText("");
jtf_Status.setText("Created new file");
}
};
actNewFile.putValue(Action.SHORT_DESCRIPTION, TTT_New);
jmi_New = menuFile.add(actNewFile);
jmi_New.setMnemonic('N');
//Oeffnen-Untermenue
menuOpen = new JMenu("Open");
menuOpen.setMnemonic('O');
actOpenFile = new AbstractAction("Filesystem...", new ImageIcon("images/Open"+iconSize+".gif")) {
public void actionPerformed(ActionEvent ae) {
String str = fileBrowser.chooseText("Datei waehlen", "./");
file = fileBrowser.getFile();
jep_EditorPane.setContentType("text/plain");
if(str!=null) {
jep_EditorPane.setText(str);
jtf_Status.setText("'Open File...' selected");
} else {
jtf_Status.setText("No file selected!");
}
}
};
//KeyStroke-Objekt (Shortcut) definieren und im Action-Objekt speichern
keyStroke = keyStroke.getKeyStroke(KeyEvent.VK_O, InputEvent.CTRL_MASK);
actOpenFile.putValue(Action.ACCELERATOR_KEY, keyStroke);
actOpenFile.putValue(Action.SHORT_DESCRIPTION, TTT_OpenFile);
jmi_OpenFile = menuOpen.add(actOpenFile);
jmi_OpenFile.setMnemonic('y');
actOpenWeb = new AbstractAction("Web...", new ImageIcon("images/WebComponent"+iconSize+".gif")) {
public void actionPerformed(ActionEvent ae) {
URL url = fileBrowser.chooseURL("HTML-Seite wählen", "./");
if(url != null) {
try {
jep_EditorPane.setPage(url);
file = fileBrowser.getFile();
} catch (IOException ioe) {
System.out.println(ioe.toString());
}
}
}
};
actOpenWeb.putValue(Action.SHORT_DESCRIPTION, TTT_OpenWeb);
jmi_OpenWeb = menuOpen.add(actOpenWeb);
jmi_OpenWeb.setMnemonic('W');
menuFile.add(menuOpen);
jmi_Close = new JMenuItem("Close");
jmi_Close.setMnemonic('C');
jmi_Close.addActionListener(this); //Listener registrieren
menuFile.add(jmi_Close);
menuFile.addSeparator();
actSaveFile = new AbstractAction("Save", new ImageIcon("images/Save"+iconSize+".gif")) {
public void actionPerformed(ActionEvent ae) {
if(file != null) {
jtf_Status.setText("'Save' selected");
save();
} else {
jtf_Status.setText("'Save as...' selected");
saveAs();
}
}
};
actSaveFile.putValue(Action.SHORT_DESCRIPTION, TTT_Save);
jmi_Save = menuFile.add(actSaveFile);
jmi_Save.setMnemonic('S');
jmi_SaveAs = new JMenuItem("Save as...");
jmi_SaveAs.addActionListener(this);
jmi_SaveAs.setMnemonic('a');
menuFile.add(jmi_SaveAs);
menuBar.add(menuFile); //zur Menuebar hinzufuegen
//Hilfemenue
menuHelp = new JMenu("?");
menuHelp.setMnemonic('?');
actHelp = new AbstractAction("Help", new ImageIcon("images/Help"+iconSize+".gif")) {
public void actionPerformed(ActionEvent ae) {
jtf_Status.setText("'Help' selected");
showHelp();
}
};
keyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_F1, 0);
actHelp.putValue(Action.ACCELERATOR_KEY, keyStroke);
actHelp.putValue(Action.SHORT_DESCRIPTION, TTT_Help);
jmi_Help = menuHelp.add(actHelp);
jmi_Help.setMnemonic('H');
menuHelp.addSeparator();
actAbout = new AbstractAction("Info", new ImageIcon("images/About"+iconSize+".gif")) {
public void actionPerformed(ActionEvent ae) {
jtf_Status.setText("'Info' selected");
showInfo();
}
};
keyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_I, InputEvent.CTRL_MASK);
actAbout.putValue(Action.ACCELERATOR_KEY, keyStroke);
actAbout.putValue(Action.SHORT_DESCRIPTION, TTT_About);
jmi_About = menuHelp.add(actAbout);
jmi_About.setMnemonic('I');
menuBar.add(menuHelp); //zur Menuebar hinzufuegen
}
public void actionPerformed(ActionEvent ae) {
if(ae.getSource().equals(jmi_Close))
System.exit(0);
else if(ae.getSource().equals(jmi_SaveAs))
jtf_Status.setText("'Save as' selected");
saveAs();
}
private void defineTooltips() {
jmi_New.setToolTipText("Create a new file");
jmi_OpenFile.setToolTipText("Open a existing local file");
jmi_OpenWeb.setToolTipText("Open a existing file from browser");
jmi_Save.setToolTipText("Save selected file");
jmi_Help.setToolTipText("Open help");
jmi_About.setToolTipText("Something about this program");
}
private void createToolBar() {
toolBar = new JToolBar();
//alle ToolBar-Buttons initialisieren
jbtn_New = toolBar.add(actNewFile);
jbtn_OpenFile = toolBar.add(actOpenFile);
jbtn_OpenWeb = toolBar.add(actOpenWeb);
jbtn_Save = toolBar.add(actSaveFile);
toolBar.addSeparator();
jbtn_Help = toolBar.add(actHelp);
jbtn_About = toolBar.add(actAbout);
contentPane.add(BorderLayout.NORTH, toolBar);
}
private void save() {
fileBrowser.saveFile(file, jep_EditorPane.getText());
}
private void saveAs() {
fileBrowser.saveFileAs(jep_EditorPane.getText(), "Choose file", "./");
}
private void showHelp() {
JInternalFrame internalFrame = new JInternalFrame("Help", true, true, false, false);
internalFrame.setSize(400, 300);
//Internal Frame zentrieren:
Dimension desktopSize = jep_EditorPane.getPreferredSize();
internalFrame.setLocation((desktopSize.width)/2-200, (desktopSize.height)/2);
System.out.println(desktopSize.width);
System.out.println(desktopSize.height);
jtp_HelpPane = new JTextPane();
JScrollPane jsp_ScrollPane = new JScrollPane(jtp_HelpPane);
internalFrame.add(BorderLayout.CENTER,jsp_ScrollPane);
desktop.add(internalFrame, 200);
internalFrame.setVisible(true);
}
private void showInfo() {
JInternalFrame internalFrame = new JInternalFrame("Info", false, true, false, false);
internalFrame.setSize(200,150);
//Internal Frame zentrieren:
Dimension desktopSize = contentPane.getPreferredSize();
internalFrame.setLocation((desktopSize.width)/2-100, (desktopSize.height)/2);
jtp_InfoPane = new JTextPane();
desktop.add(internalFrame, 300);
internalFrame.setVisible(true);
}
}