2 * Copyright 2000-2007 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.
17 package com
.maddyhome
.idea
.copyright
.ui
;
19 import com
.intellij
.openapi
.editor
.colors
.EditorColorsManager
;
20 import com
.intellij
.openapi
.editor
.colors
.EditorFontType
;
21 import com
.intellij
.openapi
.fileTypes
.FileType
;
22 import com
.intellij
.openapi
.fileTypes
.StdFileTypes
;
23 import com
.intellij
.openapi
.options
.Configurable
;
24 import com
.intellij
.openapi
.options
.ConfigurationException
;
25 import com
.intellij
.openapi
.project
.Project
;
26 import com
.intellij
.psi
.codeStyle
.CodeStyleSettingsManager
;
27 import com
.intellij
.uiDesigner
.core
.GridConstraints
;
28 import com
.maddyhome
.idea
.copyright
.CopyrightManager
;
29 import com
.maddyhome
.idea
.copyright
.CopyrightProfile
;
30 import com
.maddyhome
.idea
.copyright
.options
.LanguageOptions
;
31 import com
.maddyhome
.idea
.copyright
.options
.Options
;
32 import com
.maddyhome
.idea
.copyright
.options
.TemplateOptions
;
33 import com
.maddyhome
.idea
.copyright
.util
.FileTypeUtil
;
34 import org
.jetbrains
.annotations
.Nls
;
38 import java
.awt
.event
.ActionEvent
;
39 import java
.awt
.event
.ActionListener
;
41 public class TemplateCommentPanel
implements Configurable
{
42 private CopyrightManager myManager
;
44 private FileType fileType
;
45 private TemplateCommentPanel parentPanel
;
46 private JRadioButton
[] fileLocations
= null;
48 private FileTypeOverridePanel ftOptionsPanel
= null;
49 private TemplateOptionsPanel tempOptionsPanel
;
50 private JTextArea preview
;
51 private JPanel mainPanel
;
52 private JPanel overridePanel
;
53 private JRadioButton rbBefore
;
54 private JRadioButton rbAfter
;
55 private JPanel fileLocationPanel
;
56 private JCheckBox cbAddBlank
;
57 private JCheckBox cbUseAlternate
;
59 public TemplateCommentPanel(FileType fileType
, TemplateCommentPanel parentPanel
, String
[] locations
, Project project
) {
60 this.parentPanel
= parentPanel
;
62 myManager
= CopyrightManager
.getInstance(project
);
63 overridePanel
.setLayout(new BorderLayout());
64 if (fileType
!= null) {
65 ftOptionsPanel
= new FileTypeOverridePanel();
66 overridePanel
.add(ftOptionsPanel
.getMainComponent(), BorderLayout
.CENTER
);
69 overridePanel
.setVisible(false);
72 this.fileType
= fileType
!= null ? fileType
: StdFileTypes
.JAVA
;
73 tempOptionsPanel
.setFileType(this.fileType
);
74 FileType alternate
= FileTypeUtil
.getInstance().getAlternate(this.fileType
);
75 if (alternate
!= null) {
76 cbUseAlternate
.setText("Use " + alternate
.getName() + " Comments");
77 cbUseAlternate
.addActionListener(new ActionListener() {
78 public void actionPerformed(ActionEvent e
) {
84 cbUseAlternate
.setVisible(false);
87 if (parentPanel
!= null) {
88 parentPanel
.tempOptionsPanel
.addOptionChangeListener(new TemplateOptionsPanelListener() {
89 public void optionChanged() {
95 ButtonGroup group
= new ButtonGroup();
99 if (locations
== null) {
100 fileLocationPanel
.setBorder(BorderFactory
.createEmptyBorder());
103 fileLocations
= new JRadioButton
[locations
.length
];
104 group
= new ButtonGroup();
105 for (int i
= 0; i
< fileLocations
.length
; i
++) {
106 fileLocations
[i
] = new JRadioButton(locations
[i
]);
107 group
.add(fileLocations
[i
]);
109 fileLocationPanel
.add(fileLocations
[i
], new GridConstraints(i
, 0, 1, 1, GridConstraints
.ANCHOR_WEST
, GridConstraints
.FILL_NONE
,
110 GridConstraints
.SIZEPOLICY_CAN_SHRINK
|
111 GridConstraints
.SIZEPOLICY_CAN_GROW
, GridConstraints
.SIZEPOLICY_FIXED
,
117 if (ftOptionsPanel
!= null) {
118 ftOptionsPanel
.addOptionChangeListener(new FileTypeOverridePanelListener() {
119 public void optionChanged() {
125 tempOptionsPanel
.addOptionChangeListener(new TemplateOptionsPanelListener() {
126 public void optionChanged() {
127 showPreview(tempOptionsPanel
.getOptions());
132 preview
.setFont(EditorColorsManager
.getInstance().getGlobalScheme().getFont(EditorFontType
.PLAIN
));
134 preview
.setColumns(CodeStyleSettingsManager
.getInstance().getCurrentSettings().RIGHT_MARGIN
);
138 public LanguageOptions
getOptions() {
139 // If this is a fully custom comment we should really ensure there are no blank lines in the comments outside
140 // of a block comment. If there are any blank lines the replacement logic will fall apart.
141 LanguageOptions res
= new LanguageOptions();
142 res
.setTemplateOptions(tempOptionsPanel
.getOptions());
144 if (ftOptionsPanel
!= null) {
145 res
.setFileTypeOverride(ftOptionsPanel
.getOptions());
148 res
.setFileTypeOverride(LanguageOptions
.USE_TEMPLATE
);
150 res
.setRelativeBefore(rbBefore
.isSelected());
151 res
.setAddBlankAfter(cbAddBlank
.isSelected());
152 res
.setUseAlternate(cbUseAlternate
.isSelected());
153 if (fileLocations
!= null) {
154 for (int i
= 0; i
< fileLocations
.length
; i
++) {
155 if (fileLocations
[i
].isSelected()) {
156 res
.setFileLocation(i
+ 1);
164 private int getOverrideChoice() {
165 int choice
= LanguageOptions
.USE_TEMPLATE
;
166 if (ftOptionsPanel
!= null) {
167 choice
= ftOptionsPanel
.getOptions();
173 private void updateOverride() {
174 int choice
= getOverrideChoice();
175 LanguageOptions parentOpts
= parentPanel
!= null ? parentPanel
.getOptions() : null;
177 case LanguageOptions
.USE_NONE
:
178 tempOptionsPanel
.setEnabled(false);
180 rbBefore
.setEnabled(false);
181 rbAfter
.setEnabled(false);
182 cbAddBlank
.setEnabled(false);
183 if (fileLocations
!= null) {
184 for (JRadioButton fileLocation
: fileLocations
) {
185 fileLocation
.setEnabled(false);
189 case LanguageOptions
.USE_TEMPLATE
:
190 final boolean isTemplate
= parentPanel
== null;
191 tempOptionsPanel
.setEnabled(isTemplate
);
192 showPreview(parentOpts
!= null ? parentOpts
.getTemplateOptions() : getOptions().getTemplateOptions());
194 rbBefore
.setEnabled(isTemplate
);
195 rbAfter
.setEnabled(isTemplate
);
196 cbAddBlank
.setEnabled(isTemplate
);
197 if (fileLocations
!= null) {
198 for (JRadioButton fileLocation
: fileLocations
) {
199 fileLocation
.setEnabled(true);
203 case LanguageOptions
.USE_TEXT
:
204 tempOptionsPanel
.setEnabled(true);
205 showPreview(tempOptionsPanel
.getOptions());
206 rbBefore
.setEnabled(true);
207 rbAfter
.setEnabled(true);
208 cbAddBlank
.setEnabled(true);
209 if (fileLocations
!= null) {
210 for (JRadioButton fileLocation
: fileLocations
) {
211 fileLocation
.setEnabled(true);
218 private void showPreview(TemplateOptions options
) {
219 final String defaultCopyrightText
=
220 FileTypeUtil
.buildComment(fileType
, cbUseAlternate
.isSelected(), CopyrightProfile
.DEFAULT_COPYRIGHT_NOTICE
, options
);
221 preview
.setText(defaultCopyrightText
);
225 public FileType
getFileType() {
230 public String
getDisplayName() { //todo mapped names
231 return fileType
.getName();
234 public Icon
getIcon() {
235 return fileType
.getIcon();
238 public String
getHelpTopic() {
239 return "copyright.filetypes";
242 public JComponent
createComponent() {
246 public boolean isModified() {
247 return !myManager
.getOptions().getOptions(fileType
.getName()).equals(getOptions());
250 public void apply() throws ConfigurationException
{
251 final Options options
= myManager
.getOptions();
252 if (parentPanel
== null) {
253 options
.setTemplateOptions(getOptions());
256 options
.setOptions(fileType
.getName(), getOptions());
260 public void reset() {
261 LanguageOptions options
= myManager
.getOptions().getOptions(fileType
.getName());
264 .setOptions(parentPanel
== null ? myManager
.getOptions().getTemplateOptions().getTemplateOptions() : options
.getTemplateOptions());
265 if (ftOptionsPanel
!= null) {
266 ftOptionsPanel
.setOptions(options
.getFileTypeOverride());
268 if (options
.isRelativeBefore()) {
269 rbBefore
.setSelected(true);
272 rbAfter
.setSelected(true);
274 cbAddBlank
.setSelected(options
.isAddBlankAfter());
275 cbUseAlternate
.setSelected(options
.isUseAlternate());
277 if (fileLocations
!= null) {
278 int choice
= options
.getFileLocation() - 1;
279 choice
= Math
.max(0, Math
.min(choice
, fileLocations
.length
- 1));
280 fileLocations
[choice
].setSelected(true);
286 public void disposeUIResources() {