001: /*******************************************************************************
002: * Copyright (c) 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.ui.editor.plugin;
011:
012: import org.eclipse.jface.text.Region;
013: import org.eclipse.jface.text.source.ISourceViewer;
014: import org.eclipse.jface.viewers.ISelection;
015: import org.eclipse.jface.viewers.StructuredSelection;
016: import org.eclipse.pde.core.plugin.IPluginExtension;
017: import org.eclipse.pde.core.plugin.IPluginObject;
018: import org.eclipse.pde.internal.core.ischema.ISchemaAttribute;
019: import org.eclipse.pde.internal.core.schema.SchemaRootElement;
020: import org.eclipse.pde.internal.core.text.IDocumentAttributeNode;
021: import org.eclipse.pde.internal.core.text.IDocumentElementNode;
022: import org.eclipse.pde.internal.core.text.IDocumentRange;
023: import org.eclipse.pde.internal.ui.editor.text.XMLUtil;
024: import org.eclipse.swt.custom.StyledText;
025: import org.eclipse.swt.events.KeyEvent;
026: import org.eclipse.swt.events.KeyListener;
027: import org.eclipse.swt.events.MouseEvent;
028: import org.eclipse.swt.events.MouseListener;
029: import org.eclipse.swt.graphics.Point;
030:
031: /**
032: * ExtensionAttributePointDectector
033: *
034: */
035: public class ExtensionAttributePointDectector implements MouseListener,
036: KeyListener {
037:
038: private ManifestSourcePage fSourcePage;
039:
040: private StyledText fStyledText;
041:
042: private ISelection fSelection;
043:
044: /**
045: *
046: */
047: public ExtensionAttributePointDectector() {
048: fSelection = null;
049: fStyledText = null;
050: fSourcePage = null;
051: }
052:
053: /**
054: * @return
055: */
056: public ISelection getSelection() {
057: return fSelection;
058: }
059:
060: /**
061: * @param editor
062: */
063: public void setTextEditor(ManifestSourcePage editor) {
064: fSourcePage = editor;
065: // Get the new styled text widget
066: ISourceViewer viewer = fSourcePage.getViewer();
067: // If the source viewer is not initialized, we can't do anything here
068: if (viewer == null) {
069: return;
070: }
071: StyledText newStyledText = viewer.getTextWidget();
072: // If the new styled text equals the old one, keep the old one
073: if ((fStyledText != null) && fStyledText.equals(newStyledText)) {
074: return;
075: }
076: // Remove the listeners on the old styled text
077: removeListeners();
078: // Replace the old with the new
079: fStyledText = newStyledText;
080: // Add the listeners to the new styled text
081: addListeners();
082: // Enable this action if the selection is within the point attribute on
083: // an extension
084: checkIfOnTarget();
085: }
086:
087: /**
088: * @return
089: */
090: private void checkIfOnTarget() {
091: // Reset the Selection
092: fSelection = null;
093: // Ensure the input is valid
094: if (isInputInitialized() == false) {
095: return;
096: }
097: // Get the region selected
098: Point selectionPoint = fStyledText.getSelection();
099: Region selectionRegion = new Region(selectionPoint.x,
100: selectionPoint.y - selectionPoint.x);
101: // Determine whether the region selected is the point attribute of
102: // and extension
103: if ((selectionRegion == null) || (fSourcePage == null)) {
104: return;
105: }
106: // Retrieve the document range corresponding to the selection region
107: IDocumentRange element = fSourcePage.getRangeElement(
108: selectionRegion.getOffset(), true);
109: // Validate the obtained document range
110: if (XMLUtil.withinRange(element, selectionRegion.getOffset()) == false) {
111: return;
112: }
113: // Ensure we have a document attribute
114: if ((element instanceof IDocumentAttributeNode) == false) {
115: return;
116: }
117: // Ignore IDocumentElementNode
118: // Ignore IDocumentTextNode
119: IDocumentAttributeNode documentAttribute = ((IDocumentAttributeNode) element);
120: String attributeValue = documentAttribute.getAttributeValue();
121: // Ensure the attribute value is defined
122: if ((attributeValue == null) || (attributeValue.length() == 0)) {
123: return;
124: }
125: // Get the parent node: either extension or extension point
126: IPluginObject node = XMLUtil
127: .getTopLevelParent((IDocumentElementNode) documentAttribute);
128: // Ensure the node is defined and comes from and editable model
129: if ((node == null) || (node.getModel().isEditable() == false)) {
130: return;
131: }
132: // Ensure the node is an extension
133: if ((node instanceof IPluginExtension) == false) {
134: return;
135: }
136: // Ignore IPluginExtensionPoint
137: IPluginExtension extension = (IPluginExtension) node;
138: // Retrieve the corresponding schema attribute to this node
139: ISchemaAttribute schemaAttribute = XMLUtil.getSchemaAttribute(
140: documentAttribute, extension.getPoint());
141: // Ensure the schema attribute is defined
142: if (schemaAttribute == null) {
143: return;
144: }
145: // Ensure the attribute is a point
146: if (((schemaAttribute.getParent() instanceof SchemaRootElement) == false)
147: || (documentAttribute.getAttributeName().equals(
148: IPluginExtension.P_POINT) == false)) {
149: return;
150: }
151: fSelection = new StructuredSelection(extension);
152: }
153:
154: /**
155: *
156: */
157: private void removeListeners() {
158: if (isInputInitialized() == false) {
159: return;
160: }
161: fStyledText.removeMouseListener(this );
162: fStyledText.removeKeyListener(this );
163: }
164:
165: /**
166: * @return
167: */
168: private boolean isInputInitialized() {
169: if ((fStyledText == null) || (fStyledText.isDisposed())) {
170: return false;
171: }
172: return true;
173: }
174:
175: /**
176: *
177: */
178: private void addListeners() {
179: if (isInputInitialized() == false) {
180: return;
181: }
182: fStyledText.addMouseListener(this );
183: fStyledText.addKeyListener(this );
184: }
185:
186: /* (non-Javadoc)
187: * @see org.eclipse.swt.events.MouseListener#mouseDoubleClick(org.eclipse.swt.events.MouseEvent)
188: */
189: public void mouseDoubleClick(MouseEvent e) {
190: // Ignore
191: }
192:
193: /* (non-Javadoc)
194: * @see org.eclipse.swt.events.MouseListener#mouseDown(org.eclipse.swt.events.MouseEvent)
195: */
196: public void mouseDown(MouseEvent e) {
197: // Ignore
198: }
199:
200: public void mouseUp(MouseEvent e) {
201: checkIfOnTarget();
202: }
203:
204: public void keyPressed(KeyEvent e) {
205: checkIfOnTarget();
206: }
207:
208: /* (non-Javadoc)
209: * @see org.eclipse.swt.events.KeyListener#keyReleased(org.eclipse.swt.events.KeyEvent)
210: */
211: public void keyReleased(KeyEvent e) {
212: // Ignore
213: }
214:
215: }
|