Java tutorial
/******************************************************************************* * Copyright (c) 2014 Joachim Tessmer. * 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: * Joachim Tessmer - initial API and implementation ******************************************************************************/ package de.instanttouch.ui.scaffolding.swt.presenter; import java.util.List; import java.util.Vector; import org.eclipse.jface.dialogs.Dialog; import org.eclipse.jface.viewers.DoubleClickEvent; import org.eclipse.jface.viewers.IDoubleClickListener; import org.eclipse.jface.viewers.ISelection; import org.eclipse.jface.viewers.IStructuredContentProvider; import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.jface.viewers.LabelProvider; import org.eclipse.jface.viewers.ListViewer; import org.eclipse.jface.viewers.Viewer; import org.eclipse.swt.SWT; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Shell; import org.eclipse.ui.PlatformUI; import de.instantouch.model.base.SnakeType; import de.instantouch.model.base.ref.SnakeTypeSelectionCreator; import de.instantouch.model.collections.SnakeMap; import de.instantouch.model.collections.SnakeMapEntry; import de.instantouch.model.exception.SnakeModelException; import de.instantouch.model.exception.SnakeNotInitializedException; import de.instantouch.model.exception.SnakeWrongTypeException; import de.instantouch.model.log.SnakeLog; import de.instanttouch.api.model.ISnakeModification; import de.instanttouch.ui.scaffolding.swt.Activator; import de.instanttouch.ui.scaffolding.swt.action.SnakeAction; import de.instanttouch.ui.scaffolding.swt.dialog.SnakeSelectTypeDialog; import de.instanttouch.ui.scaffolding.swt.error.SnakeExceptionHandler; import de.instanttouch.ui.scaffolding.swt.extensions.SnakeDialogRegistry; import de.instanttouch.ui.scaffolding.swt.viewer.SnakeTableViewer; public class SnakeMapPresenter<K extends SnakeType, V extends SnakeType> extends SnakePresenter { protected SnakeMap<K, V> map = null; private ListViewer elements = null; private SnakeTableViewer tableViewer = null; protected boolean allowOk = true; private boolean bValidate = false; public class MapTableViewer extends SnakeTableViewer { public MapTableViewer(Composite parent, int style, SnakeType type) throws SnakeModelException { super(parent, style, type); } @Override protected void open(SnakeType type) { if (Dialog.OK == mapOpen(type)) { refresh(); } } } public SnakeMapPresenter(Composite parent, int style, SnakeType type, boolean allowOk) { super(parent, style, type); if (!(type instanceof SnakeMap)) { SnakeLog.error("wrong type given: " + type.getClass().getCanonicalName()); this.map = null; } else { this.map = (SnakeMap) type; } this.allowOk = allowOk; } public SnakeMapPresenter(Composite parent, int style, SnakeMap<K, V> map, boolean allowOk) { super(parent, style, map); this.map = map; this.allowOk = allowOk; } @Override public void init(Object layoutData, boolean bValidate) { this.bValidate = bValidate; composite.setLayoutData(layoutData); composite.setLayout(new GridLayout(6, false)); Label label = new Label(composite, SWT.NONE); label.setText(map.getName()); String desc = type.getConstraints().getDescription(); if (desc != null) { label.setToolTipText(desc); } GridData labelLayoutData = new GridData(); // labelLayoutData.widthHint = 100; label.setLayoutData(labelLayoutData); if (type.getConstraints().isEnabled()) { Button configure = new Button(composite, SWT.PUSH); String path = "icons/add.png"; Image image = Activator.getImage(path); configure.setImage(image); configure.setText("add"); configure.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { add(); } }); Button delete = new Button(composite, SWT.PUSH); path = "icons/delete.png"; image = Activator.getImage(path); delete.setImage(image); delete.setText("del"); delete.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent event) { if (tableViewer != null) { ISelection selection = tableViewer.getSelection(); if (selection instanceof IStructuredSelection) { IStructuredSelection structuredSelection = (IStructuredSelection) selection; Object selected = structuredSelection.getFirstElement(); if (selected instanceof SnakeMapEntry) { SnakeMapEntry<K, V> helper = (SnakeMapEntry<K, V>) selected; K type = helper.getKey(); if (type != null) { map.remove(type); refresh(); } } } } else { ISelection selection = elements.getSelection(); if (selection instanceof IStructuredSelection) { IStructuredSelection structuredSelection = (IStructuredSelection) selection; Object selected = structuredSelection.getFirstElement(); if (selected instanceof SnakeMapEntry) { SnakeMapEntry<K, V> helper = (SnakeMapEntry<K, V>) selected; K type = helper.getKey(); if (type != null) { map.remove(type); refresh(); } } } } } }); String count = "(" + map.values().size() + " elements)"; label = new Label(composite, SWT.NONE); label.setText(count); desc = type.getConstraints().getDescription(); if (desc != null) { label.setToolTipText(desc); } } boolean tableCanBeUsed = false; try { SnakeType keyType = map.newKey(); SnakeType valueType = map.newValue(); if (keyType.handleAsBasic() && valueType.handleAsBasic()) { tableCanBeUsed = true; } } catch (Exception e) { SnakeLog.log(e); } GridData listLayoutData = new GridData(GridData.FILL_BOTH); // listLayoutData.widthHint = 300; // listLayoutData.heightHint = 100; listLayoutData.horizontalSpan = 6; if (tableCanBeUsed) { try { tableViewer = new MapTableViewer(composite, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION, map); tableViewer.getTable().setLayoutData(listLayoutData); tableViewer.setDoubleClickAction(new SnakeAction("show map entry") { @Override public void run() { try { if (input == null) { throw new SnakeNotInitializedException("input is not initialized"); } if (input instanceof SnakeMapEntry) { SnakeMapEntry helper = (SnakeMapEntry) input; if (Dialog.OK == mapOpen(helper)) { refresh(); } } else { throw new SnakeWrongTypeException("unexpected type: " + input); } } catch (Exception e) { SnakeExceptionHandler.handle(e); } } }); } catch (SnakeModelException e) { SnakeLog.warning("failed creating SnakeTableViewer for map:" + map.getClass().getCanonicalName()); } } if (!tableCanBeUsed || tableViewer == null) { elements = new ListViewer(composite, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL); // listLayoutData.widthHint = 200; // listLayoutData.heightHint = 100; listLayoutData.horizontalSpan = 6; elements.getControl().setLayoutData(listLayoutData); elements.setLabelProvider(new LabelProvider() { @Override public Image getImage(Object element) { // TODO Auto-generated method stub return super.getImage(element); } @Override public String getText(Object element) { if (element instanceof SnakeMapEntry) { String label = ""; SnakeMapEntry helper = (SnakeMapEntry) element; SnakeType key = helper.getKey(); if (key != null) { label += "key: "; label += key.toString(); } label += ", value= "; SnakeType value = helper.getValue(); if (value != null) { label += value.toString(); } return label; } return super.getText(element); } }); elements.setContentProvider(new IStructuredContentProvider() { @Override public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { // TODO Auto-generated method stub } @Override public void dispose() { // TODO Auto-generated method stub } @Override public Object[] getElements(Object inputElement) { List<SnakeMapEntry<K, V>> helpers = new Vector<SnakeMapEntry<K, V>>(); for (K key : map.keySet()) { V value = map.get(key); SnakeMapEntry<K, V> helper = new SnakeMapEntry<K, V>(key, value, map); helpers.add(helper); } return helpers.toArray(); } }); elements.setInput(map); elements.addDoubleClickListener(new IDoubleClickListener() { @Override public void doubleClick(DoubleClickEvent event) { ISelection selection = event.getSelection(); if (selection instanceof IStructuredSelection) { IStructuredSelection structuredSelection = (IStructuredSelection) selection; Object selected = structuredSelection.getFirstElement(); if (selected instanceof SnakeMapEntry) { SnakeMapEntry helper = (SnakeMapEntry) selected; if (Dialog.OK == mapOpen(helper)) { refresh(); } } } } }); } } protected int mapOpen(SnakeType type) { int ok = -1; Dialog typeDialog = SnakeDialogRegistry.createForType(composite.getShell(), type, true, bValidate); if (typeDialog != null) { ok = typeDialog.open(); } return ok; } protected void open(SnakeType type) { if (Dialog.OK == mapOpen(type)) { refresh(); } } private void add() { try { if (map.keyCreator() instanceof SnakeTypeSelectionCreator) { Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(); SnakeSelectTypeDialog selectDialog = new SnakeSelectTypeDialog(shell, (SnakeTypeSelectionCreator) map.keyCreator(), "select key Type"); if (selectDialog.open() != Dialog.OK) { return; } } if (map.valueCreator() instanceof SnakeTypeSelectionCreator) { Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(); SnakeSelectTypeDialog selectDialog = new SnakeSelectTypeDialog(shell, (SnakeTypeSelectionCreator) map.valueCreator(), "select value Type"); if (selectDialog.open() != Dialog.OK) { return; } } K key = map.newKey(); V value = map.newValue(); SnakeMapEntry mapEntry = new SnakeMapEntry(key, value, map); if (Dialog.OK == mapOpen(mapEntry)) { map.put(key, value); refresh(); } else { key.setParent(null); value.setParent(null); } } catch (Exception e) { SnakeExceptionHandler.handle(e); } } protected void refresh() { if (tableViewer != null) { tableViewer.setInput(map); tableViewer.refresh(); } else { elements.setInput(map); elements.refresh(); } } @Override public void update(ISnakeModification iModification) { refresh(); } }