/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.fornsys.jvixen.sys;
import java.util.ArrayList;
import java.util.List;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Control;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
/**
*
* @author josh
*/
public class AppCommand {
private List<AppCommand> _items = new ArrayList<>();
public final static char PathDelimiter = '\\';
private MenuItem _toolStripItem;
private Object _rootControl;
private String _name;
private String _text;
private Image _image;
private boolean _enabled;
private boolean _visible;
private AppCommand.AppCommandStyle _style;
private int _mergeIndex;
private AppCommand _parent;
public MenuItem getItem()
{
return _toolStripItem;
}
public String getName()
{
return this._name;
}
public void setName(String name)
{
this._name = name;
this._toolStripItem.setId(name);
}
public String getText()
{
return this._text;
}
public void setText(String text)
{
this._text = text;
this._toolStripItem.setText(text);
}
public Image getImage()
{
return this._image;
}
public void setImage(Image image)
{
this._image = image;
final ImageView imgView = new ImageView();
this._toolStripItem.setGraphic(imgView);
imgView.setImage(image);
}
public boolean getEnabled()
{
return this._enabled;
}
public void setEnabled(boolean enabled)
{
this._enabled = enabled;
this._toolStripItem.setDisable(!enabled);
}
public boolean getVisible()
{
return this._visible;
}
public void setVisible(boolean visible)
{
this._visible = visible;
this._toolStripItem.setVisible(visible);
}
public AppCommand[] getItems()
{
return (AppCommand[])_items.toArray();
}
public AppCommand.AppCommandStyle getStyle()
{
return this._style;
}
public void setStyle(AppCommand.AppCommandStyle style)
{
if (_style == style)
return;
if (_toolStripItem != null)
_toolStripItem.setOnAction(null);
_style = style;
switch(_style)
{
case Context:
_toolStripItem = new MenuItem();
break;
case Menu:
_toolStripItem = new MenuItem();
break;
case Separator:
_toolStripItem = new MenuItem();
break;
}
setEnabled(_enabled);
setImage(_image);
// setMergeIndex(_mergeIndex);
// _toolStripItem.MergeAction = MergeAction.Insert;
AppCommand[] items = (AppCommand[])_items.toArray();
_items.clear();
for(AppCommand appCommand : items)
{
Add(appCommand);
}
setName(_name);
setText(_text);
setVisible(_visible);
_toolStripItem.setOnAction(new ItemClick());
}
/*
public int getMergeIndex()
{
return this._mergeIndex;
}
public void setMergeIndex(int index)
{
_mergeIndex = index;
_toolStripItem.MergeIndex = index;
}
*/
public AppCommand getParent()
{
return _parent;
}
public EventHandler<ActionEvent> _click;
public AppCommand(Scene rootControl)
{
setStyle(AppCommand.AppCommandStyle.Menu);
setVisible(true);
setEnabled(true);
this._rootControl = rootControl;
for(AppCommand appCommand : _items)
this._AddToRoot(appCommand);
}
public AppCommand()
{
this((Scene)null);
}
public AppCommand(String name)
{
this();
setName(name);
}
public AppCommand(String name, String text)
{
if(text != "-")
{
setText(text);
}
else
{
setStyle( AppCommand.AppCommandStyle.Separator );
setName((String) null);
}
}
public AppCommand(String name, String text, Image image)
{
this(name, text);
setImage(image);
}
private void _AddToRoot(AppCommand appCommand)
{
if (this._rootControl == null)
return;
switch( appCommand.getStyle() )
{
case Menu:
if(_rootControl instanceof Scene)
{
for(Node n : ((Scene)_rootControl).getRoot().getChildrenUnmodifiable())
{
if( n instanceof MenuBar )
{
((MenuBar)n).getMenus().add((Menu)appCommand.getItem());
}
}
}
else if (_rootControl instanceof MenuBar)
{
((MenuBar)_rootControl).getMenus().add((Menu)appCommand.getItem());
break;
}
else
break;
}
}
private void _RemoveFromRoot(AppCommand appCommand)
{
if (this._rootControl == null)
return;
switch(appCommand.getStyle())
{
case Menu:
if( _rootControl instanceof Scene )
{
for(Node n : ((Scene)_rootControl).getRoot().getChildrenUnmodifiable())
{
if( n instanceof MenuBar )
{
((MenuBar)n).getMenus().remove((Menu)appCommand.getItem());
}
}
}
else if (_rootControl instanceof MenuBar)
{
((MenuBar)_rootControl).getMenus().remove((Menu)appCommand.getItem());
break;
}
else
break;
}
}
public void Add(AppCommand appCommand)
{
appCommand._parent = this;
this._items.add(appCommand);
((Menu)_toolStripItem).getItems().add((MenuItem)appCommand.getItem());
if (this._rootControl == null)
return;
this._AddToRoot(appCommand);
}
public void Remove(String appCommandName)
{
for(AppCommand command : _items)
{
if( command.getName().equals(appCommandName) )
{
command._parent = null;
_items.remove(command);
((Menu)_toolStripItem).getItems().remove(command.getItem());
if (this._rootControl != null)
this._RemoveFromRoot(command);
}
}
return;
}
protected class ItemClick implements EventHandler<ActionEvent>
{
@Override
public void handle(ActionEvent t) {
if( _click == null ) return;
_click.handle(t);
}
}
public AppCommand find(String relativePath)
{
if (relativePath == null || relativePath.isEmpty() )
return this;
int length = relativePath.indexOf('\\');
String relativePath1;
String childName;
if (length != -1)
{
childName = relativePath.substring(0, length);
relativePath1 = relativePath.substring(length + 1);
}
else
{
childName = relativePath;
relativePath1 = "";
}
for(AppCommand command : _items)
{
if( command.getName().equals(childName) )
return command.find(relativePath1);
}
return (AppCommand) null;
}
public enum AppCommandStyle
{
Context,
Menu,
Separator,
}
}