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.*;
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.ConceptModel;
46
47 /***
48 * A helper GUI component, responsible for maintenance of a set of (WSMO-API) Instances
49 * of a certain WSMO-API Concept. It is a part of the Concept Editor component.
50 * The supported operations here are: creation, addition and removal of Instances.
51 *
52 * @author not attributable
53 * @version $Revision: 1.16 $ $Date: 2006/03/06 14:39:43 $
54 */
55
56 public class InstancesContainerPanel implements SelectionListener {
57
58 private Table instsTable;
59
60 private ConceptModel model;
61
62 public InstancesContainerPanel(TabFolder parentFolder,
63 ConceptModel model) {
64 this.model = model;
65
66 TabItem mainItem = new TabItem(parentFolder, SWT.NONE);
67
68 parentFolder.setLayoutData(new GridData(GridData.FILL_BOTH));
69 mainItem.setText("Instances");
70 parentFolder.setLayout(new GridLayout(1, false));
71
72 createTable(parentFolder);
73 initDNDTarget();
74 mainItem.setControl(instsTable);
75 }
76
77 @SuppressWarnings("unchecked")
78 public void update() {
79 Set<Instance> toAddInstances = new HashSet<Instance>(
80 model.getConcept().listInstances());
81
82 TableItem[] items = instsTable.getItems();
83 for (int i = 0; i < items.length; i++) {
84 if (toAddInstances.contains(items[i].getData())) {
85 toAddInstances.remove(items[i].getData());
86 }
87 else {
88 items[i].dispose();
89 }
90 }
91
92
93 for(Instance instance : toAddInstances) {
94 Utils.createTableItem(instsTable,
95 instance,
96 WsmoImageRegistry.INSTANCE_ICON);
97 }
98
99 }
100
101 private void createTable(Composite parent) {
102 instsTable = new Table(parent, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
103 instsTable.setLayoutData(new GridData(GridData.FILL_BOTH));
104 instsTable.setLinesVisible(false);
105
106 for(Iterator it = model.getConcept().listInstances().iterator(); it.hasNext();) {
107 Utils.createTableItem(instsTable,
108 (Instance)it.next(),
109 WsmoImageRegistry.INSTANCE_ICON);
110 }
111 instsTable.addMouseListener(new MouseAdapter() {
112 public void mouseUp(MouseEvent e) {
113 if (e.button != 3) {
114 return;
115 }
116 showContextMenu(e);
117 }
118 });
119 }
120
121 private void showContextMenu(MouseEvent e) {
122 Menu contextMenu = new Menu(instsTable.getShell(), SWT.POP_UP);
123
124 MenuItem item = new MenuItem(contextMenu, SWT.PUSH);
125 item.setText("Add Instance");
126 item.addSelectionListener(this);
127
128 item = new MenuItem(contextMenu, SWT.SEPARATOR);
129
130 item = new MenuItem(contextMenu, SWT.PUSH);
131 item.setText("Create Instance");
132 item.addSelectionListener(this);
133
134 TableItem[] sel = instsTable.getSelection();
135 if (sel != null
136 && sel.length > 0
137 && sel[0].getBounds(0).contains(e.x, e.y)) {
138 item = new MenuItem(contextMenu, SWT.SEPARATOR);
139
140 item = new MenuItem(contextMenu, SWT.PUSH);
141 item.setText("Remove Instance");
142 item.addSelectionListener(this);
143 }
144
145 contextMenu.setLocation(instsTable.toDisplay(e.x, e.y));
146 contextMenu.setVisible(true);
147 }
148
149 public void dispose() {
150 instsTable.dispose();
151 }
152
153 public void widgetSelected(SelectionEvent e) {
154 String action = ((MenuItem)e.widget).getText();
155 if (action.equals("Create Instance")) {
156 WsmoFactory factory = WSMORuntime.getRuntime().getWsmoFactory();
157 IdentifierInputDialog iDialog = new IdentifierInputDialog(
158 instsTable.getShell(),
159 "New Instance ID",
160 "Name:",
161 model.getConcept().getOntology(),
162 factory,
163 false);
164
165 if (iDialog.open() != Window.OK) {
166 return;
167 }
168 Identifier instanceID = iDialog.getIdentifier();
169 try {
170 Instance instance = factory.createInstance(instanceID);
171 model.addInstance(instance);
172 }
173 catch(InvalidModelException ime) {
174 LogManager.logError("Error creating instance "
175 + ((instanceID != null) ? instanceID.toString() : ""),
176 ime);
177 }
178 instsTable.redraw();
179 return;
180 }
181
182 if (action.equals("Remove Instance")) {
183 TableItem[] sel = instsTable.getSelection();
184 Instance instance = (Instance)sel[0].getData();
185 try {
186 model.removeInstance(instance);
187 }
188 catch(InvalidModelException ime) {
189 LogManager.logError(ime);
190 }
191 return;
192 }
193 if (action.equals("Add Instance")) {
194 WSMOChooser chooser = WSMOChooser.createInstanceChooser(
195 instsTable.getShell(),
196 model.getConcept().getOntology());
197 Instance newInstance = (Instance)chooser.open();
198 if ((newInstance == null)
199 || (newInstance.listConcepts().contains(model.getConcept()))) {
200 return;
201 }
202 try {
203 model.getConcept().getOntology().addInstance(newInstance);
204 model.addInstance(newInstance);
205 }
206 catch(InvalidModelException ime) {
207 LogManager.logError(ime);
208 }
209 return;
210 }
211
212 }
213
214 private void initDNDTarget() {
215 DropTarget target = new DropTarget(instsTable,
216 DND.DROP_LINK | DND.DROP_DEFAULT);
217
218 final WSMOTransfer wsmoTransfer = WSMOTransfer.getInstance();
219 target.setTransfer(new Transfer[] {wsmoTransfer});
220
221 target.addDropListener(new DropTargetAdapter() {
222 public void dragEnter(DropTargetEvent event) {
223 if (true == Clipboard.getInstance().ensureContentType(Instance.class)) {
224 event.detail = DND.DROP_LINK;
225 }
226 else {
227 event.detail = DND.DROP_NONE;
228 }
229 }
230 public void dragOver(DropTargetEvent event) {
231 event.feedback = DND.FEEDBACK_SELECT | DND.FEEDBACK_SCROLL;
232 }
233 public void drop(DropTargetEvent event) {
234 if (wsmoTransfer.isSupportedType(event.currentDataType)) {
235 List<Entity> instances = Clipboard.getInstance().getContent();
236 for(Entity entity : instances) {
237 if (entity instanceof Instance) {
238 try {
239 if (false == model.getConcept().listInstances().contains(entity)) {
240 model.addInstance((Instance)entity);
241 }
242 }
243 catch(InvalidModelException ime) {
244 MessageDialog.openError(instsTable.getShell(),
245 "Error",
246 ime.getMessage());
247 LogManager.logError(ime);
248 }
249 }
250 }
251 }
252 }
253 });
254 }
255
256 public void widgetDefaultSelected(SelectionEvent e) {
257 }
258 }
259
260
261
262
263
264
265
266
267
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