mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
FormBuilder. Helper for creating standard forms.
This commit is contained in:
@@ -519,7 +519,7 @@ abstract class ProjectLayoutPanel<T> extends JPanel {
|
||||
|
||||
@Nullable
|
||||
protected JComponent createCenterPanel() {
|
||||
FormBuilder builder = new FormBuilder(true);
|
||||
FormBuilder builder = FormBuilder.createFormBuilder().setVertical(true);
|
||||
builder.addLabeledComponent("&Name:", myNameField);
|
||||
builder.addLabeledComponent(getSplitDialogChooseFilesPrompt(), myChooser);
|
||||
myChooser.setPreferredSize(new Dimension(450, 300));
|
||||
|
||||
+3
-6
@@ -98,16 +98,13 @@ public abstract class LibraryEditorDialogBase extends DialogWrapper {
|
||||
}
|
||||
|
||||
protected JComponent createNorthPanel() {
|
||||
FormBuilder formBuilder = new FormBuilder();
|
||||
String currentName = myLibraryRootsComponent.getLibraryEditor().getName();
|
||||
myNameField = new JTextField(currentName);
|
||||
formBuilder.addLabeledComponent("&Name:", myNameField);
|
||||
addNorthComponents(formBuilder);
|
||||
myNameField.selectAll();
|
||||
|
||||
final JPanel panel = formBuilder.getPanel();
|
||||
panel.setBorder(BorderFactory.createEmptyBorder(0, 0, 10, 0));
|
||||
return panel;
|
||||
FormBuilder formBuilder = FormBuilder.createFormBuilder().addLabeledComponent("&Name:", myNameField);
|
||||
addNorthComponents(formBuilder);
|
||||
return formBuilder.addVerticalGap(10).getPanel();
|
||||
}
|
||||
|
||||
protected void addNorthComponents(FormBuilder formBuilder) {
|
||||
|
||||
+1
-1
@@ -87,6 +87,6 @@ public class LibraryNameAndLevelPanel {
|
||||
}
|
||||
|
||||
public static FormBuilder createFormBuilder() {
|
||||
return new FormBuilder(false, 5);
|
||||
return FormBuilder.createFormBuilder();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,10 +27,10 @@ source.column.header=Source
|
||||
action.column.header=Action
|
||||
class.search.results.title=Class search results
|
||||
extract.class.title=Extract Class
|
||||
extract.class.from.label=Extract class from:
|
||||
name.for.new.class.label=Name for new class:
|
||||
extract.class.from.label=Extract class from {0}
|
||||
name.for.new.class.label=&Name for new class:
|
||||
choose.destination.package.label=Choose Destination Package
|
||||
package.for.new.class.label=Package name:
|
||||
package.for.new.class.label=&Package name:
|
||||
name.for.wrapper.class.label=Class name:
|
||||
package.for.wrapper.class.label=Package name:
|
||||
members.to.extract.label=Mem&bers to Extract
|
||||
|
||||
@@ -32,9 +32,10 @@ import com.intellij.refactoring.util.RefactoringMessageUtil;
|
||||
import com.intellij.ui.EditorTextField;
|
||||
import com.intellij.ui.RecentsManager;
|
||||
import com.intellij.ui.ReferenceEditorComboWithBrowseButton;
|
||||
import com.intellij.ui.components.JBLabel;
|
||||
import com.intellij.usageView.UsageViewUtil;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import com.intellij.util.ui.FormBuilder;
|
||||
import com.intellij.util.ui.UIUtil;
|
||||
import org.jetbrains.annotations.NonNls;
|
||||
|
||||
import javax.swing.*;
|
||||
@@ -71,6 +72,7 @@ class CopyClassDialog extends DialogWrapper{
|
||||
String text = myDoClone ? RefactoringBundle.message("copy.class.clone.0.1", UsageViewUtil.getType(aClass), UsageViewUtil.getLongName(aClass)) :
|
||||
RefactoringBundle.message("copy.class.copy.0.1", UsageViewUtil.getType(aClass), UsageViewUtil.getLongName(aClass));
|
||||
myInformationLabel.setText(text);
|
||||
myInformationLabel.setFont(myInformationLabel.getFont().deriveFont(Font.BOLD));
|
||||
init();
|
||||
myDestinationCB.setData(myProject, defaultTargetDirectory,
|
||||
new Pass<String>() {
|
||||
@@ -95,70 +97,32 @@ class CopyClassDialog extends DialogWrapper{
|
||||
}
|
||||
|
||||
protected JComponent createNorthPanel() {
|
||||
JPanel panel = new JPanel(new GridBagLayout());
|
||||
GridBagConstraints gbConstraints = new GridBagConstraints();
|
||||
|
||||
gbConstraints.insets = new Insets(4,8,4,8);
|
||||
gbConstraints.weightx = 1;
|
||||
gbConstraints.gridwidth = 2;
|
||||
gbConstraints.fill = GridBagConstraints.HORIZONTAL;
|
||||
gbConstraints.anchor = GridBagConstraints.WEST;
|
||||
panel.add(myInformationLabel, gbConstraints);
|
||||
|
||||
gbConstraints.gridwidth = 1;
|
||||
gbConstraints.gridy = 1;
|
||||
gbConstraints.weighty = 1;
|
||||
gbConstraints.weightx = 0;
|
||||
myNameLabel.setText(RefactoringBundle.message("copy.files.new.name.label"));
|
||||
panel.add(myNameLabel, gbConstraints);
|
||||
|
||||
gbConstraints.gridx = 1;
|
||||
gbConstraints.weightx = 1;
|
||||
myNameField = new EditorTextField("");
|
||||
myNameLabel.setLabelFor(myNameField);
|
||||
panel.add(myNameField, gbConstraints);
|
||||
|
||||
gbConstraints.gridx = 0;
|
||||
gbConstraints.gridy = 2;
|
||||
gbConstraints.weightx = 0;
|
||||
panel.add(myPackageLabel, gbConstraints);
|
||||
|
||||
gbConstraints.gridx = 1;
|
||||
gbConstraints.weightx = 1;
|
||||
String qualifiedName = getQualifiedName();
|
||||
|
||||
myTfPackage = new PackageNameReferenceEditorCombo(qualifiedName, myProject, RECENTS_KEY, RefactoringBundle.message("choose.destination.package"));
|
||||
myTfPackage.setTextFieldPreferredWidth(Math.max(qualifiedName.length() + 5, 40));
|
||||
|
||||
myPackageLabel.setText(RefactoringBundle.message("destination.package"));
|
||||
myPackageLabel.setLabelFor(myTfPackage);
|
||||
|
||||
panel.add(myTfPackage, gbConstraints);
|
||||
|
||||
final JBLabel label = new JBLabel(RefactoringBundle.message("target.destination.folder"));
|
||||
if (myDoClone) {
|
||||
myTfPackage.setVisible(false);
|
||||
myPackageLabel.setVisible(false);
|
||||
}
|
||||
|
||||
final JLabel label = new JLabel(RefactoringBundle.message("target.destination.folder"));
|
||||
final boolean isMultipleSourceRoots = ProjectRootManager.getInstance(myProject).getContentSourceRoots().length > 1;
|
||||
myDestinationCB.setVisible(!myDoClone && isMultipleSourceRoots);
|
||||
label.setVisible(!myDoClone && isMultipleSourceRoots);
|
||||
|
||||
gbConstraints.gridy = 3;
|
||||
gbConstraints.gridx = 0;
|
||||
gbConstraints.gridwidth = 2;
|
||||
gbConstraints.insets.top = 12;
|
||||
gbConstraints.anchor = GridBagConstraints.WEST;
|
||||
gbConstraints.fill = GridBagConstraints.NONE;
|
||||
panel.add(label, gbConstraints);
|
||||
|
||||
gbConstraints.gridy = 4;
|
||||
gbConstraints.gridx = 0;
|
||||
gbConstraints.fill = GridBagConstraints.HORIZONTAL;
|
||||
gbConstraints.insets.top = 4;
|
||||
panel.add(myDestinationCB, gbConstraints);
|
||||
|
||||
return panel;
|
||||
return FormBuilder.createFormBuilder()
|
||||
.addComponent(myInformationLabel)
|
||||
.addLabeledComponent(myNameLabel, myNameField, UIUtil.LARGE_VGAP)
|
||||
.addLabeledComponent(myPackageLabel, myTfPackage)
|
||||
.addLabeledComponent(label, myDestinationCB)
|
||||
.getPanel();
|
||||
}
|
||||
|
||||
protected String getQualifiedName() {
|
||||
|
||||
@@ -20,7 +20,6 @@ import com.intellij.openapi.options.ConfigurationException;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.roots.ProjectRootManager;
|
||||
import com.intellij.openapi.ui.Messages;
|
||||
import com.intellij.openapi.util.Pass;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.presentation.java.SymbolPresentationUtil;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
@@ -37,8 +36,10 @@ import com.intellij.refactoring.ui.*;
|
||||
import com.intellij.refactoring.util.classMembers.MemberInfo;
|
||||
import com.intellij.ui.DocumentAdapter;
|
||||
import com.intellij.ui.ReferenceEditorComboWithBrowseButton;
|
||||
import com.intellij.ui.components.JBLabel;
|
||||
import com.intellij.ui.components.JBLabelDecorator;
|
||||
import com.intellij.util.containers.HashMap;
|
||||
import com.intellij.util.ui.FormBuilder;
|
||||
import com.intellij.util.ui.UIUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
@@ -59,7 +60,7 @@ class ExtractClassDialog extends RefactoringDialog implements MemberInfoChangeLi
|
||||
private final JTextField classNameField;
|
||||
private final ReferenceEditorComboWithBrowseButton packageTextField;
|
||||
private final DestinationFolderComboBox myDestinationFolderComboBox;
|
||||
private final JTextField sourceClassTextField;
|
||||
private final JTextField sourceClassTextField = null;
|
||||
private JCheckBox myGenerateAccessorsCb;
|
||||
private final JavaVisibilityPanel myVisibilityPanel;
|
||||
private final JCheckBox extractAsEnum;
|
||||
@@ -80,7 +81,8 @@ class ExtractClassDialog extends RefactoringDialog implements MemberInfoChangeLi
|
||||
classNameField = new JTextField();
|
||||
final PsiFile file = sourceClass.getContainingFile();
|
||||
final String text = file instanceof PsiJavaFile ? ((PsiJavaFile)file).getPackageName() : "";
|
||||
packageTextField = new PackageNameReferenceEditorCombo(text, myProject, "ExtractClass.RECENTS_KEY", RefactorJBundle.message("choose.destination.package.label"));
|
||||
packageTextField = new PackageNameReferenceEditorCombo(text, myProject, "ExtractClass.RECENTS_KEY",
|
||||
RefactorJBundle.message("choose.destination.package.label"));
|
||||
packageTextField.getChildComponent().getDocument().addDocumentListener(new com.intellij.openapi.editor.event.DocumentAdapter() {
|
||||
@Override
|
||||
public void documentChanged(com.intellij.openapi.editor.event.DocumentEvent e) {
|
||||
@@ -93,9 +95,9 @@ class ExtractClassDialog extends RefactoringDialog implements MemberInfoChangeLi
|
||||
return getPackageName();
|
||||
}
|
||||
};
|
||||
myDestinationFolderComboBox.setData(myProject, sourceClass.getContainingFile().getContainingDirectory(), packageTextField.getChildComponent());
|
||||
myDestinationFolderComboBox.setData(myProject, sourceClass.getContainingFile().getContainingDirectory(),
|
||||
packageTextField.getChildComponent());
|
||||
classNameField.getDocument().addDocumentListener(docListener);
|
||||
sourceClassTextField = new JTextField();
|
||||
final MemberInfo.Filter<PsiMember> filter = new MemberInfo.Filter<PsiMember>() {
|
||||
public boolean includeMember(PsiMember element) {
|
||||
if (element instanceof PsiMethod) {
|
||||
@@ -118,7 +120,10 @@ class ExtractClassDialog extends RefactoringDialog implements MemberInfoChangeLi
|
||||
if (member.equals(selectedMember)) {
|
||||
info.setChecked(true);
|
||||
}
|
||||
if (!hasConstants && member instanceof PsiField && member.hasModifierProperty(PsiModifier.FINAL) && member.hasModifierProperty(PsiModifier.STATIC)) {
|
||||
if (!hasConstants &&
|
||||
member instanceof PsiField &&
|
||||
member.hasModifierProperty(PsiModifier.FINAL) &&
|
||||
member.hasModifierProperty(PsiModifier.STATIC)) {
|
||||
hasConstants = true;
|
||||
}
|
||||
}
|
||||
@@ -126,7 +131,6 @@ class ExtractClassDialog extends RefactoringDialog implements MemberInfoChangeLi
|
||||
extractAsEnum.setVisible(false);
|
||||
}
|
||||
super.init();
|
||||
sourceClassTextField.setText(sourceClass.getQualifiedName());
|
||||
validateButtons();
|
||||
}
|
||||
|
||||
@@ -143,9 +147,15 @@ class ExtractClassDialog extends RefactoringDialog implements MemberInfoChangeLi
|
||||
return o1.getMember().getTextOffset() - o2.getMember().getTextOffset();
|
||||
}
|
||||
});
|
||||
final ExtractClassProcessor processor = new ExtractClassProcessor(sourceClass, fields, methods, classes, packageName, myDestinationFolderComboBox.selectDirectory(new PackageWrapper(PsiManager.getInstance(myProject), packageName), false),
|
||||
newClassName, myVisibilityPanel.getVisibility(), isGenerateAccessors(),
|
||||
isExtractAsEnum() ? enumConstants : Collections.<MemberInfo>emptyList());
|
||||
final ExtractClassProcessor processor = new ExtractClassProcessor(sourceClass, fields, methods, classes, packageName,
|
||||
myDestinationFolderComboBox.selectDirectory(
|
||||
new PackageWrapper(PsiManager.getInstance(myProject), packageName),
|
||||
false),
|
||||
newClassName, myVisibilityPanel.getVisibility(),
|
||||
isGenerateAccessors(),
|
||||
isExtractAsEnum()
|
||||
? enumConstants
|
||||
: Collections.<MemberInfo>emptyList());
|
||||
if (processor.getCreatedClass() == null) {
|
||||
Messages.showErrorDialog(myVisibilityPanel, "Unable to create class with the given name");
|
||||
classNameField.requestFocusInWindow();
|
||||
@@ -171,12 +181,13 @@ class ExtractClassDialog extends RefactoringDialog implements MemberInfoChangeLi
|
||||
}
|
||||
|
||||
final String packageName = getPackageName();
|
||||
if (packageName.length() == 0 || !nameHelper.isQualifiedName(packageName)){
|
||||
if (packageName.length() == 0 || !nameHelper.isQualifiedName(packageName)) {
|
||||
throw new ConfigurationException("\'" + packageName + "\' is invalid extracted class package name");
|
||||
}
|
||||
for (PsiClass innerClass : innerClasses) {
|
||||
if (className.equals(innerClass.getName())) {
|
||||
throw new ConfigurationException("Extracted class should have unique name. Name " + "\'" + className + "\' is already in use by one of the inner classes");
|
||||
throw new ConfigurationException(
|
||||
"Extracted class should have unique name. Name " + "\'" + className + "\' is already in use by one of the inner classes");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -233,52 +244,25 @@ class ExtractClassDialog extends RefactoringDialog implements MemberInfoChangeLi
|
||||
}
|
||||
|
||||
protected JComponent createNorthPanel() {
|
||||
final Box box = Box.createVerticalBox();
|
||||
|
||||
sourceClassTextField.setEditable(false);
|
||||
final JPanel sourceClassPanel = new JPanel(new BorderLayout());
|
||||
sourceClassPanel.add(new JLabel(RefactorJBundle.message("extract.class.from.label")), BorderLayout.NORTH);
|
||||
sourceClassPanel.add(sourceClassTextField, BorderLayout.CENTER);
|
||||
box.add(sourceClassPanel);
|
||||
|
||||
box.add(Box.createVerticalStrut(10));
|
||||
final JLabel classNameLabel = new JLabel(RefactorJBundle.message("name.for.new.class.label"));
|
||||
classNameLabel.setLabelFor(classNameField);
|
||||
classNameLabel.setDisplayedMnemonic('N');
|
||||
final JPanel classNamePanel = new JPanel(new BorderLayout());
|
||||
classNamePanel.add(classNameLabel, BorderLayout.NORTH);
|
||||
classNamePanel.add(classNameField, BorderLayout.CENTER);
|
||||
classNamePanel.add(extractAsEnum, BorderLayout.SOUTH);
|
||||
box.add(classNamePanel);
|
||||
|
||||
box.add(Box.createVerticalStrut(5));
|
||||
|
||||
final JPanel packageNamePanel = new JPanel(new BorderLayout());
|
||||
final JLabel packageLabel = new JLabel(RefactorJBundle.message("package.for.new.class.label"));
|
||||
packageLabel.setLabelFor(packageTextField);
|
||||
packageLabel.setDisplayedMnemonic('P');
|
||||
packageNamePanel.add(packageLabel, BorderLayout.NORTH);
|
||||
packageNamePanel.add(packageTextField, BorderLayout.CENTER);
|
||||
box.add(packageNamePanel);
|
||||
FormBuilder builder = FormBuilder.createFormBuilder()
|
||||
.addComponent(
|
||||
JBLabelDecorator.createJBLabelDecorator(RefactorJBundle.message("extract.class.from.label", sourceClass.getQualifiedName()))
|
||||
.setBold(true))
|
||||
.addLabeledComponent(RefactorJBundle.message("name.for.new.class.label"), classNameField, UIUtil.LARGE_VGAP)
|
||||
.addLabeledComponent(new JLabel(), extractAsEnum)
|
||||
.addLabeledComponent(RefactorJBundle.message("package.for.new.class.label"), packageTextField);
|
||||
|
||||
if (ProjectRootManager.getInstance(myProject).getContentSourceRoots().length > 1) {
|
||||
final JPanel panel = new JPanel(new BorderLayout());
|
||||
panel.setBorder(BorderFactory.createEmptyBorder(5, 0, 0, 0));
|
||||
panel.add(new JBLabel(RefactoringBundle.message("target.destination.folder")), BorderLayout.NORTH);
|
||||
panel.add(myDestinationFolderComboBox, BorderLayout.CENTER);
|
||||
box.add(panel);
|
||||
builder.addLabeledComponent(RefactoringBundle.message("target.destination.folder"), myDestinationFolderComboBox);
|
||||
}
|
||||
|
||||
box.add(Box.createVerticalStrut(10));
|
||||
final JPanel panel = new JPanel(new BorderLayout());
|
||||
panel.add(box, BorderLayout.CENTER);
|
||||
return panel;
|
||||
return builder.addVerticalGap(5).getPanel();
|
||||
}
|
||||
|
||||
protected JComponent createCenterPanel() {
|
||||
final JPanel panel = new JPanel(new BorderLayout());
|
||||
final MemberSelectionPanel memberSelectionPanel =
|
||||
new MemberSelectionPanel(RefactorJBundle.message("members.to.extract.label"), memberInfo, "As enum"){
|
||||
new MemberSelectionPanel(RefactorJBundle.message("members.to.extract.label"), memberInfo, "As enum") {
|
||||
@Override
|
||||
protected MemberSelectionTable createMemberSelectionTable(final List<MemberInfo> memberInfo, String abstractColumnHeader) {
|
||||
return new MemberSelectionTable(memberInfo, abstractColumnHeader) {
|
||||
@@ -328,7 +312,8 @@ class ExtractClassDialog extends RefactoringDialog implements MemberInfoChangeLi
|
||||
final String presentation = SymbolPresentationUtil.getSymbolPresentableText(cause);
|
||||
if (member.isChecked()) {
|
||||
return "Depends on " + presentation + " from " + sourceClass.getName();
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
final String className = getClassName();
|
||||
return "Depends on " + presentation + " from new class" + (className.length() > 0 ? ": " + className : "");
|
||||
}
|
||||
@@ -375,7 +360,7 @@ class ExtractClassDialog extends RefactoringDialog implements MemberInfoChangeLi
|
||||
private static boolean isConstantField(PsiMember member) {
|
||||
return member instanceof PsiField &&
|
||||
member.hasModifierProperty(PsiModifier.STATIC) &&
|
||||
// member.hasModifierProperty(PsiModifier.FINAL) &&
|
||||
// member.hasModifierProperty(PsiModifier.FINAL) &&
|
||||
((PsiField)member).hasInitializer();
|
||||
}
|
||||
|
||||
@@ -395,7 +380,8 @@ class ExtractClassDialog extends RefactoringDialog implements MemberInfoChangeLi
|
||||
for (Object info : memberInfoChange.getChangedMembers()) {
|
||||
if (((MemberInfo)info).isToAbstract()) {
|
||||
enumConstants.add((MemberInfo)info);
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
enumConstants.remove((MemberInfo)info);
|
||||
}
|
||||
}
|
||||
@@ -412,9 +398,11 @@ class ExtractClassDialog extends RefactoringDialog implements MemberInfoChangeLi
|
||||
final PsiMember member = info.getMember();
|
||||
if (member instanceof PsiField) {
|
||||
fields.add((PsiField)member);
|
||||
} else if (member instanceof PsiMethod) {
|
||||
}
|
||||
else if (member instanceof PsiMethod) {
|
||||
methods.add((PsiMethod)member);
|
||||
} else if (member instanceof PsiClass) {
|
||||
}
|
||||
else if (member instanceof PsiClass) {
|
||||
innerClasses.add((PsiClass)member);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,149 +19,159 @@
|
||||
*/
|
||||
package com.intellij.util.ui;
|
||||
|
||||
import com.intellij.ui.SeparatorComponent;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
|
||||
import static com.intellij.util.ui.UIUtil.DEFAULT_HGAP;
|
||||
import static com.intellij.util.ui.UIUtil.DEFAULT_VGAP;
|
||||
import static java.awt.GridBagConstraints.*;
|
||||
|
||||
public class FormBuilder {
|
||||
private static final boolean ALIGN_LABELS_TO_RIGHT = UIUtil.isUnderAquaLookAndFeel();
|
||||
private boolean myAlignLabelOnRight;
|
||||
|
||||
private int line = 0;
|
||||
private int indent;
|
||||
private final JPanel panel;
|
||||
private boolean vertical;
|
||||
private int myLineCount = 0;
|
||||
private int myIndent;
|
||||
private final JPanel myPanel;
|
||||
private boolean myVertical;
|
||||
|
||||
/**
|
||||
* @param vertical labels will be placed on their own rows
|
||||
*/
|
||||
public FormBuilder(final boolean vertical, final int indent) {
|
||||
this.vertical = vertical;
|
||||
panel = new JPanel(new GridBagLayout());
|
||||
this.indent = indent;
|
||||
}
|
||||
|
||||
public FormBuilder(final boolean vertical) {
|
||||
this(vertical, 5);
|
||||
}
|
||||
private int myVerticalGap;
|
||||
private int myHorizontalGap;
|
||||
|
||||
public FormBuilder() {
|
||||
this(false, 5);
|
||||
myPanel = new JPanel(new GridBagLayout());
|
||||
myVertical = false;
|
||||
myIndent = 0;
|
||||
myAlignLabelOnRight = false;
|
||||
myVerticalGap = DEFAULT_VGAP;
|
||||
myHorizontalGap = DEFAULT_HGAP;
|
||||
}
|
||||
|
||||
public FormBuilder addLabeledComponent(String labelText, JComponent component, final int verticalSpace) {
|
||||
return addLabeledComponent(labelText, component, verticalSpace, false);
|
||||
public static FormBuilder createFormBuilder() {
|
||||
return new FormBuilder();
|
||||
}
|
||||
|
||||
public FormBuilder addLabeledComponent(String labelText, JComponent component, final int verticalSpace, boolean labelOnTop) {
|
||||
public FormBuilder addLabeledComponent(@Nullable JLabel label, JComponent component) {
|
||||
return addLabeledComponent(label, component, myVerticalGap, false);
|
||||
}
|
||||
|
||||
public FormBuilder addLabeledComponent(@Nullable JLabel label, JComponent component, final int topInset) {
|
||||
return addLabeledComponent(label, component, topInset, false);
|
||||
}
|
||||
|
||||
public FormBuilder addLabeledComponent(@Nullable JLabel label, JComponent component, boolean labelOnTop) {
|
||||
return addLabeledComponent(label, component, myVerticalGap, labelOnTop);
|
||||
}
|
||||
|
||||
public FormBuilder addLabeledComponent(String labelText, JComponent component) {
|
||||
return addLabeledComponent(labelText, component, myVerticalGap, false);
|
||||
}
|
||||
|
||||
public FormBuilder addLabeledComponent(String labelText, JComponent component, final int topInset) {
|
||||
return addLabeledComponent(labelText, component, topInset, false);
|
||||
}
|
||||
|
||||
public FormBuilder addLabeledComponent(String labelText, JComponent component, boolean labelOnTop) {
|
||||
return addLabeledComponent(labelText, component, myVerticalGap, labelOnTop);
|
||||
}
|
||||
|
||||
public FormBuilder addLabeledComponent(String labelText, JComponent component, final int topInset, boolean labelOnTop) {
|
||||
JLabel label = null;
|
||||
if (labelText != null) {
|
||||
label = new JLabel(UIUtil.removeMnemonic(labelText));
|
||||
final int index = UIUtil.getDisplayMnemonicIndex(labelText);
|
||||
if (index != -1) {
|
||||
label.setDisplayedMnemonic(labelText.charAt(index+1));
|
||||
label.setDisplayedMnemonic(labelText.charAt(index + 1));
|
||||
}
|
||||
label.setLabelFor(component);
|
||||
}
|
||||
|
||||
return addLabelAndValueComponents(label, component, verticalSpace, false, labelOnTop);
|
||||
return addLabeledComponent(label, component, topInset, labelOnTop);
|
||||
}
|
||||
|
||||
public FormBuilder addLabeledComponent(String labelText, JComponent component) {
|
||||
return addLabeledComponent(labelText, component, 10);
|
||||
public FormBuilder addComponent(JComponent component) {
|
||||
return addLabeledComponent((JLabel)null, component, myVerticalGap, false);
|
||||
}
|
||||
|
||||
public FormBuilder addLabeledComponent(String labelText, JComponent component, boolean labelOnTop) {
|
||||
return addLabeledComponent(labelText, component, 10, labelOnTop);
|
||||
public FormBuilder addComponent(JComponent component, final int topInset) {
|
||||
return addLabeledComponent((JLabel)null, component, topInset, false);
|
||||
}
|
||||
|
||||
public FormBuilder addSeparator(final int verticalSpace) {
|
||||
return addLabelAndValueComponents(new SeparatorComponent(3, 0), new SeparatorComponent(3, 0), verticalSpace, true, false);
|
||||
public FormBuilder addSeparator(final int topInset) {
|
||||
return addComponent(new JSeparator(), topInset);
|
||||
}
|
||||
|
||||
public FormBuilder addSeparator() {
|
||||
return addSeparator(10);
|
||||
return addSeparator(myVerticalGap);
|
||||
}
|
||||
|
||||
private FormBuilder addLabelAndValueComponents(@Nullable JComponent label,
|
||||
JComponent value,
|
||||
final int verticalSpace,
|
||||
boolean fillLabel,
|
||||
boolean labelOnTop) {
|
||||
public FormBuilder addVerticalGap(final int height) {
|
||||
return addLabeledComponent((JLabel)null,
|
||||
new Box.Filler(new Dimension(0, height), new Dimension(0, height), new Dimension(Short.MAX_VALUE, height)));
|
||||
}
|
||||
|
||||
public FormBuilder addLabeledComponent(@Nullable JComponent label, JComponent component, int topInset, boolean labelOnTop) {
|
||||
GridBagConstraints c = new GridBagConstraints();
|
||||
int verticalInset = line > 0 ? verticalSpace : 0;
|
||||
topInset = myLineCount > 0 ? topInset : 0;
|
||||
|
||||
if (vertical) {
|
||||
c.gridwidth = 1;
|
||||
if (myVertical || labelOnTop || label == null) {
|
||||
c.gridwidth = 2;
|
||||
c.gridx = 0;
|
||||
c.gridy = myLineCount;
|
||||
c.weightx = 0;
|
||||
c.weighty = 0;
|
||||
c.fill = NONE;
|
||||
c.anchor = WEST;
|
||||
c.insets = new Insets(topInset, myIndent, DEFAULT_VGAP, 0);
|
||||
|
||||
if (label != null) myPanel.add(label, c);
|
||||
|
||||
c.gridx = 0;
|
||||
c.gridy = line;
|
||||
c.gridy = myLineCount + 1;
|
||||
c.weightx = 1.0;
|
||||
c.fill = GridBagConstraints.NONE;
|
||||
c.anchor = GridBagConstraints.WEST;
|
||||
c.insets = new Insets(verticalInset, this.indent, this.indent, 0);
|
||||
c.weighty = getWeightY(component);
|
||||
c.fill = getFill(component);
|
||||
c.anchor = WEST;
|
||||
c.insets = new Insets(label == null ? topInset : 0, myIndent, 0, 0);
|
||||
|
||||
if (label != null) panel.add(label, c);
|
||||
myPanel.add(component, c);
|
||||
|
||||
c.gridx = 0;
|
||||
c.gridy = line + 1;
|
||||
c.weightx = 1.0;
|
||||
|
||||
c.fill = getFill(value);
|
||||
c.weighty = getWeightY(value);
|
||||
|
||||
c.anchor = GridBagConstraints.WEST;
|
||||
c.insets = new Insets(0, this.indent, 0, this.indent);
|
||||
|
||||
panel.add(value, c);
|
||||
|
||||
line += 2;
|
||||
myLineCount += 2;
|
||||
}
|
||||
else {
|
||||
c.gridwidth = 1;
|
||||
c.gridx = 0;
|
||||
c.gridy = line;
|
||||
c.gridy = myLineCount;
|
||||
c.weightx = 0;
|
||||
|
||||
if (ALIGN_LABELS_TO_RIGHT) {
|
||||
if (labelOnTop) {
|
||||
c.anchor = GridBagConstraints.NORTHEAST;
|
||||
} else {
|
||||
c.anchor = GridBagConstraints.EAST;
|
||||
}
|
||||
} else {
|
||||
if (labelOnTop) {
|
||||
c.anchor = GridBagConstraints.NORTHWEST;
|
||||
} else {
|
||||
c.anchor = GridBagConstraints.WEST;
|
||||
}
|
||||
}
|
||||
|
||||
c.insets = new Insets(verticalInset, this.indent, 0, fillLabel ? 0 : this.indent);
|
||||
c.weighty = 0;
|
||||
c.fill = NONE;
|
||||
c.anchor = myAlignLabelOnRight ? EAST : WEST;
|
||||
c.insets = new Insets(topInset, myIndent, 0, myHorizontalGap);
|
||||
|
||||
if (fillLabel) c.fill = GridBagConstraints.HORIZONTAL;
|
||||
|
||||
if (label != null) panel.add(label, c);
|
||||
myPanel.add(label, c);
|
||||
|
||||
c.gridx = 1;
|
||||
c.gridy = line;
|
||||
c.fill = getFill(value);
|
||||
c.weighty = getWeightY(value);
|
||||
c.anchor = GridBagConstraints.WEST;
|
||||
c.weightx = 1;
|
||||
c.insets = new Insets(verticalInset, this.indent, 0, this.indent);
|
||||
panel.add(value, c);
|
||||
c.weighty = getWeightY(component);
|
||||
c.fill = getFill(component);
|
||||
c.anchor = WEST;
|
||||
c.insets = new Insets(topInset, myIndent, 0, 0);
|
||||
|
||||
line++;
|
||||
myPanel.add(component, c);
|
||||
|
||||
myLineCount++;
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
private static int getFill(JComponent value) {
|
||||
if (value instanceof JComboBox) return GridBagConstraints.NONE;
|
||||
else if (value instanceof JScrollPane) return GridBagConstraints.BOTH;
|
||||
return GridBagConstraints.HORIZONTAL;
|
||||
if (value instanceof JComboBox) {
|
||||
return NONE;
|
||||
}
|
||||
else if (value instanceof JScrollPane) return BOTH;
|
||||
return HORIZONTAL;
|
||||
}
|
||||
|
||||
private static int getWeightY(JComponent value) {
|
||||
@@ -170,10 +180,35 @@ public class FormBuilder {
|
||||
}
|
||||
|
||||
public JPanel getPanel() {
|
||||
return panel;
|
||||
return myPanel;
|
||||
}
|
||||
|
||||
public int getLine() {
|
||||
return line;
|
||||
public int getLineCount() {
|
||||
return myLineCount;
|
||||
}
|
||||
|
||||
public FormBuilder setAlignLabelOnRight(boolean alignLabelOnRight) {
|
||||
myAlignLabelOnRight = alignLabelOnRight;
|
||||
return this;
|
||||
}
|
||||
|
||||
public FormBuilder setVertical(boolean vertical) {
|
||||
myVertical = vertical;
|
||||
return this;
|
||||
}
|
||||
|
||||
public FormBuilder setVerticalGap(int verticalGap) {
|
||||
myVerticalGap = verticalGap;
|
||||
return this;
|
||||
}
|
||||
|
||||
public FormBuilder setHorizontalGap(int horizontalGap) {
|
||||
myHorizontalGap = horizontalGap;
|
||||
return this;
|
||||
}
|
||||
|
||||
public FormBuilder setIndent(int indent) {
|
||||
myIndent = indent;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user