1 package contextMenuInterceptor
;
3 import com
.sun
.star
.ui
.*;
4 import com
.sun
.star
.lang
.XMultiServiceFactory
;
5 import com
.sun
.star
.beans
.XPropertySet
;
6 import com
.sun
.star
.container
.XIndexContainer
;
7 import com
.sun
.star
.uno
.UnoRuntime
;
8 import com
.sun
.star
.uno
.Exception
;
9 import com
.sun
.star
.beans
.UnknownPropertyException
;
10 import com
.sun
.star
.lang
.IllegalArgumentException
;
12 public class ContextMenuInterceptor
implements XContextMenuInterceptor
{
14 private com
.sun
.star
.awt
.XBitmap myBitmap
;
16 public ContextMenuInterceptor( com
.sun
.star
.awt
.XBitmap aBitmap
) {
20 public ContextMenuInterceptorAction
notifyContextMenuExecute(
21 com
.sun
.star
.ui
.ContextMenuExecuteEvent aEvent
) throws RuntimeException
25 // Retrieve context menu container and query for service factory to
26 // create sub menus, menu entries and separators
27 com
.sun
.star
.container
.XIndexContainer xContextMenu
= aEvent
.ActionTriggerContainer
;
28 com
.sun
.star
.lang
.XMultiServiceFactory xMenuElementFactory
=
29 (com
.sun
.star
.lang
.XMultiServiceFactory
)UnoRuntime
.queryInterface(
30 com
.sun
.star
.lang
.XMultiServiceFactory
.class, xContextMenu
);
32 if ( xMenuElementFactory
!= null )
35 // create root menu entry for sub menu and sub menu
36 com
.sun
.star
.beans
.XPropertySet xRootMenuEntry
=
37 (XPropertySet
)UnoRuntime
.queryInterface(
38 com
.sun
.star
.beans
.XPropertySet
.class,
39 xMenuElementFactory
.createInstance("com.sun.star.ui.ActionTrigger" ));
41 // create a line separator for our new help sub menu
42 com
.sun
.star
.beans
.XPropertySet xSeparator
=
43 (com
.sun
.star
.beans
.XPropertySet
)UnoRuntime
.queryInterface(
44 com
.sun
.star
.beans
.XPropertySet
.class,
45 xMenuElementFactory
.createInstance("com.sun.star.ui.ActionTriggerSeparator" ) );
46 Short aSeparatorType
= new Short( ActionTriggerSeparatorType
.LINE
);
47 xSeparator
.setPropertyValue( "SeparatorType", (Object
)aSeparatorType
);
49 // query sub menu for index container to get access
50 com
.sun
.star
.container
.XIndexContainer xSubMenuContainer
=
51 (com
.sun
.star
.container
.XIndexContainer
)UnoRuntime
.queryInterface(
52 com
.sun
.star
.container
.XIndexContainer
.class,
53 xMenuElementFactory
.createInstance("com.sun.star.ui.ActionTriggerContainer" ));
55 // intialize root menu entry "Help"
56 xRootMenuEntry
.setPropertyValue( "Text", new String( "Help" ));
57 xRootMenuEntry
.setPropertyValue( "CommandURL", new String( "slot:5410" ));
58 xRootMenuEntry
.setPropertyValue( "HelpURL", new String( "5410" ));
59 xRootMenuEntry
.setPropertyValue( "SubContainer", (Object
)xSubMenuContainer
);
60 xRootMenuEntry
.setPropertyValue( "Image", myBitmap
);
62 // create menu entries for the new sub menu
63 // intialize help/content menu entry
65 XPropertySet xMenuEntry
= (XPropertySet
)UnoRuntime
.queryInterface(
66 XPropertySet
.class, xMenuElementFactory
.createInstance (
67 "com.sun.star.ui.ActionTrigger" ));
68 xMenuEntry
.setPropertyValue( "Text", new String( "Content" ));
69 xMenuEntry
.setPropertyValue( "CommandURL", new String( "slot:5401" ));
70 xMenuEntry
.setPropertyValue( "HelpURL", new String( "5401" ));
72 // insert menu entry to sub menu
73 xSubMenuContainer
.insertByIndex ( 0, (Object
)xMenuEntry
);
75 // intialize help/help agent
77 xMenuEntry
= (com
.sun
.star
.beans
.XPropertySet
)UnoRuntime
.queryInterface(
78 com
.sun
.star
.beans
.XPropertySet
.class,
79 xMenuElementFactory
.createInstance("com.sun.star.ui.ActionTrigger" ));
80 xMenuEntry
.setPropertyValue( "Text", new String( "Help Agent" ));
81 xMenuEntry
.setPropertyValue( "CommandURL", new String( "slot:5962" ));
82 xMenuEntry
.setPropertyValue( "HelpURL", new String( "5962" ));
84 // insert menu entry to sub menu
85 xSubMenuContainer
.insertByIndex( 1, (Object
)xMenuEntry
);
86 // intialize help/tips
88 xMenuEntry
= (com
.sun
.star
.beans
.XPropertySet
)UnoRuntime
.queryInterface(
89 com
.sun
.star
.beans
.XPropertySet
.class,
90 xMenuElementFactory
.createInstance("com.sun.star.ui.ActionTrigger" ));
91 xMenuEntry
.setPropertyValue( "Text", new String( "Tips" ));
92 xMenuEntry
.setPropertyValue( "CommandURL", new String( "slot:5404" ));
93 xMenuEntry
.setPropertyValue( "HelpURL", new String( "5404" ));
95 // insert menu entry to sub menu
96 xSubMenuContainer
.insertByIndex ( 2, (Object
)xMenuEntry
);
98 // add separator into the given context menu
99 xContextMenu
.insertByIndex ( 0, (Object
)xSeparator
);
101 // add new sub menu into the given context menu
102 xContextMenu
.insertByIndex ( 0, (Object
)xRootMenuEntry
);
104 // The controller should execute the modified context menu and stop notifying other
106 return com
.sun
.star
.ui
.ContextMenuInterceptorAction
.EXECUTE_MODIFIED
;
109 catch ( com
.sun
.star
.beans
.UnknownPropertyException ex
)
111 // do something useful
112 // we used a unknown property
114 catch ( com
.sun
.star
.lang
.IndexOutOfBoundsException ex
)
116 // do something useful
117 // we used an invalid index for accessing a container
119 catch ( com
.sun
.star
.uno
.Exception ex
)
121 // something strange has happend!
123 catch ( java
.lang
.Throwable ex
)
125 // catch java exceptions � do something useful
128 return com
.sun
.star
.ui
.ContextMenuInterceptorAction
.IGNORED
;