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 org.eclipse.jface.dialogs.MessageDialog;
20 import org.eclipse.jface.viewers.ISelectionChangedListener;
21 import org.eclipse.jface.viewers.IStructuredSelection;
22 import org.eclipse.jface.viewers.ITreeContentProvider;
23 import org.eclipse.jface.viewers.LabelProvider;
24 import org.eclipse.jface.viewers.SelectionChangedEvent;
25 import org.eclipse.jface.viewers.TreeViewer;
26 import org.eclipse.jface.viewers.Viewer;
27 import org.eclipse.jface.window.Window;
28 import org.eclipse.jface.wizard.WizardDialog;
29 import org.eclipse.swt.SWT;
30 import org.eclipse.swt.custom.CTabFolder;
31 import org.eclipse.swt.custom.CTabItem;
32 import org.eclipse.swt.events.MenuAdapter;
33 import org.eclipse.swt.events.MenuEvent;
34 import org.eclipse.swt.events.SelectionAdapter;
35 import org.eclipse.swt.events.SelectionEvent;
36 import org.eclipse.swt.graphics.Color;
37 import org.eclipse.swt.graphics.Image;
38 import org.eclipse.swt.layout.GridData;
39 import org.eclipse.swt.layout.GridLayout;
40 import org.eclipse.swt.widgets.Button;
41 import org.eclipse.swt.widgets.Composite;
42 import org.eclipse.swt.widgets.Display;
43 import org.eclipse.swt.widgets.Group;
44 import org.eclipse.swt.widgets.Menu;
45 import org.eclipse.swt.widgets.MenuItem;
46 import org.eclipse.swt.widgets.Tree;
47 import org.eclipse.ui.PlatformUI;
48 import org.eclipse.ui.dialogs.FilteredTree;
49 import org.eclipse.ui.dialogs.PatternFilter;
50 import org.eclipse.ui.part.ViewPart;
52 import java.util.List;
55 import org.oic.simulator.SimulatorException;
57 import oic.simulator.clientcontroller.Activator;
58 import oic.simulator.clientcontroller.listener.IFindResourceUIListener;
59 import oic.simulator.clientcontroller.manager.ResourceManager;
60 import oic.simulator.clientcontroller.manager.UiListenerHandler;
61 import oic.simulator.clientcontroller.remoteresource.RemoteResource;
62 import oic.simulator.clientcontroller.utils.Constants;
63 import oic.simulator.clientcontroller.view.dialogs.FindResourceWizard;
64 import oic.simulator.clientcontroller.view.dialogs.LoadRAMLDialog;
65 import oic.simulator.clientcontroller.view.dialogs.ResourceWizardDialog;
68 * This class manages and shows the resource manager view in the perspective.
70 public class ResourceManagerView extends ViewPart {
72 public static final String VIEW_ID = "oic.simulator.clientcontroller.view.resourcemanager";
74 private Button findResButton;
75 private Button refreshButton;
77 private TreeViewer treeViewer;
78 private TreeViewer favTreeViewer;
80 private CTabFolder folder;
81 private CTabItem foundResTab;
82 private CTabItem favResTab;
84 private ResourceManager resourceManager;
86 private IFindResourceUIListener findListener;
88 private Boolean foundResource;
90 private MessageDialog findDialog;
92 private MessageDialog refreshDialog;
94 private Thread sleepThreadHandle;
96 public ResourceManagerView() {
97 resourceManager = Activator.getDefault().getResourceManager();
99 findListener = new IFindResourceUIListener() {
102 public void onNewResourceFound(final RemoteResource resource) {
103 if (null == resource) {
106 // Changing the status of the find operation.
107 setFoundResource(true);
109 // Interrupt the sleep thread.
110 if (null != sleepThreadHandle && sleepThreadHandle.isAlive()) {
111 sleepThreadHandle.interrupt();
114 // Update the tree viewer
115 Display.getDefault().asyncExec(new Runnable() {
118 if (!treeViewer.getControl().isDisposed()) {
119 treeViewer.refresh();
122 if (!favTreeViewer.getControl().isDisposed()) {
123 favTreeViewer.refresh();
126 // Close the find dialog
127 if (null != findDialog) {
131 // Close the refresh dialog
132 if (null != refreshDialog) {
133 refreshDialog.close();
142 public void createPartControl(Composite parent) {
143 Composite compContent = new Composite(parent, SWT.NONE);
144 compContent.setLayout(new GridLayout());
145 GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
146 compContent.setLayoutData(gd);
148 Composite buttonComp = new Composite(compContent, SWT.NONE);
149 buttonComp.setLayout(new GridLayout(2, false));
152 gd.horizontalAlignment = SWT.FILL;
153 gd.grabExcessHorizontalSpace = true;
155 buttonComp.setLayoutData(gd);
157 findResButton = new Button(buttonComp, SWT.PUSH);
158 findResButton.setText("Find Resources");
159 findResButton.setToolTipText("Find OIC resources");
163 findResButton.setLayoutData(gd);
165 refreshButton = new Button(buttonComp, SWT.PUSH);
166 refreshButton.setText("Refresh");
167 refreshButton.setToolTipText("Restart the search once again");
171 refreshButton.setLayoutData(gd);
173 // Create a Tab Folder.
174 folder = new CTabFolder(compContent, SWT.BORDER);
175 gd = new GridData(SWT.FILL, SWT.FILL, true, true);
176 folder.setLayoutData(gd);
177 folder.setSimple(false);
178 folder.setUnselectedCloseVisible(false);
179 folder.setUnselectedImageVisible(false);
180 folder.addSelectionListener(new SelectionAdapter() {
182 public void widgetSelected(SelectionEvent e) {
184 treeViewer.setSelection(null);
185 favTreeViewer.setSelection(null);
186 resourceManager.resourceSelectionChanged(null);
190 createFoundResourcesArea();
192 createFavoriteResourcesArea();
194 folder.setSelection(foundResTab);
196 findDialog = new MessageDialog(Display.getDefault().getActiveShell(),
197 "Finding Servers", null,
198 "Finding the requested servers\nPlease wait...",
199 MessageDialog.INFORMATION, new String[] { "Cancel" }, 0);
200 // findDialog.setBlockOnOpen(false);
202 refreshDialog = new MessageDialog(
203 Display.getDefault().getActiveShell(),
206 "Refreshing the search and finding the requested servers once again\nPlease wait...",
207 MessageDialog.INFORMATION, new String[] { "Cancel" }, 0);
208 // refreshDialog.setBlockOnOpen(false);
212 addManagerListeners();
214 // Setting the initial visibility of refresh based on the last known
216 Set<String> prevSearchTypes = resourceManager.getLastKnownSearchTypes();
217 if (null == prevSearchTypes || prevSearchTypes.size() < 1) {
218 refreshButton.setEnabled(false);
220 refreshButton.setEnabled(true);
224 private void createFoundResourcesArea() {
225 foundResTab = new CTabItem(folder, SWT.NULL);
226 foundResTab.setText("Found Resources");
228 // Create a group to show all the discovered resources.
229 // Adding the group to the folder.
230 Group resourceGroup = new Group(folder, SWT.NONE);
231 // resourceGroup.setText("Discovered Resources");
233 Color color = Display.getDefault().getSystemColor(SWT.COLOR_WHITE);
234 resourceGroup.setBackground(color);
236 resourceGroup.setLayout(new GridLayout(1, false));
237 GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
238 resourceGroup.setLayoutData(gd);
240 PatternFilter filter = new PatternFilter();
241 FilteredTree filteredTree = new FilteredTree(resourceGroup,
242 SWT.H_SCROLL | SWT.V_SCROLL | SWT.SINGLE, filter, true);
243 treeViewer = filteredTree.getViewer();
244 treeViewer.getTree().setLayoutData(
245 new GridData(SWT.FILL, SWT.FILL, true, true));
246 treeViewer.setContentProvider(new TreeContentProvider());
247 treeViewer.setLabelProvider(new TreeLabelProvider());
248 treeViewer.setInput(new Object());
250 addMenuToFoundResources();
252 foundResTab.setControl(resourceGroup);
255 private void addMenuToFoundResources() {
257 if (null != treeViewer) {
258 final Tree resourceTreeHead = treeViewer.getTree();
259 if (null != resourceTreeHead) {
260 // Below code creates menu entries and shows them on right
261 // clicking a resource
262 final Menu menu = new Menu(resourceTreeHead);
263 resourceTreeHead.setMenu(menu);
264 menu.addMenuListener(new MenuAdapter() {
266 public void menuShown(MenuEvent e) {
267 // Clear existing menu items
268 MenuItem[] items = menu.getItems();
269 for (int index = 0; index < items.length; index++) {
270 items[index].dispose();
272 setupUploadRamlMenuItem(menu);
274 final RemoteResource resource = (RemoteResource) ((IStructuredSelection) treeViewer
275 .getSelection()).getFirstElement();
276 if (null == resource) {
279 String menuText = !resource.isFavorite() ? "Add to favorites"
280 : "Remove from favorites";
281 MenuItem addToFavMenuItem = new MenuItem(menu, SWT.NONE);
282 addToFavMenuItem.setText(menuText);
284 .addSelectionListener(new SelectionAdapter() {
286 public void widgetSelected(SelectionEvent e) {
287 if (!resource.isFavorite()) {
289 .addResourcetoFavorites(resource);
291 .addResourceURItoFavorites(resource);
294 .removeResourceFromFavorites(resource);
296 .removeResourceURIFromFavorites(resource);
298 favTreeViewer.refresh();
307 private void createFavoriteResourcesArea() {
308 favResTab = new CTabItem(folder, SWT.NULL);
309 favResTab.setText("Favorite Resources");
311 // Create a group to show all the discovered resources.
312 // Adding the group to the folder.
313 Group resourceGroup = new Group(folder, SWT.NONE);
314 // resourceGroup.setText("Discovered Resources");
316 Color color = Display.getDefault().getSystemColor(SWT.COLOR_WHITE);
317 resourceGroup.setBackground(color);
319 resourceGroup.setLayout(new GridLayout(1, false));
320 GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
321 resourceGroup.setLayoutData(gd);
323 PatternFilter filter = new PatternFilter();
324 FilteredTree filteredTree = new FilteredTree(resourceGroup,
325 SWT.H_SCROLL | SWT.V_SCROLL | SWT.SINGLE, filter, true);
326 favTreeViewer = filteredTree.getViewer();
327 favTreeViewer.getTree().setLayoutData(
328 new GridData(SWT.FILL, SWT.FILL, true, true));
329 favTreeViewer.setContentProvider(new FavTreeContentProvider());
330 favTreeViewer.setLabelProvider(new TreeLabelProvider());
331 favTreeViewer.setInput(new Object());
333 favResTab.setControl(resourceGroup);
335 addMenuToFavResources();
338 private void addMenuToFavResources() {
339 if (null != favTreeViewer) {
340 final Tree resourceTreeHead = favTreeViewer.getTree();
341 if (null != resourceTreeHead) {
342 // Below code creates menu entries and shows them on right
343 // clicking a resource
344 final Menu menu = new Menu(resourceTreeHead);
345 resourceTreeHead.setMenu(menu);
346 menu.addMenuListener(new MenuAdapter() {
348 public void menuShown(MenuEvent e) {
349 // Clear existing menu items
350 MenuItem[] items = menu.getItems();
351 for (int index = 0; index < items.length; index++) {
352 items[index].dispose();
355 setupUploadRamlMenuItem(menu);
357 MenuItem addToFavMenuItem = new MenuItem(menu, SWT.NONE);
358 addToFavMenuItem.setText("Remove from favorites");
360 .addSelectionListener(new SelectionAdapter() {
362 public void widgetSelected(SelectionEvent e) {
363 RemoteResource resource = (RemoteResource) ((IStructuredSelection) favTreeViewer
366 if (null == resource) {
370 .removeResourceFromFavorites(resource);
372 .removeResourceURIFromFavorites(resource);
373 favTreeViewer.refresh();
382 private void setupUploadRamlMenuItem(Menu menu) {
383 MenuItem uploadRAMLItem = new MenuItem(menu, SWT.NONE);
384 uploadRAMLItem.setText("Upload RAML Configuration");
385 uploadRAMLItem.addSelectionListener(new SelectionAdapter() {
387 public void widgetSelected(SelectionEvent e) {
388 // Open the RAML configuration dialog if
389 // RAML file is not yet uploaded for the
390 // currently selected resource
391 RemoteResource resource = resourceManager
392 .getCurrentResourceInSelection();
393 if (null == resource) {
396 if (resource.isConfigUploaded()) {
397 boolean answer = MessageDialog
399 Display.getDefault().getActiveShell(),
400 "Upload Another RAML",
401 "This resource is already configured with RAML.\n"
402 + "Do you want to upload a new configuration?");
407 // Open the dialog in a separate
409 PlatformUI.getWorkbench().getDisplay().syncExec(new Thread() {
412 LoadRAMLDialog ramlDialog = new LoadRAMLDialog(Display
413 .getDefault().getActiveShell());
414 if (ramlDialog.open() != Window.OK) {
417 String configFilePath = ramlDialog.getConfigFilePath();
418 if (null == configFilePath
419 || configFilePath.length() < 1) {
420 MessageDialog.openInformation(Display.getDefault()
422 "Invalid RAML Config path",
423 "Configuration file path is invalid.");
427 boolean result = resourceManager.setConfigFilePath(
429 .getCurrentResourceInSelection(),
433 .openInformation(Display.getDefault()
436 "Failed to obtain the details from the given RAML.");
438 } catch (SimulatorException e) {
439 MessageDialog.openInformation(Display.getDefault()
440 .getActiveShell(), "Invalid RAML",
441 "Given configuration file is invalid.");
449 private void addUIListeners() {
450 findResButton.addSelectionListener(new SelectionAdapter() {
452 public void widgetSelected(SelectionEvent e) {
453 PlatformUI.getWorkbench().getDisplay().syncExec(new Runnable() {
457 FindResourceWizard findWizard = new FindResourceWizard();
458 ResourceWizardDialog wizardDialog = new ResourceWizardDialog(
459 PlatformUI.getWorkbench().getDisplay()
460 .getActiveShell(), findWizard);
461 int open = wizardDialog.open();
462 if (open == WizardDialog.OK) {
463 // Setting initial value on starting the find
465 setFoundResource(false);
467 Set<String> searchTypes = findWizard
469 // Delete cached details of resources based on the
470 // given search types.
471 // If there are no resource types to search, then
474 resourceManager.deleteResources(searchTypes);
477 treeViewer.refresh();
478 favTreeViewer.refresh();
480 // Call native method to find Resources
481 boolean result = resourceManager
482 .findResourceRequest(searchTypes);
484 searchUIOperation(false);
487 .openError(Display.getDefault()
489 "Find Resource status",
490 "Operation failed due to some problems in core layer.");
493 // Store this information for refresh
496 .setLastKnownSearchTypes(searchTypes);
498 // Change the refresh visibility
499 refreshButton.setEnabled(true);
506 refreshButton.addSelectionListener(new SelectionAdapter() {
508 public void widgetSelected(SelectionEvent e) {
509 Set<String> searchTypes = resourceManager
510 .getLastKnownSearchTypes();
511 setFoundResource(false);
513 // Delete cached details of resources based on the given search
515 // If there are no resource types to search, then all resources
517 resourceManager.deleteResources(searchTypes);
520 treeViewer.refresh();
521 favTreeViewer.refresh();
523 // Call native method to find Resources
524 boolean result = resourceManager
525 .findResourceRequest(searchTypes);
527 searchUIOperation(true);
530 .openError(Display.getDefault().getActiveShell(),
531 "Find Resource status",
532 "Operation failed due to some problems in core layer.");
537 // Below code adds a listener to the tree for selection changes
538 // and notifies the resource manager
539 ISelectionChangedListener treeSelectionListener = new ISelectionChangedListener() {
542 public void selectionChanged(SelectionChangedEvent e) {
543 if (e.getSelection().isEmpty()) {
546 if (e.getSelection() instanceof IStructuredSelection) {
547 IStructuredSelection selection = (IStructuredSelection) e
549 RemoteResource resource = (RemoteResource) selection
551 if (null == resource) {
554 resourceManager.resourceSelectionChanged(resource);
559 treeViewer.addSelectionChangedListener(treeSelectionListener);
560 favTreeViewer.addSelectionChangedListener(treeSelectionListener);
563 // If refresh is true, then Refresh Dialog else Find Dialog will be shown.
564 private void searchUIOperation(boolean refresh) {
565 final MessageDialog targetDialog;
567 targetDialog = refreshDialog;
569 targetDialog = findDialog;
571 // Open the dialog in a new thread.
572 PlatformUI.getWorkbench().getDisplay().syncExec(new Thread() {
576 if (isFoundResource()) {
577 setFoundResource(false);
581 PlatformUI.getWorkbench().getDisplay().asyncExec(new Thread() {
584 targetDialog.open(); // This method returns once the
585 // cancel button is pressed.
587 // Interrupt the sleep thread.
588 if (null != sleepThreadHandle
589 && sleepThreadHandle.isAlive()) {
590 sleepThreadHandle.interrupt();
593 // Set the status of find.
594 setFoundResource(false);
598 // Thread for find time-out.
599 sleepThreadHandle = new Thread() {
604 Thread.sleep(Constants.FIND_RESOURCES_TIMEOUT * 1000);
605 } catch (InterruptedException e) {
609 child = new Thread() {
612 if (null != targetDialog) {
613 targetDialog.close();
615 // Check if any new resources are
617 // Is no new resources, then display a
619 if (!isFoundResource()) {
625 "No servers are available as of now.\n"
626 + "Please check the servers' status and press"
627 + "'Refresh' button to restart the search.");
629 // Resetting the status to false for
631 setFoundResource(false);
637 PlatformUI.getWorkbench().getDisplay().syncExec(child);
640 sleepThreadHandle.start();
645 private void addManagerListeners() {
646 UiListenerHandler.getInstance().addFindresourceUIListener(findListener);
650 public void dispose() {
651 // Unregister the listener
652 if (null != findListener) {
653 UiListenerHandler.getInstance().removeFindresourceUIListener(
655 resourceManager.resourceSelectionChanged(null);
661 public void setFocus() {
664 public synchronized void setFoundResource(boolean value) {
665 foundResource = value;
668 public synchronized boolean isFoundResource() {
669 return foundResource;
673 class TreeContentProvider implements ITreeContentProvider {
676 public void dispose() {
680 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
685 public Object[] getChildren(Object parent) {
690 public Object[] getElements(Object parent) {
691 List<RemoteResource> resourceList = Activator.getDefault()
692 .getResourceManager().getResourceList();
693 return resourceList.toArray();
697 public Object getParent(Object child) {
702 public boolean hasChildren(Object parent) {
708 class FavTreeContentProvider implements ITreeContentProvider {
711 public void dispose() {
715 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
720 public Object[] getChildren(Object parent) {
725 public Object[] getElements(Object parent) {
726 List<RemoteResource> resourceList = Activator.getDefault()
727 .getResourceManager().getFavResourceList();
728 return resourceList.toArray();
732 public Object getParent(Object child) {
737 public boolean hasChildren(Object parent) {
743 class TreeLabelProvider extends LabelProvider {
745 public String getText(Object element) {
746 RemoteResource resource = (RemoteResource) element;
747 return resource.getRemoteResourceRef().getURI();
751 public Image getImage(Object element) {