blob: fa1a72016d8640ecd404d94817b2513c3431310b [file] [log] [blame]
nitind102e60a2005-09-15 12:42:42 +00001/*******************************************************************************
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
13package org.eclipse.wst.css.ui.internal.selection;
14
15import org.eclipse.jface.action.IAction;
16import org.eclipse.jface.text.IDocument;
17import org.eclipse.jface.text.ITextSelection;
18import org.eclipse.jface.text.Region;
19import org.eclipse.wst.css.core.internal.provisional.document.ICSSNode;
20import org.eclipse.wst.css.ui.internal.CSSUIMessages;
21import org.eclipse.wst.sse.core.internal.provisional.IndexedRegion;
22import org.eclipse.wst.sse.ui.internal.selection.StructuredSelectActionDelegate;
23
24/**
25 * Selects enclosing CSS element
26 */
27public 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}