Merge pull request #3190 from Akarshit/enhan-findReplace

Find/Replace dialog uses GroupLayout
This commit is contained in:
Ben Fry
2015-04-17 17:23:53 -04:00
+138 -79
View File
@@ -26,6 +26,7 @@ import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.GroupLayout.Group;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
@@ -35,10 +36,10 @@ import javax.swing.text.Document;
*/
public class FindReplace extends JFrame {
static final int EDGE = Base.isMacOS() ? 20 : 13;
static final int SMALL = 6;
// 12 is correct for Mac, other numbers may be required for other platforms
static final int BUTTON_GAP = 12;
// static final int EDGE = Base.isMacOS() ? 20 : 13;
// static final int SMALL = 6;
// // 12 is correct for Mac, other numbers may be required for other platforms
// static final int BUTTON_GAP = 12;
Editor editor;
@@ -65,22 +66,22 @@ public class FindReplace extends JFrame {
public FindReplace(Editor editor) {
super(Language.text("find"));
setResizable(false);
setResizable(true);
this.editor = editor;
Container pain = getContentPane();
pain.setLayout(null);
JLabel findLabel = new JLabel(Language.text("find.find"));
JLabel replaceLabel = new JLabel(Language.text("find.replace_with"));
Dimension labelDimension = replaceLabel.getPreferredSize();
// Dimension labelDimension = replaceLabel.getPreferredSize();
findField = new JTextField();
replaceField = new JTextField();
// pain.add(findLabel);
// pain.add(replaceLabel);
pain.add(findLabel);
pain.add(replaceLabel);
pain.add(findField = new JTextField());
pain.add(replaceField = new JTextField());
int fieldHeight = findField.getPreferredSize().height;
// pain.add(findField = new JTextField());
// pain.add(replaceField = new JTextField());
// int fieldHeight = findField.getPreferredSize().height;
if (findString != null) findField.setText(findString);
if (replaceString != null) replaceField.setText(replaceString);
@@ -92,7 +93,7 @@ public class FindReplace extends JFrame {
}
});
ignoreCaseBox.setSelected(ignoreCase);
pain.add(ignoreCaseBox);
// pain.add(ignoreCaseBox);
allTabsBox = new JCheckBox(Language.text("find.all_tabs"));
allTabsBox.addActionListener(new ActionListener() {
@@ -102,7 +103,7 @@ public class FindReplace extends JFrame {
});
allTabsBox.setSelected(allTabs);
allTabsBox.setEnabled(true);
pain.add(allTabsBox);
// pain.add(allTabsBox);
wrapAroundBox = new JCheckBox(Language.text("find.wrap_around"));
wrapAroundBox.addActionListener(new ActionListener() {
@@ -111,10 +112,16 @@ public class FindReplace extends JFrame {
}
});
wrapAroundBox.setSelected(wrapAround);
pain.add(wrapAroundBox);
// pain.add(wrapAroundBox);
JPanel buttons = new JPanel();
buttons.setLayout(new FlowLayout(FlowLayout.CENTER,BUTTON_GAP, 0));
// JPanel buttons = new JPanel();
// buttons.setLayout(new FlowLayout(FlowLayout.CENTER,BUTTON_GAP, 0));
GroupLayout layout = new GroupLayout(pain);
pain.setLayout(layout);
layout.setAutoCreateGaps(true);
layout.setAutoCreateContainerGaps(true);
Group buttonsHorizontalGroup = layout.createSequentialGroup(); // To hold the buttons in the specified order depending on the OS
replaceAllButton = new JButton(Language.text("find.btn.replace_all"));
replaceButton = new JButton(Language.text("find.btn.replace"));
@@ -124,81 +131,133 @@ public class FindReplace extends JFrame {
// ordering is different on mac versus pc
if (Base.isMacOS()) {
buttons.add(replaceAllButton);
buttons.add(replaceButton);
buttons.add(replaceAndFindButton);
buttons.add(previousButton);
buttons.add(findButton);
buttonsHorizontalGroup.addComponent(replaceAllButton)
.addComponent(replaceButton)
.addComponent(replaceAndFindButton)
.addComponent(previousButton)
.addComponent(findButton);
// buttons.add(replaceAllButton);
// buttons.add(replaceButton);
// buttons.add(replaceAndFindButton);
// buttons.add(previousButton);
// buttons.add(findButton);
// to fix ugliness.. normally macosx java 1.3 puts an
// ugly white border around this object, so turn it off.
buttons.setBorder(null);
// buttons.setBorder(null);
} else {
buttons.add(findButton);
buttons.add(previousButton);
buttons.add(replaceAndFindButton);
buttons.add(replaceButton);
buttons.add(replaceAllButton);
buttonsHorizontalGroup.addComponent(findButton)
.addComponent(previousButton)
.addComponent(replaceAndFindButton)
.addComponent(replaceButton)
.addComponent(replaceAllButton);
// buttons.add(findButton);
// buttons.add(previousButton);
// buttons.add(replaceAndFindButton);
// buttons.add(replaceButton);
// buttons.add(replaceAllButton);
}
pain.add(buttons);
// pain.add(buttons);
setFound(false);
Group buttonsVerticalGroup = layout.createParallelGroup(); // Creates group for arranging buttons vertically
buttonsVerticalGroup.addComponent(findButton)
.addComponent(previousButton)
.addComponent(replaceAndFindButton)
.addComponent(replaceButton)
.addComponent(replaceAllButton);
Dimension buttonsDimension = buttons.getPreferredSize();
int visibleButtonWidth = buttonsDimension.width - 2 * BUTTON_GAP;
int fieldWidth = visibleButtonWidth - (labelDimension.width + SMALL);
layout.setHorizontalGroup(layout.createParallelGroup()
.addGroup(GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() // TRAILING makes everything right alinged
.addComponent(findLabel)
.addComponent(findField))
.addGroup(GroupLayout.Alignment.TRAILING,
layout.createSequentialGroup()
.addComponent(replaceLabel)
.addComponent(replaceField))
.addGroup(GroupLayout.Alignment.TRAILING,
layout.createSequentialGroup()
.addComponent(ignoreCaseBox)
.addComponent(allTabsBox)
.addComponent(wrapAroundBox))
.addGroup(buttonsHorizontalGroup));
// +1 since it's better to tend downwards
int yoff = (1 + fieldHeight - labelDimension.height) / 2;
layout.linkSize(SwingConstants.HORIZONTAL, findLabel, replaceLabel); //Making size of findLabel and replaceLabel equal
int ypos = EDGE;
layout.setVerticalGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.CENTER)
.addComponent(findLabel)
.addComponent(findField))
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.CENTER)
.addComponent(replaceLabel)
.addComponent(replaceField))
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.CENTER)
.addComponent(ignoreCaseBox)
.addComponent(allTabsBox)
.addComponent(wrapAroundBox))
.addGroup(buttonsVerticalGroup));
int labelWidth = findLabel.getPreferredSize().width;
findLabel.setBounds(EDGE + (labelDimension.width-labelWidth), ypos + yoff, // + yoff was added to the wrong field
labelWidth, labelDimension.height);
findField.setBounds(EDGE + labelDimension.width + SMALL, ypos,
fieldWidth, fieldHeight);
// Dimension buttonsDimension = buttons.getPreferredSize();
// int visibleButtonWidth = buttonsDimension.width - 2 * BUTTON_GAP;
// int fieldWidth = visibleButtonWidth - (labelDimension.width + SMALL);
ypos += fieldHeight + SMALL;
labelWidth = replaceLabel.getPreferredSize().width;
replaceLabel.setBounds(EDGE + (labelDimension.width-labelWidth), ypos + yoff,
labelWidth, labelDimension.height);
replaceField.setBounds(EDGE + labelDimension.width + SMALL, ypos,
fieldWidth, fieldHeight);
ypos += fieldHeight + SMALL;
final int third = (fieldWidth - SMALL*2) / 3;
ignoreCaseBox.setBounds(EDGE + labelDimension.width + SMALL,
ypos,
third, fieldHeight);
allTabsBox.setBounds(EDGE + labelDimension.width + SMALL + third + SMALL,
ypos,
third, fieldHeight);
//wrapAroundBox.setBounds(EDGE + labelDimension.width + SMALL + (fieldWidth-SMALL)/2 + SMALL,
wrapAroundBox.setBounds(EDGE + labelDimension.width + SMALL + third*2 + SMALL*2,
ypos,
third, fieldHeight);
ypos += fieldHeight + SMALL;
buttons.setBounds(EDGE-BUTTON_GAP, ypos,
buttonsDimension.width, buttonsDimension.height);
ypos += buttonsDimension.height + EDGE;
int wide = visibleButtonWidth + EDGE*2;
int high = ypos;
pack();
Insets insets = getInsets();
setSize(wide + insets.left + insets.right,high + insets.top + insets.bottom);
// // +1 since it's better to tend downwards
// int yoff = (1 + fieldHeight - labelDimension.height) / 2;
//
// int ypos = EDGE;
//
// int labelWidth = findLabel.getPreferredSize().width;
// findLabel.setBounds(EDGE + (labelDimension.width-labelWidth), ypos + yoff, // + yoff was added to the wrong field
// labelWidth, labelDimension.height);
// findField.setBounds(EDGE + labelDimension.width + SMALL, ypos,
// fieldWidth, fieldHeight);
//
// ypos += fieldHeight + SMALL;
//
// labelWidth = replaceLabel.getPreferredSize().width;
// replaceLabel.setBounds(EDGE + (labelDimension.width-labelWidth), ypos + yoff,
// labelWidth, labelDimension.height);
// replaceField.setBounds(EDGE + labelDimension.width + SMALL, ypos,
// fieldWidth, fieldHeight);
//
// ypos += fieldHeight + SMALL;
//
// final int third = (fieldWidth - SMALL*2) / 3;
// ignoreCaseBox.setBounds(EDGE + labelDimension.width + SMALL,
// ypos,
// third, fieldHeight);
//
// allTabsBox.setBounds(EDGE + labelDimension.width + SMALL + third + SMALL,
// ypos,
// third, fieldHeight);
//
// //wrapAroundBox.setBounds(EDGE + labelDimension.width + SMALL + (fieldWidth-SMALL)/2 + SMALL,
// wrapAroundBox.setBounds(EDGE + labelDimension.width + SMALL + third*2 + SMALL*2,
// ypos,
// third, fieldHeight);
//
// ypos += fieldHeight + SMALL;
//
// buttons.setBounds(EDGE-BUTTON_GAP, ypos,
// buttonsDimension.width, buttonsDimension.height);
//
// ypos += buttonsDimension.height + EDGE;
//
// int wide = visibleButtonWidth + EDGE*2;
// int high = ypos;
//
// pack();
// Insets insets = getInsets();
// setSize(wide + insets.left + insets.right,high + insets.top + insets.bottom);
setLocationRelativeTo(null); // center
Dimension size = layout.preferredLayoutSize(pain);
setSize(size.width, size.height);
Dimension screen = Toolkit.getScreenSize();
setLocation((screen.width - size.width) / 2,
(screen.height - size.height) / 2);
replaceButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
replace();