package frontend.controller;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileFilter;
import java.lang.reflect.Constructor;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import frontend.dialogs.FileChooser;
import frontend.dialogs.Jif;
import frontend.panels.FrontendInterface;
/**
* @author Foo Inc
*
*/
@SuppressWarnings("serial")
public class Controller implements ControllerInterface {
public static FileChooser m_fileChooser = null;
public Jif m_jif = null;
public FrontendInterface[] m_panels = null;
public Controller(Jif _jif) {
m_jif = _jif;
m_fileChooser = new FileChooser();
}
public JMenuBar initMenuBar() {
// Note: "SEPERATOR" and "SEP" identifies a file menu seperator.
JMenuBar menuBar = new JMenuBar();
// Datei
String[] fileMenuItems = {"Neu",
"Oeffnen",
"Speichern",
"Speichern unter ...",
"SEPERATOR",
"Drucken",
"SEPERATOR",
"Beenden"};
String[] fileItemIDs = {"FILE_NEW",
"FILE_OPEN",
"FILE_SAVE",
"FILE_SAVE_AS",
"SEP",
"FILE_PRINT",
"SEP",
"FILE_CLOSEEE"};
menuBar.add(this.generateMenuItems("Datei", fileMenuItems, fileItemIDs));
// Bearbeiten
String[] editMenuItems = {"Ausschneiden",
"Kopieren",
"Einfuegen"};
String[] editItemIDs = {"EDIT_CUT",
"EDIT_COPY",
"EDIT_PASTE"};
menuBar.add(this.generateMenuItems("Bearbeiten", editMenuItems, editItemIDs));
// Anzeige
JMenu viewMenu = initViewMenu("Anzeige");
if((viewMenu != null) || (viewMenu.getSubElements().length != 0)) {
menuBar.add(viewMenu);
}
// Hilfe
String[] helpMenuItems = {"Ueber ..."};
String[] helpItemIDs = {"HELP_ABOUT"};
menuBar.add(this.generateMenuItems("Hilfe", helpMenuItems, helpItemIDs));
return(menuBar);
}
@SuppressWarnings("unchecked")
private JMenu initViewMenu(String _menuName) {
File pluginsPath = new File(System.getProperty("user.dir") + "/frontend/panels");
File[] pluginFiles = null;
if(pluginsPath.isDirectory()) {
FileFilter ff = new FileFilter() {
public boolean accept(File pathname) {
if(pathname.isFile() && pathname.getPath().endsWith("Plugin.class")) {
//System.out.println("IF " + pathname.getPath());
return(true);
} else {
//System.out.println("ELSE " + pathname.getPath());
return(false);
}
}
};
pluginFiles = pluginsPath.listFiles(ff);
}
if(pluginFiles == null) {
return(null);
}
//====================================================================
JMenu anyMenu = new JMenu(_menuName);
Vector pluginsVector = new Vector(10, 1);
String className = null;
Constructor[] tmpConstructor = null;
for(int i = 0; i < pluginFiles.length; i++) {
try {
className = this.getStringEndTillChar(File.separatorChar, pluginFiles[i].getPath());
className = "frontend.panels." + className.substring(0, className.length() - 6);
tmpConstructor = Class.forName(className).getConstructors();;
for(int y = 0; y < tmpConstructor.length; y++) {
if(tmpConstructor[y].toGenericString().endsWith(className + "(frontend.controller.ControllerInterface)")) {
pluginsVector.addElement(tmpConstructor[y].newInstance((ControllerInterface) this));
break;
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
if(!pluginsVector.isEmpty()) {
Vector panelsVector = new Vector(10, 1);
FrontendInterface tmpInterface = null;
for(int i = 0; i < pluginsVector.size(); i++) {
tmpInterface = (FrontendInterface) pluginsVector.elementAt(i);
if(tmpInterface.isPanel()) {
panelsVector.addElement(tmpInterface);
}
}
if(!panelsVector.isEmpty()) {
m_panels = new FrontendInterface[panelsVector.size()];
JMenuItem anyItem = null;
for(int i = 0; i < m_panels.length; i++) {
m_panels[i] = (FrontendInterface) panelsVector.elementAt(i);
anyItem = new JMenuItem(m_panels[i].getItemName());
final int index = i;
anyItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
m_panels[index].performActionOnMenuClick();
}
});
anyMenu.add(anyItem);
}
}
}
return(anyMenu);
}
private String getStringEndTillChar(char _ch, String _toEdit) {
char[] newString = null;
int stringLength = 0;
for(int i = _toEdit.length() - 1; i > -1; i--) {
if(_toEdit.charAt(i) == _ch) {
break;
} else {
stringLength += 1;
}
}
newString = new char[stringLength];
int newIndex = 0;
for(int i = _toEdit.length() - stringLength; i < _toEdit.length(); i++) {
newString[newIndex] = _toEdit.charAt(i);
newIndex += 1;
}
return(new String(newString));
}
public JMenu generateMenuItems(String _menuName, String[] _menuItemNames, String[] _itemIDs) {
JMenu anyMenu = new JMenu(_menuName);
for(int i = 0; i < _menuItemNames.length; i++) {
if(! _menuItemNames[i].endsWith("SEPERATOR")) {
JMenuItem anyItem = new JMenuItem(_menuItemNames[i]);
final String itemID = _itemIDs[i];
final JFrame parentFrame = m_jif;
anyItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
callMenuItemMethod(parentFrame, itemID);
}
});
anyMenu.add(anyItem);
} else {
anyMenu.addSeparator();
}
}
return(anyMenu);
}
private static void callMenuItemMethod(JFrame _parent, String _itemID) {
/*
* Datei
*/
if(_itemID.endsWith("FILE_NEW")) {
// TODO neues Blatt anlegen
showWorkingOnDialog(_parent);
return;
} else if(_itemID.endsWith("FILE_OPEN")) {
// TODO Oe�ffnen einer Datei...
m_fileChooser.openFiles();
return;
} else if(_itemID.endsWith("FILE_SAVE")) {
// TODO
// if open file has not been saved yet... FileChooser
// else overwrite old file without request.
showWorkingOnDialog(_parent);
return;
} else if(_itemID.endsWith("FILE_SAVE_AS")) {
// TODO save a file a specified with the FileChooser
m_fileChooser.saveFile(new BufferedImage(1, 1, BufferedImage.TYPE_BYTE_GRAY));
return;
} else if(_itemID.endsWith("FILE_PRINT")) {
// TODO print the activ worksheet (if not blank) else
// show dialog, "worksheet is blank" foo
showWorkingOnDialog(_parent);
return;
} else if(_itemID.endsWith("FILE_CLOSE")) {
System.exit(0);
} else
/*
* Bearbeiten
*/
if(_itemID.endsWith("EDIT_CUT")) {
// TODO
showWorkingOnDialog(_parent);
return;
} else if(_itemID.endsWith("EDIT_COPY")) {
// TODO
showWorkingOnDialog(_parent);
return;
} else if(_itemID.endsWith("EDIT_PASTE")) {
// TODO
showWorkingOnDialog(_parent);
return;
} else
/*
* Hilfe
*/
if(_itemID.endsWith("HELP_ABOUT")) {
JOptionPane.showMessageDialog((Component) _parent,
"Die Authoren:\n - skomp: Anwendungskern\n - MrUnknown: Design (GUI)", "Ueber ...", 1);
return;
}
JOptionPane.showMessageDialog((Component) _parent,
"Item ID konnte nicht zugeordnet werden.\nItem ID ist: \"" + _itemID + "\"", "DEBUG", 0);
}
private static void showWorkingOnDialog(JFrame _parent) {
JOptionPane.showMessageDialog((Component) _parent,
"Hier entsteht eine neue Methode.", "Baustelle ...", 1);
}
// Interface methods:
public BufferedImage getSelectedImage() {
// TODO
return(null);
}
public void addToTabbedPane(String _name, JPanel _panel) {
m_jif.m_tabbedPane.add(_name, _panel);
}
public void removeFromTabbedPane() {
}
// End of interface methods.
}