OpenOffice.org Forum at OOoForum.orgThe OpenOffice.org Forum
 
 [Home]   [FAQ]   [Search]   [Memberlist]   [Usergroups]   [Register
 [Profile]   [Log in to check your private messages]   [Log in

Java UNO Component.
Goto page 1, 2  Next
 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Code Snippets
View previous topic :: View next topic  
Author Message
BHARATHY
Power User
Power User


Joined: 27 Apr 2006
Posts: 56
Location: bangalore

PostPosted: Tue Aug 01, 2006 2:44 am    Post subject: Java UNO Component. Reply with quote

Hi this is an example of an Addon using a Java UNO component. The Addon toolbar contains 2 buttons. When you click the 1st button it creates an htm file (in C drive) which reads from Yahoo.com. When you click the 2nd button an htm file which reads from google.com is created.

Code:


Toolbarbutton.java is as follows

package Hello.example.openoffice;
import com.sun.star.lib.uno.helper.*;
import com.sun.star.task.XJobExecutor;
import com.sun.star.lang.*;
import com.sun.star.uno.*;
import com.sun.star.registry.*;
import com.sun.star.frame.XDesktop;
import com.sun.star.text.XTextDocument;
import com.sun.star.text.XTextRange;
import com.sun.star.util.XSearchDescriptor;
import com.sun.star.util.XSearchable;
import com.sun.star.container.XIndexAccess;
import java.net.*;
import java.util.*;
import java.io.*;

public class ToolbarButton
{
    public static final String version = "0.5";
    private XtextDocument xTextDoc;
       
 public static class _ ToolbarButton
 extends WeakBase implements XJobExecutor,     XServiceInfo
    {
    static private final String __serviceName="Hello.example.openoffice.ToolbarButton";
       

 public _ ToolbarButton(XComponentContext xCompContext)
        {
            try
            {
                XMultiComponentFactory xMCF = xCompContext.getServiceManager();
               
                Object desktop = xMCF.createInstanceWithContext(
                        "com.sun.star.frame.Desktop", xCompContext);

                XDesktop xDesktop = (XDesktop)
                    UnoRuntime.queryInterface(XDesktop.class, desktop);
       
                XComponent xComponent = xDesktop.getCurrentComponent();

                xTextDoc = (XTextDocument)
                    UnoRuntime.queryInterface( XTextDocument.class, xComponent );
            }
            catch (java.lang.Exception e)
            {
                e.printStackTrace(System.err);
            }
        }

        public void trigger(String sEvent)
        {   
   String buttonName;
   buttonName=sEvent;
   if(buttonName.equals("button1"))
   yahoocom();
   else if(buttonName.equals("button2"))
   googlecom();
         
        }
    public void yahoocom()
      {
         try
         {
            URL yahoo = new URL("http://www.yahoo.com/");
            BufferedReader in = new BufferedReader(
               new InputStreamReader(
               yahoo.openStream()));

            String inputLine;
            File outFile = new File("c:\\yahoo.htm");
            FileWriter out = new FileWriter(outFile);

            while ((inputLine = in.readLine()) != null)
            {            
               out.write(inputLine);
            }

            in.close();         
            out.close();
         }
         catch(java.io.IOException e){}   
      }


       
  public void googlecom()
      {
         try
         {
            URL google = new URL("http://www.google.com/");
            BufferedReader in = new BufferedReader(
               new InputStreamReader(
               google.openStream()));

            String inputLine;
            File outFile = new File("c:\\google.htm");
            FileWriter out = new FileWriter(outFile);

            while ((inputLine = in.readLine()) != null)
            {            
               out.write(inputLine);
            }

            in.close();         
            out.close();
         }


         catch(java.io.IOException e){}   
      }


      

        public void initialize( Object[] object )
            throws com.sun.star.uno.Exception {
        }
       
        public String[] getSupportedServiceNames() {
            return getServiceNames();
        }
       
        public static String[] getServiceNames()
        {
            String[] sSupportedServiceNames = { __serviceName };
            return sSupportedServiceNames;
        }
       
        public boolean supportsService( String sServiceName )
        {
            return sServiceName.equals( __serviceName );
        }
   
        public String getImplementationName()
        {
            return  _ToolbarButton.class.getName();
        }
    }

public static XSingleComponentFactory __getComponentFactory(String     sImplName)
    {
        XSingleComponentFactory xFactory = null;
       
        if ( sImplName.equals( _ ToolbarButton.class.getName() ) )
      xFactory = Factory.createComponentFactory(_ToolbarButton.class,
       _ToolbarButton.getServiceNames());
       
        return xFactory;
    }

    public static boolean __writeRegistryServiceInfo(XRegistryKey regKey)
    {
        return Factory.writeRegistryServiceInfo(_ToolbarButton.class.getName(),
                                                _ToolbarButton.getServiceNames(),regKey);
    }
}


The behaviour of the Addon can be changed by changing the code in the trigger method.

These are the steps to be followed
1)You have to include the following jar files separated by semi colon in the CLASSPATH Environment Variable.
CLASSPATH=<Openoffice path>\OpenOffice.org 2.0\program\classes\jurt.jar;
<Openoffice path>\OpenOffice.org 2.0\program\classes\unoil.jar;<Openoffice path>\OpenOffice.org 2.0\program\classes\ridl.jar;<Openoffice path>\OpenOffice.org 2.0\program\classes\juh.jar;

Compile ToolbarButton.java
javac ToolbarButton.java


2.you will get ToolbarButton.class and ToolbarButton$_ToolbarButton.class

3.Create a new folder by name CreateJar .create an folder by name Hello in CreateJar. In Hello create a folder by name example.In example create a folder by name
openoffice.

4. Copy the two class files and paste them in openoffice folder.

5. In the CreateJar folder create “Button.uno.manifest” file . The contents of this file
is

RegistrationClassName: Hello.example.openoffice.ToolbarButton

Note: Open a txt file and paste the above line and press “enter”(because The file should end with a new line.). Save the txt file as Button.uno.manifest

6. Create the jar file using this command
jar cfm Button.uno.jar CreateJar/Button.uno.manifest -C CreateJar .

(Copy the above line of code as it is including the . )

7.The contents of Button.uno.jar should be
Hello/example/openoffice/ToolbarButton.class
Hello/example/openoffice/ToolbarButton$_ToolbarButton.class
META-INF/MANIFEST.MF
Button.uno.manifest
You can check this using the command
jar tf Button.uno.jar

8. Now create a folder by name Package. Package will contain the following

Addons.xcu
META-INF folder which contains manifest.xml
Button.uno.jar
images folder
9. Create a folder by name META-INF

10. We should place the manifest.xml in the META-INF folder. The contents of the manifest.xml is as follows.
Code:


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE manifest:manifest PUBLIC "-//OpenOffice.org//DTD Manifest 1.0//EN" "Manifest.dtd">
<manifest:manifest xmlns:manifest="http://openoffice.org/2001/manifest">
<manifest:file-entry manifest:media-type="application/vnd.sun.star.uno-component;type=Java" manifest:full-path="Button.uno.jar"/>
</manifest:manifest>


9. The contents of Addons.xcu is as follows

Code:

<?xml version="1.0" encoding="UTF-8"?>
<oor:component-data xmlns:oor="http://openoffice.org/2001/registry"
  xmlns:xs="http://www.w3.org/2001/XMLSchema" oor:name="Addons"
  oor:package="org.openoffice.Office">

 <node oor:name="AddonUI">
<node oor:name="OfficeToolBar">

<node oor:name="Hello.example.openoffice.ToolbarButton" oor:op="replace">

<node oor:name="m1" oor:op="replace">
<prop oor:name="URL" oor:type="xs:string">                                   <value>service:Hello.example.openoffice.ToolbarButton?button1</value>
</prop>                   
<prop oor:name="ImageIdentifier" oor:type="xs:string">
  <value/>
</prop>
<prop oor:name="Title" oor:type="xs:string">
  <value xml:lang="en-US">YAHOOO</value>
</prop>           
 <prop oor:name="Target" oor:type="xs:string">
      <value>_self</value>
 </prop>             
<prop oor:name="Context" oor:type="xs:string">
  <value>com.sun.star.text.TextDocument</value>
 </prop>
 </node>

 <node oor:name="m2" oor:op="replace">
<prop oor:name="URL" oor:type="xs:string">                       <value>service:Hello.example.openoffice.ToolbarButton?button2</value> </prop>
<prop oor:name="ImageIdentifier" oor:type="xs:string">
<value/>
</prop>
<prop oor:name="Title" oor:type="xs:string">
<value/>
 <value xml:lang="en-US">GOOOGLE</value>
 </prop>
 <prop oor:name="Target" oor:type="xs:string">
 <value>_self</value>
 </prop>
 <prop oor:name="Context" oor:type="xs:string"> <value>com.sun.star.text.TextDocument</value>
 </prop>
  </node>
                           
  </node>
  </node>
  <node oor:name="Images">

<node oor:name="Hello.example.openoffice.ToolbarButton.image1" oor:op="replace">
 <prop oor:name="URL">
 <value>service:Hello.example.openoffice.ToolbarButton?execute</value>
 </prop>
  <node oor:name="UserDefinedImages">
  <prop oor:name="ImageSmallURL" oor:type="xs:string">
  <value>%origin%/images/Figure1.bmp</value>
  </prop>
  </node>
  </node>
           
<node oor:name="Hello.example.openoffice.ToolbarButton.image2" oor:op="replace">
<prop oor:name="URL">
<value>service:Hello.example.openoffice.ToolbarButton?button2</value>
</prop>
<node oor:name="UserDefinedImages">
<prop oor:name="ImageSmallURL" oor:type="xs:string">
<value>%origin%/images/Figure2.bmp</value>
</prop>
</node>
</node>
    </node>
</node>

</oor:component-data>


11. The images folder contains 3 bmp files namely Figure1.bmp, Figure2.bmp and Figure3.bmp . (16x16 )

12. Zip the contents of the Package folder. That is
Addons.xcu
META-INF/manifest.xml
Button.uno.jar
images folder

and name the zip file as NewToolbar.uno.zip

13. Now install the package using
<OpenOffice path>\program\
UNOPKG add NewToolbar.uno.zip

Note: Close all openoffice documents and also the Quickstarter before installing the packge.

14)To remove the package
<OpenOffice path>\ OpenOffice.org 2.0\program\
UNOPKG remove NewToolbar.uno.zip


I had used jdk1.5.0_07 and openoffice2.0.

I referred this link
http://blog.vojta.name/archives/2005/06/02/T13_36_48/

You can also find a Python UNo compponent at this link

With Regards,
Bhararthy B
_________________
Bharathy
bharathyb83@yahoo.com


Last edited by BHARATHY on Wed Nov 15, 2006 7:55 am; edited 1 time in total
Back to top
View user's profile Send private message Yahoo Messenger
SergeM
Super User
Super User


Joined: 09 Sep 2003
Posts: 3211
Location: Troyes France

PostPosted: Wed Aug 02, 2006 9:11 am    Post subject: Reply with quote

Happy to see you solve all your problems.
Thank you for the code.
_________________
Linux & Windows OOo3.0
UNO & C++ : WIKI
http://wiki.services.openoffice.org/wiki/Using_Cpp_with_the_OOo_SDK
In French
http://wiki.services.openoffice.org/wiki/Documentation/FR/Cpp_Guide
Back to top
View user's profile Send private message Visit poster's website
BHARATHY
Power User
Power User


Joined: 27 Apr 2006
Posts: 56
Location: bangalore

PostPosted: Wed Aug 02, 2006 10:31 pm    Post subject: Reply with quote

Hi thank you. I would be happy if someone could add a code snippet for Compiling the Java Components in SDK. i have been trying to compile them but without success. I dont know as to where i am going wrong in using the make utility.It would really be helpfull for people like me who are new to both openoffice and Java. i dont know if i am supposed to discuss it in CodeSnippets forum.

With Regards,
Bharathy B
_________________
Bharathy
bharathyb83@yahoo.com
Back to top
View user's profile Send private message Yahoo Messenger
gavin_m
Newbie
Newbie


Joined: 02 Aug 2006
Posts: 2

PostPosted: Wed Aug 02, 2006 11:51 pm    Post subject: compiling the above in linux(fedora core 5) Reply with quote

Hi,
I am new to UNO programming. I tried your Java Component on Linux.
But it is not getting added to the writer.

Will it work only on windows?

with regards
Gavin
Back to top
View user's profile Send private message Send e-mail
BHARATHY
Power User
Power User


Joined: 27 Apr 2006
Posts: 56
Location: bangalore

PostPosted: Thu Aug 03, 2006 12:49 am    Post subject: Reply with quote

Hi sorry i hav tried it on Windows.I have not tried it on Linux. I guess it should work on Linux too. May be someone who has more knowledge regarding this should be able to clear your doubt.Didnt you get any errors

check if u hav selected the Addon. (View-Toolbar-Addon1) Or else check if the package is added using Tools-->PackageManager-->My Packages-->NewToolbar.uno.zip

To check if the package has been added or not you can use the following command.

<openofficepath>\OpenOffice.org 2.0\program>UNOPKG list

You will get the following

<openofficepath>\OpenOffice.org 2.0\program>UNOPKG list
all deployed user packages:
Name :NewToolbar.uno.zip
URL:
Media-Type:
description:

and so on

With Regards
bharathy
_________________
Bharathy
bharathyb83@yahoo.com
Back to top
View user's profile Send private message Yahoo Messenger
SergeM
Super User
Super User


Joined: 09 Sep 2003
Posts: 3211
Location: Troyes France

PostPosted: Fri Aug 04, 2006 12:41 am    Post subject: Reply with quote

Quote:
Hi thank you. I would be happy if someone could add a code snippet for Compiling the Java Components in SDK. i have been trying to compile them but without success.

I never encounter problem :
Code:

make ALL
make ????

The make ALL gives you what you take to replace ????
_________________
Linux & Windows OOo3.0
UNO & C++ : WIKI
http://wiki.services.openoffice.org/wiki/Using_Cpp_with_the_OOo_SDK
In French
http://wiki.services.openoffice.org/wiki/Documentation/FR/Cpp_Guide
Back to top
View user's profile Send private message Visit poster's website
gavin_m
Newbie
Newbie


Joined: 02 Aug 2006
Posts: 2

PostPosted: Fri Aug 04, 2006 1:05 am    Post subject: Java UNO Component Reply with quote

Hi,
I get the following error messages when i use the unopkg cmd.

Name: NewToolbarButton.uno.zip
URL: vnd.sun.star.expand:$UNO_USER_PACKAGES_CACHE/uno_packages/Xn3Yrz_/NewToolbarButton.uno.zip is registered: no
Media-Type: application/vnd.sun.star.legacy-package-bundle
Description: UNO Package Bundle
It says that it is not registered.

When I checked the Tools->Package Manager-> My Packages box I see the NewToolBar added there but its status is Disabled.

When I tried to enable it I got a DispatchException error.

Any idea what can be done.

Regards
Gavin
Back to top
View user's profile Send private message Send e-mail
BHARATHY
Power User
Power User


Joined: 27 Apr 2006
Posts: 56
Location: bangalore

PostPosted: Fri Aug 04, 2006 2:26 am    Post subject: Reply with quote

Hi i think you can remove the package and try to redo it.and install again. I have no idea why it is happening.

Go through the link i have posted. the author has explained the procedure more clearly.

With regards,
Bharathy B
_________________
Bharathy
bharathyb83@yahoo.com
Back to top
View user's profile Send private message Yahoo Messenger
SergeM
Super User
Super User


Joined: 09 Sep 2003
Posts: 3211
Location: Troyes France

PostPosted: Wed Aug 23, 2006 7:55 am    Post subject: Reply with quote

I just add a cross reference :

connecting menus to macros http://www.oooforum.org/forum/viewtopic.phtml?t=41951&highlight=
_________________
Linux & Windows OOo3.0
UNO & C++ : WIKI
http://wiki.services.openoffice.org/wiki/Using_Cpp_with_the_OOo_SDK
In French
http://wiki.services.openoffice.org/wiki/Documentation/FR/Cpp_Guide
Back to top
View user's profile Send private message Visit poster's website
WenJinChao
General User
General User


Joined: 16 Oct 2006
Posts: 13

PostPosted: Mon Oct 16, 2006 7:45 pm    Post subject: Reply with quote

Hi,
I got a warning messsage as follows:

WARNING: An error occurred while enabling the package: Button.uno.jar, Cause: (
com.sun.star.registry.CannotRegisterImplementationException) { { Message = "Hell
o.example.openoffice.ToolbarButton ", Context = (com.sun.star.uno.XInterface) @0
} }

Do you have any idea how would this happens?
Is there any solution?

Thsnk you for any help

Wen
Back to top
View user's profile Send private message Send e-mail Yahoo Messenger MSN Messenger
BHARATHY
Power User
Power User


Joined: 27 Apr 2006
Posts: 56
Location: bangalore

PostPosted: Wed Oct 18, 2006 12:35 am    Post subject: Reply with quote

hi u can find out why the error message by reading this file.

<Path>\OpenOffice.org2.0_SDK\idl\com\sun\star\registry\CannotRegisterImplementationException.idl

Open this file in Visual Studio.. Dont change anything in this file.. Just read why the exception is thrown..

With Regards,
Bharathy B
_________________
Bharathy
bharathyb83@yahoo.com
Back to top
View user's profile Send private message Yahoo Messenger
nadim_sd
General User
General User


Joined: 04 Oct 2006
Posts: 30
Location: Lebanon

PostPosted: Fri Oct 20, 2006 4:39 am    Post subject: Reply with quote

Hi,

Thanks for the example, it really helps...
I have some problems though;
    - The toolbar appear in writer, but how can I make it appear in Calc? (I couldn't notice any reference to writer... did I miss it)
    - The toolbar is displayed, but clicking the buttons is not triggering any action. What could I be doing wrong? is there a way to debug it?

Thank you...
Back to top
View user's profile Send private message
BHARATHY
Power User
Power User


Joined: 27 Apr 2006
Posts: 56
Location: bangalore

PostPosted: Sun Oct 22, 2006 9:21 pm    Post subject: Reply with quote

Hi in the Addons.xcu you can find this line. This tells the Addon should appear in Writer..Similarly u can add one more line
<value>com.sun.star.sheet.SpreadsheetDocument</value>

<prop oor:name="Context" oor:type="xs:string">
<value>com.sun.star.text.TextDocument</value>
<value>com.sun.star.sheet.SpreadsheetDocument</value>
</prop>

Writer: com.sun.star.text.TextDocument
Spreadsheet: com.sun.star.sheet.SpreadsheetDocument
Presentation: com.sun.star.presentation.PresentationDocument
Draw: com.sun.star.drawing.DrawingDocument
Formula: com.sun.star.formula.FormulaProperties
Chart: com.sun.star.chart.ChartDocument
Bibliography: com.sun.star.frame.Bibliography

you can find this in pg 300 of dev guide.

And regarding why the button sare not triggering any action.. these may be one of the reasons..
check if u hv placed the class files in proper directory structure.
check if u hv specified the correct url in the Addons.xcu
check if u hav spelled the names of the addon files properly. sometimes u name the file ProtocolHandler.xcu and in the manifest file spell it as ProtocolHAndlerAddon.xcu.

These were some if the mistakes i had comitted. U can use NetBeans to debug them.

With Regards
Bharathy B
_________________
Bharathy
bharathyb83@yahoo.com
Back to top
View user's profile Send private message Yahoo Messenger
nadim_sd
General User
General User


Joined: 04 Oct 2006
Posts: 30
Location: Lebanon

PostPosted: Wed Oct 25, 2006 10:38 pm    Post subject: Reply with quote

Thanks for the reply BHARATHY...
I think you guys can help me with the following problem...

I wanted to test the following example in the SDK directory:
<SDK>/examples/DevelopersGuide/Spreadsheet/ExampleDataPilotSource.java

However I need to deploy it first...
so I tried the following (which is similar to the tutorial at the begining of this post):
    - I generated a jar that contains the build of both ExampleDataPilotSource.java and SpreadsheetDocHelper.java
    - I put the corresponding manifest in the META-INF directory
    - I zipped the jar file and the META-INF folder to test.uno.zip
    - I installed them using unopkg add test.uno.zip

Is there anything wrong in my procedure???

According to the readme file, I'm supposed to choose External source/interface in the Dialog Data->DataPilot->Start. But this option is always disabled!!! and I can't proceed...

Any help is appreciated...

Thanks...
Back to top
View user's profile Send private message
BHARATHY
Power User
Power User


Joined: 27 Apr 2006
Posts: 56
Location: bangalore

PostPosted: Thu Oct 26, 2006 12:39 am    Post subject: Reply with quote

Hi I hav tried to use the build given in SDk examples but was nvr abl to understand how to use them. Some one who has used them and with better knowledge regarding this can help you.. If you get to know.. let me know..

With Regards,
Bharathy B
_________________
Bharathy
bharathyb83@yahoo.com
Back to top
View user's profile Send private message Yahoo Messenger
Display posts from previous:   
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Code Snippets All times are GMT - 8 Hours
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group