}
});
jm_file.add(jmi_open_archive);
final JMenu jm_open_recent = new JMenu("Open recent");
jm_open_recent.addMenuListener(new MenuListener() {
@Override
public void menuSelected(MenuEvent me) {
List<File> recentFiles = recentFilesHelper.getRecentFiles();
jm_open_recent.removeAll();
for(final File f: recentFiles) {
JMenuItem jmi = new JMenuItem(f.getName());
jmi.setToolTipText(f.getAbsolutePath());
jmi.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
FileOpenUtil.open(view, f);
}
});
jm_open_recent.add(jmi);
}
// Add clear option:
if(!recentFilesHelper.isEmpty()) {
jm_open_recent.addSeparator();
JMenuItem jmi = new JMenuItem("Clear");
jmi.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
recentFilesHelper.clear();
}
});
jm_open_recent.add(jmi);
}
}
@Override
public void menuDeselected(MenuEvent me) {
// do nothing
}
@Override
public void menuCanceled(MenuEvent me) {
// do nothing
}
});
jm_file.add(jm_open_recent);
jm_file.addSeparator();
JMenuItem jmi_save_req = new JMenuItem("Save Request", RCFileView.REQUEST_ICON);
jmi_save_req.setMnemonic(KeyEvent.VK_Q);
jmi_save_req.setAccelerator(KeyStroke.getKeyStroke(
KeyEvent.VK_S, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
jmi_save_req.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
actionSave(FileChooserType.SAVE_REQUEST);
}
});
jm_file.add(jmi_save_req);
JMenuItem jmi_save_res = new JMenuItem("Save Response", RCFileView.RESPONSE_ICON);
jmi_save_res.setMnemonic(KeyEvent.VK_S);
jmi_save_res.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
actionSave(FileChooserType.SAVE_RESPONSE);
}
});
jm_file.add(jmi_save_res);
JMenuItem jmi_save_res_body = new JMenuItem("Save Response Body", RCFileView.FILE_ICON);
// jmi_save_res_body.setMnemonic(' ');
jmi_save_res_body.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
actionSave(FileChooserType.SAVE_RESPONSE_BODY);
}
});
jm_file.add(jmi_save_res_body);
JMenuItem jmi_save_archive = new JMenuItem("Save Req-Res Archive", RCFileView.ARCHIVE_ICON);
jmi_save_archive.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
actionSave(FileChooserType.SAVE_ARCHIVE);
}
});
jm_file.add(jmi_save_archive);
if(!application.isMac()) { // Shown only for non-Mac platform!
jm_file.addSeparator();
JMenuItem jmi_exit = new JMenuItem("Exit", UIUtil.getIconFromClasspath(RCFileView.iconBasePath + "fv_exit.png"));
jmi_exit.setMnemonic(KeyEvent.VK_X);
jmi_exit.setAccelerator(KeyStroke.getKeyStroke(
KeyEvent.VK_Q, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
jmi_exit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
shutdownCall();
}
});
jm_file.add(jmi_exit);
}
// Edit menu
JMenu jm_edit = new JMenu("Edit");
jm_edit.setMnemonic(KeyEvent.VK_E);
JMenuItem jmi_clear_res = new JMenuItem("Clear Response");
jmi_clear_res.setMnemonic(KeyEvent.VK_C);
jmi_clear_res.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
view.clearUIResponse();
}
});
jm_edit.add(jmi_clear_res);
JMenuItem jmi_reset_all = new JMenuItem("Reset All");
jmi_reset_all.setMnemonic('a');
jmi_reset_all.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
view.clearUIResponse();
view.clearUIRequest();
}
});
jm_edit.add(jmi_reset_all);
jm_edit.addSeparator();
JMenuItem jmi_reset_to_last = new JMenuItem("Reset to Last Request-Response");
jmi_reset_to_last.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(view.getLastRequest() != null && view.getLastResponse() != null){
view.setUIToLastRequestResponse();
}
else{
JOptionPane.showMessageDialog(frame,
"No Last Request-Response Available",
"No Last Request-Response Available",
JOptionPane.INFORMATION_MESSAGE);
}
}
});
jm_edit.add(jmi_reset_to_last);
// History Menu
JMenu jm_history = new JMenu("History");
final JMenuItem jmi_back = new JMenuItem("Back");
jmi_back.setAccelerator(KeyStroke.getKeyStroke(
KeyEvent.VK_LEFT, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
jmi_back.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// Verify if most recent history needs to be shown:
if(historyManager.isMostRecent()) {
try {
Request reqFromUi = view.getRequestFromUI();
if(!reqFromUi.equals(historyManager.current())) {
view.setUIFromRequest(historyManager.current());
return;
}
}
catch(IllegalStateException ex) {
if(historyManager.current() != null) {
view.setUIFromRequest(historyManager.current());
return;
}
}
}
// Normal logic, cursor-1 history:
if(!historyManager.isOldest()) {
Request request = historyManager.back();
if(request != null) {
view.setUIFromRequest(request);
}
}
else {
view.setStatusMessage("Already in oldest");
}
}
});
jm_history.add(jmi_back);
final JMenuItem jmi_forward = new JMenuItem("Forward");
jmi_forward.setAccelerator(KeyStroke.getKeyStroke(
KeyEvent.VK_RIGHT, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
jmi_forward.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(!historyManager.isMostRecent()) {
Request request = historyManager.forward();
if(request != null) {
view.setUIFromRequest(request);
}
}
else {
view.setStatusMessage("Already in latest");
}
}
});
jm_history.add(jmi_forward);
jm_history.addSeparator();
JMenuItem jmi_clear_history = new JMenuItem("Clear History");
jmi_clear_history.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
historyManager.clear();
view.setStatusMessage("History cleared");
}
});
jm_history.add(jmi_clear_history);
jm_history.addSeparator();
JMenuItem jmi_save_history = new JMenuItem("Save History");
jmi_save_history.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(historyManager.isEmpty()) {
JOptionPane.showMessageDialog(frame, "History is empty!");
return;
}
final File f = FileType.getWithExtension(
getSaveFile(FileChooserType.SAVE_HISTORY),
FileType.HISTORY);
if(f != null) {
try {
historyManager.save(f);
view.setStatusMessage("Saved history file: " + f.getName());
}
catch(IOException ex) {
view.showError(Util.getStackTrace(ex));
}
}
}
});
jm_history.add(jmi_save_history);
JMenuItem jmi_load_history = new JMenuItem("Load History");
jmi_load_history.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(!historyManager.isEmpty()) {
final int confirm = JOptionPane.showConfirmDialog(null,
"Overwrite existing history?",
"Existing history will be overwritten. Proceed?", JOptionPane.YES_NO_OPTION);
if(confirm == JOptionPane.NO_OPTION || confirm == JOptionPane.CLOSED_OPTION) {
return;
}
}
final File f = getOpenFile(FileChooserType.OPEN_HISTORY);
if(f != null) {
try {
historyManager.clear();
historyManager.load(f);
}
catch(IOException ex) {
view.showError(ex);
}
catch(XMLException ex) {
view.showError(ex);
}
}
}
});
jm_history.add(jmi_load_history);
// Enable disable history controls based on cursor positions:
jm_history.addMenuListener(new MenuListener() {
@Override
public void menuSelected(MenuEvent e) {
if(historyManager.isOldest()) {
jmi_back.setEnabled(false);