Merging code from trunk that fixes #774, this work was done by incastrix
[phpeclipse.git] / net.sourceforge.phpeclipse.ui / src / net / sourceforge / phpeclipse / ui / text / source / DefaultSourceViewerConfiguration.java
1 /*
2  * Copyright (c) 2002-2004 Widespace, OU 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://solareclipse.sourceforge.net/legal/cpl-v10.html
7  * 
8  * Contributors:
9  *     Igor Malinin - initial contribution
10  * 
11  * $Id: DefaultSourceViewerConfiguration.java,v 1.2 2006-10-21 23:13:54 pombredanne Exp $
12  */
13
14 package net.sourceforge.phpeclipse.ui.text.source;
15
16 import org.eclipse.jface.preference.IPreferenceStore;
17 import org.eclipse.jface.text.source.ISourceViewer;
18 import org.eclipse.jface.text.source.SourceViewerConfiguration;
19
20 /**
21  * @author Igor Malinin
22  */
23 public class DefaultSourceViewerConfiguration extends SourceViewerConfiguration {
24
25         /** Preference key used to look up display tab width. */
26         public static final String PREFERENCE_TAB_WIDTH = "net.sourceforge.phpeclipse.ui.editor.tabWidth"; //$NON-NLS-1$
27
28         /** Preference key for inserting spaces rather than tabs. */
29         public static final String PREFERENCE_SPACES_FOR_TABS = "net.sourceforge.phpeclipse.ui.editor.spacesForTabs"; //$NON-NLS-1$
30
31         private IPreferenceStore store;
32
33         public DefaultSourceViewerConfiguration(IPreferenceStore store) {
34                 this.store = store;
35         }
36
37         /*
38          * @see SourceViewerConfiguration#getIndentPrefixes(ISourceViewer, String)
39          */
40         public String[] getIndentPrefixes(ISourceViewer sourceViewer,
41                         String contentType) {
42                 // prefix[0] is either '\t' or ' ' x tabWidth, depending on useSpaces
43
44                 int tabWidth = store.getInt(PREFERENCE_TAB_WIDTH);
45                 boolean useSpaces = store.getBoolean(PREFERENCE_SPACES_FOR_TABS);
46
47                 String[] prefixes = new String[tabWidth + 1];
48
49                 for (int i = 0; i <= tabWidth; i++) {
50                         StringBuffer prefix = new StringBuffer(tabWidth - 1);
51
52                         if (useSpaces) {
53                                 for (int j = 0; j + i < tabWidth; j++) {
54                                         prefix.append(' ');
55                                 }
56
57                                 if (i != 0) {
58                                         prefix.append('\t');
59                                 }
60                         } else {
61                                 for (int j = 0; j < i; j++) {
62                                         prefix.append(' ');
63                                 }
64
65                                 if (i != tabWidth) {
66                                         prefix.append('\t');
67                                 }
68                         }
69
70                         prefixes[i] = prefix.toString();
71                 }
72
73                 prefixes[tabWidth] = ""; //$NON-NLS-1$
74
75                 return prefixes;
76         }
77 }