Imported Upstream version 1.1.0
[platform/upstream/iotivity.git] / service / simulator / java / eclipse-plugin / ServiceProviderPlugin / src / oic / simulator / serviceprovider / view / dialogs / DeleteResourceWizard.java
1 /*
2  * Copyright 2015 Samsung Electronics All Rights Reserved.
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package oic.simulator.serviceprovider.view.dialogs;
18
19 import org.eclipse.core.runtime.FileLocator;
20 import org.eclipse.core.runtime.IPath;
21 import org.eclipse.core.runtime.IProgressMonitor;
22 import org.eclipse.core.runtime.Path;
23 import org.eclipse.jface.operation.IRunnableWithProgress;
24 import org.eclipse.jface.resource.ImageDescriptor;
25 import org.eclipse.jface.wizard.Wizard;
26
27 import java.lang.reflect.InvocationTargetException;
28 import java.net.URL;
29 import java.util.Date;
30 import java.util.Set;
31
32 import org.oic.simulator.ILogger.Level;
33 import org.oic.simulator.SimulatorException;
34
35 import oic.simulator.serviceprovider.Activator;
36 import oic.simulator.serviceprovider.manager.ResourceManager;
37 import oic.simulator.serviceprovider.model.Resource;
38 import oic.simulator.serviceprovider.model.SingleResource;
39
40 /**
41  * This class creates a UI wizard for delete resource operation.
42  */
43 public class DeleteResourceWizard extends Wizard {
44
45     private DeleteResourcePage page;
46
47     private boolean            success;
48
49     public DeleteResourceWizard() {
50         setWindowTitle("Delete resources");
51         IPath path = new Path("/icons/oic_logo_64x64.png");
52         URL find = FileLocator.find(Activator.getDefault().getBundle(), path,
53                 null);
54         setDefaultPageImageDescriptor(ImageDescriptor.createFromURL(find));
55
56         setNeedsProgressMonitor(true);
57     }
58
59     @Override
60     public void addPages() {
61         page = new DeleteResourcePage();
62         addPage(page);
63     }
64
65     @Override
66     public boolean performFinish() {
67         if (null == page) {
68             return false;
69         }
70         try {
71             getContainer().run(true, false, new IRunnableWithProgress() {
72
73                 @Override
74                 public void run(IProgressMonitor monitor)
75                         throws InvocationTargetException, InterruptedException {
76                     ResourceManager resourceManager = Activator.getDefault()
77                             .getResourceManager();
78                     try {
79                         monitor.beginTask("Resource Deletion", 2);
80                         monitor.worked(1);
81                         Set<SingleResource> singleResources = page
82                                 .getSelectedSingleResourcesList();
83                         if (null != singleResources
84                                 && singleResources.size() > 0) {
85                             Activator.getDefault().getResourceManager()
86                                     .removeSingleResources(singleResources);
87
88                             Resource res = resourceManager
89                                     .getCurrentResourceInSelection();
90                             if (null != res && res instanceof SingleResource) {
91                                 if (singleResources
92                                         .contains((SingleResource) res)) {
93                                     resourceManager
94                                             .resourceSelectionChanged(null);
95                                 }
96                             }
97                         }
98                         monitor.worked(1);
99                         success = true;
100                     } catch (SimulatorException e) {
101                         success = false;
102                     } finally {
103                         monitor.done();
104                     }
105                 }
106             });
107         } catch (InvocationTargetException e) {
108             Activator.getDefault().getLogManager()
109                     .log(Level.ERROR.ordinal(), new Date(), e.getMessage());
110         } catch (InterruptedException e) {
111             Activator.getDefault().getLogManager()
112                     .log(Level.ERROR.ordinal(), new Date(), e.getMessage());
113         }
114         return true;
115     }
116
117     public boolean getStatus() {
118         return success;
119     }
120 }