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.ruleeditor;
 
 /*
  * 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 java.util.Date;
 
 import  org.drools.guvnor.client.common.*;
 import  org.drools.guvnor.client.explorer.ExplorerLayoutManager;
 import  org.drools.guvnor.client.messages.Constants;
 import  org.drools.guvnor.client.rpc.MetaData;
 import  org.drools.guvnor.client.rpc.RepositoryServiceFactory;
 import  org.drools.guvnor.client.security.Capabilities;
 
 import  com.google.gwt.core.client.GWT;
 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.ChangeListener;
 import  com.google.gwt.user.client.ui.CheckBox;
 import  com.google.gwt.user.client.ui.ClickListener;
 import  com.google.gwt.user.client.ui.Composite;
 import  com.google.gwt.user.client.ui.HorizontalPanel;
 import  com.google.gwt.user.client.ui.Image;
 import  com.google.gwt.user.client.ui.Label;
 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.gwtext.client.util.Format;
 import  com.gwtext.client.widgets.form.FormPanel;
 import  com.gwtext.client.widgets.form.event.FormPanelListener;
 import  com.gwtext.client.widgets.form.event.FormPanelListenerAdapter;

This displays the metadata for a versionable asset. It also captures edits, but it does not load or save anything itself.

Author(s):
Michael Neale
 
 public class MetaDataWidget extends Composite {
 
     private MetaData        data;
     private boolean         readOnly;
     private String          uuid;
     private Command         metaDataRefreshView;
     private Command         fullRefreshView;
     private VerticalPanel   layout = new VerticalPanel();
     AssetCategoryEditor     ed;
     private FormStyleLayout currentSection;
     private String          currentSectionName;
     private Constants       constants = ((Constants) GWT.create( Constants.class ));
 
     public MetaDataWidget(final MetaData d,
                           final boolean readOnly,
                           final String uuid,
                           final Command metaDataRefreshView,
                           final Command fullRefreshView) {
 
         super();
 
         this. = metaDataRefreshView;
         this. = fullRefreshView;
 
         if (d.format.equals(AssetFormats.DRL) || d.format.equals(AssetFormats.FUNCTION) || d.format.equals(AssetFormats.ENUMERATION)) {
             renderd,
                     readOnly,
                     uuid );
             
         } else {
             Button show = new Button( .showMoreInfo() );
             show.addClickListener( new ClickListener() {
                public void onClick(Widget sender) {
                    .clear();
                    renderd,
                            readOnly,
                            uuid );
                }
            } );
            .add( new SmallLabel( Format.format( "Title: [<b>{0}</b>]",
                                                       d.name ) ) );
            
            .add( show );
        }
        initWidget(  );
    }
    private void render(MetaData d,
                        boolean readOnly,
                        String uuid) {
        //        layout = new Form(new FormConfig() {
        //        	{
        //        		setWidth(250);
        //        		//setHeader(d.name);
        //        		setLabelWidth(75);
        //        		setSurroundWithBox(true);
        //        	}
        //        });
        if ( !readOnly ) {
            Image edit = new ImageButton( "images/edit.gif"//NON-NLS
                                          .RenameThisAsset() );
            edit.addClickListener( new ClickListener() {
                public void onClick(Widget w) {
                    showRenameAssetw );
                }
            } );
            addHeader"images/meta_data.png"//NON-NLS
                       d.name,
                       edit );
        } else {
            addHeader"images/asset_version.png"//NON-NLS
                       d.name,
                       null );
        }
        this. = uuid;
        this. = d;
        this. = readOnly;
        //setWidth("20%");
        loadDatad );
    }
    private void addHeader(String img,
                           String name,
                           Image edit) {
        startSectionname );
        HorizontalPanel hp = new HorizontalPanel();
        hp.add( new SmallLabel( "<b>" + name + "</b>" ) );
        if ( edit != null ) hp.add( edit );
        .addAttribute( .Title(),
                                     hp );
    }
    private void loadData(MetaData d) {
        this. = d;
        addAttribute.CategoriesMetaData(),
                      categories() );
        addAttribute.ModifiedOnMetaData(),
                      readOnlyDate. ) );
        addAttribute.ModifiedByMetaData(),
                      readOnlyText. ) );
        addAttribute.NoteMetaData(),
                      readOnlyText. ) );
        if ( ! ) {
            addAttribute.CreatedOnMetaData(),
                          readOnlyDate. ) );
        }
        addAttribute.CreatedByMetaData(),
                      readOnlyText. ) );
        addAttribute.FormatMetaData(),
                      new SmallLabel( "<b>" + . + "</b>" ) );
        addAttribute.PackageMetaData(),
                      packageEditor. ) );
        addAttribute.IsDisabledMetaData(),
                      editableBooleannew FieldBooleanBinding() {
                                           public boolean getValue() {
                                               return .;
                                           }
                                           public void setValue(boolean val) {
                                               . = val;
                                           }
                                       },
                                       .DisableTip() ) );
        
        addAttribute"UUID:",
        		readOnlyText() );
        
        endSection();
        startSection.OtherMetaData() );
        addAttribute.SubjectMetaData(),
                      editableTextnew FieldBinding() {
                                        public String getValue() {
                                            return .;
                                        }
                                        public void setValue(String val) {
                                            . = val;
                                        }
                                    },
                                    .AShortDescriptionOfTheSubjectMatter() ) );
        addAttribute.TypeMetaData(),
                      editableTextnew FieldBinding() {
                                        public String getValue() {
                                            return .;
                                        }
                                        public void setValue(String val) {
                                            . = val;
                                        }
                                    },
                                    .TypeTip() ) );
        addAttribute.ExternalLinkMetaData(),
                      editableTextnew FieldBinding() {
                                        public String getValue() {
                                            return .;
                                        }
                                        public void setValue(String val) {
                                            . = val;
                                        }
                                    },
                                    .ExternalLinkTip() ) );
        addAttribute.SourceMetaData(),
                      editableTextnew FieldBinding() {
                                        public String getValue() {
                                            return .;
                                        }
                                        public void setValue(String val) {
                                            . = val;
                                        }
                                    },
                                    .SourceMetaDataTip() ) );
        endSectiontrue );
        startSection.VersionHistory() );
        addAttribute.CurrentVersionNumber(),
                      getVersionNumberLabel() );
        if ( ! ) {
            addRownew VersionBrowserthis.,
                                        this.,
                                         ) );
        }
        endSectiontrue );
    }
    private void addRow(VersionBrowser versionBrowser) {
        this..addRow( versionBrowser );
    }
    private void addAttribute(String string,
                              Widget editable) {
        this..addAttribute( string,
                                          editable );
    }
    private void endSection() {
        endSectionfalse );
    }
    private void endSection(boolean collapsed) {
        FormPanel config = new FormPanel();
        config.setTitle(  );
        config.setCollapsible( true );
        config.setCollapsed( collapsed );
        config.add( this. );
        .add( config );
    }
    private void startSection(String name) {
         = new FormStyleLayout();
         = name;
    }
    private Widget packageEditor(final String packageName) {
        if ( this. || !ExplorerLayoutManager.shouldShow( Capabilities.SHOW_PACKAGE_VIEW ) ) {
            return readOnlyTextpackageName );
        } else {
            HorizontalPanel horiz = new HorizontalPanel();
            horiz.setStyleName( "metadata-Widget" ); //NON-NLS
            horiz.add( readOnlyTextpackageName ) );
            Image editPackage = new ImageButton( "images/edit.gif" ); //NON-NLS
            editPackage.addClickListener( new ClickListener() {
                public void onClick(Widget w) {
                    showEditPackagepackageName,
                                     w );
                }
            } );
            horiz.add( editPackage );
            return horiz;
        }
    }
    private void showRenameAsset(Widget source) {
        final FormStylePopup pop = new FormStylePopup( "images/package_large.png"//NON-NLS
                                                       .RenameThisItem() );
        final TextBox box = new TextBox();
        box.setText( . );
        pop.addAttribute( .NewNameAsset(),
                          box );
        Button ok = new Button( .RenameItem() );
        pop.addAttribute( "",
                          ok );
        ok.addClickListener( new ClickListener() {
            public void onClick(Widget w) {
                RepositoryServiceFactory.getService().renameAsset( ,
                                                                   box.getText(),
                                                                   new GenericCallback() {
                                                                       public void onSuccess(Object data) {
                                                                           .execute();
                                                                           Window.alert( .ItemHasBeenRenamed() );
                                                                           pop.hide();
                                                                       }
                                                                   } );
            }
        } );
        pop.show();
    }
    private void showEditPackage(final String pkg,
                                 Widget source) {
        final FormStylePopup pop = new FormStylePopup( "images/package_large.png"//NON-NLS
                                                       .MoveThisItemToAnotherPackage() );
        pop.addAttribute( .CurrentPackage(),
                          new Label( pkg ) );
        final RulePackageSelector sel = new RulePackageSelector();
        pop.addAttribute( .NewPackage(),
                          sel );
        Button ok = new Button( .ChangePackage() );
        pop.addAttribute( "",
                          ok );
        ok.addClickListener( new ClickListener() {
            public void onClick(Widget w) {
                if ( sel.getSelectedPackage().equals( pkg ) ) {
                    Window.alert( .YouNeedToPickADifferentPackageToMoveThisTo() );
                    return;
                }
                RepositoryServiceFactory.getService().changeAssetPackage( ,
                                                                          sel.getSelectedPackage(),
                                                                          Format.format( .MovedFromPackage(),
                                                                                         pkg ),
                                                                          new GenericCallback() {
                                                                              public void onSuccess(Object data) {
                                                                                  .execute();
                                                                                  pop.hide();
                                                                              }
                                                                          } );
            }
        } );
        pop.show();
    }
    private Widget getVersionNumberLabel() {
        if ( . == 0 ) {
            return new SmallLabel( .NotCheckedInYet() );
        } else {
            return readOnlyText( Long.toString. ) );
        }
    }
    private Widget readOnlyDate(Date lastModifiedDate) {
        if ( lastModifiedDate == null ) {
            return null;
        } else {
            return new SmallLabel( lastModifiedDate.toLocaleString() );
        }
    }
    private Label readOnlyText(String text) {
        SmallLabel lbl = new SmallLabel( text );
        lbl.setWidth( "100%" );
        return lbl;
    }
    private Widget categories() {
         = new AssetCategoryEditorthis.,
                                      this. );
        return ;
    }

    
This binds a field, and returns a text editor for it
    private Widget editableText(final FieldBinding bind,
                                String toolTip) {
        if ( ! ) {
            final TextBox box = new TextBox();
            box.setTitle( toolTip );
            box.setText( bind.getValue() );
            box.setVisibleLength( 10 );
            ChangeListener listener = new ChangeListener() {
                public void onChange(Widget w) {
                    bind.setValuebox.getText() );
                }
            };
            box.addChangeListener( listener );
            return box;
        } else {
            return new Label( bind.getValue() );
        }
    }

    
This binds a field, and returns a check box editor for it.

Parameters:
bind Interface to bind to.
toolTip tool tip.
Returns:
    private Widget editableBoolean(final FieldBooleanBinding bind,
                                   String toolTip) {
        if ( ! ) {
            final CheckBox box = new CheckBox();
            box.setTitle( toolTip );
            box.setChecked( bind.getValue() );
            ClickListener listener = new ClickListener() {
                public void onClick(Widget w) {
                    boolean b = box.isChecked();
                    bind.setValueb );
                }
            };
            box.addClickListener( listener );
            return box;
        } else {
            final CheckBox box = new CheckBox();
            box.setChecked( bind.getValue() );
            box.setEnabled( false );
            return box;
        }
    }

    
used to bind fields in the meta data DTO to the form
    static interface FieldBinding {
        void setValue(String val);
        String getValue();
    }

    
used to bind fields in the meta data DTO to the form
    static interface FieldBooleanBinding {
        void setValue(boolean val);
        boolean getValue();
    }

    
Return the data if it is to be saved.
    public MetaData getData() {
        return ;
    }
New to GrepCode? Check out our FAQ X