diff --git a/batch/itests/org.jboss.tools.batch.ui.itest/src/org/jboss/tools/batch/ui/itest/BatchPropertyHyperlinkDetectorTest.java b/batch/itests/org.jboss.tools.batch.ui.itest/src/org/jboss/tools/batch/ui/itest/BatchPropertyHyperlinkDetectorTest.java new file mode 100644 index 0000000000..a4a1b42f27 --- /dev/null +++ b/batch/itests/org.jboss.tools.batch.ui.itest/src/org/jboss/tools/batch/ui/itest/BatchPropertyHyperlinkDetectorTest.java @@ -0,0 +1,147 @@ +/******************************************************************************* + * Copyright (c) 2015 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.batch.ui.itest; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +import junit.framework.TestCase; + +import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.IProject; +import org.eclipse.core.resources.ResourcesPlugin; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.jdt.core.IField; +import org.eclipse.jdt.core.IJavaProject; +import org.eclipse.jdt.core.IType; +import org.eclipse.swt.widgets.Display; +import org.eclipse.ui.PlatformUI; +import org.jboss.tools.batch.core.IBatchArtifact; +import org.jboss.tools.batch.core.IBatchProperty; +import org.jboss.tools.batch.internal.core.impl.BatchProject; +import org.jboss.tools.batch.internal.core.impl.BatchProjectFactory; +import org.jboss.tools.batch.internal.core.impl.BatchUtil.NodePathTextSourceReference; +import org.jboss.tools.batch.ui.hyperlink.BatchHyperlinkDetector; +import org.jboss.tools.batch.ui.hyperlink.BatchHyperlinkMessages; +import org.jboss.tools.batch.ui.hyperlink.BatchPropertyDialog; +import org.jboss.tools.batch.ui.hyperlink.BatchPropertyHyperlink; +import org.jboss.tools.batch.ui.hyperlink.BatchPropertyHyperlinkDetector; +import org.jboss.tools.common.model.util.EclipseResourceUtil; +import org.jboss.tools.common.text.ext.hyperlink.OpenJavaElementHyperlink; +import org.jboss.tools.common.text.ext.hyperlink.xml.XMLJumpToHyperlink; +import org.jboss.tools.common.util.EclipseJavaUtil; +import org.jboss.tools.jst.jsp.test.openon.HyperlinkTestUtil; +import org.jboss.tools.jst.jsp.test.openon.HyperlinkTestUtil.TestHyperlink; +import org.jboss.tools.jst.jsp.test.openon.HyperlinkTestUtil.TestRegion; + +public class BatchPropertyHyperlinkDetectorTest extends TestCase { + private static final String PROJECT_NAME = "BatchTestProject"; //$NON-NLS-1$ + + public IProject project = null; + + protected void setUp() { + project = ResourcesPlugin.getWorkspace().getRoot().getProject( + PROJECT_NAME); + PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().closeAllEditors(false); + } + + protected void tearDown() { + PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().closeAllEditors(false); + } + + public BatchPropertyHyperlinkDetectorTest() { + super("BatchHyperlinkDetector Test"); //$NON-NLS-1$ + } + + public void testDetector() throws Exception{ + checkHyperlinkDetector("src/batch/SearchableBatchlet.java"); //$NON-NLS-1$ + } + + public void testBatchPropertyDialog() throws CoreException { + String path = "src/batch/SearchableBatchlet.java"; + IFile file = project.getFile(path); + assertTrue(file.exists()); + + BatchProject batchProject = (BatchProject) BatchProjectFactory.getBatchProjectWithProgress(project); + + assertNotNull(batchProject); + + IJavaProject javaProject = EclipseResourceUtil.getJavaProject(project); + assertNotNull(javaProject); + + IType type = EclipseJavaUtil.findType(javaProject, "batch.SearchableBatchlet"); + assertNotNull(type); + + IField field = EclipseJavaUtil.findField(type, "otherName"); + assertNotNull(field); + + IBatchArtifact artifact = batchProject.getArtifact(type); + assertNotNull(artifact); + + IBatchProperty batchProperty = artifact.getProperty(field); + assertNotNull(batchProperty); + + Display display = Display.getCurrent(); + if(display == null) { + display = Display.getDefault(); + } + BatchPropertyDialog dialog = new BatchPropertyDialog(display.getActiveShell(), batchProperty); + dialog.setBlockOnOpen(false); + dialog.open(); + + HashMap> references = dialog.getDisplayedReferences(); + + IFile jobXMLFile = project.getFile("src/META-INF/batch-jobs/job-search.xml"); + assertTrue(jobXMLFile.exists()); + + List list = references.get(jobXMLFile); + assertNotNull(list); + + assertEquals(1, list.size()); + + NodePathTextSourceReference reference = list.get(0); + assertNotNull(reference); + } + + private void checkHyperlinkDetector(String pageName) throws Exception{ + List regionList = getTestRegionList(); + HyperlinkTestUtil.checkRegions(project, pageName, regionList, new BatchPropertyHyperlinkDetector()); + } + + private List getTestRegionList(){ + ArrayList regionList = new ArrayList(); + + regionList.add(new TestRegion("SearchableBatchlet", new TestHyperlink[]{})); + + regionList.add(new TestRegion("Inject", new TestHyperlink[]{ //$NON-NLS-1$ + new TestHyperlink(BatchPropertyHyperlink.class, "Show All Batch Property References") //$NON-NLS-1$ + })); + + regionList.add(new TestRegion("BatchProperty", new TestHyperlink[]{ //$NON-NLS-1$ + new TestHyperlink(BatchPropertyHyperlink.class, "Show All Batch Property References") //$NON-NLS-1$ + })); + + regionList.add(new TestRegion("String", new TestHyperlink[]{ //$NON-NLS-1$ + new TestHyperlink(BatchPropertyHyperlink.class, BatchHyperlinkMessages.SHOW_ALL_BATCH_PROPERTY_REFERENCES) //$NON-NLS-1$ + })); + + regionList.add(new TestRegion("otherName", new TestHyperlink[]{ //$NON-NLS-1$ + new TestHyperlink(BatchPropertyHyperlink.class, BatchHyperlinkMessages.SHOW_ALL_BATCH_PROPERTY_REFERENCES) //$NON-NLS-1$ + })); + + return regionList; + } + + + + +} diff --git a/batch/itests/org.jboss.tools.batch.ui.itest/src/org/jboss/tools/batch/ui/itest/BatchUIAllTests.java b/batch/itests/org.jboss.tools.batch.ui.itest/src/org/jboss/tools/batch/ui/itest/BatchUIAllTests.java index 083632b0fd..f3a20e55f3 100644 --- a/batch/itests/org.jboss.tools.batch.ui.itest/src/org/jboss/tools/batch/ui/itest/BatchUIAllTests.java +++ b/batch/itests/org.jboss.tools.batch.ui.itest/src/org/jboss/tools/batch/ui/itest/BatchUIAllTests.java @@ -45,6 +45,7 @@ public static Test suite() { TestSuite suite = new TestSuite("Editor"); suite.addTestSuite(BatchEditorTest.class); suite.addTestSuite(BatchHyperlinkDetectorTest.class); + suite.addTestSuite(BatchPropertyHyperlinkDetectorTest.class); suite.addTestSuite(BatchELHyperlinkTest.class); suite.addTestSuite(BatchQueryParticipantTest.class); suite.addTestSuite(BatchRenameParticipantTest.class); diff --git a/batch/plugins/org.jboss.tools.batch.core/src/org/jboss/tools/batch/internal/core/impl/BatchProperty.java b/batch/plugins/org.jboss.tools.batch.core/src/org/jboss/tools/batch/internal/core/impl/BatchProperty.java index 7a735fba43..a1fa3db995 100644 --- a/batch/plugins/org.jboss.tools.batch.core/src/org/jboss/tools/batch/internal/core/impl/BatchProperty.java +++ b/batch/plugins/org.jboss.tools.batch.core/src/org/jboss/tools/batch/internal/core/impl/BatchProperty.java @@ -19,6 +19,7 @@ import org.jboss.tools.batch.core.IBatchArtifact; import org.jboss.tools.batch.core.IBatchProperty; import org.jboss.tools.batch.internal.core.impl.BatchUtil.AttrReferencesRequestor; +import org.jboss.tools.batch.internal.core.impl.BatchUtil.TextSourceReference; import org.jboss.tools.batch.internal.core.impl.definition.FieldDefinition; import org.jboss.tools.common.java.IAnnotationDeclaration; import org.jboss.tools.common.text.ITextSourceReference; @@ -82,9 +83,9 @@ public Collection getReferences() { + "/*[name()=\"" + BatchConstants.TAG_PROPERTIES + "\"]" + "/*[name()=\"" + BatchConstants.TAG_PROPERTY + "\" and @" + BatchConstants.ATTR_NAME + "=\"" + getPropertyName() +"\"]" + "/@" + BatchConstants.ATTR_NAME; - AttrReferencesRequestor requestor = new AttrReferencesRequestor(file, expression); + AttrReferencesRequestor requestor = new AttrReferencesRequestor(file, expression, TextSourceReference.class); BatchUtil.scanXMLFile(file, requestor); - result.addAll(requestor.getResults()); + result.addAll((Collection) requestor.getResults()); } return result; } diff --git a/batch/plugins/org.jboss.tools.batch.core/src/org/jboss/tools/batch/internal/core/impl/BatchUtil.java b/batch/plugins/org.jboss.tools.batch.core/src/org/jboss/tools/batch/internal/core/impl/BatchUtil.java index a22c4b14fa..fc9ee760a2 100644 --- a/batch/plugins/org.jboss.tools.batch.core/src/org/jboss/tools/batch/internal/core/impl/BatchUtil.java +++ b/batch/plugins/org.jboss.tools.batch.core/src/org/jboss/tools/batch/internal/core/impl/BatchUtil.java @@ -12,8 +12,10 @@ import java.io.File; import java.io.IOException; +import java.util.ArrayList; import java.util.Collection; -import java.util.HashSet; +import java.util.HashMap; +import java.util.List; import javax.xml.xpath.XPath; import javax.xml.xpath.XPathConstants; @@ -39,9 +41,11 @@ import org.jboss.tools.common.EclipseUtil; import org.jboss.tools.common.text.ITextSourceReference; import org.jboss.tools.common.zip.UnzipOperation; +import org.jboss.tools.jst.web.kb.KbQuery.Tag; import org.jboss.tools.jst.web.kb.WebKbPlugin; import org.osgi.framework.Bundle; import org.w3c.dom.Document; +import org.w3c.dom.NamedNodeMap; import org.w3c.dom.Node; import org.w3c.dom.NodeList; @@ -102,9 +106,9 @@ public static void scanXMLFile(IFile file, DocumentScanner scanner) { * @param value * @return */ - public static Collection getAttributeReferences(IFile file, String name, String value) { + public static List getAttributeReferences(IFile file, String name, String value) { String expression = "//*[@" + name + "=\"" + value + "\"]/@" + name; - AttrReferencesRequestor requestor = new AttrReferencesRequestor(file, expression); + AttrReferencesRequestor requestor = new AttrReferencesRequestor(file, expression, TextSourceReference.class); scanXMLFile(file, requestor); return requestor.results; } @@ -117,24 +121,42 @@ public static Collection getAttributeReferences(IFile file * @param nameValue value of name attribute * @return */ - public static Collection getPropertyAttributeReferences(IFile file, String refValue, String propertyName) { + public static List getPropertyAttributeReferences(IFile file, String refValue, String propertyName) { String expression = "//*[@"+BatchConstants.ATTR_REF+"=\""+refValue+"\"]//*[@" + BatchConstants.ATTR_NAME + "=\"" + propertyName + "\"]/@" + BatchConstants.ATTR_NAME; - AttrReferencesRequestor requestor = new AttrReferencesRequestor(file, expression); + AttrReferencesRequestor requestor = new AttrReferencesRequestor(file, expression, TextSourceReference.class); scanXMLFile(file, requestor); return requestor.results; } - public static class AttrReferencesRequestor implements DocumentScanner { + /** + * Returns collection of text source references in xml file to name attribute of property tag by refValue and nameValue + * + * @param file + * @param refValue value of ref attribute + * @param nameValue value of name attribute + * @return + */ + public static List getNodePathPropertyAttributeReferences(IFile file, String refValue, String propertyName) { + String expression = "//*[@"+BatchConstants.ATTR_REF+"=\""+refValue+"\"]//*[@" + BatchConstants.ATTR_NAME + + "=\"" + propertyName + "\"]/@" + BatchConstants.ATTR_NAME; + AttrReferencesRequestor requestor = new AttrReferencesRequestor(file, expression, NodePathTextSourceReference.class); + scanXMLFile(file, requestor); + return requestor.results; + } + + public static class AttrReferencesRequestor implements DocumentScanner { IFile file; String expression; - Collection results = new HashSet(); + List results = new ArrayList(); + Class cls; - public AttrReferencesRequestor(IFile file, String expression) { + public AttrReferencesRequestor(IFile file, String expression, Class cls) { this.file = file; this.expression = expression; + this.cls = cls; } - + @Override public void scanDocument(Document document) { XPath xPath = XPathFactory.newInstance().newXPath(); @@ -154,21 +176,19 @@ public void scanDocument(Document document) { } final int start = start0; final int length = length0; - ITextSourceReference ref = new ITextSourceReference() { - @Override - public int getStartPosition() { - return start; - } - @Override - public IResource getResource() { - return file; - } - @Override - public int getLength() { - return length; - } - }; - results.add(ref); + try{ + E ref = cls.newInstance(); + ref.setLength(length); + ref.setResource(file); + ref.setStartPosition(start); + ref.setNodePath(n); + + results.add(ref); + }catch(InstantiationException e){ + + }catch(IllegalAccessException e){ + + } } } } @@ -177,11 +197,37 @@ public int getLength() { } } - public Collection getResults() { + public List getResults() { return results; } } + + private static List getNodePath(Node node){ + ArrayList tags = new ArrayList(); + Node n = node; + if(n instanceof AttrImpl){ + n = ((AttrImpl) node).getOwnerElement(); + } + + while(!(n instanceof IDOMDocument) && n != null){ + HashMap attributes = new HashMap(); + if(n.hasAttributes()){ + + NamedNodeMap attrs = n.getAttributes(); + for(int index = 0; index < attrs.getLength(); index++){ + Node attribute = attrs.item(index); + attributes.put(attribute.getNodeName(), attribute.getNodeValue()); + } + + } + Tag tag = new Tag(n.getNodeName(), attributes); + tags.add(tag); + n = n.getParentNode(); + } + return tags; + } + private static File TEMPLATE_FOLDER; public static File getTemplatesFolder() throws IOException { @@ -226,4 +272,57 @@ public static IPath getBatchXMLPath(IProject p) { } return result; } + + public static class TextSourceReference implements ITextSourceReference{ + private IResource resource; + int startPosition; + int length; + + public TextSourceReference(){ + } + + @Override + public int getStartPosition() { + return startPosition; + } + + @Override + public int getLength() { + return length; + } + + @Override + public IResource getResource() { + return resource; + } + + public void setResource(IResource resource) { + this.resource = resource; + } + + public void setStartPosition(int startPosition) { + this.startPosition = startPosition; + } + + public void setLength(int length) { + this.length = length; + } + + public void setNodePath(Node node){ + + } + + } + + public static class NodePathTextSourceReference extends TextSourceReference{ + private List tags; + + public Collection getNodePath(){ + return tags; + } + + public void setNodePath(Node node){ + this.tags = BatchUtil.getNodePath(node); + } + } } diff --git a/batch/plugins/org.jboss.tools.batch.ui/plugin.xml b/batch/plugins/org.jboss.tools.batch.ui/plugin.xml index 6189464daa..dd32798964 100644 --- a/batch/plugins/org.jboss.tools.batch.ui/plugin.xml +++ b/batch/plugins/org.jboss.tools.batch.ui/plugin.xml @@ -77,6 +77,15 @@ targetId="org.eclipse.core.runtime.xml"> + + + + diff --git a/batch/plugins/org.jboss.tools.batch.ui/src/org/jboss/tools/batch/ui/editor/internal/model/JobXMLEditor.java b/batch/plugins/org.jboss.tools.batch.ui/src/org/jboss/tools/batch/ui/editor/internal/model/JobXMLEditor.java index a21a0f2577..01e4f412eb 100644 --- a/batch/plugins/org.jboss.tools.batch.ui/src/org/jboss/tools/batch/ui/editor/internal/model/JobXMLEditor.java +++ b/batch/plugins/org.jboss.tools.batch.ui/src/org/jboss/tools/batch/ui/editor/internal/model/JobXMLEditor.java @@ -114,4 +114,9 @@ private void preferFlyoutPalette() { // closes the view which activates the flyout composite workbenchPage.hideView(paletteView); } + + @Override + public void switchToSourceTab() { + setActiveEditor(schemaSourceEditor); + } } diff --git a/batch/plugins/org.jboss.tools.batch.ui/src/org/jboss/tools/batch/ui/hyperlink/BatchHyperlinkMessages.java b/batch/plugins/org.jboss.tools.batch.ui/src/org/jboss/tools/batch/ui/hyperlink/BatchHyperlinkMessages.java index c336d42a90..9974798ef9 100644 --- a/batch/plugins/org.jboss.tools.batch.ui/src/org/jboss/tools/batch/ui/hyperlink/BatchHyperlinkMessages.java +++ b/batch/plugins/org.jboss.tools.batch.ui/src/org/jboss/tools/batch/ui/hyperlink/BatchHyperlinkMessages.java @@ -18,6 +18,9 @@ public class BatchHyperlinkMessages extends NLS{ public static String OPEN_JAVA_CLASS; public static String GO_TO_NODE; public static String OPEN_JAVA_FIELD; + public static String SHOW_ALL_BATCH_PROPERTY_REFERENCES; + public static String BATCH_PROPERTY_REFERENCES; + public static String SHOWING_REFERENCES_FOR_BATCH_PROPERTY; static { NLS.initializeMessages(BUNDLE_NAME, BatchHyperlinkMessages.class); diff --git a/batch/plugins/org.jboss.tools.batch.ui/src/org/jboss/tools/batch/ui/hyperlink/BatchHyperlinkMessages.properties b/batch/plugins/org.jboss.tools.batch.ui/src/org/jboss/tools/batch/ui/hyperlink/BatchHyperlinkMessages.properties index 5511476e44..cae201c19c 100644 --- a/batch/plugins/org.jboss.tools.batch.ui/src/org/jboss/tools/batch/ui/hyperlink/BatchHyperlinkMessages.properties +++ b/batch/plugins/org.jboss.tools.batch.ui/src/org/jboss/tools/batch/ui/hyperlink/BatchHyperlinkMessages.properties @@ -12,3 +12,6 @@ OPEN_JAVA_CLASS=Open Class ''{0}'' GO_TO_NODE=Go to ''<{0} id=\"{1}\">'' OPEN_JAVA_FIELD=Open Field ''{0}'' +SHOW_ALL_BATCH_PROPERTY_REFERENCES=Show All Batch Property References +BATCH_PROPERTY_REFERENCES=Batch Property References +SHOWING_REFERENCES_FOR_BATCH_PROPERTY=Showing references for Batch Property: ''{0}'' diff --git a/batch/plugins/org.jboss.tools.batch.ui/src/org/jboss/tools/batch/ui/hyperlink/BatchPropertyDialog.java b/batch/plugins/org.jboss.tools.batch.ui/src/org/jboss/tools/batch/ui/hyperlink/BatchPropertyDialog.java new file mode 100644 index 0000000000..7155365205 --- /dev/null +++ b/batch/plugins/org.jboss.tools.batch.ui/src/org/jboss/tools/batch/ui/hyperlink/BatchPropertyDialog.java @@ -0,0 +1,455 @@ +/******************************************************************************* + * Copyright (c) 2015 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.batch.ui.hyperlink; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.eclipse.core.resources.IFile; +import org.eclipse.jface.dialogs.Dialog; +import org.eclipse.jface.dialogs.PopupDialog; +import org.eclipse.jface.resource.JFaceResources; +import org.eclipse.jface.text.Region; +import org.eclipse.jface.viewers.DelegatingStyledCellLabelProvider; +import org.eclipse.jface.viewers.ILabelProvider; +import org.eclipse.jface.viewers.IOpenListener; +import org.eclipse.jface.viewers.ISelection; +import org.eclipse.jface.viewers.IStructuredSelection; +import org.eclipse.jface.viewers.ITreeContentProvider; +import org.eclipse.jface.viewers.OpenEvent; +import org.eclipse.jface.viewers.StructuredSelection; +import org.eclipse.jface.viewers.StyledCellLabelProvider; +import org.eclipse.jface.viewers.StyledString; +import org.eclipse.jface.viewers.StyledString.Styler; +import org.eclipse.jface.viewers.TreeViewer; +import org.eclipse.jface.viewers.Viewer; +import org.eclipse.jface.viewers.ViewerCell; +import org.eclipse.jface.viewers.ViewerFilter; +import org.eclipse.osgi.util.NLS; +import org.eclipse.swt.SWT; +import org.eclipse.swt.custom.BusyIndicator; +import org.eclipse.swt.events.FocusEvent; +import org.eclipse.swt.events.FocusListener; +import org.eclipse.swt.events.KeyEvent; +import org.eclipse.swt.events.KeyListener; +import org.eclipse.swt.events.ModifyEvent; +import org.eclipse.swt.events.ModifyListener; +import org.eclipse.swt.events.MouseAdapter; +import org.eclipse.swt.events.MouseEvent; +import org.eclipse.swt.events.MouseMoveListener; +import org.eclipse.swt.graphics.Color; +import org.eclipse.swt.graphics.Image; +import org.eclipse.swt.graphics.Point; +import org.eclipse.swt.graphics.Rectangle; +import org.eclipse.swt.graphics.TextStyle; +import org.eclipse.swt.layout.GridData; +import org.eclipse.swt.layout.GridLayout; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Control; +import org.eclipse.swt.widgets.Display; +import org.eclipse.swt.widgets.Shell; +import org.eclipse.swt.widgets.Text; +import org.eclipse.swt.widgets.Widget; +import org.eclipse.ui.dialogs.PatternFilter; +import org.jboss.tools.batch.core.BatchConstants; +import org.jboss.tools.batch.core.IBatchProperty; +import org.jboss.tools.batch.internal.core.impl.BatchUtil; +import org.jboss.tools.batch.internal.core.impl.BatchUtil.NodePathTextSourceReference; +import org.jboss.tools.batch.ui.JobImages; +import org.jboss.tools.common.text.ITextSourceReference; +import org.jboss.tools.common.text.ext.hyperlink.xpl.AbstractBaseHyperlink; +import org.jboss.tools.common.text.ext.util.StructuredSelectionHelper; +import org.jboss.tools.jst.web.kb.KbQuery.Tag; + +public class BatchPropertyDialog extends PopupDialog { + private IBatchProperty batchProperty; + private HashMap> references = new HashMap>(); + + private Composite composite; + + private Text fFilterText; + private TreeViewer tree; + private ReferencePatternFilter patternFilter; + + public BatchPropertyDialog(Shell parentShell, final IBatchProperty batchProperty) { + super(parentShell, 0, true, true, false, false, false, "", null); + this.batchProperty = batchProperty; + BusyIndicator.showWhile(parentShell.getDisplay(), + new Runnable() { + @Override + public void run() { + for (IFile file : batchProperty.getArtifact().getProject().getDeclaredBatchJobs()) { + List list = BatchUtil.getNodePathPropertyAttributeReferences(file, + batchProperty.getArtifact().getName(), + batchProperty.getPropertyName()); + if(list.size() > 0){ + references.put(file, list); + } + } + } + }); + } + + /** + * for test purpose + * @return + */ + public HashMap> getDisplayedReferences(){ + return references; + } + + private String computeTitle() { + StringBuffer result = new StringBuffer(); + + result.append(NLS.bind(BatchHyperlinkMessages.SHOWING_REFERENCES_FOR_BATCH_PROPERTY, batchProperty.getPropertyName())); + + return result.toString(); + } + + @Override + protected Control getFocusControl() { + return getFilterText(); + } + + @Override + protected Control createTitleMenuArea(Composite parent) { + Composite fViewMenuButtonComposite= (Composite) super.createTitleMenuArea(parent); + fFilterText = createFilterText(parent); + return fViewMenuButtonComposite; + } + + @Override + protected void configureShell(Shell newShell) { + super.configureShell(newShell); + newShell.setText(BatchHyperlinkMessages.BATCH_PROPERTY_REFERENCES); + } + + @Override + protected Control createDialogArea(Composite parent) { + composite = new Composite(parent, SWT.NONE); + GridLayout layout = new GridLayout(2, false); + layout.marginHeight = 0; + layout.marginWidth = 0; + layout.verticalSpacing = 0; + layout.horizontalSpacing = 0; + composite.setLayout(layout); + composite.setLayoutData(new GridData(GridData.FILL_BOTH)); + composite.setFont(parent.getFont()); + + setTitleText(computeTitle()); + createTreeView(composite); + composite.addFocusListener(new FocusListener(){ + @Override + public void focusGained(FocusEvent e) { + } + @Override + public void focusLost(FocusEvent e) { + close(); + } + }); + installFilter(); + return composite; + } + + + void createTreeView(Composite parent) { + tree = new TreeViewer(parent, SWT.SINGLE | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER); + tree.setAutoExpandLevel(TreeViewer.ALL_LEVELS); + GridData g = new GridData(GridData.FILL_BOTH); + + tree.getControl().setLayoutData(g); + tree.setContentProvider(new TreeContent()); + tree.setLabelProvider(new LabelProvider()); + tree.setInput(batchProperty); + patternFilter = new ReferencePatternFilter(); + tree.setFilters(new ViewerFilter[]{patternFilter}); + tree.addOpenListener(new IOpenListener() { + @Override + public void open(OpenEvent event) { + ISelection s = event.getSelection(); + if(!s.isEmpty() && s instanceof IStructuredSelection) { + Object o = ((IStructuredSelection)s).getFirstElement(); + if(o instanceof NodePathTextSourceReference) { + openElement((NodePathTextSourceReference) o); + close(); + } + } + } + }); + + tree.getTree().addMouseMoveListener(new MouseMoveListener() { + @Override + public void mouseMove(MouseEvent e) { + ViewerCell cell = tree.getCell(new Point(e.x, e.y)); + if(cell != null) { + Widget w = cell.getItem(); + if(w != null && w.getData() != null) { + tree.setSelection(new StructuredSelection(w.getData())); + } + } + } + }); + tree.getTree().addMouseListener(new MouseAdapter() { + @Override + public void mouseUp(MouseEvent e) { + gotoSelectedElement(); + } + }); + + + tree.refresh(); + } + + protected void gotoSelectedElement() { + ISelection s = tree.getSelection(); + if(!s.isEmpty() && s instanceof IStructuredSelection) { + Object o = ((IStructuredSelection)s).getFirstElement(); + if(o instanceof NodePathTextSourceReference) { + openElement((NodePathTextSourceReference) o); + close(); + } + } + } + + + protected Text getFilterText() { + return fFilterText; + } + + protected Text createFilterText(Composite parent) { + fFilterText= new Text(parent, SWT.NONE); + Dialog.applyDialogFont(fFilterText); + + GridData data= new GridData(GridData.FILL_HORIZONTAL); + data.horizontalAlignment= GridData.FILL; + data.verticalAlignment= GridData.CENTER; + fFilterText.setLayoutData(data); + + + fFilterText.addKeyListener(new KeyListener() { + @Override + public void keyPressed(KeyEvent e) { + if (e.keyCode == 0x0D) // return + gotoSelectedElement(); + if (e.keyCode == SWT.ARROW_DOWN) + tree.getTree().setFocus(); + if (e.keyCode == SWT.ARROW_UP) + tree.getTree().setFocus(); + if (e.character == 0x1B) // ESC + close(); + } + @Override + public void keyReleased(KeyEvent e) { + // do nothing + } + }); + return fFilterText; + } + + private void installFilter() { + fFilterText.setText(""); //$NON-NLS-1$ + + fFilterText.addModifyListener(new ModifyListener() { + @Override + public void modifyText(ModifyEvent e) { + //textFilter.filter(); + patternFilter.setPattern(fFilterText.getText()); + tree.refresh(); + tree.expandAll(); + } + }); + } + + @Override + protected Point getDefaultSize() { + return new Point(700, 300); + } + + @Override + protected Point getDefaultLocation(Point size) { + Display display = Display.getCurrent(); + if(display == null) { + display = Display.getDefault(); + } + if(display.getActiveShell() == null) { + return super.getDefaultLocation(size); + } + Rectangle b = display.getActiveShell().getBounds(); + int x = b.x + (b.width - size.x) / 2; + int y = b.y + (b.height - size.y) / 2; + return new Point(x, y); + } + + class ReferencePatternFilter extends PatternFilter{ + protected boolean isLeafMatch(Viewer viewer, Object element){ + if(element instanceof NodePathTextSourceReference){ + boolean parentMatch = super.isLeafMatch(viewer, ((NodePathTextSourceReference) element).getResource()); + if(parentMatch){ + return true; + } + } + return super.isLeafMatch(viewer, element); + } + } + + class TreeContent implements ITreeContentProvider { + + @Override + public void dispose() { + } + + @Override + public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { + } + + @Override + public Object[] getElements(Object inputElement) { + return getChildren(inputElement); + } + + @Override + public Object[] getChildren(Object parentElement) { + if(parentElement instanceof IBatchProperty){ + return references.keySet().toArray(); + }else if(parentElement instanceof IFile){ + return references.get((IFile)parentElement).toArray(); + } + return null; + } + + @Override + public Object getParent(Object element) { + if(element instanceof NodePathTextSourceReference){ + return ((ITextSourceReference) element).getResource(); + } + return null; + } + + @Override + public boolean hasChildren(Object element) { + if(element instanceof IBatchProperty){ + return references.keySet().size() > 0; + }else if(element instanceof IFile){ + return references.get((IFile)element).size() > 0; + } + return false; + } + + } + + static Color gray = new Color(null, 128, 128, 128); + static Color black = Display.getDefault().getSystemColor(SWT.COLOR_INFO_FOREGROUND); + + static Styler FILE_NAME_STYLER = new DefaultStyler(black, true, false); + static Styler PATH_STYLER = new DefaultStyler(gray, true, false); + static Styler PROPERTY_PATH_STYLER = new DefaultStyler(black, false, false); + + private static class DefaultStyler extends Styler { + private Color foreground; + private boolean bold; + private boolean italic; + + public DefaultStyler(Color foreground, boolean bold, boolean italic) { + this.foreground = foreground; + this.italic = italic; + this.bold = bold; + } + + @Override + public void applyStyles(TextStyle textStyle) { + if (foreground != null) { + textStyle.foreground = foreground; + } + if(italic) { + textStyle.font = JFaceResources.getFontRegistry().getItalic(JFaceResources.DEFAULT_FONT); + } + if(bold) { + textStyle.font = JFaceResources.getFontRegistry().getBold(JFaceResources.DEFAULT_FONT); + } + } + } + + class LabelProvider extends StyledCellLabelProvider implements DelegatingStyledCellLabelProvider.IStyledLabelProvider, ILabelProvider { + @Override + public void update(ViewerCell cell) { + Object element = cell.getElement(); + StyledString styledString = getStyledText(element); + cell.setText(styledString.getString()); + cell.setStyleRanges(styledString.getStyleRanges()); + cell.setImage(getImage(element)); + + super.update(cell); + } + + public String getText(Object element) { + return getStyledText(element).getString(); + } + + @Override + public StyledString getStyledText(Object element) { + if(element instanceof IFile){ + StyledString sb = new StyledString(); + + sb.append(((IFile)element).getName(), FILE_NAME_STYLER).append(" - "); + + sb.append(((IFile)element).getParent().getFullPath().toString(), PATH_STYLER); + + return sb; + }else if(element instanceof NodePathTextSourceReference){ + return getNodePath((NodePathTextSourceReference)element); + } + return null; + } + + @Override + public Image getImage(Object element) { + if(element instanceof IFile){ + return JobImages.getImage(JobImages.JOB_IMAGE); + }else if(element instanceof NodePathTextSourceReference){ + return JobImages.getImage(JobImages.PROPERTY_IMAGE); + } + return null; + } + } + + private static StyledString getNodePath(NodePathTextSourceReference element){ + StyledString ss = new StyledString(); + List tags = (List) element.getNodePath(); + for(int index = tags.size()-1; index >= 0; index--){ + Tag tag = tags.get(index); + if(BatchConstants.TAG_JOB.equalsIgnoreCase(tag.getName())){ + continue; + } + Map attributes = tag.getAttributes(); + String attributeValue = null; + String attributeName = BatchConstants.ATTR_ID; + attributeValue = attributes.get(attributeName); + if(attributeValue == null){ + attributeName = BatchConstants.ATTR_REF; + attributeValue = attributes.get(attributeName); + } + ss.append("/", PROPERTY_PATH_STYLER); + ss.append(tag.getName(), PROPERTY_PATH_STYLER); + if(attributeValue != null){ + ss.append("(", PATH_STYLER ).append(attributeName, PATH_STYLER).append("=\"", PATH_STYLER).append(attributeValue, PATH_STYLER).append("\")", PATH_STYLER); + } + + } + return ss; + } + + private void openElement(NodePathTextSourceReference reference){ + AbstractBaseHyperlink.openFileInEditor((IFile)reference.getResource()); + + StructuredSelectionHelper.setSelectionAndRevealInActiveEditor(new Region(reference.getStartPosition(), reference.getLength())); + } +} \ No newline at end of file diff --git a/batch/plugins/org.jboss.tools.batch.ui/src/org/jboss/tools/batch/ui/hyperlink/BatchPropertyHyperlink.java b/batch/plugins/org.jboss.tools.batch.ui/src/org/jboss/tools/batch/ui/hyperlink/BatchPropertyHyperlink.java new file mode 100644 index 0000000000..f495fdb350 --- /dev/null +++ b/batch/plugins/org.jboss.tools.batch.ui/src/org/jboss/tools/batch/ui/hyperlink/BatchPropertyHyperlink.java @@ -0,0 +1,42 @@ +/******************************************************************************* + * Copyright (c) 2011-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.batch.ui.hyperlink; + +import org.eclipse.jface.text.IDocument; +import org.eclipse.jface.text.IRegion; +import org.eclipse.swt.widgets.Display; +import org.jboss.tools.batch.core.IBatchProperty; +import org.jboss.tools.common.text.ext.hyperlink.AbstractHyperlink; + +public class BatchPropertyHyperlink extends AbstractHyperlink{ + protected IBatchProperty batchProperty; + + public BatchPropertyHyperlink(IRegion region, IBatchProperty batchProperty, IDocument document){ + this.batchProperty = batchProperty; + setRegion(region); + setDocument(document); + } + + protected void doHyperlink(IRegion region) { + Display display = Display.getCurrent(); + if(display == null) { + display = Display.getDefault(); + } + BatchPropertyDialog dialog = new BatchPropertyDialog(display.getActiveShell(), batchProperty); + dialog.open(); + } + + @Override + public String getHyperlinkText() { + return BatchHyperlinkMessages.SHOW_ALL_BATCH_PROPERTY_REFERENCES; + } + +} diff --git a/batch/plugins/org.jboss.tools.batch.ui/src/org/jboss/tools/batch/ui/hyperlink/BatchPropertyHyperlinkDetector.java b/batch/plugins/org.jboss.tools.batch.ui/src/org/jboss/tools/batch/ui/hyperlink/BatchPropertyHyperlinkDetector.java new file mode 100644 index 0000000000..c357280f9d --- /dev/null +++ b/batch/plugins/org.jboss.tools.batch.ui/src/org/jboss/tools/batch/ui/hyperlink/BatchPropertyHyperlinkDetector.java @@ -0,0 +1,103 @@ +package org.jboss.tools.batch.ui.hyperlink; + +import java.util.ArrayList; +import java.util.List; + +import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.IProject; +import org.eclipse.jdt.core.IField; +import org.eclipse.jdt.core.IJavaElement; +import org.eclipse.jdt.core.IType; +import org.eclipse.jdt.core.ITypeRoot; +import org.eclipse.jdt.core.JavaModelException; +import org.eclipse.jdt.internal.ui.javaeditor.EditorUtility; +import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor; +import org.eclipse.jdt.internal.ui.text.JavaWordFinder; +import org.eclipse.jface.text.IDocument; +import org.eclipse.jface.text.IRegion; +import org.eclipse.jface.text.ITextViewer; +import org.eclipse.jface.text.hyperlink.AbstractHyperlinkDetector; +import org.eclipse.jface.text.hyperlink.IHyperlink; +import org.eclipse.ui.IFileEditorInput; +import org.eclipse.ui.texteditor.ITextEditor; +import org.jboss.tools.batch.core.IBatchArtifact; +import org.jboss.tools.batch.core.IBatchProperty; +import org.jboss.tools.batch.internal.core.impl.BatchProject; +import org.jboss.tools.batch.internal.core.impl.BatchProjectFactory; +import org.jboss.tools.batch.ui.BatchUIPlugin; + +public class BatchPropertyHyperlinkDetector extends AbstractHyperlinkDetector { + protected IFile file; + + @Override + public IHyperlink[] detectHyperlinks(ITextViewer textViewer, IRegion region, + boolean canShowMultipleHyperlinks) { + List links = new ArrayList(); + + ITextEditor textEditor= (ITextEditor)getAdapter(ITextEditor.class); + if (region == null || !(textEditor instanceof JavaEditor)) + return null; + + if(textEditor.getEditorInput() instanceof IFileEditorInput){ + file = ((IFileEditorInput)textEditor.getEditorInput()).getFile(); + } + + int offset= region.getOffset(); + + ITypeRoot input= EditorUtility.getEditorInputJavaElement(textEditor, true); + if (input == null) + return null; + + IDocument document= textEditor.getDocumentProvider().getDocument(textEditor.getEditorInput()); + IRegion wordRegion= JavaWordFinder.findWord(document, offset); + if (wordRegion == null) + return null; + + IProject project = input.getJavaProject().getProject(); + + BatchProject batchProject = (BatchProject) BatchProjectFactory.getBatchProjectWithProgress(project); + + if (batchProject == null) { + return null; + } + + IJavaElement[] elements = null; + + try { + elements = input.codeSelect(wordRegion.getOffset(), wordRegion.getLength()); + + if(elements.length != 1) + return null; + + ArrayList hyperlinks = new ArrayList(); + + IField field = null; + + if(elements[0] instanceof IType){ + elements[0] = input.getElementAt(wordRegion.getOffset()); + } + + if(elements[0] instanceof IField){ + field = (IField) elements[0]; + } + + if(field != null){ + IType type = field.getDeclaringType(); + IBatchArtifact artifact = batchProject.getArtifact(type); + if (artifact != null) { + IBatchProperty property = artifact.getProperty(field); + if(property != null){ + hyperlinks.add(new BatchPropertyHyperlink(region, property, document)); + } + } + } + if (hyperlinks != null && !hyperlinks.isEmpty()) { + return (IHyperlink[])hyperlinks.toArray(new IHyperlink[hyperlinks.size()]); + } + } catch (JavaModelException jme) { + BatchUIPlugin.getDefault().logError(jme); + } + return null; + } + +}