]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/widgets/impl/WidgetSupportImpl.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.browsing.ui.swt / src / org / simantics / browsing / ui / swt / widgets / impl / WidgetSupportImpl.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2012 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.browsing.ui.swt.widgets.impl;
13
14 import java.util.HashMap;
15 import java.util.List;
16 import java.util.Map;
17 import java.util.concurrent.CopyOnWriteArrayList;
18
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;
24
25 public class WidgetSupportImpl implements WidgetSupport {
26
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;
32
33         final protected List<Listener<?>> listeners = new CopyOnWriteArrayList<Listener<?>>();
34         
35         @Override
36         public synchronized void register(Widget widget) {
37                 widgets.add(widget);
38         }
39         
40         @Override
41         public void update(Widget widget) {
42                 if(lastContext != null && lastInput != NO_INPUT) widget.setInput(lastContext, lastInput);
43         }
44
45         @Override
46         public void update() {
47                 for(Widget widget : widgets) update(widget);
48         }
49         
50         @Override
51         public void finish() {
52                 finished = true;
53                 //for(Widget widget : widgets) update(widget);
54         }
55         
56         @SuppressWarnings("unchecked")
57         @Override
58         public <T> T getInput() {
59                 return (T) lastInput;
60         }
61         
62         public void fireInput(ISessionContext context, Object input) {
63                 
64                 lastInput = input;
65                 lastContext = context;
66                 
67                 for(Widget widget : widgets) {
68                         widget.setInput(context, input);
69                         for (Listener listener : listeners)
70                                 listener.execute(input);
71                 }
72                 
73         }
74         
75         @SuppressWarnings("unchecked")
76         @Override
77         public <T> T getParameter(String key) {
78                 return (T) parameters.get(key);
79         }
80         
81         @Override
82         public void setParameter(String key, Object value) {
83                 parameters.put(key, value);
84         }
85
86         @Override
87         public <T> T getInput(ReadGraph graph) throws DatabaseException {
88                 return graph.sync(new ParametrizedPrimitiveRead<WidgetSupport, T>(this) {
89
90                         Listener<T> procedure;
91
92                         @SuppressWarnings("unchecked")
93                         @Override
94                         public void register(ReadGraph graph, Listener<T> procedure) {
95                                 synchronized (listeners) {
96                                         if(listeners.contains(procedure))
97                                                 return;
98                                         this.procedure = procedure;
99                                         listeners.add(procedure);
100                                         procedure.execute((T)lastInput);
101                                 }
102                         }
103
104                         @Override
105                         public void unregistered() {
106                                 synchronized (listeners) {
107                                         listeners.remove(procedure);
108                                         procedure = null;
109                                 }
110                         }
111
112                 });
113         }
114         
115 }