View Javadoc
1   /*
2    * Copyright (c) 2022 fbahle.
3    * All rights reserved. This program and the accompanying materials
4    * are made available under the terms of the Eclipse Public License v2.0
5    * which accompanies this distribution, and is available at
6    * http://www.eclipse.org/legal/epl-v20.html
7    * Contributors:
8    * fbahle - initial API and implementation and/or initial documentation
9    */
10  
11  package de.funfried.netbeans.plugins.external.formatter.java.base.actions;
12  
13  import java.awt.event.ActionEvent;
14  
15  import javax.swing.text.Document;
16  import javax.swing.text.JTextComponent;
17  
18  import org.netbeans.api.editor.EditorActionRegistration;
19  import org.netbeans.editor.BaseAction;
20  import org.netbeans.editor.ext.ExtKit;
21  import org.openide.util.Lookup;
22  import org.openide.util.NbBundle;
23  import org.openide.util.lookup.Lookups;
24  
25  import de.funfried.netbeans.plugins.external.formatter.FormatterServiceDelegate;
26  import de.funfried.netbeans.plugins.external.formatter.ui.editor.EditorUtils;
27  
28  /**
29   * {@link BaseAction} for fixing imports which overrides the NetBeans original action.
30   *
31   * @author fbahle
32   */
33  @EditorActionRegistration(name = "fix-imports", mimeType = "text/x-java", shortDescription = "#desc-fix-imports", popupText = "#popup-fix-imports")
34  public class JavaFixImportsAction extends BaseAction {
35  	private static final long serialVersionUID = -3969332137881749109L;
36  
37  	public JavaFixImportsAction() {
38  		super(BaseAction.ABBREV_RESET | BaseAction.MAGIC_POSITION_RESET | BaseAction.UNDO_MERGE_RESET);
39  		putValue(ExtKit.TRIMMED_TEXT, NbBundle.getMessage(JavaFixImportsAction.class, "fix-imports-trimmed"));
40  		putValue(BaseAction.SHORT_DESCRIPTION, NbBundle.getMessage(JavaFixImportsAction.class, "desc-fix-imports")); // NOI18N
41  		putValue(BaseAction.POPUP_MENU_TEXT, NbBundle.getMessage(JavaFixImportsAction.class, "popup-fix-imports")); // NOI18N
42  	}
43  
44  	/**
45  	 * {@inheritDoc}
46  	 */
47  	@Override
48  	public void actionPerformed(ActionEvent evt, JTextComponent target) {
49  		if (target != null) {
50  			Document document = target.getDocument();
51  			Lookup lookup = Lookups.forPath("extFormatters/backupOrgActions/fixImports");
52  			BaseAction action = lookup.lookup(BaseAction.class);
53  			action.actionPerformed(evt, target);
54  
55  			FormatterServiceDelegate.getInstance().organizeImports(EditorUtils.toStyledDocument(document), true);
56  		}
57  	}
58  }