Refactor SWT UX.
[lgpl/argeo-commons.git] / eclipse / org.argeo.cms.swt / src / org / argeo / cms / swt / widgets / AbstractSwtPart.java
index e846bb54e13db47886e04f34ac460590157b6d41..c3d11a181124774d66b63bc1912d9a833a2fb9d1 100644 (file)
@@ -1,14 +1,45 @@
 package org.argeo.cms.swt.widgets;
 
 import org.argeo.cms.swt.CmsSwtUtils;
+import org.argeo.cms.ux.widgets.DataPart;
+import org.argeo.cms.ux.widgets.DataView;
+import org.eclipse.swt.events.SelectionEvent;
+import org.eclipse.swt.events.SelectionListener;
 import org.eclipse.swt.widgets.Composite;
 
-public abstract class AbstractSwtPart {
-       protected final Composite area;
+public abstract class AbstractSwtPart<INPUT, TYPE> extends Composite implements DataView<INPUT, TYPE> {
+       private static final long serialVersionUID = -1999179054267812170L;
 
-       public AbstractSwtPart(Composite parent, int style) {
-               area = new Composite(parent, style);
-               area.setLayout(CmsSwtUtils.noSpaceGridLayout());
+       protected DataPart<INPUT, TYPE> dataPart;
+
+       protected final SelectionListener selectionListener;
+
+       public AbstractSwtPart(Composite parent, int style, DataPart<INPUT, TYPE> dataPart) {
+               super(parent, style);
+               setLayout(CmsSwtUtils.noSpaceGridLayout());
+
+               this.dataPart = dataPart;
+
+               selectionListener = new SelectionListener() {
+
+                       private static final long serialVersionUID = 4334785560035009330L;
+
+                       @Override
+                       public void widgetSelected(SelectionEvent e) {
+                               if (dataPart.getOnSelected() != null)
+                                       dataPart.getOnSelected().accept((TYPE) e.item.getData());
+                       }
+
+                       @Override
+                       public void widgetDefaultSelected(SelectionEvent e) {
+                               if (dataPart.getOnAction() != null)
+                                       dataPart.getOnAction().accept((TYPE) e.item.getData());
+                       }
+               };
+
+               dataPart.addView(this);
+               addDisposeListener((e) -> dataPart.removeView(this));
        }
 
+       public abstract void refresh();
 }