2222import com .intellij .ui .EditorTextField ;
2323import com .microsoft .azure .toolkit .intellij .azuresdk .model .AzureSdkArtifactEntity ;
2424import com .microsoft .azure .toolkit .intellij .azuresdk .model .AzureSdkArtifactEntity .DependencyType ;
25- import com .microsoft .azure .toolkit .intellij .azuresdk .model .module .GradleProjectModule ;
26- import com .microsoft .azure .toolkit .intellij .azuresdk .model .module .MavenProjectModule ;
27- import com .microsoft .azure .toolkit .intellij .azuresdk .model .module .ProjectModule ;
28- import com .microsoft .azure .toolkit .intellij .azuresdk .referencebook .components .ModuleComboBox ;
29- import com .microsoft .azure .toolkit .lib .common .bundle .AzureString ;
30- import com .microsoft .azure .toolkit .lib .common .messager .AzureMessager ;
31- import com .microsoft .azure .toolkit .lib .common .messager .IAzureMessage ;
32- import com .microsoft .azure .toolkit .lib .common .messager .IAzureMessager ;
33- import com .microsoft .azure .toolkit .lib .common .operation .AzureOperation ;
34- import com .microsoft .azure .toolkit .lib .common .operation .OperationContext ;
35- import com .microsoft .azure .toolkit .lib .common .task .AzureTask ;
36- import com .microsoft .azure .toolkit .lib .common .task .AzureTaskManager ;
3725import com .microsoft .intellij .util .GradleUtils ;
3826import com .microsoft .intellij .util .MavenUtils ;
3927import icons .GradleIcons ;
4028import icons .OpenapiIcons ;
4129import lombok .Getter ;
42- import org .apache .commons .lang .StringUtils ;
43- import org .apache .maven .artifact .versioning .ComparableVersion ;
4430import org .jetbrains .annotations .NotNull ;
45- import org .jetbrains .idea .maven .model .MavenArtifact ;
46- import org .jetbrains .plugins .gradle .model .ExternalDependency ;
4731
4832import javax .annotation .Nonnull ;
4933import javax .annotation .Nullable ;
5236import java .awt .datatransfer .StringSelection ;
5337import java .util .ArrayList ;
5438import java .util .List ;
55- import java .util .Optional ;
5639import java .util .function .Consumer ;
5740
5841import static com .microsoft .azure .toolkit .intellij .azuresdk .model .AzureSdkArtifactEntity .DependencyType .GRADLE ;
5942import static com .microsoft .azure .toolkit .intellij .azuresdk .model .AzureSdkArtifactEntity .DependencyType .MAVEN ;
6043
6144public class AzureSdkArtifactGroupPanel {
62- public static final String DEPENDENCY_ADDED = "Dependency Added" ;
63- public static final String ADD_DEPENDENCY = "Add Dependency" ;
64- public static final String UPDATE_DEPENDENCY = "Update Dependency" ;
6545 @ Getter
6646 private JPanel contentPanel ;
6747 private EditorTextField viewer ;
6848 private JPanel artifactsPnl ;
6949 private ActionToolbarImpl toolbar ;
70- private JPanel pnlAddDependencies ;
71- private ModuleComboBox cbModule ;
72- private JButton btnAddDependency ;
73- private JTextPane paneMessage ;
50+ private AzureSdkProjectDependencyPanel pnlAddDependencies ;
7451 private ButtonGroup artifactsGroup ;
7552 private final List <AzureSdkArtifactDetailPanel > artifactPnls = new ArrayList <>();
7653 private AzureSdkArtifactEntity pkg ;
7754 private String version ;
7855 private static DependencyType type = MAVEN ;
7956
8057 private final Project project ;
81- private final IAzureMessager messager ;
8258
8359 public AzureSdkArtifactGroupPanel (@ Nullable Project project ) {
8460 this .project = project ;
85- this .messager = new DependencyNotificationMessager ();
8661 $$$setupUI$$$ ();
8762 init ();
8863 }
@@ -91,8 +66,6 @@ private void init() {
9166 if (!MavenUtils .isMavenProject (project ) && !GradleUtils .isGradleProject (project )) {
9267 pnlAddDependencies .setVisible (false );
9368 }
94- cbModule .addItemListener (e -> refreshDependencyButton ());
95- btnAddDependency .addActionListener (e -> onAddDependency ());
9669 }
9770
9871 public void setData (@ Nonnull final List <? extends AzureSdkArtifactEntity > artifacts ) {
@@ -118,7 +91,9 @@ private void onPackageOrVersionSelected(AzureSdkArtifactEntity pkg, String versi
11891 this .pkg = pkg ;
11992 this .version = version ;
12093 this .viewer .setText (pkg .getDependencySnippet (type , version ));
121- refreshDependencyButton ();
94+ this .pnlAddDependencies .setPkg (pkg );
95+ this .pnlAddDependencies .setVersion (version );
96+ this .pnlAddDependencies .onSelectModule ();
12297 }
12398
12499 private void onDependencyTypeSelected (DependencyType type ) {
@@ -175,68 +150,7 @@ private void createUIComponents() {
175150 this .toolbar = this .buildCodeViewerToolbar ();
176151 this .toolbar .setForceMinimumSize (true );
177152 this .toolbar .setTargetComponent (this .viewer );
178- this .cbModule = new ModuleComboBox (this .project );
179- cbModule .reloadItems ();
180- }
181-
182- @ AzureOperation (name = "sdk.refresh_dependency" , type = AzureOperation .Type .ACTION )
183- private void refreshDependencyButton () {
184- OperationContext .action ().setMessager (messager );
185- btnAddDependency .setText ("Loading..." );
186- btnAddDependency .setEnabled (false );
187- AzureTaskManager .getInstance ().runInBackground ("Loading dependencies status" , () -> {
188- final ProjectModule module = cbModule .getValue ();
189- if (module instanceof MavenProjectModule ) {
190- final MavenArtifact mavenDependency = ((MavenProjectModule ) module ).getMavenDependency (pkg .getGroupId (), pkg .getArtifactId ());
191- final String currentVersion = Optional .ofNullable (mavenDependency ).map (MavenArtifact ::getVersion ).orElse (null );
192- updateDependencyStatus (module , currentVersion );
193- } else if (module instanceof GradleProjectModule ) {
194- final ExternalDependency gradleDependency = ((GradleProjectModule ) module ).getGradleDependency (pkg .getGroupId (), pkg .getArtifactId ());
195- final String currentVersion = Optional .ofNullable (gradleDependency ).map (ExternalDependency ::getVersion ).orElse (null );
196- updateDependencyStatus (module , currentVersion );
197- } else {
198- btnAddDependency .setEnabled (false );
199- }
200- });
201- }
202-
203- private void updateDependencyStatus (final ProjectModule module , final String currentVersion ) {
204- if (StringUtils .isEmpty (currentVersion )) {
205- AzureMessager .getMessager ().info (AzureString .format ("Library %s was not found in module %s" , pkg .getArtifactId (), module .getName ()));
206- btnAddDependency .setEnabled (true );
207- btnAddDependency .setText (ADD_DEPENDENCY );
208- } else {
209- AzureMessager .getMessager ().info (AzureString .format ("Library %s was found in module %s with version %s" ,
210- pkg .getArtifactId (), module .getName (), currentVersion ));
211- final ComparableVersion current = new ComparableVersion (currentVersion );
212- final ComparableVersion targetVersion = new ComparableVersion (version );
213- btnAddDependency .setText (current .compareTo (targetVersion ) >= 0 ? DEPENDENCY_ADDED : UPDATE_DEPENDENCY );
214- btnAddDependency .setEnabled (current .compareTo (targetVersion ) < 0 );
215- }
216- }
217-
218- @ AzureOperation (name = "sdk.add_dependency" , type = AzureOperation .Type .ACTION )
219- private void onAddDependency () {
220- OperationContext .action ().setMessager (messager );
221- btnAddDependency .setText ("Running..." );
222- btnAddDependency .setEnabled (false );
223- AzureTaskManager .getInstance ().runInBackground (new AzureTask <>("Add dependency" , this ::addDependency ));
224- }
225-
226- private void addDependency () {
227- final ProjectModule module = cbModule .getValue ();
228- try {
229- if (module instanceof MavenProjectModule ) {
230- DependencyUtils .addOrUpdateMavenDependency ((MavenProjectModule ) module , pkg , version );
231- } else if (module instanceof GradleProjectModule ) {
232- DependencyUtils .addOrUpdateGradleDependency ((GradleProjectModule ) module , pkg , version );
233- }
234- btnAddDependency .setText (DEPENDENCY_ADDED );
235- btnAddDependency .setEnabled (false );
236- } catch (final Throwable t ) {
237- AzureMessager .getMessager ().error (t );
238- refreshDependencyButton ();
239- }
153+ this .pnlAddDependencies = new AzureSdkProjectDependencyPanel (project );
240154 }
241155
242156 /**
@@ -279,15 +193,6 @@ public void actionPerformed(@NotNull final AnActionEvent e) {
279193 }
280194 }
281195
282- private class DependencyNotificationMessager implements IAzureMessager {
283- @ Override
284- public boolean show (IAzureMessage message ) {
285- paneMessage .setText (message .getMessage ().toString ());
286- return true ;
287- }
288-
289- }
290-
291196 // CHECKSTYLE IGNORE check FOR NEXT 1 LINES
292197 public void $$$setupUI$$$ () {
293198 }
0 commit comments