View Javadoc

1   /*
2    * This file is a part of CAST project.
3    * (c) Copyright 2007, AGH University of Science & Technology
4    * https://caribou.iisg.agh.edu.pl/trac/cast
5    *
6    * Licensed under the Eclipse Public License, Version 1.0 (the "License").
7    * You may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    * http://www.eclipse.org/legal/epl-v10.html
10   */
11  /*
12   * File: CastApplication.java
13   * Created: 2007-00-00
14   * Author: cast
15   * $Id: CastApplication.java 3266 2009-08-27 15:28:45Z tmilos $
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   * This class controls all aspects of the application's execution.
39   *
40   * @author AGH CAST Team
41   */
42  public class CastApplication implements IApplication, IPropertyChangeProvider {
43  
44  	/**
45  	 * Id of the <em>Active project changed</em> property.
46  	 */
47  	public static final String PROPERTY_ACTIVE_PROJECT = "CastApplication.ActiveProject"; //$NON-NLS-1$
48  
49  	/**
50  	 * Bundle Id.
51  	 */
52  	public static final String ID = "pl.edu.agh.cast"; //$NON-NLS-1$
53  
54  	private static final String PROJECT_ARG = "project="; //$NON-NLS-1$
55  
56  	public static final String KEY_SCHEME_ID = "pl.edu.agh.cast.keys.scheme"; //$NON-NLS-1$
57  
58  	private static Display display;
59  
60  	private static Shell shell;
61  
62  	private static Clipboard clipboard;
63  
64  	/**
65  	 * Active project location read as an command line argument 'project=...'. Contains project directory location.
66  	 */
67  	private static String activeProjectLocation;
68  
69  	/**
70  	 * Active project for current workbench
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  	 * Sets new active project.
94  	 *
95  	 * Note: should be used only by <code>ApplicationWorkbenchAdvisor</code>.
96  	 *
97  	 * @param activeProjectArg
98  	 *            new active project
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 	 * Returns the active {@link Shell}.
108 	 *
109 	 * @return the active {@link Shell}
110 	 */
111 	public static Shell getActiveShell() {
112 		if (shell == null) {
113 			shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
114 		}
115 		return shell;
116 	}
117 
118 	/**
119 	 * Displays a confirmation dialog.
120 	 *
121 	 * @param title
122 	 *            dialog title
123 	 * @param message
124 	 *            message to confirm
125 	 * @return confirmation result
126 	 */
127 	public static boolean confirmationDialog(String title, String message) {
128 		return MessageDialog.openConfirm(getActiveShell(), title, message);
129 	}
130 
131 	/**
132 	 * Displays an error dialog.
133 	 *
134 	 * @param title
135 	 *            dialog title
136 	 * @param message
137 	 *            error message
138 	 */
139 	public static void errorDialog(String title, String message) {
140 		MessageDialog.openError(getActiveShell(), title, message);
141 	}
142 
143 	/**
144 	 * Displays a warning dialog.
145 	 *
146 	 * @param title
147 	 *            dialog title
148 	 * @param message
149 	 *            warning message
150 	 */
151 	public static void warningDialog(String title, String message) {
152 		MessageDialog.openWarning(getActiveShell(), title, message);
153 	}
154 
155 	/**
156 	 * Displays an information dialog.
157 	 *
158 	 * @param title
159 	 *            dialog title
160 	 * @param message
161 	 *            information message
162 	 */
163 	public static void informationDialog(String title, String message) {
164 		MessageDialog.openInformation(getActiveShell(), title, message);
165 	}
166 
167 	/**
168 	 * Displays a Y/N question dialog.
169 	 *
170 	 * @param title
171 	 *            dialog title
172 	 * @param message
173 	 *            question to confirm
174 	 * @return the answer
175 	 */
176 	public static boolean questionDialog(String title, String message) {
177 		return MessageDialog.openQuestion(getActiveShell(), title, message);
178 	}
179 
180 	/**
181 	 * {@inheritDoc}
182 	 *
183 	 * @see org.eclipse.equinox.app.IApplication#start(org.eclipse.equinox.app.IApplicationContext)
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 	 * {@inheritDoc}
207 	 *
208 	 * @see org.eclipse.equinox.app.IApplication#stop()
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 				// if project file path point to a file it is cut to file's
220 				// parent
221 				if (projectLocationFile.exists() && projectLocationFile.isFile()) {
222 					activeProjectLocation = projectLocationFile.getParent();
223 				} else {
224 					activeProjectLocation = projectLocation;
225 				}
226 			}
227 		}
228 	}
229 
230 	/**
231 	 * {@inheritDoc}
232 	 *
233 	 * @see pl.edu.agh.cast.data.model.property.IPropertyChangeProvider#addPropertyChangeListener(java.beans.PropertyChangeListener)
234 	 */
235 	public void addPropertyChangeListener(PropertyChangeListener l) {
236 		pcpHelper.addPropertyChangeListener(l);
237 	}
238 
239 	/**
240 	 * {@inheritDoc}
241 	 *
242 	 * @see pl.edu.agh.cast.data.model.property.IPropertyChangeProvider
243 	 *      #removePropertyChangeListener(java.beans.PropertyChangeListener)
244 	 */
245 	public void removePropertyChangeListener(PropertyChangeListener l) {
246 		pcpHelper.removePropertyChangeListener(l);
247 	}
248 
249 	public static IPropertyChangeProvider getPropertyChangeProvider() {
250 		return pcpHelper;
251 	}
252 }