1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package pl.edu.agh.cast;
19
20 import java.beans.PropertyChangeListener;
21 import java.io.File;
22
23 import org.eclipse.core.resources.IProject;
24 import org.eclipse.equinox.app.IApplication;
25 import org.eclipse.equinox.app.IApplicationContext;
26 import org.eclipse.jface.dialogs.MessageDialog;
27 import org.eclipse.swt.dnd.Clipboard;
28 import org.eclipse.swt.widgets.Display;
29 import org.eclipse.swt.widgets.Shell;
30 import org.eclipse.ui.PlatformUI;
31
32 import pl.edu.agh.cast.data.model.property.IPropertyChangeProvider;
33 import pl.edu.agh.cast.data.model.property.PropertyChangeProviderHelper;
34 import pl.edu.agh.cast.project.UserPreferences;
35 import pl.edu.agh.cast.ui.advisor.ApplicationWorkbenchAdvisor;
36
37
38
39
40
41
42 public class CastApplication implements IApplication, IPropertyChangeProvider {
43
44
45
46
47 public static final String PROPERTY_ACTIVE_PROJECT = "CastApplication.ActiveProject";
48
49
50
51
52 public static final String ID = "pl.edu.agh.cast";
53
54 private static final String PROJECT_ARG = "project=";
55
56 public static final String KEY_SCHEME_ID = "pl.edu.agh.cast.keys.scheme";
57
58 private static Display display;
59
60 private static Shell shell;
61
62 private static Clipboard clipboard;
63
64
65
66
67 private static String activeProjectLocation;
68
69
70
71
72 private static IProject activeProject;
73
74 private static PropertyChangeProviderHelper pcpHelper;
75
76 public static Display getDisplay() {
77 return display;
78 }
79
80 public static Clipboard getClipboard() {
81 return clipboard;
82 }
83
84 public static String getActiveProjectLocation() {
85 return activeProjectLocation;
86 }
87
88 public static IProject getActiveProject() {
89 return activeProject;
90 }
91
92
93
94
95
96
97
98
99
100 public static void setActiveProject(IProject activeProjectArg) {
101 IProject oldProject = activeProject;
102 activeProject = activeProjectArg;
103 pcpHelper.firePropertyChange(PROPERTY_ACTIVE_PROJECT, oldProject, activeProject);
104 }
105
106
107
108
109
110
111 public static Shell getActiveShell() {
112 if (shell == null) {
113 shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
114 }
115 return shell;
116 }
117
118
119
120
121
122
123
124
125
126
127 public static boolean confirmationDialog(String title, String message) {
128 return MessageDialog.openConfirm(getActiveShell(), title, message);
129 }
130
131
132
133
134
135
136
137
138
139 public static void errorDialog(String title, String message) {
140 MessageDialog.openError(getActiveShell(), title, message);
141 }
142
143
144
145
146
147
148
149
150
151 public static void warningDialog(String title, String message) {
152 MessageDialog.openWarning(getActiveShell(), title, message);
153 }
154
155
156
157
158
159
160
161
162
163 public static void informationDialog(String title, String message) {
164 MessageDialog.openInformation(getActiveShell(), title, message);
165 }
166
167
168
169
170
171
172
173
174
175
176 public static boolean questionDialog(String title, String message) {
177 return MessageDialog.openQuestion(getActiveShell(), title, message);
178 }
179
180
181
182
183
184
185 public Object start(IApplicationContext context) throws Exception {
186 display = PlatformUI.createDisplay();
187 clipboard = new Clipboard(display);
188 pcpHelper = new PropertyChangeProviderHelper(this);
189
190 parseArguments((String[])context.getArguments().get(IApplicationContext.APPLICATION_ARGS));
191 UserPreferences.getInstance().initializeDefaults();
192
193 try {
194 int returnCode = PlatformUI.createAndRunWorkbench(display, new ApplicationWorkbenchAdvisor());
195 if (returnCode == PlatformUI.RETURN_RESTART) {
196 return IApplication.EXIT_RESTART;
197 }
198 return IApplication.EXIT_OK;
199 } finally {
200 clipboard.dispose();
201 display.dispose();
202 }
203 }
204
205
206
207
208
209
210 public void stop() {
211 }
212
213 private static void parseArguments(String[] args) {
214 for (String arg : args) {
215 if (arg.startsWith(PROJECT_ARG)) {
216
217 String projectLocation = arg.substring(PROJECT_ARG.length());
218 File projectLocationFile = new File(projectLocation);
219
220
221 if (projectLocationFile.exists() && projectLocationFile.isFile()) {
222 activeProjectLocation = projectLocationFile.getParent();
223 } else {
224 activeProjectLocation = projectLocation;
225 }
226 }
227 }
228 }
229
230
231
232
233
234
235 public void addPropertyChangeListener(PropertyChangeListener l) {
236 pcpHelper.addPropertyChangeListener(l);
237 }
238
239
240
241
242
243
244
245 public void removePropertyChangeListener(PropertyChangeListener l) {
246 pcpHelper.removePropertyChangeListener(l);
247 }
248
249 public static IPropertyChangeProvider getPropertyChangeProvider() {
250 return pcpHelper;
251 }
252 }