-
Notifications
You must be signed in to change notification settings - Fork 4
/
LockResourcesPart.java
115 lines (101 loc) · 3.53 KB
/
LockResourcesPart.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package org.eclipse.platform.resources;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.observable.value.WritableValue;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceVisitor;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.databinding.swt.typed.WidgetProperties;
import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.jface.layout.GridLayoutFactory;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import com.xored.q7.quality.mockups.issues.BaseMockupPart;
public class LockResourcesPart extends BaseMockupPart {
private Map<IFile, InputStream> files = new HashMap<IFile, InputStream>();
private DataBindingContext dbc = new DataBindingContext();
private WritableValue<String> value = new WritableValue<>("", String.class);
public String getLabel() {
return "Resource Locking";
}
public Control construct(Composite section) {
GridLayoutFactory.swtDefaults().numColumns(1).applyTo(section);
Composite client = new Composite(section, SWT.NONE);
GridLayoutFactory.swtDefaults().numColumns(2).applyTo(client);
Button lockResources = new Button(client, SWT.PUSH);
Button unlockResources = new Button(client, SWT.PUSH);
lockResources.setText("Lock resources");
lockResources.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
lockResources();
}
});
unlockResources.setText("Unlock resources");
unlockResources.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
unlockResources();
}
});
client.addDisposeListener(new DisposeListener() {
public void widgetDisposed(DisposeEvent e) {
unlockResources();
}
});
Label msg = new Label(client, SWT.NONE);
dbc.bindValue(value, WidgetProperties.text().observe(msg));
value.setValue("Unlocked");
GridDataFactory.fillDefaults().grab(true, false).span(2, 1)
.applyTo(msg);
return client;
}
@SuppressWarnings("resource")
protected void unlockResources() {
if (files.size() > 0) {
for (InputStream stream : files.values()) {
try {
stream.close();
} catch (IOException e) {
throw new AssertionError(e);
}
}
}
files.clear();
value.setValue("Unlocked");
}
protected void lockResources() {
if (files.size() == 0) {
try {
ResourcesPlugin.getWorkspace().getRoot()
.accept(new IResourceVisitor() {
@SuppressWarnings("resource")
public boolean visit(IResource resource)
throws CoreException {
if (resource.isAccessible()
&& resource.getType() == IResource.FILE) {
files.put((IFile) resource,
((IFile) resource).getContents());
}
return resource.isAccessible();
}
});
} catch (CoreException e) {
e.printStackTrace();
}
if (files.size() > 0) {
value.setValue("Locked:" + files.size());
}
}
}
}