I know this code is a little long. My problem is that if the popupwindow get
called twice then all the data is null afterwards. 

public class StorePage extends BaseAdministrationPage {
        
        private Brand brand;
        private Store store;
        private BrandTranslation brandTranslation;
        private BrandPresentation brandPresentation;
        

        public StorePage( Brand brand, Store store ){
                this.brand = getBrandDao().getBrand( brand.getId() );
                this.store = getStoreDao().getStoreById( store.getId() );
                
                
                add( new Label( "storeName" , getStore().getName() ) );
                
                initiateTranslation();
                initiatePresentation();
                

                
                final CSSFeedbackPanel panel = new CSSFeedbackPanel( "feedback" 
);
                panel.setOutputMarkupId(true);
                add( panel );
                
                final FileUploadField overviewImage = new FileUploadField( 
"overviewImage"
);
                final FileUploadField backgroundImage = new FileUploadField(
"backgroundImage" );
                final FileUploadField promotionImage = new FileUploadField(
"promotionImage" );
                
                
                Form brandForm = new Form( "brandForm" ){
                        private static final long serialVersionUID = 1L;

                        @Override
                        protected void onSubmit()
                {
                                
                                final FileUpload uploadOverviewImage = 
overviewImage.getFileUpload();
                                final FileUpload uploadBackgroundImage= 
backgroundImage.getFileUpload();
                                final FileUpload uploadPromotionImage = 
promotionImage.getFileUpload();
                    /**
                     * If the brand is new check that both files will be 
uploaded
                     */
                    if( getBrandPresentation().getId() == null ){
                        if ( uploadOverviewImage == null ||  
uploadBackgroundImage ==
null || uploadPromotionImage == null ){
                                info( "Please upload all images" );
                                return;
                        }
                    }
                    
                    if( uploadOverviewImage != null  ){
                        FileResource resource = newFileResource(new File(
getHairlessApplicationSettings().getBrandFolder(), UUID.randomUUID() + "." +
FileType.getFileExtension( uploadOverviewImage.getClientFileName()
)),uploadOverviewImage);
                        if( resource == null ){
                                info( "Could not upload overview file" );
                                return;
                        }else{
                                getBrandPresentation().setOverviewImage( 
resource );
                        }
                    }
                                
                    if( uploadBackgroundImage != null  ){
                        FileResource resource = newFileResource(new File(
getHairlessApplicationSettings().getBrandFolder(), UUID.randomUUID() + "." +
FileType.getFileExtension( uploadBackgroundImage.getClientFileName()
)),uploadBackgroundImage);
                        if( resource == null ){
                                info( "Could not upload background image" );
                                return;
                        }else{
                                getBrandPresentation().setBackgroundImage( 
resource );
                        }
                    }
                                
                    if( uploadPromotionImage != null  ){
                        FileResource resource = newFileResource(new File(
getHairlessApplicationSettings().getBrandFolder(), UUID.randomUUID() + "." +
FileType.getFileExtension( uploadPromotionImage.getClientFileName()
)),uploadPromotionImage);
                        if( resource == null ){
                                info( "Could not upload background image" );
                                return;
                        }else{
                                getBrandPresentation().setPromotionImage( 
resource );
                        }
                    }
                                
                                String code = getHexCode( 
getBrandPresentation().getTextColor());
                                getBrandPresentation().setTextColor(code);
                                code = getHexCode( 
getBrandPresentation().getBackgroundColor());
                                getBrandPresentation().setBackgroundColor( 
code);
                                code = getHexCode( 
getBrandPresentation().getHeadlineColor());
                                getBrandPresentation().setHeadlineColor( code);
                                
                        
                                
                                getTranslationDao().save(  
getBrandTranslation() );
                                getPresentationDao().save( 
getBrandPresentation() );
                                info( "Brand updated!" );
                                
                }
                };
                
                /** Adding data for translation */
                TextArea shortDescription = new TextArea( "shortDescription", 
new
PropertyModel( getBrandTranslation(), "shortDescription" ) );
                TextArea longDescription = new TextArea( "longDescription", new
PropertyModel( getBrandTranslation(), "longDescription" ) );
                TextArea promotionText = new TextArea( "promotionText", new 
PropertyModel(
getBrandTranslation(), "promotionText" ) );
                CheckBox inPromotion = new CheckBox( "inPromotion" , new 
PropertyModel(
getBrandTranslation(), "inPromotion" ));
                shortDescription.add( StringValidator.lengthBetween( 1, 500 ));
                shortDescription.setRequired( true );
                longDescription.add( StringValidator.lengthBetween( 1, 1000 ));
                longDescription.setRequired( true );
                promotionText.add( StringValidator.lengthBetween( 1, 500 ));
                promotionText.setRequired( true );
                brandForm.add( shortDescription );
                brandForm.add( longDescription );
                brandForm.add( inPromotion );
                brandForm.add( promotionText );
                
                /** Adding data for presentation */
                
                /** Text color */
                TextField textColor = new TextField( "textColor",new 
PropertyModel(
getBrandPresentation(), "textColor" ) );
                textColor.add( new ColorValidator( 
getBrandPresentation().getTextColor()
));
                textColor.add( StringValidator.exactLength(7));
                textColor.setRequired( true );
                
                /** Headline color */
                TextField headlineColor = new TextField( "headlineColor",new
PropertyModel( getBrandPresentation(), "headlineColor" ) );
                headlineColor.add( new ColorValidator(
getBrandPresentation().getHeadlineColor() ));
                headlineColor.add( StringValidator.exactLength(7));
                headlineColor.setRequired( true );
                
                /** background color */
                TextField backgroundColor = new TextField( "backgroundColor",new
PropertyModel( getBrandPresentation(), "backgroundColor" ) );
                backgroundColor.add( new ColorValidator(
getBrandPresentation().getBackgroundColor() ));
                backgroundColor.add( StringValidator.exactLength(7));
                backgroundColor.setRequired( true );
                
                
                brandForm.add( promotionImage );
                brandForm.add( backgroundImage );
                brandForm.add( overviewImage );
                brandForm.add( textColor );
                brandForm.add( backgroundColor );
                brandForm.add( headlineColor );
                add( brandForm );
                
                PopupSettings popupSettings = new PopupSettings();
                Link promotionImageLink = new Link( "promotionImageLink" ){

                        private static final long serialVersionUID = 1L;

                        @Override
                        public void onClick() {
                                
                                BrandPresentation pres = 
getPresentationDao().getPresentation(
getStore(), getBrand( ));
                                setResponsePage(new ImageWindow( 
pres.getPromotionImage().getId() ));

                        }
                        
                        @Override
                        public boolean isEnabled(){
                                if( getBrandPresentation().getId() != null ) 
return true;
                                return false;
                        }
                        
                };
                promotionImageLink.setPopupSettings( popupSettings );
                brandForm.add( promotionImageLink );
                
                Link deleteLink = new Link( "delete" ){
                        private static final long serialVersionUID = 1L;

                        @Override
                        public void onClick(){
                                getBrandDao().detachBrandFromStore( 
getBrand(),getStore() );
                                setResponsePage( new BrandPage( getBrand() ));
                        }
                        
                        @Override
                        public boolean isVisible(){
                                if(getBrandPresentation().getId() != null ){
                                        return true;
                                }
                                return false;
                        }
                };
                
                deleteLink.add( new SimpleAttributeModifier( "onclick" , 
"return confirm(
'Are you sure?' );" ));
                
                add( deleteLink );
                
                add(new Link( "back" ){
                        private static final long serialVersionUID = 1L;

                        @Override
                        public void onClick(){
                                setResponsePage( new BrandPage( getBrand() ));
                        }
                        
                        
                } );
                
                IModel unusedStoresModel = new LoadableDetachableModel(){
                        private static final long serialVersionUID = 1L;
                        protected Object load()
                        {
                                return getStoreDao().getUnusedStores( 
getBrand() );
                        }
                };

                final DropDownChoice unusedStoresChoice = new DropDownChoice(
"unusedStoresChoice", new Model( "" ),unusedStoresModel, new
StoreChoiceRenderer()){
                        private static final long serialVersionUID = 1L;
                        
                        @Override
                        protected java.lang.CharSequence getDefaultChoice(final 
Object selected){
                                return "<option value=\"\">--- Copy to store 
---</option>"; 
                        }
                        
                         @Override
                         public boolean isVisible(){
                                 if( getChoices().size() > 0 &&  
getBrandTranslation().getId() != null
){
                                         return true;
                                 }
                                 return false;
                         }
                };
                
                Form copyForm = new Form( "copyForm" );
                copyForm.add( unusedStoresChoice );
                
                IndicatingAjaxButton ajaxBtn = new IndicatingAjaxButton("copy", 
copyForm
){
                 
                        private static final long serialVersionUID = 1L;
                        
                        @Override
                        protected IAjaxCallDecorator getAjaxCallDecorator()
                        {
                                return new IAjaxCallDecorator()
                                {
                                        private static final long 
serialVersionUID = 1L;
                                        public CharSequence 
decorateScript(CharSequence script)
                                        {
                                                return 
"document.getElementById('"+ getMarkupId()
+"').disabled=true;"+script;
                                        }
                                        public CharSequence 
decorateOnFailureScript(CharSequence script)
                                        {
                                                return 
script+"document.getElementById('"+ getMarkupId()
+"').disabled=false;";
                                        }
                                        public CharSequence 
decorateOnSuccessScript(CharSequence script)
                                        {
                                                return 
script+"document.getElementById('"+ getMarkupId()
+"').disabled=false;";
                                        }
                                };
                        }

                        protected void onSubmit(AjaxRequestTarget target, Form 
form){
                                this.setEnabled(false);
                                target.addComponent(this);
                target.addComponent(panel);
                                
                Store toStore = (Store )unusedStoresChoice.getModelObject();
                
                if( toStore != null ){
                        getBrandDao().copyBrand( getBrand().getId(),
getStore().getId(), toStore.getId() );
                        setResponsePage( new StorePage( getBrand(), toStore ));
                }
                
                                this.setEnabled( true );
                                
            }

            protected void onError(AjaxRequestTarget target, Form form)
            {
                target.addComponent(panel);
            }
            
            @Override
            public boolean isVisible(){
                return unusedStoresChoice.isVisible();
            }
        };
        ajaxBtn.setOutputMarkupId( true );
        copyForm.add( ajaxBtn );
                add( copyForm );
                
                
        }

        
        
        public void initiateTranslation(){


                brandTranslation = getTranslationDao().getTranslation( 
getStore(),
getBrand( ));
                
                if( brandTranslation == null ){
                        brandTranslation = new BrandTranslation();
                        brandTranslation.setName( getBrand().getName() );
                        brandTranslation.setBrand( getBrand() );
                        brandTranslation.setStore( store );
                }
                
        }
        
        public void initiatePresentation(){
                
                brandPresentation = getPresentationDao().getPresentation( 
getStore(),
getBrand( ));
                
                if( brandPresentation == null ){
                        brandPresentation = new BrandPresentation();
                        brandPresentation.setBackgroundColor( "#FFFFFF" );
                        brandPresentation.setHeadlineColor( "#FFFFFF" );
                        brandPresentation.setTextColor( "#FFFFFF" );
                        brandPresentation.setBrand( getBrand() );
                        brandPresentation.setStore( store );
                }
                
        }
        
        private String getHexCode( String decode ){
                
                try{
                        java.awt.Color color = java.awt.Color.decode( decode );
                        
                        String red = Integer.toHexString(color.getRed());
                        String green = Integer.toHexString(color.getGreen());
                        String blue = Integer.toHexString(color.getBlue());
                         
                        if (red.length() == 1) red = "0" + red;
                        if (green.length() == 1) green = "0" + green;
                        if (blue.length() == 1) blue = "0" + blue;
                         
                        String hexColor = "#" + red + green + blue;
                        
                        return hexColor;
                }catch( NumberFormatException nex ){
                        
                }
                
                return null;
        }
        
        public Brand getBrand() {
                return brand;
        }

        public Store getStore() {
                return store;
        }

        public BrandTranslation getBrandTranslation() {
                return brandTranslation;
        }

        public BrandPresentation getBrandPresentation() {
                return brandPresentation;
        }

}
-- 
View this message in context: 
http://www.nabble.com/Very-annoing-error-tp17323755p17323755.html
Sent from the Wicket - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to