Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright 2010 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.packages;
 
 /*
  * Copyright 2005 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.
  */
 
 import  org.drools.guvnor.client.common.ErrorPopup;
 import  org.drools.guvnor.client.common.FormStyleLayout;
 import  org.drools.guvnor.client.common.FormStylePopup;
 import  org.drools.guvnor.client.common.GenericCallback;
 import  org.drools.guvnor.client.common.HTMLFileManagerFields;
 import  org.drools.guvnor.client.common.ImageButton;
 import  org.drools.guvnor.client.common.LoadingPopup;
 import  org.drools.guvnor.client.common.RulePackageSelector;
 import  org.drools.guvnor.client.messages.Constants;
 import  org.drools.guvnor.client.rpc.RepositoryServiceFactory;
 
 import  com.google.gwt.core.client.GWT;
 import  com.google.gwt.event.dom.client.ClickEvent;
 import  com.google.gwt.event.dom.client.ClickHandler;
 import  com.google.gwt.user.client.Command;
 import  com.google.gwt.user.client.Window;
 import  com.google.gwt.user.client.ui.Button;
 import  com.google.gwt.user.client.ui.FileUpload;
 import  com.google.gwt.user.client.ui.FormPanel;
 import  com.google.gwt.user.client.ui.HTML;
 import  com.google.gwt.user.client.ui.HorizontalPanel;
 import  com.google.gwt.user.client.ui.Label;
 import  com.google.gwt.user.client.ui.RadioButton;
 import  com.google.gwt.user.client.ui.TextBox;
 import  com.google.gwt.user.client.ui.VerticalPanel;
 import  com.google.gwt.user.client.ui.Widget;
 import  com.google.gwt.user.client.ui.FormPanel.SubmitCompleteEvent;
 import  com.google.gwt.user.client.ui.FormPanel.SubmitCompleteHandler;
 import  com.google.gwt.user.client.ui.FormPanel.SubmitEvent;
 import  com.google.gwt.user.client.ui.FormPanel.SubmitHandler;
 import  com.gwtext.client.util.Format;

This is the wizard used when creating new packages or importing them.

Author(s):
Michael Neale
 
 public class NewPackageWizard extends FormStylePopup {
 
     private TextBox               nameBox;
     private TextBox               descBox;
     private final FormStyleLayout importLayout     = new FormStyleLayout();
     private final FormStyleLayout newPackageLayout = new FormStyleLayout();
     private static Constants      constants        = ((Constants) GWT.create( Constants.class ));
 
     public NewPackageWizard(final Command afterCreatedEvent) {
         super"images/new_wiz.gif",
                .CreateANewPackage() ); //NON-NLS
          = new TextBox();
          = new TextBox();
 
         //newPackageLayout.addRow( new HTML(constants.CreateNewPackage()) );
 
         .addAttribute( .NameColon(),
                                         );
         .addAttribute( .DescriptionColon(),
                                         );
 
         .setTitle( .PackageNameTip() );
 
         RadioButton newPackage = new RadioButton( "action",
                                                   .CreateNewPackageRadio() ); //NON-NLS
         RadioButton importPackage = new RadioButton( "action",
                                                      .ImportFromDrlRadio() ); //NON-NLS
 
        newPackage.setValue(true);
        .setVisible( true );
        newPackage.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent arg0) {
                .setVisible( true );
                .setVisible( false );				
			}        	
        });
        this.setAfterShownew Command() {
            public void execute() {
                .setFocus( true );
            }
        } );
        .setVisible( false );
        importPackage.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent arg0) {
                .setVisible( false );
                .setVisible( true );				
			}        	
        });
        
        VerticalPanel ab = new VerticalPanel();
        ab.add( newPackage );
        ab.add( importPackage );
        addAttribute( ""ab );
        addRow(  );
        addRow(  );
        .addAttribute( .DRLFileToImport(),
                                   newImportWidgetafterCreatedEvent,
                                                    this ) );
        .addRow( new HTML( "<br/><b>" + .NoteNewPackageDrlImportWarning() + "</b>" ) );
        .addRow( new HTML( .ImportDRLDesc1() ) );
        .addRow( new HTML( .ImportDRLDesc2() ) );
        .addRow( new HTML( .ImportDRLDesc3() ) );
        Button create = new Button( .CreatePackage() );
        create.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent arg0) {
                if ( PackageNameValidator.validatePackageName.getText() ) ) {
                    createPackageAction.getText(),
                                         .getText(),
                                         afterCreatedEvent );
                    hide();
                } else {
                    .setText( "" );
                    Window.alert( .PackageNameCorrectHint() );
                }				
			}        	
        });
        .addAttribute( "",
                                       create );
    }
    private void createPackageAction(final String name,
                                     final String descr,
                                     final Command refresh) {
        LoadingPopup.showMessage( .CreatingPackagePleaseWait() );
        RepositoryServiceFactory.getService().createPackage( name,
                                                             descr,
                                                             new GenericCallback() {
                                                                 public void onSuccess(Object data) {
                                                                     RulePackageSelector.currentlySelectedPackage = name;
                                                                     LoadingPopup.close();
                                                                     refresh.execute();
                                                                 }
                                                             } );
    }
    public static Widget newImportWidget(final Command afterCreatedEvent,
                                         final FormStylePopup parent) {
        final FormPanel uploadFormPanel = new FormPanel();
        uploadFormPanel.setAction( GWT.getModuleBaseURL() + "package" );
        uploadFormPanel.setEncoding( FormPanel.ENCODING_MULTIPART );
        uploadFormPanel.setMethod( FormPanel.METHOD_POST );
        HorizontalPanel panel = new HorizontalPanel();
        uploadFormPanel.setWidget( panel );
        final FileUpload upload = new FileUpload();
        upload.setName( HTMLFileManagerFields.CLASSIC_DRL_IMPORT );
        panel.add( upload );
        panel.add( new Label( .upload() ) );
        ImageButton ok = new ImageButton( "images/upload.gif",
                                          .Import() ); //NON-NLS
        
        ClickHandler okClickHandler = new ClickHandler() {
			public void onClick(ClickEvent arg0) {
                if ( Window.confirm( .ImportMergeWarning() ) ) {
                    uploadFormPanel.submit();
                }			
            }        	
        };
        ok.addClickHandler(okClickHandler);       
        panel.add( ok );
        final FormStylePopup packageNamePopup = new FormStylePopup( "images/package_large.png",
                                                                    .PackageName() );
        HorizontalPanel packageNamePanel = new HorizontalPanel();
        packageNamePopup.addRow( new Label( .ImportedDRLContainsNoNameForThePackage() ) );
        final TextBox packageName = new TextBox();
        packageNamePanel.add( new Label( .PackageName() + ":" ) );
        packageNamePanel.add( packageName );
        Button uploadWithNameButton = new Button( .OK() );
        uploadWithNameButton.addClickHandler( okClickHandler );
        
        packageNamePanel.add( uploadWithNameButton );
        packageNamePopup.addRow( packageNamePanel );
        uploadFormPanel.addSubmitCompleteHandler(new SubmitCompleteHandler() {
			public void onSubmitComplete(SubmitCompleteEvent event) {
                if ( event.getResults().indexOf( "OK" ) > -1 ) { //NON-NLS
                    Window.alert( .PackageWasImportedSuccessfully() );
                    afterCreatedEvent.execute();
                    parent.hide();
                    if ( packageNamePopup != null ) {
                        packageNamePopup.hide();
                    }
                } else if ( event.getResults().indexOf( "Missing package name." ) > -1 ) {
                    LoadingPopup.close();
                    packageNamePopup.show();
                } else {
                    ErrorPopup.showMessage( Format.format( .UnableToImportIntoThePackage0(),
                                                           event.getResults() ) );
                }
                LoadingPopup.close();				
			}     	
        });
        
        uploadFormPanel.addSubmitHandler(new SubmitHandler() {
			public void onSubmit(SubmitEvent event) {
                if ( upload.getFilename().length() == 0 ) {
                    LoadingPopup.close();  
                    Window.alert( .YouDidNotChooseADrlFileToImport() );
                    event.cancel();
                } else if ( !upload.getFilename().endsWith( ".drl" ) ) { //NON-NLS
                    LoadingPopup.close();                    
                    Window.alert( .YouCanOnlyImportDrlFiles() );
                    event.cancel();
                } else if ( packageName.getText() != null && !packageName.getText().equals( "" ) ) {
                    LoadingPopup.showMessage( .ImportingDRLPleaseWait() );
                    uploadFormPanel.setAction( uploadFormPanel.getAction() + "?packageName=" + packageName.getText() );
                }
			}        	
        });
        return uploadFormPanel;
    }
New to GrepCode? Check out our FAQ X