Procházet zdrojové kódy

Merge pull request #822 from Yakindu/issue_821

Issue 821
Thomas Kutz před 9 roky
rodič
revize
2cc57e49c7

binární
plugins/org.yakindu.sct.examples.wizard/icons/collapseall.gif


binární
plugins/org.yakindu.sct.examples.wizard/icons/expandall.gif


+ 72 - 0
plugins/org.yakindu.sct.examples.wizard/src/org/yakindu/sct/examples/wizard/ExampleWizardImages.java

@@ -0,0 +1,72 @@
+/** 
+ * Copyright (c) 2015 committers of YAKINDU and others. 
+ * All rights reserved. This program and the accompanying materials 
+ * are made available under the terms of the Eclipse Public License v1.0 
+ * which accompanies this distribution, and is available at 
+ * http://www.eclipse.org/legal/epl-v10.html 
+ * Contributors:
+ * committers of YAKINDU - initial API and implementation
+ *
+*/
+package org.yakindu.sct.examples.wizard;
+
+import org.eclipse.jface.resource.ImageDescriptor;
+import org.eclipse.jface.resource.ImageRegistry;
+import org.eclipse.swt.graphics.Image;
+
+/**
+ * 
+ * @author andreas muelder - Initial contribution and API
+ * 
+ */
+public enum ExampleWizardImages {
+
+	COLLAPSE_ALL("icons/collapseall.gif"),
+	EXPAND_ALL("icons/expandall.gif");
+	
+	private final String path;
+
+	private ExampleWizardImages(String path) {
+		this.path = path;
+	}
+
+	/**
+	 * Returns an image. Clients do not need to dispose the image, it will be
+	 * disposed automatically.
+	 * 
+	 * @return an {@link Image}
+	 */
+	public Image image() {
+		final ImageRegistry imageRegistry = ExampleActivator.getDefault().getImageRegistry();
+		Image image = imageRegistry.get(path);
+		if (image == null) {
+			addImageDescriptor();
+			image = imageRegistry.get(path);
+		}
+
+		return image;
+	}
+
+	/**
+	 * Returns an image descriptor.
+	 * 
+	 * @return an {@link ImageDescriptor}
+	 */
+	public ImageDescriptor imageDescriptor() {
+		final ImageRegistry imageRegistry = ExampleActivator.getDefault().getImageRegistry();
+		ImageDescriptor imageDescriptor = imageRegistry.getDescriptor(path);
+		if (imageDescriptor == null) {
+			addImageDescriptor();
+			imageDescriptor = imageRegistry.getDescriptor(path);
+		}
+
+		return imageDescriptor;
+	}
+
+	private void addImageDescriptor() {
+		final ExampleActivator plugin = ExampleActivator.getDefault();
+		final ImageDescriptor id = ImageDescriptor.createFromURL(plugin.getBundle().getEntry(path));
+		plugin.getImageRegistry().put(path, id);
+	}
+
+}

+ 2 - 3
plugins/org.yakindu.sct.examples.wizard/src/org/yakindu/sct/examples/wizard/pages/MessageArea.java

@@ -36,7 +36,7 @@ import org.eclipse.ui.dialogs.PreferencesUtil;
 public class MessageArea extends Composite {
 
 	private static final String DOWNLOAD_LINK = "http://www.statecharts.org/examples.html";
-	protected static final String PREF_PAGE_ID = "org.yakindu.sct.ui.preferences.root";
+	protected static final String PREF_PAGE_ID = "com.yakindu.sct.examples";
 	protected static final String DISPLAY_ID = "com.yakindu.sct.examples";
 	private Label imageLabel;
 	private Link textLabel;
@@ -118,11 +118,10 @@ public class MessageArea extends Composite {
 	}
 
 	public void hide() {
-		state = state.HIDE;
+		state = State.HIDE;
 		setVisible(false);
 		GridData data = (GridData) getLayoutData();
 		data.exclude = true;
-		getParent().pack();
 	}
 
 	public void addSelectionListener(SelectionListener listener) {

+ 29 - 2
plugins/org.yakindu.sct.examples.wizard/src/org/yakindu/sct/examples/wizard/pages/SelectExamplePage.java

@@ -30,6 +30,7 @@ import org.eclipse.jface.wizard.WizardPage;
 import org.eclipse.swt.SWT;
 import org.eclipse.swt.browser.Browser;
 import org.eclipse.swt.custom.SashForm;
+import org.eclipse.swt.events.SelectionAdapter;
 import org.eclipse.swt.events.SelectionEvent;
 import org.eclipse.swt.events.SelectionListener;
 import org.eclipse.swt.layout.FillLayout;
@@ -37,7 +38,10 @@ import org.eclipse.swt.layout.GridData;
 import org.eclipse.swt.layout.GridLayout;
 import org.eclipse.swt.widgets.Composite;
 import org.eclipse.swt.widgets.Display;
+import org.eclipse.swt.widgets.ToolBar;
+import org.eclipse.swt.widgets.ToolItem;
 import org.yakindu.sct.examples.wizard.ExampleActivator;
+import org.yakindu.sct.examples.wizard.ExampleWizardImages;
 import org.yakindu.sct.examples.wizard.preferences.ExamplesPreferenceConstants;
 import org.yakindu.sct.examples.wizard.service.ExampleData;
 import org.yakindu.sct.examples.wizard.service.ExampleWizardConstants;
@@ -80,6 +84,7 @@ public class SelectExamplePage extends WizardPage
 		Composite root = new Composite(parent, SWT.NONE);
 		root.setLayout(new GridLayout(1, true));
 		createUpdateGroup(root);
+		createToolbar(root);
 		SashForm container = new SashForm(root, SWT.NONE);
 		GridDataFactory.fillDefaults().grab(true, true).applyTo(container);
 		GridLayout layout = new GridLayout(2, false);
@@ -90,12 +95,32 @@ public class SelectExamplePage extends WizardPage
 		setControl(container);
 	}
 
+	protected void createToolbar(Composite root) {
+		ToolBar tb = new ToolBar(root, SWT.NONE);
+		GridDataFactory.fillDefaults().grab(true, false).applyTo(tb);
+		ToolItem collapseAllItem = new ToolItem(tb, SWT.NONE);
+		collapseAllItem.setImage(ExampleWizardImages.COLLAPSE_ALL.image());
+		collapseAllItem.addSelectionListener(new SelectionAdapter() {
+			@Override
+			public void widgetSelected(SelectionEvent e) {
+				viewer.collapseAll();
+			}
+		});
+		ToolItem expandAllItem = new ToolItem(tb, SWT.NONE);
+		expandAllItem.setImage(ExampleWizardImages.EXPAND_ALL.image());
+		expandAllItem.addSelectionListener(new SelectionAdapter() {
+			@Override
+			public void widgetSelected(SelectionEvent e) {
+				viewer.expandAll();
+			}
+		});
+	}
+
 	private void createUpdateGroup(Composite root) {
 		messageArea = new MessageArea(root);
 		GridDataFactory.fillDefaults().grab(true, false).applyTo(messageArea);
 		messageArea.addSelectionListener(this);
 		messageArea.hide();
-
 	}
 
 	@Override
@@ -151,10 +176,12 @@ public class SelectExamplePage extends WizardPage
 
 	protected void setInput(final IProgressMonitor monitor) {
 		final List<ExampleData> input = exampleService.getExamples(new NullProgressMonitor());
-
+		
 		messageArea.hide();
 		viewer.setInput(input);
 		viewer.expandAll();
+		// explicit layouting required for Unix systems
+		viewer.getControl().getParent().getParent().layout(true);
 	}
 
 	protected void createTreeViewer(Composite container) {

+ 25 - 2
plugins/org.yakindu.sct.examples.wizard/src/org/yakindu/sct/examples/wizard/preferences/ExamplesPreferencePage.java

@@ -28,6 +28,29 @@ import org.yakindu.sct.examples.wizard.ExampleActivator;
  */
 public class ExamplesPreferencePage extends FieldEditorPreferencePage implements IWorkbenchPreferencePage {
 
+	private static class StorageLocationFieldEditor extends DirectoryFieldEditor {
+		
+		public StorageLocationFieldEditor(String name, String labelText, Composite parent) {
+			super(name, labelText, parent);
+			setEmptyStringAllowed(false);
+			setErrorMessage("Storage location must not be empty.");
+		}
+		
+		/**
+		 * Checks only if the input text is not empty, but not if the directory
+		 * exists as in that case it will be created by the example wizard.
+		 */
+		@Override
+		protected boolean doCheckState() {
+			String fileName = getTextControl().getText();
+	        fileName = fileName.trim();
+			if (fileName.length() == 0 && !isEmptyStringAllowed()) {
+				return false;
+			}
+			return true;
+		}
+	};
+	
 	public ExamplesPreferencePage() {
 		super(GRID);
 		setDescription("Examples Preference Page");
@@ -46,8 +69,8 @@ public class ExamplesPreferencePage extends FieldEditorPreferencePage implements
 
 	private void createStorageLocationEditor(Composite main) {
 		Composite composite = createGroupComposite(main, "Storage Location");
-		addField(new DirectoryFieldEditor(ExamplesPreferenceConstants.STORAGE_LOCATION, "Storage Location:",
-				composite));
+		addField(new StorageLocationFieldEditor(ExamplesPreferenceConstants.STORAGE_LOCATION,
+				"Storage Location:", composite));
 	}
 
 	protected Composite createPageLayout(Composite parent) {

+ 4 - 4
plugins/org.yakindu.sct.examples.wizard/src/org/yakindu/sct/examples/wizard/service/ExampleWizardConstants.java

@@ -26,8 +26,8 @@ public interface ExampleWizardConstants {
 	public static final String UPDATE_PAGE_DESCRIPTION = "Please wait while the program updates your local example files.";
 	public static final String UPDATE_PAGE_DESCRIPTION_FINISH = "Finished! Click \"next\" to continue";
 	public static final String SELECT_PAGE_TITLE = "Select an example";
-	public static final String SELECT_PAGE_DESCRIPTION = "Choose an Example Project to import it.";
-	public static final String WIZARD_ERROR_NOTHING_SELECTED = "You have to select an Example in order to continue.";
-	public static final String WIZARD_ERROR_NO_INTERNET_CONNECTION = "You are not connected to the Internet\nPlease check your cable and network configs.";
-	public static final String WIZARD_ERROR_GIT = "It seems there is a Problem with git\nPlease try again later.";
+	public static final String SELECT_PAGE_DESCRIPTION = "Choose an example project to import it.";
+	public static final String WIZARD_ERROR_NOTHING_SELECTED = "You have to select an example in order to continue.";
+	public static final String WIZARD_ERROR_NO_INTERNET_CONNECTION = "You are not connected to the internet.\nPlease check your cable and network configs.";
+	public static final String WIZARD_ERROR_GIT = "It seems there is a problem with git\nPlease try again later.";
 }

+ 22 - 20
plugins/org.yakindu.sct.examples.wizard/src/org/yakindu/sct/examples/wizard/service/git/EclipseGitProgressTransformer.java

@@ -2,16 +2,16 @@
 package org.yakindu.sct.examples.wizard.service.git;
 
 import org.eclipse.core.runtime.IProgressMonitor;
-import org.eclipse.core.runtime.SubProgressMonitor;
+import org.eclipse.core.runtime.SubMonitor;
 import org.eclipse.jgit.lib.ProgressMonitor;
 
 /** Create a new Git to Eclipse progress monitor. */
 public class EclipseGitProgressTransformer implements ProgressMonitor {
-	private static final String EMPTY_STRING = "";  //$NON-NLS-1$
 
 	private final IProgressMonitor root;
 
-	private IProgressMonitor task;
+	private SubMonitor mainTask;
+	private SubMonitor subTask;
 
 	private String msg;
 
@@ -31,32 +31,34 @@ public class EclipseGitProgressTransformer implements ProgressMonitor {
 
 	@Override
 	public void start(final int totalTasks) {
-		root.beginTask(EMPTY_STRING, totalTasks * 1000);
+		mainTask = SubMonitor.convert(root, 5);
 	}
 
 	@Override
 	public void beginTask(final String name, final int total) {
-		endTask();
 		msg = name;
 		lastWorked = 0;
 		totalWork = total;
-		task = new SubProgressMonitor(root, 1000);
-		if (totalWork == UNKNOWN)
-			task.beginTask(EMPTY_STRING, IProgressMonitor.UNKNOWN);
-		else
-			task.beginTask(EMPTY_STRING, totalWork);
-		task.subTask(msg);
+		
+		SubMonitor sub = mainTask.newChild(1);
+		
+		if (totalWork == UNKNOWN) {
+			subTask = SubMonitor.convert(sub, IProgressMonitor.UNKNOWN);
+		} else {
+			subTask = SubMonitor.convert(sub, totalWork);
+		}
+		subTask.subTask(msg);
 	}
 
 	@Override
 	public void update(final int work) {
-		if (task == null)
+		if (subTask == null)
 			return;
 
 		final int cmp = lastWorked + work;
 		if (totalWork == UNKNOWN && cmp > 0) {
 			if (lastWorked != cmp)
-				task.subTask(msg + ", " + cmp); //$NON-NLS-1$
+				subTask.subTask(msg + ", " + cmp); //$NON-NLS-1$
 		} else if (totalWork <= 0) {
 			// Do nothing to update the task.
 		} else if (cmp * 100 / totalWork != lastWorked * 100 / totalWork) {
@@ -82,27 +84,27 @@ public class EclipseGitProgressTransformer implements ProgressMonitor {
 			m.append(twstr);
 			m.append(")"); //$NON-NLS-1$
 
-			task.subTask(m.toString());
+			subTask.subTask(m.toString());
 		}
 		lastWorked = cmp;
-		task.worked(work);
+		subTask.worked(work);
 	}
 
 	@Override
 	public void endTask() {
-		if (task != null) {
+		if (subTask != null) {
 			try {
-				task.done();
+				subTask.done();
 			} finally {
-				task = null;
+				subTask = null;
 			}
 		}
 	}
 
 	@Override
 	public boolean isCancelled() {
-		if (task != null)
-			return task.isCanceled();
+		if (subTask != null)
+			return subTask.isCanceled();
 		return root.isCanceled();
 	}
 }

+ 8 - 1
plugins/org.yakindu.sct.examples.wizard/src/org/yakindu/sct/examples/wizard/service/git/GitRepositoryExampleService.java

@@ -58,6 +58,8 @@ import com.google.inject.name.Named;
 @Singleton
 public class GitRepositoryExampleService implements IExampleService {
 
+	private static final String GIT_METADATA_FOLDER = ".git";
+
 	private static final String METADATA_JSON = "metadata.json";
 
 	private static final String RELEASE = "release";
@@ -72,10 +74,15 @@ public class GitRepositoryExampleService implements IExampleService {
 		return java.nio.file.Paths.get(ExampleActivator.getDefault().getPreferenceStore()
 				.getString(ExamplesPreferenceConstants.STORAGE_LOCATION));
 	}
+	
+	protected java.nio.file.Path getGitMetadataLocation() {
+		return java.nio.file.Paths.get(ExampleActivator.getDefault().getPreferenceStore()
+				.getString(ExamplesPreferenceConstants.STORAGE_LOCATION), GIT_METADATA_FOLDER);
+	}
 
 	@Override
 	public boolean exists() {
-		return Files.exists(getStorageLocation());
+		return Files.exists(getGitMetadataLocation());
 	}
 
 	@Override