KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > plugin > ExtensionAttributePointDectector


1 /*******************************************************************************
2  * Copyright (c) 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.pde.internal.ui.editor.plugin;
13
14 import org.eclipse.jface.text.Region;
15 import org.eclipse.jface.text.source.ISourceViewer;
16 import org.eclipse.jface.viewers.ISelection;
17 import org.eclipse.jface.viewers.StructuredSelection;
18 import org.eclipse.pde.core.plugin.IPluginExtension;
19 import org.eclipse.pde.core.plugin.IPluginObject;
20 import org.eclipse.pde.internal.core.ischema.ISchemaAttribute;
21 import org.eclipse.pde.internal.core.schema.SchemaRootElement;
22 import org.eclipse.pde.internal.core.text.IDocumentAttribute;
23 import org.eclipse.pde.internal.core.text.IDocumentNode;
24 import org.eclipse.pde.internal.core.text.IDocumentRange;
25 import org.eclipse.pde.internal.ui.editor.text.XMLUtil;
26 import org.eclipse.swt.custom.StyledText;
27 import org.eclipse.swt.events.KeyEvent;
28 import org.eclipse.swt.events.KeyListener;
29 import org.eclipse.swt.events.MouseEvent;
30 import org.eclipse.swt.events.MouseListener;
31 import org.eclipse.swt.graphics.Point;
32
33 /**
34  * ExtensionAttributePointDectector
35  *
36  */

37 public class ExtensionAttributePointDectector implements MouseListener,
38         KeyListener {
39
40     private ManifestSourcePage fSourcePage;
41     
42     private StyledText fStyledText;
43     
44     private ISelection fSelection;
45     
46     /**
47      *
48      */

49     public ExtensionAttributePointDectector() {
50         fSelection = null;
51         fStyledText = null;
52         fSourcePage = null;
53     }
54     
55     /**
56      * @return
57      */

58     public ISelection getSelection() {
59         return fSelection;
60     }
61     
62     /**
63      * @param editor
64      */

65     public void setTextEditor(ManifestSourcePage editor) {
66         fSourcePage = editor;
67         // Get the new styled text widget
68
ISourceViewer viewer = fSourcePage.getViewer();
69         // If the source viewer is not initialized, we can't do anything here
70
if (viewer == null) {
71             return;
72         }
73         StyledText newStyledText = viewer.getTextWidget();
74         // If the new styled text equals the old one, keep the old one
75
if ((fStyledText != null) &&
76                 fStyledText.equals(newStyledText)) {
77             return;
78         }
79         // Remove the listeners on the old styled text
80
removeListeners();
81         // Replace the old with the new
82
fStyledText = newStyledText;
83         // Add the listeners to the new styled text
84
addListeners();
85         // Enable this action if the selection is within the point attribute on
86
// an extension
87
checkIfOnTarget();
88     }
89     
90     /**
91      * @return
92      */

93     private void checkIfOnTarget() {
94         // Reset the Selection
95
fSelection = null;
96         // Ensure the input is valid
97
if (isInputInitialized() == false) {
98             return;
99         }
100         // Get the region selected
101
Point selectionPoint = fStyledText.getSelection();
102         Region selectionRegion = new Region(selectionPoint.x,
103                 selectionPoint.y - selectionPoint.x);
104         // Determine whether the region selected is the point attribute of
105
// and extension
106
if ((selectionRegion == null) ||
107                 (fSourcePage == null)) {
108             return;
109         }
110         // Retrieve the document range corresponding to the selection region
111
IDocumentRange element =
112             fSourcePage.getRangeElement(selectionRegion.getOffset(), true);
113         // Validate the obtained document range
114
if (XMLUtil.withinRange(element, selectionRegion.getOffset()) == false) {
115             return;
116         }
117         // Ensure we have a document attribute
118
if ((element instanceof IDocumentAttribute) == false) {
119             return;
120         }
121         // Ignore IDocumentNode
122
// Ignore IDocumentTextNode
123
IDocumentAttribute documentAttribute = ((IDocumentAttribute)element);
124         String JavaDoc attributeValue = documentAttribute.getAttributeValue();
125         // Ensure the attribute value is defined
126
if ((attributeValue == null) ||
127                 (attributeValue.length() == 0)) {
128             return;
129         }
130         // Get the parent node: either extension or extension point
131
IPluginObject node =
132             XMLUtil.getTopLevelParent((IDocumentNode)documentAttribute);
133         // Ensure the node is defined and comes from and editable model
134
if ((node == null) ||
135                 (node.getModel().isEditable() == false)) {
136             return;
137         }
138         // Ensure the node is an extension
139
if ((node instanceof IPluginExtension) == false) {
140             return;
141         }
142         // Ignore IPluginExtensionPoint
143
IPluginExtension extension = (IPluginExtension)node;
144         // Retrieve the corresponding schema attribute to this node
145
ISchemaAttribute schemaAttribute =
146             XMLUtil.getSchemaAttribute(documentAttribute,
147                     extension.getPoint());
148         // Ensure the schema attribute is defined
149
if (schemaAttribute == null) {
150             return;
151         }
152         // Ensure the attribute is a point
153
if (((schemaAttribute.getParent() instanceof SchemaRootElement) == false) ||
154                 (documentAttribute.getAttributeName().equals(IPluginExtension.P_POINT) == false)) {
155             return;
156         }
157         fSelection = new StructuredSelection(extension);
158     }
159
160     /**
161      *
162      */

163     private void removeListeners() {
164         if (isInputInitialized() == false) {
165             return;
166         }
167         fStyledText.removeMouseListener(this);
168         fStyledText.removeKeyListener(this);
169     }
170
171     /**
172      * @return
173      */

174     private boolean isInputInitialized() {
175         if ((fStyledText == null) ||
176                 (fStyledText.isDisposed())) {
177             return false;
178         }
179         return true;
180     }
181     
182     /**
183      *
184      */

185     private void addListeners() {
186         if (isInputInitialized() == false) {
187             return;
188         }
189         fStyledText.addMouseListener(this);
190         fStyledText.addKeyListener(this);
191     }
192
193     /* (non-Javadoc)
194      * @see org.eclipse.swt.events.MouseListener#mouseDoubleClick(org.eclipse.swt.events.MouseEvent)
195      */

196     public void mouseDoubleClick(MouseEvent e) {
197         // Ignore
198
}
199
200     /* (non-Javadoc)
201      * @see org.eclipse.swt.events.MouseListener#mouseDown(org.eclipse.swt.events.MouseEvent)
202      */

203     public void mouseDown(MouseEvent e) {
204         // Ignore
205
}
206
207     public void mouseUp(MouseEvent e) {
208         checkIfOnTarget();
209     }
210
211     public void keyPressed(KeyEvent e) {
212         checkIfOnTarget();
213     }
214
215     /* (non-Javadoc)
216      * @see org.eclipse.swt.events.KeyListener#keyReleased(org.eclipse.swt.events.KeyEvent)
217      */

218     public void keyReleased(KeyEvent e) {
219         // Ignore
220
}
221     
222 }
223
Popular Tags