Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 409c43d3a5d597bd2f76c8461cd063d23804676a (plain) (blame)
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*******************************************************************************
 * Copyright (c) 2007, 2018 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.swt.examples.ole.win32;

import java.io.*;

import org.eclipse.swt.*;
import org.eclipse.swt.events.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.ole.win32.*;
import org.eclipse.swt.widgets.*;

/**
 * OLEExample is an example that uses <code>org.eclipse.swt</code> 
 * libraries to implement a simple SWT window that can host different Active X
 * controls.
 *
 * @since 3.3
 */ 
public class OLEExample {

	OleClientSite clientSite;
	OleFrame oleFrame;
	Button closeButton;
	boolean createClientSite = true; // to create OleControlSite, set to false.
	
	public static void main(String[] args) {
		Display display = new Display();
		OLEExample example = new OLEExample();
		example.open(display);
		display.dispose();
	}

	/** 
	 * Create a file Exit menu item 
	 */
	void addFileMenu(OleFrame frame) {
		final Shell shell = frame.getShell();
		Menu menuBar = shell.getMenuBar();
		if (menuBar == null) {
			menuBar = new Menu(shell, SWT.BAR);
			shell.setMenuBar(menuBar);
		}
		MenuItem fileMenu = new MenuItem(menuBar, SWT.CASCADE);
		fileMenu.setText("&File");
		Menu menuFile = new Menu(fileMenu);
		fileMenu.setMenu(menuFile);
		frame.setFileMenus(new MenuItem[] { fileMenu });
		
		MenuItem menuFileExit = new MenuItem(menuFile, SWT.CASCADE);
		menuFileExit.setText("Exit");
		menuFileExit.addSelectionListener(SelectionListener.widgetSelectedAdapter(e -> shell.dispose()));
	}

	OleClientSite createSite(OleFrame frame, String progID) {
		if (createClientSite) {
			return new OleClientSite(frame, SWT.NONE, progID);
		} else {
			return new OleControlSite(frame, SWT.NONE, progID);
		}
	}
	
	OleClientSite createSite(OleFrame frame, String progID, File file) {
		if (createClientSite) {
			return new OleClientSite(frame, SWT.NONE, progID, file);
		} else {
			return new OleControlSite(frame, SWT.NONE, progID, file);
		}
	}
	
	void disposeClient() {
		if (clientSite != null)
			clientSite.dispose();
		clientSite = null;
	}

	/**
	 * Prompt the user for a file and try to open it with some known ActiveX controls.
	 */
	void fileOpen() {
		Shell shell = oleFrame.getShell();
		FileDialog dialog = new FileDialog(shell, SWT.OPEN);
		String fileName = dialog.open();
		if (fileName == null) return;

		disposeClient();

		// try opening a .doc file using Word
		if (clientSite == null) {
			int index = fileName.lastIndexOf('.');
			if (index != -1) {
				String fileExtension = fileName.substring(index + 1);
				if (fileExtension.equalsIgnoreCase("doc") || 
						fileExtension.equalsIgnoreCase("rtf") ||
						fileExtension.equalsIgnoreCase("txt")) {
					try {
						clientSite = createSite(oleFrame, "Word.Document", new File(fileName));
					} catch (SWTException error2) {
						disposeClient();
					}
				}
			}
		}

		// try opening a xls file with Excel
		if (clientSite == null) {
			int index = fileName.lastIndexOf('.');
			if (index != -1) {
				String fileExtension = fileName.substring(index + 1);
				if (fileExtension.equalsIgnoreCase("xls")) {
					try {
						clientSite = createSite(oleFrame, "Excel.Sheet", new File(fileName));
					} catch (SWTException error2) {
						disposeClient();
					}
				}
			}
		}

		// try opening a media file with MPlayer
		if (clientSite == null) {
			int index = fileName.lastIndexOf('.');
			if (index != -1) {
				String fileExtension = fileName.substring(index + 1);
				if (fileExtension.equalsIgnoreCase("mpa")) {
					try {
						clientSite = createSite(oleFrame, "MPlayer", new File(fileName));
					} catch (SWTException error2) {
						disposeClient();
					}
				}
			}
		}

		// try opening with wmv, mpg, mpeg, avi, asf, wav with WMPlayer
		if (clientSite == null) {
			int index = fileName.lastIndexOf('.');
			if (index != -1) {
				String fileExtension = fileName.substring(index + 1);
				if (fileExtension.equalsIgnoreCase("wmv")
						|| fileExtension.equalsIgnoreCase("mpg")
						|| fileExtension.equalsIgnoreCase("mpeg")
						|| fileExtension.equalsIgnoreCase("avi")
						|| fileExtension.equalsIgnoreCase("asf")
						|| fileExtension.equalsIgnoreCase("wav")) {
					try {
						clientSite = createSite(oleFrame, "WMPlayer.OCX");
						OleAutomation player = new OleAutomation(clientSite);
						int playURL[] = player.getIDsOfNames(new String[] { "URL" });
						if (playURL != null) {
							boolean suceeded = player.setProperty(playURL[0], new Variant(fileName));
							if (!suceeded)
								disposeClient();
						} else {
							disposeClient();
						}
						player.dispose();
					} catch (SWTException error2) {
						disposeClient();
					}
				}
			}
		}

		// try opening a PDF file with Acrobat reader
		if (clientSite == null) {
			int index = fileName.lastIndexOf('.');
			if (index != -1) {
				String fileExtension = fileName.substring(index + 1);
				if (fileExtension.equalsIgnoreCase("pdf")) {
					try {
						clientSite = createSite(oleFrame, "PDF.PdfCtrl.5");
						clientSite.doVerb(OLE.OLEIVERB_INPLACEACTIVATE);
					    OleAutomation pdf = new OleAutomation (clientSite);
					    int loadFile[] = pdf.getIDsOfNames (new String [] {"LoadFile"});
					    if (loadFile != null) {
					    	Variant result = pdf.invoke(loadFile[0], new Variant[] {new Variant(fileName)});
							if (result == null)
								disposeClient();
							else
								result.dispose();
					    } else {
							disposeClient();
					    }
					    pdf.dispose();
					} catch (SWTException error2) {
						disposeClient();
					}
				}
			}
		}

		// try opening with Explorer
		if (clientSite == null) {
			try {
				clientSite = createSite(oleFrame, "Shell.Explorer");
				OleAutomation explorer = new OleAutomation(clientSite);
				int[] navigate = explorer.getIDsOfNames(new String[]{"Navigate"}); 
				
				if (navigate != null) {
					Variant result = explorer.invoke(navigate[0], new Variant[] {new Variant(fileName)});
					if (result == null)
						disposeClient();
					else
						result.dispose();
				} else {
					disposeClient();
				}
				explorer.dispose();
			} catch (SWTException error2) {
				disposeClient();
			}
		}

		if (clientSite != null){
			clientSite.doVerb(OLE.OLEIVERB_INPLACEACTIVATE);
		}
	}

	void newClientSite(String progID) {
		disposeClient();
		try {
			clientSite = createSite(oleFrame, progID);
		} catch (SWTException error) {

		}
		if (clientSite != null)
			clientSite.doVerb(OLE.OLEIVERB_INPLACEACTIVATE);
	}

	public void open(Display display) {
		Shell shell = new Shell(display);
		shell.setText("OLE Example");
		shell.setLayout(new FillLayout());

		Composite parent = new Composite(shell, SWT.NONE);
		parent.setLayout(new GridLayout(4, true));
		
		Composite buttons = new Composite(parent, SWT.NONE);
		buttons.setLayout(new GridLayout());
		GridData gridData = new GridData(SWT.BEGINNING, SWT.FILL, false, false);
		buttons.setLayoutData(gridData);
		
		Composite displayArea = new Composite(parent, SWT.BORDER);
		displayArea.setLayout(new FillLayout());
		displayArea.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 3, 1));

		Button excelButton = new Button(buttons, SWT.RADIO);
		excelButton.setText("New Excel Sheet");
		excelButton.addSelectionListener(SelectionListener.widgetSelectedAdapter(e -> {
			if (((Button) e.widget).getSelection())
				newClientSite("Excel.Sheet");
		}));
		Button mediaPlayerButton = new Button(buttons, SWT.RADIO);
		mediaPlayerButton.setText("New MPlayer");
		mediaPlayerButton.addSelectionListener(SelectionListener.widgetSelectedAdapter(e -> {
			if (((Button) e.widget).getSelection())
				newClientSite("MPlayer");
		}));
		Button powerPointButton = new Button(buttons, SWT.RADIO);
		powerPointButton.setText("New PowerPoint Slide");
		powerPointButton.addSelectionListener(SelectionListener.widgetSelectedAdapter(e -> {
			if (((Button) e.widget).getSelection())
				newClientSite("PowerPoint.Slide");
		}));
		Button wordButton = new Button(buttons, SWT.RADIO);
		wordButton.setText("New Word Document");
		wordButton.addSelectionListener(SelectionListener.widgetSelectedAdapter(e -> {
			if (((Button) e.widget).getSelection())
				newClientSite("Word.Document");
		}));
		new Label(buttons, SWT.NONE);
		Button openButton = new Button(buttons, SWT.RADIO);
		openButton.setText("Open file...");
		openButton.addSelectionListener(SelectionListener.widgetSelectedAdapter(e -> {
			if (((Button) e.widget).getSelection())
				fileOpen();
		}));
		new Label(buttons, SWT.NONE);
		closeButton = new Button(buttons, SWT.RADIO);
		closeButton.setText("Close file");
		closeButton.addSelectionListener(SelectionListener.widgetSelectedAdapter(e ->{
				if (((Button) e.widget).getSelection())
					disposeClient();
			}
		));	
		closeButton.setSelection(true);
		
		oleFrame = new OleFrame(displayArea, SWT.NONE);
		addFileMenu(oleFrame);

		shell.setSize(800, 600);
		shell.open();

		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
	}
}

Back to the top