/*
 * Copyright 2012 JBoss Inc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.drools.guvnor.client.editors.texteditor;

import javax.enterprise.context.Dependent;

import org.drools.guvnor.client.mvp.IPlaceRequest;
import org.drools.guvnor.client.mvp.PlaceRequest;

import com.google.gwt.place.shared.PlaceTokenizer;

@Dependent
public class TextEditorPlace extends PlaceRequest implements IPlaceRequest {

    private final String helloName;
    private final String fileName;

    public TextEditorPlace() {
        super("TextEditor");
        this.helloName = "TextEditor";
        this.fileName = "myfile.hack";
    }

    public TextEditorPlace(final String token) {
        super("TextEditor");
        String[] parts = token.split("\\|");
        if (parts.length != 2) {
            throw new RuntimeException("Invalid token");
        }
        this.helloName = parts[0];
        this.fileName = parts[1];
    }


    public String getFileName() {
        return fileName;
    }

    @Override
    public String toString() {
        return helloName + '|' + fileName;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        TextEditorPlace that = (TextEditorPlace) o;

        if (!helloName.equals(that.helloName)) {
            return false;
        }
        if (!fileName.equals(that.fileName)) {
            return false;
        }

        return true;
    }

    @Override
    public int hashCode() {
        int result = helloName.hashCode();
        result = 31 * result + fileName.hashCode();
        return result;
    }

    public static class Tokenizer implements PlaceTokenizer<TextEditorPlace> {

        public String getToken(final TextEditorPlace place) {
            return place.toString();
        }

        public TextEditorPlace getPlace(final String token) {
            return new TextEditorPlace(token);
        }
    }

}

@@ -901,16 +901,20 @@
 eRequest
+%0A   
  impleme
@@ -916,16 +916,20 @@
 plements
+%0A   
  IPlaceR
@@ -947,211 +947,63 @@
    p
-rivate final String helloName;%0A    private final String fileName;%0A%0A    public TextEditorPlace() %7B%0A        super(%22TextEditor%22);%0A        this.helloName = %22TextEditor%22;%0A        this.fileName = %22myfile.hack%22
+ublic TextEditorPlace() %7B%0A        super( %22TextEditor%22 )
 ;%0A  
@@ -1070,16 +1070,17 @@
   super(
+ 
 %22TextEdi
@@ -1083,16 +1083,17 @@
 tEditor%22
+ 
 );%0A     
@@ -1128,13 +1128,15 @@
 lit(
+ 
 %22%5C%5C%7C%22
+ 
 );%0A 
@@ -1146,16 +1146,17 @@
     if (
+ 
 parts.le
@@ -1163,17 +1163,18 @@
 ngth != 
-2
+1 
 ) %7B%0A    
@@ -1208,16 +1208,17 @@
 ception(
+ 
 %22Invalid
@@ -1224,16 +1224,17 @@
 d token%22
+ 
 );%0A     
@@ -1250,67 +1250,62 @@
     
-this.helloName = parts%5B0%5D;%0A        this.fileName =
+addParameter( %22path%22,%0A                     
  parts%5B
-1%5D
+0%5D )
 ;%0A  
@@ -1309,17 +1309,16 @@
 %0A    %7D%0A%0A
-%0A
     publ
@@ -1358,24 +1358,71 @@
  return 
-fileName
+getParameter( %22path%22,%0A                             %22%22 )
 ;%0A    %7D%0A
@@ -1486,34 +1486,63 @@
 urn 
-helloName + '%7C' + fileName
+getParameter( %22path%22,%0A                             %22%22 )
 ;%0A  
@@ -1610,16 +1610,17 @@
     if (
+ 
 this == 
@@ -1620,16 +1620,17 @@
 his == o
+ 
 ) %7B%0A    
@@ -1672,16 +1672,17 @@
     if (
+ 
 o == nul
@@ -1712,16 +1712,17 @@
 tClass()
+ 
 ) %7B%0A    
@@ -1806,25 +1806,24 @@
 ace) o;%0A
-%0A
         
 if (!hel
@@ -1818,83 +1818,110 @@
     
-if (!helloName.equals(that.helloName)) %7B%0A            return false;%0A
+final String thisPath = this.getParameter( %22path%22,%0A                                           
         
 %7D%0A  
@@ -1908,33 +1908,37 @@
                 
-%7D
+%22%22 );
 %0A        if (!fi
@@ -1934,81 +1934,109 @@
     
-if (!fileName.equals(that.fileName)) %7B%0A            return false;%0A
+final String thatPath = that.getParameter( %22path%22,%0A                                          
         
 %7D%0A%0A 
@@ -2023,34 +2023,38 @@
                 
-%7D%0A
+ %22%22 );
 %0A        return 
@@ -2050,27 +2050,50 @@
     return t
-rue
+hisPath.equals( thatPath )
 ;%0A    %7D%0A%0A   
@@ -2156,69 +2156,69 @@
 t = 
-helloName.hashCode();%0A        result = 31 * result + fileName
+getParameter( %22path%22,%0A                                   %22%22 )
 .has
@@ -2289,16 +2289,24 @@
 okenizer
+%0A       
  impleme
@@ -2308,16 +2308,24 @@
 plements
+%0A       
  PlaceTo
@@ -2563,21 +2563,23 @@
 orPlace(
+ 
 token
+ 
 );%0A     

Fix TextEditorPlace issue.