~ldd/oowebdict/trunk

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package com.lddubeau.oowebdict.comp;

import java.net.MalformedURLException;

import com.lddubeau.ddb.CancelledByUser;
import com.lddubeau.ddb.Index;
import com.lddubeau.ddb.ProgressMonitor;
import com.lddubeau.oowebdict.LastBuild;
import com.sun.star.lang.XSingleComponentFactory;
import com.sun.star.lib.uno.helper.Factory;
import com.sun.star.lib.uno.helper.WeakBase;
import com.sun.star.registry.XRegistryKey;
import com.sun.star.uno.Exception;
import com.sun.star.uno.XComponentContext;

public final class IndexImpl extends WeakBase
		implements
			com.sun.star.lang.XServiceInfo,
			com.lddubeau.oowebdict.XIndex,
			com.sun.star.lang.XInitialization
{
	private final XComponentContext m_xContext;

	private static final String m_implementationName = IndexImpl.class
			.getName();

	protected static final String __serviceName = "com.lddubeau.oowebdict.Index";

	private Index index = null;

	public IndexImpl(XComponentContext context)
	{
		m_xContext = context;
	};

	public static XSingleComponentFactory __getComponentFactory(
			String sImplementationName)
	{
		XSingleComponentFactory xFactory = null;

		if (sImplementationName.equals(m_implementationName))
			xFactory = Factory.createComponentFactory(IndexImpl.class,
					new String[]{__serviceName});
		return xFactory;
	}

	public static boolean __writeRegistryServiceInfo(XRegistryKey xRegistryKey)
	{
		return Factory.writeRegistryServiceInfo(m_implementationName,
				new String[]{__serviceName}, xRegistryKey);
	}

	// com.sun.star.lang.XServiceInfo:
	public String getImplementationName()
	{
		return m_implementationName;
	}

	public boolean supportsService(String sService)
	{
		return sService.equals(__serviceName);
	}

	public String [] getSupportedServiceNames()
	{
		return new String[]{__serviceName};
	}

	// com.lddubeau.oowebdict.XIndex:
	public boolean exists(String term)
			throws com.lddubeau.oowebdict.CancelledByUser
	{
		try
		{
			return this.index.exists(term);
		}
		catch (CancelledByUser e)
		{
			throw new com.lddubeau.oowebdict.CancelledByUser();
		}
	}

	public int getLongestTermLength()
			throws com.lddubeau.oowebdict.CancelledByUser

	{
		try
		{
			return this.index.getLongestTermLength();
		}
		catch (CancelledByUser e)
		{
			throw new com.lddubeau.oowebdict.CancelledByUser();
		}
	}

	public String getBuildTime()
	{
		return LastBuild.getTime();
	}

	public String getVersion()
	{
		return LastBuild.getVersion();
	}

	public int getMajor()
	{
		return LastBuild.getMajor();
	}

	public int getMinor()
	{
		return LastBuild.getMinor();
	}

	public String getVersions()
	{
		return Index.getVersions() + "\nOOWebDict version: " + getVersion();
	}

	// com.sun.star.lang.XInitialization:
	public void initialize(Object [] aArguments)
			throws com.sun.star.uno.Exception
	{
		try
		{
			String url = (String) aArguments[0];
			this.index = new Index(url, new ProgressGlue(url));
		}
		catch (MalformedURLException ex)
		{
			throw new Exception("bad URL " + (String) aArguments[0] + " "
					+ ex.toString());
		}
	}

	static final class ProgressGlue implements ProgressMonitor
	{
		private final javax.swing.ProgressMonitor monitor;

		public ProgressGlue(String url)
		{
			this.monitor = new javax.swing.ProgressMonitor(null,
					"Loading from: " + url, "", 0, 0);
		}

		public void close()
		{
			this.monitor.close();
		}

		public boolean isCancelled()
		{
			return this.monitor.isCanceled();
		}

		public void setMaximum(int m)
		{
			this.monitor.setMaximum(m);
		}

		public void setMinimum(int m)
		{
			this.monitor.setMinimum(m);
		}

		public void setProgress(int m)
		{
			this.monitor.setProgress(m);
		}
	}

}