~ubuntu-branches/ubuntu/raring/accessodf/raring

« back to all changes in this revision

Viewing changes to addon/be/docarch/accessodf/ooo/AccessODFAddOn.java

  • Committer: Package Import Robot
  • Author(s): Sebastian Humenda
  • Date: 2012-04-09 11:21:13 UTC
  • Revision ID: package-import@ubuntu.com-20120409112113-v0kmfdj1ks80xoj8
Tags: upstream-0.1
ImportĀ upstreamĀ versionĀ 0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 *  AccessODF - Accessibility checker for OpenOffice.org and LibreOffice Writer.
 
3
 *
 
4
 *  Copyright (c) 2011 by DocArch <http://www.docarch.be>.
 
5
 *
 
6
 *  This program is free software: you can redistribute it and/or modify
 
7
 *  it under the terms of the GNU Lesser General Public License as
 
8
 *  published by the Free Software Foundation, either version 3 of the
 
9
 *  License, or (at your option) any later version.
 
10
 *
 
11
 *  This program is distributed in the hope that it will be useful,
 
12
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 *  GNU Lesser General Public License for more details.
 
15
 *
 
16
 *  You should have received a copy of the GNU Lesser General Public License
 
17
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
18
 */
 
19
 
 
20
package be.docarch.accessodf.ooo;
 
21
 
 
22
import java.io.File;
 
23
import java.util.logging.Logger;
 
24
import java.util.logging.Level;
 
25
import java.util.logging.Handler;
 
26
import java.util.logging.FileHandler;
 
27
import java.util.logging.SimpleFormatter;
 
28
 
 
29
import com.sun.star.beans.XPropertySet;
 
30
import com.sun.star.uno.UnoRuntime;
 
31
import com.sun.star.uno.XComponentContext;
 
32
import com.sun.star.frame.XModel;
 
33
import com.sun.star.frame.XFrame;
 
34
import com.sun.star.frame.XLayoutManager;
 
35
import com.sun.star.frame.XDesktop;
 
36
import com.sun.star.lang.XMultiComponentFactory;
 
37
import com.sun.star.lang.XComponent;
 
38
 
 
39
import com.sun.star.lang.XSingleComponentFactory;
 
40
import com.sun.star.registry.XRegistryKey;
 
41
import com.sun.star.lib.uno.helper.WeakBase;
 
42
import com.sun.star.lib.uno.helper.Factory;
 
43
 
 
44
import be.docarch.accessodf.Constants;
 
45
 
 
46
 
 
47
public final class AccessODFAddOn extends WeakBase
 
48
   implements com.sun.star.lang.XInitialization,
 
49
              com.sun.star.frame.XDispatch,
 
50
              com.sun.star.lang.XServiceInfo,
 
51
              com.sun.star.frame.XDispatchProvider
 
52
{
 
53
    private final XComponentContext m_xContext;
 
54
    private com.sun.star.frame.XFrame m_xFrame;
 
55
    private static final String m_implementationName = AccessODFAddOn.class.getName();
 
56
    private static final String[] m_serviceNames = {
 
57
        "com.sun.star.frame.ProtocolHandler" };
 
58
 
 
59
    private static final Logger logger = Logger.getLogger(Constants.LOGGER_NAME);
 
60
    private static final String TMP_NAME = Constants.TMP_PREFIX;
 
61
    private static final File TMP_DIR = Constants.getTmpDirectory();
 
62
    private Handler fh = null;
 
63
    private File logFile = null;
 
64
    private int counter = 0;
 
65
 
 
66
    public AccessODFAddOn( XComponentContext context )
 
67
    {
 
68
        m_xContext = context;
 
69
 
 
70
        try {
 
71
 
 
72
            logFile = File.createTempFile(TMP_NAME, ".log", TMP_DIR);
 
73
            fh = new FileHandler(logFile.getAbsolutePath());
 
74
            fh.setFormatter(new SimpleFormatter());
 
75
            Logger.getLogger("").addHandler(fh);
 
76
            Logger.getLogger("").setLevel(Level.FINEST);
 
77
 
 
78
        } catch (java.io.IOException ex) {
 
79
            logger.log(Level.SEVERE, null, ex);
 
80
        } catch (RuntimeException ex) {
 
81
            logger.log(Level.SEVERE, null, ex);
 
82
        }
 
83
 
 
84
    };
 
85
 
 
86
    public static XSingleComponentFactory __getComponentFactory( String sImplementationName ) {
 
87
        XSingleComponentFactory xFactory = null;
 
88
 
 
89
        if ( sImplementationName.equals( m_implementationName ) )
 
90
            xFactory = Factory.createComponentFactory(AccessODFAddOn.class, m_serviceNames);
 
91
        return xFactory;
 
92
    }
 
93
 
 
94
    public static boolean __writeRegistryServiceInfo( XRegistryKey xRegistryKey ) {
 
95
        return Factory.writeRegistryServiceInfo(m_implementationName,
 
96
                                                m_serviceNames,
 
97
                                                xRegistryKey);
 
98
    }
 
99
 
 
100
    // com.sun.star.lang.XInitialization:
 
101
    public void initialize( Object[] object )
 
102
        throws com.sun.star.uno.Exception
 
103
    {
 
104
        if ( object.length > 0 )
 
105
        {
 
106
            m_xFrame = (com.sun.star.frame.XFrame)UnoRuntime.queryInterface(
 
107
                com.sun.star.frame.XFrame.class, object[0]);
 
108
        }
 
109
    }
 
110
 
 
111
    // com.sun.star.frame.XDispatch:
 
112
     public void dispatch( com.sun.star.util.URL aURL,
 
113
                           com.sun.star.beans.PropertyValue[] aArguments ) {
 
114
 
 
115
         logger.info(String.valueOf(counter++));
 
116
 
 
117
         if ( aURL.Protocol.startsWith("be.docarch.accessodf.ooo.accessodfaddon")) {
 
118
 
 
119
            if ( aURL.Path.compareTo("TaskPanelCommand") == 0 ) {
 
120
 
 
121
                try {
 
122
 
 
123
                    XMultiComponentFactory xMCF = (XMultiComponentFactory) UnoRuntime.queryInterface(
 
124
                                                   XMultiComponentFactory.class, m_xContext.getServiceManager());
 
125
                    Object desktop = xMCF.createInstanceWithContext("com.sun.star.frame.Desktop", m_xContext);
 
126
                    XDesktop xDesktop = (XDesktop) UnoRuntime.queryInterface(XDesktop.class, desktop);
 
127
                    XComponent doc = (XComponent)xDesktop.getCurrentComponent();
 
128
 
 
129
                    XModel xModel = (XModel) UnoRuntime.queryInterface(XModel.class, doc);
 
130
                    XFrame xFrame = xModel.getCurrentController().getFrame();
 
131
                    XPropertySet xPropertySet = (XPropertySet)UnoRuntime.queryInterface(
 
132
                                                 XPropertySet.class, xFrame);
 
133
                    XLayoutManager xLayoutManager = (XLayoutManager) UnoRuntime.queryInterface(
 
134
                                                     XLayoutManager.class, xPropertySet.getPropertyValue("LayoutManager"));
 
135
 
 
136
                    String toolPanel = "private:resource/toolpanel/be.docarch.accessibility.ooo.toolpanel";
 
137
 
 
138
                    if (xLayoutManager.isElementVisible(toolPanel)) { // geeft altijd false
 
139
                        xLayoutManager.hideElement(toolPanel);
 
140
                    } else {
 
141
                        xLayoutManager.showElement(toolPanel);
 
142
                    }
 
143
 
 
144
                } catch (Exception e) {
 
145
                    logger.log(Level.SEVERE, null, e);
 
146
                }
 
147
 
 
148
                return;
 
149
            }
 
150
        }
 
151
    }
 
152
 
 
153
    public void addStatusListener( com.sun.star.frame.XStatusListener xControl,
 
154
                                    com.sun.star.util.URL aURL )
 
155
    {
 
156
        // add your own code here
 
157
    }
 
158
 
 
159
    public void removeStatusListener( com.sun.star.frame.XStatusListener xControl,
 
160
                                       com.sun.star.util.URL aURL )
 
161
    {
 
162
        // add your own code here
 
163
    }
 
164
 
 
165
    // com.sun.star.lang.XServiceInfo:
 
166
    public String getImplementationName() {
 
167
         return m_implementationName;
 
168
    }
 
169
 
 
170
    public boolean supportsService( String sService ) {
 
171
        int len = m_serviceNames.length;
 
172
 
 
173
        for( int i=0; i < len; i++) {
 
174
            if (sService.equals(m_serviceNames[i]))
 
175
                return true;
 
176
        }
 
177
        return false;
 
178
    }
 
179
 
 
180
    public String[] getSupportedServiceNames() {
 
181
        return m_serviceNames;
 
182
    }
 
183
 
 
184
    // com.sun.star.frame.XDispatchProvider:
 
185
    public com.sun.star.frame.XDispatch queryDispatch( com.sun.star.util.URL aURL,
 
186
                                                       String sTargetFrameName,
 
187
                                                       int iSearchFlags )
 
188
    {
 
189
        if ( aURL.Protocol.startsWith("be.docarch.accessodf.ooo.accessodfaddon"))
 
190
        {
 
191
            if ( aURL.Path.compareTo("TaskPanelCommand") == 0 )
 
192
                return this;
 
193
        }
 
194
        return null;
 
195
    }
 
196
 
 
197
    // com.sun.star.frame.XDispatchProvider:
 
198
    public com.sun.star.frame.XDispatch[] queryDispatches(
 
199
         com.sun.star.frame.DispatchDescriptor[] seqDescriptors )
 
200
    {
 
201
        int nCount = seqDescriptors.length;
 
202
        com.sun.star.frame.XDispatch[] seqDispatcher =
 
203
            new com.sun.star.frame.XDispatch[seqDescriptors.length];
 
204
 
 
205
        for( int i=0; i < nCount; ++i )
 
206
        {
 
207
            seqDispatcher[i] = queryDispatch(seqDescriptors[i].FeatureURL,
 
208
                                             seqDescriptors[i].FrameName,
 
209
                                             seqDescriptors[i].SearchFlags );
 
210
        }
 
211
        return seqDispatcher;
 
212
    }
 
213
 
 
214
}