--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.selectionview;\r
+\r
+import java.util.concurrent.atomic.AtomicBoolean;\r
+\r
+import org.eclipse.jface.viewers.ISelectionProvider;\r
+import org.eclipse.swt.events.DisposeEvent;\r
+import org.eclipse.swt.events.DisposeListener;\r
+import org.eclipse.swt.widgets.Composite;\r
+import org.eclipse.swt.widgets.Control;\r
+import org.eclipse.ui.IWorkbenchSite;\r
+import org.simantics.browsing.ui.common.ErrorLogger;\r
+import org.simantics.db.common.procedure.adapter.ListenerSupport;\r
+import org.simantics.db.management.ISessionContext;\r
+\r
+/**\r
+ * Override {@link #createControls(Composite, ISessionContext)} to create\r
+ * controls for your own tab.\r
+ * \r
+ * <p>\r
+ * This class implements {@link ListenerSupport} to help in dealing with graph\r
+ * request listeners.\r
+ * \r
+ * <p>\r
+ * This adapter has the following default implementations for IPropertyTab:\r
+ * <ul>\r
+ * <li>{@link #getSelectionProvider()} - returns null, i.e. does not provide any\r
+ * selection to other UI/Workbench parts.</li>\r
+ * <li>{@link #requestFocus()} - calls {@link Control#setFocus()} for\r
+ * {@link #getControl()} if a control exists</li>\r
+ * <li>{@link #isDisposed()} - returns <code>false</code> by default and\r
+ * <code>true</code> if {@link #setDisposed()} has been called</li>\r
+ * </ul>\r
+ * \r
+ * @author Tuukka Lehtonen\r
+ * \r
+ * @see PropertyTabContributorImpl\r
+ */\r
+public abstract class PropertyTabAdapter implements IPropertyTab2, ListenerSupport {\r
+\r
+ protected IWorkbenchSite site;\r
+\r
+ private final AtomicBoolean disposed = new AtomicBoolean();\r
+\r
+ public PropertyTabAdapter(IWorkbenchSite site) {\r
+ this.site = site;\r
+ }\r
+\r
+\r
+ /**\r
+ * Override this implementation and call super.createControl(parent) as the\r
+ * last thing in your property table.\r
+ * \r
+ * @see org.simantics.selectionview.IPropertyTab#createControl(org.eclipse.swt.widgets.Composite)\r
+ */\r
+ @Override\r
+ public final void createControl(Composite parent, ISessionContext context) {\r
+ createControls(parent, context);\r
+ Control control = getControl();\r
+\r
+ if (control == null || control.isDisposed())\r
+ return;\r
+\r
+ control.addDisposeListener(new DisposeListener() {\r
+ @Override\r
+ public void widgetDisposed(DisposeEvent e) {\r
+ setDisposed();\r
+ }\r
+ });\r
+ }\r
+\r
+ /**\r
+ * Override this implementation to create the tab's controls.\r
+ * \r
+ * @see #createControl(Composite, ISessionContext)\r
+ */\r
+ public void createControls(Composite parent, ISessionContext context) {\r
+ Control control = getControl();\r
+ if (control == null || control.isDisposed())\r
+ return;\r
+\r
+ control.addDisposeListener(new DisposeListener() {\r
+ @Override\r
+ public void widgetDisposed(DisposeEvent e) {\r
+ setDisposed();\r
+ }\r
+ });\r
+ }\r
+\r
+ @Override\r
+ public void dispose() {\r
+ }\r
+\r
+ /**\r
+ * @return <code>true</code> if tab was marked disposed, <code>false</code>\r
+ * if it was already marked disposed\r
+ */\r
+ protected boolean setDisposed() {\r
+ return disposed.compareAndSet(false, true);\r
+ }\r
+\r
+ @Override\r
+ public boolean isDisposed() {\r
+ return disposed.get();\r
+ }\r
+\r
+ @Override\r
+ public ISelectionProvider getSelectionProvider() {\r
+ return null;\r
+ }\r
+\r
+ @Override\r
+ public void requestFocus() {\r
+ Control control = getControl();\r
+ if (control == null || control.isDisposed())\r
+ return;\r
+\r
+ control.setFocus();\r
+ }\r
+\r
+ @Override\r
+ public void exception(Throwable t) {\r
+ ErrorLogger.defaultLogError("PropertyTabAdapter received unexpected exception.", t);\r
+ }\r
+\r
+}\r