2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com
.theoryinpractice
.testng
.ui
.actions
;
18 import com
.intellij
.execution
.RunManager
;
19 import com
.intellij
.execution
.RunnerAndConfigurationSettings
;
20 import com
.intellij
.execution
.configurations
.ConfigurationFactory
;
21 import com
.intellij
.openapi
.actionSystem
.AnAction
;
22 import com
.intellij
.openapi
.actionSystem
.AnActionEvent
;
23 import com
.intellij
.openapi
.actionSystem
.DataContext
;
24 import com
.intellij
.openapi
.actionSystem
.LangDataKeys
;
25 import com
.intellij
.openapi
.application
.ApplicationManager
;
26 import com
.intellij
.openapi
.module
.Module
;
27 import com
.intellij
.psi
.JavaDirectoryService
;
28 import com
.intellij
.psi
.PsiDirectory
;
29 import com
.intellij
.psi
.PsiElement
;
30 import com
.intellij
.psi
.PsiPackage
;
31 import com
.theoryinpractice
.testng
.configuration
.TestNGConfiguration
;
32 import com
.theoryinpractice
.testng
.configuration
.TestNGConfigurationType
;
35 * @author Hani Suleiman Date: Dec 1, 2006 Time: 9:40:24 PM
37 public class CreatePackageTestAction
extends AnAction
39 private PsiPackage pkg
;
40 private Module module
;
42 public void update(AnActionEvent event
) {
43 pkg
= getSelectedPackage(event
.getDataContext());
44 event
.getPresentation().setVisible(pkg
!= null);
45 event
.getPresentation().setEnabled(pkg
!= null);
47 event
.getPresentation().setText("Create \"Tests in '" + pkg
.getQualifiedName() + "'\"...");
48 module
= (Module
) event
.getDataContext().getData(LangDataKeys
.MODULE
.getName());
54 public void actionPerformed(AnActionEvent event
) {
55 RunManager runManager
= RunManager
.getInstance(pkg
.getProject());
56 ConfigurationFactory
[] factory
= ApplicationManager
.getApplication().getComponent(TestNGConfigurationType
.class).getConfigurationFactories();
57 RunnerAndConfigurationSettings settings
= runManager
.createRunConfiguration("", factory
[0]);
58 final TestNGConfiguration configuration
= (TestNGConfiguration
) settings
.getConfiguration();
59 configuration
.setPackageConfiguration(module
, pkg
);
60 settings
.setName(configuration
.getName());
63 PsiPackage
getSelectedPackage(DataContext context
) {
64 final PsiElement element
= (PsiElement
) context
.getData(LangDataKeys
.PSI_ELEMENT
.getName());
65 if (element
== null) {
69 if (element
instanceof PsiDirectory
) {
70 return JavaDirectoryService
.getInstance().getPackage(((PsiDirectory
)element
));