| 1 |
/* |
| 2 |
* This file is part of the LibreOffice project. |
| 3 |
* |
| 4 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 5 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 6 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 7 |
* |
| 8 |
* This file incorporates work covered by the following license notice: |
| 9 |
* |
| 10 |
* Licensed to the Apache Software Foundation (ASF) under one or more |
| 11 |
* contributor license agreements. See the NOTICE file distributed |
| 12 |
* with this work for additional information regarding copyright |
| 13 |
* ownership. The ASF licenses this file to you under the Apache |
| 14 |
* License, Version 2.0 (the "License"); you may not use this file |
| 15 |
* except in compliance with the License. You may obtain a copy of |
| 16 |
* the License at http://www.apache.org/licenses/LICENSE-2.0 . |
| 17 |
*/ |
| 18 |
|
| 19 |
package complex.contextMenuInterceptor; |
| 20 |
|
| 21 |
import com.sun.star.ui.*; |
| 22 |
import com.sun.star.beans.XPropertySet; |
| 23 |
import com.sun.star.uno.UnoRuntime; |
| 24 |
|
| 25 |
public class ContextMenuInterceptor implements XContextMenuInterceptor |
| 26 |
{ |
| 27 |
|
| 28 |
private final com.sun.star.awt.XBitmap myBitmap; |
| 29 |
|
| 30 |
public ContextMenuInterceptor(com.sun.star.awt.XBitmap aBitmap) |
| 31 |
{ |
| 32 |
myBitmap = aBitmap; |
| 33 |
} |
| 34 |
|
| 35 |
public ContextMenuInterceptorAction notifyContextMenuExecute( |
| 36 |
com.sun.star.ui.ContextMenuExecuteEvent aEvent) throws RuntimeException |
| 37 |
{ |
| 38 |
try |
| 39 |
{ |
| 40 |
// Retrieve context menu container and query for service factory to |
| 41 |
// create sub menus, menu entries and separators |
| 42 |
com.sun.star.container.XIndexContainer xContextMenu = aEvent.ActionTriggerContainer; |
| 43 |
com.sun.star.lang.XMultiServiceFactory xMenuElementFactory = |
| 44 |
UnoRuntime.queryInterface(com.sun.star.lang.XMultiServiceFactory.class, xContextMenu); |
| 45 |
|
| 46 |
if (xMenuElementFactory != null) |
| 47 |
{ |
| 48 |
|
| 49 |
// create root menu entry for sub menu and sub menu |
| 50 |
com.sun.star.beans.XPropertySet xRootMenuEntry = |
| 51 |
UnoRuntime.queryInterface(com.sun.star.beans.XPropertySet.class, xMenuElementFactory.createInstance("com.sun.star.ui.ActionTrigger")); |
| 52 |
|
| 53 |
// create a line separator for our new help sub menu |
| 54 |
com.sun.star.beans.XPropertySet xSeparator = |
| 55 |
UnoRuntime.queryInterface(com.sun.star.beans.XPropertySet.class, xMenuElementFactory.createInstance("com.sun.star.ui.ActionTriggerSeparator")); |
| 56 |
Short aSeparatorType = Short.valueOf(ActionTriggerSeparatorType.LINE); |
| 57 |
xSeparator.setPropertyValue("SeparatorType", aSeparatorType); |
| 58 |
|
| 59 |
// query sub menu for index container to get access |
| 60 |
com.sun.star.container.XIndexContainer xSubMenuContainer = |
| 61 |
UnoRuntime.queryInterface(com.sun.star.container.XIndexContainer.class, xMenuElementFactory.createInstance("com.sun.star.ui.ActionTriggerContainer")); |
| 62 |
|
| 63 |
// initialize root menu entry "Help" |
| 64 |
xRootMenuEntry.setPropertyValue("Text", "Help"); |
| 65 |
xRootMenuEntry.setPropertyValue("CommandURL", ".uno:HelpMenu"); |
| 66 |
xRootMenuEntry.setPropertyValue("HelpURL", "5410"); |
| 67 |
xRootMenuEntry.setPropertyValue("SubContainer", xSubMenuContainer); |
| 68 |
xRootMenuEntry.setPropertyValue("Image", myBitmap); |
| 69 |
|
| 70 |
// create menu entries for the new sub menu |
| 71 |
// initialize help/content menu entry |
| 72 |
// entry "Content" |
| 73 |
XPropertySet xMenuEntry = UnoRuntime.queryInterface(XPropertySet.class, xMenuElementFactory.createInstance("com.sun.star.ui.ActionTrigger")); |
| 74 |
xMenuEntry.setPropertyValue("Text", "Content"); |
| 75 |
xMenuEntry.setPropertyValue("CommandURL", ".uno:HelpIndex"); |
| 76 |
xMenuEntry.setPropertyValue("HelpURL", "5401"); |
| 77 |
|
| 78 |
// insert menu entry to sub menu |
| 79 |
xSubMenuContainer.insertByIndex(0, xMenuEntry); |
| 80 |
|
| 81 |
// initialize help/help on help |
| 82 |
// entry "Help on Help" |
| 83 |
xMenuEntry = UnoRuntime.queryInterface(com.sun.star.beans.XPropertySet.class, xMenuElementFactory.createInstance("com.sun.star.ui.ActionTrigger")); |
| 84 |
xMenuEntry.setPropertyValue("Text", "Help on Help"); |
| 85 |
xMenuEntry.setPropertyValue("CommandURL", ".uno:HelpOnHelp"); |
| 86 |
xMenuEntry.setPropertyValue("HelpURL", "5400"); |
| 87 |
|
| 88 |
// insert menu entry to sub menu |
| 89 |
xSubMenuContainer.insertByIndex(1, xMenuEntry); |
| 90 |
// initialize help/tips |
| 91 |
// entry "Tips" |
| 92 |
xMenuEntry = UnoRuntime.queryInterface(com.sun.star.beans.XPropertySet.class, xMenuElementFactory.createInstance("com.sun.star.ui.ActionTrigger")); |
| 93 |
xMenuEntry.setPropertyValue("Text", "Tips"); |
| 94 |
xMenuEntry.setPropertyValue("CommandURL", ".uno:HelpTip"); |
| 95 |
xMenuEntry.setPropertyValue("HelpURL", "5404"); |
| 96 |
|
| 97 |
// insert menu entry to sub menu |
| 98 |
xSubMenuContainer.insertByIndex(2, xMenuEntry); |
| 99 |
|
| 100 |
// add separator into the given context menu |
| 101 |
xContextMenu.insertByIndex(0, xSeparator); |
| 102 |
|
| 103 |
// add new sub menu into the given context menu |
| 104 |
xContextMenu.insertByIndex(0, xRootMenuEntry); |
| 105 |
|
| 106 |
// The controller should execute the modified context menu and stop notifying other |
| 107 |
// interceptors. |
| 108 |
return com.sun.star.ui.ContextMenuInterceptorAction.EXECUTE_MODIFIED; |
| 109 |
} |
| 110 |
} |
| 111 |
catch (com.sun.star.beans.UnknownPropertyException ex) |
| 112 |
{ |
| 113 |
// do something useful |
| 114 |
// we used an unknown property |
| 115 |
} |
| 116 |
catch (com.sun.star.lang.IndexOutOfBoundsException ex) |
| 117 |
{ |
| 118 |
// do something useful |
| 119 |
// we used an invalid index for accessing a container |
| 120 |
} |
| 121 |
catch (com.sun.star.uno.Exception ex) |
| 122 |
{ |
| 123 |
// something strange has happened! |
| 124 |
} |
| 125 |
catch (java.lang.Throwable ex) |
| 126 |
{ |
| 127 |
// catch java exceptions do something useful |
| 128 |
} |
| 129 |
|
| 130 |
return com.sun.star.ui.ContextMenuInterceptorAction.IGNORED; |
| 131 |
} |
| 132 |
} |