package gui;
import gui.options.JMOptionsDialog;
import java.awt.Desktop;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JMenuBar;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileNameExtensionFilter;
public class JMMenuBar extends JMenuBar implements ActionListener {
File lastFile = new File("");
int fileCharLength = 60;
String[] fileKeyShortcuts = { "", "", "Ctrl+S", "Ctrl+Shift+A", "Ctrl+W" };
int numRecentFiles = 10;
JMMenuItem[] recentItem = new JMMenuItem[numRecentFiles];
JMFrame parent;
String[] fileNames = new String[10];
File[] files = new File[10];
public JMMenuBar(JMFrame parent) {
this.parent = parent;
initRecentFiles();
}
public void initRecentFiles() {
File fileNamesFile = new File("recentFiles.txt");
try {
FileReader reader = new FileReader(fileNamesFile);
BufferedReader br = new BufferedReader(reader);
for (int i = 0; i < numRecentFiles; i++) {
try {
String file = br.readLine();
if (file == null)
file = "";
files[i] = new File(file);
fileNames[i] = files[i].getName();
} catch (IOException e) {
fileNames[i] = "";
files[i] = new File("");
}
}
try {
br.close();
reader.close();
} catch (IOException i) {
}
} catch (FileNotFoundException e) {
for (int i = 0; i < 10; i++) {
fileNames[i] = "";
files[i] = new File("");
}
}
}
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("load image"))
loadImage();
else if (e.getActionCommand().length() > 6
&& e.getActionCommand().substring(0, 6).equals("recent"))
openRecent(Integer.parseInt(e.getActionCommand().substring(6)));
else if (e.getActionCommand().equals("save"))
saveMap();
else if (e.getActionCommand().equals("save as"))
saveMap();
else if (e.getActionCommand().length() > 6
&& e.getActionCommand().substring(0, 6).equals("delete")) {
this.parent.imagePanel.delete();
} else if (e.getActionCommand().equals("close"))
System.exit(0);
else if (e.getActionCommand().equals("home page")) {
try {
Desktop.getDesktop().browse(
new URL("http://jmapper.sourceforge.net/").toURI());
} catch (MalformedURLException e1) {
} catch (IOException e1) {
} catch (URISyntaxException e1) {
}
} else if (e.getActionCommand().equals("jmapper manual")) {
try {
Desktop.getDesktop().browse(
new URL("http://jmapper.sourceforge.net/manual/").toURI());
} catch (MalformedURLException e1) {
} catch (IOException e1) {
} catch (URISyntaxException e1) {
}
} else if (e.getActionCommand().equals("preferences...")) {
new JMOptionsDialog(this.parent);
}
}
public void loadImage() {
JFileChooser choose = new JFileChooser(lastFile);
FileNameExtensionFilter allImages = new FileNameExtensionFilter(
"All Supported Image Types", "jpg", "gif", "png");
FileNameExtensionFilter JPGImages = new FileNameExtensionFilter(
"JPEG images (jpg)", "jpg");
choose.setFileFilter(JPGImages);
choose.setFileFilter(allImages);
if (choose.showOpenDialog(this.getParent()) == JFileChooser.APPROVE_OPTION) {
File selected = choose.getSelectedFile();
refreshFile(selected);
}
}
public void refreshFile(File selected) {
parent.setImage(new ImageIcon(selected.toString()));
refreshRecentFiles(selected.toString());
}
public void refreshRecentFiles(String name) {
try {
FileReader reader = new FileReader("recentFiles.txt");
BufferedReader br = new BufferedReader(reader);
String[] files = new String[numRecentFiles];
for (int i = 0; i < numRecentFiles; i++) {
try {
files[i] = br.readLine();
} catch (IOException e) {
files[i] = "";
}
}
try {
br.close();
} catch (IOException e) {
}
PrintWriter print = new PrintWriter("recentFiles.txt");
print.println(name);
int subtract = 0;
for (int i = 0; i < numRecentFiles - subtract; i++) {
if(files[i] !=null && !files[i].toString().equals(name)){
print.println((files[i] == null ? "" : files[i].toString()));
subtract = 1;
}else if(files[i] == null){
print.println((files[i] == null ? "" : files[i].toString()));
}
}
print.close();
} catch (FileNotFoundException e) {
}
initRecentFiles();
for (int i = 0; i < numRecentFiles; i++) {
recentItem[i].setText(fileNames[i]);
}
}
public boolean match(String[] names, String name) {
for (int i = 0; i < names.length; i++) {
if (names[i] != null && names[i].equals(name))
return true;
}
return false;
}
public void openRecent(int fileIndex) {
refreshFile(files[fileIndex]);
}
public JMMenuBar create() {
JMMenuBar menuBar = new JMMenuBar(parent);
menuBar.add(file());
menuBar.add(edit());
menuBar.add(help());
return menuBar;
}
private JMMenu help() {
JMMenu help = new JMMenu("Help");
JMMenuItem home = new JMMenuItem("Home Page");
home.setToolTipText("http://jmapper.sourceforge.net/");
home.addActionListener(this);
help.add(home);
JMMenuItem manual = new JMMenuItem("JMapper Manual");
manual
.setToolTipText("http://jmapper.sourceforge.net/manual/");
manual.addActionListener(this);
help.add(manual);
return help;
}
private JMMenu edit() {
JMMenu editMenu = new JMMenu("Edit");
JMMenuItem delete = new JMMenuItem("Delete Ctrl+X");
delete.addActionListener(this);
editMenu.add(delete);
JMMenuItem preferences = new JMMenuItem("Preferences...");
preferences.addActionListener(this);
editMenu.add(preferences);
return editMenu;
}
private JMMenu file() {
JMMenu fileMenu = new JMMenu("File");
JMMenuItem open = new JMMenuItem("Load Image");
open.addActionListener(this);
fileMenu.add(open);
fileMenu.add(openRecentMenu());
fileMenu.addSeparator();
JMMenuItem save = new JMMenuItem("Save");
for (int i = 0; i < fileCharLength
- (save.getText().length() + fileKeyShortcuts[2].length()); i++)
save.setText(save.getText() + " ");
save.setText(save.getText() + " ");
save.setText(save.getText() + fileKeyShortcuts[2]);
save.addActionListener(this);
fileMenu.add(save);
JMMenuItem saveAs = new JMMenuItem("Save As");
for (int i = 0; i < fileCharLength
- (saveAs.getText().length() + fileKeyShortcuts[3].length()); i++)
saveAs.setText(saveAs.getText() + " ");
saveAs.setText(saveAs.getText() + fileKeyShortcuts[3]);
saveAs.addActionListener(this);
saveAs.setActionCommand("save as");
fileMenu.add(saveAs);
fileMenu.addSeparator();
JMMenuItem close = new JMMenuItem("Close");
for (int i = 0; i < fileCharLength
- (close.getText().length() + fileKeyShortcuts[4].length()); i++)
close.setText(close.getText() + " ");
close.setText(close.getText() + fileKeyShortcuts[4]);
close.addActionListener(this);
fileMenu.add(close);
return fileMenu;
}
public JMMenu openRecentMenu() {
JMMenu openRecent = new JMMenu("Load Recent Image");
for (int i = 0; i < 10; i++) {
recentItem[i] = new JMMenuItem(fileNames[i]);
recentItem[i].setActionCommand("recent" + i);
recentItem[i].addActionListener(this);
openRecent.add(recentItem[i]);
}
return openRecent;
}
private void saveMap() {
JOptionPane.showMessageDialog(this.parent,
"Feature not implemented yet!");
// boolean hasAlpha = true;
// BufferedImage bimage = null;
// GraphicsEnvironment ge = GraphicsEnvironment
// .getLocalGraphicsEnvironment();
// try {
//
// int transparency = Transparency.OPAQUE;
// if (hasAlpha) {
// transparency = Transparency.BITMASK;
// }
//
// // Create the buffered image
// GraphicsDevice gs = ge.getDefaultScreenDevice();
// GraphicsConfiguration gc = gs.getDefaultConfiguration();
// bimage = gc.createCompatibleImage(this.parent.imageIcon.getImage()
// .getWidth(null), this.parent.imageIcon.getImage()
// .getHeight(null), transparency);
// } catch (HeadlessException e) {
// // The system does not have a screen
// }
// if (bimage == null) {
// // Create a buffered image using the default color model
// int type = BufferedImage.TYPE_INT_RGB;
// if (hasAlpha) {
// type = BufferedImage.TYPE_INT_ARGB;
// }
// bimage = new BufferedImage(this.parent.imageIcon.getImage()
// .getWidth(null), this.parent.imageIcon.getImage()
// .getHeight(null), type);
// }
//
// // Copy image to buffered image
// Graphics g = bimage.createGraphics();
// // Paint the image onto the buffered image
// g.drawImage(this.parent.imageIcon.getImage(), 0, 0, null);
// g.dispose();
// JFileChooser choose = new JFileChooser();
// choose.setMultiSelectionEnabled(false);
//
// if(choose.showSaveDialog(this.parent) ==
// JFileChooser.APPROVE_OPTION){
// File f = new File("");
// f = choose.getSelectedFile();
// try {
// f.mkdir();
// File f2 = new File("C:" + File.separatorChar
// + "Documents and Settings" + File.separatorChar
// + "anderswc" + File.separatorChar + "Desktop"
// + File.separatorChar + "test.jmp" + File.separatorChar
// + "test.xml");
// f2.createNewFile();
// } catch (IOException e1) {
// e1.printStackTrace();
// }
// }
//
// try {
// ImageIO.write(bimage, "gif", new File("C:" + File.separatorChar
// + "Documents and Settings" + File.separatorChar
// + "anderswc" + File.separatorChar + "Desktop"
// + File.separatorChar + "test.jmp" + File.separatorChar
// + "test.gif"));
// } catch (IOException e) {
// System.out.println("Error: Image Could Not Be Written");
// }
}
}