package cologne.eck.all_peas.gui;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ResourceBundle;
import javax.accessibility.AccessibleContext;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.plaf.FontUIResource;
import cologne.eck.all_peas.data.PeaProperties;
import cologne.eck.all_peas.files.FileComposer;
import cologne.eck.all_peas.files.FileTypePanel;
@SuppressWarnings("serial")
public class ViewMenu extends JMenu implements ActionListener {
private ResourceBundle languagesBundle;
FileTypePanel ftp;
public ViewMenu(FileTypePanel _ftp, boolean plainModus) {
ftp = _ftp;
languagesBundle = PeaProperties.getBundle();
this.setText(languagesBundle.getString("view"));
// int fontSize = PswDialogView.getFontsize();
// this.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, fontSize));
//help.setMnemonic(KeyEvent.VK_H);
//this.setMnemonic(KeyStroke.getKeyStroke(languagesBundle.getString("view_mnemonic")).getKeyCode());
JMenuItem treeViewItem = new JMenuItem(languagesBundle.getString("tree_view"));
treeViewItem.setActionCommand("treeView");
// treeViewItem.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, fontSize));
treeViewItem.setToolTipText(languagesBundle.getString("tooltip_tree_view"));
//instructionItem.setMnemonic(KeyEvent.VK_I);
//treeViewItem.setMnemonic(KeyStroke.getKeyStroke(languagesBundle.getString("instruction_mnemonic")).getKeyCode());
treeViewItem.addActionListener(this);
this.add(treeViewItem);
JMenuItem fullNameItem = new JMenuItem(languagesBundle.getString("full_name"));
fullNameItem.setActionCommand("fullName");
// fullNameItem.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, fontSize));
//webItem.setMnemonic(KeyEvent.VK_I);
//fullNameItem.setMnemonic(KeyStroke.getKeyStroke(languagesBundle.getString("web_mnemonic")).getKeyCode());
fullNameItem.addActionListener(this);
this.add(fullNameItem);
if (plainModus == false){
JMenuItem checkAgainItem = new JMenuItem(languagesBundle.getString("refresh"));
checkAgainItem.setActionCommand("refresh");
checkAgainItem.setToolTipText(languagesBundle.getString("tooltip_refresh"));
// checkAgainItem.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, fontSize));
checkAgainItem.addActionListener(this);
this.add(checkAgainItem);
}
JMenuItem cleanupItem = new JMenuItem(languagesBundle.getString("cleanup"));
cleanupItem.setActionCommand("cleanup");
// cleanupItem.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 12));
cleanupItem.setToolTipText(languagesBundle.getString("tooltip_cleanup"));
//cleanupItem.setMnemonic(KeyStroke.getKeyStroke(languagesBundle.getString("about_mnemonic")).getKeyCode());
cleanupItem.addActionListener(this);
this.add(cleanupItem);
JMenuItem increaseFontSizeItem = new JMenuItem(languagesBundle.getString("enlarge_font"));//languagesBundle.getString("cleanup"));
increaseFontSizeItem.setActionCommand("increaseFontSize");
// increaseFontSizeItem.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 12));
// increaseFontSizeItem.setToolTipText(languagesBundle.getString("enlarge_font"));
//cleanupItem.setMnemonic(KeyStroke.getKeyStroke(languagesBundle.getString("about_mnemonic")).getKeyCode());
increaseFontSizeItem.addActionListener(this);
this.add(increaseFontSizeItem);
JMenuItem decreaseFontSizeItem = new JMenuItem(languagesBundle.getString("reduce_font"));//languagesBundle.getString("cleanup"));
decreaseFontSizeItem.setActionCommand("decreaseFontSize");
// increaseFontSizeItem.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 12));
// increaseFontSizeItem.setToolTipText(languagesBundle.getString("tooltip_cleanup"));
//cleanupItem.setMnemonic(KeyStroke.getKeyStroke(languagesBundle.getString("about_mnemonic")).getKeyCode());
decreaseFontSizeItem.addActionListener(this);
this.add(decreaseFontSizeItem);
}
/**
* Call for every child of a container updateUI()
*
* @param c the container to update
*/
/* public static void updateAllComponents(Container c) {
Component[] comps = c.getComponents();
int len = comps.length;
// ArrayList<Component> compList = new ArrayList<Component>();
for (int i = 0;i < len; i++) {
//compList.add(comp);
if (comps[i] instanceof Container) {
((JComponent)comps[i]).updateUI();
System.out.println(comps[i].getClass());
updateAllComponents((Container)comps[i]);
//compList.addAll(updateAllComponents((Container) comp));
} else {
try {
((JComponent)comps[i]).updateUI();
System.out.println(comps[i].getClass());
} catch (Exception e) {
System.out.println(e);
}
}
}
}*/
@Override
public void actionPerformed(ActionEvent ae) {
String command = ae.getActionCommand();
// System.out.println(command);
FileComposer fc = ftp.getFileComposer();
if (command.equals("increaseFontSize")) {
// change font size
int oldFontSize = PswDialogView.getFontsize();
int newFontSize = oldFontSize;
if (oldFontSize < 18) {
newFontSize = oldFontSize + 2;
PswDialogView.setFontSize( newFontSize);
}
// set font size (UIManager)
FontUIResource f = new FontUIResource(Font.SANS_SERIF, Font.PLAIN, newFontSize);
java.util.Enumeration<Object> keys = UIManager.getDefaults().keys();
while (keys.hasMoreElements()) {
Object key = keys.nextElement();
Object value = UIManager.get(key);
if (value instanceof javax.swing.plaf.FontUIResource) {
UIManager.put(key, f);
}
}
// UIManager.put("OptionPane.messageFont", f);
// UIManager.put("OptionPane.buttonFont", f);
// resizes currently shown items, labels...
UIManager.getLookAndFeelDefaults()
.put("defaultFont", new Font(Font.SANS_SERIF, Font.PLAIN, newFontSize));
// update (current) view
SwingUtilities.updateComponentTreeUI(PswDialogView.getView());
// updateAllComponents(PswDialogView.getView().getContentPane());
PswDialogView.setUI(true);
PswDialogView.getView().validate();
PswDialogView.getView().invalidate();
PswDialogView.getView().repaint();
} else if (command.equals("decreaseFontSize")) {
// change font size
int oldFontSize = PswDialogView.getFontsize();
int newFontSize = oldFontSize;
if (oldFontSize > 10) {
newFontSize = oldFontSize - 2;
PswDialogView.setFontSize( newFontSize);
}
// set font size (UIManager)
FontUIResource f = new FontUIResource(Font.SANS_SERIF, Font.PLAIN, newFontSize);
java.util.Enumeration<Object> keys = UIManager.getDefaults().keys();
while (keys.hasMoreElements()) {
Object key = keys.nextElement();
Object value = UIManager.get(key);
if (value instanceof javax.swing.plaf.FontUIResource) {
UIManager.put(key, f);
}
}
// UIManager.put("OptionPane.messageFont", f);
// UIManager.put("OptionPane.buttonFont", f);
// resizes currently shown items, labels...
UIManager.getLookAndFeelDefaults()
.put("defaultFont", new Font(Font.SANS_SERIF, Font.PLAIN, newFontSize));
// update (current) view
SwingUtilities.updateComponentTreeUI(PswDialogView.getView());
// updateAllComponents(PswDialogView.getView().getContentPane());
PswDialogView.setUI(true);
PswDialogView.getView().validate();
PswDialogView.getView().invalidate();
PswDialogView.getView().repaint();
} else if (command.equals("treeView")){
String[] newTexts = fc.showFileHierarchy(ftp.getValidFileNames(false));
if (newTexts == null) {
return;
}
// set the new texts as check box texts
ftp.updateCheckBoxTexts(newTexts);
ftp.updateWindow();
} else if (command.equals("fullName")){
ftp.showFullFileNames();
ftp.updateWindow();
} else if (command.equals("refresh")){
/* if (fc.getPlainModus() == true){
// do not check wrong password or wrong salt files
x
} */
fc.updateFiles(true);
ftp.getFileComposer().getFileModel().calculateNumberAndSize();
ftp.updateNumberAndSize();
ftp.updateWindow();
} else if (command.equals("cleanup")) {
if (ftp.getFileComposer().getPlainModus() == true) {
String[] unselectedFiles = ftp.getUnselectedValidFileNames();
if (unselectedFiles != null) {
StringBuilder unselectedFilesBuilder = new StringBuilder();
for (int i = 0; i < unselectedFiles.length; i++) {
unselectedFilesBuilder.append(unselectedFiles[i]);
unselectedFilesBuilder.append("\n");
}
String unselectedFilesString = new String(unselectedFilesBuilder);
// display a warning and an option to break
int result = JOptionPane.showConfirmDialog(ftp,
PeaProperties.getBundle().getString("leave_files_unencrypted")
+ "\n" + unselectedFilesString,
PeaProperties.getBundle().getString("warning"),
JOptionPane.WARNING_MESSAGE,
JOptionPane.YES_NO_OPTION);
if (result != 0) { //yes = 0,
return;
}
}
}
ftp.cleanupView();
ftp.getFileComposer().getFileModel().cleanupMap();
ftp.getFileComposer().getFileModel().calculateNumberAndSize();
ftp.updateNumberAndSize();
ftp.updateWindow();
} else {
System.out.println("Invalid command in help menu");
}
}
public AccessibleContext getAccessibleContext() {
if (accessibleContext == null) {
accessibleContext = new AccessibleViewMenu();
}
return accessibleContext;
}
protected class AccessibleViewMenu extends AccessibleJMenu {
//Inherit everything, override nothing.
}
}