Commits

Anonymous committed b666f7d

#171418 - setter with fluent interface

  • Participants
  • Parent commits f33e914

Comments (0)

Files changed (5)

php.editor/src/org/netbeans/modules/php/editor/codegen/CGSGenerator.java

     private static final String UP_FIRST_LETTER_PROPERTY = "${UpFirstLetterProperty}";  //NOI18N
     private static final String UP_FIRST_LETTER_PROPERTY_WITHOUT_UNDERSCORE = "${UpFirstLetterPropertyWithoutUnderscore}";  //NOI18N
     private static final String PROPERTY_WITHOUT_UNDERSCORE = "${PropertyWithoutUnderscore}";  //NOI18N
+    private static final String FLUENT_SETTER = "${FluentSetter}"; //NOI18N
 
     public enum GenType {
         CONSTRUCTOR {
                 return setters.toString();
             }
 
+            @Override
+            public boolean isFluentSetterComboVisible() {
+                return true;
+            }
+
         },
         GETTER_AND_SETTER {
 
                 return gettersAndSetters.toString();
             }
 
+            @Override
+            public boolean isFluentSetterComboVisible() {
+                return true;
+            }
+
         },
         METHODS {
 
             return new ConstructorPanel(this, cgsInfo);
         }
 
+        public boolean isFluentSetterComboVisible() {
+            return false;
+        }
+
         String getGetterTemplate(final CGSInfo cgsInfo) {
             return GETTER_TEMPLATE.replace(TEMPLATE_NAME, cgsInfo.getHowToGenerate().getGetterTemplate());
         }
 
         String getSetterTemplate(final CGSInfo cgsInfo) {
-            return SETTER_TEMPLATE.replace(TEMPLATE_NAME, cgsInfo.getHowToGenerate().getSetterTemplate());
+            final String preparedSetterTemplate = SETTER_TEMPLATE.replace(TEMPLATE_NAME, cgsInfo.getHowToGenerate().getSetterTemplate());
+            return cgsInfo.isFluentSetter() ? preparedSetterTemplate.replace(FLUENT_SETTER, "return $this;" + NEW_LINE) : preparedSetterTemplate.replace(FLUENT_SETTER, ""); //NOI18N
         }
 
     }
     private static final String CONSTRUCTOR_TEMPLATE = "function __construct(" + PARAMS + ") {" + ASSIGNMENTS  + CURSOR + NEW_LINE + "}" + NEW_LINE;    //NOI18N
     private static final String ASSIGNMENT_TEMPLATE = NEW_LINE + "$this->" + PROPERTY + " = $" + PARAM_NAME + ";";          //NOI18N
     private static final String GETTER_TEMPLATE = "public function " + TEMPLATE_NAME + "() {" + NEW_LINE + "return $$this->" + PROPERTY + ";" + NEW_LINE + "}" + NEW_LINE;    //NOI18N
-    private static final String SETTER_TEMPLATE = "public function " + TEMPLATE_NAME + "($$" + PARAM_NAME + ") {" + ASSIGNMENT_TEMPLATE + NEW_LINE + "}" + NEW_LINE; //NOI18N
+    private static final String SETTER_TEMPLATE = "public function " + TEMPLATE_NAME + "($$" + PARAM_NAME + ") {" + ASSIGNMENT_TEMPLATE + NEW_LINE + FLUENT_SETTER + "}" + NEW_LINE; //NOI18N
     private final GenType type;
     private final CGSInfo cgsInfo;
     private final JTextComponent component;
 
     private static final String GETTER_SETTER_PROJECT_PROPERTY = "getter.setter.method.name.generation";
+    private static final String FLUENT_SETTER_PROJECT_PROPERTY = "fluent.setter.project.property"; //NOI18N
 
     private CGSGenerator(JTextComponent component, CGSInfo cgsInfo, GenType type) {
         this.type = type;
         String methodGenerationWay = null;
         AntProjectHelper helper = null;
         EditableProperties properties = null;
+        boolean fluentSetter = false;
 
         // obtain the generation from project properties
         FileObject fo = NbEditorUtilities.getFileObject(component.getDocument());
             helper = project.getLookup().lookup(AntProjectHelper.class);
             properties = helper.getProperties(AntProjectHelper.PRIVATE_PROPERTIES_PATH);
             methodGenerationWay = properties.getProperty(GETTER_SETTER_PROJECT_PROPERTY);
+            fluentSetter = Boolean.valueOf(properties.getProperty(FLUENT_SETTER_PROJECT_PROPERTY));
         }
         if (methodGenerationWay != null) {
             try {
         } else {
             cgsInfo.setHowToGenerate(GenWay.AS_JAVA);
         }
+        cgsInfo.setFluentSetter(fluentSetter);
         DialogDescriptor desc = new DialogDescriptor(type.createPanel(cgsInfo), type.getDialogTitle());
         Dialog dialog = DialogDisplayer.getDefault().createDialog(desc);
         dialog.setVisible(true);
             //save the gen type value to the project properties
             if (project != null) {
                 properties.put(GETTER_SETTER_PROJECT_PROPERTY, cgsInfo.getHowToGenerate().name());
+                properties.put(FLUENT_SETTER_PROJECT_PROPERTY, String.valueOf(cgsInfo.isFluentSetter()));
                 helper.putProperties(AntProjectHelper.PRIVATE_PROPERTIES_PATH, properties);
             }
         }

php.editor/src/org/netbeans/modules/php/editor/codegen/CGSInfo.java

  */
 package org.netbeans.modules.php.editor.codegen;
 
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.HashSet;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.Set;
+import java.util.*;
 import javax.swing.text.JTextComponent;
 import org.netbeans.modules.csl.spi.ParserResult;
 import org.netbeans.modules.parsing.api.ParserManager;
 import org.netbeans.modules.php.editor.api.ElementQueryFactory;
 import org.netbeans.modules.php.editor.api.NameKind;
 import org.netbeans.modules.php.editor.api.QualifiedName;
-import org.netbeans.modules.php.editor.api.elements.ClassElement;
-import org.netbeans.modules.php.editor.api.elements.ElementFilter;
-import org.netbeans.modules.php.editor.api.elements.ElementTransformation;
-import org.netbeans.modules.php.editor.api.elements.MethodElement;
-import org.netbeans.modules.php.editor.api.elements.TypeElement;
-import org.netbeans.modules.php.editor.api.elements.TreeElement;
+import org.netbeans.modules.php.editor.api.elements.*;
 import org.netbeans.modules.php.editor.codegen.CGSGenerator.GenWay;
 import org.netbeans.modules.php.editor.model.impl.VariousUtils;
 import org.netbeans.modules.php.editor.nav.NavUtils;
      */
     private CGSGenerator.GenWay howToGenerate;
     private boolean generateDoc;
+    private boolean fluentSetter;
 
     private CGSInfo(JTextComponent textComp) {
         properties = new ArrayList<Property>();
         this.textComp = textComp;
         hasConstructor = false;
         this.generateDoc = true;
+        fluentSetter = false;
         this.howToGenerate = CGSGenerator.GenWay.AS_JAVA;
     }
 
         this.generateDoc = generateDoc;
     }
 
+    public boolean isFluentSetter() {
+        return fluentSetter;
+    }
+
+    public void setFluentSetter(final boolean fluentSetter) {
+        this.fluentSetter = fluentSetter;
+    }
 
     /**
      * Extract attributes and methods from caret enclosing class and initialize list of properties.

php.editor/src/org/netbeans/modules/php/editor/codegen/ui/Bundle.properties

 TableGeneratorPanel.tableLabel.AccessibleContext.accessibleName=Table
 TableGeneratorPanel.connVariableLabel.AccessibleContext.accessibleName=Connection Variable
 LBL_Generate_Documentation=Generate documentation stub
+ConstructorPanel.fluentSetterCheckBox.text=Fluent Setter

php.editor/src/org/netbeans/modules/php/editor/codegen/ui/ConstructorPanel.form

   <Layout>
     <DimensionLayout dim="0">
       <Group type="103" groupAlignment="0" attributes="0">
-          <Group type="102" alignment="1" attributes="0">
+          <Group type="102" attributes="0">
               <EmptySpace max="-2" attributes="0"/>
-              <Group type="103" groupAlignment="1" attributes="0">
+              <Group type="103" groupAlignment="0" attributes="0">
                   <Component id="scrollPane" alignment="0" pref="422" max="32767" attributes="0"/>
-                  <Component id="label" alignment="0" min="-2" max="-2" attributes="0"/>
                   <Component id="cbGenerateDoc" alignment="0" pref="422" max="32767" attributes="0"/>
                   <Component id="pGSCustomize" alignment="0" max="32767" attributes="0"/>
+                  <Group type="102" attributes="0">
+                      <Group type="103" groupAlignment="0" attributes="0">
+                          <Component id="label" alignment="0" min="-2" max="-2" attributes="0"/>
+                          <Component id="fluentSetterCheckBox" alignment="0" min="-2" max="-2" attributes="0"/>
+                      </Group>
+                      <EmptySpace min="0" pref="0" max="32767" attributes="0"/>
+                  </Group>
               </Group>
               <EmptySpace max="-2" attributes="0"/>
           </Group>
               <EmptySpace max="-2" attributes="0"/>
               <Component id="label" min="-2" max="-2" attributes="0"/>
               <EmptySpace max="-2" attributes="0"/>
-              <Component id="scrollPane" pref="226" max="32767" attributes="0"/>
+              <Component id="scrollPane" pref="206" max="32767" attributes="0"/>
               <EmptySpace max="-2" attributes="0"/>
               <Component id="pGSCustomize" min="-2" pref="45" max="-2" attributes="0"/>
               <EmptySpace max="-2" attributes="0"/>
               <Component id="cbGenerateDoc" min="-2" max="-2" attributes="0"/>
               <EmptySpace max="-2" attributes="0"/>
+              <Component id="fluentSetterCheckBox" min="-2" max="-2" attributes="0"/>
+              <EmptySpace max="-2" attributes="0"/>
           </Group>
       </Group>
     </DimensionLayout>
         <EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="cbGenerateDocActionPerformed"/>
       </Events>
     </Component>
+    <Component class="javax.swing.JCheckBox" name="fluentSetterCheckBox">
+      <Properties>
+        <Property name="mnemonic" type="int" value="70"/>
+        <Property name="text" type="java.lang.String" editor="org.netbeans.modules.i18n.form.FormI18nStringEditor">
+          <ResourceString bundle="org/netbeans/modules/php/editor/codegen/ui/Bundle.properties" key="ConstructorPanel.fluentSetterCheckBox.text" replaceFormat="org.openide.util.NbBundle.getMessage({sourceFileName}.class, &quot;{key}&quot;)"/>
+        </Property>
+      </Properties>
+      <Events>
+        <EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="fluentSetterCheckBoxActionPerformed"/>
+      </Events>
+    </Component>
   </SubComponents>
 </Form>

php.editor/src/org/netbeans/modules/php/editor/codegen/ui/ConstructorPanel.java

         }
         cbGenerateDoc.setSelected(cgsInfo.isGenerateDoc());
         cbGenerateDoc.setVisible(false);
+        fluentSetterCheckBox.setVisible(genType.isFluentSetterComboVisible());
+        fluentSetterCheckBox.setSelected(cgsInfo.isFluentSetter());
     }
 
     private void initTree(){
         jLabel1 = new javax.swing.JLabel();
         cbMethodGeneration = new javax.swing.JComboBox();
         cbGenerateDoc = new javax.swing.JCheckBox();
+        fluentSetterCheckBox = new javax.swing.JCheckBox();
 
         label.setDisplayedMnemonic('G');
         label.setLabelFor(scrollPane);
             }
         });
 
+        fluentSetterCheckBox.setMnemonic('F');
+        fluentSetterCheckBox.setText(org.openide.util.NbBundle.getMessage(ConstructorPanel.class, "ConstructorPanel.fluentSetterCheckBox.text")); // NOI18N
+        fluentSetterCheckBox.addActionListener(new java.awt.event.ActionListener() {
+            public void actionPerformed(java.awt.event.ActionEvent evt) {
+                fluentSetterCheckBoxActionPerformed(evt);
+            }
+        });
+
         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
         this.setLayout(layout);
         layout.setHorizontalGroup(
             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
-            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
+            .addGroup(layout.createSequentialGroup()
                 .addContainerGap()
-                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
-                    .addComponent(scrollPane, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 422, Short.MAX_VALUE)
-                    .addComponent(label, javax.swing.GroupLayout.Alignment.LEADING)
-                    .addComponent(cbGenerateDoc, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 422, Short.MAX_VALUE)
-                    .addComponent(pGSCustomize, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
+                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
+                    .addComponent(scrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 422, Short.MAX_VALUE)
+                    .addComponent(cbGenerateDoc, javax.swing.GroupLayout.DEFAULT_SIZE, 422, Short.MAX_VALUE)
+                    .addComponent(pGSCustomize, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
+                    .addGroup(layout.createSequentialGroup()
+                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
+                            .addComponent(label)
+                            .addComponent(fluentSetterCheckBox))
+                        .addGap(0, 0, Short.MAX_VALUE)))
                 .addContainerGap())
         );
         layout.setVerticalGroup(
                 .addContainerGap()
                 .addComponent(label)
                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
-                .addComponent(scrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 226, Short.MAX_VALUE)
+                .addComponent(scrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 206, Short.MAX_VALUE)
                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                 .addComponent(pGSCustomize, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                 .addComponent(cbGenerateDoc)
+                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
+                .addComponent(fluentSetterCheckBox)
                 .addContainerGap())
         );
 
         cgsInfo.setGenerateDoc(cbGenerateDoc.isSelected());
     }//GEN-LAST:event_cbGenerateDocActionPerformed
 
+    private void fluentSetterCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fluentSetterCheckBoxActionPerformed
+        cgsInfo.setFluentSetter(fluentSetterCheckBox.isSelected());
+    }//GEN-LAST:event_fluentSetterCheckBoxActionPerformed
+
 
     // Variables declaration - do not modify//GEN-BEGIN:variables
     private javax.swing.JCheckBox cbGenerateDoc;
     private javax.swing.JComboBox cbMethodGeneration;
+    private javax.swing.JCheckBox fluentSetterCheckBox;
     private javax.swing.JLabel jLabel1;
     private javax.swing.JLabel label;
     private javax.swing.JPanel pGSCustomize;