1) Added setting of syntax properties to italic, underline and strike through.
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / ui / text / AbstractJavaScanner.java
index 8d65144..9906d9e 100644 (file)
@@ -23,156 +23,370 @@ import org.eclipse.jface.util.PropertyChangeEvent;
 import org.eclipse.swt.SWT;
 import org.eclipse.swt.graphics.RGB;
 
-
 /**
  * Initialized with a color manager and a preference store, its subclasses are
- * only responsible for providing a list of preference keys based on which tokens
- * are generated and to use this tokens to define the rules controlling this scanner.
+ * only responsible for providing a list of preference keys based on which
+ * tokens are generated and to use this tokens to define the rules controlling
+ * this scanner.
  */
 public abstract class AbstractJavaScanner extends BufferedRuleBasedScanner {
-                       
-       
+
        private IColorManager fColorManager;
+
        private IPreferenceStore fPreferenceStore;
-       
-       private Map fTokenMap= new HashMap();
+
+       private Map fTokenMap = new HashMap();
+
        private String[] fPropertyNamesColor;
-       private String[] fPropertyNamesStyle;
-       
-       
-       /** 
-        * Returns the list of preference keys which define the tokens
-        * used in the rules of this scanner.
+       private String[] fPropertyNamesBold;
+       private String[] fPropertyNamesItalic;
+       private String[] fPropertyNamesUnderline;
+       private String[] fPropertyNamesStrikeThrough;
+
+       /**
+        * Returns the list of preference keys which define the tokens used in the
+        * rules of this scanner.
         */
        abstract protected String[] getTokenProperties();
-               
+
        /**
         * Creates the list of rules controlling this scanner.
         */
        abstract protected List createRules();
-               
-       
+
        /**
         * Creates an abstract Java scanner.
         */
        public AbstractJavaScanner(IColorManager manager, IPreferenceStore store) {
                super();
-               fColorManager= manager;
-               fPreferenceStore= store;
+               fColorManager = manager;
+               fPreferenceStore = store;
        }
-       
+
        /**
         * Must be called after the constructor has been called.
         */
        public final void initialize() {
-               
-               fPropertyNamesColor= getTokenProperties();
-               int length= fPropertyNamesColor.length;
-               fPropertyNamesStyle= new String[length];
-               for (int i= 0; i < length; i++) {
-                       fPropertyNamesStyle[i]= fPropertyNamesColor[i] + "_bold"; //$NON-NLS-1$
-                       addToken(fPropertyNamesColor[i], fPropertyNamesStyle[i]);
-               }
-               
-               initializeRules();
+               fPropertyNamesColor         = getTokenProperties ();
+               int length                  = fPropertyNamesColor.length;
+               fPropertyNamesBold          = new String[length];
+               fPropertyNamesItalic        = new String[length];
+               fPropertyNamesUnderline     = new String[length];
+               fPropertyNamesStrikeThrough = new String[length];
+
+               for (int i = 0; i < length; i++) {
+                       fPropertyNamesBold[i]          = fPropertyNamesColor[i] + "_bold";          //$NON-NLS-1$
+                       fPropertyNamesItalic[i]        = fPropertyNamesColor[i] + "_italic";        //$NON-NLS-1$
+                       fPropertyNamesUnderline[i]     = fPropertyNamesColor[i] + "_underline";     //$NON-NLS-1$
+                       fPropertyNamesStrikeThrough[i] = fPropertyNamesColor[i] + "_strikethrough"; //$NON-NLS-1$
+
+                       addToken (fPropertyNamesColor[i],
+                                 fPropertyNamesBold[i],                 // bold
+                                 fPropertyNamesItalic[i],               // italic
+                                 fPropertyNamesUnderline[i],            // underline
+                                         fPropertyNamesStrikeThrough[i]);       // strike through
+               }
+
+               initializeRules ();
        }
-               
-       private void addToken(String colorKey, String styleKey) {
-               RGB rgb= PreferenceConverter.getColor(fPreferenceStore, colorKey);
+
+       /**
+        *
+        */
+
+       private void addToken (String colorKey, 
+                                      String styleKeyBold, String styleKeyItalic, 
+                                      String styleKeyUnderline, String styleKeyStrikeThrough) {
+               RGB rgb = PreferenceConverter.getColor (fPreferenceStore, colorKey);
+
                if (fColorManager instanceof IColorManagerExtension) {
-                       IColorManagerExtension ext= (IColorManagerExtension) fColorManager;
+                       IColorManagerExtension ext = (IColorManagerExtension) fColorManager;
                        ext.unbindColor(colorKey);
                        ext.bindColor(colorKey, rgb);
                }
-               
-               boolean bold= fPreferenceStore.getBoolean(styleKey);
-               fTokenMap.put(colorKey, new Token(new TextAttribute(fColorManager.getColor(colorKey), null, bold ? SWT.BOLD : SWT.NORMAL)));
+
+               boolean bold          = fPreferenceStore.getBoolean (styleKeyBold);
+               boolean italic        = fPreferenceStore.getBoolean (styleKeyItalic);
+               boolean underline     = fPreferenceStore.getBoolean (styleKeyUnderline);
+               boolean strikethrough = fPreferenceStore.getBoolean (styleKeyStrikeThrough);
+
+               int     style     = SWT.NORMAL;
+
+               style                    |= (bold ? SWT.BOLD : SWT.NORMAL) |
+                                                       (italic ? SWT.ITALIC : SWT.NORMAL) |
+                                                       (underline ? (1 << 30) : SWT.NORMAL) |
+                                                       (strikethrough ? (1 << 29) : SWT.NORMAL);
+
+               fTokenMap.put (colorKey, new Token (new TextAttribute (fColorManager.getColor(colorKey), null, style)));
        }
-       
-       protected Token getToken(String key) {
+
+       /**
+        *
+        */
+
+       protected Token getToken (String key) {
                return (Token) fTokenMap.get(key);
        }
-               
+
+       /**
+        *
+        */
+
        private void initializeRules() {
-               List rules= createRules();
+               List rules = createRules();
                if (rules != null) {
-                       IRule[] result= new IRule[rules.size()];
+                       IRule[] result = new IRule[rules.size()];
                        rules.toArray(result);
                        setRules(result);
                }
        }
-       
-       private int indexOf(String property) {
+
+       /**
+        *
+        */
+
+       private int indexOfNamesColor (String property) {
                if (property != null) {
-                       int length= fPropertyNamesColor.length;
-                       for (int i= 0; i < length; i++) {
-                               if (property.equals(fPropertyNamesColor[i]) || property.equals(fPropertyNamesStyle[i]))
+                       int length = fPropertyNamesColor.length;
+
+                       for (int i = 0; i < length; i++) {
+                               if (property.equals (fPropertyNamesColor[i])) {
                                        return i;
+                               }
                        }
                }
+
                return -1;
        }
-       
-       public boolean affectsBehavior(PropertyChangeEvent event) {
-               return indexOf(event.getProperty()) >= 0;
+
+       /**
+        *
+        */
+
+       private int indexOfNamesBold (String property) {
+               if (property != null) {
+                       int length = fPropertyNamesBold.length;
+
+                       for (int i = 0; i < length; i++) {
+                               if (property.equals (fPropertyNamesBold[i])) {
+                                       return i;
+                               }
+                       }
+               }
+
+               return -1;
+       }
+
+       /**
+        *
+        */
+
+       private int indexOfNamesItalic (String property) {
+               if (property != null) {
+                       int length = fPropertyNamesItalic.length;
+
+                       for (int i = 0; i < length; i++) {
+                               if (property.equals (fPropertyNamesItalic[i])) {
+                                       return i;
+                               }
+                       }
+               }
+
+               return -1;
        }
-       
-       public void adaptToPreferenceChange(PropertyChangeEvent event) {
-               String p= event.getProperty();
-               int index= indexOf(p);
-               Token token= getToken(fPropertyNamesColor[index]);
-               if (fPropertyNamesColor[index].equals(p))
-                       adaptToColorChange(token, event);
-               else
-                       adaptToStyleChange(token, event);
+
+       /**
+        *
+        */
+
+       private int indexOfNamesUnderline (String property) {
+               if (property != null) {
+                       int length = fPropertyNamesUnderline.length;
+
+                       for (int i = 0; i < length; i++) {
+                               if (property.equals (fPropertyNamesUnderline[i])) {
+                                       return i;
+                               }
+                       }
+               }
+
+               return -1;
        }
-       
-       private void adaptToColorChange(Token token, PropertyChangeEvent event) {
-               RGB rgb= null;
-               
-               Object value= event.getNewValue();
-               if (value instanceof RGB)
-                       rgb= (RGB) value;
-               else if (value instanceof String)
-                       rgb= StringConverter.asRGB((String) value);
-                       
+
+       /**
+        *
+        */
+
+       private int indexOfNamesStrikeThrough (String property) {
+               if (property != null) {
+                       int length = fPropertyNamesStrikeThrough.length;
+
+                       for (int i = 0; i < length; i++) {
+                               if (property.equals (fPropertyNamesStrikeThrough[i])) {
+                                       return i;
+                               }
+                       }
+               }
+
+               return -1;
+       }
+
+
+       /**
+        *
+        */
+
+       public boolean affectsBehavior (PropertyChangeEvent event) {
+               return (indexOfNamesColor         (event.getProperty ()) >= 0) ||
+                      (indexOfNamesBold          (event.getProperty ()) >= 0) ||
+                          (indexOfNamesItalic        (event.getProperty ()) >= 0) ||
+                          (indexOfNamesUnderline     (event.getProperty ()) >= 0) ||
+                          (indexOfNamesStrikeThrough (event.getProperty ()) >= 0);
+       }
+
+       /**
+        *
+        */
+
+       public void adaptToPreferenceChange (PropertyChangeEvent event) {
+               String p = event.getProperty ();
+               int index = indexOfNamesColor (p);
+
+               if (index < 0) {
+                       index = indexOfNamesBold (p);
+               }
+
+               if (index < 0) {
+                       index = indexOfNamesItalic (p);
+               }
+
+               if (index < 0) {
+                       index = indexOfNamesUnderline (p);
+               }
+
+               if (index < 0) {
+                       index = indexOfNamesStrikeThrough (p);
+               }
+
+               Token token = getToken (fPropertyNamesColor[index]);
+
+               if (fPropertyNamesColor[index].equals (p)) {
+                       adaptToColorChange (token, event);
+               }
+               else {
+                       adaptToStyleChange (token, event);
+               }
+       }
+
+       /**
+        *
+        */
+       private void adaptToColorChange (Token token, PropertyChangeEvent event) {
+               RGB rgb = null;
+
+               Object value = event.getNewValue();
+
+               if (value instanceof RGB) {
+                       rgb = (RGB) value;
+               }
+               else if (value instanceof String) {
+                       rgb = StringConverter.asRGB((String) value);
+               }
+
                if (rgb != null) {
-                       
-                       String property= event.getProperty();
-                       
+                       String property = event.getProperty ();
+
                        if (fColorManager instanceof IColorManagerExtension) {
-                               IColorManagerExtension ext= (IColorManagerExtension) fColorManager;
-                               ext.unbindColor(property);
-                               ext.bindColor(property, rgb);
+                               IColorManagerExtension ext = (IColorManagerExtension) fColorManager;
+                               ext.unbindColor (property);
+                               ext.bindColor (property, rgb);
                        }
-                       
-                       Object data= token.getData();
+
+                       Object data = token.getData ();
+
                        if (data instanceof TextAttribute) {
-                               TextAttribute oldAttr= (TextAttribute) data;
-                               token.setData(new TextAttribute(fColorManager.getColor(property), oldAttr.getBackground(), oldAttr.getStyle()));
+                               TextAttribute oldAttr = (TextAttribute) data;
+                               token.setData (new TextAttribute (fColorManager.getColor (property), oldAttr.getBackground (), oldAttr.getStyle ()));
                        }
                }
        }
-       
+
+       /**
+        *
+        */
+
        private void adaptToStyleChange(Token token, PropertyChangeEvent event) {
-               boolean bold= false;
-               Object value= event.getNewValue();
-               if (value instanceof Boolean)
-                       bold= ((Boolean) value).booleanValue();
+               int       index = -1;
+               boolean   newValue = false;
+               Object    value = event.getNewValue ();
+
+               if (value instanceof Boolean) {
+                       newValue = ((Boolean) value).booleanValue();
+               }
                else if (value instanceof String) {
-                       String s= (String) value;
-                       if (IPreferenceStore.TRUE.equals(s))
-                               bold= true;
-                       else if (IPreferenceStore.FALSE.equals(s))
-                               bold= false;
-               }
-               
-               Object data= token.getData();
+                       String s = (String) value;
+
+                       if (IPreferenceStore.TRUE.equals(s)) {
+                               newValue = true;
+                       }
+                       else if (IPreferenceStore.FALSE.equals(s)) {
+                               newValue = false;
+                       }
+               }
+
+               Object data = token.getData ();
+
                if (data instanceof TextAttribute) {
-                       TextAttribute oldAttr= (TextAttribute) data;
-                       boolean isBold= (oldAttr.getStyle() == SWT.BOLD);
-                       if (isBold != bold) 
-                               token.setData(new TextAttribute(oldAttr.getForeground(), oldAttr.getBackground(), bold ? SWT.BOLD : SWT.NORMAL));
+                       TextAttribute oldAttr = (TextAttribute) data;
+
+                       int style = oldAttr.getStyle ();
+
+                       index = indexOfNamesBold (event.getProperty ());
+
+                       if (index >= 0) {
+                               if (newValue) {
+                                       style |= SWT.BOLD;
+                               }
+                               else {
+                                       style &= ~SWT.BOLD;
+                               }
+                       }
+
+                       index = indexOfNamesItalic (event.getProperty ());
+
+                       if (index >= 0) {
+                               if (newValue) {
+                                       style |= SWT.ITALIC;
+                               }
+                               else {
+                                       style &= ~SWT.ITALIC;
+                               }
+                       }
+
+                       index = indexOfNamesUnderline (event.getProperty ());
+
+                       if (index >= 0) {
+                               if (newValue) {
+                                       style |= (1 << 30);
+                               }
+                               else {
+                                       style &= ~(1 << 30);
+                               }
+                       }
+
+                       index = indexOfNamesStrikeThrough (event.getProperty ());
+
+                       if (index >= 0) {
+                               if (newValue) {
+                                       style |= (1 << 29);
+                               }
+                               else {
+                                       style &= ~(1 << 29);
+                               }
+                       }
+
+
+                       token.setData (new TextAttribute (oldAttr.getForeground (), oldAttr.getBackground (), style));
                }
        }
-}
\ No newline at end of file
+}