package de.tutorials.training.swt;
import org.eclipse.swt.SWT;
import org.eclipse.swt.ole.win32.OLE;
import org.eclipse.swt.ole.win32.OleAutomation;
import org.eclipse.swt.ole.win32.OleClientSite;
import org.eclipse.swt.ole.win32.OleFrame;
import org.eclipse.swt.ole.win32.Variant;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
class Demo {
private static final String REGEX_QUOTED_STRING_ARGUMENT = "^'.*'$";
private static final String REGEX_EXTRACT_FUNCTION_ARGUMENTS = ".*\\((.*)\\).*";
void run() {
Display display = new Display();
Shell shell = new Shell(display);
OleFrame frm = new OleFrame(shell, SWT.NONE);
OleClientSite site = new OleClientSite(frm, SWT.NONE, "Outlook.Application");
site.doVerb(OLE.OLEIVERB_INPLACEACTIVATE);
OleAutomation auto = new OleAutomation(site);
addNewOutlookCategory(auto, "test1234");
while (!display.readAndDispatch()) {
display.sleep();
}
shell.dispose();
}
private void addNewOutlookCategory(OleAutomation auto, String newCategoryName) {
Variant categories = evalPropertyPathExpression(auto, "GetNamespace('MAPI').Categories");
if (!isCategoryNameAlreadyTaken(newCategoryName, categories)) {
Variant result = evalPropertyPathExpression(categories.getAutomation(), "Add('" + newCategoryName + "')"); // ...color,
// shortcut
// keys...
System.out.println("Added new category: " + newCategoryName + " -> " + result);
} else {
System.err.println("Could not create category:" + newCategoryName + " name is already taken!");
}
}
private boolean isCategoryNameAlreadyTaken(String newCategoryName, Variant categories) {
boolean categoryNameAlreadyTaken = false;
Variant categoriesCount = evalPropertyPathExpression(categories.getAutomation(), "Count");
for (int i = 0; i < categoriesCount.getInt(); i++) {
Variant category = evalPropertyPathExpression(categories.getAutomation(), "Item(" + (i + 1) + ")");
Variant categoryName = evalPropertyPathExpression(category.getAutomation(), "Name");
categoryNameAlreadyTaken = categoryName.getString().trim().toUpperCase()
.equals(newCategoryName.trim().toUpperCase());
if (categoryNameAlreadyTaken) {
break;
}
}
return categoryNameAlreadyTaken;
}
private Variant evalPropertyPathExpression(OleAutomation auto, String propertyPath) {
Variant result = null;
OleAutomation currentAuto = auto;
String[] propertyPathElements = propertyPath.split("\\.");
for (int i = 0; i < propertyPathElements.length; i++) {
String propertyPathElement = propertyPathElements[i];
if (isFunctionExpression(propertyPathElement)) {
result = evalFunctionExpression(currentAuto, propertyPathElement);
} else {
result = evalPropertyExpression(currentAuto, propertyPathElement);
}
if (result != null && result.getType() == OLE.VT_DISPATCH) {
currentAuto = result.getAutomation();
}
}
return result;
}
private Variant evalPropertyExpression(OleAutomation currentAuto, String propertyPathElement) {
String propertyName = propertyPathElement;
int propertyId = getIdForName(currentAuto, propertyName);
Variant result = currentAuto.getProperty(propertyId);
return result;
}
private Variant evalFunctionExpression(OleAutomation currentAuto, String propertyPathElement) {
String propertyName = propertyPathElement.substring(0, propertyPathElement.indexOf('('));
int propertyId = getIdForName(currentAuto, propertyName);
String argsString = propertyPathElement.replaceAll(REGEX_EXTRACT_FUNCTION_ARGUMENTS, "$1");
Object[] args = convertArgsStringToObjectArgs(argsString);
Variant[] varArgs = convertObjectArgsToVariantArgs(args);
Variant result = currentAuto.invoke(propertyId, varArgs);
return result;
}
private Object[] convertArgsStringToObjectArgs(String argsString) {
String[] argsStrings = argsString.split(",");
Object[] args = null;
if (argsStrings.length > 0) {
args = new Object[argsStrings.length];
for (int a = 0; a < argsStrings.length; a++) {
String argValue = argsStrings[a].trim();
if (argValue.matches(REGEX_QUOTED_STRING_ARGUMENT)) {
args[a] = argValue.substring(1, argValue.length() - 1);
} else {
args[a] = Integer.valueOf(argValue);
}
}
}
return args;
}
private Variant[] convertObjectArgsToVariantArgs(Object[] args) {
Variant[] varArgs = new Variant[args.length];
for (int v = 0; v < varArgs.length; v++) {
varArgs[v] = args[v] instanceof String ? new Variant((String) args[v]) : new Variant((Integer) args[v]);
}
return varArgs;
}
private boolean isFunctionExpression(String propertyPathElement) {
return propertyPathElement.matches(".*\\(.*\\).*");
}
int getIdForName(OleAutomation auto, String name) {
int[] ids = auto.getIDsOfNames(new String[] { name });
return ids[0];
}
}
public class AddOutlookAppointmentCategoriesViaOLEExample {
public static void main(String[] args) {
new Demo().run();
}
}