2 * Copyright (c) 2003-2004 Christopher Lenz and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * Christopher Lenz - initial API and implementation
11 * $Id: CssOutlineLabelProvider.java,v 1.1 2004-09-02 18:11:49 jsurfer Exp $
14 package net.sourceforge.phpeclipse.css.ui.internal.outline;
16 import net.sourceforge.phpeclipse.core.model.ISourceReference;
17 import net.sourceforge.phpeclipse.css.core.model.IAtRule;
18 import net.sourceforge.phpeclipse.css.core.model.IStyleRule;
19 import net.sourceforge.phpeclipse.css.ui.CssUI;
21 import org.eclipse.jface.viewers.LabelProvider;
22 import org.eclipse.swt.graphics.Image;
25 * Label provider for the CSS outline page.
27 public class CssOutlineLabelProvider extends LabelProvider {
29 // LabelProvider Implementation --------------------------------------------
32 * @see org.eclipse.jface.viewers.ILabelProvider#getImage(java.lang.Object)
34 public Image getImage(Object element) {
35 if (element instanceof IStyleRule) {
36 return CssUI.getDefault().getImageRegistry().get(
37 CssUI.ICON_STYLE_RULE);
38 } else if (element instanceof IAtRule) {
39 return CssUI.getDefault().getImageRegistry().get(
46 * @see org.eclipse.jface.viewers.ILabelProvider#getText(java.lang.Object)
48 public String getText(Object element) {
49 StringBuffer text = new StringBuffer();
50 if (element instanceof IStyleRule) {
51 IStyleRule rule = (IStyleRule) element;
52 ISourceReference selector = rule.getSelector();
53 if (selector != null) {
54 text.append(selector.getSource());
56 } else if (element instanceof IAtRule) {
57 IAtRule rule = (IAtRule) element;
58 ISourceReference name = rule.getName();
59 ISourceReference value = rule.getValue();
61 text.append(name.getSource());
67 text.append(value.getSource());
70 return normalizeWhitespace(text.toString());
73 // Private Methods ---------------------------------------------------------
76 * Replaces all whitespace characters by regular spaces, and trims excessive
77 * whitespace down to at most one whitespace between non-whitespace
80 * @param text The text to normalize
81 * @return The normalized text
83 public String normalizeWhitespace(String text) {
84 StringBuffer buf = new StringBuffer();
85 boolean previousWasWhitespace = false;
86 for (int i = 0; i < text.length(); i++) {
87 char c = text.charAt(i);
88 if ((c == 9) || (c == 10) || (c == 13)) { // Tab
91 if ((c != ' ') || !previousWasWhitespace) {
94 previousWasWhitespace = (c == ' ');
96 return buf.toString();