1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 /***
19 * <p>Title: WSMO Studio</p>
20 * <p>Description: Semantic Web Service Editor</p>
21 * <p>Copyright: Copyright (c) 2004-2006</p>
22 * <p>Company: OntoText Lab. / SIRMA </p>
23 */
24
25 package org.wsmostudio.ui.editors.common;
26
27 import java.util.*;
28 import java.util.List;
29
30 import org.eclipse.jface.dialogs.MessageDialog;
31 import org.eclipse.jface.window.Window;
32 import org.eclipse.swt.SWT;
33 import org.eclipse.swt.dnd.*;
34 import org.eclipse.swt.events.*;
35 import org.eclipse.swt.layout.*;
36 import org.eclipse.swt.widgets.*;
37 import org.omwg.ontology.RelationInstance;
38 import org.wsmo.common.*;
39 import org.wsmo.common.exception.InvalidModelException;
40 import org.wsmo.factory.WsmoFactory;
41 import org.wsmostudio.runtime.*;
42 import org.wsmostudio.ui.*;
43 import org.wsmostudio.ui.dnd.*;
44 import org.wsmostudio.ui.dnd.Clipboard;
45 import org.wsmostudio.ui.editors.model.RelationModel;
46 import org.wsmostudio.ui.views.navigator.WSMOContentProvider;
47
48 /***
49 * A helper GUI component, responsible for maintenance of a set of (WSMO-API)
50 * RelationInstances of a certain WSMO-API Relation. It is a part of the
51 * Relationt Editor component.
52 * The supported operations here are: creation, addition and removal.
53 *
54 * @author not attributable
55 * @version $Revision: 1.13 $ $Date: 2006/01/09 12:51:13 $
56 */
57
58
59 public class RelInstancesPanel implements SelectionListener {
60
61 private Table instsTable;
62 private RelationModel model;
63
64 public RelInstancesPanel(TabFolder parentFolder,
65 RelationModel uiModel) {
66
67 this.model = uiModel;
68
69 TabItem mainItem = new TabItem(parentFolder, SWT.NONE);
70
71 parentFolder.setLayoutData(new GridData(GridData.FILL_BOTH));
72 mainItem.setText("Relation Instances");
73 parentFolder.setLayout(new GridLayout(1, false));
74
75 createTable(parentFolder);
76 initDNDTarget();
77
78 mainItem.setControl(instsTable);
79 }
80
81 private void createTable(Composite parent) {
82 instsTable = new Table(parent, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
83 instsTable.setLayoutData(new GridData(GridData.FILL_BOTH));
84 instsTable.setLinesVisible(false);
85
86 Set instances = model.getRelation().listRelationInstances();
87 for(Iterator it = instances.iterator(); it.hasNext();) {
88 Utils.createTableItem(instsTable,
89 (RelationInstance)it.next(),
90 WsmoImageRegistry.REL_INSTANCE_ICON);
91 }
92 instsTable.addMouseListener(new MouseAdapter() {
93 public void mouseUp(MouseEvent e) {
94 if (e.button != 3) {
95 return;
96 }
97 showContextMenu(e);
98 }
99 });
100 }
101
102 @SuppressWarnings("unchecked")
103 public void update() {
104
105 Set<RelationInstance> toAddInstances = new HashSet<RelationInstance>(
106 model.getRelation().listRelationInstances());
107 TableItem[] items = instsTable.getItems();
108 for (int i = 0; i < items.length; i++) {
109 if (toAddInstances.contains(items[i].getData())) {
110 toAddInstances.remove(items[i].getData());
111 }
112 else {
113 items[i].dispose();
114 }
115 }
116
117
118 for(RelationInstance relInst : toAddInstances) {
119 Utils.createTableItem(instsTable,
120 relInst,
121 WsmoImageRegistry.REL_INSTANCE_ICON);
122 }
123 }
124
125 private void showContextMenu(MouseEvent e) {
126 Menu contextMenu = new Menu(instsTable.getShell(), SWT.POP_UP);
127
128 MenuItem item = new MenuItem(contextMenu, SWT.PUSH);
129 item.setText("Add Instance");
130 item.addSelectionListener(this);
131
132 item = new MenuItem(contextMenu, SWT.SEPARATOR);
133
134 item = new MenuItem(contextMenu, SWT.PUSH);
135 item.setText("Create Instance");
136 item.addSelectionListener(this);
137
138 TableItem[] sel = instsTable.getSelection();
139 if (sel != null
140 && sel.length > 0
141 && sel[0].getBounds(0).contains(e.x, e.y)) {
142 item = new MenuItem(contextMenu, SWT.SEPARATOR);
143
144 item = new MenuItem(contextMenu, SWT.PUSH);
145 item.setText("Remove Instance");
146 item.addSelectionListener(this);
147 }
148
149 contextMenu.setLocation(instsTable.toDisplay(e.x, e.y));
150 contextMenu.setVisible(true);
151 }
152
153 public void dispose() {
154 instsTable.dispose();
155 }
156
157 public void widgetSelected(SelectionEvent e) {
158 String action = ((MenuItem)e.widget).getText();
159 if (action.equals("Create Instance")) {
160 WsmoFactory factory = WSMORuntime.getRuntime().getWsmoFactory();
161 IdentifierInputDialog iDialog = new IdentifierInputDialog(
162 instsTable.getShell(),
163 "New Relation Instance ID",
164 "Name (leave blank for anonymous): ",
165 model.getRelation().getOntology(),
166 factory,
167 true);
168 if (iDialog.open() != Window.OK) {
169 return;
170 }
171 Identifier instanceID = iDialog.getIdentifier();
172 try {
173 factory.createRelationInstance(instanceID,
174 model.getRelation());
175 model.setChanged();
176 }
177 catch(InvalidModelException ime) {
178 LogManager.logError("Error creating rel instance "
179 + ((instanceID != null) ? instanceID.toString() : ""),
180 ime);
181 }
182 return;
183 }
184
185 if (action.equals("Remove Instance")) {
186 TableItem[] sel = instsTable.getSelection();
187 RelationInstance instance = (RelationInstance)sel[0].getData();
188 try {
189 if (model.getRelation().getOntology() != null) {
190 model.getRelation().getOntology().removeRelationInstance(instance);
191 }
192 model.removeRelationInstance(instance);
193 }
194 catch(InvalidModelException ime) {
195 LogManager.logError(ime);
196 }
197 return;
198 }
199 if (action.equals("Add Instance")) {
200 WSMOChooser chooser = new WSMOChooser(instsTable.getShell(),
201 model.getRelation().getOntology(),
202 WSMOContentProvider.RELATION_INSTANCE_MASK);
203 chooser.setFilter(new IWSMOSelectionValidator() {
204 public String isValid(Object selected) {
205 if (false == selected instanceof RelationInstance) {
206 return "Relation Instance selection expected";
207 }
208 return null;
209 }
210 });
211 RelationInstance newInstance = (RelationInstance)chooser.open();
212 if ((newInstance == null)
213 || (newInstance.getRelation() != null
214 && newInstance.getRelation().equals(model.getRelation()))) {
215 return;
216 }
217 try {
218 if (model.getRelation().getOntology() != null) {
219 model.getRelation().getOntology().addRelationInstance(newInstance);
220 }
221 model.addRelationInstance(newInstance);
222 }
223 catch(InvalidModelException ime) {
224 LogManager.logError(ime);
225 }
226 return;
227 }
228
229 }
230
231 public void widgetDefaultSelected(SelectionEvent e) {
232 }
233
234 private void initDNDTarget() {
235 DropTarget target = new DropTarget(instsTable,
236 DND.DROP_LINK | DND.DROP_DEFAULT);
237
238 final WSMOTransfer wsmoTransfer = WSMOTransfer.getInstance();
239 target.setTransfer(new Transfer[] {wsmoTransfer});
240
241 target.addDropListener(new DropTargetAdapter() {
242 public void dragEnter(DropTargetEvent event) {
243 if (true == Clipboard.getInstance().ensureContentType(RelationInstance.class)) {
244 event.detail = DND.DROP_LINK;
245 }
246 else {
247 event.detail = DND.DROP_NONE;
248 }
249 }
250 public void dragOver(DropTargetEvent event) {
251 event.feedback = DND.FEEDBACK_SELECT | DND.FEEDBACK_SCROLL;
252 }
253 public void drop(DropTargetEvent event) {
254 if (wsmoTransfer.isSupportedType(event.currentDataType)) {
255 List<Entity> instances = Clipboard.getInstance().getContent();
256 for(Entity entity : instances) {
257 if (entity instanceof RelationInstance) {
258 try {
259 if (false == model.getRelation().listRelationInstances().contains(entity)) {
260 model.addRelationInstance((RelationInstance)entity);
261 }
262 }
263 catch(InvalidModelException ime) {
264 MessageDialog.openError(instsTable.getShell(),
265 "Error",
266 ime.getMessage());
267 LogManager.logError(ime);
268 }
269 }
270 }
271 }
272 }
273 });
274 }
275
276
277 }
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309