2 * Copyright 2015 Samsung Electronics All Rights Reserved.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package oic.simulator.clientcontroller.view;
19 import java.util.List;
22 import oic.simulator.clientcontroller.Activator;
23 import oic.simulator.clientcontroller.listener.IFindResourceUIListener;
24 import oic.simulator.clientcontroller.manager.ResourceManager;
25 import oic.simulator.clientcontroller.manager.UiListenerHandler;
26 import oic.simulator.clientcontroller.remoteresource.RemoteResource;
27 import oic.simulator.clientcontroller.utils.Constants;
28 import oic.simulator.clientcontroller.view.dialogs.FindResourceWizard;
29 import oic.simulator.clientcontroller.view.dialogs.LoadRAMLDialog;
30 import oic.simulator.clientcontroller.view.dialogs.ResourceWizardDialog;
32 import org.eclipse.jface.dialogs.MessageDialog;
33 import org.eclipse.jface.viewers.ISelectionChangedListener;
34 import org.eclipse.jface.viewers.IStructuredSelection;
35 import org.eclipse.jface.viewers.ITreeContentProvider;
36 import org.eclipse.jface.viewers.LabelProvider;
37 import org.eclipse.jface.viewers.SelectionChangedEvent;
38 import org.eclipse.jface.viewers.TreeViewer;
39 import org.eclipse.jface.viewers.Viewer;
40 import org.eclipse.jface.window.Window;
41 import org.eclipse.jface.wizard.WizardDialog;
42 import org.eclipse.swt.SWT;
43 import org.eclipse.swt.custom.CTabFolder;
44 import org.eclipse.swt.custom.CTabItem;
45 import org.eclipse.swt.events.MenuAdapter;
46 import org.eclipse.swt.events.MenuEvent;
47 import org.eclipse.swt.events.SelectionAdapter;
48 import org.eclipse.swt.events.SelectionEvent;
49 import org.eclipse.swt.graphics.Color;
50 import org.eclipse.swt.graphics.Image;
51 import org.eclipse.swt.layout.GridData;
52 import org.eclipse.swt.layout.GridLayout;
53 import org.eclipse.swt.widgets.Button;
54 import org.eclipse.swt.widgets.Composite;
55 import org.eclipse.swt.widgets.Display;
56 import org.eclipse.swt.widgets.Group;
57 import org.eclipse.swt.widgets.Menu;
58 import org.eclipse.swt.widgets.MenuItem;
59 import org.eclipse.swt.widgets.Tree;
60 import org.eclipse.ui.PlatformUI;
61 import org.eclipse.ui.dialogs.FilteredTree;
62 import org.eclipse.ui.dialogs.PatternFilter;
63 import org.eclipse.ui.part.ViewPart;
64 import org.oic.simulator.SimulatorException;
67 * This class manages and shows the resource manager view in the perspective.
69 public class ResourceManagerView extends ViewPart {
71 public static final String VIEW_ID = "oic.simulator.clientcontroller.view.resourcemanager";
73 private Button findResButton;
74 private Button refreshButton;
76 private TreeViewer treeViewer;
77 private TreeViewer favTreeViewer;
79 private CTabFolder folder;
80 private CTabItem foundResTab;
81 private CTabItem favResTab;
83 private ResourceManager resourceManager;
85 private IFindResourceUIListener findListener;
87 private Boolean foundResource;
89 private MessageDialog findDialog;
91 private MessageDialog refreshDialog;
93 private Thread sleepThreadHandle;
95 public ResourceManagerView() {
96 resourceManager = Activator.getDefault().getResourceManager();
98 findListener = new IFindResourceUIListener() {
101 public void onNewResourceFound(final RemoteResource resource) {
102 if (null == resource) {
105 // Changing the status of the find operation.
106 setFoundResource(true);
108 // Interrupt the sleep thread.
109 if (null != sleepThreadHandle && sleepThreadHandle.isAlive()) {
110 sleepThreadHandle.interrupt();
113 // Update the tree viewer
114 Display.getDefault().asyncExec(new Runnable() {
117 if (!treeViewer.getControl().isDisposed()) {
118 treeViewer.refresh();
121 if (!favTreeViewer.getControl().isDisposed()) {
122 favTreeViewer.refresh();
125 // Close the find dialog
126 if (null != findDialog) {
130 // Close the refresh dialog
131 if (null != refreshDialog) {
132 refreshDialog.close();
141 public void createPartControl(Composite parent) {
142 Composite compContent = new Composite(parent, SWT.NONE);
143 compContent.setLayout(new GridLayout());
144 GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
145 compContent.setLayoutData(gd);
147 Composite buttonComp = new Composite(compContent, SWT.NONE);
148 buttonComp.setLayout(new GridLayout(2, false));
151 gd.horizontalAlignment = SWT.FILL;
152 gd.grabExcessHorizontalSpace = true;
154 buttonComp.setLayoutData(gd);
156 findResButton = new Button(buttonComp, SWT.PUSH);
157 findResButton.setText("Find Resources");
158 findResButton.setToolTipText("Find OIC resources");
162 findResButton.setLayoutData(gd);
164 refreshButton = new Button(buttonComp, SWT.PUSH);
165 refreshButton.setText("Refresh");
166 refreshButton.setToolTipText("Restart the search once again");
170 refreshButton.setLayoutData(gd);
172 // Create a Tab Folder.
173 folder = new CTabFolder(compContent, SWT.BORDER);
174 gd = new GridData(SWT.FILL, SWT.FILL, true, true);
175 folder.setLayoutData(gd);
176 folder.setSimple(false);
177 folder.setUnselectedCloseVisible(false);
178 folder.setUnselectedImageVisible(false);
179 folder.addSelectionListener(new SelectionAdapter() {
181 public void widgetSelected(SelectionEvent e) {
183 treeViewer.setSelection(null);
184 favTreeViewer.setSelection(null);
185 resourceManager.resourceSelectionChanged(null);
189 createFoundResourcesArea();
191 createFavoriteResourcesArea();
193 folder.setSelection(foundResTab);
195 findDialog = new MessageDialog(Display.getDefault().getActiveShell(),
196 "Finding Servers", null,
197 "Finding the requested servers\nPlease wait...",
198 MessageDialog.INFORMATION, new String[] { "Cancel" }, 0);
199 // findDialog.setBlockOnOpen(false);
201 refreshDialog = new MessageDialog(
202 Display.getDefault().getActiveShell(),
205 "Refreshing the search and finding the requested servers once again\nPlease wait...",
206 MessageDialog.INFORMATION, new String[] { "Cancel" }, 0);
207 // refreshDialog.setBlockOnOpen(false);
211 addManagerListeners();
213 // Setting the initial visibility of refresh based on the last known
215 Set<String> prevSearchTypes = resourceManager.getLastKnownSearchTypes();
216 if (null == prevSearchTypes || prevSearchTypes.size() < 1) {
217 refreshButton.setEnabled(false);
219 refreshButton.setEnabled(true);
223 private void createFoundResourcesArea() {
224 foundResTab = new CTabItem(folder, SWT.NULL);
225 foundResTab.setText("Found Resources");
227 // Create a group to show all the discovered resources.
228 // Adding the group to the folder.
229 Group resourceGroup = new Group(folder, SWT.NONE);
230 // resourceGroup.setText("Discovered Resources");
232 Color color = Display.getDefault().getSystemColor(SWT.COLOR_WHITE);
233 resourceGroup.setBackground(color);
235 resourceGroup.setLayout(new GridLayout(1, false));
236 GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
237 resourceGroup.setLayoutData(gd);
239 PatternFilter filter = new PatternFilter();
240 FilteredTree filteredTree = new FilteredTree(resourceGroup,
241 SWT.H_SCROLL | SWT.V_SCROLL | SWT.SINGLE, filter, true);
242 treeViewer = filteredTree.getViewer();
243 treeViewer.getTree().setLayoutData(
244 new GridData(SWT.FILL, SWT.FILL, true, true));
245 treeViewer.setContentProvider(new TreeContentProvider());
246 treeViewer.setLabelProvider(new TreeLabelProvider());
247 treeViewer.setInput(new Object());
249 addMenuToFoundResources();
251 foundResTab.setControl(resourceGroup);
254 private void addMenuToFoundResources() {
256 if (null != treeViewer) {
257 final Tree resourceTreeHead = treeViewer.getTree();
258 if (null != resourceTreeHead) {
259 // Below code creates menu entries and shows them on right
260 // clicking a resource
261 final Menu menu = new Menu(resourceTreeHead);
262 resourceTreeHead.setMenu(menu);
263 menu.addMenuListener(new MenuAdapter() {
265 public void menuShown(MenuEvent e) {
266 // Clear existing menu items
267 MenuItem[] items = menu.getItems();
268 for (int index = 0; index < items.length; index++) {
269 items[index].dispose();
271 setupUploadRamlMenuItem(menu);
273 final RemoteResource resource = (RemoteResource) ((IStructuredSelection) treeViewer
274 .getSelection()).getFirstElement();
275 if (null == resource) {
278 String menuText = !resource.isFavorite() ? "Add to favorites"
279 : "Remove from favorites";
280 MenuItem addToFavMenuItem = new MenuItem(menu, SWT.NONE);
281 addToFavMenuItem.setText(menuText);
283 .addSelectionListener(new SelectionAdapter() {
285 public void widgetSelected(SelectionEvent e) {
286 if (!resource.isFavorite()) {
288 .addResourcetoFavorites(resource);
290 .addResourceURItoFavorites(resource);
293 .removeResourceFromFavorites(resource);
295 .removeResourceURIFromFavorites(resource);
297 favTreeViewer.refresh();
306 private void createFavoriteResourcesArea() {
307 favResTab = new CTabItem(folder, SWT.NULL);
308 favResTab.setText("Favorite Resources");
310 // Create a group to show all the discovered resources.
311 // Adding the group to the folder.
312 Group resourceGroup = new Group(folder, SWT.NONE);
313 // resourceGroup.setText("Discovered Resources");
315 Color color = Display.getDefault().getSystemColor(SWT.COLOR_WHITE);
316 resourceGroup.setBackground(color);
318 resourceGroup.setLayout(new GridLayout(1, false));
319 GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
320 resourceGroup.setLayoutData(gd);
322 PatternFilter filter = new PatternFilter();
323 FilteredTree filteredTree = new FilteredTree(resourceGroup,
324 SWT.H_SCROLL | SWT.V_SCROLL | SWT.SINGLE, filter, true);
325 favTreeViewer = filteredTree.getViewer();
326 favTreeViewer.getTree().setLayoutData(
327 new GridData(SWT.FILL, SWT.FILL, true, true));
328 favTreeViewer.setContentProvider(new FavTreeContentProvider());
329 favTreeViewer.setLabelProvider(new TreeLabelProvider());
330 favTreeViewer.setInput(new Object());
332 favResTab.setControl(resourceGroup);
334 addMenuToFavResources();
337 private void addMenuToFavResources() {
338 if (null != favTreeViewer) {
339 final Tree resourceTreeHead = favTreeViewer.getTree();
340 if (null != resourceTreeHead) {
341 // Below code creates menu entries and shows them on right
342 // clicking a resource
343 final Menu menu = new Menu(resourceTreeHead);
344 resourceTreeHead.setMenu(menu);
345 menu.addMenuListener(new MenuAdapter() {
347 public void menuShown(MenuEvent e) {
348 // Clear existing menu items
349 MenuItem[] items = menu.getItems();
350 for (int index = 0; index < items.length; index++) {
351 items[index].dispose();
354 setupUploadRamlMenuItem(menu);
356 MenuItem addToFavMenuItem = new MenuItem(menu, SWT.NONE);
357 addToFavMenuItem.setText("Remove from favorites");
359 .addSelectionListener(new SelectionAdapter() {
361 public void widgetSelected(SelectionEvent e) {
362 RemoteResource resource = (RemoteResource) ((IStructuredSelection) favTreeViewer
365 if (null == resource) {
369 .removeResourceFromFavorites(resource);
371 .removeResourceURIFromFavorites(resource);
372 favTreeViewer.refresh();
381 private void setupUploadRamlMenuItem(Menu menu) {
382 MenuItem uploadRAMLItem = new MenuItem(menu, SWT.NONE);
383 uploadRAMLItem.setText("Upload RAML Configuration");
384 uploadRAMLItem.addSelectionListener(new SelectionAdapter() {
386 public void widgetSelected(SelectionEvent e) {
387 // Open the RAML configuration dialog if
388 // RAML file is not yet uploaded for the
389 // currently selected resource
390 RemoteResource resource = resourceManager
391 .getCurrentResourceInSelection();
392 if (null == resource) {
395 if (resource.isConfigUploaded()) {
396 boolean answer = MessageDialog
398 Display.getDefault().getActiveShell(),
399 "Upload Another RAML",
400 "This resource is already configured with RAML.\n"
401 + "Do you want to upload a new configuration?");
406 // Open the dialog in a separate
408 PlatformUI.getWorkbench().getDisplay().syncExec(new Thread() {
411 LoadRAMLDialog ramlDialog = new LoadRAMLDialog(Display
412 .getDefault().getActiveShell());
413 if (ramlDialog.open() != Window.OK) {
416 String configFilePath = ramlDialog.getConfigFilePath();
417 if (null == configFilePath
418 || configFilePath.length() < 1) {
419 MessageDialog.openInformation(Display.getDefault()
421 "Invalid RAML Config path",
422 "Configuration file path is invalid.");
426 boolean result = resourceManager.setConfigFilePath(
428 .getCurrentResourceInSelection(),
432 .openInformation(Display.getDefault()
435 "Failed to obtain the details from the given RAML.");
437 } catch (SimulatorException e) {
438 MessageDialog.openInformation(Display.getDefault()
439 .getActiveShell(), "Invalid RAML",
440 "Given configuration file is invalid.");
448 private void addUIListeners() {
449 findResButton.addSelectionListener(new SelectionAdapter() {
451 public void widgetSelected(SelectionEvent e) {
452 PlatformUI.getWorkbench().getDisplay().syncExec(new Runnable() {
456 FindResourceWizard findWizard = new FindResourceWizard();
457 ResourceWizardDialog wizardDialog = new ResourceWizardDialog(
458 PlatformUI.getWorkbench().getDisplay()
459 .getActiveShell(), findWizard);
460 int open = wizardDialog.open();
461 if (open == WizardDialog.OK) {
462 // Setting initial value on starting the find
464 setFoundResource(false);
466 Set<String> searchTypes = findWizard
468 // Delete cached details of resources based on the
469 // given search types.
470 // If there are no resource types to search, then
473 resourceManager.deleteResources(searchTypes);
476 treeViewer.refresh();
477 favTreeViewer.refresh();
479 // Call native method to find Resources
480 boolean result = resourceManager
481 .findResourceRequest(searchTypes);
483 searchUIOperation(false);
486 .openError(Display.getDefault()
488 "Find Resource status",
489 "Operation failed due to some problems in core layer.");
492 // Store this information for refresh
495 .setLastKnownSearchTypes(searchTypes);
497 // Change the refresh visibility
498 refreshButton.setEnabled(true);
505 refreshButton.addSelectionListener(new SelectionAdapter() {
507 public void widgetSelected(SelectionEvent e) {
508 Set<String> searchTypes = resourceManager
509 .getLastKnownSearchTypes();
510 setFoundResource(false);
512 // Delete cached details of resources based on the given search
514 // If there are no resource types to search, then all resources
516 resourceManager.deleteResources(searchTypes);
519 treeViewer.refresh();
520 favTreeViewer.refresh();
522 // Call native method to find Resources
523 boolean result = resourceManager
524 .findResourceRequest(searchTypes);
526 searchUIOperation(true);
529 .openError(Display.getDefault().getActiveShell(),
530 "Find Resource status",
531 "Operation failed due to some problems in core layer.");
536 // Below code adds a listener to the tree for selection changes
537 // and notifies the resource manager
538 ISelectionChangedListener treeSelectionListener = new ISelectionChangedListener() {
541 public void selectionChanged(SelectionChangedEvent e) {
542 if (e.getSelection().isEmpty()) {
545 if (e.getSelection() instanceof IStructuredSelection) {
546 IStructuredSelection selection = (IStructuredSelection) e
548 RemoteResource resource = (RemoteResource) selection
550 if (null == resource) {
553 resourceManager.resourceSelectionChanged(resource);
558 treeViewer.addSelectionChangedListener(treeSelectionListener);
559 favTreeViewer.addSelectionChangedListener(treeSelectionListener);
562 // If refresh is true, then Refresh Dialog else Find Dialog will be shown.
563 private void searchUIOperation(boolean refresh) {
564 final MessageDialog targetDialog;
566 targetDialog = refreshDialog;
568 targetDialog = findDialog;
570 // Open the dialog in a new thread.
571 PlatformUI.getWorkbench().getDisplay().syncExec(new Thread() {
575 if (isFoundResource()) {
576 setFoundResource(false);
580 PlatformUI.getWorkbench().getDisplay().asyncExec(new Thread() {
583 targetDialog.open(); // This method returns once the
584 // cancel button is pressed.
586 // Interrupt the sleep thread.
587 if (null != sleepThreadHandle
588 && sleepThreadHandle.isAlive()) {
589 sleepThreadHandle.interrupt();
592 // Set the status of find.
593 setFoundResource(false);
597 // Thread for find time-out.
598 sleepThreadHandle = new Thread() {
603 Thread.sleep(Constants.FIND_RESOURCES_TIMEOUT * 1000);
604 } catch (InterruptedException e) {
608 child = new Thread() {
611 if (null != targetDialog) {
612 targetDialog.close();
614 // Check if any new resources are
616 // Is no new resources, then display a
618 if (!isFoundResource()) {
624 "No servers are available as of now.\n"
625 + "Please check the servers' status and press"
626 + "'Refresh' button to restart the search.");
628 // Resetting the status to false for
630 setFoundResource(false);
636 PlatformUI.getWorkbench().getDisplay().syncExec(child);
639 sleepThreadHandle.start();
644 private void addManagerListeners() {
645 UiListenerHandler.getInstance().addFindresourceUIListener(findListener);
649 public void dispose() {
650 // Unregister the listener
651 if (null != findListener) {
652 UiListenerHandler.getInstance().removeFindresourceUIListener(
654 resourceManager.resourceSelectionChanged(null);
660 public void setFocus() {
663 public synchronized void setFoundResource(boolean value) {
664 foundResource = value;
667 public synchronized boolean isFoundResource() {
668 return foundResource;
672 class TreeContentProvider implements ITreeContentProvider {
675 public void dispose() {
679 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
684 public Object[] getChildren(Object parent) {
689 public Object[] getElements(Object parent) {
690 List<RemoteResource> resourceList = Activator.getDefault()
691 .getResourceManager().getResourceList();
692 return resourceList.toArray();
696 public Object getParent(Object child) {
701 public boolean hasChildren(Object parent) {
707 class FavTreeContentProvider implements ITreeContentProvider {
710 public void dispose() {
714 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
719 public Object[] getChildren(Object parent) {
724 public Object[] getElements(Object parent) {
725 List<RemoteResource> resourceList = Activator.getDefault()
726 .getResourceManager().getFavResourceList();
727 return resourceList.toArray();
731 public Object getParent(Object child) {
736 public boolean hasChildren(Object parent) {
742 class TreeLabelProvider extends LabelProvider {
744 public String getText(Object element) {
745 RemoteResource resource = (RemoteResource) element;
746 return resource.getRemoteResourceRef().getURI();
750 public Image getImage(Object element) {