Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorpnehrer2006-07-05 02:11:18 +0000
committerpnehrer2006-07-05 02:11:18 +0000
commitac7782c0e2e05c8e41ba662c98fe5cd88cc9f612 (patch)
tree5645f0b7fe00af876a782a67c4ad6c173064439f /examples/bundles/org.eclipse.ecf.example.pubsub/src/org/eclipse/ecf/example
parentb7547bc2af999d6982bab3fc0ab91dfa07499f87 (diff)
downloadorg.eclipse.ecf-ac7782c0e2e05c8e41ba662c98fe5cd88cc9f612.tar.gz
org.eclipse.ecf-ac7782c0e2e05c8e41ba662c98fe5cd88cc9f612.tar.xz
org.eclipse.ecf-ac7782c0e2e05c8e41ba662c98fe5cd88cc9f612.zip
Initial check-in.
Diffstat (limited to 'examples/bundles/org.eclipse.ecf.example.pubsub/src/org/eclipse/ecf/example')
-rw-r--r--examples/bundles/org.eclipse.ecf.example.pubsub/src/org/eclipse/ecf/example/pubsub/Activator.java60
-rw-r--r--examples/bundles/org.eclipse.ecf.example.pubsub/src/org/eclipse/ecf/example/pubsub/AppendableList.java75
-rw-r--r--examples/bundles/org.eclipse.ecf.example.pubsub/src/org/eclipse/ecf/example/pubsub/IAppendableListListener.java16
-rw-r--r--examples/bundles/org.eclipse.ecf.example.pubsub/src/org/eclipse/ecf/example/pubsub/ListAppender.java22
-rw-r--r--examples/bundles/org.eclipse.ecf.example.pubsub/src/org/eclipse/ecf/example/pubsub/PubSubView.java270
-rw-r--r--examples/bundles/org.eclipse.ecf.example.pubsub/src/org/eclipse/ecf/example/pubsub/SubscriptionView.java81
6 files changed, 524 insertions, 0 deletions
diff --git a/examples/bundles/org.eclipse.ecf.example.pubsub/src/org/eclipse/ecf/example/pubsub/Activator.java b/examples/bundles/org.eclipse.ecf.example.pubsub/src/org/eclipse/ecf/example/pubsub/Activator.java
new file mode 100644
index 000000000..643da6e8d
--- /dev/null
+++ b/examples/bundles/org.eclipse.ecf.example.pubsub/src/org/eclipse/ecf/example/pubsub/Activator.java
@@ -0,0 +1,60 @@
+/**
+ * Copyright (c) 2006 Ecliptical Software Inc. and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Ecliptical Software Inc. - initial API and implementation
+ */
+package org.eclipse.ecf.example.pubsub;
+
+import org.eclipse.ui.plugin.AbstractUIPlugin;
+import org.osgi.framework.BundleContext;
+
+/**
+ * The activator class controls the plug-in life cycle
+ */
+public class Activator extends AbstractUIPlugin {
+
+ // The plug-in ID
+ public static final String PLUGIN_ID = "org.eclipse.ecf.example.pubsub";
+
+ // The shared instance
+ private static Activator plugin;
+
+ /**
+ * The constructor
+ */
+ public Activator() {
+ plugin = this;
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext)
+ */
+ public void start(BundleContext context) throws Exception {
+ super.start(context);
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext)
+ */
+ public void stop(BundleContext context) throws Exception {
+ plugin = null;
+ super.stop(context);
+ }
+
+ /**
+ * Returns the shared instance
+ *
+ * @return the shared instance
+ */
+ public static Activator getDefault() {
+ return plugin;
+ }
+
+}
diff --git a/examples/bundles/org.eclipse.ecf.example.pubsub/src/org/eclipse/ecf/example/pubsub/AppendableList.java b/examples/bundles/org.eclipse.ecf.example.pubsub/src/org/eclipse/ecf/example/pubsub/AppendableList.java
new file mode 100644
index 000000000..b164487c7
--- /dev/null
+++ b/examples/bundles/org.eclipse.ecf.example.pubsub/src/org/eclipse/ecf/example/pubsub/AppendableList.java
@@ -0,0 +1,75 @@
+/**
+ * Copyright (c) 2006 Ecliptical Software Inc. and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Ecliptical Software Inc. - initial API and implementation
+ */
+package org.eclipse.ecf.example.pubsub;
+
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.eclipse.core.runtime.ISafeRunnable;
+import org.eclipse.core.runtime.ListenerList;
+import org.eclipse.core.runtime.SafeRunner;
+
+public class AppendableList implements Serializable {
+
+ private static final long serialVersionUID = -5447897626712251185L;
+
+ private transient ListenerList listeners;
+
+ protected final List values = new ArrayList();
+
+ public void addListener(IAppendableListListener listener) {
+ getListenerList().add(listener);
+ }
+
+ public void removeListener(IAppendableListListener listener) {
+ getListenerList().remove(listener);
+ }
+
+ protected void fireAppended(final Object value) {
+ Object[] l = getListenerList().getListeners();
+ for (int i = 0; i < l.length; ++i) {
+ final IAppendableListListener listener = (IAppendableListListener) l[i];
+ SafeRunner.run(new ISafeRunnable() {
+
+ public void run() throws Exception {
+ listener.appended(AppendableList.this, value);
+ }
+
+ public void handleException(Throwable exception) {
+ // TODO Auto-generated method stub
+
+ }
+ });
+ }
+ }
+
+ protected synchronized ListenerList getListenerList() {
+ if (listeners == null)
+ listeners = new ListenerList();
+
+ return listeners;
+ }
+
+ public synchronized Object[] getValues() {
+ return values.toArray();
+ }
+
+ public synchronized boolean add(Object value) {
+ boolean result = values.add(value);
+ fireAppended(value);
+ return result;
+ }
+
+ public String toString() {
+ return values.toString();
+ }
+}
diff --git a/examples/bundles/org.eclipse.ecf.example.pubsub/src/org/eclipse/ecf/example/pubsub/IAppendableListListener.java b/examples/bundles/org.eclipse.ecf.example.pubsub/src/org/eclipse/ecf/example/pubsub/IAppendableListListener.java
new file mode 100644
index 000000000..57b21e99a
--- /dev/null
+++ b/examples/bundles/org.eclipse.ecf.example.pubsub/src/org/eclipse/ecf/example/pubsub/IAppendableListListener.java
@@ -0,0 +1,16 @@
+/**
+ * Copyright (c) 2006 Ecliptical Software Inc. and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Ecliptical Software Inc. - initial API and implementation
+ */
+package org.eclipse.ecf.example.pubsub;
+
+public interface IAppendableListListener {
+
+ void appended(AppendableList list, Object value);
+}
diff --git a/examples/bundles/org.eclipse.ecf.example.pubsub/src/org/eclipse/ecf/example/pubsub/ListAppender.java b/examples/bundles/org.eclipse.ecf.example.pubsub/src/org/eclipse/ecf/example/pubsub/ListAppender.java
new file mode 100644
index 000000000..b4f6c6bd0
--- /dev/null
+++ b/examples/bundles/org.eclipse.ecf.example.pubsub/src/org/eclipse/ecf/example/pubsub/ListAppender.java
@@ -0,0 +1,22 @@
+/**
+ * Copyright (c) 2006 Ecliptical Software Inc. and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Ecliptical Software Inc. - initial API and implementation
+ */
+package org.eclipse.ecf.example.pubsub;
+
+import org.eclipse.ecf.pubsub.model.IModelUpdater;
+
+public class ListAppender implements IModelUpdater {
+
+ public static final String ID = "org.eclipse.ecf.example.pubsub.ListAppender";
+
+ public void update(Object data, Object update) {
+ ((AppendableList) data).add(update);
+ }
+}
diff --git a/examples/bundles/org.eclipse.ecf.example.pubsub/src/org/eclipse/ecf/example/pubsub/PubSubView.java b/examples/bundles/org.eclipse.ecf.example.pubsub/src/org/eclipse/ecf/example/pubsub/PubSubView.java
new file mode 100644
index 000000000..d4e9f9ab2
--- /dev/null
+++ b/examples/bundles/org.eclipse.ecf.example.pubsub/src/org/eclipse/ecf/example/pubsub/PubSubView.java
@@ -0,0 +1,270 @@
+/**
+ * Copyright (c) 2006 Ecliptical Software Inc. and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Ecliptical Software Inc. - initial API and implementation
+ */
+package org.eclipse.ecf.example.pubsub;
+
+import java.io.IOException;
+import java.util.Vector;
+
+import org.eclipse.ecf.core.ISharedObjectContainer;
+import org.eclipse.ecf.core.SharedObjectContainerFactory;
+import org.eclipse.ecf.core.SharedObjectCreateException;
+import org.eclipse.ecf.core.identity.IDFactory;
+import org.eclipse.ecf.core.identity.IDInstantiationException;
+import org.eclipse.ecf.core.util.ECFException;
+import org.eclipse.ecf.pubsub.IPublishedServiceDirectory;
+import org.eclipse.ecf.pubsub.IPublishedServiceDirectoryListener;
+import org.eclipse.ecf.pubsub.IPublishedServiceRequestor;
+import org.eclipse.ecf.pubsub.ISubscription;
+import org.eclipse.ecf.pubsub.ISubscriptionCallback;
+import org.eclipse.ecf.pubsub.PublishedServiceDescriptor;
+import org.eclipse.ecf.pubsub.PublishedServiceDirectoryChangeEvent;
+import org.eclipse.ecf.pubsub.model.IMasterModel;
+import org.eclipse.ecf.pubsub.model.SharedModelFactory;
+import org.eclipse.jface.action.Action;
+import org.eclipse.jface.action.IMenuManager;
+import org.eclipse.jface.action.MenuManager;
+import org.eclipse.jface.dialogs.ErrorDialog;
+import org.eclipse.jface.dialogs.InputDialog;
+import org.eclipse.jface.dialogs.MessageDialog;
+import org.eclipse.jface.viewers.ArrayContentProvider;
+import org.eclipse.jface.viewers.IStructuredContentProvider;
+import org.eclipse.jface.viewers.IStructuredSelection;
+import org.eclipse.jface.viewers.LabelProvider;
+import org.eclipse.jface.viewers.TableViewer;
+import org.eclipse.jface.viewers.Viewer;
+import org.eclipse.jface.viewers.ViewerSorter;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.layout.GridData;
+import org.eclipse.swt.layout.GridLayout;
+import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Control;
+import org.eclipse.swt.widgets.Display;
+import org.eclipse.swt.widgets.Label;
+import org.eclipse.ui.IViewSite;
+import org.eclipse.ui.IWorkbenchPage;
+import org.eclipse.ui.PartInitException;
+import org.eclipse.ui.PlatformUI;
+import org.eclipse.ui.actions.BaseSelectionListenerAction;
+import org.eclipse.ui.part.ViewPart;
+
+public class PubSubView extends ViewPart {
+
+ protected TableViewer viewer;
+
+ protected MenuManager menuManager;
+
+ protected ISharedObjectContainer container;
+
+ private BaseSelectionListenerAction subscribeAction;
+
+ protected TableViewer sharedListViewer;
+
+ protected MenuManager sharedListMenuManager;
+
+ private BaseSelectionListenerAction appendAction;
+
+ protected final Vector sharedLists = new Vector();
+
+ public void init(IViewSite site) throws PartInitException {
+ super.init(site);
+
+ final Action shareSomethingAction = new Action("Share something...") {
+ public void run() {
+ try {
+ IMasterModel sds = SharedModelFactory.getInstance().createSharedDataSource(container, IDFactory.getDefault().createGUID(), new AppendableList(), ListAppender.ID);
+ if (sds == null)
+ MessageDialog.openError(getSite().getShell(), "Error", "Could not share anything.");
+ else {
+ sharedLists.add(sds);
+ sharedListViewer.add(sds);
+ }
+ } catch (SharedObjectCreateException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } catch (IDInstantiationException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+ };
+
+ shareSomethingAction.setEnabled(false);
+
+ IMenuManager mgr = site.getActionBars().getMenuManager();
+ mgr.add(new Action("Connect") {
+ public void run() {
+ try {
+ container = SharedObjectContainerFactory.getDefault().createSharedObjectContainer("ecf.generic.client");
+ container.connect(IDFactory.getDefault().createStringID("ecftcp://localhost:3282/server"), null);
+ IPublishedServiceDirectory directory = (IPublishedServiceDirectory) container.getAdapter(IPublishedServiceDirectory.class);
+ viewer.setInput(directory);
+ setEnabled(false);
+ shareSomethingAction.setEnabled(true);
+ } catch (ECFException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+ });
+
+ mgr.add(shareSomethingAction);
+
+ menuManager = new MenuManager();
+ subscribeAction = new BaseSelectionListenerAction("Subscribe") {
+
+ public void run() {
+ PublishedServiceDescriptor desc = (PublishedServiceDescriptor) getStructuredSelection().getFirstElement();
+ IPublishedServiceRequestor requestor = (IPublishedServiceRequestor) container.getAdapter(IPublishedServiceRequestor.class);
+ requestor.subscribe(desc.getContainerID(), desc.getSharedObjectID(), new SubscriptionViewOpener());
+ }
+
+ protected boolean updateSelection(IStructuredSelection selection) {
+ return !selection.isEmpty();
+ }
+ };
+
+ subscribeAction.setEnabled(false);
+ menuManager.add(subscribeAction);
+
+ sharedListMenuManager = new MenuManager();
+ appendAction = new BaseSelectionListenerAction("Append...") {
+
+ public void run() {
+ InputDialog dlg = new InputDialog(getSite().getShell(), "Append to Shared List", "Enter element to append:", null, null);
+ dlg.open();
+ String value = dlg.getValue();
+ if (value != null) {
+ IMasterModel list = (IMasterModel) getStructuredSelection().getFirstElement();
+ try {
+ list.update(value);
+ } catch (IOException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+ }
+
+ protected boolean updateSelection(IStructuredSelection selection) {
+ return !selection.isEmpty();
+ }
+ };
+
+ appendAction.setEnabled(false);
+ sharedListMenuManager.add(appendAction);
+ }
+
+ public void createPartControl(Composite parent) {
+ Composite composite = new Composite(parent, SWT.NONE);
+ composite.setLayout(new GridLayout(2, true));
+
+ Label label = new Label(composite, SWT.NONE);
+ label.setText("Remote Shared Services");
+
+ label = new Label(composite, SWT.NONE);
+ label.setText("Local Shared Sample Lists");
+
+ viewer = new TableViewer(composite);
+ viewer.getTable().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
+ viewer.setUseHashlookup(true);
+ viewer.setLabelProvider(new LabelProvider());
+ viewer.setContentProvider(new ContentProvider());
+ viewer.setSorter(new ViewerSorter());
+ viewer.addSelectionChangedListener(subscribeAction);
+ viewer.getControl().setMenu(menuManager.createContextMenu(viewer.getControl()));
+
+ sharedListViewer = new TableViewer(composite);
+ sharedListViewer.getTable().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
+ sharedListViewer.setUseHashlookup(true);
+ sharedListViewer.setLabelProvider(new LabelProvider());
+ sharedListViewer.setContentProvider(new ArrayContentProvider());
+ sharedListViewer.addSelectionChangedListener(appendAction);
+ sharedListViewer.getControl().setMenu(sharedListMenuManager.createContextMenu(sharedListViewer.getControl()));
+ }
+
+ public void setFocus() {
+ viewer.getControl().setFocus();
+ }
+
+ public void dispose() {
+ menuManager.dispose();
+
+ if (container != null)
+ container.disconnect();
+
+ super.dispose();
+ }
+
+ protected class ContentProvider implements IStructuredContentProvider, IPublishedServiceDirectoryListener {
+
+ private Viewer viewer;
+
+ public Object[] getElements(Object inputElement) {
+ return new Object[0];
+ }
+
+ public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
+ this.viewer = viewer;
+
+ if (oldInput instanceof IPublishedServiceDirectory)
+ ((IPublishedServiceDirectory) oldInput).removeReplicatedServiceListener(this);
+
+ if (newInput instanceof IPublishedServiceDirectory)
+ ((IPublishedServiceDirectory) newInput).addReplicatedServiceListener(this);
+ }
+
+ public void dispose() {
+ viewer = null;
+ }
+
+ public void publishedServiceDirectoryChanged(final PublishedServiceDirectoryChangeEvent event) {
+ if (viewer instanceof TableViewer) {
+ Control ctrl = viewer == null ? null : viewer.getControl();
+ if (ctrl != null && !ctrl.isDisposed())
+ ctrl.getDisplay().asyncExec(new Runnable() {
+ public void run() {
+ TableViewer tableViewer = (TableViewer) viewer;
+ if (event.getKind() == PublishedServiceDirectoryChangeEvent.ADDED)
+ tableViewer.add(event.getReplicatedServices());
+ else
+ tableViewer.remove(event.getReplicatedServices());
+ }
+ });
+ }
+ }
+ }
+
+ protected class SubscriptionViewOpener implements ISubscriptionCallback {
+
+ public void subscribed(final ISubscription subscription) {
+ Display.getDefault().asyncExec(new Runnable() {
+ public void run() {
+ SubscriptionView view;
+ try {
+ view = (SubscriptionView) PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().showView(SubscriptionView.VIEW_ID, subscription.getID().getName(), IWorkbenchPage.VIEW_ACTIVATE);
+ } catch (PartInitException e) {
+ ErrorDialog.openError(getSite().getShell(), "Subscription Error", "Could not create subscription view.", e.getStatus());
+ return;
+ }
+
+ view.setSubscription(container, subscription);
+ }
+ });
+ }
+
+ public void subscriptionFailed(final Throwable t) {
+ Display.getDefault().asyncExec(new Runnable() {
+ public void run() {
+ MessageDialog.openError(getSite().getShell(), "Subscription Error", t.getLocalizedMessage());
+ }
+ });
+ }
+ }
+}
diff --git a/examples/bundles/org.eclipse.ecf.example.pubsub/src/org/eclipse/ecf/example/pubsub/SubscriptionView.java b/examples/bundles/org.eclipse.ecf.example.pubsub/src/org/eclipse/ecf/example/pubsub/SubscriptionView.java
new file mode 100644
index 000000000..653a3e22f
--- /dev/null
+++ b/examples/bundles/org.eclipse.ecf.example.pubsub/src/org/eclipse/ecf/example/pubsub/SubscriptionView.java
@@ -0,0 +1,81 @@
+/**
+ * Copyright (c) 2006 Ecliptical Software Inc. and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Ecliptical Software Inc. - initial API and implementation
+ */
+package org.eclipse.ecf.example.pubsub;
+
+import java.io.PrintWriter;
+import java.io.StringWriter;
+
+import org.eclipse.ecf.core.ISharedObjectContainer;
+import org.eclipse.ecf.pubsub.ISubscription;
+import org.eclipse.ecf.pubsub.model.IReplicaModel;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Display;
+import org.eclipse.swt.widgets.Text;
+import org.eclipse.ui.part.ViewPart;
+
+public class SubscriptionView extends ViewPart implements IAppendableListListener {
+
+ public static final String VIEW_ID = "org.eclipse.ecf.example.pubsub.subscription";
+
+ protected ISubscription subscription;
+
+ protected AppendableList model;
+
+ protected Text text;
+
+ public synchronized void setSubscription(ISharedObjectContainer container, ISubscription subscription) {
+ this.subscription = subscription;
+ setPartName("Subscription: " + subscription.getID());
+ Object object = container.getSharedObjectManager().getSharedObject(subscription.getID());
+ if (object instanceof IReplicaModel) {
+ Object data = ((IReplicaModel) object).getData();
+ if (data instanceof AppendableList) {
+ model = (AppendableList) data;
+ model.addListener(this);
+ Object[] values = model.getValues();
+ StringWriter buf = new StringWriter();
+ PrintWriter writer = new PrintWriter(buf);
+ for (int i = 0; i < values.length; ++i)
+ writer.println(values[i]);
+
+ writer.close();
+ text.setText(buf.toString());
+ }
+ }
+ }
+
+ public void createPartControl(Composite parent) {
+ text = new Text(parent, SWT.WRAP | SWT.READ_ONLY);
+ }
+
+ public void setFocus() {
+ text.setFocus();
+ }
+
+ public void dispose() {
+ if (model != null)
+ model.removeListener(this);
+
+ if (subscription != null)
+ subscription.dispose();
+
+ super.dispose();
+ }
+
+ public synchronized void appended(AppendableList list, final Object value) {
+ Display.getDefault().asyncExec(new Runnable() {
+ public void run() {
+ text.append(String.valueOf(value) + System.getProperty("line.separator"));
+ }
+ });
+ }
+}

Back to the top