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
.intellij
.execution
.junit2
.configuration
;
18 import com
.intellij
.execution
.ExecutionBundle
;
19 import com
.intellij
.execution
.RunJavaConfiguration
;
20 import com
.intellij
.openapi
.actionSystem
.LangDataKeys
;
21 import com
.intellij
.openapi
.diagnostic
.Logger
;
22 import com
.intellij
.openapi
.fileChooser
.FileChooser
;
23 import com
.intellij
.openapi
.fileChooser
.FileChooserDescriptor
;
24 import com
.intellij
.openapi
.fileChooser
.FileChooserDescriptorFactory
;
25 import com
.intellij
.openapi
.module
.Module
;
26 import com
.intellij
.openapi
.ui
.LabeledComponent
;
27 import com
.intellij
.openapi
.ui
.TextFieldWithBrowseButton
;
28 import com
.intellij
.openapi
.vfs
.VirtualFile
;
29 import com
.intellij
.ui
.RawCommandLineEditor
;
33 import java
.awt
.event
.ActionEvent
;
34 import java
.awt
.event
.ActionListener
;
36 public class CommonJavaParameters
extends JPanel
{
37 private static final Logger LOG
= Logger
.getInstance("#com.intellij.execution.junit2.configuration.CommonJavaParameters");
38 private static final int[] ourProperties
= new int[]{
39 RunJavaConfiguration
.PROGRAM_PARAMETERS_PROPERTY
,
40 RunJavaConfiguration
.VM_PARAMETERS_PROPERTY
,
41 RunJavaConfiguration
.WORKING_DIRECTORY_PROPERTY
44 private JPanel myWholePanel
;
45 private LabeledComponent
<TextFieldWithBrowseButton
> myWorkingDirectory
;
46 private LabeledComponent
<RawCommandLineEditor
> myProgramParameters
;
47 private LabeledComponent
<RawCommandLineEditor
> myVMParameters
;
48 private JCheckBox myUseModuleDirectoryAsCheckBox
;
50 private final LabeledComponent
[] myFields
= new LabeledComponent
[3];
51 private Module myModule
= null;
53 public CommonJavaParameters() {
54 super(new BorderLayout());
55 add(myWholePanel
, BorderLayout
.CENTER
);
56 copyDialogCaption(myProgramParameters
);
57 copyDialogCaption(myVMParameters
);
58 myWorkingDirectory
.getComponent()
59 .addActionListener(new ActionListener() {
60 public void actionPerformed(ActionEvent e
) {
61 FileChooserDescriptor fileChooserDescriptor
= FileChooserDescriptorFactory
.createSingleFolderDescriptor();
62 fileChooserDescriptor
.setTitle(ExecutionBundle
.message("select.working.directory.message"));
63 fileChooserDescriptor
.putUserData(LangDataKeys
.MODULE_CONTEXT
, myModule
);
64 VirtualFile
[] files
= FileChooser
.chooseFiles(myWorkingDirectory
, fileChooserDescriptor
);
65 if (files
.length
!= 0) {
66 setText(RunJavaConfiguration
.WORKING_DIRECTORY_PROPERTY
, files
[0].getPresentableUrl());
70 myFields
[RunJavaConfiguration
.PROGRAM_PARAMETERS_PROPERTY
] = myProgramParameters
;
71 myFields
[RunJavaConfiguration
.VM_PARAMETERS_PROPERTY
] = myVMParameters
;
72 myFields
[RunJavaConfiguration
.WORKING_DIRECTORY_PROPERTY
] = myWorkingDirectory
;
74 myUseModuleDirectoryAsCheckBox
.addActionListener(new ActionListener() {
75 public void actionPerformed(ActionEvent e
) {
76 final boolean selected
= ((JCheckBox
)e
.getSource()).isSelected();
77 myWorkingDirectory
.getComponent().setEnabled(!selected
);
82 private static void copyDialogCaption(final LabeledComponent
<RawCommandLineEditor
> component
) {
83 final RawCommandLineEditor rawCommandLineEditor
= component
.getComponent();
84 rawCommandLineEditor
.setDialogCaption(component
.getRawText());
85 component
.getLabel().setLabelFor(rawCommandLineEditor
.getTextField());
88 public String
getProgramParametersText() {
89 return getLabeledComponent(RunJavaConfiguration
.PROGRAM_PARAMETERS_PROPERTY
).getText();
92 public void setProgramParametersText(String textWithMnemonic
) {
93 getLabeledComponent(RunJavaConfiguration
.PROGRAM_PARAMETERS_PROPERTY
).setText(textWithMnemonic
);
94 copyDialogCaption(myProgramParameters
);
97 public void applyTo(final RunJavaConfiguration configuration
) {
98 for (final int property
: ourProperties
) {
99 configuration
.setProperty(property
, getText(property
));
103 public void reset(final RunJavaConfiguration configuration
) {
104 for (final int property
: ourProperties
) {
105 setText(property
, configuration
.getProperty(property
));
109 public void setText(final int property
, final String value
) {
110 if (RunJavaConfiguration
.WORKING_DIRECTORY_PROPERTY
== property
&& value
!= null && "$MODULE_DIR$".equals(value
.trim())) {
111 myUseModuleDirectoryAsCheckBox
.setSelected(true);
112 myWorkingDirectory
.getComponent().setEnabled(false);
115 final JComponent component
= getLabeledComponent(property
).getComponent();
116 if (component
instanceof TextFieldWithBrowseButton
)
117 ((TextFieldWithBrowseButton
)component
).setText(value
);
118 else if (component
instanceof RawCommandLineEditor
)
119 ((RawCommandLineEditor
)component
).setText(value
);
120 else LOG
.error(component
.getClass().getName());
123 public String
getText(final int property
) {
124 if (RunJavaConfiguration
.WORKING_DIRECTORY_PROPERTY
== property
&& myUseModuleDirectoryAsCheckBox
.isSelected()) {
125 return "$MODULE_DIR$";
128 final JComponent component
= getLabeledComponent(property
).getComponent();
129 if (component
instanceof TextFieldWithBrowseButton
)
130 return ((TextFieldWithBrowseButton
)component
).getText();
131 else if (component
instanceof RawCommandLineEditor
)
132 return ((RawCommandLineEditor
)component
).getText();
133 else LOG
.error(component
.getClass().getName());
137 private LabeledComponent
getLabeledComponent(final int index
) {
138 return myFields
[index
];
141 public void setModuleContext(final Module module
) {