//{HEADER
/**
* This class is part of jnex 'Nexirius Application Framework for Java'
*
* Copyright (C) Nexirius GmbH, CH-4450 Sissach, Switzerland (www.nexirius.ch)
*
* <p>This library is free software; you can redistribute it and/or<br>
* modify it under the terms of the GNU Lesser General Public<br>
* License as published by the Free Software Foundation; either<br>
* version 2.1 of the License, or (at your option) any later version.</p>
*
* <p>This library is distributed in the hope that it will be useful,<br>
* but WITHOUT ANY WARRANTY; without even the implied warranty of<br>
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU<br>
* Lesser General Public License for more details.</p>
*
* <p>You should have received a copy of the GNU Lesser General Public<br>
* License along with this library; if not, write to the Free Software<br>
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA</p>
* </blockquote>
*
* <p>
* Nexirius GmbH, hereby disclaims all copyright interest in<br>
* the library jnex' 'Nexirius Application Framework for Java' written<br>
* by Marcel Baumann.</p>
*/
//}HEADER
package com.nexirius.ulc.application;
import com.nexirius.framework.datamodel.DataModelCommand;
import com.nexirius.framework.datamodel.DataModelCommandVector;
import com.nexirius.framework.menu.MenuCreator;
import com.nexirius.ulc.ulcviewer.ULCViewerCreator;
import com.nexirius.ulc.ulcviewer.ULCViewerFactory;
import com.nexirius.ulc.ulcviewer.editor.CommandULCEditor;
import com.nexirius.ulc.ulcviewer.editor.MenuItemULCEditor;
import com.nexirius.util.resource.Util;
import com.ulcjava.base.application.*;
import com.ulcjava.base.application.util.*;
import javax.swing.*;
import java.util.Stack;
public class ULCMenuCreator implements MenuCreator {
private ULCViewerFactory factory;
private ULCMenuBar menuBar;
private Stack actMenuStack;
private ULCToolBar toolBar;
private DataModelCommandVector commands;
public ULCMenuCreator(ULCViewerFactory factory, DataModelCommandVector commands) {
this.factory = factory;
this.commands = commands;
}
public ULCMenuBar getMenuBar() {
return menuBar;
}
public ULCToolBar getToolBar() {
return toolBar;
}
public void newMenuBar() {
menuBar = new ULCMenuBar();
actMenuStack = new Stack();
}
public void newToolBar() {
toolBar = new ULCToolBar();
}
public void addMenu(String menuName) {
ULCMenu menu = createULCMenu(menuName);
if (actMenuStack.isEmpty()) {
menuBar.add(menu);
} else {
ULCMenu actMenu = (ULCMenu)actMenuStack.peek();
actMenu.add(menu);
}
actMenuStack.push(menu);
}
private ULCMenu createULCMenu(String menuName) {
String menuText = factory.getClientResource().getButtonText(menuName);
if (menuText == null) {
menuText = menuName;
}
ULCMenu menu = new ULCMenu(menuText);
String toolTipText = factory.getClientResource().getToolTipText(menuName);
if (toolTipText != null) {
menu.setToolTipText(toolTipText);
}
int mnemo = factory.getClientResource().getMnemonic(menuName, ".menu");
if (mnemo != -1) {
menu.setMnemonic(menuText.charAt(mnemo));
}
menu.setIcon(factory.getUlcIcon(menuName));
return menu;
}
public void addMenuItem(String menuItem) {
if (actMenuStack != null) {
ULCMenu actMenu = (ULCMenu) actMenuStack.peek();
actMenu.add(createMenuItem(menuItem));
} else if (toolBar != null) {
toolBar.add(createToolBarButton(menuItem));
}
}
public void addSeparator() {
if (actMenuStack != null) {
ULCMenu actMenu = (ULCMenu) actMenuStack.peek();
//TODO actMenu.add(new ULCSeparator());
} else if (toolBar != null) {
//TODO toolBar.add(new ULCToolBar.Separator());
}
}
public void end() {
if (actMenuStack != null) {
if (!actMenuStack.isEmpty()) {
actMenuStack.pop();
} else {
actMenuStack = null;
}
}
}
private ULCMenuItem createMenuItem(String name) {
ULCMenuItem menuItem = null;
if (commands != null) {
for (DataModelCommand m = commands.firstItem(); m != null; m = commands.nextItem()) {
if (m.getCommandName().equals(name)) {
MenuItemULCEditor viewer = null;
try {
viewer = (MenuItemULCEditor) factory.createUlcViewer(new ULCViewerCreator(MenuItemULCEditor.class), m);
menuItem = (ULCMenuItem) viewer.getULCComponent(factory);
} catch (Exception e) {
e.printStackTrace(); //TODO
}
}
}
}
if (menuItem == null) {
menuItem = new ULCMenuItem(name);
}
return menuItem;
}
private ULCButton createToolBarButton(String text) {
ULCButton ret = null;
if (commands != null) {
for (DataModelCommand m = commands.firstItem(); m != null; m = commands.nextItem()) {
if (m.getCommandName().equals(text)) {
CommandULCEditor viewer = null;
try {
viewer = (CommandULCEditor) factory.createDefaultUlcEditor(m);
ret = (ULCButton) viewer.getULCComponent(factory);
} catch (Exception e) {
e.printStackTrace(); //TODO
}
break;
}
}
}
if (ret == null) {
ret = new ULCButton(text);
}
Icon icon = factory.getIcon(text);
if (icon != null) {
// ret.setIcon(icon);
// ret.setText("");
}
return ret;
}
}