nitind | 102e60a | 2005-09-15 12:42:42 +0000 | [diff] [blame] | 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2005 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 | |
| 13 | package org.eclipse.wst.css.ui.internal.selection; |
| 14 | |
| 15 | import org.eclipse.jface.action.IAction; |
| 16 | import org.eclipse.jface.text.IDocument; |
| 17 | import org.eclipse.jface.text.ITextSelection; |
| 18 | import org.eclipse.jface.text.Region; |
| 19 | import org.eclipse.wst.css.core.internal.provisional.document.ICSSNode; |
| 20 | import org.eclipse.wst.css.ui.internal.CSSUIMessages; |
| 21 | import org.eclipse.wst.sse.core.internal.provisional.IndexedRegion; |
| 22 | import org.eclipse.wst.sse.ui.internal.selection.StructuredSelectActionDelegate; |
| 23 | |
| 24 | /** |
| 25 | * Selects enclosing CSS element |
| 26 | */ |
| 27 | public class StructuredSelectEnclosingCSSActionDelegate extends StructuredSelectActionDelegate { |
| 28 | public void init(IAction action) { |
| 29 | if (action != null) { |
| 30 | action.setText(CSSUIMessages.StructureSelectEnclosing_label); |
| 31 | action.setToolTipText(CSSUIMessages.StructureSelectEnclosing_tooltip); |
| 32 | action.setDescription(CSSUIMessages.StructureSelectEnclosing_description); |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | protected IndexedRegion getCursorIndexedRegion(IDocument document, ITextSelection textSelection) { |
| 37 | IndexedRegion indexedRegion = null; |
| 38 | |
| 39 | indexedRegion = getIndexedRegion(document, textSelection.getOffset()); |
| 40 | |
| 41 | return indexedRegion; |
| 42 | } |
| 43 | |
| 44 | protected Region getNewSelectionRegion(IndexedRegion indexedRegion, ITextSelection textSelection) { |
| 45 | Region newRegion = null; |
| 46 | if (indexedRegion instanceof ICSSNode) { |
| 47 | ICSSNode cursorNode = (ICSSNode) indexedRegion; |
| 48 | Region cursorNodeRegion = new Region(indexedRegion.getStartOffset(), indexedRegion.getEndOffset() - indexedRegion.getStartOffset()); |
| 49 | int currentOffset = textSelection.getOffset(); |
| 50 | int currentEndOffset = currentOffset + textSelection.getLength(); |
| 51 | if (cursorNodeRegion.getOffset() >= currentOffset && cursorNodeRegion.getOffset() <= currentEndOffset && cursorNodeRegion.getOffset() + cursorNodeRegion.getLength() >= currentOffset && cursorNodeRegion.getOffset() + cursorNodeRegion.getLength() <= currentEndOffset) { |
| 52 | ICSSNode newNode = cursorNode.getParentNode(); |
| 53 | |
| 54 | if (newNode instanceof IndexedRegion) { |
| 55 | IndexedRegion newIndexedRegion = (IndexedRegion) newNode; |
| 56 | newRegion = new Region(newIndexedRegion.getStartOffset(), newIndexedRegion.getEndOffset() - newIndexedRegion.getStartOffset()); |
| 57 | } |
| 58 | } |
| 59 | else |
| 60 | newRegion = cursorNodeRegion; |
| 61 | } |
| 62 | return newRegion; |
| 63 | } |
| 64 | } |