intial source from ttp://www.sf.net/projects/wdte
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.css.ui / src / net / sourceforge / phpeclipse / css / ui / internal / outline / CssOutlineLabelProvider.java
1 /*
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
7  * 
8  * Contributors:
9  *     Christopher Lenz - initial API and implementation
10  * 
11  * $Id: CssOutlineLabelProvider.java,v 1.1 2004-09-02 18:11:49 jsurfer Exp $
12  */
13
14 package net.sourceforge.phpeclipse.css.ui.internal.outline;
15
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;
20
21 import org.eclipse.jface.viewers.LabelProvider;
22 import org.eclipse.swt.graphics.Image;
23
24 /**
25  * Label provider for the CSS outline page.
26  */
27 public class CssOutlineLabelProvider extends LabelProvider {
28
29         // LabelProvider Implementation --------------------------------------------
30
31         /**
32          * @see org.eclipse.jface.viewers.ILabelProvider#getImage(java.lang.Object)
33          */
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(
40                                 CssUI.ICON_AT_RULE);
41                 }
42                 return null;
43         }
44
45         /**
46          * @see org.eclipse.jface.viewers.ILabelProvider#getText(java.lang.Object)
47          */
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());
55                         }
56                 } else if (element instanceof IAtRule) {
57                         IAtRule rule = (IAtRule) element;
58                         ISourceReference name = rule.getName();
59                         ISourceReference value = rule.getValue();
60                         if (name != null) {
61                                 text.append(name.getSource());
62                                 if (value != null) {
63                                         text.append(' ');
64                                 }
65                         }
66                         if (value != null) {
67                                 text.append(value.getSource());
68                         }
69                 }
70                 return normalizeWhitespace(text.toString());
71         }
72
73         // Private Methods ---------------------------------------------------------
74
75         /**
76          * Replaces all whitespace characters by regular spaces, and trims excessive
77          * whitespace down to at most one whitespace between non-whitespace
78          * characters.
79          * 
80          * @param text The text to normalize
81          * @return The normalized text
82          */
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
89                                 c = ' ';
90                         }
91                         if ((c != ' ') || !previousWasWhitespace) {
92                                 buf.append(c);
93                         }
94                         previousWasWhitespace = (c == ' ');
95                 }
96                 return buf.toString();
97         }
98
99 }