Author: scabanovich
Date: 2012-10-04 17:11:23 -0400 (Thu, 04 Oct 2012)
New Revision: 44314
Added:
trunk/common/plugins/org.jboss.tools.common.ui/src/org/jboss/tools/common/ui/wizard/service/RegisterAsServiceDialog.java
trunk/common/plugins/org.jboss.tools.common.ui/src/org/jboss/tools/common/ui/wizard/service/RegisterAsServiceHandler.java
trunk/common/plugins/org.jboss.tools.common.ui/src/org/jboss/tools/common/ui/wizard/service/RegisterServiceUtil.java
Modified:
trunk/common/plugins/org.jboss.tools.common.ui/META-INF/MANIFEST.MF
trunk/common/plugins/org.jboss.tools.common.ui/plugin.xml
trunk/common/plugins/org.jboss.tools.common.ui/src/org/jboss/tools/common/ui/CommonUIMessages.java
trunk/common/plugins/org.jboss.tools.common.ui/src/org/jboss/tools/common/ui/messages.properties
trunk/common/plugins/org.jboss.tools.common.ui/src/org/jboss/tools/common/ui/wizard/service/NewServiceCreationWizard.java
trunk/common/plugins/org.jboss.tools.common.ui/src/org/jboss/tools/common/ui/wizard/service/NewServiceWizardPage.java
Log:
JBIDE-12793
https://issues.jboss.org/browse/JBIDE-12793
Action and dialog 'Register As Service' are implemented.
Modified: trunk/common/plugins/org.jboss.tools.common.ui/META-INF/MANIFEST.MF
===================================================================
--- trunk/common/plugins/org.jboss.tools.common.ui/META-INF/MANIFEST.MF 2012-10-04
20:27:48 UTC (rev 44313)
+++ trunk/common/plugins/org.jboss.tools.common.ui/META-INF/MANIFEST.MF 2012-10-04
21:11:23 UTC (rev 44314)
@@ -32,6 +32,7 @@
org.eclipse.ltk.ui.refactoring;bundle-version="3.6.0",
org.eclipse.wst.server.ui;bundle-version="1.3.0",
org.eclipse.jdt.junit;bundle-version="3.7.0",
+ org.eclipse.core.expressions,
org.jboss.tools.common.text.xml
Export-Package: org.jboss.tools.common.ui,
org.jboss.tools.common.ui.databinding,
Modified: trunk/common/plugins/org.jboss.tools.common.ui/plugin.xml
===================================================================
--- trunk/common/plugins/org.jboss.tools.common.ui/plugin.xml 2012-10-04 20:27:48 UTC (rev
44313)
+++ trunk/common/plugins/org.jboss.tools.common.ui/plugin.xml 2012-10-04 21:11:23 UTC (rev
44314)
@@ -77,4 +77,39 @@
</extension>
+ <extension point="org.eclipse.ui.commands">
+ <command
+ name="Register As Service"
+
defaultHandler="org.jboss.tools.common.ui.wizard.service.RegisterAsServiceHandler"
+ id="org.jboss.tools.common.ui.RegisterAsService">
+ </command>
+
+ </extension>
+
+ <extension point="org.eclipse.ui.menus">
+ <menuContribution
+ allPopups="true"
+ locationURI="popup:org.eclipse.ui.popup.any?after=additions">
+ <command
+ commandId="org.jboss.tools.common.ui.RegisterAsService"
+ label="Register As Service...">
+ <visibleWhen
+ checkEnabled="true">
+ </visibleWhen>
+ <!--visibleWhen checkEnabled="false">
+ <with variable="selection">
+ <count value="1" />
+ <or>
+ <iterate>
+ <adapt type="org.eclipse.jdt.core.IType" />
+ </iterate>
+ <iterate>
+ <adapt type="org.eclipse.jdt.core.ICompilationUnit"
/>
+ </iterate>
+ </or>
+ </with>
+ </visibleWhen-->
+ </command>
+ </menuContribution>
+ </extension>
</plugin>
Modified:
trunk/common/plugins/org.jboss.tools.common.ui/src/org/jboss/tools/common/ui/CommonUIMessages.java
===================================================================
---
trunk/common/plugins/org.jboss.tools.common.ui/src/org/jboss/tools/common/ui/CommonUIMessages.java 2012-10-04
20:27:48 UTC (rev 44313)
+++
trunk/common/plugins/org.jboss.tools.common.ui/src/org/jboss/tools/common/ui/CommonUIMessages.java 2012-10-04
21:11:23 UTC (rev 44314)
@@ -94,6 +94,14 @@
public static String NEW_SERVICE_WIZARD_SERVICE_TYPE_CONCRETE;
public static String NEW_SERVICE_WIZARD_SELECT_SERVICE_TYPE_TITLE;
+ public static String REGISTER_AS_SERVICE_TITLE;
+ public static String REGISTER_AS_SERVICE_SUB_TITLE;
+ public static String REGISTER_AS_SERVICE_MESSAGE;
+ public static String REGISTER_AS_SERVICE_NO_TYPES_MESSAGE;
+ public static String REGISTER_AS_SERVICE_TYPE_LABEL;
+ public static String REGISTER_AS_SERVICE_NON_ABSTRACT_MESSAGE;
+ public static String REGISTER_AS_SERVICE_ALREADY_REGISTERED_MESSAGE;
+
static {
NLS.initializeMessages(BUNDLE_NAME, CommonUIMessages.class);
}
Modified:
trunk/common/plugins/org.jboss.tools.common.ui/src/org/jboss/tools/common/ui/messages.properties
===================================================================
---
trunk/common/plugins/org.jboss.tools.common.ui/src/org/jboss/tools/common/ui/messages.properties 2012-10-04
20:27:48 UTC (rev 44313)
+++
trunk/common/plugins/org.jboss.tools.common.ui/src/org/jboss/tools/common/ui/messages.properties 2012-10-04
21:11:23 UTC (rev 44314)
@@ -50,6 +50,12 @@
NEW_SERVICE_WIZARD_SERVICE_TYPE_EMPTY=Please select service type.
NEW_SERVICE_WIZARD_SERVICE_TYPE_NOT_EXISTS=Type {0} does not exist.
NEW_SERVICE_WIZARD_SERVICE_TYPE_FINAL=Service type cannot be final.
-NEW_SERVICE_WIZARD_SERVICE_TYPE_CONCRETE=Non-abstract service type is discouraged.
+NEW_SERVICE_WIZARD_SERVICE_TYPE_CONCRETE=Non-abstract service type is discouraged.Service
Provider
NEW_SERVICE_WIZARD_SELECT_SERVICE_TYPE_TITLE=Select Service Type
-
+REGISTER_AS_SERVICE_TITLE=Register As Service
+REGISTER_AS_SERVICE_SUB_TITLE=Service Provider {0}
+REGISTER_AS_SERVICE_MESSAGE=Select Service Type
+REGISTER_AS_SERVICE_NO_TYPES_MESSAGE=This type does not implement any service type.
+REGISTER_AS_SERVICE_TYPE_LABEL=Service Type:
+REGISTER_AS_SERVICE_NON_ABSTRACT_MESSAGE=The use of a non-abstract type as a service type
is discouraged.
+REGISTER_AS_SERVICE_ALREADY_REGISTERED_MESSAGE=This type is already registered for the
selected service type.
Modified:
trunk/common/plugins/org.jboss.tools.common.ui/src/org/jboss/tools/common/ui/wizard/service/NewServiceCreationWizard.java
===================================================================
---
trunk/common/plugins/org.jboss.tools.common.ui/src/org/jboss/tools/common/ui/wizard/service/NewServiceCreationWizard.java 2012-10-04
20:27:48 UTC (rev 44313)
+++
trunk/common/plugins/org.jboss.tools.common.ui/src/org/jboss/tools/common/ui/wizard/service/NewServiceCreationWizard.java 2012-10-04
21:11:23 UTC (rev 44314)
@@ -11,25 +11,17 @@
package org.jboss.tools.common.ui.wizard.service;
-import java.io.ByteArrayInputStream;
-
-import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
-import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
-import org.eclipse.core.runtime.NullProgressMonitor;
-import org.eclipse.core.runtime.Path;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.internal.ui.wizards.NewElementWizard;
import org.eclipse.jdt.ui.wizards.NewClassWizardPage;
import org.eclipse.jdt.ui.wizards.NewTypeWizardPage;
-import org.jboss.tools.common.EclipseUtil;
import org.jboss.tools.common.ui.CommonUIMessages;
import org.jboss.tools.common.ui.CommonUIPlugin;
-import org.jboss.tools.common.util.FileUtil;
/**
*
@@ -56,7 +48,6 @@
if (fPage == null) {
fPage = new NewServiceWizardPage();
((NewClassWizardPage)fPage).init(getSelection());
- //init page
}
addPage(fPage);
}
@@ -88,55 +79,12 @@
}
private void registerService() throws CoreException {
+ IProject project = fPage.getCreatedType().getResource().getProject();
String typeName = fPage.getCreatedType().getFullyQualifiedName();
String serviceType = ((NewServiceWizardPage)fPage).getServiceRawType();
- IContainer f = getServiceFolder();
- if(f != null) {
- IFile file = f.getFile(new Path(serviceType));
- if(file.exists()) {
- String content = FileUtil.readStream(file);
- if(content.length() > 0 && !content.endsWith("\n")) {
//$NON-NLS-1$
- content += "\n"; //$NON-NLS-1$
- }
- content += typeName;
- file.setContents(new ByteArrayInputStream(content.getBytes()), true, true, new
NullProgressMonitor());
- } else {
- String content = typeName;
- file.create(new ByteArrayInputStream(content.getBytes()), true, new
NullProgressMonitor());
- }
- }
+ RegisterServiceUtil.registerService(project, typeName, serviceType);
}
- static final String META_INF_FOLDER_NAME = "META-INF"; //$NON-NLS-1$
- static final String SERVICES_FOLDER_NAME = "services"; //$NON-NLS-1$
-
- private IFolder getServiceFolder() throws CoreException {
- IContainer m = getMetaInf();
- if(m != null) {
- IFolder ss = m.getFolder(new Path(SERVICES_FOLDER_NAME));
- if(!ss.exists()) {
- ss.create(true, true, new NullProgressMonitor());
- }
- return ss;
- }
- return null;
- }
-
- private IContainer getMetaInf() throws CoreException {
- IProject project = fPage.getCreatedType().getResource().getProject();
- IResource[] rs = EclipseUtil.getJavaSourceRoots(project);
- if(rs == null || rs.length == 0) {
- return null;
- }
- for (IResource r: rs) {
- IFolder f = ((IContainer)r).getFolder(new Path(META_INF_FOLDER_NAME));
- if(f.exists()) return f;
- }
- IFolder f = ((IContainer)rs[0]).getFolder(new Path(META_INF_FOLDER_NAME));
- f.create(true, true, new NullProgressMonitor());
- return f;
- }
-
@Override
protected void finishPage(IProgressMonitor monitor) throws InterruptedException,
CoreException {
fPage.createType(monitor); // use the full progress monitor
Modified:
trunk/common/plugins/org.jboss.tools.common.ui/src/org/jboss/tools/common/ui/wizard/service/NewServiceWizardPage.java
===================================================================
---
trunk/common/plugins/org.jboss.tools.common.ui/src/org/jboss/tools/common/ui/wizard/service/NewServiceWizardPage.java 2012-10-04
20:27:48 UTC (rev 44313)
+++
trunk/common/plugins/org.jboss.tools.common.ui/src/org/jboss/tools/common/ui/wizard/service/NewServiceWizardPage.java 2012-10-04
21:11:23 UTC (rev 44314)
@@ -221,6 +221,10 @@
return null;
}
});
+ List<String> s = getSuperInterfaces();
+ if(!s.isEmpty()) {
+ setServiceType(s.get(0));
+ }
interfaceField.doFillIntoGrid(composite);
interfaceField.addPropertyChangeListener(new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent evt) {
Added:
trunk/common/plugins/org.jboss.tools.common.ui/src/org/jboss/tools/common/ui/wizard/service/RegisterAsServiceDialog.java
===================================================================
---
trunk/common/plugins/org.jboss.tools.common.ui/src/org/jboss/tools/common/ui/wizard/service/RegisterAsServiceDialog.java
(rev 0)
+++
trunk/common/plugins/org.jboss.tools.common.ui/src/org/jboss/tools/common/ui/wizard/service/RegisterAsServiceDialog.java 2012-10-04
21:11:23 UTC (rev 44314)
@@ -0,0 +1,160 @@
+package org.jboss.tools.common.ui.wizard.service;
+
+import java.beans.PropertyChangeEvent;
+import java.beans.PropertyChangeListener;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.jdt.core.Flags;
+import org.eclipse.jdt.core.IType;
+import org.eclipse.jface.dialogs.IDialogConstants;
+import org.eclipse.jface.dialogs.IMessageProvider;
+import org.eclipse.jface.dialogs.TitleAreaDialog;
+import org.eclipse.osgi.util.NLS;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.layout.GridData;
+import org.eclipse.swt.layout.GridLayout;
+import org.eclipse.swt.widgets.Button;
+import org.eclipse.swt.widgets.Combo;
+import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Control;
+import org.eclipse.swt.widgets.Label;
+import org.eclipse.swt.widgets.Shell;
+import org.jboss.tools.common.java.IParametedType;
+import org.jboss.tools.common.ui.CommonUIMessages;
+import org.jboss.tools.common.ui.CommonUIPlugin;
+import org.jboss.tools.common.ui.widget.editor.IFieldEditor;
+import org.jboss.tools.common.ui.widget.editor.IFieldEditorFactory;
+
+public class RegisterAsServiceDialog extends TitleAreaDialog {
+ IType type;
+ Map<String, IParametedType> types;
+ IFieldEditor serviceTypeSelector;
+ String result;
+
+ public RegisterAsServiceDialog(Shell parentShell, IType type, Map<String,
IParametedType> types) {
+ super(parentShell);
+ this.type = type;
+ this.types = types;
+ setShellStyle(getShellStyle() | SWT.RESIZE);
+ List<String> serviceTypeNames = new ArrayList<String>(types.keySet());
+ String defaultValue = serviceTypeNames.isEmpty() ? "" :
serviceTypeNames.get(0);
+ serviceTypeSelector =
IFieldEditorFactory.INSTANCE.createComboEditor("serviceType", //$NON-NLS-1$
+ CommonUIMessages.REGISTER_AS_SERVICE_TYPE_LABEL, serviceTypeNames, defaultValue);
+ }
+
+ private final int DIALOG_WIDTH = 400;
+ private final int DIALOG_HEIGHT = 60;
+
+ protected Control createDialogArea(Composite parent) {
+ getShell().setText(CommonUIMessages.REGISTER_AS_SERVICE_TITLE);
+ setTitle(NLS.bind(CommonUIMessages.REGISTER_AS_SERVICE_SUB_TITLE,
type.getFullyQualifiedName()));
+// setTitleImage(ModelUIImages.getImage(ModelUIImages.WIZARD_DEFAULT)); // image is
managed by registry
+ setMessage(CommonUIMessages.REGISTER_AS_SERVICE_MESSAGE);
+ if(types.isEmpty()) {
+ setErrorMessage(CommonUIMessages.REGISTER_AS_SERVICE_NO_TYPES_MESSAGE);
+ }
+
+ Composite composite = new Composite(parent, SWT.NONE);
+ GridLayout gridLayout = new GridLayout(1, false);
+ gridLayout.marginHeight = 5;
+ gridLayout.marginWidth = 5;
+ gridLayout.horizontalSpacing = 10;
+ gridLayout.verticalSpacing = 0;
+ composite.setLayout(gridLayout);
+
+ Label dialogAreaSeparator = new Label(composite, SWT.HORIZONTAL | SWT.SEPARATOR);
+ dialogAreaSeparator.setLayoutData(new GridData(SWT.FILL, SWT.NONE, true, false));
+
+ Control pageArea = createField(composite);
+ GridData gd = new GridData(SWT.FILL, SWT.BEGINNING, true, true);
+ pageArea.setLayoutData(gd);
+
+ dialogAreaSeparator = new Label(composite, SWT.HORIZONTAL | SWT.SEPARATOR);
+ dialogAreaSeparator.setLayoutData(new GridData(SWT.FILL, SWT.NONE, true, false));
+
+ gd = new GridData(SWT.FILL, SWT.FILL, true, true);
+ gd.widthHint = DIALOG_WIDTH;
+ gd.heightHint = DIALOG_HEIGHT;
+ composite.setLayoutData(gd);
+
+ return composite;
+ }
+
+ Control createField(Composite parent) {
+ Composite composite = new Composite(parent, SWT.NONE);
+ GridLayout gridLayout = new GridLayout(2, false);
+ gridLayout.marginHeight = 10;
+ gridLayout.marginWidth = 0;
+ gridLayout.horizontalSpacing = 10;
+ gridLayout.verticalSpacing = 10;
+ GridData gd = new GridData(GridData.FILL_HORIZONTAL);
+ composite.setLayoutData(gd);
+ composite.setLayout(gridLayout);
+ serviceTypeSelector.doFillIntoGrid(composite);
+ Object[] os = serviceTypeSelector.getEditorControls();
+ if(os.length == 2 && os[1] instanceof Combo) {
+ ((Combo)os[1]).setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
+ }
+ serviceTypeSelector.addPropertyChangeListener(new PropertyChangeListener() {
+
+ @Override
+ public void propertyChange(PropertyChangeEvent evt) {
+ validate();
+ }
+ });
+ return composite;
+ }
+
+ void validate() {
+ String serviceType = serviceTypeSelector.getValueAsString();
+ if(serviceType == null || serviceType.length() == 0) {
+ return;
+ }
+ IParametedType type = types.get(serviceType);
+ String warning = null;
+ if(type != null) {
+ IType t = type.getType();
+ try {
+ if(!t.isInterface() && !Flags.isAbstract(t.getFlags())) {
+ warning = CommonUIMessages.REGISTER_AS_SERVICE_NON_ABSTRACT_MESSAGE;
+ }
+ } catch (CoreException e) {
+ CommonUIPlugin.getDefault().logError(e);
+ }
+ }
+ String typeName = this.type.getFullyQualifiedName();
+ if(RegisterServiceUtil.isServiceRegistered(type.getType().getJavaProject().getProject(),
typeName, serviceType)) {
+ getButton(IDialogConstants.OK_ID).setEnabled(false);
+ setMessage(CommonUIMessages.REGISTER_AS_SERVICE_ALREADY_REGISTERED_MESSAGE,
IMessageProvider.ERROR);
+ } else {
+ getButton(IDialogConstants.OK_ID).setEnabled(true);
+ if(warning != null) {
+ setMessage(warning, IMessageProvider.WARNING);
+ } else {
+ setMessage(CommonUIMessages.REGISTER_AS_SERVICE_MESSAGE);
+ }
+ }
+ }
+
+ protected Control createContents(Composite parent) {
+ Control c = super.createContents(parent);
+ if(types.isEmpty()) {
+ Button ok = getButton(IDialogConstants.OK_ID);
+ ok.setEnabled(false);
+ }
+ validate();
+ return c;
+ }
+
+ protected void okPressed() {
+ result = serviceTypeSelector.getValueAsString();
+ super.okPressed();
+ }
+
+ public String getResult() {
+ return result;
+ }
+}
Property changes on:
trunk/common/plugins/org.jboss.tools.common.ui/src/org/jboss/tools/common/ui/wizard/service/RegisterAsServiceDialog.java
___________________________________________________________________
Added: svn:mime-type
+ text/plain
Added:
trunk/common/plugins/org.jboss.tools.common.ui/src/org/jboss/tools/common/ui/wizard/service/RegisterAsServiceHandler.java
===================================================================
---
trunk/common/plugins/org.jboss.tools.common.ui/src/org/jboss/tools/common/ui/wizard/service/RegisterAsServiceHandler.java
(rev 0)
+++
trunk/common/plugins/org.jboss.tools.common.ui/src/org/jboss/tools/common/ui/wizard/service/RegisterAsServiceHandler.java 2012-10-04
21:11:23 UTC (rev 44314)
@@ -0,0 +1,129 @@
+/*******************************************************************************
+ * Copyright (c) 2012 Red Hat, Inc.
+ * Distributed under license by Red Hat, Inc. All rights reserved.
+ * This program is made available under the terms of the
+ * Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at
http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Red Hat, Inc. - initial API and implementation
+ ******************************************************************************/
+package org.jboss.tools.common.ui.wizard.service;
+
+import java.util.Collection;
+import java.util.Map;
+import java.util.TreeMap;
+
+import org.eclipse.core.commands.AbstractHandler;
+import org.eclipse.core.commands.ExecutionEvent;
+import org.eclipse.core.expressions.IEvaluationContext;
+import org.eclipse.core.resources.IProject;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.jdt.core.Flags;
+import org.eclipse.jdt.core.ICompilationUnit;
+import org.eclipse.jdt.core.IType;
+import org.eclipse.jface.dialogs.IDialogConstants;
+import org.eclipse.jface.viewers.ISelection;
+import org.eclipse.jface.viewers.IStructuredSelection;
+import org.eclipse.ui.ISources;
+import org.eclipse.ui.handlers.HandlerUtil;
+import org.jboss.tools.common.java.IParametedType;
+import org.jboss.tools.common.java.ParametedType;
+import org.jboss.tools.common.java.ParametedTypeFactory;
+import org.jboss.tools.common.ui.CommonUIPlugin;
+
+/**
+ *
+ * @author Viacheslav Kabanovich
+ *
+ */
+public class RegisterAsServiceHandler extends AbstractHandler {
+
+ public RegisterAsServiceHandler() {
+ }
+
+ @Override
+ public void setEnabled(Object evaluationContext) {
+ setBaseEnabled(computeEnabled(evaluationContext));
+ }
+
+ private boolean computeEnabled(Object evaluationContext) {
+ if(evaluationContext instanceof IEvaluationContext) {
+ IEvaluationContext c = (IEvaluationContext)evaluationContext;
+ ISelection selection =
(ISelection)c.getVariable(ISources.ACTIVE_CURRENT_SELECTION_NAME);
+ return getSelectedType(selection) != null;
+ }
+ return false;
+ }
+
+ /**
+ * Returns the first IType from selection which is a concrete class.
+ * @param selection
+ * @return
+ */
+ private IType getSelectedType(ISelection selection) {
+ if(selection != null && !selection.isEmpty() && (selection instanceof
IStructuredSelection)) {
+ for (Object selected: ((IStructuredSelection)selection).toList()) {
+ try {
+ if(selected instanceof ICompilationUnit) {
+ ICompilationUnit u = (ICompilationUnit)selected;
+ for(IType type: u.getTypes()) {
+ if(accept(type)) {
+ return type;
+ }
+ }
+ } else if(selected instanceof IType) {
+ IType type = (IType)selected;
+ if(accept(type)) {
+ return type;
+ }
+ }
+ } catch (CoreException e) {
+ CommonUIPlugin.getDefault().logError(e);
+ }
+ }
+ }
+ return null;
+ }
+
+ private boolean accept(IType type) throws CoreException {
+ return !type.isInterface() && !type.isAnnotation() &&
!Flags.isAbstract(type.getFlags());
+ }
+
+ @Override
+ public Object execute(ExecutionEvent event) throws
org.eclipse.core.commands.ExecutionException {
+ ISelection selection =
HandlerUtil.getActiveWorkbenchWindow(event).getSelectionService().getSelection();
+ IType type = getSelectedType(selection);
+ if(type == null) {
+ return null;
+ }
+ try {
+ ParametedType parametedType = new ParametedTypeFactory().newParametedType(type);
+ Collection<IParametedType> ts = parametedType.getAllTypes();
+ Map<String, IParametedType> types = new TreeMap<String,
IParametedType>();
+ for (IParametedType t: ts) {
+ if(t.getType() != null) {
+ String q = t.getType().getFullyQualifiedName();
+ types.put(q, t);
+ }
+ }
+ types.remove("java.lang.Object"); //$NON-NLS-1$
+ types.remove(type.getFullyQualifiedName());
+
+ RegisterAsServiceDialog dialog = new
RegisterAsServiceDialog(HandlerUtil.getActiveShell(event), type, types);
+ dialog.create();
+
+ int i = dialog.open();
+ if(i == IDialogConstants.OK_ID) {
+ IProject project = type.getJavaProject().getProject();
+ String typeName = type.getFullyQualifiedName();
+ String serviceType = dialog.getResult();
+ RegisterServiceUtil.registerService(project, typeName, serviceType);
+ }
+ } catch (CoreException e) {
+ CommonUIPlugin.getDefault().logError(e);
+ }
+ return null;
+ }
+
+}
Property changes on:
trunk/common/plugins/org.jboss.tools.common.ui/src/org/jboss/tools/common/ui/wizard/service/RegisterAsServiceHandler.java
___________________________________________________________________
Added: svn:mime-type
+ text/plain
Added:
trunk/common/plugins/org.jboss.tools.common.ui/src/org/jboss/tools/common/ui/wizard/service/RegisterServiceUtil.java
===================================================================
---
trunk/common/plugins/org.jboss.tools.common.ui/src/org/jboss/tools/common/ui/wizard/service/RegisterServiceUtil.java
(rev 0)
+++
trunk/common/plugins/org.jboss.tools.common.ui/src/org/jboss/tools/common/ui/wizard/service/RegisterServiceUtil.java 2012-10-04
21:11:23 UTC (rev 44314)
@@ -0,0 +1,168 @@
+/*******************************************************************************
+ * Copyright (c) 2012 Red Hat, Inc.
+ * Distributed under license by Red Hat, Inc. All rights reserved.
+ * This program is made available under the terms of the
+ * Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at
http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Red Hat, Inc. - initial API and implementation
+ ******************************************************************************/
+package org.jboss.tools.common.ui.wizard.service;
+
+import java.io.ByteArrayInputStream;
+
+import org.eclipse.core.resources.IContainer;
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.IFolder;
+import org.eclipse.core.resources.IProject;
+import org.eclipse.core.resources.IResource;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.NullProgressMonitor;
+import org.eclipse.core.runtime.Path;
+import org.jboss.tools.common.EclipseUtil;
+import org.jboss.tools.common.ui.CommonUIPlugin;
+import org.jboss.tools.common.util.FileUtil;
+
+/**
+ *
+ * @author Viacheslav Kabanovich
+ *
+ */
+public class RegisterServiceUtil {
+ static final String META_INF_FOLDER_NAME = "META-INF"; //$NON-NLS-1$
+ static final String SERVICES_FOLDER_NAME = "services"; //$NON-NLS-1$
+
+ /**
+ * Returns true if class %typeName% is listed in existing file
+ * META-INF/services/%serviceType% in a Java source folder of the project.
+ *
+ * @param project
+ * @param typeName
+ * @param serviceType
+ * @return
+ * @throws CoreException
+ */
+ public static boolean isServiceRegistered(IProject project, String typeName, String
serviceType) {
+ try {
+ IContainer f = getServiceFolder(project, false);
+ if(f == null || !f.exists()) {
+ return false;
+ }
+ IFile file = f.getFile(new Path(serviceType));
+ return file.exists() && contains(FileUtil.readStream(file), typeName);
+ } catch (CoreException e) {
+ CommonUIPlugin.getDefault().logError(e);
+ }
+ return false;
+ }
+
+ /**
+ * Returns true if %content% contains %typeName% separated from other text by
whitespaces.
+ *
+ * @param content
+ * @param typeName
+ * @return
+ */
+ private static boolean contains(String content, String typeName) {
+ int p = content.indexOf(typeName);
+ while(p >= 0) {
+ int p0 = p;
+ p += typeName.length();
+ if((p == content.length() || Character.isWhitespace(content.charAt(p)))
+ && (p0 == 0 || Character.isWhitespace(content.charAt(p0 - 1))) ) {
+ //registered already
+ return true;
+ }
+ p = content.indexOf(typeName, p);
+ }
+ return false;
+ }
+
+ /**
+ * Includes class %typeName% into file META-INF/services/%serviceType%
+ * in a Java source folder of the project, if it is not registered yet.
+ * If the file does not exist, it is created in the first Java source folder.
+ * Existence of a Java source folder is evident by clients of this method:
+ * (1) Service creation wizard that creates a new Java class in existing Java source;
+ * (2) Service registration context action, that is run on selected IType or
ICompilationUnit in existing Java source. *
+ *
+ * @param project
+ * @param typeName
+ * @param serviceType
+ * @throws CoreException
+ */
+ public static void registerService(IProject project, String typeName, String
serviceType) throws CoreException {
+ IContainer f = getServiceFolder(project, true);
+ if(f != null) {
+ IFile file = f.getFile(new Path(serviceType));
+ if(file.exists()) {
+ String content = FileUtil.readStream(file);
+ if(contains(content, typeName)) {
+ return;
+ }
+ if(content.length() > 0 && !content.endsWith("\n")) {
//$NON-NLS-1$
+ content += "\n"; //$NON-NLS-1$
+ }
+ content += typeName;
+ file.setContents(new ByteArrayInputStream(content.getBytes()), true, true, new
NullProgressMonitor());
+ } else {
+ String content = typeName;
+ file.create(new ByteArrayInputStream(content.getBytes()), true, new
NullProgressMonitor());
+ }
+ }
+ }
+
+ /**
+ * Returns the first existing 'META-INF/services' folder in a Java source
folder,
+ * or newly created 'META-INF/services' folder in an existing Java source folder
if 'create' is set to true,
+ * or null.
+ *
+ * @param project
+ * @param create
+ * @return
+ * @throws CoreException
+ */
+ private static IFolder getServiceFolder(IProject project, boolean create) throws
CoreException {
+ IContainer m = getMetaInf(project, create);
+ if(m != null) {
+ IFolder ss = m.getFolder(new Path(SERVICES_FOLDER_NAME));
+ if(!ss.exists()) {
+ if(!create) {
+ return null;
+ }
+ ss.create(true, true, new NullProgressMonitor());
+ }
+ return ss;
+ }
+ return null;
+ }
+
+ /**
+ * Returns the first existing META-INF folder in a Java source folder,
+ * or newly created META-INF folder in an existing Java source folder if
'create' is set to true,
+ * or null.
+ *
+ * @param project
+ * @param create
+ * @return
+ * @throws CoreException
+ */
+ private static IContainer getMetaInf(IProject project, boolean create) throws
CoreException {
+ IResource[] rs = EclipseUtil.getJavaSourceRoots(project);
+ if(rs == null || rs.length == 0) {
+ return null;
+ }
+ for (IResource r: rs) {
+ IFolder f = ((IContainer)r).getFolder(new Path(META_INF_FOLDER_NAME));
+ if(f.exists()) return f;
+ }
+ if(!create) {
+ return null;
+ }
+ IFolder f = ((IContainer)rs[0]).getFolder(new Path(META_INF_FOLDER_NAME));
+ f.create(true, true, new NullProgressMonitor());
+ return f;
+ }
+
+}
Property changes on:
trunk/common/plugins/org.jboss.tools.common.ui/src/org/jboss/tools/common/ui/wizard/service/RegisterServiceUtil.java
___________________________________________________________________
Added: svn:mime-type
+ text/plain