diff options
Diffstat (limited to 'src/plugins/android.codeutils/src/com/motorola/studio/android/wizards')
22 files changed, 5068 insertions, 0 deletions
diff --git a/src/plugins/android.codeutils/src/com/motorola/studio/android/wizards/buildingblocks/ActivitySampleSelectionPage.java b/src/plugins/android.codeutils/src/com/motorola/studio/android/wizards/buildingblocks/ActivitySampleSelectionPage.java new file mode 100644 index 0000000..fd53350 --- /dev/null +++ b/src/plugins/android.codeutils/src/com/motorola/studio/android/wizards/buildingblocks/ActivitySampleSelectionPage.java @@ -0,0 +1,364 @@ +/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.motorola.studio.android.wizards.buildingblocks;
+
+import org.eclipse.jface.dialogs.IPageChangeProvider;
+import org.eclipse.jface.dialogs.IPageChangedListener;
+import org.eclipse.jface.dialogs.PageChangedEvent;
+import org.eclipse.jface.resource.ImageDescriptor;
+import org.eclipse.jface.viewers.ArrayContentProvider;
+import org.eclipse.jface.viewers.ISelectionChangedListener;
+import org.eclipse.jface.viewers.ITreeContentProvider;
+import org.eclipse.jface.viewers.LabelProvider;
+import org.eclipse.jface.viewers.SelectionChangedEvent;
+import org.eclipse.jface.viewers.TreeViewer;
+import org.eclipse.jface.viewers.ViewerComparator;
+import org.eclipse.jface.wizard.IWizardPage;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.custom.ScrolledComposite;
+import org.eclipse.swt.graphics.Image;
+import org.eclipse.swt.layout.GridData;
+import org.eclipse.swt.layout.GridLayout;
+import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Group;
+import org.eclipse.swt.widgets.Label;
+
+import com.motorola.studio.android.codeutils.CodeUtilsActivator;
+import com.motorola.studio.android.codeutils.i18n.CodeUtilsNLS;
+import com.motorola.studio.android.common.exception.AndroidException;
+import com.motorola.studio.android.common.utilities.AndroidUtils;
+import com.motorola.studio.android.model.ActivityBasedOnTemplate;
+
+/**
+ * New activity wizard sample page.
+ */
+public class ActivitySampleSelectionPage extends NewLauncherWizardPage
+{
+ private static final String ANDROID_LOGO_ICON_PATH = "icons/obj16/androidLogo.png";
+
+ private TreeViewer treeViewer;
+
+ private Label descriptionLabel;
+
+ private String content[];
+
+ private boolean canFlip = false;
+
+ private static final String NEW_ACTIVITY_BASED_ON_TEMPLATE_HELP = CodeUtilsActivator.PLUGIN_ID
+ + ".new-activity-based-on-template"; //$NON-NLS-1$
+
+ private static Image androidImg = null;
+
+ public static final String PAGE_NAME = "Samples Page";
+
+ /*
+ * Listener to update description pane whenever this page is open
+ */
+ private class PageChangeListener implements IPageChangedListener
+ {
+ public void pageChanged(PageChangedEvent event)
+ {
+ if ((event.getSelectedPage() == ActivitySampleSelectionPage.this))
+ {
+ ActivitySampleSelectionPage.this.getControl().update();
+ ((ActivitySampleSelectionPage) event.getSelectedPage()).updateDescriptionPane();
+ }
+ }
+ }
+
+ /**
+ * Create a new wizard page based on activity samples.
+ * @param activity The building block model to be used in the wizard page.
+ * */
+ protected ActivitySampleSelectionPage(ActivityBasedOnTemplate activity)
+ {
+ super(activity, PAGE_NAME);
+
+ activity.evaluateSamplesList(ActivityBasedOnTemplate.SAMPLE_CATEGORY.SAMPLE_ACTIVITIES_CATEGORY);
+
+ ImageDescriptor imgDescr =
+ CodeUtilsActivator.imageDescriptorFromPlugin(CodeUtilsActivator.PLUGIN_ID,
+ ANDROID_LOGO_ICON_PATH);
+ if (imgDescr != null)
+ {
+ androidImg = imgDescr.createImage();
+ }
+ }
+
+ /*
+ * (non-Javadoc)
+ * */
+ @Override
+ public boolean canFlipToNextPage()
+ {
+
+ return canFlip;
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see com.motorola.studio.android.wizards.buildingblocks.NewBuildingBlocksWizard#getBuildBlock()
+ */
+ @Override
+ public ActivityBasedOnTemplate getBuildBlock()
+ {
+ return (ActivityBasedOnTemplate) super.getBuildBlock();
+ }
+
+ @Override
+ public IWizardPage getNextPage()
+ {
+ String selection =
+ treeViewer.getSelection() != null ? treeViewer.getSelection().toString() : null;
+ selection = selection != null ? selection.substring(1, selection.length() - 1) : null;
+
+ if ((selection != null)
+ && selection
+ .equalsIgnoreCase(ActivityBasedOnTemplate.LIST_ACTIVITIES_SAMPLE_LOCALIZED))
+ {
+ return this.getWizard().getPage(NewActivityWizardListTemplatesPage.PAGE_NAME);
+ }
+ return this.getWizard().getPage(CodeUtilsNLS.UI_NewActivityMainPage_PageTitle);
+ }
+
+ /* (non-Javadoc)
+ * @see com.motorola.studio.android.wizards.buildingblocks.NewBuildingBlocksWizardPage#getMethods()
+ */
+ @Override
+ protected Method[] getMethods()
+ {
+ Method onCreateMethod = new Method(getBuildBlock().getOnStartMessage())
+ {
+ @Override
+ public void handle(boolean selection)
+ {
+ getBuildBlock().setOnStart(selection);
+ }
+ };
+ return new Method[]
+ {
+ onCreateMethod
+ };
+ }
+
+ /* (non-Javadoc)
+ * @see com.motorola.studio.android.wizards.buildingblocks.NewBuildingBlocksWizardPage#createIntermediateControls(org.eclipse.swt.widgets.Composite)
+ */
+ @Override
+ protected void createExtendedControls(Composite parent)
+ {
+ Composite mainComposite = new Composite(parent, SWT.FILL);
+ mainComposite.setLayout(new GridLayout(1, false));
+ mainComposite.setLayoutData(new GridData(GridData.FILL_BOTH));
+
+ // Samples Tree Label
+ Label itemsTableLabel = new Label(mainComposite, SWT.NONE);
+ itemsTableLabel.setText(CodeUtilsNLS.UI_SampleSelectionPage_SamplesTreeLabel);
+
+ // Samples Tree Viewer
+ treeViewer = new TreeViewer(mainComposite, SWT.BORDER | SWT.SINGLE | SWT.V_SCROLL);
+ treeViewer.getControl().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
+ treeViewer.setLabelProvider(new LabelProvider()
+ {
+ @Override
+ public Image getImage(Object obj)
+ {
+ return androidImg;
+ }
+
+ @Override
+ public String getText(Object element)
+ {
+ return element.toString();
+ }
+ });
+
+ content = new String[getBuildBlock().getAvailableSamples().size()];
+
+ int i = 0;
+ for (String currentSample : getBuildBlock().getAvailableSamples().keySet())
+ {
+ content[i] = currentSample;
+ i++;
+ }
+
+ treeViewer.setContentProvider(new SampleTreeContentProvider(content));
+ treeViewer.setInput(content);
+
+ final Group intentFilterGroup = new Group(mainComposite, SWT.NONE);
+
+ treeViewer.addSelectionChangedListener(new ISelectionChangedListener()
+ {
+ public void selectionChanged(SelectionChangedEvent e)
+ {
+ String selection = e.getSelection().toString();
+ getBuildBlock().setSample(selection.substring(1, selection.length() - 1));
+
+ getBuildBlock().setSampleCategoty(
+ ActivityBasedOnTemplate.SAMPLE_CATEGORY.SAMPLE_ACTIVITIES_CATEGORY);
+
+ if (selection.substring(1, selection.length() - 1).equals(
+ ActivityBasedOnTemplate.DATABASE_LIST_SAMPLE_LOCALIZED))
+ {
+ getBuildBlock().setDatabaseTemplateSelected(true);
+ }
+ else
+ {
+ getBuildBlock().setDatabaseTemplateSelected(false);
+ }
+
+ canFlip = true;
+
+ updateDescriptionPane();
+ getWizard().getContainer().updateButtons();
+ }
+ });
+
+ treeViewer.setComparator(new ViewerComparator());
+
+ treeViewer.expandAll();
+
+ intentFilterGroup.setText(CodeUtilsNLS.UI_SampleSelectionPage_SamplesDescriptionPane);
+ intentFilterGroup.setLayout(new GridLayout(1, false));
+ intentFilterGroup
+ .setLayoutData(new GridData(GridData.FILL, GridData.BEGINNING, true, false));
+
+ ScrolledComposite scrolledArea = new ScrolledComposite(intentFilterGroup, SWT.V_SCROLL);
+ GridData descriptionLabelData = new GridData(GridData.FILL, GridData.FILL, true, true);
+ descriptionLabelData.heightHint = 140;
+ scrolledArea.setLayoutData(descriptionLabelData);
+
+ descriptionLabel = new Label(scrolledArea, SWT.FILL | SWT.WRAP);
+ descriptionLabel.setText("");
+ scrolledArea.setContent(descriptionLabel);
+ descriptionLabel.setSize(descriptionLabel.computeSize(SWT.DEFAULT, SWT.DEFAULT));
+
+ // Add a listener to the wizard to listen for page changes
+ if (getContainer() instanceof IPageChangeProvider)
+ {
+ ((IPageChangeProvider) getContainer()).addPageChangedListener(new PageChangeListener());
+ }
+ setControl(mainComposite);
+ }
+
+ /*
+ * Updates selected label description.
+ */
+ private void updateDescriptionPane()
+ {
+ descriptionLabel.setText(getBuildBlock().getSampleDescription());
+ // descriptionLabel.setVisible(true);
+ descriptionLabel.setSize(descriptionLabel.computeSize(SWT.DEFAULT, SWT.DEFAULT));
+ descriptionLabel.update();
+ descriptionLabel.getParent().update();
+ }
+
+ /* (non-Javadoc)
+ * @see com.motorola.studio.android.wizards.buildingblocks.NewBuildingBlocksWizardPage#getDefaultMessage()
+ */
+ @Override
+ public String getDefaultMessage()
+ {
+ return CodeUtilsNLS.UI_NewActivityMainPage_DescriptionCreateActivityBasedOnTemplate;
+ }
+
+ /* (non-Javadoc)
+ * @see com.motorola.studio.android.wizards.buildingblocks.NewBuildingBlocksWizardPage#getWizardTitle()
+ */
+ @Override
+ public String getWizardTitle()
+ {
+ return CodeUtilsNLS.UI_NewActivityMainPage_TitleActivityBasedOnTemplate;
+ }
+
+ /* (non-Javadoc)
+ * @see com.motorola.studio.android.wizards.buildingblocks.NewLauncherWizardPage#getIntentFiltersActions()
+ */
+ @Override
+ protected String[] getIntentFiltersActions()
+ {
+ String[] intentFiltersActions = new String[0];
+ try
+ {
+ intentFiltersActions = AndroidUtils.getActivityActions(getBuildBlock().getProject());
+ }
+ catch (AndroidException e)
+ {
+ setErrorMessage(e.getMessage());
+ }
+ return intentFiltersActions;
+ }
+
+ /**
+ * Gets the help ID to be used for attaching
+ * context sensitive help.
+ *
+ * Classes that extends this class and want to set
+ * their on help should override this method
+ */
+ @Override
+ protected String getHelpId()
+ {
+ return NEW_ACTIVITY_BASED_ON_TEMPLATE_HELP;
+ }
+
+ /**
+ * Returns true if page has header false otherwise
+ *
+ * @return true if page has header false otherwise
+ */
+ @Override
+ public boolean hasHeader()
+ {
+ return false;
+ }
+}
+
+/**
+ * Fills tree viewer with sample options
+ */
+class SampleTreeContentProvider extends ArrayContentProvider implements ITreeContentProvider
+{
+
+ Object[] elements;
+
+ public SampleTreeContentProvider(Object[] elements)
+ {
+ this.elements = elements;
+ }
+
+ @Override
+ public Object[] getElements(Object inputElement)
+ {
+ return elements;
+ }
+
+ public Object[] getChildren(Object parentElement)
+ {
+ return new Object[0];
+ }
+
+ public Object getParent(Object element)
+ {
+ return new Object[0];
+ }
+
+ public boolean hasChildren(Object element)
+ {
+ return false;
+ }
+}
\ No newline at end of file diff --git a/src/plugins/android.codeutils/src/com/motorola/studio/android/wizards/buildingblocks/ElementTreeContentProvider.java b/src/plugins/android.codeutils/src/com/motorola/studio/android/wizards/buildingblocks/ElementTreeContentProvider.java new file mode 100644 index 0000000..b3d00a4 --- /dev/null +++ b/src/plugins/android.codeutils/src/com/motorola/studio/android/wizards/buildingblocks/ElementTreeContentProvider.java @@ -0,0 +1,63 @@ +/* + * Copyright (C) 2012 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.motorola.studio.android.wizards.buildingblocks; + +import java.util.ArrayList; +import java.util.List; + +import org.eclipse.core.runtime.CoreException; +import org.eclipse.jdt.core.IJavaModel; +import org.eclipse.jdt.core.IJavaProject; +import org.eclipse.jdt.core.JavaModelException; +import org.eclipse.jdt.ui.StandardJavaElementContentProvider; + +import com.motorola.studio.android.common.IAndroidConstants; +import com.motorola.studio.android.common.log.StudioLogger; + +/** + * Class that implements a Content Provider for the project selection + * on the New Project Wizard + */ +class ElementTreeContentProvider extends StandardJavaElementContentProvider +{ + /* (non-Javadoc) + * @see org.eclipse.jdt.ui.StandardJavaElementContentProvider#getJavaProjects(org.eclipse.jdt.core.IJavaModel) + */ + @Override + protected Object[] getJavaProjects(IJavaModel jm) throws JavaModelException + { + Object[] javaProjects = super.getJavaProjects(jm); + List<Object> androidProjects = new ArrayList<Object>(); + for (Object obj : javaProjects) + { + try + { + if ((obj instanceof IJavaProject) + && ((IJavaProject) obj).getProject().hasNature( + IAndroidConstants.ANDROID_NATURE)) + { + androidProjects.add(obj); + } + } + catch (CoreException ce) + { + StudioLogger.error(ElementTreeContentProvider.class, ce.getLocalizedMessage(), ce); + } + + } + return androidProjects.toArray(); + } +} diff --git a/src/plugins/android.codeutils/src/com/motorola/studio/android/wizards/buildingblocks/ElementTreeValidator.java b/src/plugins/android.codeutils/src/com/motorola/studio/android/wizards/buildingblocks/ElementTreeValidator.java new file mode 100644 index 0000000..29b6c1a --- /dev/null +++ b/src/plugins/android.codeutils/src/com/motorola/studio/android/wizards/buildingblocks/ElementTreeValidator.java @@ -0,0 +1,82 @@ +/* + * Copyright (C) 2012 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.motorola.studio.android.wizards.buildingblocks; + +import org.eclipse.core.runtime.IPath; +import org.eclipse.jdt.core.IJavaProject; +import org.eclipse.jdt.core.IPackageFragmentRoot; +import org.eclipse.jdt.core.JavaModelException; +import org.eclipse.jdt.internal.ui.wizards.TypedElementSelectionValidator; + +import com.motorola.studio.android.common.IAndroidConstants; +import com.motorola.studio.android.common.log.StudioLogger; + +/** + * Class that implements a Validator for the project/package selection + * on the New Project Wizard + */ +@SuppressWarnings("restriction") +class ElementTreeValidator extends TypedElementSelectionValidator +{ + private static Class<?>[] acceptedClasses = new Class[] + { + IPackageFragmentRoot.class, IJavaProject.class + }; + + public ElementTreeValidator() + { + super(acceptedClasses, false); + } + + /* (non-Javadoc) + * @see org.eclipse.jdt.internal.ui.wizards.TypedElementSelectionValidator#isSelectedValid(java.lang.Object) + */ + @Override + public boolean isSelectedValid(Object element) + { + boolean isValid = false; + try + { + if (element instanceof IJavaProject) + { + IJavaProject jproject = (IJavaProject) element; + IPath path = jproject.getProject().getFullPath(); + isValid = (jproject.findPackageFragmentRoot(path) != null); + } + else if (element instanceof IPackageFragmentRoot) + { + IPackageFragmentRoot packageFragmentRoot = (IPackageFragmentRoot) element; + + boolean isSrc = (packageFragmentRoot.getKind() == IPackageFragmentRoot.K_SOURCE); + boolean isGen = + packageFragmentRoot.getElementName().equals( + IAndroidConstants.GEN_SRC_FOLDER) + && (packageFragmentRoot.getParent() instanceof IJavaProject); + + isValid = isSrc && !isGen; + } + else + { + isValid = true; + } + } + catch (JavaModelException e) + { + StudioLogger.error(ElementTreeValidator.class, e.getLocalizedMessage(), e); + } + return isValid; + } +} diff --git a/src/plugins/android.codeutils/src/com/motorola/studio/android/wizards/buildingblocks/ElementTreeViewFilter.java b/src/plugins/android.codeutils/src/com/motorola/studio/android/wizards/buildingblocks/ElementTreeViewFilter.java new file mode 100644 index 0000000..cc9f9e6 --- /dev/null +++ b/src/plugins/android.codeutils/src/com/motorola/studio/android/wizards/buildingblocks/ElementTreeViewFilter.java @@ -0,0 +1,72 @@ +/* + * Copyright (C) 2012 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.motorola.studio.android.wizards.buildingblocks; + +import org.eclipse.jdt.core.IJavaModel; +import org.eclipse.jdt.core.IJavaProject; +import org.eclipse.jdt.core.IPackageFragmentRoot; +import org.eclipse.jdt.core.JavaModelException; +import org.eclipse.jdt.internal.ui.wizards.TypedViewerFilter; +import org.eclipse.jface.viewers.Viewer; + +import com.motorola.studio.android.common.log.StudioLogger; + +/** + * Class that implements a View Filter for the package selection + * on the New Project Wizard + */ +@SuppressWarnings("restriction") +class ElementTreeViewFilter extends TypedViewerFilter +{ + private static Class<?>[] acceptedClasses = new Class[] + { + IJavaModel.class, IPackageFragmentRoot.class, IJavaProject.class + }; + + /** + * Default constructor + */ + public ElementTreeViewFilter() + { + super(acceptedClasses); + } + + /* (non-Javadoc) + * @see org.eclipse.jdt.internal.ui.wizards.TypedViewerFilter#select(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object) + */ + @Override + public boolean select(Viewer viewer, Object parent, Object element) + { + boolean select = false; + if (element instanceof IPackageFragmentRoot) + { + try + { + select = + (((IPackageFragmentRoot) element).getKind() == IPackageFragmentRoot.K_SOURCE); + } + catch (JavaModelException e) + { + StudioLogger.error(ElementTreeViewFilter.class, e.getLocalizedMessage(), e); + } + } + else + { + select = super.select(viewer, parent, element); + } + return select; + } +} diff --git a/src/plugins/android.codeutils/src/com/motorola/studio/android/wizards/buildingblocks/FilteredActionsSelectionDialog.java b/src/plugins/android.codeutils/src/com/motorola/studio/android/wizards/buildingblocks/FilteredActionsSelectionDialog.java new file mode 100644 index 0000000..5399ab3 --- /dev/null +++ b/src/plugins/android.codeutils/src/com/motorola/studio/android/wizards/buildingblocks/FilteredActionsSelectionDialog.java @@ -0,0 +1,153 @@ +/* + * Copyright (C) 2012 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.motorola.studio.android.wizards.buildingblocks; + +import java.util.Comparator; +import java.util.Set; + +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Status; +import org.eclipse.jface.dialogs.IDialogSettings; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Control; +import org.eclipse.swt.widgets.Shell; +import org.eclipse.ui.dialogs.FilteredItemsSelectionDialog; + +import com.motorola.studio.android.codeutils.CodeUtilsActivator; + +/** + * Class that implements a filter dialog to be used by NewLauncherWizardPage class + */ +class FilteredActionsSelectionDialog extends FilteredItemsSelectionDialog +{ + private final Set<String> categorySet; + + public FilteredActionsSelectionDialog(Shell shell, Set<String> categorySet) + { + super(shell, true); + this.categorySet = categorySet; + } + + /* (non-Javadoc) + * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#createExtendedContentArea(org.eclipse.swt.widgets.Composite) + */ + @Override + protected Control createExtendedContentArea(Composite parent) + { + return null; + } + + /* (non-Javadoc) + * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#createFilter() + */ + @Override + protected ItemsFilter createFilter() + { + return new ItemsFilter() + { + /* (non-Javadoc) + * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog.ItemsFilter#isConsistentItem(java.lang.Object) + */ + @Override + public boolean isConsistentItem(Object item) + { + return true; + } + + /* (non-Javadoc) + * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog.ItemsFilter#matchItem(java.lang.Object) + */ + @Override + public boolean matchItem(Object item) + { + if (!(item instanceof String)) + { + return false; + } + return matches((String) item); + } + + }; + } + + /* (non-Javadoc) + * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#fillContentProvider(org.eclipse.ui.dialogs.FilteredItemsSelectionDialog.AbstractContentProvider, org.eclipse.ui.dialogs.FilteredItemsSelectionDialog.ItemsFilter, org.eclipse.core.runtime.IProgressMonitor) + */ + @Override + protected void fillContentProvider(AbstractContentProvider contentProvider, + ItemsFilter itemsFilter, IProgressMonitor progressMonitor) throws CoreException + { + for (String action : categorySet) + { + contentProvider.add(action, itemsFilter); + } + + } + + /* (non-Javadoc) + * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#getDialogSettings() + */ + @Override + protected IDialogSettings getDialogSettings() + { + return CodeUtilsActivator.getDefault().getDialogSettings(); + } + + /* (non-Javadoc) + * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#getElementName(java.lang.Object) + */ + @Override + public String getElementName(Object item) + { + return (String) item; + } + + /* (non-Javadoc) + * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#getItemsComparator() + */ + @Override + protected Comparator<String> getItemsComparator() + { + return new Comparator<String>() + { + + public int compare(String o1, String o2) + { + return o1.compareToIgnoreCase(o2); + } + }; + } + + /* (non-Javadoc) + * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#validateItem(java.lang.Object) + */ + @Override + protected IStatus validateItem(Object item) + { + IStatus status; + if (categorySet.contains(item)) + { + status = new Status(IStatus.OK, CodeUtilsActivator.PLUGIN_ID, ""); + } + else + { + status = new Status(IStatus.ERROR, CodeUtilsActivator.PLUGIN_ID, "Select an item."); + } + return status; + } +} diff --git a/src/plugins/android.codeutils/src/com/motorola/studio/android/wizards/buildingblocks/Method.java b/src/plugins/android.codeutils/src/com/motorola/studio/android/wizards/buildingblocks/Method.java new file mode 100644 index 0000000..4779e2d --- /dev/null +++ b/src/plugins/android.codeutils/src/com/motorola/studio/android/wizards/buildingblocks/Method.java @@ -0,0 +1,53 @@ +/* + * Copyright (C) 2012 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.motorola.studio.android.wizards.buildingblocks; + +/** + * Abstract bean to define method descriptions to be used on the + * building block wizards + */ +public abstract class Method +{ + private final String message; + + /** + * Default constructor + * + * @param message The method description + */ + public Method(String message) + { + this.message = message; + } + + /** + * Retrieves the method description + * + * @return the method description + */ + public String getMessage() + { + return message; + } + + /** + * Handles the method selection, i.e., when the user + * selects the method on the wizard + * + * @param selection if the method has been selected or not + */ + public abstract void handle(boolean selection); +} diff --git a/src/plugins/android.codeutils/src/com/motorola/studio/android/wizards/buildingblocks/NewActivityBasedOnTemplatePage.java b/src/plugins/android.codeutils/src/com/motorola/studio/android/wizards/buildingblocks/NewActivityBasedOnTemplatePage.java new file mode 100644 index 0000000..6efe847 --- /dev/null +++ b/src/plugins/android.codeutils/src/com/motorola/studio/android/wizards/buildingblocks/NewActivityBasedOnTemplatePage.java @@ -0,0 +1,258 @@ +/* + * Copyright (C) 2012 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.motorola.studio.android.wizards.buildingblocks; + +import org.eclipse.jdt.core.IPackageFragmentRoot; +import org.eclipse.jdt.ui.wizards.NewTypeWizardPage; +import org.eclipse.jface.dialogs.IPageChangeProvider; +import org.eclipse.jface.dialogs.IPageChangedListener; +import org.eclipse.jface.dialogs.PageChangedEvent; +import org.eclipse.jface.wizard.IWizardPage; +import org.eclipse.swt.SWT; +import org.eclipse.swt.events.SelectionAdapter; +import org.eclipse.swt.events.SelectionEvent; +import org.eclipse.swt.layout.GridData; +import org.eclipse.swt.widgets.Button; +import org.eclipse.swt.widgets.Composite; + +import com.motorola.studio.android.codeutils.CodeUtilsActivator; +import com.motorola.studio.android.codeutils.i18n.CodeUtilsNLS; +import com.motorola.studio.android.common.exception.AndroidException; +import com.motorola.studio.android.common.utilities.AndroidUtils; +import com.motorola.studio.android.model.ActivityBasedOnTemplate; + +/** + * Class that implements the Activity Wizard Main Page + */ +public class NewActivityBasedOnTemplatePage extends NewLauncherWizardPage +{ + + private IPackageFragmentRoot currentPackageFragmentRoot; + + private IPackageFragmentRoot previousPackageFragmentRoot; + + private boolean firstLoad = true; + + /** + * Listener to verify when this page is visible + */ + private class PageChangeListener implements IPageChangedListener + { + /* + * (non-Javadoc) + * + * @see + * org.eclipse.jface.dialogs.IPageChangedListener#pageChanged(org.eclipse + * .jface.dialogs.PageChangedEvent) + */ + public void pageChanged(PageChangedEvent event) + { + if ((event.getSelectedPage() == NewActivityBasedOnTemplatePage.this)) + { + if (!firstLoad) + { + handleFieldChanged(NewTypeWizardPage.TYPENAME); + handleFieldChanged(NewTypeWizardPage.PACKAGE); + } + firstLoad = false; + } + } + } + + @Override + public boolean canFlipToNextPage() + { + if (getBuildBlock().isDatabaseTemplateSelected()) + { + return !getBuildBlock().isBasicInformationFilledIn(); + } + else + { + return false; + } + + } + + private static final String NEW_ACTIVITY_BASED_ON_TEMPLATE_HELP = CodeUtilsActivator.PLUGIN_ID + + ".new-activity-based-on-template"; //$NON-NLS-1$ + + /** + * Default constructor + * + * @param activity The activity model + */ + public NewActivityBasedOnTemplatePage(ActivityBasedOnTemplate activity) + { + super(activity, CodeUtilsNLS.UI_NewActivityMainPage_PageTitle); + } + + /* + * (non-Javadoc) + * @see com.motorola.studio.android.wizards.buildingblocks.NewBuildingBlocksWizard#getBuildBlock() + */ + @Override + public ActivityBasedOnTemplate getBuildBlock() + { + return (ActivityBasedOnTemplate) super.getBuildBlock(); + } + + /* (non-Javadoc) + * @see com.motorola.studio.android.wizards.buildingblocks.NewBuildingBlocksWizardPage#getMethods() + */ + @Override + protected Method[] getMethods() + { + return null; + } + + /* (non-Javadoc) + * @see com.motorola.studio.android.wizards.buildingblocks.NewBuildingBlocksWizardPage#createIntermediateControls(org.eclipse.swt.widgets.Composite) + */ + @Override + protected void createIntermediateControls(Composite parent) + { + createIntentFilterControls(parent); + createMainActivityControl(parent, 4); + } + + private void createMainActivityControl(Composite parent, int nColumns) + { + // Create a checkbox to allow the user to set the created activity as the MAIN activity + Button checkButtonMainActivity = new Button(parent, SWT.CHECK | SWT.LEFT); + GridData gridData = new GridData(SWT.FILL, SWT.CENTER, true, false, nColumns, 1); + checkButtonMainActivity.setLayoutData(gridData); + checkButtonMainActivity.setText(CodeUtilsNLS.UI_NewActivityMainPage_CheckMainButton); + checkButtonMainActivity.addSelectionListener(new SelectionAdapter() + { + + @Override + public void widgetSelected(SelectionEvent e) + { + if (e.widget instanceof Button) + { + // Set Activity flag accordingly + getBuildBlock().setMainActivity(((Button) e.widget).getSelection()); + + } + } + + }); + + // Add a listener to the wizard to listen for page changes + if (getContainer() instanceof IPageChangeProvider) + { + ((IPageChangeProvider) getContainer()).addPageChangedListener(new PageChangeListener()); + } + + currentPackageFragmentRoot = getBuildBlock().getPackageFragmentRoot(); + previousPackageFragmentRoot = getBuildBlock().getPackageFragmentRoot(); + } + + /* (non-Javadoc) + * @see com.motorola.studio.android.wizards.buildingblocks.NewBuildingBlocksWizardPage#getDefaultMessage() + */ + @Override + public String getDefaultMessage() + { + return CodeUtilsNLS.UI_NewActivityMainPage_DescriptionCreateActivityBasedOnTemplate; + } + + /* (non-Javadoc) + * @see com.motorola.studio.android.wizards.buildingblocks.NewBuildingBlocksWizardPage#getWizardTitle() + */ + @Override + public String getWizardTitle() + { + return CodeUtilsNLS.UI_NewActivityMainPage_TitleActivityBasedOnTemplate; + } + + /* (non-Javadoc) + * @see com.motorola.studio.android.wizards.buildingblocks.NewLauncherWizardPage#getIntentFiltersActions() + */ + @Override + protected String[] getIntentFiltersActions() + { + String[] intentFiltersActions = new String[0]; + try + { + intentFiltersActions = AndroidUtils.getActivityActions(getBuildBlock().getProject()); + } + catch (AndroidException e) + { + setErrorMessage(e.getMessage()); + } + return intentFiltersActions; + } + + /** + * Gets the help ID to be used for attaching + * context sensitive help. + * + * Classes that extends this class and want to set + * their on help should override this method + */ + @Override + protected String getHelpId() + { + return NEW_ACTIVITY_BASED_ON_TEMPLATE_HELP; + } + + /* + * (non-Javadoc) + * + * @see + * org.eclipse.jdt.ui.wizards.NewTypeWizardPage#handleFieldChanged(java. + * lang.String) + */ + @Override + protected void handleFieldChanged(String fieldName) + { + super.handleFieldChanged(fieldName); + + if (NewTypeWizardPage.CONTAINER.equals(fieldName)) + { + currentPackageFragmentRoot = getBuildBlock().getPackageFragmentRoot(); + + if ((currentPackageFragmentRoot != null) + && (!currentPackageFragmentRoot.equals(previousPackageFragmentRoot))) + { + previousPackageFragmentRoot = currentPackageFragmentRoot; + // Set the collector info to null + getBuildBlock().setCollectorTable(null); + getBuildBlock().setCollectorDatabaseName(null); + getBuildBlock().setDatabaseTableSelected(false); + getBuildBlock().setSqlOpenHelperDefined(false); + getBuildBlock().setUseSampleDatabaseTableSelected(false); + + IWizardPage page = + getWizard().getPage(CodeUtilsNLS.UI_DefineSqlOpenHelperPage_Title); + + if (!firstLoad) + { + if (page instanceof NewTypeWizardPage) + { + ((NewTypeWizardPage) page).setPackageFragment(getPackageFragment(), true); + } + } + } + } + if (getWizard().getContainer().getCurrentPage() != null) + { + getWizard().getContainer().updateButtons(); + } + + } +}
\ No newline at end of file diff --git a/src/plugins/android.codeutils/src/com/motorola/studio/android/wizards/buildingblocks/NewActivityBasedOnTemplateWizard.java b/src/plugins/android.codeutils/src/com/motorola/studio/android/wizards/buildingblocks/NewActivityBasedOnTemplateWizard.java new file mode 100644 index 0000000..feb75a7 --- /dev/null +++ b/src/plugins/android.codeutils/src/com/motorola/studio/android/wizards/buildingblocks/NewActivityBasedOnTemplateWizard.java @@ -0,0 +1,219 @@ +/* + * Copyright (C) 2012 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.motorola.studio.android.wizards.buildingblocks; + +import java.lang.reflect.InvocationTargetException; +import java.util.List; + +import org.eclipse.core.resources.IResource; +import org.eclipse.core.resources.IncrementalProjectBuilder; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Status; +import org.eclipse.jdt.core.ICompilationUnit; +import org.eclipse.jdt.ui.JavaUI; +import org.eclipse.jface.operation.IRunnableWithProgress; +import org.eclipse.jface.viewers.IStructuredSelection; +import org.eclipse.jface.wizard.IWizardPage; +import org.eclipse.ui.IWorkbench; + +import com.motorola.studio.android.codeutils.CodeUtilsActivator; +import com.motorola.studio.android.codeutils.i18n.CodeUtilsNLS; +import com.motorola.studio.android.common.exception.AndroidException; +import com.motorola.studio.android.common.log.StudioLogger; +import com.motorola.studio.android.common.utilities.EclipseUtils; +import com.motorola.studio.android.model.ActivityBasedOnTemplate; +import com.motorola.studio.android.model.BuildingBlockModel; +import com.motorola.studio.android.model.IDatabaseSampleActivityParametersWizardCollector; + +/** + * Class that implements the Activity Wizard + */ +public class NewActivityBasedOnTemplateWizard extends NewBuildingBlocksWizard +{ + private static final String WIZBAN_ICON = "icons/wizban/new_activity_template_wiz.png"; //$NON-NLS-1$ + + private final ActivityBasedOnTemplate activity = new ActivityBasedOnTemplate(); + + /* + * IRunnableWithProgress object to create the activity + */ + private class DoSave implements IRunnableWithProgress + { + AndroidException exception = null; + + boolean saved = false; + + public void run(IProgressMonitor monitor) throws InvocationTargetException, + InterruptedException + { + try + { + saved = getBuildingBlock().save(getContainer(), monitor); + getBuildingBlock().getProject().refreshLocal(IResource.DEPTH_INFINITE, monitor); + getBuildingBlock().getProject().build(IncrementalProjectBuilder.INCREMENTAL_BUILD, + monitor); + } + catch (CoreException ce) + { + //build failed - show a warning message + StudioLogger.error(this.getClass(), ce.getMessage(), ce); + EclipseUtils + .showWarningDialog( + CodeUtilsNLS.UI_NewActivityWizard_TitleNewActivityWizard, + CodeUtilsNLS.NewActivityWizard_MessageSomeProblemsOccurredWhileBuildingProject); + } + catch (AndroidException e) + { + exception = e; + } + } + } + + /* (non-Javadoc) + * @see org.eclipse.jface.wizard.Wizard#canFinish() + */ + @Override + public boolean canFinish() + { + return (!activity.needMoreInformation()) + && !(getContainer().getCurrentPage() instanceof ActivitySampleSelectionPage); + } + + /* (non-Javadoc) + * @see org.eclipse.jface.wizard.Wizard#performFinish() + */ + @Override + public boolean performFinish() + { + boolean saved = false; + + try + { + DoSave doSave = new DoSave(); + + getContainer().run(false, false, doSave); + + if (doSave.exception != null) + { + throw doSave.exception; + } + else + { + saved = doSave.saved; + } + } + catch (Exception e) + { + IStatus status = + new Status(IStatus.ERROR, CodeUtilsActivator.PLUGIN_ID, e.getLocalizedMessage()); + EclipseUtils.showErrorDialog(CodeUtilsNLS.UI_GenericErrorDialogTitle, + CodeUtilsNLS.ERR_BuildingBlockCreation_ErrorMessage, status); + } + + if (saved) + { + ICompilationUnit javaFile = + getBuildingBlock().getPackageFragment().getCompilationUnit( + getBuildingBlock().getName() + ".java"); //$NON-NLS-1$ + + if ((javaFile != null) && javaFile.exists()) + { + try + { + JavaUI.openInEditor(javaFile); + } + catch (Exception e) + { + // Do nothing + StudioLogger.error(NewActivityBasedOnTemplateWizard.class, + "Could not open the activity " //$NON-NLS-1$ + + getBuildingBlock().getName() + " on an editor.", e); //$NON-NLS-1$ + } + } + } + + if (saved) + { + // Collecting usage data for statistical purposes + try + { + StudioLogger.collectUsageData(StudioLogger.WHAT_BUILDINGBLOCK_ACTIVITY, + StudioLogger.KIND_BUILDINGBLOCK, StudioLogger.DESCRIPTION_DEFAULT, + CodeUtilsActivator.PLUGIN_ID, CodeUtilsActivator.getDefault().getBundle() + .getVersion().toString()); + } + catch (Throwable e) + { + //Do nothing, but error on the log should never prevent app from working + } + } + return saved; + } + + /* (non-Javadoc) + * @see org.eclipse.ui.IWorkbenchWizard#init(org.eclipse.ui.IWorkbench, org.eclipse.jface.viewers.IStructuredSelection) + */ + public void init(IWorkbench workbench, IStructuredSelection selection) + { + setWindowTitle(CodeUtilsNLS.UI_NewActivityWizard_TitleNewActivityBasedOnTemplateWizard); + setNeedsProgressMonitor(true); + setDefaultPageImageDescriptor(CodeUtilsActivator.getImageDescriptor(WIZBAN_ICON)); + activity.configure(selection); + } + + /* (non-Javadoc) + * @see org.eclipse.jface.wizard.Wizard#addPages() + */ + @Override + public void addPages() + { + addPage(new ActivitySampleSelectionPage(activity)); + addPage(new NewActivityBasedOnTemplatePage(activity)); + IDatabaseSampleActivityParametersWizardCollector collector = + activity.getDatabaseSampleActivityParametersWizardCollector(); + if (collector != null) + { + List<IWizardPage> contributedPageList = collector.getWizardPages(); + if (contributedPageList != null) + { + for (IWizardPage page : contributedPageList) + { + if (page instanceof NewBuildingBlocksWizardPage) + { + //there is page to select parameters for activity creation + NewBuildingBlocksWizardPage buildBlockPage = + (NewBuildingBlocksWizardPage) page; + buildBlockPage.setBuildBlock(activity); + addPage(buildBlockPage); + } + } + } + } + addPage(new NewActivityWizardListTemplatesPage(activity)); + + } + + /* (non-Javadoc) + * @see com.motorola.studio.android.wizards.buildingblocks.NewBuildingBlocksWizard#getBuildingBlock() + */ + @Override + protected BuildingBlockModel getBuildingBlock() + { + return activity; + } +} diff --git a/src/plugins/android.codeutils/src/com/motorola/studio/android/wizards/buildingblocks/NewActivityMainPage.java b/src/plugins/android.codeutils/src/com/motorola/studio/android/wizards/buildingblocks/NewActivityMainPage.java new file mode 100644 index 0000000..b91a5b1 --- /dev/null +++ b/src/plugins/android.codeutils/src/com/motorola/studio/android/wizards/buildingblocks/NewActivityMainPage.java @@ -0,0 +1,277 @@ +/* + * Copyright (C) 2012 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.motorola.studio.android.wizards.buildingblocks; + +import org.eclipse.core.runtime.CoreException; +import org.eclipse.jface.resource.ImageDescriptor; +import org.eclipse.jface.wizard.IWizardContainer; +import org.eclipse.jface.wizard.WizardDialog; +import org.eclipse.swt.SWT; +import org.eclipse.swt.custom.CLabel; +import org.eclipse.swt.events.SelectionAdapter; +import org.eclipse.swt.events.SelectionEvent; +import org.eclipse.swt.graphics.Image; +import org.eclipse.swt.layout.GridData; +import org.eclipse.swt.layout.GridLayout; +import org.eclipse.swt.widgets.Button; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Link; +import org.eclipse.ui.PlatformUI; +import org.eclipse.ui.wizards.IWizardDescriptor; + +import com.motorola.studio.android.codeutils.CodeUtilsActivator; +import com.motorola.studio.android.codeutils.i18n.CodeUtilsNLS; +import com.motorola.studio.android.common.exception.AndroidException; +import com.motorola.studio.android.common.log.StudioLogger; +import com.motorola.studio.android.common.utilities.AndroidUtils; +import com.motorola.studio.android.model.Activity; + +/** + * Class that implements the Activity Wizard Main Page + */ +public class NewActivityMainPage extends NewLauncherWizardPage +{ + @Override + public boolean canFlipToNextPage() + { + + return false; + } + + private static final String NEW_ACTIVITY_HELP = CodeUtilsActivator.PLUGIN_ID + ".newactivity"; + + /** + * Default constructor + * + * @param activity The activity model + */ + public NewActivityMainPage(Activity activity) + { + super(activity, CodeUtilsNLS.UI_NewActivityMainPage_PageTitle); + } + + /* + * (non-Javadoc) + * @see com.motorola.studio.android.wizards.buildingblocks.NewBuildingBlocksWizard#getBuildBlock() + */ + @Override + public Activity getBuildBlock() + { + return (Activity) super.getBuildBlock(); + } + + /* (non-Javadoc) + * @see com.motorola.studio.android.wizards.buildingblocks.NewBuildingBlocksWizardPage#getMethods() + */ + @Override + protected Method[] getMethods() + { + Method onCreateMethod = new Method(getBuildBlock().getOnStartMessage()) + { + @Override + public void handle(boolean selection) + { + getBuildBlock().setOnStart(selection); + } + }; + return new Method[] + { + onCreateMethod + }; + } + + /* (non-Javadoc) + * @see com.motorola.studio.android.wizards.buildingblocks.NewBuildingBlocksWizardPage#createIntermediateControls(org.eclipse.swt.widgets.Composite) + */ + @Override + protected void createIntermediateControls(Composite parent) + { + createIntentFilterControls(parent); + createMainActivityControl(parent, 4); + createSeparator(parent, 4); + } + + /** + * add samples control. + * + * @param composite + * The wizard page composite + */ + @Override + protected void createSampleControls(Composite composite, int nColumns) + { + GridData data = null; + + Composite linkCompositecomposite = new Composite(composite, SWT.FILL); + linkCompositecomposite.setFont(composite.getFont()); + GridLayout layout = new GridLayout(nColumns, false); + linkCompositecomposite.setLayout(layout); + data = new GridData(SWT.FILL, SWT.FILL, true, true, nColumns, 2); + linkCompositecomposite.setLayoutData(data); + + Image image = null; + try + { + ImageDescriptor imageDesc = + ImageDescriptor.createFromURL(CodeUtilsActivator.getDefault().getBundle() + .getEntry("icons/obj16/new_activity_template_wiz.png")); //$NON-NLS-1$ + image = imageDesc.createImage(); + } + catch (Exception ex) + { + // do nothing; + } + + CLabel imgLabelLeft = new CLabel(linkCompositecomposite, SWT.CENTER); + data = new GridData(SWT.LEFT, SWT.CENTER, false, false); + imgLabelLeft.setLayoutData(data); + imgLabelLeft.setImage(image); + + final Link templateLink = new Link(linkCompositecomposite, SWT.NONE); + data = new GridData(SWT.LEFT, SWT.CENTER, true, true); + templateLink.setLayoutData(data); + templateLink.setText("<a>" + CodeUtilsNLS.UI_CreateNewActivityBasedOnTemplateLink //$NON-NLS-1$ + + "</a>"); //$NON-NLS-1$ + + templateLink.addSelectionListener(new SelectionAdapter() + { + /* (non-Javadoc) + * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent) + */ + @Override + public void widgetSelected(SelectionEvent event) + { + + NewActivityWizard newActivityWizard = (NewActivityWizard) getWizard(); + IWizardContainer container = newActivityWizard.getContainer(); + + container.getShell().setVisible(false); + + if (container instanceof WizardDialog) + { + + IWizardDescriptor descriptor = + PlatformUI + .getWorkbench() + .getNewWizardRegistry() + .findWizard( + "com.motorola.studio.android.wizards.newActivityBasedOnTemplateWizard"); + if (descriptor != null) + { + NewActivityBasedOnTemplateWizard wizard = null; + try + { + wizard = (NewActivityBasedOnTemplateWizard) descriptor.createWizard(); + NewActivityWizard activityWizard = (NewActivityWizard) getWizard(); + + wizard.init(PlatformUI.getWorkbench(), activityWizard.getSelection()); + WizardDialog nextWd = new WizardDialog(getShell(), wizard); + nextWd.setTitle(wizard.getWindowTitle()); + nextWd.open(); + } + catch (CoreException e) + { + StudioLogger + .error(NewActivityMainPage.class, + "could not open new activity based on template wizard from inside new activity wizard"); + } + } + + WizardDialog nextWd = (WizardDialog) container; + nextWd.close(); + } + + } + + }); + + templateLink.setEnabled(true); + + } + + private void createMainActivityControl(Composite parent, int nColumns) + { + // Create a checkbox to allow the user to set the created activity as the MAIN activity + Button checkButtonMainActivity = new Button(parent, SWT.CHECK | SWT.LEFT); + GridData gridData = new GridData(SWT.FILL, SWT.CENTER, true, false, nColumns, 1); + checkButtonMainActivity.setLayoutData(gridData); + checkButtonMainActivity.setText(CodeUtilsNLS.UI_NewActivityMainPage_CheckMainButton); + checkButtonMainActivity.addSelectionListener(new SelectionAdapter() + { + + @Override + public void widgetSelected(SelectionEvent e) + { + if (e.widget instanceof Button) + { + // Set Activity flag accordingly + getBuildBlock().setMainActivity(((Button) e.widget).getSelection()); + + } + } + + }); + } + + /* (non-Javadoc) + * @see com.motorola.studio.android.wizards.buildingblocks.NewBuildingBlocksWizardPage#getDefaultMessage() + */ + @Override + public String getDefaultMessage() + { + return CodeUtilsNLS.UI_NewActivityMainPage_DescriptionCreateActivity; + } + + /* (non-Javadoc) + * @see com.motorola.studio.android.wizards.buildingblocks.NewBuildingBlocksWizardPage#getWizardTitle() + */ + @Override + public String getWizardTitle() + { + return CodeUtilsNLS.UI_NewActivityMainPage_TitleActivity; + } + + /* (non-Javadoc) + * @see com.motorola.studio.android.wizards.buildingblocks.NewLauncherWizardPage#getIntentFiltersActions() + */ + @Override + protected String[] getIntentFiltersActions() + { + String[] intentFiltersActions = new String[0]; + try + { + intentFiltersActions = AndroidUtils.getActivityActions(getBuildBlock().getProject()); + } + catch (AndroidException e) + { + setErrorMessage(e.getMessage()); + } + return intentFiltersActions; + } + + /** + * Gets the help ID to be used for attaching + * context sensitive help. + * + * Classes that extends this class and want to set + * their on help should override this method + */ + @Override + protected String getHelpId() + { + return NEW_ACTIVITY_HELP; + } +}
\ No newline at end of file diff --git a/src/plugins/android.codeutils/src/com/motorola/studio/android/wizards/buildingblocks/NewActivityWizard.java b/src/plugins/android.codeutils/src/com/motorola/studio/android/wizards/buildingblocks/NewActivityWizard.java new file mode 100644 index 0000000..0d57fb0 --- /dev/null +++ b/src/plugins/android.codeutils/src/com/motorola/studio/android/wizards/buildingblocks/NewActivityWizard.java @@ -0,0 +1,235 @@ +/* + * Copyright (C) 2012 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.motorola.studio.android.wizards.buildingblocks; + +import java.lang.reflect.InvocationTargetException; + +import org.eclipse.core.resources.IResource; +import org.eclipse.core.resources.IncrementalProjectBuilder; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Status; +import org.eclipse.jdt.core.ICompilationUnit; +import org.eclipse.jdt.core.JavaModelException; +import org.eclipse.jdt.ui.JavaUI; +import org.eclipse.jface.operation.IRunnableWithProgress; +import org.eclipse.jface.viewers.IStructuredSelection; +import org.eclipse.ui.IWorkbench; +import org.eclipse.ui.PartInitException; + +import com.motorola.studio.android.codeutils.CodeUtilsActivator; +import com.motorola.studio.android.codeutils.i18n.CodeUtilsNLS; +import com.motorola.studio.android.common.exception.AndroidException; +import com.motorola.studio.android.common.log.StudioLogger; +import com.motorola.studio.android.common.utilities.EclipseUtils; +import com.motorola.studio.android.model.Activity; +import com.motorola.studio.android.model.BuildingBlockModel; + +/** + * Class that implements the Activity Wizard. + */ +public class NewActivityWizard extends NewBuildingBlocksWizard +{ + private static final String WIZBAN_ICON = "icons/wizban/new_activity_wiz.png"; //$NON-NLS-1$ + + private final Activity activity = new Activity(); + + private IStructuredSelection selection = null; + + private IWorkbench workbench = null; + + /** + * IRunnableWithProgress object to create the activity. + */ + private class DoSave implements IRunnableWithProgress + { + AndroidException exception = null; + + boolean saved = false; + + public void run(IProgressMonitor monitor) throws InvocationTargetException, + InterruptedException + { + try + { + saved = getBuildingBlock().save(getContainer(), monitor); + getBuildingBlock().getProject().refreshLocal(IResource.DEPTH_INFINITE, monitor); + getBuildingBlock().getProject().build(IncrementalProjectBuilder.INCREMENTAL_BUILD, + monitor); + } + catch (CoreException ce) + { + //build failed - show a warning message + StudioLogger.error(this.getClass(), ce.getMessage(), ce); + EclipseUtils + .showWarningDialog( + CodeUtilsNLS.UI_NewActivityWizard_TitleNewActivityWizard, + CodeUtilsNLS.NewActivityWizard_MessageSomeProblemsOccurredWhileBuildingProject); + } + catch (AndroidException e) + { + exception = e; + } + } + } + + /* (non-Javadoc) + * @see org.eclipse.jface.wizard.Wizard#canFinish() + */ + @Override + public boolean canFinish() + { + return !activity.needMoreInformation(); + } + + /* (non-Javadoc) + * @see org.eclipse.jface.wizard.Wizard#performFinish() + */ + @Override + public boolean performFinish() + { + boolean saved = false; + + try + { + DoSave doSave = new DoSave(); + + getContainer().run(false, false, doSave); + + if (doSave.exception != null) + { + throw doSave.exception; + } + else + { + saved = doSave.saved; + } + } + catch (AndroidException e) + { + IStatus status = + new Status(IStatus.ERROR, CodeUtilsActivator.PLUGIN_ID, e.getLocalizedMessage()); + EclipseUtils.showErrorDialog(CodeUtilsNLS.UI_GenericErrorDialogTitle, + CodeUtilsNLS.ERR_BuildingBlockCreation_ErrorMessage, status); + } + catch (InvocationTargetException e) + { + IStatus status = + new Status(IStatus.ERROR, CodeUtilsActivator.PLUGIN_ID, e.getLocalizedMessage()); + EclipseUtils.showErrorDialog(CodeUtilsNLS.UI_GenericErrorDialogTitle, + CodeUtilsNLS.ERR_BuildingBlockCreation_ErrorMessage, status); + } + catch (InterruptedException e) + { + IStatus status = + new Status(IStatus.ERROR, CodeUtilsActivator.PLUGIN_ID, e.getLocalizedMessage()); + EclipseUtils.showErrorDialog(CodeUtilsNLS.UI_GenericErrorDialogTitle, + CodeUtilsNLS.ERR_BuildingBlockCreation_ErrorMessage, status); + } + + if (saved) + { + ICompilationUnit javaFile = + getBuildingBlock().getPackageFragment().getCompilationUnit( + getBuildingBlock().getName() + ".java"); //$NON-NLS-1$ + + if ((javaFile != null) && javaFile.exists()) + { + try + { + JavaUI.openInEditor(javaFile); + } + catch (PartInitException e) + { + // Do nothing + StudioLogger.error(NewActivityWizard.class, "Could not open the activity " //$NON-NLS-1$ + + getBuildingBlock().getName() + " on an editor.", e); //$NON-NLS-1$ + } + catch (JavaModelException e) + { + // Do nothing + StudioLogger.error(NewActivityWizard.class, "Could not open the activity " //$NON-NLS-1$ + + getBuildingBlock().getName() + " on an editor.", e); //$NON-NLS-1$ + } + } + } + + if (saved) + { + // Collecting usage data for statistical purposes + try + { + StudioLogger.collectUsageData(StudioLogger.WHAT_BUILDINGBLOCK_ACTIVITY, + StudioLogger.KIND_BUILDINGBLOCK, StudioLogger.DESCRIPTION_DEFAULT, + CodeUtilsActivator.PLUGIN_ID, CodeUtilsActivator.getDefault().getBundle() + .getVersion().toString()); + } + catch (Throwable e) + { + //Do nothing, but error on the log should never prevent app from working + } + } + return saved; + } + + /* (non-Javadoc) + * @see org.eclipse.ui.IWorkbenchWizard#init(org.eclipse.ui.IWorkbench, org.eclipse.jface.viewers.IStructuredSelection) + */ + public void init(IWorkbench workbench, IStructuredSelection selection) + { + this.selection = selection; + this.workbench = workbench; + setWindowTitle(CodeUtilsNLS.UI_NewActivityWizard_TitleNewActivityWizard); + setNeedsProgressMonitor(true); + setDefaultPageImageDescriptor(CodeUtilsActivator.getImageDescriptor(WIZBAN_ICON)); + activity.configure(selection); + } + + /* (non-Javadoc) + * @see org.eclipse.jface.wizard.Wizard#addPages() + */ + @Override + public void addPages() + { + addPage(new NewActivityMainPage(activity)); + } + + /* (non-Javadoc) + * @see com.motorola.studio.android.wizards.buildingblocks.NewBuildingBlocksWizard#getBuildingBlock() + */ + @Override + protected BuildingBlockModel getBuildingBlock() + { + return activity; + } + + /** + * @return the selection + */ + protected IStructuredSelection getSelection() + { + return selection; + } + + /** + * @return the workbench + */ + protected IWorkbench getWorkbench() + { + return workbench; + } +} diff --git a/src/plugins/android.codeutils/src/com/motorola/studio/android/wizards/buildingblocks/NewActivityWizardListTemplatesPage.java b/src/plugins/android.codeutils/src/com/motorola/studio/android/wizards/buildingblocks/NewActivityWizardListTemplatesPage.java new file mode 100644 index 0000000..0b53800 --- /dev/null +++ b/src/plugins/android.codeutils/src/com/motorola/studio/android/wizards/buildingblocks/NewActivityWizardListTemplatesPage.java @@ -0,0 +1,383 @@ +/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.motorola.studio.android.wizards.buildingblocks;
+
+import java.net.URL;
+
+import org.eclipse.core.runtime.FileLocator;
+import org.eclipse.core.runtime.IPath;
+import org.eclipse.jface.dialogs.IPageChangeProvider;
+import org.eclipse.jface.dialogs.IPageChangedListener;
+import org.eclipse.jface.dialogs.PageChangedEvent;
+import org.eclipse.jface.resource.ImageDescriptor;
+import org.eclipse.jface.viewers.ISelection;
+import org.eclipse.jface.viewers.ISelectionChangedListener;
+import org.eclipse.jface.viewers.LabelProvider;
+import org.eclipse.jface.viewers.SelectionChangedEvent;
+import org.eclipse.jface.viewers.TreeViewer;
+import org.eclipse.jface.viewers.ViewerComparator;
+import org.eclipse.jface.wizard.IWizardPage;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.custom.ScrolledComposite;
+import org.eclipse.swt.graphics.Image;
+import org.eclipse.swt.layout.GridData;
+import org.eclipse.swt.layout.GridLayout;
+import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Group;
+import org.eclipse.swt.widgets.Label;
+
+import com.motorola.studio.android.codeutils.CodeUtilsActivator;
+import com.motorola.studio.android.codeutils.i18n.CodeUtilsNLS;
+import com.motorola.studio.android.common.exception.AndroidException;
+import com.motorola.studio.android.common.utilities.AndroidUtils;
+import com.motorola.studio.android.model.ActivityBasedOnTemplate;
+
+/**
+ * New activity wizard sample page.
+ */
+public class NewActivityWizardListTemplatesPage extends NewLauncherWizardPage
+{
+
+ private TreeViewer treeViewer;
+
+ private Label imgLabel;
+
+ private Label descriptionLabel;
+
+ private String content[];
+
+ private static final String NEW_ACTIVITY_HELP = CodeUtilsActivator.PLUGIN_ID + ".newactivity";
+
+ private static Image androidImg = null;
+
+ public static final String PAGE_NAME = "List Activities Page";
+
+ //private boolean canFlip = false;
+
+ /*
+ * Listener to update description pane whenever this page is open
+ */
+ private class PageChangeListener implements IPageChangedListener
+ {
+ public void pageChanged(PageChangedEvent event)
+ {
+ if ((event.getSelectedPage() == NewActivityWizardListTemplatesPage.this))
+ {
+ if (!treeViewer.getSelection().isEmpty())
+ {
+ updateTreeViewAfterSelection(treeViewer.getSelection());
+ }
+
+ NewActivityWizardListTemplatesPage.this.getControl().update();
+ ((NewActivityWizardListTemplatesPage) event.getSelectedPage())
+ .updateDescriptionPane();
+ }
+ }
+ }
+
+ protected NewActivityWizardListTemplatesPage(ActivityBasedOnTemplate activity)
+ {
+ super(activity, PAGE_NAME);
+
+ activity.evaluateSamplesList(ActivityBasedOnTemplate.SAMPLE_CATEGORY.LIST_ACTIVITIES_CATEGORY);
+
+ ImageDescriptor descr =
+ CodeUtilsActivator.imageDescriptorFromPlugin(CodeUtilsActivator.PLUGIN_ID,
+ "icons/device_refresh_on.png");
+ androidImg = descr.createImage();
+ }
+
+ /*
+ * (non-Javadoc)
+ * */
+ @Override
+ public boolean canFlipToNextPage()
+ {
+ return getBuildBlock().isListActivitySelected();
+ }
+
+ @Override
+ public IWizardPage getNextPage()
+ {
+ return this.getWizard().getPage(CodeUtilsNLS.UI_NewActivityMainPage_PageTitle);
+ }
+
+ @Override
+ public IWizardPage getPreviousPage()
+ {
+ return this.getWizard().getPage(ActivitySampleSelectionPage.PAGE_NAME);
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see com.motorola.studio.android.wizards.buildingblocks.NewBuildingBlocksWizard#getBuildBlock()
+ */
+ @Override
+ public ActivityBasedOnTemplate getBuildBlock()
+ {
+ return (ActivityBasedOnTemplate) super.getBuildBlock();
+ }
+
+ /* (non-Javadoc)
+ * @see com.motorola.studio.android.wizards.buildingblocks.NewBuildingBlocksWizardPage#getMethods()
+ */
+ @Override
+ protected Method[] getMethods()
+ {
+ Method onCreateMethod = new Method(getBuildBlock().getOnStartMessage())
+ {
+ @Override
+ public void handle(boolean selection)
+ {
+ getBuildBlock().setOnStart(selection);
+ }
+ };
+ return new Method[]
+ {
+ onCreateMethod
+ };
+ }
+
+ /* (non-Javadoc)
+ * @see com.motorola.studio.android.wizards.buildingblocks.NewBuildingBlocksWizardPage#createIntermediateControls(org.eclipse.swt.widgets.Composite)
+ */
+ @Override
+ protected void createExtendedControls(Composite parent)
+ {
+ Composite mainComposite = new Composite(parent, SWT.FILL);
+ mainComposite.setLayout(new GridLayout(2, false));
+ mainComposite.setLayoutData(new GridData(GridData.FILL_BOTH));
+
+ // Samples Tree Label
+ Label itemsTableLabel = new Label(mainComposite, SWT.NONE);
+ itemsTableLabel
+ .setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, false, 2, 1));
+ itemsTableLabel.setText(CodeUtilsNLS.UI_SampleSelectionPage_SamplesTreeLabel);
+
+ // Samples Tree Viewer
+ treeViewer = new TreeViewer(mainComposite, SWT.BORDER | SWT.SINGLE | SWT.V_SCROLL);
+ treeViewer.getControl().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
+ treeViewer.setLabelProvider(new LabelProvider()
+ {
+ @Override
+ public Image getImage(Object obj)
+ {
+ return androidImg;
+ }
+
+ @Override
+ public String getText(Object element)
+ {
+ return element.toString();
+ }
+ });
+
+ content = new String[getBuildBlock().getListActivitiesSamples().size()];
+
+ int i = 0;
+ for (String currentSample : getBuildBlock().getListActivitiesSamples().keySet())
+ {
+ content[i] = currentSample;
+ i++;
+ }
+
+ //sets tree content and icon
+ treeViewer.setContentProvider(new SampleTreeContentProvider(content));
+ treeViewer.setInput(content);
+
+ final Group previewGroup = new Group(mainComposite, SWT.NONE);
+ previewGroup.setText(CodeUtilsNLS.UI_ListActivityPage_Preview);
+ previewGroup.setLayout(new GridLayout(1, false));
+ previewGroup.setLayoutData(new GridData(GridData.FILL, GridData.FILL, false, true, 1, 1));
+
+ imgLabel = new Label(previewGroup, SWT.NONE);
+ imgLabel.setImage(null);
+
+ GridData imageLabelData = new GridData(GridData.FILL, GridData.FILL, true, true);
+ imageLabelData.widthHint = 200;
+ imgLabel.setLayoutData(imageLabelData);
+
+ final Group descriptionGroup = new Group(mainComposite, SWT.NONE);
+
+ treeViewer.addSelectionChangedListener(new ISelectionChangedListener()
+ {
+ public void selectionChanged(SelectionChangedEvent e)
+ {
+ updateTreeViewAfterSelection(e.getSelection());
+
+ updateDescriptionPane();
+ getWizard().getContainer().updateButtons();
+ }
+ });
+
+ //sort tree
+ treeViewer.setComparator(new ViewerComparator());
+ treeViewer.expandAll();
+
+ //description pane
+ descriptionGroup.setText(CodeUtilsNLS.UI_SampleSelectionPage_SamplesDescriptionPane);
+ descriptionGroup.setLayout(new GridLayout(1, false));
+ descriptionGroup.setLayoutData(new GridData(GridData.FILL, GridData.CENTER, true, false, 2,
+ 1));
+
+ ScrolledComposite scroll = new ScrolledComposite(descriptionGroup, SWT.V_SCROLL);
+ GridData scrollData = new GridData(GridData.FILL, GridData.FILL, true, true);
+ scroll.setLayoutData(scrollData);
+ scroll.setMinSize(100, 140);
+
+ descriptionLabel = new Label(scroll, SWT.FILL | SWT.WRAP);
+ descriptionLabel.setText("");
+
+ scroll.setContent(descriptionLabel);
+
+ // Add a listener to the wizard to listen for page changes
+ if (getContainer() instanceof IPageChangeProvider)
+ {
+ ((IPageChangeProvider) getContainer()).addPageChangedListener(new PageChangeListener());
+ }
+
+ setControl(mainComposite);
+ }
+
+ private void updateTreeViewAfterSelection(ISelection selection)
+ {
+ String template = selection.toString();
+
+ getBuildBlock().setSample(template.substring(1, template.length() - 1));
+
+ //condition to enable finish button
+ getBuildBlock().setIsListActivitySelected(true);
+ //category of sample, used to load the correct files
+ getBuildBlock().setSampleCategoty(
+ ActivityBasedOnTemplate.SAMPLE_CATEGORY.LIST_ACTIVITIES_CATEGORY);
+
+ String strPreview = getBuildBlock().getSamplePreview();
+ if (strPreview != null)
+ {
+ URL url = null;
+ try
+ {
+ //loads the selected sample preview image
+ url =
+ FileLocator.toFileURL(CodeUtilsActivator
+ .getDefault()
+ .getBundle()
+ .getEntry(
+ ActivityBasedOnTemplate.ACTIVITY_SAMPLES_FOLDER
+ + IPath.SEPARATOR + strPreview));
+
+ ImageDescriptor imageDesc = ImageDescriptor.createFromURL(url);
+ Image image = imageDesc.createImage();
+
+ Image previousImage = imgLabel.getImage();
+ if (previousImage != null)
+ {
+ previousImage.dispose();
+ }
+
+ imgLabel.setImage(image);
+ }
+ catch (Exception ex)
+ {
+ imgLabel.setImage(null);
+ }
+ }
+ else
+ {
+ imgLabel.setImage(null);
+ }
+ }
+
+ /*
+ * Updates selected label description
+ */
+ private void updateDescriptionPane()
+ {
+ if (getBuildBlock().isListActivitySelected())
+ {
+ descriptionLabel.setText(getBuildBlock().getSampleDescription());
+ }
+ else
+ {
+ descriptionLabel.setText("");
+ imgLabel.setImage(null);
+ }
+ descriptionLabel.update();
+ descriptionLabel.setSize(descriptionLabel.computeSize(SWT.DEFAULT, SWT.DEFAULT));
+ descriptionLabel.getParent().layout();
+ }
+
+ /* (non-Javadoc)
+ * @see com.motorola.studio.android.wizards.buildingblocks.NewBuildingBlocksWizardPage#getDefaultMessage()
+ */
+ @Override
+ public String getDefaultMessage()
+ {
+ return CodeUtilsNLS.UI_NewActivityMainPage_DescriptionCreateActivityBasedOnTemplate;
+ }
+
+ /* (non-Javadoc)
+ * @see com.motorola.studio.android.wizards.buildingblocks.NewBuildingBlocksWizardPage#getWizardTitle()
+ */
+ @Override
+ public String getWizardTitle()
+ {
+ return CodeUtilsNLS.UI_ListActivityPage_TitleWizard;
+ }
+
+ /* (non-Javadoc)
+ * @see com.motorola.studio.android.wizards.buildingblocks.NewLauncherWizardPage#getIntentFiltersActions()
+ */
+ @Override
+ protected String[] getIntentFiltersActions()
+ {
+ String[] intentFiltersActions = new String[0];
+ try
+ {
+ intentFiltersActions = AndroidUtils.getActivityActions(getBuildBlock().getProject());
+ }
+ catch (AndroidException e)
+ {
+ setErrorMessage(e.getMessage());
+ }
+ return intentFiltersActions;
+ }
+
+ /**
+ * Gets the help ID to be used for attaching
+ * context sensitive help.
+ *
+ * Classes that extends this class and want to set
+ * their on help should override this method
+ */
+ @Override
+ protected String getHelpId()
+ {
+ return NEW_ACTIVITY_HELP;
+ }
+
+ /**
+ * Returns true if page has header false otherwise
+ *
+ * @return true if page has header false otherwise
+ */
+ @Override
+ public boolean hasHeader()
+ {
+ return false;
+ }
+}
diff --git a/src/plugins/android.codeutils/src/com/motorola/studio/android/wizards/buildingblocks/NewBuildingBlocksWizard.java b/src/plugins/android.codeutils/src/com/motorola/studio/android/wizards/buildingblocks/NewBuildingBlocksWizard.java new file mode 100644 index 0000000..a705ee0 --- /dev/null +++ b/src/plugins/android.codeutils/src/com/motorola/studio/android/wizards/buildingblocks/NewBuildingBlocksWizard.java @@ -0,0 +1,44 @@ +/* + * Copyright (C) 2012 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.motorola.studio.android.wizards.buildingblocks; + +import org.eclipse.core.runtime.IStatus; +import org.eclipse.jface.wizard.Wizard; +import org.eclipse.ui.INewWizard; + +import com.motorola.studio.android.model.BuildingBlockModel; + +/** + * Base abstract class to create the building block wizards UI. + * A building block represents an Android abstraction, which can be an Activity, Service, Broadcast Receiver, Content Provider or Wigdet Provider. + */ +public abstract class NewBuildingBlocksWizard extends Wizard implements INewWizard +{ + /* (non-Javadoc) + * @see org.eclipse.jface.wizard.Wizard#canFinish() + */ + @Override + public boolean canFinish() + { + return (!getBuildingBlock().needMoreInformation()) + && (getBuildingBlock().getStatus().getSeverity() != IStatus.ERROR); + } + + /** + * @return The building block model used by the wizard. + */ + protected abstract BuildingBlockModel getBuildingBlock(); +} diff --git a/src/plugins/android.codeutils/src/com/motorola/studio/android/wizards/buildingblocks/NewBuildingBlocksWizardPage.java b/src/plugins/android.codeutils/src/com/motorola/studio/android/wizards/buildingblocks/NewBuildingBlocksWizardPage.java new file mode 100644 index 0000000..c10e668 --- /dev/null +++ b/src/plugins/android.codeutils/src/com/motorola/studio/android/wizards/buildingblocks/NewBuildingBlocksWizardPage.java @@ -0,0 +1,1078 @@ +/* + * Copyright (C) 2012 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.motorola.studio.android.wizards.buildingblocks; + +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.eclipse.core.resources.IResource; +import org.eclipse.core.resources.IWorkspaceRoot; +import org.eclipse.core.resources.ResourcesPlugin; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.MultiStatus; +import org.eclipse.core.runtime.Status; +import org.eclipse.jdt.core.IJavaElement; +import org.eclipse.jdt.core.IJavaProject; +import org.eclipse.jdt.core.IPackageFragment; +import org.eclipse.jdt.core.IPackageFragmentRoot; +import org.eclipse.jdt.core.JavaCore; +import org.eclipse.jdt.core.JavaModelException; +import org.eclipse.jdt.ui.JavaElementComparator; +import org.eclipse.jdt.ui.JavaElementLabelProvider; +import org.eclipse.jdt.ui.StandardJavaElementContentProvider; +import org.eclipse.jdt.ui.wizards.NewTypeWizardPage; +import org.eclipse.jface.dialogs.Dialog; +import org.eclipse.jface.viewers.ILabelProvider; +import org.eclipse.jface.viewers.ViewerFilter; +import org.eclipse.jface.window.Window; +import org.eclipse.osgi.util.NLS; +import org.eclipse.swt.SWT; +import org.eclipse.swt.custom.ScrolledComposite; +import org.eclipse.swt.events.ControlAdapter; +import org.eclipse.swt.events.ControlEvent; +import org.eclipse.swt.events.ShellEvent; +import org.eclipse.swt.events.ShellListener; +import org.eclipse.swt.layout.FillLayout; +import org.eclipse.swt.layout.GridData; +import org.eclipse.swt.layout.GridLayout; +import org.eclipse.swt.widgets.Button; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Event; +import org.eclipse.swt.widgets.Label; +import org.eclipse.swt.widgets.List; +import org.eclipse.swt.widgets.Listener; +import org.eclipse.swt.widgets.Shell; +import org.eclipse.swt.widgets.Text; +import org.eclipse.ui.PlatformUI; +import org.eclipse.ui.dialogs.ElementTreeSelectionDialog; +import org.eclipse.ui.dialogs.ISelectionStatusValidator; + +import com.motorola.studio.android.codeutils.CodeUtilsActivator; +import com.motorola.studio.android.codeutils.i18n.CodeUtilsNLS; +import com.motorola.studio.android.common.IAndroidConstants; +import com.motorola.studio.android.common.exception.AndroidException; +import com.motorola.studio.android.common.log.StudioLogger; +import com.motorola.studio.android.common.utilities.AndroidUtils; +import com.motorola.studio.android.common.utilities.EclipseUtils; +import com.motorola.studio.android.manifest.AndroidProjectManifestFile; +import com.motorola.studio.android.model.BuildingBlockModel; +import com.motorola.studio.android.model.manifest.AndroidManifestFile; +import com.motorola.studio.android.wizards.elements.AddRemoveButtons; + +/** + * Abstract class used to create the building block wizard main pages. + */ +public abstract class NewBuildingBlocksWizardPage extends NewTypeWizardPage +{ + private static final String JAVA_EXTENSION = ".java"; //$NON-NLS-1$ + + private static final int MAX_PATH_SIZE = 255; + + protected static String LABEL = TYPENAME + ".LABEL"; //$NON-NLS-1$ + + protected IWorkspaceRoot fWorkspaceRoot = ResourcesPlugin.getWorkspace().getRoot(); + + private BuildingBlockModel buildBlock; + + private Text labelText; + + private Button defaultLabelButton; + + private AddRemoveButtons addRemovePermissionsButtons; + + private List activityPermissions; + + private final Set<String> intentFilterPermissions = new HashSet<String>(); + + private MethodCreationControl methodCreationControl; + + /** + * Listener to check if the wizard can be opened. + */ + private class WizardShellListener implements ShellListener + { + private boolean wasChecked = false; + + /* + * (non-Javadoc) + * + * @see + * org.eclipse.swt.events.ShellListener#shellActivated(org.eclipse.swt + * .events.ShellEvent) + */ + @Override + public void shellActivated(ShellEvent e) + { + if (!wasChecked) + { + wasChecked = true; + + if (!canOpen()) + { + ((Shell) e.widget).close(); + } + } + + } + + /* + * (non-Javadoc) + * + * @see + * org.eclipse.swt.events.ShellListener#shellClosed(org.eclipse.swt. + * events.ShellEvent) + */ + @Override + public void shellClosed(ShellEvent e) + { + // Do nothing + } + + /* + * (non-Javadoc) + * + * @see + * org.eclipse.swt.events.ShellListener#shellDeactivated(org.eclipse + * .swt.events.ShellEvent) + */ + @Override + public void shellDeactivated(ShellEvent e) + { + // Do nothing + } + + /* + * (non-Javadoc) + * + * @see + * org.eclipse.swt.events.ShellListener#shellDeiconified(org.eclipse + * .swt.events.ShellEvent) + */ + @Override + public void shellDeiconified(ShellEvent e) + { + // Do nothing + } + + /* + * (non-Javadoc) + * + * @see + * org.eclipse.swt.events.ShellListener#shellIconified(org.eclipse.swt + * .events.ShellEvent) + */ + @Override + public void shellIconified(ShellEvent e) + { + // Do nothing + } + + } + + /* Each building block is represent by a class (e.g., an Activity or Service). + * Each of these classes contain some methods that must be overridden by subclasses + * in order to be called by android framework (e.g., onCreate(...) methods). + * This class is responsible to create check boxes that let users choose which of these methods + * should be automatically created by the wizard. + * */ + private class MethodCreationControl + { + private Label stubMessage; + + private Button[] stubButtonArray; + + MethodCreationControl(Composite parent, Method[] methods) + { + if (methods != null) + { + if (methods.length > 0) + { + stubMessage = new Label(parent, SWT.NONE); + stubMessage.setLayoutData(new GridData(GridData.FILL, GridData.FILL, false, + false, 4, 1)); + stubMessage + .setText(CodeUtilsNLS.UI_NewBuildingBlocksWizardPage_QuestionWhichMethodCreate); + } + + createStubsComponent(parent, methods); + } + } + + /* + * Creates a single method declaration to the wizard page + * + * @param parent + * The wizard page composite + * @param method + * The method to add + */ + private void createStubsComponent(Composite parent, Method[] methods) + { + stubButtonArray = new Button[methods.length]; + int i = 0; + for (final Method method : methods) + { + new Label(parent, SWT.NONE); + final Button stubsButton = new Button(parent, SWT.CHECK); + stubsButton.setLayoutData(new GridData(GridData.FILL, GridData.FILL, false, false, + 3, 1)); + stubsButton.setText(method.getMessage()); + stubsButton.addListener(SWT.Selection, new Listener() + { + @Override + public void handleEvent(Event event) + { + method.handle(stubsButton.getSelection()); + } + }); + stubButtonArray[i++] = stubsButton; + } + } + + public void setMethodCreationControlEnabled(boolean enabled) + { + stubMessage.setEnabled(enabled); + for (Button stubButton : stubButtonArray) + { + stubButton.setEnabled(enabled); + } + } + } + + /** + * Default constructor. + * + * @param buildBlock + * The building block model that the wizard will create. + * @param pageName + * The page name. + */ + protected NewBuildingBlocksWizardPage(BuildingBlockModel buildBlock, String pageName) + { + super(true, pageName); + this.buildBlock = buildBlock; + setTitle(getWizardTitle()); + setDescription(getDefaultMessage()); + setPageComplete(false); + } + + /** + * Gets the help ID to be used for attaching context sensitive help. + * + * Classes that extends this class and want to set their own help should + * override this method. + */ + protected abstract String getHelpId(); + + /** + * Returns the wizard title. + * + * @return the wizard title. + */ + public abstract String getWizardTitle(); + + /** + * Returns the wizard default status message. + * + * @return the wizard default status message. + */ + public abstract String getDefaultMessage(); + + /** + * Returns all methods that the building block can override. + * + * @return all methods that the building block can override. + */ + protected abstract Method[] getMethods(); + + /** + * @param enabled If true, all available methods in the building block will be checked + * for automatic creation. + * */ + public void setMethodCreationControlEnabled(boolean enabled) + { + methodCreationControl.setMethodCreationControlEnabled(enabled); + } + + /* + * (non-Javadoc) + * + * @see + * org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets + * .Composite) + */ + @Override + public void createControl(Composite parent) + { + initializeDialogUnits(parent); + + // main control + Composite mainComposite = new Composite(parent, SWT.FILL); + mainComposite.setLayout(new FillLayout(SWT.FILL)); + final ScrolledComposite scroll = + new ScrolledComposite(mainComposite, SWT.H_SCROLL | SWT.V_SCROLL); + + final Composite composite = new Composite(scroll, SWT.NONE); + composite.setFont(parent.getFont()); + + int nColumns = 4; + + createSampleControls(composite, nColumns); + + GridLayout layout = new GridLayout(nColumns, false); + composite.setLayout(layout); + composite.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, true)); + + if (hasHeader()) + { + setPackageFragmentRoot(getBuildBlock().getPackageFragmentRoot(), true); + createContainerControls(composite, nColumns); + setPackageFragment(getBuildBlock().getPackageFragment(), true); + createPackageControls(composite, nColumns); + + createSeparator(composite, nColumns); + + createTypeNameControls(composite, nColumns); + + createLabelControls(composite); + + setSuperClass(getBuildBlock().getSuperClass(), getBuildBlock().useExtendedClass()); + createSuperClassControls(composite, nColumns); + + createPermissionControls(composite); + + createIntermediateControls(composite); + + createMethodCreationControl(composite, getMethods()); + } + createExtendedControls(composite); + + // set up scroll + scroll.setContent(composite); + + scroll.setExpandHorizontal(true); + scroll.setExpandVertical(true); + + scroll.addControlListener(new ControlAdapter() + { + @Override + public void controlResized(ControlEvent e) + { + scroll.setMinSize(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT)); + } + }); + + setControl(mainComposite); + Dialog.applyDialogFont(mainComposite); + + mainComposite.getShell().addShellListener(new WizardShellListener()); + PlatformUI.getWorkbench().getHelpSystem().setHelp(parent, getHelpId()); + PlatformUI.getWorkbench().getHelpSystem().setHelp(mainComposite, getHelpId()); + } + + /** + * Override this class to create the label controls. + * + * @param parent + * The wizard page composite + */ + protected void createLabelControls(Composite parent) + { + Label label = new Label(parent, SWT.NONE); + label.setText(CodeUtilsNLS.UI_NewBuildingBlocksWizardPage_TextLabel); + + labelText = new Text(parent, SWT.BORDER); + GridData gridData = new GridData(GridData.FILL_HORIZONTAL); + gridData.horizontalSpan = 2; + labelText.setLayoutData(gridData); + labelText.setEnabled(false); + + defaultLabelButton = new Button(parent, SWT.CHECK); + defaultLabelButton.setText(CodeUtilsNLS.UI_NewBuildingBlocksWizardPage_ButtonNameDefault); + defaultLabelButton.setSelection(true); + + Listener listener = new Listener() + { + @Override + public void handleEvent(Event event) + { + if (!defaultLabelButton.getSelection() || !event.widget.equals(labelText)) + { + handleFieldChanged(LABEL); + } + if (event.widget.equals(defaultLabelButton) && !defaultLabelButton.getSelection() + && labelText.isEnabled()) + { + labelText.forceFocus(); + labelText.selectAll(); + } + } + }; + + labelText.addListener(SWT.Modify, listener); + defaultLabelButton.addListener(SWT.Selection, listener); + } + + /** + * Override this class to add samples control. + * + * @param composite + * The wizard page composite + */ + protected void createSampleControls(Composite composite, int nColumns) + { + //default implementation does nothing + } + + /** + * Override this class to add components after superclass control. + * + * @param composite + * The wizard page composite + */ + protected void createIntermediateControls(Composite composite) + { + //default implementation does nothing + } + + /** + * Return all Filter Permissions as an Array. + * + * @return + */ + public String[] getIntentFilterPermissionsAsArray() + { + return intentFilterPermissions.toArray(new String[intentFilterPermissions.size()]); + } + + /** + * Creates the "Permissions" section on the wizard. + * + * @param composite + * the wizard composite + */ + protected void createPermissionControls(Composite composite) + { + GridData gridData; + Label activityPermissionsLabel = new Label(composite, SWT.NONE); + activityPermissionsLabel.setText(CodeUtilsNLS.NewBuildingBlocksWizardPage_PermissionLabel); + gridData = new GridData(SWT.LEFT, SWT.CENTER, false, false); + gridData.verticalAlignment = GridData.BEGINNING; + activityPermissionsLabel.setLayoutData(gridData); + + activityPermissions = new List(composite, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL); + activityPermissions.setItems(getBuildBlock().getIntentFilterPermissionsAsArray()); + gridData = new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1); + gridData.heightHint = convertHeightInCharsToPixels(3); + activityPermissions.setLayoutData(gridData); + + addRemovePermissionsButtons = new AddRemoveButtons(composite); + setButtonLayoutData(addRemovePermissionsButtons.getAddButton()); + setButtonLayoutData(addRemovePermissionsButtons.getRemoveButton()); + addRemovePermissionsButtons.getAddButton().addListener(SWT.Selection, new Listener() + { + @Override + public void handleEvent(Event arg0) + { + Set<String> permissionSet = + new HashSet<String>(Arrays.asList(AndroidUtils + .getIntentFilterPermissions(getBuildBlock().getProject()))); + + permissionSet.removeAll(getBuildBlock().getIntentFilterPermissions()); + + FilteredActionsSelectionDialog dialog = + new FilteredActionsSelectionDialog(getShell(), permissionSet); + dialog.setInitialPattern("**"); //$NON-NLS-1$ + dialog.setTitle("Select an action permission"); //$NON-NLS-1$ + dialog.setMessage(CodeUtilsNLS.UI_NewLauncherWizardPage_CategorySelectionDialogMessage); + + if (Dialog.OK == dialog.open()) + { + for (Object result : dialog.getResult()) + { + getBuildBlock().addIntentFilterPermissions((String) result); + } + activityPermissions.setItems(getBuildBlock() + .getIntentFilterPermissionsAsArray()); + addRemovePermissionsButtons.getRemoveButton().setEnabled( + activityPermissions.getSelectionCount() > 0); + updateStatus(getBuildBlock().getStatus()); + } + } + }); + addRemovePermissionsButtons.getRemoveButton().addListener(SWT.Selection, new Listener() + { + @Override + public void handleEvent(Event arg0) + { + for (int selection : activityPermissions.getSelectionIndices()) + { + getBuildBlock().removeIntentFilterPermissions( + activityPermissions.getItem(selection)); + } + activityPermissions.setItems(getBuildBlock().getIntentFilterPermissionsAsArray()); + addRemovePermissionsButtons.getRemoveButton().setEnabled( + activityPermissions.getSelectionCount() > 0); + updateStatus(getBuildBlock().getStatus()); + } + }); + addRemovePermissionsButtons.getRemoveButton().setEnabled( + activityPermissions.getSelectionCount() > 0); + activityPermissions.addListener(SWT.Selection, new Listener() + { + @Override + public void handleEvent(Event arg0) + { + addRemovePermissionsButtons.getRemoveButton().setEnabled( + activityPermissions.getSelectionCount() > 0); + } + }); + } + + /** + * Override this class to add components at the end of the Page. + * + * @param parent + * The wizard page composite + */ + protected void createExtendedControls(Composite parent) + { + //default implementation does nothing + } + + /* + * (non-Javadoc) + * + * @see org.eclipse.jdt.ui.wizards.NewElementWizardPage#setVisible(boolean) + */ + @Override + public void setVisible(boolean visible) + { + super.setVisible(visible); + if (visible) + { + setFocus(); + } + } + + /** + * Returns true if page has header false otherwise. + * + * @return true if page has header false otherwise. + */ + public boolean hasHeader() + { + return true; + } + + /** + * Adds the methods that a building block can override to the wizard page. + * + * @param parent + * The wizard page composite. + * @param methods + * The methods to add to the wizard. + */ + protected void createMethodCreationControl(Composite parent, Method[] methods) + { + methodCreationControl = new MethodCreationControl(parent, methods); + } + + /* + * (non-Javadoc) + * + * @see org.eclipse.jdt.ui.wizards.NewContainerWizardPage#chooseContainer() + */ + @Override + protected IPackageFragmentRoot chooseContainer() + { + IJavaElement initElement = getPackageFragmentRoot(); + + ISelectionStatusValidator validator = new ElementTreeValidator(); + ViewerFilter filter = new ElementTreeViewFilter(); + StandardJavaElementContentProvider provider = new ElementTreeContentProvider(); + + ILabelProvider labelProvider = + new JavaElementLabelProvider(JavaElementLabelProvider.SHOW_DEFAULT); + ElementTreeSelectionDialog dialog = + new ElementTreeSelectionDialog(getShell(), labelProvider, provider); + dialog.setComparator(new JavaElementComparator()); + dialog.setValidator(validator); + dialog.setTitle(CodeUtilsNLS.UI_NewBuildingBlocksWizardPage_WizardTitle); + dialog.setMessage(CodeUtilsNLS.UI_NewBuildingBlocksWizardPage_MessageChooseFolder); + dialog.setInput(JavaCore.create(fWorkspaceRoot)); + dialog.setInitialSelection(initElement); + dialog.addFilter(filter); + dialog.setHelpAvailable(false); + + IPackageFragmentRoot rootSelection = null; + if (dialog.open() == Window.OK) + { + Object element = dialog.getFirstResult(); + if (element instanceof IJavaProject) + { + IJavaProject jproject = (IJavaProject) element; + rootSelection = jproject.getPackageFragmentRoot(jproject.getProject()); + } + else if (element instanceof IPackageFragmentRoot) + { + rootSelection = (IPackageFragmentRoot) element; + } + } + return rootSelection; + } + + /* + * (non-Javadoc) + * + * @see + * org.eclipse.jdt.ui.wizards.NewTypeWizardPage#handleFieldChanged(java. + * lang.String) + */ + @Override + protected void handleFieldChanged(String fieldName) + { + if (NewTypeWizardPage.TYPENAME.equals(fieldName)) + { + getBuildBlock().setName(getTypeName()); + getBuildBlock().setNameStatus(typeNameChanged()); + getBuildBlock().setPackageStatus(packageChanged()); + } + else if (NewTypeWizardPage.CONTAINER.equals(fieldName)) + { + // source folder + getBuildBlock().setPackageFragmentRoot(getPackageFragmentRoot()); + getBuildBlock().setPackageFragmentRootStatus(containerChanged()); + getBuildBlock().setPackageStatus(packageChanged()); + getBuildBlock().setNameStatus(typeNameChanged()); + + updatePackage(getPackageFragmentRoot()); + } + else if (NewTypeWizardPage.PACKAGE.equals(fieldName)) + { + if (getPackageFragmentRoot() != null) + { + getBuildBlock().setPackageFragment( + getPackageFragmentRoot().getPackageFragment(getPackageText())); + } + getBuildBlock().setPackageStatus(packageChanged()); + getBuildBlock().setNameStatus(typeNameChanged()); + } + else if (LABEL.equals(fieldName)) + { + getBuildBlock().setLabelStatus(labelChanged()); + } + updateStatus(getBuildBlock().getStatus()); + } + + private void updatePackage(IPackageFragmentRoot packageFragmentRoot) + { + if (packageFragmentRoot != null) + { + IJavaProject project = null; + IPackageFragment pack = null; + + project = packageFragmentRoot.getJavaProject(); + try + { + pack = EclipseUtils.getDefaultPackageFragment(project); + getBuildBlock().setPackageFragment(pack); + } + catch (JavaModelException e) + { + StudioLogger.error(NewBuildingBlocksWizardPage.class, + "Error getting default package fragment.", e); //$NON-NLS-1$ + // do nothing + } + setPackageFragment(pack, true); + handleFieldChanged(NewTypeWizardPage.PACKAGE); + } + } + + /** + * @return A status indicating if the building block label property has been change. + */ + protected IStatus labelChanged() + { + IStatus status = new Status(IStatus.OK, CodeUtilsActivator.PLUGIN_ID, null); + if ((defaultLabelButton != null) && (labelText != null)) + { + if (defaultLabelButton.getSelection()) + { + labelText.setText(""); //$NON-NLS-1$ + + } + labelText.setEnabled(!defaultLabelButton.getSelection()); + getBuildBlock().setLabel(getLabel()); + } + return status; + } + + /* + * (non-Javadoc) + * + * @see org.eclipse.jdt.ui.wizards.NewTypeWizardPage#typeNameChanged() + */ + @Override + protected IStatus typeNameChanged() + { + IStatus superStatus = super.typeNameChanged(); + IStatus status = + new Status(superStatus.getSeverity(), CodeUtilsActivator.PLUGIN_ID, + superStatus.getMessage()); + + Pattern pattern = Pattern.compile("([A-Za-z0-9_]+)"); //$NON-NLS-1$ + + if (superStatus.getSeverity() != IStatus.ERROR) + { + Matcher matcher = pattern.matcher(getTypeName()); + + if (!matcher.matches() || !matcher.group().equals(getTypeName())) + { + String errMsg = + NLS.bind(CodeUtilsNLS.ERR_NewBuildingBlocksWizardPage_InvalidTypeName, + getTypeName()); + + status = new Status(IStatus.ERROR, CodeUtilsActivator.PLUGIN_ID, errMsg); + } + else if (packageAndClassExist()) + { + status = + new Status( + IStatus.ERROR, + CodeUtilsActivator.PLUGIN_ID, + CodeUtilsNLS.ERR_NewBuildingBlocksWizardPage_PackageAndClassAlreadyExist); + } + else if (isTooLongOnFileSystem()) + { + status = + new Status(IStatus.ERROR, CodeUtilsActivator.PLUGIN_ID, + CodeUtilsNLS.ERR_NewBuildingBlocksWizardPage_FileNameTooLong); + } + } + + labelChanged(); + return status; + } + + /** + * Returns the building block label property value. + * + * @return the building block label property value. + */ + protected String getLabel() + { + String label; + if (defaultLabelButton.getSelection()) + { + label = ""; //$NON-NLS-1$ + } + else + { + label = labelText.getText(); + } + return label; + } + + /** + * Returns the building block model + * + * @return the building block model + */ + public BuildingBlockModel getBuildBlock() + { + return buildBlock; + } + + public void setBuildBlock(BuildingBlockModel buildBlock) + { + this.buildBlock = buildBlock; + } + + /* + * (non-Javadoc) + * + * @see org.eclipse.jdt.ui.wizards.NewTypeWizardPage#packageChanged() + */ + @Override + protected IStatus packageChanged() + { + IStatus superStatus = super.packageChanged(); + IStatus status = + new Status(superStatus.getSeverity(), CodeUtilsActivator.PLUGIN_ID, + superStatus.getMessage()); + + // The package name is being get by getPackageText because the method + // getPackageFragment + // (from super class) is not returning the right value in some cases + String packageName = getPackageText(); + + if (status.getCode() != IStatus.ERROR) + { + if (packageName != null) + { + Pattern pattern = Pattern.compile("[A-Za-z0-9_\\.]+"); //$NON-NLS-1$ + Matcher matcher = pattern.matcher(packageName); + + if (packageName.indexOf('.') == -1) + { + status = + new Status( + IStatus.ERROR, + CodeUtilsActivator.PLUGIN_ID, + CodeUtilsNLS.ERR_NewBuildingBlocksWizardPage_PackageMustHaveAtLeastTwoIdentifiers); + } + else if (!matcher.matches()) + { + String errMsg = + NLS.bind( + CodeUtilsNLS.ERR_NewBuildingBlocksWizardPage_InvalidPackageName, + packageName); + status = new Status(IStatus.ERROR, CodeUtilsActivator.PLUGIN_ID, errMsg); + } + else if (packageAndClassExist()) + { + status = + new Status( + IStatus.ERROR, + CodeUtilsActivator.PLUGIN_ID, + CodeUtilsNLS.ERR_NewBuildingBlocksWizardPage_PackageAndClassAlreadyExist); + } + else if (isTooLongOnFileSystem()) + { + status = + new Status(IStatus.ERROR, CodeUtilsActivator.PLUGIN_ID, + CodeUtilsNLS.ERR_NewBuildingBlocksWizardPage_FileNameTooLong); + } + } + + } + + return status; + } + + /* + * (non-Javadoc) + * + * @see org.eclipse.jdt.ui.wizards.NewTypeWizardPage#containerChanged() + */ + @Override + protected IStatus containerChanged() + { + IStatus superStatus = super.containerChanged(); + IStatus status = + new Status(superStatus.getSeverity(), CodeUtilsActivator.PLUGIN_ID, + superStatus.getMessage()); + + boolean hasNature = false; + + if (status.getCode() != IStatus.ERROR) + { + try + { + if ((getPackageFragmentRoot() != null) + && (getPackageFragmentRoot().getJavaProject() != null)) + { + hasNature = + getPackageFragmentRoot().getJavaProject().getProject() + .hasNature(IAndroidConstants.ANDROID_NATURE); + } + } + catch (CoreException ce) + { + StudioLogger.error(NewBuildingBlocksWizardPage.class, + "Error getting the project nature.", ce); //$NON-NLS-1$ + hasNature = false; + } + + if ((getPackageFragmentRoot() != null) && !hasNature) + { + status = + new Status(IStatus.ERROR, CodeUtilsActivator.PLUGIN_ID, + CodeUtilsNLS.ERR_NewBuildingBlocksWizardPage_SelectAnAndroidProject); + } + else if ((getPackageFragmentRoot() == null) + || (getPackageFragmentRoot().getResource().getType() == IResource.PROJECT) + || ((getPackageFragmentRoot().getElementType() & IPackageFragmentRoot.K_SOURCE) != IPackageFragmentRoot.K_SOURCE)) + { + status = + new Status( + IStatus.ERROR, + CodeUtilsActivator.PLUGIN_ID, + CodeUtilsNLS.ERR_NewBuildingBlocksWizardPage_SelectAValidSourceFolder); + } + else if (getPackageFragmentRoot().getElementName().equals( + IAndroidConstants.GEN_SRC_FOLDER) + && (getPackageFragmentRoot().getParent() instanceof IJavaProject)) + { + status = + new Status( + IStatus.ERROR, + CodeUtilsActivator.PLUGIN_ID, + CodeUtilsNLS.ERR_NewBuildingBlocksWizardPage_CannotUseTheGenFolderAsSourceFolder); + } + else if (isTooLongOnFileSystem()) + { + status = + new Status(IStatus.ERROR, CodeUtilsActivator.PLUGIN_ID, + CodeUtilsNLS.ERR_NewBuildingBlocksWizardPage_FileNameTooLong); + } + } + + return status; + } + + /** + * Checks for cross package/class collision among source folders + * + * @return true if there is any collision or false otherwise + */ + private boolean packageAndClassExist() + { + boolean exists = false; + + try + { + if ((getJavaProject() != null) && getJavaProject().isOpen()) + { + IPackageFragmentRoot[] roots = getJavaProject().getPackageFragmentRoots(); + + if (roots != null) + { + for (IPackageFragmentRoot root : roots) + { + if ((root.getKind() & IPackageFragmentRoot.K_SOURCE) == IPackageFragmentRoot.K_SOURCE) + { + IPackageFragment pack = root.getPackageFragment(getPackageText()); + + if ((pack != null) && pack.exists()) + { + IJavaElement classes[] = pack.getChildren(); + + if (classes != null) + { + for (IJavaElement clazz : classes) + { + if (clazz.getElementName().equals( + getTypeName() + JAVA_EXTENSION)) + { + exists = true; + break; + } + } + } + } + } + + if (exists) + { + break; + } + } + } + } + } + catch (JavaModelException e) + { + // Do nothing + StudioLogger.error(NewBuildingBlocksWizardPage.class, e.getLocalizedMessage(), e); + } + + return exists; + } + + /** + * Checks if the current building block that is being created can be written + * to the file system without throw a "file name too long" error + * + * @return true if the building block can be written or false otherwise + */ + private boolean isTooLongOnFileSystem() + { + boolean isTooLong = false; + + if (getPackageFragment() != null) + { + String javaFileName = + getPackageFragment().getCompilationUnit(getTypeName() + JAVA_EXTENSION) + .getResource().getLocation().toFile().getPath(); + + isTooLong = javaFileName.length() > MAX_PATH_SIZE; + } + + return isTooLong; + } + + /** + * Checks if the wizard can be opened. If the wizard cannot be opened, an + * error message is displayed. + * + * @return true if the wizard can be opened or false otherwise. + */ + private boolean canOpen() + { + boolean canOpen = true; + + if (getBuildBlock().getProject() != null) + { + IStatus status = null; + + try + { + AndroidManifestFile manifestFile = + AndroidProjectManifestFile.getFromProject(getBuildBlock().getProject()); + + if (manifestFile.hasErrors()) + { + status = + new MultiStatus( + CodeUtilsActivator.PLUGIN_ID, + IStatus.ERROR, + manifestFile.getErrors(), + CodeUtilsNLS.ERR_NewBuildingBlocksWizardPage_OneOrMoreErrorsWhenParsingManifest, + null); + } + } + catch (AndroidException e) + { + status = + new Status(IStatus.ERROR, CodeUtilsActivator.PLUGIN_ID, + e.getLocalizedMessage()); + } + catch (CoreException e) + { + status = e.getStatus(); + } + + if (status != null) + { + canOpen = false; + + EclipseUtils + .showErrorDialog( + CodeUtilsNLS.UI_GenericErrorDialogTitle, + CodeUtilsNLS.ERR_NewBuildingBlocksWizardPage_CannotProceedWithTheBuildingBlockCreation, + status); + } + } + + return canOpen; + } +} diff --git a/src/plugins/android.codeutils/src/com/motorola/studio/android/wizards/buildingblocks/NewLauncherWizardPage.java b/src/plugins/android.codeutils/src/com/motorola/studio/android/wizards/buildingblocks/NewLauncherWizardPage.java new file mode 100644 index 0000000..709b063 --- /dev/null +++ b/src/plugins/android.codeutils/src/com/motorola/studio/android/wizards/buildingblocks/NewLauncherWizardPage.java @@ -0,0 +1,344 @@ +/* + * Copyright (C) 2012 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.motorola.studio.android.wizards.buildingblocks; + +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; + +import org.eclipse.jface.dialogs.Dialog; +import org.eclipse.jface.dialogs.IInputValidator; +import org.eclipse.jface.dialogs.InputDialog; +import org.eclipse.swt.SWT; +import org.eclipse.swt.layout.GridData; +import org.eclipse.swt.layout.GridLayout; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Event; +import org.eclipse.swt.widgets.Group; +import org.eclipse.swt.widgets.Label; +import org.eclipse.swt.widgets.List; +import org.eclipse.swt.widgets.Listener; + +import com.motorola.studio.android.codeutils.i18n.CodeUtilsNLS; +import com.motorola.studio.android.common.exception.AndroidException; +import com.motorola.studio.android.common.log.StudioLogger; +import com.motorola.studio.android.common.utilities.AndroidUtils; +import com.motorola.studio.android.model.BuildingBlockModel; +import com.motorola.studio.android.model.Launcher; +import com.motorola.studio.android.wizards.elements.AddInputRemoveButtons; + +/** + * Abstract class to contribute with the Building Blocks Wizards. This class + * creates the controls to add Intent Filters and Categories to the + * Building Blocks + */ +public abstract class NewLauncherWizardPage extends NewBuildingBlocksWizardPage +{ + private List activityActions; + + private AddInputRemoveButtons addRemoveActionButtons; + + private AddInputRemoveButtons addRemoveCategoryButtons; + + private List activityCategories; + + private class InputDialogValidator implements IInputValidator + { + + /* (non-Javadoc) + * @see org.eclipse.jface.dialogs.IInputValidator#isValid(java.lang.String) + */ + public String isValid(String newText) + { + String message = null; + if (((newText == null) || (newText.length() == 0) || (newText.contains(" ")))) //$NON-NLS-1$ + { + message = CodeUtilsNLS.NewLauncherWizardPage_InputDialogValidationMessage; + } + return message; + } + } + + /** + * Default constructor + * + * @param buildBlock The building block model + * @param pageName The page name + */ + protected NewLauncherWizardPage(BuildingBlockModel buildBlock, String pageName) + { + super(buildBlock, pageName); + } + + /** + * Creates the "Action" section on the wizard + * + * @param composite the wizard composite + */ + protected void createActionsControls(Composite composite) + { + Label activityActionsLabel = new Label(composite, SWT.NONE); + activityActionsLabel.setText(CodeUtilsNLS.UI_NewLauncherWizardPage_ActionLabel); + GridData gridData = new GridData(); + gridData.widthHint = convertWidthInCharsToPixels(12); + gridData.verticalAlignment = GridData.BEGINNING; + activityActionsLabel.setLayoutData(gridData); + + activityActions = new List(composite, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL); + activityActions.setItems(getBuildBlock().getIntentFilterActionsAsArray()); + gridData = new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1); + gridData.heightHint = convertHeightInCharsToPixels(3); + activityActions.setLayoutData(gridData); + addRemoveActionButtons = new AddInputRemoveButtons(composite); + setButtonLayoutData(addRemoveActionButtons.getAddButton()); + setButtonLayoutData(addRemoveActionButtons.getInputButton()); + setButtonLayoutData(addRemoveActionButtons.getRemoveButton()); + + addRemoveActionButtons.getAddButton().addListener(SWT.Selection, new Listener() + { + public void handleEvent(Event arg0) + { + Set<String> categorySet = + new HashSet<String>(Arrays.asList(getIntentFiltersActions())); + + try + { + categorySet.removeAll(getBuildBlock().getIntentFilterActions()); + } + catch (Exception e) + { + StudioLogger.error(NewLauncherWizardPage.class, e.getLocalizedMessage(), e); + } + FilteredActionsSelectionDialog dialog = + new FilteredActionsSelectionDialog(getShell(), categorySet); + dialog.setInitialPattern("**"); + dialog.setTitle(CodeUtilsNLS.UI_NewLauncherWizardPage_ActionSelectionDialogTitle); + dialog.setMessage(CodeUtilsNLS.UI_NewLauncherWizardPage_ActionSelectionDialogMessage); + + if (Dialog.OK == dialog.open()) + { + for (Object result : dialog.getResult()) + { + getBuildBlock().addIntentFilterAction((String) result); + } + activityActions.setItems(getBuildBlock().getIntentFilterActionsAsArray()); + addRemoveActionButtons.getRemoveButton().setEnabled( + activityActions.getSelectionCount() > 0); + updateStatus(getBuildBlock().getStatus()); + } + } + + }); + addRemoveActionButtons.getInputButton().addListener(SWT.Selection, new Listener() + { + public void handleEvent(Event event) + { + InputDialog dialog = + new InputDialog(getShell(), + CodeUtilsNLS.NewLauncherWizardPage_ActionTypeDialogTitle, + CodeUtilsNLS.NewLauncherWizardPage_ActionTypeDialogMessage, null, + new InputDialogValidator()); + int result = dialog.open(); + if (result == InputDialog.OK) + { + String action = dialog.getValue(); + if (action != null) + { + getBuildBlock().addIntentFilterAction(action.trim()); + activityActions.setItems(getBuildBlock().getIntentFilterActionsAsArray()); + addRemoveActionButtons.getRemoveButton().setEnabled( + activityActions.getSelectionCount() > 0); + updateStatus(getBuildBlock().getStatus()); + } + } + } + }); + addRemoveActionButtons.getRemoveButton() + .setEnabled(activityActions.getSelectionCount() > 0); + addRemoveActionButtons.getRemoveButton().addListener(SWT.Selection, new Listener() + { + + public void handleEvent(Event arg0) + { + for (int selection : activityActions.getSelectionIndices()) + { + getBuildBlock().removeIntentFilterAction(activityActions.getItem(selection)); + } + activityActions.setItems(getBuildBlock().getIntentFilterActionsAsArray()); + addRemoveActionButtons.getRemoveButton().setEnabled( + activityActions.getSelectionCount() > 0); + updateStatus(getBuildBlock().getStatus()); + } + + }); + activityActions.addListener(SWT.Selection, new Listener() + { + public void handleEvent(Event arg0) + { + addRemoveActionButtons.getRemoveButton().setEnabled( + activityActions.getSelectionCount() > 0); + } + }); + } + + /** + * Creates the "Categories" section on the wizard + * + * @param composite the wizard composite + */ + protected void createCategoriesControls(Composite composite) + { + GridData gridData; + Label activitycategoriesLabel = new Label(composite, SWT.NONE); + activitycategoriesLabel.setText(CodeUtilsNLS.UI_NewLauncherWizardPage_CategoryLabel); + gridData = new GridData(); + gridData.verticalAlignment = GridData.BEGINNING; + activitycategoriesLabel.setLayoutData(gridData); + + activityCategories = new List(composite, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL); + activityCategories.setItems(getBuildBlock().getIntentFilterCategoriesAsArray()); + gridData = new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1); + gridData.heightHint = convertHeightInCharsToPixels(3); + activityCategories.setLayoutData(gridData); + + addRemoveCategoryButtons = new AddInputRemoveButtons(composite); + setButtonLayoutData(addRemoveCategoryButtons.getAddButton()); + setButtonLayoutData(addRemoveCategoryButtons.getInputButton()); + setButtonLayoutData(addRemoveCategoryButtons.getRemoveButton()); + + addRemoveCategoryButtons.getAddButton().addListener(SWT.Selection, new Listener() + { + public void handleEvent(Event arg0) + { + Set<String> categorySet = new HashSet<String>(0); + try + { + categorySet = + new HashSet<String>(Arrays.asList(AndroidUtils + .getIntentFilterCategories(getBuildBlock().getProject()))); + } + catch (AndroidException e) + { + setErrorMessage(e.getMessage()); + } + + categorySet.removeAll(getBuildBlock().getIntentFilterCategories()); + + FilteredActionsSelectionDialog dialog = + new FilteredActionsSelectionDialog(getShell(), categorySet); + dialog.setInitialPattern("**"); + dialog.setTitle(CodeUtilsNLS.UI_NewLauncherWizardPage_CategorySelectionDialogTitle); + dialog.setMessage(CodeUtilsNLS.UI_NewLauncherWizardPage_CategorySelectionDialogMessage); + + if (Dialog.OK == dialog.open()) + { + for (Object result : dialog.getResult()) + { + getBuildBlock().addIntentFilterCategories((String) result); + } + activityCategories.setItems(getBuildBlock().getIntentFilterCategoriesAsArray()); + addRemoveCategoryButtons.getRemoveButton().setEnabled( + activityCategories.getSelectionCount() > 0); + updateStatus(getBuildBlock().getStatus()); + } + } + }); + addRemoveCategoryButtons.getInputButton().addListener(SWT.Selection, new Listener() + { + public void handleEvent(Event event) + { + InputDialog dialog = + new InputDialog(getShell(), + CodeUtilsNLS.NewLauncherWizardPage_CategoryTypeDialogTitle, + CodeUtilsNLS.NewLauncherWizardPage_CategoryTypeDialogMessage, null, + new InputDialogValidator()); + int result = dialog.open(); + if (result == InputDialog.OK) + { + String action = dialog.getValue(); + if (action != null) + { + getBuildBlock().addIntentFilterCategories(action.trim()); + activityCategories.setItems(getBuildBlock() + .getIntentFilterCategoriesAsArray()); + addRemoveCategoryButtons.getRemoveButton().setEnabled( + activityCategories.getSelectionCount() > 0); + updateStatus(getBuildBlock().getStatus()); + } + } + } + }); + addRemoveCategoryButtons.getRemoveButton().addListener(SWT.Selection, new Listener() + { + public void handleEvent(Event arg0) + { + for (int selection : activityCategories.getSelectionIndices()) + { + getBuildBlock().removeIntentFilterCategories( + activityCategories.getItem(selection)); + } + activityCategories.setItems(getBuildBlock().getIntentFilterCategoriesAsArray()); + addRemoveCategoryButtons.getRemoveButton().setEnabled( + activityCategories.getSelectionCount() > 0); + updateStatus(getBuildBlock().getStatus()); + } + }); + addRemoveCategoryButtons.getRemoveButton().setEnabled( + activityCategories.getSelectionCount() > 0); + activityCategories.addListener(SWT.Selection, new Listener() + { + public void handleEvent(Event arg0) + { + addRemoveCategoryButtons.getRemoveButton().setEnabled( + activityCategories.getSelectionCount() > 0); + } + }); + } + + /** + * Creates the Intent Filter group on the wizard page + * + * @param parent The wizard composite + */ + protected void createIntentFilterControls(Composite parent) + { + Group intentFilterGroup = new Group(parent, SWT.NONE); + intentFilterGroup.setText(CodeUtilsNLS.UI_NewLauncherWizardPage_IntentFilterGroupName); + intentFilterGroup.setLayout(new GridLayout(4, false)); + intentFilterGroup.setLayoutData(new GridData(GridData.FILL, GridData.FILL, false, false, 4, + 1)); + + createActionsControls(intentFilterGroup); + createCategoriesControls(intentFilterGroup); + } + + /* + * (non-Javadoc) + * @see com.motorola.studio.android.wizards.buildingblocks.NewBuildingBlocksWizardPage#getBuildBlock() + */ + @Override + public Launcher getBuildBlock() + { + return (Launcher) super.getBuildBlock(); + } + + /** + * Return all intent filters actions according with the Building Block + * @return + */ + protected abstract String[] getIntentFiltersActions(); + +} diff --git a/src/plugins/android.codeutils/src/com/motorola/studio/android/wizards/buildingblocks/NewProviderMainPage.java b/src/plugins/android.codeutils/src/com/motorola/studio/android/wizards/buildingblocks/NewProviderMainPage.java new file mode 100644 index 0000000..2eb8782 --- /dev/null +++ b/src/plugins/android.codeutils/src/com/motorola/studio/android/wizards/buildingblocks/NewProviderMainPage.java @@ -0,0 +1,345 @@ +/* + * Copyright (C) 2012 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.motorola.studio.android.wizards.buildingblocks; + +import java.util.Arrays; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Status; +import org.eclipse.jdt.core.JavaConventions; +import org.eclipse.jdt.core.JavaCore; +import org.eclipse.jface.dialogs.Dialog; +import org.eclipse.jface.dialogs.IInputValidator; +import org.eclipse.jface.dialogs.InputDialog; +import org.eclipse.swt.SWT; +import org.eclipse.swt.layout.GridData; +import org.eclipse.swt.widgets.Button; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Event; +import org.eclipse.swt.widgets.Label; +import org.eclipse.swt.widgets.List; +import org.eclipse.swt.widgets.Listener; + +import com.motorola.studio.android.codeutils.CodeUtilsActivator; +import com.motorola.studio.android.codeutils.i18n.CodeUtilsNLS; +import com.motorola.studio.android.model.ContentProvider; +import com.motorola.studio.android.wizards.elements.InputRemoveButtons; + +/** + * Class that implements the Content Provider Wizard Main Page. + */ +public class NewProviderMainPage extends NewBuildingBlocksWizardPage +{ + private List authoritiesList; + + private Button useDefault; + + private static final String NEW_PROVIDER_HELP = CodeUtilsActivator.PLUGIN_ID + ".newcontprov"; + + protected int defaultIndex = -1; + + /* + * Private class that implements an Input Validator for the Input Button. + */ + private class InputButtonValidator implements IInputValidator + { + /* (non-Javadoc) + * @see org.eclipse.jface.dialogs.IInputValidator#isValid(java.lang.String) + */ + public String isValid(String newText) + { + IStatus status = + JavaConventions.validatePackageName(newText, JavaCore.VERSION_1_7, + JavaCore.VERSION_1_7); + if (status.isOK() && getBuildBlock().getAuthoritiesList().contains(newText)) + { + status = + new Status(IStatus.ERROR, CodeUtilsActivator.PLUGIN_ID, + CodeUtilsNLS.ERR_NewProviderMainPage_ErrorMessageAlreadyExists); + } + + if (status.isOK()) + { + Pattern pattern = Pattern.compile("[a-z0-9\\._]+"); + Matcher matcher = pattern.matcher(newText); + + if (!matcher.matches()) + { + status = + new Status( + IStatus.ERROR, + CodeUtilsActivator.PLUGIN_ID, + CodeUtilsNLS.ERR_NewProviderMainPage_InvalidCharactersInAuthority); + } + } + + if (status.isOK() && newText.equalsIgnoreCase(getBuildBlock().getDefaultAuthority())) + { + status = + new Status(IStatus.ERROR, CodeUtilsActivator.PLUGIN_ID, + CodeUtilsNLS.ERR_NewProviderMainPage_ErrorMessageDefaultName); + } + return status.isOK() ? null : status.getMessage(); + } + } + + /* + * Private class that implements a Listener for the Add Button. + */ + private class InputButtonListener implements Listener + { + /* (non-Javadoc) + * @see org.eclipse.swt.widgets.Listener#handleEvent(org.eclipse.swt.widgets.Event) + */ + public void handleEvent(Event arg0) + { + InputDialog dialog = + new InputDialog(getShell(), + CodeUtilsNLS.UI_NewProviderMainPage_TitleNewAuthority, + CodeUtilsNLS.UI_NewProviderMainPage_MessageAvoidConflicts, + "", new InputButtonValidator()); //$NON-NLS-1$ + if (dialog.open() == Dialog.OK) + { + String authority = dialog.getValue(); + authoritiesList.add(authority); + getBuildBlock().addAuthority(authority); + } + updateStatus(getBuildBlock().getStatus()); + } + } + + /* + * Private class that implements a Listener for the Remove Button. + */ + private class RemoveButtonListener implements Listener + { + /* (non-Javadoc) + * @see org.eclipse.swt.widgets.Listener#handleEvent(org.eclipse.swt.widgets.Event) + */ + public void handleEvent(Event arg0) + { + int selectionIndex = authoritiesList.getSelectionIndex(); + if (selectionIndex != defaultIndex) + { + String authoritySelected = authoritiesList.getItem(selectionIndex); + authoritiesList.remove(selectionIndex); + getBuildBlock().removeAuthority(authoritySelected); + updateStatus(getBuildBlock().getStatus()); + } + } + } + + /* + * Private class that implements a Listener for the Use Default Button. + */ + private class UseDefaultButtonListener implements Listener + { + /* (non-Javadoc) + * @see org.eclipse.swt.widgets.Listener#handleEvent(org.eclipse.swt.widgets.Event) + */ + public void handleEvent(Event arg0) + { + java.util.List<String> asList = Arrays.asList(authoritiesList.getItems()); + if (useDefault.getSelection() + && !asList.contains(getBuildBlock().getDefaultAuthority())) + { + getBuildBlock().addAuthority(getBuildBlock().getDefaultAuthority()); + authoritiesList.add(getBuildBlock().getDefaultAuthority(), 0); + defaultIndex = authoritiesList.indexOf(getBuildBlock().getDefaultAuthority()); + } + else if (!useDefault.getSelection()) + { + getBuildBlock().removeAuthority(getBuildBlock().getDefaultAuthority()); + authoritiesList.remove(getBuildBlock().getDefaultAuthority()); + defaultIndex = -1; + } + updateStatus(getBuildBlock().getStatus()); + } + } + + /* + * Private class that implements a Listener for the Authorities Listener + */ + private class AuthoritiesListListener implements Listener + { + InputRemoveButtons inputRemoveButtons; + + /** + * Default constructor + * + * @param addRemoveButtons The AddRemoveButtons composite + */ + public AuthoritiesListListener(InputRemoveButtons addRemoveButtons) + { + this.inputRemoveButtons = addRemoveButtons; + } + + /* (non-Javadoc) + * @see org.eclipse.swt.widgets.Listener#handleEvent(org.eclipse.swt.widgets.Event) + */ + public void handleEvent(Event arg0) + { + int selectionCount = authoritiesList.getSelectionCount(); + boolean enabled = selectionCount > 0; + if (selectionCount == 1) + { + enabled = + !getBuildBlock().getDefaultAuthority().equals( + authoritiesList.getItem(authoritiesList.getSelectionIndex())); + } + inputRemoveButtons.getRemoveButton().setEnabled(enabled); + updateStatus(getBuildBlock().getStatus()); + } + } + + /** + * Default constructor. + * + * @param buildBlock The content provider building block model. + */ + protected NewProviderMainPage(ContentProvider buildBlock) + { + super(buildBlock, CodeUtilsNLS.UI_NewProviderMainPage_WizardTitle); + } + + /* (non-Javadoc) + * @see com.motorola.studio.android.wizards.buildingblocks.NewBuildingBlocksWizardPage#getBuildBlock() + */ + @Override + public ContentProvider getBuildBlock() + { + return (ContentProvider) super.getBuildBlock(); + } + + /* (non-Javadoc) + * @see com.motorola.studio.android.wizards.buildingblocks.NewBuildingBlocksWizardPage#createIntermediateControls(org.eclipse.swt.widgets.Composite) + */ + @Override + protected void createIntermediateControls(Composite parent) + { + + Label authoritiesLabel = new Label(parent, SWT.NONE); + GridData gridData = new GridData(); + gridData.verticalAlignment = GridData.BEGINNING; + authoritiesLabel.setLayoutData(gridData); + authoritiesLabel.setText(CodeUtilsNLS.UI_NewProviderMainPage_LabelAuthorities); + authoritiesList = new List(parent, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL); + gridData = new GridData(SWT.FILL, SWT.FILL, false, false, 2, 1); + gridData.heightHint = convertHeightInCharsToPixels(3); + authoritiesList.setLayoutData(gridData); + + final InputRemoveButtons inputRemoveButtons = new InputRemoveButtons(parent); + setButtonLayoutData(inputRemoveButtons.getInputButton()); + setButtonLayoutData(inputRemoveButtons.getRemoveButton()); + inputRemoveButtons.getRemoveButton().setEnabled(false); + inputRemoveButtons.getInputButton().addListener(SWT.Selection, new InputButtonListener()); + inputRemoveButtons.getRemoveButton().addListener(SWT.Selection, new RemoveButtonListener()); + new Label(parent, SWT.NONE); + useDefault = new Button(parent, SWT.CHECK); + useDefault.setText(CodeUtilsNLS.UI_NewProviderMainPage_OptionUseDefault); + gridData = new GridData(); + gridData.horizontalSpan = 3; + useDefault.setLayoutData(gridData); + useDefault.setSelection(true); + + String defaultAuthority = getBuildBlock().getDefaultAuthority(); + authoritiesList.add(defaultAuthority); + defaultIndex = 0; + getBuildBlock().addAuthority(defaultAuthority); + + useDefault.addListener(SWT.Selection, new UseDefaultButtonListener()); + authoritiesList.addListener(SWT.Selection, new AuthoritiesListListener(inputRemoveButtons)); + + } + + /* (non-Javadoc) + * @see com.motorola.studio.android.wizards.buildingblocks.NewBuildingBlocksWizardPage#typeNameChanged() + */ + @Override + protected IStatus typeNameChanged() + { + if (authoritiesList != null) + { + String defaultAuthority = getBuildBlock().getDefaultAuthority(); + if (defaultIndex != -1) + { + getBuildBlock().removeAuthority(authoritiesList.getItem(defaultIndex)); + if ((defaultAuthority != null) && (defaultAuthority.length() > 0)) + { + + if (!getBuildBlock().containsAuthority(defaultAuthority)) + { + authoritiesList.setItem(defaultIndex, defaultAuthority); + getBuildBlock().addAuthority(defaultAuthority); + } + else + { + authoritiesList.remove(defaultIndex); + defaultIndex = -1; + } + } + else + { + authoritiesList.remove(defaultIndex); + defaultIndex = -1; + } + } + } + return super.typeNameChanged(); + } + + /* (non-Javadoc) + * @see com.motorola.studio.android.wizards.buildingblocks.NewBuildingBlocksWizardPage#getMethods() + */ + @Override + protected Method[] getMethods() + { + return new Method[0]; + } + + /* (non-Javadoc) + * @see com.motorola.studio.android.wizards.buildingblocks.NewBuildingBlocksWizardPage#getDefaultMessage() + */ + @Override + public String getDefaultMessage() + { + return CodeUtilsNLS.UI_NewProviderMainPage_SubtitleCreateContentProvider; + } + + /* (non-Javadoc) + * @see com.motorola.studio.android.wizards.buildingblocks.NewBuildingBlocksWizardPage#getWizardTitle() + */ + @Override + public String getWizardTitle() + { + return CodeUtilsNLS.UI_NewProviderMainPage_TitleContentProvider; + } + + /** + * Gets the help ID to be used for attaching + * context sensitive help. + * + * Classes that extends this class and want to set + * their on help should override this method + */ + @Override + protected String getHelpId() + { + return NEW_PROVIDER_HELP; + } +} diff --git a/src/plugins/android.codeutils/src/com/motorola/studio/android/wizards/buildingblocks/NewProviderWizard.java b/src/plugins/android.codeutils/src/com/motorola/studio/android/wizards/buildingblocks/NewProviderWizard.java new file mode 100644 index 0000000..7f83b81 --- /dev/null +++ b/src/plugins/android.codeutils/src/com/motorola/studio/android/wizards/buildingblocks/NewProviderWizard.java @@ -0,0 +1,191 @@ +/* + * Copyright (C) 2012 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.motorola.studio.android.wizards.buildingblocks; + +import java.lang.reflect.InvocationTargetException; + +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Status; +import org.eclipse.jdt.core.ICompilationUnit; +import org.eclipse.jdt.core.JavaModelException; +import org.eclipse.jdt.ui.JavaUI; +import org.eclipse.jface.operation.IRunnableWithProgress; +import org.eclipse.jface.viewers.IStructuredSelection; +import org.eclipse.ui.IWorkbench; +import org.eclipse.ui.PartInitException; + +import com.motorola.studio.android.codeutils.CodeUtilsActivator; +import com.motorola.studio.android.codeutils.i18n.CodeUtilsNLS; +import com.motorola.studio.android.common.exception.AndroidException; +import com.motorola.studio.android.common.log.StudioLogger; +import com.motorola.studio.android.common.utilities.EclipseUtils; +import com.motorola.studio.android.model.BuildingBlockModel; +import com.motorola.studio.android.model.ContentProvider; + +/** + * Class that implements the Content Provider Wizard. + */ +public class NewProviderWizard extends NewBuildingBlocksWizard +{ + private static final String WIZBAN_ICON = "icons/wizban/new_provider_wiz.png"; + + private ContentProvider contentProvider = new ContentProvider(); + + /* (non-Javadoc) + * @see org.eclipse.jface.wizard.Wizard#performFinish() + */ + @Override + public boolean performFinish() + { + boolean saved = false; + + try + { + DoSave doSave = new DoSave(); + + getContainer().run(false, false, doSave); + + if (doSave.exception != null) + { + throw doSave.exception; + } + else + { + saved = doSave.saved; + } + } + catch (AndroidException e) + { + IStatus status = + new Status(IStatus.ERROR, CodeUtilsActivator.PLUGIN_ID, e.getLocalizedMessage()); + EclipseUtils.showErrorDialog(CodeUtilsNLS.UI_GenericErrorDialogTitle, + CodeUtilsNLS.ERR_BuildingBlockCreation_ErrorMessage, status); + } + catch (InvocationTargetException e) + { + IStatus status = + new Status(IStatus.ERROR, CodeUtilsActivator.PLUGIN_ID, e.getLocalizedMessage()); + EclipseUtils.showErrorDialog(CodeUtilsNLS.UI_GenericErrorDialogTitle, + CodeUtilsNLS.ERR_BuildingBlockCreation_ErrorMessage, status); + } + catch (InterruptedException e) + { + IStatus status = + new Status(IStatus.ERROR, CodeUtilsActivator.PLUGIN_ID, e.getLocalizedMessage()); + EclipseUtils.showErrorDialog(CodeUtilsNLS.UI_GenericErrorDialogTitle, + CodeUtilsNLS.ERR_BuildingBlockCreation_ErrorMessage, status); + } + + if (saved) + { + ICompilationUnit javaFile = + getBuildingBlock().getPackageFragment().getCompilationUnit( + getBuildingBlock().getName() + ".java"); + + if ((javaFile != null) && javaFile.exists()) + { + try + { + JavaUI.openInEditor(javaFile); + } + catch (PartInitException e) + { + // Do nothing + StudioLogger.error(NewProviderWizard.class, + "Could not open the content provider " + getBuildingBlock().getName() + + " on an editor.", e); + } + catch (JavaModelException e) + { + // Do nothing + StudioLogger.error(NewProviderWizard.class, + "Could not open the content provider " + getBuildingBlock().getName() + + " on an editor.", e); + } + } + } + + if (saved) + { + // Collecting usage data for statistical purposes + try + { + StudioLogger.collectUsageData(StudioLogger.WHAT_BUILDINGBLOCK_PROVIDER, + StudioLogger.KIND_BUILDINGBLOCK, StudioLogger.DESCRIPTION_DEFAULT, + CodeUtilsActivator.PLUGIN_ID, CodeUtilsActivator.getDefault().getBundle() + .getVersion().toString()); + } + catch (Throwable e) + { + //Do nothing, but error on the log should never prevent app from working + } + } + return saved; + } + + /* (non-Javadoc) + * @see org.eclipse.ui.IWorkbenchWizard#init(org.eclipse.ui.IWorkbench, org.eclipse.jface.viewers.IStructuredSelection) + */ + public void init(IWorkbench arg0, IStructuredSelection selection) + { + setWindowTitle(CodeUtilsNLS.UI_NewProviderWizard_WizardTitle); + setNeedsProgressMonitor(true); + setDefaultPageImageDescriptor(CodeUtilsActivator.getImageDescriptor(WIZBAN_ICON)); + contentProvider.configure(selection); + } + + /* (non-Javadoc) + * @see org.eclipse.jface.wizard.Wizard#addPages() + */ + @Override + public void addPages() + { + addPage(new NewProviderMainPage(contentProvider)); + } + + /* (non-Javadoc) + * @see com.motorola.studio.android.wizards.buildingblocks.NewBuildingBlocksWizard#getBuildingBlock() + */ + @Override + protected BuildingBlockModel getBuildingBlock() + { + return contentProvider; + } + + /* + * IRunnableWithProgress object to create the content provider + */ + private class DoSave implements IRunnableWithProgress + { + AndroidException exception = null; + + boolean saved = false; + + public void run(IProgressMonitor monitor) throws InvocationTargetException, + InterruptedException + { + try + { + saved = getBuildingBlock().save(getContainer(), monitor); + } + catch (AndroidException e) + { + exception = e; + } + } + } +} diff --git a/src/plugins/android.codeutils/src/com/motorola/studio/android/wizards/buildingblocks/NewReceiverMainPage.java b/src/plugins/android.codeutils/src/com/motorola/studio/android/wizards/buildingblocks/NewReceiverMainPage.java new file mode 100644 index 0000000..3fae001 --- /dev/null +++ b/src/plugins/android.codeutils/src/com/motorola/studio/android/wizards/buildingblocks/NewReceiverMainPage.java @@ -0,0 +1,119 @@ +/* + * Copyright (C) 2012 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.motorola.studio.android.wizards.buildingblocks; + +import org.eclipse.swt.widgets.Composite; + +import com.motorola.studio.android.codeutils.CodeUtilsActivator; +import com.motorola.studio.android.codeutils.i18n.CodeUtilsNLS; +import com.motorola.studio.android.common.exception.AndroidException; +import com.motorola.studio.android.common.utilities.AndroidUtils; +import com.motorola.studio.android.model.Receiver; + +/** + * Class that implements the Broadcast Receiver Wizard Main Page + */ +public class NewReceiverMainPage extends NewLauncherWizardPage +{ + + private static final String NEW_RECEIVER_HELP = CodeUtilsActivator.PLUGIN_ID + ".newbcastrcvr"; + + /** + * Default constructor + * + * @param buildBlock The broadcast receiver model + */ + protected NewReceiverMainPage(Receiver buildBlock) + { + super(buildBlock, CodeUtilsNLS.UI_NewReceiverMainPage_PageTitle); + } + + /* (non-Javadoc) + * @see com.motorola.studio.android.wizards.buildingblocks.NewBuildingBlocksWizardPage#createIntermediateControls(org.eclipse.swt.widgets.Composite) + */ + @Override + protected void createIntermediateControls(Composite parent) + { + createIntentFilterControls(parent); + } + + /* (non-Javadoc) + * @see com.motorola.studio.android.wizards.buildingblocks.NewLauncherWizardPage#getBuildBlock() + */ + @Override + public Receiver getBuildBlock() + { + return (Receiver) super.getBuildBlock(); + } + + /* (non-Javadoc) + * @see com.motorola.studio.android.wizards.buildingblocks.NewBuildingBlocksWizardPage#getMethods() + */ + @Override + protected Method[] getMethods() + { + return new Method[0]; + } + + /* (non-Javadoc) + * @see com.motorola.studio.android.wizards.buildingblocks.NewBuildingBlocksWizardPage#getDefaultMessage() + */ + @Override + public String getDefaultMessage() + { + return CodeUtilsNLS.UI_NewReceiverMainPage_DefaultWizardDescription; + } + + /* (non-Javadoc) + * @see com.motorola.studio.android.wizards.buildingblocks.NewBuildingBlocksWizardPage#getWizardTitle() + */ + @Override + public String getWizardTitle() + { + return CodeUtilsNLS.UI_NewReceiverMainPage_WizardTitle; + } + + /* (non-Javadoc) + * @see com.motorola.studio.android.wizards.buildingblocks.NewLauncherWizardPage#getIntentFiltersActions() + */ + @Override + protected String[] getIntentFiltersActions() + { + String[] receiverActions = new String[0]; + try + { + receiverActions = AndroidUtils.getReceiverActions(getBuildBlock().getProject()); + } + catch (AndroidException e) + { + setErrorMessage(e.getMessage()); + } + return receiverActions; + } + + /** + * Gets the help ID to be used for attaching + * context sensitive help. + * + * Classes that extends this class and want to set + * their on help should override this method + */ + @Override + protected String getHelpId() + { + return NEW_RECEIVER_HELP; + } +} diff --git a/src/plugins/android.codeutils/src/com/motorola/studio/android/wizards/buildingblocks/NewReceiverWizard.java b/src/plugins/android.codeutils/src/com/motorola/studio/android/wizards/buildingblocks/NewReceiverWizard.java new file mode 100644 index 0000000..64fd47c --- /dev/null +++ b/src/plugins/android.codeutils/src/com/motorola/studio/android/wizards/buildingblocks/NewReceiverWizard.java @@ -0,0 +1,192 @@ +/* + * Copyright (C) 2012 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.motorola.studio.android.wizards.buildingblocks; + +import java.lang.reflect.InvocationTargetException; + +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Status; +import org.eclipse.jdt.core.ICompilationUnit; +import org.eclipse.jdt.core.JavaModelException; +import org.eclipse.jdt.ui.JavaUI; +import org.eclipse.jface.operation.IRunnableWithProgress; +import org.eclipse.jface.viewers.IStructuredSelection; +import org.eclipse.ui.IWorkbench; +import org.eclipse.ui.PartInitException; + +import com.motorola.studio.android.codeutils.CodeUtilsActivator; +import com.motorola.studio.android.codeutils.i18n.CodeUtilsNLS; +import com.motorola.studio.android.common.exception.AndroidException; +import com.motorola.studio.android.common.log.StudioLogger; +import com.motorola.studio.android.common.utilities.EclipseUtils; +import com.motorola.studio.android.model.BuildingBlockModel; +import com.motorola.studio.android.model.Receiver; + +/** + * Class that implements the Broadcast Receiver Wizard. + */ +public class NewReceiverWizard extends NewBuildingBlocksWizard +{ + private static final String WIZ_BANNER_ICON = "icons/wizban/new_receiver_wiz.png"; + + private Receiver receiver = new Receiver(); + + /* (non-Javadoc) + * @see org.eclipse.jface.wizard.Wizard#performFinish() + */ + @Override + public boolean performFinish() + { + boolean saved = false; + + try + { + DoSave doSave = new DoSave(); + + getContainer().run(false, false, doSave); + + if (doSave.exception != null) + { + throw doSave.exception; + } + else + { + saved = doSave.saved; + } + } + catch (AndroidException e) + { + IStatus status = + new Status(IStatus.ERROR, CodeUtilsActivator.PLUGIN_ID, e.getLocalizedMessage()); + EclipseUtils.showErrorDialog(CodeUtilsNLS.UI_GenericErrorDialogTitle, + CodeUtilsNLS.ERR_BuildingBlockCreation_ErrorMessage, status); + } + catch (InvocationTargetException e) + { + IStatus status = + new Status(IStatus.ERROR, CodeUtilsActivator.PLUGIN_ID, e.getLocalizedMessage()); + EclipseUtils.showErrorDialog(CodeUtilsNLS.UI_GenericErrorDialogTitle, + CodeUtilsNLS.ERR_BuildingBlockCreation_ErrorMessage, status); + } + catch (InterruptedException e) + { + IStatus status = + new Status(IStatus.ERROR, CodeUtilsActivator.PLUGIN_ID, e.getLocalizedMessage()); + EclipseUtils.showErrorDialog(CodeUtilsNLS.UI_GenericErrorDialogTitle, + CodeUtilsNLS.ERR_BuildingBlockCreation_ErrorMessage, status); + } + + if (saved) + { + ICompilationUnit javaFile = + getBuildingBlock().getPackageFragment().getCompilationUnit( + getBuildingBlock().getName() + ".java"); + + if ((javaFile != null) && javaFile.exists()) + { + try + { + JavaUI.openInEditor(javaFile); + } + catch (PartInitException e) + { + // Do nothing + StudioLogger.error(NewReceiverWizard.class, + "Could not open the broadcast receiver " + getBuildingBlock().getName() + + " on an editor.", e); + } + catch (JavaModelException e) + { + // Do nothing + StudioLogger.error(NewReceiverWizard.class, + "Could not open the broadcast receiver " + getBuildingBlock().getName() + + " on an editor.", e); + } + } + } + + if (saved) + { + // Collecting usage data for statistical purposes + try + { + StudioLogger.collectUsageData(StudioLogger.WHAT_BUILDINGBLOCK_RECEIVER, + StudioLogger.KIND_BUILDINGBLOCK, StudioLogger.DESCRIPTION_DEFAULT, + CodeUtilsActivator.PLUGIN_ID, CodeUtilsActivator.getDefault().getBundle() + .getVersion().toString()); + } + catch (Throwable e) + { + //Do nothing, but error on the log should never prevent app from working + } + } + + return saved; + } + + /* (non-Javadoc) + * @see org.eclipse.ui.IWorkbenchWizard#init(org.eclipse.ui.IWorkbench, org.eclipse.jface.viewers.IStructuredSelection) + */ + public void init(IWorkbench arg0, IStructuredSelection selection) + { + setWindowTitle(CodeUtilsNLS.UI_NewReceiverWizard_WizardTitle); + setNeedsProgressMonitor(true); + setDefaultPageImageDescriptor(CodeUtilsActivator.getImageDescriptor(WIZ_BANNER_ICON)); + receiver.configure(selection); + } + + /* (non-Javadoc) + * @see org.eclipse.jface.wizard.Wizard#addPages() + */ + @Override + public void addPages() + { + addPage(new NewReceiverMainPage(receiver)); + } + + /* (non-Javadoc) + * @see com.motorola.studio.android.wizards.buildingblocks.NewBuildingBlocksWizard#getBuildingBlock() + */ + @Override + protected BuildingBlockModel getBuildingBlock() + { + return receiver; + } + + /* + * IRunnableWithProgress object to create the broadcast receiver + */ + private class DoSave implements IRunnableWithProgress + { + AndroidException exception = null; + + boolean saved = false; + + public void run(IProgressMonitor monitor) throws InvocationTargetException, + InterruptedException + { + try + { + saved = getBuildingBlock().save(getContainer(), monitor); + } + catch (AndroidException e) + { + exception = e; + } + } + } +} diff --git a/src/plugins/android.codeutils/src/com/motorola/studio/android/wizards/buildingblocks/NewServiceMainPage.java b/src/plugins/android.codeutils/src/com/motorola/studio/android/wizards/buildingblocks/NewServiceMainPage.java new file mode 100644 index 0000000..7a63913 --- /dev/null +++ b/src/plugins/android.codeutils/src/com/motorola/studio/android/wizards/buildingblocks/NewServiceMainPage.java @@ -0,0 +1,110 @@ +/* + * Copyright (C) 2012 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.motorola.studio.android.wizards.buildingblocks; + +import com.motorola.studio.android.codeutils.CodeUtilsActivator; +import com.motorola.studio.android.codeutils.i18n.CodeUtilsNLS; +import com.motorola.studio.android.model.Service; + +/** + * Class that implements the Service Wizard Main Page. + */ +public class NewServiceMainPage extends NewBuildingBlocksWizardPage +{ + + private static final String NEW_SERVICE_HELP = CodeUtilsActivator.PLUGIN_ID + ".newservice"; + + /** + * Default constructor + * + * @param service The service wizard model + */ + public NewServiceMainPage(Service service) + { + super(service, CodeUtilsNLS.UI_NewServiceMainPage_WizardTitle); + } + + /* (non-Javadoc) + * @see com.motorola.studio.android.wizards.buildingblocks.NewBuildingBlocksWizardPage#getBuildBlock() + */ + @Override + public Service getBuildBlock() + { + return (Service) super.getBuildBlock(); + } + + /* (non-Javadoc) + * @see com.motorola.studio.android.wizards.buildingblocks.NewBuildingBlocksWizardPage#getMethods() + */ + @Override + protected Method[] getMethods() + { + Method onCreateMethod = new Method(getBuildBlock().getOnCreateMessage()) + { + @Override + public void handle(boolean selection) + { + getBuildBlock().setOnCreateMethod(selection); + } + }; + + Method onStartMethod = new Method(getBuildBlock().getOnStartMessage()) + { + @Override + public void handle(boolean selection) + { + getBuildBlock().setOnStartMethod(selection); + } + }; + + Method[] methods = new Method[] + { + onCreateMethod, onStartMethod + }; + return methods; + } + + /* (non-Javadoc) + * @see com.motorola.studio.android.wizards.buildingblocks.NewBuildingBlocksWizardPage#getDefaultMessage() + */ + @Override + public String getDefaultMessage() + { + return CodeUtilsNLS.UI_NewServiceMainPage_SubtitleCreateService; + } + + /* (non-Javadoc) + * @see com.motorola.studio.android.wizards.buildingblocks.NewBuildingBlocksWizardPage#getWizardTitle() + */ + @Override + public String getWizardTitle() + { + return CodeUtilsNLS.UI_NewServiceMainPage_TitleService; + } + + /** + * Gets the help ID to be used for attaching + * context sensitive help. + * + * Classes that extends this class and want to set + * their on help should override this method + */ + @Override + protected String getHelpId() + { + return NEW_SERVICE_HELP; + } +} diff --git a/src/plugins/android.codeutils/src/com/motorola/studio/android/wizards/buildingblocks/NewServiceWizard.java b/src/plugins/android.codeutils/src/com/motorola/studio/android/wizards/buildingblocks/NewServiceWizard.java new file mode 100644 index 0000000..c626633 --- /dev/null +++ b/src/plugins/android.codeutils/src/com/motorola/studio/android/wizards/buildingblocks/NewServiceWizard.java @@ -0,0 +1,190 @@ +/* + * Copyright (C) 2012 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.motorola.studio.android.wizards.buildingblocks; + +import java.lang.reflect.InvocationTargetException; + +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Status; +import org.eclipse.jdt.core.ICompilationUnit; +import org.eclipse.jdt.core.JavaModelException; +import org.eclipse.jdt.ui.JavaUI; +import org.eclipse.jface.operation.IRunnableWithProgress; +import org.eclipse.jface.viewers.IStructuredSelection; +import org.eclipse.ui.IWorkbench; +import org.eclipse.ui.PartInitException; + +import com.motorola.studio.android.codeutils.CodeUtilsActivator; +import com.motorola.studio.android.codeutils.i18n.CodeUtilsNLS; +import com.motorola.studio.android.common.exception.AndroidException; +import com.motorola.studio.android.common.log.StudioLogger; +import com.motorola.studio.android.common.utilities.EclipseUtils; +import com.motorola.studio.android.model.BuildingBlockModel; +import com.motorola.studio.android.model.Service; + +/** + * Class that implements the Service Wizard. + */ +public class NewServiceWizard extends NewBuildingBlocksWizard +{ + private static final String WIZBAN_ICON = "icons/wizban/new_service_wiz.png"; + + private Service service = new Service(); + + /* (non-Javadoc) + * @see org.eclipse.jface.wizard.Wizard#performFinish() + */ + @Override + public boolean performFinish() + { + boolean saved = false; + + try + { + DoSave doSave = new DoSave(); + + getContainer().run(false, false, doSave); + + if (doSave.exception != null) + { + throw doSave.exception; + } + else + { + saved = doSave.saved; + } + } + catch (AndroidException e) + { + IStatus status = + new Status(IStatus.ERROR, CodeUtilsActivator.PLUGIN_ID, e.getLocalizedMessage()); + EclipseUtils.showErrorDialog(CodeUtilsNLS.UI_GenericErrorDialogTitle, + CodeUtilsNLS.ERR_BuildingBlockCreation_ErrorMessage, status); + } + catch (InvocationTargetException e) + { + IStatus status = + new Status(IStatus.ERROR, CodeUtilsActivator.PLUGIN_ID, e.getLocalizedMessage()); + EclipseUtils.showErrorDialog(CodeUtilsNLS.UI_GenericErrorDialogTitle, + CodeUtilsNLS.ERR_BuildingBlockCreation_ErrorMessage, status); + } + catch (InterruptedException e) + { + IStatus status = + new Status(IStatus.ERROR, CodeUtilsActivator.PLUGIN_ID, e.getLocalizedMessage()); + EclipseUtils.showErrorDialog(CodeUtilsNLS.UI_GenericErrorDialogTitle, + CodeUtilsNLS.ERR_BuildingBlockCreation_ErrorMessage, status); + } + + if (saved) + { + ICompilationUnit javaFile = + getBuildingBlock().getPackageFragment().getCompilationUnit( + getBuildingBlock().getName() + ".java"); + + if ((javaFile != null) && javaFile.exists()) + { + try + { + JavaUI.openInEditor(javaFile); + } + catch (PartInitException e) + { + // Do nothing + StudioLogger.error(NewServiceWizard.class, "Could not open the service " + + getBuildingBlock().getName() + " on an editor.", e); + } + catch (JavaModelException e) + { + // Do nothing + StudioLogger.error(NewServiceWizard.class, "Could not open the service " + + getBuildingBlock().getName() + " on an editor.", e); + } + } + } + + if (saved) + { + // Collecting usage data for statistical purposes + try + { + StudioLogger.collectUsageData(StudioLogger.WHAT_BUILDINGBLOCK_SERVICE, + StudioLogger.KIND_BUILDINGBLOCK, StudioLogger.DESCRIPTION_DEFAULT, + CodeUtilsActivator.PLUGIN_ID, CodeUtilsActivator.getDefault().getBundle() + .getVersion().toString()); + } + catch (Throwable e) + { + //Do nothing, but error on the log should never prevent app from working + } + } + + return saved; + } + + /* (non-Javadoc) + * @see org.eclipse.ui.IWorkbenchWizard#init(org.eclipse.ui.IWorkbench, org.eclipse.jface.viewers.IStructuredSelection) + */ + public void init(IWorkbench arg0, IStructuredSelection selection) + { + setWindowTitle(CodeUtilsNLS.UI_NewServiceWizard_WizardTitle); + setNeedsProgressMonitor(true); + setDefaultPageImageDescriptor(CodeUtilsActivator.getImageDescriptor(WIZBAN_ICON)); + service.configure(selection); + } + + /* (non-Javadoc) + * @see org.eclipse.jface.wizard.Wizard#addPages() + */ + @Override + public void addPages() + { + addPage(new NewServiceMainPage(service)); + } + + /* (non-Javadoc) + * @see com.motorola.studio.android.wizards.buildingblocks.NewBuildingBlocksWizard#getBuildingBlock() + */ + @Override + protected BuildingBlockModel getBuildingBlock() + { + return service; + } + + /* + * IRunnableWithProgress object to create the service + */ + private class DoSave implements IRunnableWithProgress + { + AndroidException exception = null; + + boolean saved = false; + + public void run(IProgressMonitor monitor) throws InvocationTargetException, + InterruptedException + { + try + { + saved = getBuildingBlock().save(getContainer(), monitor); + } + catch (AndroidException e) + { + exception = e; + } + } + } +} diff --git a/src/plugins/android.codeutils/src/com/motorola/studio/android/wizards/buildingblocks/NewWidgetProviderMainPage.java b/src/plugins/android.codeutils/src/com/motorola/studio/android/wizards/buildingblocks/NewWidgetProviderMainPage.java new file mode 100644 index 0000000..d30eb14 --- /dev/null +++ b/src/plugins/android.codeutils/src/com/motorola/studio/android/wizards/buildingblocks/NewWidgetProviderMainPage.java @@ -0,0 +1,110 @@ +/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.motorola.studio.android.wizards.buildingblocks;
+
+import com.motorola.studio.android.codeutils.CodeUtilsActivator;
+import com.motorola.studio.android.codeutils.i18n.CodeUtilsNLS;
+import com.motorola.studio.android.common.exception.AndroidException;
+import com.motorola.studio.android.common.utilities.AndroidUtils;
+import com.motorola.studio.android.model.WidgetProvider;
+
+/**
+ * Class that implements the Widget Provider Wizard Main Page.
+ */
+public class NewWidgetProviderMainPage extends NewLauncherWizardPage
+{
+
+ private static final String NEW_WIDGET_PROVIDER_HELP = CodeUtilsActivator.PLUGIN_ID
+ + ".newwidgtprvd";
+
+ /**
+ * Default constructor
+ *
+ * @param buildBlock The broadcast receiver model
+ */
+ protected NewWidgetProviderMainPage(WidgetProvider buildBlock)
+ {
+ super(buildBlock, CodeUtilsNLS.UI_NewWidgetProviderMainPage_PageTitle);
+ }
+
+ /* (non-Javadoc)
+ * @see com.motorola.studio.android.wizards.buildingblocks.NewLauncherWizardPage#getBuildBlock()
+ */
+ @Override
+ public WidgetProvider getBuildBlock()
+ {
+ return (WidgetProvider) super.getBuildBlock();
+ }
+
+ /* (non-Javadoc)
+ * @see com.motorola.studio.android.wizards.buildingblocks.NewBuildingBlocksWizardPage#getMethods()
+ */
+ @Override
+ protected Method[] getMethods()
+ {
+ return new Method[0];
+ }
+
+ /* (non-Javadoc)
+ * @see com.motorola.studio.android.wizards.buildingblocks.NewBuildingBlocksWizardPage#getDefaultMessage()
+ */
+ @Override
+ public String getDefaultMessage()
+ {
+ return CodeUtilsNLS.UI_NewWidgetProviderMainPage_DefaultWizardDescription;
+ }
+
+ /* (non-Javadoc)
+ * @see com.motorola.studio.android.wizards.buildingblocks.NewBuildingBlocksWizardPage#getWizardTitle()
+ */
+ @Override
+ public String getWizardTitle()
+ {
+ return CodeUtilsNLS.UI_NewWidgetProviderMainPage_WizardTitle;
+ }
+
+ /* (non-Javadoc)
+ * @see com.motorola.studio.android.wizards.buildingblocks.NewLauncherWizardPage#getIntentFiltersActions()
+ */
+ @Override
+ protected String[] getIntentFiltersActions()
+ {
+ String[] receiverActions = new String[0];
+ try
+ {
+ receiverActions = AndroidUtils.getReceiverActions(getBuildBlock().getProject());
+ }
+ catch (AndroidException e)
+ {
+ setErrorMessage(e.getMessage());
+ }
+ return receiverActions;
+ }
+
+ /**
+ * Gets the help ID to be used for attaching
+ * context sensitive help.
+ *
+ * Classes that extends this class and want to set
+ * their on help should override this method
+ */
+ @Override
+ protected String getHelpId()
+ {
+ return NEW_WIDGET_PROVIDER_HELP;
+ }
+}
diff --git a/src/plugins/android.codeutils/src/com/motorola/studio/android/wizards/buildingblocks/NewWidgetProviderWizard.java b/src/plugins/android.codeutils/src/com/motorola/studio/android/wizards/buildingblocks/NewWidgetProviderWizard.java new file mode 100644 index 0000000..be83a7e --- /dev/null +++ b/src/plugins/android.codeutils/src/com/motorola/studio/android/wizards/buildingblocks/NewWidgetProviderWizard.java @@ -0,0 +1,186 @@ +/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.motorola.studio.android.wizards.buildingblocks;
+
+import java.lang.reflect.InvocationTargetException;
+
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.Status;
+import org.eclipse.jdt.core.ICompilationUnit;
+import org.eclipse.jdt.core.JavaModelException;
+import org.eclipse.jdt.ui.JavaUI;
+import org.eclipse.jface.operation.IRunnableWithProgress;
+import org.eclipse.jface.viewers.IStructuredSelection;
+import org.eclipse.ui.IWorkbench;
+import org.eclipse.ui.PartInitException;
+
+import com.motorola.studio.android.codeutils.CodeUtilsActivator;
+import com.motorola.studio.android.codeutils.i18n.CodeUtilsNLS;
+import com.motorola.studio.android.common.exception.AndroidException;
+import com.motorola.studio.android.common.log.StudioLogger;
+import com.motorola.studio.android.common.utilities.EclipseUtils;
+import com.motorola.studio.android.model.BuildingBlockModel;
+import com.motorola.studio.android.model.WidgetProvider;
+
+/**
+ * Class that implements the Widget Provider building block wizard.
+ */
+public class NewWidgetProviderWizard extends NewBuildingBlocksWizard
+{
+
+ private static final String WIZ_BANNER_ICON = "icons/wizban/widget_provider_block_wiz.png";
+
+ private WidgetProvider widgetProvider = new WidgetProvider();
+
+ @Override
+ protected BuildingBlockModel getBuildingBlock()
+ {
+ return widgetProvider;
+ }
+
+ @Override
+ public boolean performFinish()
+ {
+ boolean saved = false;
+
+ try
+ {
+ DoSave doSave = new DoSave();
+
+ getContainer().run(false, false, doSave);
+
+ if (doSave.exception != null)
+ {
+ throw doSave.exception;
+ }
+ else
+ {
+ saved = doSave.saved;
+ }
+ }
+ catch (AndroidException e)
+ {
+ IStatus status =
+ new Status(IStatus.ERROR, CodeUtilsActivator.PLUGIN_ID, e.getLocalizedMessage());
+ EclipseUtils.showErrorDialog(CodeUtilsNLS.UI_GenericErrorDialogTitle,
+ CodeUtilsNLS.ERR_BuildingBlockCreation_ErrorMessage, status);
+ }
+ catch (InvocationTargetException e)
+ {
+ IStatus status =
+ new Status(IStatus.ERROR, CodeUtilsActivator.PLUGIN_ID, e.getLocalizedMessage());
+ EclipseUtils.showErrorDialog(CodeUtilsNLS.UI_GenericErrorDialogTitle,
+ CodeUtilsNLS.ERR_BuildingBlockCreation_ErrorMessage, status);
+ }
+ catch (InterruptedException e)
+ {
+ IStatus status =
+ new Status(IStatus.ERROR, CodeUtilsActivator.PLUGIN_ID, e.getLocalizedMessage());
+ EclipseUtils.showErrorDialog(CodeUtilsNLS.UI_GenericErrorDialogTitle,
+ CodeUtilsNLS.ERR_BuildingBlockCreation_ErrorMessage, status);
+ }
+
+ if (saved)
+ {
+ ICompilationUnit javaFile =
+ getBuildingBlock().getPackageFragment().getCompilationUnit(
+ getBuildingBlock().getName() + ".java");
+
+ if ((javaFile != null) && javaFile.exists())
+ {
+ try
+ {
+ JavaUI.openInEditor(javaFile);
+ }
+ catch (PartInitException e)
+ {
+ // Do nothing
+ StudioLogger.error(NewWidgetProviderWizard.class,
+ "Could not open the widget provider " + getBuildingBlock().getName()
+ + " on an editor.", e);
+ }
+ catch (JavaModelException e)
+ {
+ // Do nothing
+ StudioLogger.error(NewWidgetProviderWizard.class,
+ "Could not open the widget provider " + getBuildingBlock().getName()
+ + " on an editor.", e);
+ }
+ }
+ }
+
+ if (saved)
+ {
+ // Collecting usage data for statistical purposes
+ try
+ {
+ StudioLogger.collectUsageData(StudioLogger.WHAT_BUILDINGBLOCK_WIDGET_PROVIDER,
+ StudioLogger.KIND_BUILDINGBLOCK, StudioLogger.DESCRIPTION_DEFAULT,
+ CodeUtilsActivator.PLUGIN_ID, CodeUtilsActivator.getDefault().getBundle()
+ .getVersion().toString());
+ }
+ catch (Throwable e)
+ {
+ //Do nothing, but error on the log should never prevent app from working
+ }
+ }
+
+ return saved;
+ }
+
+ public void init(IWorkbench workbench, IStructuredSelection selection)
+ {
+ setWindowTitle(CodeUtilsNLS.UI_NewWidgetProviderWizard_WizardTitle);
+ setNeedsProgressMonitor(true);
+ setDefaultPageImageDescriptor(CodeUtilsActivator.getImageDescriptor(WIZ_BANNER_ICON));
+ widgetProvider.configure(selection);
+
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jface.wizard.Wizard#addPages()
+ */
+ @Override
+ public void addPages()
+ {
+ addPage(new NewWidgetProviderMainPage(widgetProvider));
+ }
+
+ /*
+ * IRunnableWithProgress object to create the broadcast receiver
+ */
+ private class DoSave implements IRunnableWithProgress
+ {
+ AndroidException exception = null;
+
+ boolean saved = false;
+
+ public void run(IProgressMonitor monitor) throws InvocationTargetException,
+ InterruptedException
+ {
+ try
+ {
+ saved = getBuildingBlock().save(getContainer(), monitor);
+ }
+ catch (AndroidException e)
+ {
+ exception = e;
+ }
+ }
+ }
+
+}
|