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: ProjectStartupDialog.java
13   * Created: 2007-00-00
14   * Author: cast
15   * $Id: ProjectStartupDialog.java 3266 2009-08-27 15:28:45Z tmilos $
16   */
17  
18  package pl.edu.agh.cast.ui.dialogs;
19  
20  import java.util.List;
21  
22  import org.apache.log4j.Logger;
23  import org.eclipse.core.runtime.IPath;
24  import org.eclipse.jface.viewers.CellLabelProvider;
25  import org.eclipse.jface.viewers.TableViewer;
26  import org.eclipse.jface.viewers.TableViewerColumn;
27  import org.eclipse.jface.viewers.ViewerCell;
28  import org.eclipse.swt.SWT;
29  import org.eclipse.swt.custom.CLabel;
30  import org.eclipse.swt.custom.TableEditor;
31  import org.eclipse.swt.events.DisposeEvent;
32  import org.eclipse.swt.events.DisposeListener;
33  import org.eclipse.swt.events.MouseAdapter;
34  import org.eclipse.swt.events.MouseEvent;
35  import org.eclipse.swt.events.MouseTrackAdapter;
36  import org.eclipse.swt.events.PaintEvent;
37  import org.eclipse.swt.events.PaintListener;
38  import org.eclipse.swt.events.SelectionAdapter;
39  import org.eclipse.swt.events.SelectionEvent;
40  import org.eclipse.swt.events.ShellAdapter;
41  import org.eclipse.swt.events.ShellEvent;
42  import org.eclipse.swt.graphics.FontData;
43  import org.eclipse.swt.layout.FillLayout;
44  import org.eclipse.swt.layout.FormAttachment;
45  import org.eclipse.swt.layout.FormData;
46  import org.eclipse.swt.layout.FormLayout;
47  import org.eclipse.swt.layout.RowData;
48  import org.eclipse.swt.layout.RowLayout;
49  import org.eclipse.swt.widgets.Composite;
50  import org.eclipse.swt.widgets.Display;
51  import org.eclipse.swt.widgets.Event;
52  import org.eclipse.swt.widgets.Group;
53  import org.eclipse.swt.widgets.Label;
54  import org.eclipse.swt.widgets.Link;
55  import org.eclipse.swt.widgets.Listener;
56  import org.eclipse.swt.widgets.Shell;
57  import org.eclipse.swt.widgets.TableItem;
58  
59  import pl.edu.agh.cast.Activator;
60  import pl.edu.agh.cast.navigator.ui.action.CreateProjectAction;
61  import pl.edu.agh.cast.navigator.ui.action.OpenProjectAction;
62  import pl.edu.agh.cast.project.MostRecentlyUsedProject;
63  import pl.edu.agh.cast.project.ProjectUtil;
64  import pl.edu.agh.cast.project.UserPreferences;
65  import pl.edu.agh.cast.ui.util.MsgBoxHelper;
66  import pl.edu.agh.cast.ui.util.SWTHelper;
67  import pl.edu.agh.cast.util.Messages;
68  
69  import com.cloudgarden.resource.SWTResourceManager;
70  
71  /*
72   * This code was edited or generated using CloudGarden's Jigloo
73   * SWT/Swing GUI Builder, which is free for non-commercial
74   * use. If Jigloo is being used commercially (ie, by a corporation,
75   * company or business for any purpose whatever) then you
76   * should purchase a license for each developer using Jigloo.
77   * Please visit www.cloudgarden.com for details.
78   * Use of Jigloo implies acceptance of these licensing terms.
79   * A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
80   * THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
81   * LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
82   */
83  
84  /**
85   * Dialog used for selecting project on startup.
86   *
87   * @author AGH CAST Team
88   */
89  public class ProjectStartupDialog extends org.eclipse.swt.widgets.Dialog {
90  
91  	private static Logger log = Activator.getLogger();
92  
93  	private Shell dialogShell;
94  
95  	private CLabel cLabel1;
96  
97  	private TableViewerColumn colName;
98  
99  	private Group group1;
100 
101 	private Label label1;
102 
103 	private Link link5;
104 
105 	private Group group2;
106 
107 	private Label label5;
108 
109 	private Label label4;
110 
111 	private Link link3;
112 
113 	private Link link1;
114 
115 	private Link link2;
116 
117 	private Composite composite1;
118 
119 	private TableViewerColumn colDir;
120 
121 	private TableViewer tableViewer1;
122 
123 	private String defaultFontName;
124 
125 	/**
126 	 * Creates new startup dialog.
127 	 *
128 	 * @param parent
129 	 *            parent {@link Shell}
130 	 * @param style
131 	 *            SWT window style
132 	 */
133 	public ProjectStartupDialog(Shell parent, int style) {
134 		super(parent, style);
135 	}
136 
137 	/**
138 	 * Opens the dialog.
139 	 */
140 	public void open() {
141 		try {
142 			Shell parent = getParent();
143 			dialogShell = new Shell(parent, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
144 
145 			// Register as a resource user - SWTResourceManager will
146 			// handle the obtaining and disposing of resources
147 			SWTResourceManager.registerResourceUser(dialogShell);
148 
149 			FontData[] fontDatas = dialogShell.getFont().getFontData();
150 			defaultFontName = fontDatas[0].getName();
151 
152 			List<MostRecentlyUsedProject> projects = UserPreferences.getInstance().getMostRecentlyUsedProjects();
153 
154 			dialogShell.setLayout(new FormLayout());
155 			dialogShell.setSize(543, 476);
156 			dialogShell.layout();
157 			dialogShell.pack();
158 			dialogShell.setText(Messages.ProjectStartupDialog_0);
159 			dialogShell.setSize(543, 455);
160 
161 			cLabel1 = new CLabel(dialogShell, SWT.NONE);
162 			FormData cLabel1LData = new FormData();
163 			cLabel1LData.width = 310;
164 			cLabel1LData.height = 22;
165 			cLabel1LData.left = new FormAttachment(0, 1000, 6);
166 			cLabel1LData.top = new FormAttachment(0, 1000, 11);
167 			cLabel1.setLayoutData(cLabel1LData);
168 			cLabel1.setText(Messages.ProjectStartupDialog_0);
169 			cLabel1.setFont(SWTResourceManager.getFont(defaultFontName, 10, 1, false, false));
170 
171 			group1 = new Group(dialogShell, SWT.NONE);
172 			RowLayout group1Layout = new RowLayout(org.eclipse.swt.SWT.HORIZONTAL);
173 			group1Layout.marginTop = 10;
174 			group1Layout.marginLeft = 5;
175 			group1.setLayout(group1Layout);
176 			FormData group1LData = new FormData();
177 			group1LData.width = 505;
178 			group1LData.height = 34;
179 			group1LData.left = new FormAttachment(0, 1000, 12);
180 			group1LData.top = new FormAttachment(0, 1000, 45);
181 			group1LData.right = new FormAttachment(1000, 1000, -12);
182 			group1.setLayoutData(group1LData);
183 			group1.setText(Messages.ProjectStartupDialog_1);
184 
185 			RowData label4LData = new RowData();
186 			label4 = new Label(group1, SWT.NONE);
187 			label4.setLayoutData(label4LData);
188 
189 			link3 = new Link(group1, SWT.NONE);
190 			RowData link3LData = new RowData();
191 			link3.setLayoutData(link3LData);
192 			link3.setText(Messages.ProjectStartupDialog_2);
193 			link3.addSelectionListener(new SelectionAdapter() {
194 				@Override
195 				public void widgetSelected(SelectionEvent evt) {
196 					link3WidgetSelected(evt);
197 				}
198 			});
199 
200 			RowData label5LData = new RowData();
201 			label5 = new Label(group1, SWT.NONE);
202 			label5.setLayoutData(label5LData);
203 
204 			group2 = new Group(dialogShell, SWT.NONE);
205 			FormLayout group2Layout = new FormLayout();
206 			group2.setLayout(group2Layout);
207 			FormData group2LData = new FormData();
208 			group2LData.width = 504;
209 			group2LData.height = 286;
210 			group2LData.left = new FormAttachment(0, 1000, 13);
211 			group2LData.top = new FormAttachment(0, 1000, 107);
212 			group2LData.bottom = new FormAttachment(1000, 1000, -12);
213 			group2LData.right = new FormAttachment(1000, 1000, -12);
214 			group2.setLayoutData(group2LData);
215 			group2.setText(Messages.ProjectStartupDialog_3);
216 
217 			link5 = new Link(group2, SWT.NONE);
218 			FormData link5LData = new FormData();
219 			link5LData.left = new FormAttachment(0, 1000, 9);
220 			link5LData.top = new FormAttachment(0, 1000, 55);
221 			link5LData.width = -1;
222 			link5LData.height = -1;
223 			link5.setLayoutData(link5LData);
224 			link5.setText(Messages.ProjectStartupDialog_13);
225 			link5.addSelectionListener(new SelectionAdapter() {
226 				@Override
227 				public void widgetSelected(SelectionEvent evt) {
228 					link1WidgetSelected(evt);
229 				}
230 			});
231 
232 			label1 = new Label(group2, SWT.NONE);
233 			FormData label1LData = new FormData();
234 			label1LData.left = new FormAttachment(0, 1000, 9);
235 			label1LData.top = new FormAttachment(0, 1000, 77);
236 			label1LData.width = -1;
237 			label1LData.height = -1;
238 			label1.setLayoutData(label1LData);
239 			label1.setText(Messages.ProjectStartupDialog_4);
240 
241 			FormData composite1LData = new FormData();
242 			composite1LData.left = new FormAttachment(0, 1000, 9);
243 			composite1LData.top = new FormAttachment(0, 1000, 95);
244 			composite1LData.width = 486;
245 			composite1LData.height = 185;
246 			composite1 = new Composite(group2, SWT.BORDER);
247 			FillLayout composite1Layout = new FillLayout(SWT.HORIZONTAL);
248 			composite1Layout.marginHeight = 0;
249 			composite1Layout.marginWidth = 0;
250 			composite1.setLayout(composite1Layout);
251 			composite1.setLayoutData(composite1LData);
252 
253 			tableViewer1 = new TableViewer(composite1, SWT.FULL_SELECTION);
254 
255 			colName = new TableViewerColumn(tableViewer1, SWT.NONE);
256 			colName.getColumn().setText(Messages.ProjectStartupDialog_5);
257 			colName.getColumn().setWidth(160);
258 			colName.getColumn().setResizable(false);
259 			colName.setLabelProvider(new ProjectNameLocationCellLabelProvider());
260 
261 			colDir = new TableViewerColumn(tableViewer1, SWT.NONE);
262 			colDir.getColumn().setText(Messages.ProjectStartupDialog_6);
263 			colDir.getColumn().setWidth(325);
264 			colDir.getColumn().setResizable(false);
265 			colDir.setLabelProvider(new ProjectNameLocationCellLabelProvider());
266 
267 			tableViewer1.getTable().addListener(SWT.MeasureItem, new MRUListener());
268 			tableViewer1.setItemCount(projects.size());
269 
270 			link2 = new Link(group2, SWT.NONE);
271 			FormData link2LData = new FormData();
272 			link2LData.left = new FormAttachment(0, 1000, 9);
273 			link2LData.top = new FormAttachment(0, 1000, 11);
274 			link2LData.width = -1;
275 			link2LData.height = -1;
276 			link2.setLayoutData(link2LData);
277 			link2.setText(Messages.ProjectStartupDialog_7);
278 			link2.addSelectionListener(new SelectionAdapter() {
279 				@Override
280 				public void widgetSelected(SelectionEvent evt) {
281 					link2WidgetSelected(evt);
282 				}
283 			});
284 
285 			link1 = new Link(group2, SWT.NONE);
286 			FormData link1LData = new FormData();
287 			link1LData.left = new FormAttachment(0, 1000, 9);
288 			link1LData.top = new FormAttachment(0, 1000, 33);
289 			link1LData.width = -1;
290 			link1LData.height = -1;
291 			link1.setLayoutData(link1LData);
292 			link1.setText(Messages.ProjectStartupDialog_8);
293 			link1.addSelectionListener(new SelectionAdapter() {
294 				@Override
295 				public void widgetSelected(SelectionEvent evt) {
296 					link1WidgetSelected(evt);
297 				}
298 			});
299 
300 			SWTHelper.placeDialogInCenter(dialogShell, getParent());
301 			dialogShell.open();
302 			dialogShell.addPaintListener(new PaintListener() {
303 				private boolean firstPaint = true;
304 
305 				public void paintControl(PaintEvent e) {
306 					if (firstPaint) {
307 						firstPaint = false;
308 						updateTableViewer();
309 					}
310 				}
311 			});
312 			dialogShell.addShellListener(new ShellAdapter() {
313 				@Override
314 				public void shellActivated(ShellEvent e) {
315 					/*
316 					 * Do this to fix some strange repainting bugs in WinXP
317 					 */
318 					updateTableViewer();
319 					tableViewer1.getTable().select(0);
320 				}
321 			});
322 
323 			updateTableViewer();
324 			tableViewer1.getTable().select(0);
325 
326 			Display display = dialogShell.getDisplay();
327 			while (!dialogShell.isDisposed()) {
328 				if (!display.readAndDispatch()) {
329 					display.sleep();
330 				}
331 			}
332 		} catch (Exception e) {
333 			e.printStackTrace();
334 		}
335 	}
336 
337 	private void link3WidgetSelected(SelectionEvent evt) {
338 		dialogShell.close();
339 		new CreateProjectAction().run(null);
340 	}
341 
342 	private void link2WidgetSelected(SelectionEvent evt) {
343 		dialogShell.close();
344 		new OpenProjectAction().run(null);
345 	}
346 
347 	private void tryOpenProject(MostRecentlyUsedProject mrup) {
348 		boolean res = ProjectUtil.getInstance().tryOpenRecentProject(mrup);
349 		if (res) {
350 			dialogShell.close();
351 		} else {
352 			// refresh MRU list, project may have been removed
353 			updateTableViewer();
354 		}
355 	}
356 
357 	private void updateTableViewer() {
358 		tableViewer1.setItemCount(0);
359 		List<MostRecentlyUsedProject> projects = UserPreferences.getInstance().getMostRecentlyUsedProjects();
360 		tableViewer1.add(projects.toArray());
361 		link1.setEnabled(projects.size() > 0);
362 	}
363 
364 	private void link1WidgetSelected(SelectionEvent evt) {
365 		if ("recent-always".equals(evt.text)) { //$NON-NLS-1$
366 			UserPreferences.getInstance().setPreference(UserPreferences.Pref.SHOW_PROJECT_STARTUP_DIALOG,
367 			        Boolean.toString(false));
368 		}
369 		List<MostRecentlyUsedProject> mruProjects = UserPreferences.getInstance().getMostRecentlyUsedProjects();
370 		if (mruProjects.size() > 0) {
371 			ProjectUtil.getInstance().tryOpenRecentProject(mruProjects.get(0));
372 		} else {
373 			MsgBoxHelper
374 			        .showWarningBox(dialogShell, Messages.ProjectStartupDialog_11, Messages.ProjectStartupDialog_12);
375 		}
376 		dialogShell.close();
377 	}
378 
379 	/**
380 	 *
381 	 *
382 	 * @author AGH CAST Team
383 	 */
384 	private final class MRUListener implements Listener {
385 
386 		public void handleEvent(Event event) {
387 			switch (event.type) {
388 				case SWT.MeasureItem:
389 					event.height = (event.gc.getFontMetrics().getHeight() + 2) * 2;
390 					break;
391 				default:
392 					break;
393 			}
394 
395 		}
396 	}
397 
398 	/**
399 	 * Provider of project name and location labels.
400 	 *
401 	 * @author AGH CAST Team
402 	 */
403 	private final class ProjectNameLocationCellLabelProvider extends CellLabelProvider {
404 
405 		/**
406 		 * Mouse track adapter for selecting recent projects.
407 		 *
408 		 * @author AGH CAST Team
409 		 */
410 		private final class MouseTrackAdapterExtension extends MouseTrackAdapter {
411 			private final Composite comp;
412 
413 			private final Composite compDirs;
414 
415 			private MouseTrackAdapterExtension(Composite comp, Composite compDirs) {
416 				this.comp = comp;
417 				this.compDirs = compDirs;
418 			}
419 
420 			@Override
421 			public void mouseEnter(MouseEvent e) {
422 				comp.setBackground(comp.getDisplay().getSystemColor(SWT.COLOR_INFO_BACKGROUND));
423 				comp.setForeground(comp.getDisplay().getSystemColor(SWT.COLOR_INFO_FOREGROUND));
424 				compDirs.setBackground(compDirs.getDisplay().getSystemColor(SWT.COLOR_INFO_BACKGROUND));
425 				compDirs.setForeground(compDirs.getDisplay().getSystemColor(SWT.COLOR_INFO_FOREGROUND));
426 			}
427 
428 			@Override
429 			public void mouseExit(MouseEvent e) {
430 				comp.setBackground(comp.getDisplay().getSystemColor(SWT.COLOR_LIST_BACKGROUND));
431 				comp.setForeground(comp.getDisplay().getSystemColor(SWT.COLOR_LIST_FOREGROUND));
432 				compDirs.setBackground(compDirs.getDisplay().getSystemColor(SWT.COLOR_LIST_BACKGROUND));
433 				compDirs.setForeground(compDirs.getDisplay().getSystemColor(SWT.COLOR_LIST_FOREGROUND));
434 			}
435 		}
436 
437 		/**
438 		 * Mouse adapter for selecting recent projects.
439 		 *
440 		 * @author AGH CAST Team
441 		 */
442 		private final class MouseAdapterExtension extends MouseAdapter {
443 			private final Composite comp;
444 
445 			private final MostRecentlyUsedProject mrup;
446 
447 			private MouseAdapterExtension(Composite comp, MostRecentlyUsedProject mrup) {
448 				this.comp = comp;
449 				this.mrup = mrup;
450 			}
451 
452 			@Override
453 			public void mouseUp(MouseEvent e) {
454 				if (e.x >= 0 && e.x < comp.getBounds().width && e.y >= 0 && e.y < comp.getBounds().height) {
455 					log.debug("Trying to open item " + mrup.getName()); //$NON-NLS-1$
456 					tryOpenProject(mrup);
457 				}
458 			}
459 		}
460 
461 		@Override
462 		public void update(final ViewerCell cell) {
463 			final TableItem item = (TableItem)cell.getItem();
464 			final TableEditor editor = new TableEditor(item.getParent());
465 
466 			final Composite comp = new Composite(item.getParent(), SWT.NONE);
467 			comp.setBackground(item.getBackground());
468 			comp.setBackgroundMode(SWT.INHERIT_FORCE);
469 			FillLayout l = new FillLayout();
470 			l.marginHeight = 0;
471 			l.marginWidth = 0;
472 			comp.setLayout(l);
473 			final CLabel link = new CLabel(comp, SWT.NONE);
474 			final MostRecentlyUsedProject mrup = (MostRecentlyUsedProject)item.getData();
475 			link.setText(mrup.getName());
476 			link.setFont(SWTResourceManager.getFont(defaultFontName, 10, SWT.BOLD));
477 
478 			final TableEditor editorDirs = new TableEditor(item.getParent());
479 
480 			final Composite compDirs = new Composite(item.getParent(), SWT.NONE);
481 			compDirs.setBackground(item.getBackground());
482 			compDirs.setBackgroundMode(SWT.INHERIT_FORCE);
483 			compDirs.setLayout(l);
484 			final CLabel label = new CLabel(compDirs, SWT.NONE);
485 			String s = ""; //$NON-NLS-1$
486 			for (IPath path : mrup.getLocations()) {
487 				s += "\n" + path.toOSString(); //$NON-NLS-1$
488 			}
489 			label.setText(s.substring(1));
490 
491 			item.addDisposeListener(new DisposeListener() {
492 				public void widgetDisposed(DisposeEvent event) {
493 					label.dispose();
494 					link.dispose();
495 					comp.dispose();
496 					compDirs.dispose();
497 					editor.dispose();
498 					editorDirs.dispose();
499 				}
500 			});
501 			MouseTrackAdapter mouseTrackAdapter = new MouseTrackAdapterExtension(comp, compDirs);
502 			link.addMouseListener(new MouseAdapterExtension(comp, mrup));
503 			label.addMouseListener(new MouseAdapterExtension(compDirs, mrup));
504 			link.addMouseTrackListener(mouseTrackAdapter);
505 			label.addMouseTrackListener(mouseTrackAdapter);
506 
507 			editor.grabHorizontal = true;
508 			editor.grabVertical = true;
509 			editor.setEditor(comp, item, 0);
510 			editorDirs.grabHorizontal = true;
511 			editorDirs.grabVertical = true;
512 			editorDirs.setEditor(compDirs, item, 1);
513 		}
514 
515 	}
516 
517 }