1 /*******************************************************************************
2 * Copyright (c) 2007, 2012 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.browsing.ui.swt.widgets.impl;
14 import java.util.HashMap;
15 import java.util.List;
17 import java.util.concurrent.CopyOnWriteArrayList;
19 import org.simantics.db.ReadGraph;
20 import org.simantics.db.common.request.ParametrizedPrimitiveRead;
21 import org.simantics.db.exception.DatabaseException;
22 import org.simantics.db.management.ISessionContext;
23 import org.simantics.db.procedure.Listener;
25 public class WidgetSupportImpl implements WidgetSupport {
27 final protected CopyOnWriteArrayList<Widget> widgets = new CopyOnWriteArrayList<Widget>();
28 final protected Map<String, Object> parameters = new HashMap<String, Object>();
29 protected Object lastInput = NO_INPUT;
30 protected ISessionContext lastContext = null;
31 private boolean finished = false;
33 final protected List<Listener<?>> listeners = new CopyOnWriteArrayList<Listener<?>>();
36 public synchronized void register(Widget widget) {
41 public void update(Widget widget) {
42 if(lastContext != null && lastInput != NO_INPUT) widget.setInput(lastContext, lastInput);
46 public void update() {
47 for(Widget widget : widgets) update(widget);
51 public void finish() {
53 //for(Widget widget : widgets) update(widget);
56 @SuppressWarnings("unchecked")
58 public <T> T getInput() {
62 public void fireInput(ISessionContext context, Object input) {
65 lastContext = context;
67 for(Widget widget : widgets) {
68 widget.setInput(context, input);
69 for (Listener listener : listeners)
70 listener.execute(input);
75 @SuppressWarnings("unchecked")
77 public <T> T getParameter(String key) {
78 return (T) parameters.get(key);
82 public void setParameter(String key, Object value) {
83 parameters.put(key, value);
87 public <T> T getInput(ReadGraph graph) throws DatabaseException {
88 return graph.sync(new ParametrizedPrimitiveRead<WidgetSupport, T>(this) {
90 Listener<T> procedure;
92 @SuppressWarnings("unchecked")
94 public void register(ReadGraph graph, Listener<T> procedure) {
95 synchronized (listeners) {
96 if(listeners.contains(procedure))
98 this.procedure = procedure;
99 listeners.add(procedure);
100 procedure.execute((T)lastInput);
105 public void unregistered() {
106 synchronized (listeners) {
107 listeners.remove(procedure);