3232import consulo .language .psi .PsiElement ;
3333import consulo .localize .LocalizeValue ;
3434import consulo .project .Project ;
35+ import consulo .ui .annotation .RequiredUIAccess ;
3536import consulo .ui .ex .awt .DialogWrapper ;
3637import consulo .ui .ex .awt .UIUtil ;
3738import consulo .util .collection .MultiMap ;
3839import consulo .util .lang .StringUtil ;
39- import org .jetbrains .annotations .NonNls ;
40-
4140import jakarta .annotation .Nonnull ;
4241
4342import javax .swing .*;
4443import java .awt .*;
45- import java .awt .event .ActionEvent ;
4644import java .awt .event .ActionListener ;
4745import java .util .Enumeration ;
48- import java .util .function .Function ;
4946
5047public class ExtractMethodObjectDialog extends DialogWrapper implements AbstractExtractDialog {
5148 private final Project myProject ;
@@ -122,7 +119,6 @@ public ExtractMethodObjectDialog(
122119
123120 // Create UI components
124121
125-
126122 myCbMakeVarargs .setVisible (canBeVarargs );
127123 myCbMakeVarargsAnonymous .setVisible (canBeVarargs );
128124
@@ -131,6 +127,7 @@ public ExtractMethodObjectDialog(
131127
132128 }
133129
130+ @ Override
134131 public boolean isMakeStatic () {
135132 if (myStaticFlag ) {
136133 return true ;
@@ -141,6 +138,7 @@ public boolean isMakeStatic() {
141138 return myCbMakeStatic .isSelected ();
142139 }
143140
141+ @ Override
144142 public boolean isChainedConstructor () {
145143 return false ;
146144 }
@@ -151,6 +149,7 @@ public PsiType getReturnType() {
151149 }
152150
153151 @ Nonnull
152+ @ Override
154153 protected Action [] createActions () {
155154 return new Action []{
156155 getOKAction (),
@@ -159,22 +158,30 @@ protected Action[] createActions() {
159158 };
160159 }
161160
161+ @ Override
162162 public String getChosenMethodName () {
163163 return myCreateInnerClassRb .isSelected () ? myInnerClassName .getText () : myMethodName .getText ();
164164 }
165165
166+ @ Override
166167 public VariableData [] getChosenParameters () {
167168 return myInputVariables ;
168169 }
169170
171+ @ Override
172+ @ RequiredUIAccess
170173 public JComponent getPreferredFocusedComponent () {
171174 return myInnerClassName ;
172175 }
173176
177+ @ Override
178+ @ RequiredUIAccess
174179 protected void doHelpAction () {
175180 HelpManager .getInstance ().invokeHelp (HelpID .EXTRACT_METHOD_OBJECT );
176181 }
177182
183+ @ Override
184+ @ RequiredUIAccess
178185 protected void doOKAction () {
179186 MultiMap <PsiElement , LocalizeValue > conflicts = new MultiMap <>();
180187 if (myCreateInnerClassRb .isSelected ()) {
@@ -201,8 +208,8 @@ protected void doOKAction() {
201208 JCheckBox makeVarargsCb = myCreateInnerClassRb .isSelected () ? myCbMakeVarargs : myCbMakeVarargsAnonymous ;
202209 if (makeVarargsCb != null && makeVarargsCb .isSelected ()) {
203210 VariableData data = myInputVariables [myInputVariables .length - 1 ];
204- if (data .type instanceof PsiArrayType ) {
205- data .type = new PsiEllipsisType ((( PsiArrayType ) data . type ) .getComponentType ());
211+ if (data .type instanceof PsiArrayType arrayType ) {
212+ data .type = new PsiEllipsisType (arrayType .getComponentType ());
206213 }
207214 }
208215 super .doOKAction ();
@@ -222,11 +229,13 @@ private void update() {
222229 myCbMakeStatic .setEnabled (myCreateInnerClassRb .isSelected () && myCanBeStatic && !myStaticFlag );
223230 updateSignature ();
224231 PsiNameHelper helper = PsiNameHelper .getInstance (myProject );
225- setOKActionEnabled ((myCreateInnerClassRb .isSelected () && helper .isIdentifier (myInnerClassName .getText ())) || (!myCreateInnerClassRb .isSelected () && helper .isIdentifier (
226- myMethodName .getText ()
227- )));
232+ setOKActionEnabled (
233+ (myCreateInnerClassRb .isSelected () && helper .isIdentifier (myInnerClassName .getText ()))
234+ || (!myCreateInnerClassRb .isSelected () && helper .isIdentifier (myMethodName .getText ()))
235+ );
228236 }
229237
238+ @ Override
230239 public String getVisibility () {
231240 if (myPublicRadioButton .isSelected ()) {
232241 return PsiModifier .PUBLIC ;
@@ -243,16 +252,12 @@ public String getVisibility() {
243252 return null ;
244253 }
245254
246-
255+ @ Override
247256 protected JComponent createCenterPanel () {
248257 mySignatureArea .setEditable (false );
249258 myCreateInnerClassRb .setSelected (true );
250259
251- ActionListener enableDisableListener = new ActionListener () {
252- public void actionPerformed (ActionEvent e ) {
253- enable (myCreateInnerClassRb .isSelected ());
254- }
255- };
260+ ActionListener enableDisableListener = e -> enable (myCreateInnerClassRb .isSelected ());
256261 myCreateInnerClassRb .addActionListener (enableDisableListener );
257262 myCreateAnonymousClassWrapperRb .addActionListener (enableDisableListener );
258263 myCreateAnonymousClassWrapperRb .setEnabled (!myMultipleExitPoints );
@@ -261,33 +266,29 @@ public void actionPerformed(ActionEvent e) {
261266 myFoldCb .setVisible (myVariableData .isFoldable ());
262267 myVariableData .setFoldingAvailable (myFoldCb .isSelected ());
263268 myInputVariables = myVariableData .getInputVariables ().toArray (new VariableData [myVariableData .getInputVariables ().size ()]);
264- myFoldCb .addActionListener (new ActionListener () {
265- public void actionPerformed (ActionEvent e ) {
266- myVariableData .setFoldingAvailable (myFoldCb .isSelected ());
267- myInputVariables = myVariableData .getInputVariables ().toArray (new VariableData [myVariableData .getInputVariables ().size ()]);
268- myParametersTableContainer .removeAll ();
269- myParametersTableContainer .add (createParametersPanel (), BorderLayout .CENTER );
270- myParametersTableContainer .revalidate ();
271- updateSignature ();
272- updateVarargsEnabled ();
273- }
269+ myFoldCb .addActionListener (e -> {
270+ myVariableData .setFoldingAvailable (myFoldCb .isSelected ());
271+ myInputVariables = myVariableData .getInputVariables ().toArray (new VariableData [myVariableData .getInputVariables ().size ()]);
272+ myParametersTableContainer .removeAll ();
273+ myParametersTableContainer .add (createParametersPanel (), BorderLayout .CENTER );
274+ myParametersTableContainer .revalidate ();
275+ updateSignature ();
276+ updateVarargsEnabled ();
274277 });
275278 myParametersTableContainer .add (createParametersPanel (), BorderLayout .CENTER );
276279
277- ActionListener updateSugnatureListener = new ActionListener () {
278- public void actionPerformed (ActionEvent e ) {
279- updateSignature ();
280- ApplicationIdeFocusManager .getInstance ()
281- .getInstanceForProject (myProject )
282- .requestFocus (myCreateInnerClassRb .isSelected () ? myInnerClassName : myMethodName , false );
283- }
280+ ActionListener updateSignatureListener = e -> {
281+ updateSignature ();
282+ ApplicationIdeFocusManager .getInstance ()
283+ .getInstanceForProject (myProject )
284+ .requestFocus (myCreateInnerClassRb .isSelected () ? myInnerClassName : myMethodName , false );
284285 };
285286
286287 if (myStaticFlag || myCanBeStatic ) {
287288 myCbMakeStatic .setEnabled (!myStaticFlag );
288289 myCbMakeStatic .setSelected (myStaticFlag );
289290
290- myCbMakeStatic .addActionListener (updateSugnatureListener );
291+ myCbMakeStatic .addActionListener (updateSignatureListener );
291292 }
292293 else {
293294 myCbMakeStatic .setSelected (false );
@@ -297,10 +298,10 @@ public void actionPerformed(ActionEvent e) {
297298 updateVarargsEnabled ();
298299
299300 myCbMakeVarargs .setSelected (myWasStatic );
300- myCbMakeVarargs .addActionListener (updateSugnatureListener );
301+ myCbMakeVarargs .addActionListener (updateSignatureListener );
301302
302303 myCbMakeVarargsAnonymous .setSelected (myWasStatic );
303- myCbMakeVarargsAnonymous .addActionListener (updateSugnatureListener );
304+ myCbMakeVarargsAnonymous .addActionListener (updateSignatureListener );
304305
305306 DocumentAdapter nameListener = new DocumentAdapter () {
306307 @ Override
@@ -313,12 +314,12 @@ public void documentChanged(DocumentEvent e) {
313314
314315 myPrivateRadioButton .setSelected (true );
315316
316- myCreateInnerClassRb .addActionListener (updateSugnatureListener );
317- myCreateAnonymousClassWrapperRb .addActionListener (updateSugnatureListener );
317+ myCreateInnerClassRb .addActionListener (updateSignatureListener );
318+ myCreateAnonymousClassWrapperRb .addActionListener (updateSignatureListener );
318319
319320 Enumeration <AbstractButton > visibilities = myVisibilityGroup .getElements ();
320321 while (visibilities .hasMoreElements ()) {
321- visibilities .nextElement ().addActionListener (updateSugnatureListener );
322+ visibilities .nextElement ().addActionListener (updateSignatureListener );
322323 }
323324
324325 enable (true );
@@ -333,15 +334,18 @@ private void enable(boolean innerClassSelected) {
333334
334335 private JComponent createParametersPanel () {
335336 return new ParameterTablePanel (myProject , myInputVariables , myElementsToExtract ) {
337+ @ Override
336338 protected void updateSignature () {
337339 updateVarargsEnabled ();
338340 ExtractMethodObjectDialog .this .updateSignature ();
339341 }
340342
343+ @ Override
341344 protected void doEnterAction () {
342345 clickDefaultButton ();
343346 }
344347
348+ @ Override
345349 protected void doCancelAction () {
346350 ExtractMethodObjectDialog .this .doCancelAction ();
347351 }
@@ -361,13 +365,13 @@ protected void updateSignature() {
361365 if (mySignatureArea == null ) {
362366 return ;
363367 }
364- @ NonNls StringBuffer buffer = getSignature ();
368+ StringBuilder buffer = getSignature ();
365369 mySignatureArea .setText (buffer .toString ());
366370 }
367371
368- protected StringBuffer getSignature () {
369- String INDENT = " " ;
370- @ NonNls StringBuffer buffer = new StringBuffer ();
372+ protected StringBuilder getSignature () {
373+ String indent = " " ;
374+ StringBuilder buffer = new StringBuilder ();
371375 String visibilityString = VisibilityUtil .getVisibilityString (getVisibility ());
372376 if (myCreateInnerClassRb .isSelected ()) {
373377 buffer .append (visibilityString );
@@ -384,67 +388,63 @@ protected StringBuffer getSignature() {
384388 buffer .append (" " );
385389 }
386390 buffer .append ("{\n " );
387- buffer .append (INDENT );
391+ buffer .append (indent );
388392 buffer .append ("public " );
389393 buffer .append (myInnerClassName .getText ());
390- methodSignature (INDENT , buffer );
394+ methodSignature (indent , buffer );
391395 buffer .append ("\n }" );
392396 }
393397 else {
394398 buffer .append ("new Object(){\n " );
395- buffer .append (INDENT );
399+ buffer .append (indent );
396400 buffer .append ("private " );
397401 buffer .append (PsiFormatUtil .formatType (myReturnType , 0 , PsiSubstitutor .EMPTY ));
398402 buffer .append (" " );
399403 buffer .append (myMethodName .getText ());
400- methodSignature (INDENT , buffer );
404+ methodSignature (indent , buffer );
401405 buffer .append ("\n }." );
402406 buffer .append (myMethodName .getText ());
403407 buffer .append ("(" );
404- buffer .append (StringUtil .join (myInputVariables , new Function <VariableData , String >() {
405- public String apply (VariableData variableData ) {
406- return variableData .name ;
407- }
408- }, ", " ));
408+ buffer .append (StringUtil .join (myInputVariables , variableData -> variableData .name , ", " ));
409409 buffer .append (")" );
410410 }
411411
412412 return buffer ;
413413 }
414414
415- private void methodSignature (String INDENT , StringBuffer buffer ) {
415+ private void methodSignature (String indent , StringBuilder buffer ) {
416416 buffer .append ("(" );
417417 int count = 0 ;
418- String indent = " " ;
419418 for (int i = 0 ; i < myInputVariables .length ; i ++) {
420419 VariableData data = myInputVariables [i ];
421- if (data .passAsParameter ) {
422- //String typeAndModifiers = PsiFormatUtil.formatVariable(data.variable,
423- // PsiFormatUtil.SHOW_MODIFIERS | PsiFormatUtil.SHOW_TYPE);
424- PsiType type = data .type ;
425- if (i == myInputVariables .length - 1 && type instanceof PsiArrayType && ((myCreateInnerClassRb .isSelected () && myCbMakeVarargs .isSelected ()) || (myCreateAnonymousClassWrapperRb
426- .isSelected () && myCbMakeVarargsAnonymous .isSelected ()))) {
427- type = new PsiEllipsisType (((PsiArrayType ) type ).getComponentType ());
428- }
420+ if (!data .passAsParameter ) {
421+ continue ;
422+ }
429423
430- String typeText = type . getPresentableText () ;
431- if ( count > 0 ) {
432- buffer . append ( ", " );
433- }
434- buffer . append ( " \n " );
435- buffer . append ( indent );
436- buffer . append ( typeText );
437- buffer . append ( " " );
438- buffer . append ( data . name );
439- count ++ ;
424+ PsiType type = data . type ;
425+ if ( i == myInputVariables . length - 1 && type instanceof PsiArrayType arrayType
426+ && (( myCreateInnerClassRb . isSelected () && myCbMakeVarargs . isSelected ())
427+ || ( myCreateAnonymousClassWrapperRb . isSelected () && myCbMakeVarargsAnonymous . isSelected ()))) {
428+ type = new PsiEllipsisType ( arrayType . getComponentType () );
429+ }
430+
431+ String typeText = type . getPresentableText ( );
432+ if ( count > 0 ) {
433+ buffer . append ( ", " ) ;
440434 }
435+ buffer .append ("\n " );
436+ buffer .append (indent );
437+ buffer .append (typeText );
438+ buffer .append (" " );
439+ buffer .append (data .name );
440+ count ++;
441441 }
442442 buffer .append (")" );
443443 if (myExceptions .length > 0 ) {
444444 buffer .append ("\n " );
445445 buffer .append ("throws\n " );
446446 for (PsiType exception : myExceptions ) {
447- buffer .append (INDENT );
447+ buffer .append (indent );
448448 buffer .append (PsiFormatUtil .formatType (exception , 0 , PsiSubstitutor .EMPTY ));
449449 buffer .append ("\n " );
450450 }
0 commit comments