forked from bndtools/bnd
/
Model.java
149 lines (125 loc) · 2.99 KB
/
Model.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package bndtools.explorer;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
import org.eclipse.core.resources.IProject;
import org.eclipse.swt.widgets.Display;
import aQute.bnd.build.Workspace;
import aQute.bnd.exceptions.Exceptions;
import aQute.lib.strings.Strings;
import aQute.libg.glob.Glob;
import bndtools.central.Central;
class Model {
Glob glob;
IProject selectedProject;
Object selection;
String prompt;
String message = "initializing workspace";
int severity;
String filterText;
final List<Runnable> updates = new ArrayList<>();
final AtomicBoolean dirty = new AtomicBoolean(false);
final Set<IProject> pinned = new HashSet<>();
void setSelectedProject(IProject project) {
if (project != selectedProject) {
this.selectedProject = project;
update();
}
}
void closeProject(IProject selectedProject) {
if (this.selectedProject == selectedProject)
setSelectedProject(null);
}
void setFilterText(String value) {
if (Objects.equals(this.filterText, value))
return;
this.filterText = value;
if (value == null)
glob = null;
else
glob = new Glob(value);
update();
}
void setPrompt(String prompt) {
if (Objects.equals(prompt, this.prompt))
return;
this.prompt = prompt;
updateMessage();
}
void setMessage(String message) {
if (Objects.equals(this.message, message))
return;
this.message = message;
update();
}
boolean isPinned(IProject project) {
return pinned.contains(project);
}
void updateMessage() {
Central.onAnyWorkspace(ws -> setMessage(getPrompt(ws)));
}
void setSeverity(int severity) {
if (this.severity != severity) {
this.severity = severity;
update();
}
}
private String getPrompt(Workspace ws) {
try {
if (prompt == null || prompt.isEmpty())
prompt = "<b>${basename;${workspace}}</b> ${def;Bundle-Version} <a href='prefs'>change macro def</a>";
else if ("-".equals(prompt))
return "";
String s = ws.getReplacer()
.process(prompt);
s = Strings.removeQuotes(s);
return s;
} catch (Exception e) {
throw Exceptions.duck(e);
}
}
void onUpdate(Runnable runnable) {
updates.add(runnable);
}
void update() {
if (dirty.getAndSet(true))
return;
Display.getDefault()
.asyncExec(this::update0);
}
/*
* This runs async on the display thread.
*/
private void update0() {
try {
// coalesce some more updates on
// the worker thread(s).
Thread.sleep(10);
} catch (InterruptedException e) {
Thread.currentThread()
.interrupt();
return;
}
if (dirty.getAndSet(false)) {
updates.forEach(Runnable::run);
}
}
void doPin() {
if (!(selection instanceof IProject))
return;
IProject p = (IProject) selection;
if (pinned.contains(p)) {
pinned.remove(p);
} else {
pinned.add(p);
}
update();
}
void setActualSelection(Object selection) {
this.selection = selection;
update();
}
}