Hello ,
I am trying to create a project doing the following
1. First of all i am creating a menu dynamically - My menu consists of Menus
and Submenus
2. My goal is to catch an event when my new menu is clicked and then to add
code so as to print the menu Name in the document
BUT MY PROBLEM IS THAT IT NEVER GOES IN THE EVENTLISTENER!!! :-(
Here is my code
/////////////////////////////////////////////////////////
Code:
public class CreateMenu {
private ArrayList<String> iuo_arraymenu;
private String[][] is_menus;
private XComponent xCurrentComponent;
private XMultiComponentFactory mxRemoteServiceManager = null;
private XComponentContext mxRemoteContext = null;
private XLayoutManager xLayoutManager =null;
private XModuleUIConfigurationManagerSupplier xCmSupplier = null;
private XUIConfigurationManager uiConfigManager = null;
private XIndexContainer oMenuBarSettings = null;
private XUIElementSettings xoMenuBarSettings = null;
private XIndexContainer[] oContainer = null;
private PropertyValue[] oMenuOTS= null;
private XIndexAccess settings;
String sMenuBar = "private:resource/menubar/menubar";
public CreateMenu(ArrayList<String> iuo_arraymenu,
XComponent xCurrentComponent ,
XMultiComponentFactory mxRemoteServiceManager,
XComponentContext mxRemoteContext)
throws Exception {
this.iuo_arraymenu = iuo_arraymenu;
this.xCurrentComponent = xCurrentComponent;
this.mxRemoteServiceManager = mxRemoteServiceManager;
this.mxRemoteContext = mxRemoteContext;
this.ConvertArrayListToArray();
this.initialize();
this.createMenuMain();
this.getTopMenu();
oMenuMain[2].Name = "ItemDescriptorContainer";
oMenuMain[2].Value = oContainer[0];
oMenuBarSettings.insertByIndex( oMenuBarSettings.getCount(),
oMenuMain);
xoMenuBarSettings.setSettings(oMenuBarSettings);
}
public void initialize() throws
WrappedTargetException,
UnknownPropertyException,
Exception{
XModel xModel = (XModel) UnoRuntime.queryInterface(XModel.class,
xCurrentComponent);
XController xController = xModel.getCurrentController();
XFrame xFrame = xController.getFrame();
xFrame.addEventListener(new com.sun.star.document.XEventListener() {
public void notifyEvent(com.sun.star.document.EventObject arg0)
{
System.out.print("IT NEVER GETS HERE");
}
public void disposing(EventObject arg0) {
System.out.print("IT NEVER GETS HERE");
}
});
XPropertySet xps = (XPropertySet) UnoRuntime.queryInterface(
XPropertySet.class, xFrame);
xLayoutManager = (XLayoutManager) UnoRuntime.queryInterface(
XLayoutManager.class,
xps.getPropertyValue("LayoutManager"));
xCmSupplier = (XModuleUIConfigurationManagerSupplier) UnoRuntime.
queryInterface(XModuleUIConfigurationManagerSupplier.class,
mxRemoteServiceManager.createInstanceWithContext("com.sun.star.ui.ModuleUIConfigurationManagerSupplier",mxRemoteContext));
uiConfigManager =
xCmSupplier.getUIConfigurationManager("com.sun.star.text.TextDocument");
oContainer = new XIndexContainer[this.getMenuContextSum()];
oContainer[0] = uiConfigManager.createSettings();
}
public int getMenuContextSum(){
int ll_sum = 0;
for (int i=0;i<is_menus.length;i++ ){
if (is_menus[i][3].equals("0")) {
ll_sum= ll_sum + 1;
}
}
return ll_sum + 1;
}
public void createMenuMAIN() throws
NoSuchElementException,
IllegalArgumentException,
UnknownPropertyException,
PropertyVetoException,
WrappedTargetException,
IndexOutOfBoundsException{
settings = uiConfigManager.getSettings(sMenuBar, true);
// 2. init menu elements
XUIElement oMenuBar = xLayoutManager.getElement(sMenuBar);
XPropertySet props = (XPropertySet) UnoRuntime.queryInterface(
XPropertySet.class, oMenuBar);
props.setPropertyValue("Persistent", new Boolean(true));
xoMenuBarSettings = (XUIElementSettings) UnoRuntime
.queryInterface(XUIElementSettings.class, oMenuBar);
oMenuBarSettings = (XIndexContainer) UnoRuntime
.queryInterface(XIndexContainer.class, xoMenuBarSettings
.getSettings(true));
oMenuMAIN = createMenuOption(".uno:PickList", "Main");
}
public void ConvertArrayListToArray(){
int i , j;
if (!this.iuo_arraymenu.isEmpty()){
j=0;
is_menus = new String[this.iuo_arraymenu.size()/4][4];
System.out.println("is_menus.length" + is_menus.length );
for(i=0;i<this.iuo_arraymenu.size();i=i+4){
is_menus[j][0] = this.iuo_arraymenu.get(i);
is_menus[j][1] = this.iuo_arraymenu.get(i+1);
is_menus[j][2] = this.iuo_arraymenu.get(i+2);
is_menus[j][3] = this.iuo_arraymenu.get(i+3);
j= j + 1;
}
}
}
private PropertyValue[] createMenuOption(String sMyCommand, String
label)
throws IllegalArgumentException, IndexOutOfBoundsException,
WrappedTargetException {
PropertyValue[] loadProps = new PropertyValue[3];
loadProps[0] = new PropertyValue();
loadProps[1] = new PropertyValue();
loadProps[2] = new PropertyValue();
loadProps[0].Name = "CommandURL";
loadProps[0].Value = sMyCommand;
loadProps[1].Name = "Label";
loadProps[1].Value = label;
return loadProps;
}
private PropertyValue[] createMenuItem(String sMyCommand, String label)
{
PropertyValue[] loadProps = new PropertyValue[2];
loadProps[0] = new PropertyValue();
loadProps[1] = new PropertyValue();
loadProps[0].Name = "CommandURL";
loadProps[0].Value = sMyCommand;
loadProps[1].Name = "Label";
loadProps[1].Value = label;
return loadProps;
}
public void getTopMenu() throws IllegalArgumentException,
IndexOutOfBoundsException, WrappedTargetException{
for (int i=0;i<is_menus.length;i++){
if (is_menus[i][2].equals("0")){
CreateTreeTopMenu( is_menus[i][0] , 0 , is_menus[i][1]);
}
}
}
public void CreateTreeTopMenu( String as_fatherid , int
ai_indexContainer , String as_menudescr ) throws IllegalArgumentException,
IndexOutOfBoundsException, WrappedTargetException{
boolean lbl_found = false;
for (int i=0;i<is_menus.length;i++){
if (is_menus[i][2].equals(as_fatherid)){
lbl_found = true;
break;
}
}
if (!lbl_found) {
PropertyValue[] property = createMenuItem(".uno:PickList",
as_menudescr);
oContainer[ai_indexContainer].insertByIndex(oContainer[ai_indexContainer].getCount(),property);
return;
}else{
int curindexContainer;
PropertyValue[] MenuOption = createMenuOption(".uno:PickList",
as_menudescr);
curindexContainer = ai_indexContainer + 1;
oContainer[curindexContainer] =
uiConfigManager.createSettings();
for (int i=0;i<is_menus.length;i++){
if (is_menus[i][2].equals(as_fatherid)){
CreateTreeTopMenu(is_menus[i][0], curindexContainer ,
is_menus[i][1] );
}
}
MenuOption[2].Name="ItemDescriptorContainer";
MenuOption[2].Value = oContainer[curindexContainer];
oContainer[ai_indexContainer].insertByIndex(oContainer[ai_indexContainer].getCount(),MenuOption);
}
}
}
//////////////////////////////////////////////////////////////////////////////////////////
Sorry for the long post.....
As you can see i have attached in the xFrame.addEventListener but it is
never fired...
I have also tried to insert it to the xModel even the xController...
But no lack...
Maybe there is something i haven't undestood well