]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
1b04c098bdd9da65e3e74cd706b91bdd1d023fc9
[simantics/sysdyn.git] /
1 /*******************************************************************************\r
2  * Copyright (c) 2013 Association for Decentralized Information Management in\r
3  * Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     Semantum Oy - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.sysdyn.ui.properties.widgets.sensitivity;\r
13 \r
14 import org.eclipse.jface.bindings.keys.KeyStroke;\r
15 import org.eclipse.jface.bindings.keys.ParseException;\r
16 import org.eclipse.jface.fieldassist.ContentProposalAdapter;\r
17 import org.eclipse.jface.fieldassist.IContentProposal;\r
18 import org.eclipse.jface.fieldassist.IContentProposalListener;\r
19 import org.eclipse.jface.fieldassist.IContentProposalListener2;\r
20 import org.eclipse.jface.fieldassist.TextContentAdapter;\r
21 import org.eclipse.swt.widgets.Control;\r
22 import org.simantics.browsing.ui.swt.widgets.impl.TextModifyListenerImpl;\r
23 import org.simantics.browsing.ui.swt.widgets.impl.TrackedModifyEvent;\r
24 import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;\r
25 import org.simantics.databoard.Bindings;\r
26 import org.simantics.db.ReadGraph;\r
27 import org.simantics.db.Resource;\r
28 import org.simantics.db.WriteGraph;\r
29 import org.simantics.db.exception.DatabaseException;\r
30 import org.simantics.jfreechart.chart.properties.VariableProposalProvider;\r
31 \r
32 public class VariableNameModifier extends TextModifyListenerImpl<Resource> {\r
33 \r
34     private boolean active;\r
35     private Control control;\r
36     private String variableNameRelationUri;\r
37     private String indexUri;\r
38     \r
39     private char[] alphaNumericCharacters = {\r
40         'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','å','ä','ö',\r
41         'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','Å','Ä','Ö',\r
42         '1','2','3','4','5','6','7','8','9','0','.','_'};\r
43     \r
44         \r
45     public VariableNameModifier(Control control, WidgetSupport support, String variableNameRelationUri, String indexUri) {\r
46         this.variableNameRelationUri = variableNameRelationUri;\r
47         this.indexUri = indexUri;\r
48         \r
49         this.control = control;\r
50         this.active = true;\r
51         \r
52         KeyStroke keyStroke = null;\r
53         try {\r
54             keyStroke = KeyStroke.getInstance("Ctrl+Space");\r
55         } catch (ParseException e1) {\r
56             e1.printStackTrace();\r
57         }\r
58         \r
59         //SimpleContentProposalProvider scpp = new VariableProposalProvider(control, support);\r
60         VariableProposalProvider scpp = new VariableProposalProvider(control, support);\r
61         scpp.setFiltering(true);\r
62 \r
63         ContentProposalAdapter adapter = new ContentProposalAdapter(\r
64                 control, new TextContentAdapter(), scpp, keyStroke, alphaNumericCharacters);\r
65         adapter.setAutoActivationDelay(0);\r
66         adapter.setProposalAcceptanceStyle(ContentProposalAdapter.PROPOSAL_REPLACE);\r
67         adapter.addContentProposalListener(new IContentProposalListener2() {\r
68 \r
69             @Override\r
70             public void proposalPopupOpened(ContentProposalAdapter adapter) {\r
71                 if(VariableNameModifier.this != null)\r
72                     VariableNameModifier.this.deactivate();\r
73             }\r
74 \r
75             @Override\r
76             public void proposalPopupClosed(ContentProposalAdapter adapter) {\r
77                 if(VariableNameModifier.this != null)\r
78                     VariableNameModifier.this.activate();\r
79             }\r
80         });\r
81 \r
82         adapter.addContentProposalListener(new IContentProposalListener() {\r
83 \r
84             @Override\r
85             public void proposalAccepted(IContentProposal proposal) {\r
86                 if(VariableNameModifier.this.control != null && !VariableNameModifier.this.control.isDisposed())\r
87                     VariableNameModifier.this.modifyText(new TrackedModifyEvent(VariableNameModifier.this.control, proposal.getContent()));\r
88             }\r
89         });\r
90     \r
91     \r
92     }\r
93     \r
94 \r
95     @Override\r
96     public void applyText(WriteGraph graph, Resource resource, String text) throws DatabaseException {\r
97         if(active) {\r
98             graph.claimLiteral(resource, getVariableNameRelation(graph), text, Bindings.STRING);\r
99             graph.deny(resource, getIndexRelation(graph));\r
100         } else {\r
101             System.out.println("NÄHÄÄ");\r
102         }\r
103     }\r
104     \r
105     private Resource getVariableNameRelation(ReadGraph graph) throws DatabaseException {\r
106         return graph.getResource(variableNameRelationUri);\r
107     }\r
108     \r
109     private Resource getIndexRelation(ReadGraph graph) throws DatabaseException {\r
110         return graph.getResource(indexUri);\r
111     }\r
112 \r
113     public void deactivate() {\r
114         active = false;\r
115     }\r
116 \r
117     public void activate() {\r
118         active = true;\r
119     }\r
120 \r
121 }\r