package muscodgui;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JSplitPane;
import javax.swing.JTree;
import javax.swing.JTabbedPane;
import javax.swing.JScrollPane;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.event.TreeSelectionListener;
import javax.swing.event.TreeSelectionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.Event;
import java.awt.Toolkit;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Font;
import java.io.File;
import java.io.FileReader;
import java.io.FileNotFoundException;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.ImageIcon;
import javax.swing.JTextArea;
import java.util.Properties;
public class MainFrame extends JFrame implements ActionListener, TreeSelectionListener {
private MenuBar menuBar;
private JTree tree;
private JSplitPane splitPane;
private Container content = getContentPane();
private JTabbedPane tabPane = new JTabbedPane();
private Chooser chooser;
private String file = null;
private String path = null;
private DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer();
private FileHandler cFile;
private FileHandler dFile;
private FileHandler tFile;
private JTextArea cArea = new JTextArea();
private JTextArea dArea = new JTextArea();
private JTextArea tArea = new JTextArea();
public MainFrame() {
super("MuscodGui");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addWindowListener(new WindowAdapter() {
public void WindowClosing(WindowEvent e) {
System.exit(0);
}
});
/* add menu */
menuBar = new MenuBar(this);
setJMenuBar(menuBar);
/* add tree */
DefaultMutableTreeNode root = new DefaultMutableTreeNode("Modules");
DefaultMutableTreeNode source = new DefaultMutableTreeNode("Source code");
DefaultMutableTreeNode dat = new DefaultMutableTreeNode("Dat file");
DefaultMutableTreeNode text = new DefaultMutableTreeNode("Text file");
root.add(source); root.add(dat); root.add(text);
ImageIcon leafIcon = new ImageIcon("muscodgui/module.gif");
renderer.setLeafIcon(leafIcon);
renderer.setOpenIcon(null);
renderer.setClosedIcon(null);
renderer.setBackgroundSelectionColor(null);
renderer.setBorderSelectionColor(null);
tree = new JTree(root);
tree.setCellRenderer(renderer);
tree.setShowsRootHandles(true);
tree.addTreeSelectionListener(this);
tree.expandRow(1);
content.add(new JScrollPane(tree), BorderLayout.CENTER);
/* add splitpane */
splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
tree, tabPane);
splitPane.setDividerSize(6);
splitPane.setResizeWeight(0);
content.add(splitPane, BorderLayout.CENTER);
/* add tabs */
cArea.setTabSize(8);
cArea.setLineWrap(true);
cArea.setWrapStyleWord(true);
cArea.setFont(new Font("Courier", Font.PLAIN, 12));
dArea.setTabSize(8);
tArea.setTabSize(8);
tabPane.addTab("Source code", cArea);
tabPane.addTab("Dat file", dArea);
tabPane.addTab("Text file", tArea);
setLocation(
(Toolkit.getDefaultToolkit().getScreenSize().width - 800)/2,
(Toolkit.getDefaultToolkit().getScreenSize().height - 600)/2
);
setSize(800, 600);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
Object obj = e.getSource();
String cmd = e.getActionCommand();
if (obj instanceof JMenuItem && cmd != null) {
System.out.println("Menu: " + cmd);
if (cmd.equals("new")) {
/* create file */
}
if (cmd.equals("open")) {
chooser = new Chooser(cmd);
int returnVal = chooser.showOpenDialog(this);
if (returnVal == Chooser.APPROVE_OPTION) {
File rfile = chooser.getSelectedFile();
file = rfile.getName();
path = rfile.getParent();
open();
}
}
if (cmd.equals("save")) {
/* save file */
}
if (cmd.equals("saveAs")) {
chooser = new Chooser(cmd);
int returnVal = chooser.showSaveDialog(this);
if (returnVal == Chooser.APPROVE_OPTION) {
File rfile = chooser.getSelectedFile();
file = rfile.getName();
path = rfile.getParent();
/* save file */
}
}
if (cmd.equals("exit")) {
System.exit(0);
}
}
}
public void valueChanged(TreeSelectionEvent e) {
Object obj = e.getSource();
String cmd = null;
try { /* can throw a NullPointerException, but it doesn't matter */
cmd = tree.getLastSelectedPathComponent().toString();
} catch(Exception ev) {}
if (obj instanceof JTree && cmd != null) {
if (!cmd.equals("Modules")) {
System.out.println("Tree: " + cmd);
}
if (cmd.equals("Source code")) {
tabPane.setSelectedIndex(0);
}
if (cmd.equals("Dat file")) {
tabPane.setSelectedIndex(1);
}
if (cmd.equals("Text file")) {
tabPane.setSelectedIndex(2);
}
}
}
private void open() {
if (file != null && path != null) {
int point = file.lastIndexOf('.');
String fileOnlyName = file.substring(0, point);
String sep = System.getProperty("file.separator");
if (!path.endsWith(sep)) {
path = path.concat(sep);
}
try {
cFile = new FileHandler(new FileReader(path + "src" + sep + fileOnlyName + ".c"));
} catch (Exception e) {
cFile = null;
System.out.println(e);
}
try {
dFile = new FileHandler(new FileReader(path + "dat" + sep + fileOnlyName + ".dat"));
} catch (Exception e2) {
dFile = null;
System.out.println(e2);
}
try {
tFile = new FileHandler(new FileReader(path + "txt" + sep + fileOnlyName + ".txt"));
} catch (Exception e3) {
tFile = null;
System.out.println(e3);
}
try {
tabPane.removeTabAt(0);
tabPane.removeTabAt(0);
tabPane.removeTabAt(0);
} catch (Exception e4) {}
try {
cArea = new JTextArea();
cArea.append(cFile.getFile());
} catch (Exception e5) {
System.out.println(e5);
}
try {
dArea = new JTextArea();
dArea.append(dFile.getFile());
} catch (Exception e6) {
System.out.println(e6);
}
try {
tArea = new JTextArea();
tArea.append(tFile.getFile());
} catch (Exception e7) {
System.out.println(e7);
}
tabPane.addTab(fileOnlyName + ".c", cArea);
tabPane.addTab(fileOnlyName + ".dat", dArea);
tabPane.addTab(fileOnlyName + ".txt", tArea);
}
}
}