david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2001, 2004 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 | * Jens Lukowski/Innoopract - initial renaming/restructuring |
| 11 | * |
| 12 | *******************************************************************************/ |
nitind | d6e591d | 2005-03-14 22:21:57 +0000 | [diff] [blame] | 13 | package org.eclipse.wst.dtd.core.internal; |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 14 | |
| 15 | import java.util.ArrayList; |
| 16 | |
| 17 | import org.eclipse.swt.graphics.Image; |
nitind | d6e591d | 2005-03-14 22:21:57 +0000 | [diff] [blame] | 18 | import org.eclipse.wst.dtd.core.internal.parser.DTDRegionTypes; |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 19 | import org.eclipse.wst.dtd.core.internal.text.RegionIterator; |
david_williams | 4ad020f | 2005-04-18 08:00:30 +0000 | [diff] [blame] | 20 | import org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion; |
| 21 | import org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion; |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 22 | |
| 23 | |
| 24 | public class AttributeList extends NamedTopLevelNode { |
| 25 | public AttributeList(DTDFile file, IStructuredDocumentRegion flatNode) { |
| 26 | super(file, flatNode, DTDRegionTypes.ATTLIST_TAG); |
| 27 | } |
| 28 | |
| 29 | public void addAttribute(String name) { |
david_williams | 3804601 | 2005-04-08 19:04:29 +0000 | [diff] [blame] | 30 | beginRecording(this, DTDCoreMessages._UI_LABEL_ATTR_LIST_ADD); //$NON-NLS-1$ |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 31 | |
| 32 | DTDNode lastAttribute = (DTDNode) getLastChild(); |
| 33 | if (lastAttribute != null) { |
| 34 | replaceText(this, lastAttribute.getEndOffset(), 0, "\n\t" + name + " CDATA #IMPLIED"); //$NON-NLS-1$ //$NON-NLS-2$ |
nitind | d6e591d | 2005-03-14 22:21:57 +0000 | [diff] [blame] | 35 | } |
| 36 | else { |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 37 | ITextRegion nameRegion = getNameRegion(); |
| 38 | if (nameRegion != null) { |
david_williams | 29d6968 | 2004-11-20 17:49:29 +0000 | [diff] [blame] | 39 | replaceText(this, getStructuredDTDDocumentRegion().getEndOffset(nameRegion), 0, "\n\t" + name + " CDATA #IMPLIED"); //$NON-NLS-1$ //$NON-NLS-2$ |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | } |
| 43 | |
| 44 | endRecording(this); |
| 45 | } |
| 46 | |
| 47 | public Image getImage() { |
david_williams | 425ffe7 | 2004-12-07 21:46:39 +0000 | [diff] [blame] | 48 | return DTDCorePlugin.getInstance().getImage(DTDResource.ATTRIBUTELISTICON); |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | public void insertIntoModel(Object requestor, Attribute reference, Attribute node, boolean isAfter) { |
| 52 | int offset = 0; |
| 53 | String newText = ""; //$NON-NLS-1$ |
| 54 | String nodeText = node.getFullNodeText(); |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 55 | if (!isAfter) { |
| 56 | offset = reference.getStartOffset(); |
nitind | d6e591d | 2005-03-14 22:21:57 +0000 | [diff] [blame] | 57 | } |
| 58 | else { |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 59 | // try and get next child |
| 60 | Attribute attr = (Attribute) reference.getNextSibling(); |
| 61 | if (attr != null) { |
| 62 | offset = attr.getStartOffset(); |
nitind | d6e591d | 2005-03-14 22:21:57 +0000 | [diff] [blame] | 63 | } |
| 64 | else { |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 65 | // just use the end offset |
| 66 | offset = reference.getWhitespaceEndOffset(); |
| 67 | } |
| 68 | } |
| 69 | newText += nodeText;// + (isLastChild ? "\n" : "\n\t"); |
| 70 | if (!node.hasTrailingWhitespace()) { |
| 71 | newText += "\n\t"; //$NON-NLS-1$ |
| 72 | } |
| 73 | replaceText(requestor, offset, 0, newText); |
| 74 | } |
| 75 | |
| 76 | public void resolveRegions() { |
| 77 | removeChildNodes(); |
| 78 | RegionIterator iter = iterator(); |
| 79 | |
| 80 | if (getNameRegion() != null) { |
| 81 | // we skip past the name token is our name |
| 82 | skipPastName(iter); |
| 83 | } |
| 84 | |
| 85 | ArrayList children = new ArrayList(); |
| 86 | Attribute attribute = null; |
| 87 | boolean trailingWhitespace = false; |
| 88 | while (iter.hasNext()) { |
| 89 | ITextRegion currentRegion = iter.next(); |
| 90 | if (currentRegion.getType().equals(DTDRegionTypes.ATTRIBUTE_NAME)) { |
david_williams | 29d6968 | 2004-11-20 17:49:29 +0000 | [diff] [blame] | 91 | attribute = new Attribute(getDTDFile(), getStructuredDTDDocumentRegion()); |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 92 | children.add(attribute); |
| 93 | appendChild(attribute); |
| 94 | trailingWhitespace = false; |
| 95 | } |
| 96 | if (attribute != null && currentRegion.getType() != DTDRegionTypes.END_TAG) { |
| 97 | if (!trailingWhitespace) { |
| 98 | attribute.addRegion(currentRegion); |
nitind | d6e591d | 2005-03-14 22:21:57 +0000 | [diff] [blame] | 99 | } |
| 100 | else { |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 101 | if (currentRegion.getType() == DTDRegionTypes.WHITESPACE) { |
| 102 | attribute.addWhitespaceRegion(currentRegion); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | // the following prevents extra whitespace from being picked |
| 107 | // up by the attribute |
| 108 | if (currentRegion.getType() == DTDRegionTypes.REQUIRED_KEYWORD || currentRegion.getType() == DTDRegionTypes.IMPLIED_KEYWORD || currentRegion.getType() == DTDRegionTypes.SINGLEQUOTED_LITERAL || currentRegion.getType() == DTDRegionTypes.DOUBLEQUOTED_LITERAL) { |
| 109 | trailingWhitespace = true; |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | int numKids = children.size(); |
| 114 | for (int i = 0; i < numKids; i++) { |
| 115 | ((Attribute) children.get(i)).resolveRegions(); |
| 116 | } // end of for () |
| 117 | } |
| 118 | } |