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.
17 package com
.intellij
.uiDesigner
.actions
;
19 import com
.intellij
.ide
.IdeView
;
20 import com
.intellij
.ide
.actions
.CreateElementActionBase
;
21 import com
.intellij
.openapi
.actionSystem
.AnActionEvent
;
22 import com
.intellij
.openapi
.actionSystem
.LangDataKeys
;
23 import com
.intellij
.openapi
.actionSystem
.PlatformDataKeys
;
24 import com
.intellij
.openapi
.actionSystem
.Presentation
;
25 import com
.intellij
.openapi
.project
.DumbAware
;
26 import com
.intellij
.openapi
.project
.Project
;
27 import com
.intellij
.openapi
.roots
.ProjectFileIndex
;
28 import com
.intellij
.openapi
.roots
.ProjectRootManager
;
29 import com
.intellij
.openapi
.util
.text
.StringUtil
;
30 import com
.intellij
.psi
.JavaDirectoryService
;
31 import com
.intellij
.psi
.PsiDirectory
;
32 import com
.intellij
.uiDesigner
.UIDesignerBundle
;
33 import com
.intellij
.util
.IncorrectOperationException
;
34 import org
.jetbrains
.annotations
.NonNls
;
35 import org
.jetbrains
.annotations
.Nullable
;
38 import java
.io
.IOException
;
39 import java
.io
.InputStream
;
44 public abstract class AbstractCreateFormAction
extends CreateElementActionBase
implements DumbAware
{
45 public AbstractCreateFormAction(String text
, String description
, Icon icon
) {
46 super(text
, description
, icon
);
49 public void update(final AnActionEvent e
) {
51 final Project project
= e
.getData(PlatformDataKeys
.PROJECT
);
52 final Presentation presentation
= e
.getPresentation();
53 if (presentation
.isEnabled()) {
54 final IdeView view
= e
.getData(LangDataKeys
.IDE_VIEW
);
56 final ProjectFileIndex projectFileIndex
= ProjectRootManager
.getInstance(project
).getFileIndex();
57 final PsiDirectory
[] dirs
= view
.getDirectories();
58 for (final PsiDirectory dir
: dirs
) {
59 if (projectFileIndex
.isInSourceContent(dir
.getVirtualFile()) && JavaDirectoryService
.getInstance().getPackage(dir
) != null) {
65 presentation
.setEnabled(false);
66 presentation
.setVisible(false);
70 protected String
createFormBody(@Nullable final String fullQualifiedClassName
, @NonNls final String formName
,
71 final String layoutManager
) throws IncorrectOperationException
{
73 final InputStream inputStream
= getClass().getResourceAsStream(formName
);
75 final StringBuffer buffer
= new StringBuffer();
77 for (int ch
; (ch
= inputStream
.read()) != -1;) {
78 buffer
.append((char)ch
);
81 catch (IOException e
) {
82 throw new IncorrectOperationException(UIDesignerBundle
.message("error.cannot.read", formName
),e
);
85 String s
= buffer
.toString();
87 if (fullQualifiedClassName
!= null) {
88 s
= StringUtil
.replace(s
, "$CLASS$", fullQualifiedClassName
);
91 s
= StringUtil
.replace(s
, "bind-to-class=\"$CLASS$\"", "");
94 s
= StringUtil
.replace(s
, "$LAYOUT$", layoutManager
);
96 return StringUtil
.convertLineSeparators(s
);
99 protected String
getActionName(final PsiDirectory directory
, final String newName
) {
100 return UIDesignerBundle
.message("progress.creating.class", JavaDirectoryService
.getInstance().getPackage(directory
).getQualifiedName(), newName
);