/**
 * 
 */
package org.csstudio.sds.cosyrules.color;

import static org.csstudio.sds.cosyrules.color.MaintenanceRulePreference.MAINTENANCE_DISPLAY_PATH;
import static org.csstudio.sds.cosyrules.color.MaintenanceRulePreference.MAINTENANCE_PRE_FILE_NAME;
import static org.csstudio.sds.cosyrules.color.MaintenanceRulePreference.MAINTENANCE_UNKNOWN_DISPLAY_PATH;

import org.csstudio.sds.model.IRule;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Platform;

/**
 * @author hrickens
 *
 */
public class MaintenanceRule implements IRule {
    
    private IPath _defaultPath;
    private IPath _dispayPath = null;
    private String _preFileName = null;
    
    //    private Map<String, IPath> _rTypUrlMap;
    
    /**
     * 
     */
    public MaintenanceRule() {
        init();
    }
    
    /**
    * 
    */
    private void init() {
        _defaultPath = MAINTENANCE_UNKNOWN_DISPLAY_PATH.getValue();
        _dispayPath = MAINTENANCE_DISPLAY_PATH.getValue();
        _preFileName = MAINTENANCE_PRE_FILE_NAME.getValue();
    }
    
    /* (non-Javadoc)
     * @see org.csstudio.sds.model.IRule#evaluate(java.lang.Object[])
     */
    @Override
    public Object evaluate(Object[] arguments) {
        init();
        IPath iPath = _defaultPath;
        if (arguments.length > 0) {
            Object obj = arguments[0];
            if (obj instanceof String) {
                String rtyp = (String) obj;
                int indexOf = _preFileName.toLowerCase().indexOf("{rtyp}");
                if (indexOf >= 0) {
                    StringBuilder sb = new StringBuilder();
                    sb.append(_preFileName.substring(0, indexOf));
                    sb.append(rtyp);
                    sb.append(_preFileName.substring(indexOf + 6));
                    if (!sb.toString().toLowerCase().endsWith(".css-sds")) {
                        sb.append(".css-sds");
                    }
                    iPath = _dispayPath.append(sb.toString());
                } else {
                    iPath = _dispayPath.append(_preFileName + rtyp + ".css-sds");
                }
                IPath location = Platform.getLocation();
                IPath append = location.append(iPath);
                if(!append.toFile().isFile()) {
                    iPath = _defaultPath;
                }
            }
        }
        return iPath;
    }
    
    /* (non-Javadoc)
     * @see org.csstudio.sds.model.IRule#getDescription()
     */
    @Override
    public String getDescription() {
        return "Gibt das Maintenance Display fr den bergebenen RTYP wieder";
    }
    
}

@@ -484,16 +484,75 @@
 atform;%0D
+%0Aimport org.slf4j.Logger;%0D%0Aimport org.slf4j.LoggerFactory;%0D
 %0A%0D%0A/**%0D%0A
@@ -632,24 +632,117 @@
 le %7B%0D%0A    %0D%0A
+    private static final Logger LOG = LoggerFactory.getLogger(MaintenanceRule.class);%0D%0A    %0D%0A
     private 
@@ -1035,12 +1035,15 @@
 ate 
-void
+boolean
  ini
@@ -1045,24 +1045,43 @@
 n init() %7B%0D%0A
+        try %7B%0D%0A    
         _def
@@ -1129,32 +1129,36 @@
 TH.getValue();%0D%0A
+    
         _dispayP
@@ -1209,16 +1209,20 @@
         
+    
 _preFile
@@ -1263,24 +1263,165 @@
 etValue();%0D%0A
+            return true;%0D%0A        %7D catch (Exception e) %7B%0D%0A            LOG.warn(%22Wrong preferences!%22, e);%0D%0A        %7D%0D%0A        return false;%0D%0A
     %7D%0D%0A    %0D
@@ -1592,31 +1592,65 @@
  %7B%0D%0A        
+IPath iPath = null;%0D%0A        if(
 init()
-;
+) %7B
 %0D%0A        IP
@@ -1639,37 +1639,35 @@
 t()) %7B%0D%0A        
-IPath
+   
  iPath = _defaul
@@ -1682,19 +1682,22 @@
         
+   
+ 
 if
- 
 (argumen
@@ -1726,16 +1726,20 @@
         
+   
+ 
 Object o
@@ -1770,19 +1770,22 @@
         
+    
 if
- 
 (obj ins
@@ -1799,24 +1799,28 @@
  String) %7B%0D%0A
+    
             
@@ -1868,16 +1868,20 @@
         
+    
 int inde
@@ -1949,19 +1949,22 @@
         
+    
 if
- 
 (indexOf
@@ -1989,24 +1989,28 @@
             
+ 
+   
 StringBuilde
@@ -2034,24 +2034,28 @@
 Builder();%0D%0A
+    
             
@@ -2106,24 +2106,28 @@
 indexOf));%0D%0A
+    
             
@@ -2164,32 +2164,36 @@
                 
+    
 sb.append(_preFi
@@ -2249,11 +2249,14 @@
     
+    
 if
- 
 (!sb
@@ -2322,32 +2322,36 @@
                 
+ 
+   
 sb.append(%22.css-
@@ -2370,35 +2370,43 @@
                 
-%7D%0D%0A
+    %7D%0D%0A    
                 
@@ -2469,16 +2469,20 @@
         
+    
 %7D else %7B
@@ -2479,24 +2479,28 @@
   %7D else %7B%0D%0A
+    
             
@@ -2578,35 +2578,43 @@
                 
+  
+  
 %7D%0D%0A
+    
                 
@@ -2667,24 +2667,28 @@
             
+    
 IPath append
@@ -2727,24 +2727,28 @@
             
+ 
+   
 if(!append.t
@@ -2780,32 +2780,36 @@
                 
+    
 iPath = _default
@@ -2807,32 +2807,55 @@
  _defaultPath;%0D%0A
+                    %7D%0D%0A
                 

handle wrong preference