[cleanup] Refactored naming in ScaleSelector to be more consistent 'Zoom' -> 'Scale'
This commit is contained in:
parent
9aa71c94cf
commit
95b1e8718b
@ -16,23 +16,24 @@ import net.miginfocom.swing.MigLayout;
|
|||||||
import net.sf.openrocket.gui.util.Icons;
|
import net.sf.openrocket.gui.util.Icons;
|
||||||
import net.sf.openrocket.util.StateChangeListener;
|
import net.sf.openrocket.util.StateChangeListener;
|
||||||
|
|
||||||
|
@SuppressWarnings("serial")
|
||||||
public class ScaleSelector extends JPanel {
|
public class ScaleSelector extends JPanel {
|
||||||
|
|
||||||
// Ready zoom settings
|
// Ready zoom settings
|
||||||
private static final DecimalFormat PERCENT_FORMAT = new DecimalFormat("0.#%");
|
private static final DecimalFormat PERCENT_FORMAT = new DecimalFormat("0.#%");
|
||||||
|
|
||||||
private static final double[] ZOOM_LEVELS = { 0.15, 0.25, 0.5, 0.75, 1.0, 1.5, 2.0 };
|
private static final double[] SCALE_LEVELS = { 0.15, 0.25, 0.5, 0.75, 1.0, 1.5, 2.0 };
|
||||||
private static final String ZOOM_FIT = "Fit";
|
private static final String SCALE_FIT = "Fit"; // trans.get("ScaleSelector.something.something");
|
||||||
private static final String[] ZOOM_SETTINGS;
|
private static final String[] SCALE_LABELS;
|
||||||
static {
|
static {
|
||||||
ZOOM_SETTINGS = new String[ZOOM_LEVELS.length + 1];
|
SCALE_LABELS = new String[SCALE_LEVELS.length + 1];
|
||||||
for (int i = 0; i < ZOOM_LEVELS.length; i++)
|
for (int i = 0; i < SCALE_LEVELS.length; i++)
|
||||||
ZOOM_SETTINGS[i] = PERCENT_FORMAT.format(ZOOM_LEVELS[i]);
|
SCALE_LABELS[i] = PERCENT_FORMAT.format(SCALE_LEVELS[i]);
|
||||||
ZOOM_SETTINGS[ZOOM_SETTINGS.length - 1] = ZOOM_FIT;
|
SCALE_LABELS[SCALE_LABELS.length - 1] = SCALE_FIT;
|
||||||
}
|
}
|
||||||
|
|
||||||
private final ScaleScrollPane scrollPane;
|
private final ScaleScrollPane scrollPane;
|
||||||
private JComboBox zoomSelector;
|
private JComboBox<String> scaleSelector;
|
||||||
|
|
||||||
public ScaleSelector(ScaleScrollPane scroll) {
|
public ScaleSelector(ScaleScrollPane scroll) {
|
||||||
super(new MigLayout());
|
super(new MigLayout());
|
||||||
@ -45,29 +46,29 @@ public class ScaleSelector extends JPanel {
|
|||||||
@Override
|
@Override
|
||||||
public void actionPerformed(ActionEvent e) {
|
public void actionPerformed(ActionEvent e) {
|
||||||
double scale = scrollPane.getScaling();
|
double scale = scrollPane.getScaling();
|
||||||
scale = getPreviousScale(scale);
|
scale = getNextLargerScale(scale);
|
||||||
scrollPane.setScaling(scale);
|
scrollPane.setScaling(scale);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
add(button, "gap");
|
add(button, "gap");
|
||||||
|
|
||||||
// Zoom level selector
|
// Zoom level selector
|
||||||
String[] settings = ZOOM_SETTINGS;
|
String[] settings = SCALE_LABELS;
|
||||||
if (!scrollPane.isFittingAllowed()) {
|
if (!scrollPane.isFittingAllowed()) {
|
||||||
settings = Arrays.copyOf(settings, settings.length - 1);
|
settings = Arrays.copyOf(settings, settings.length - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
zoomSelector = new JComboBox(settings);
|
scaleSelector = new JComboBox<>(settings);
|
||||||
zoomSelector.setEditable(true);
|
scaleSelector.setEditable(true);
|
||||||
setZoomText();
|
setZoomText();
|
||||||
zoomSelector.addActionListener(new ActionListener() {
|
scaleSelector.addActionListener(new ActionListener() {
|
||||||
@Override
|
@Override
|
||||||
public void actionPerformed(ActionEvent e) {
|
public void actionPerformed(ActionEvent e) {
|
||||||
try {
|
try {
|
||||||
String text = (String) zoomSelector.getSelectedItem();
|
String text = (String) scaleSelector.getSelectedItem();
|
||||||
text = text.replaceAll("%", "").trim();
|
text = text.replaceAll("%", "").trim();
|
||||||
|
|
||||||
if (text.toLowerCase(Locale.getDefault()).startsWith(ZOOM_FIT.toLowerCase(Locale.getDefault())) &&
|
if (text.toLowerCase(Locale.getDefault()).startsWith(SCALE_FIT.toLowerCase(Locale.getDefault())) &&
|
||||||
scrollPane.isFittingAllowed()) {
|
scrollPane.isFittingAllowed()) {
|
||||||
scrollPane.setFitting(true);
|
scrollPane.setFitting(true);
|
||||||
setZoomText();
|
setZoomText();
|
||||||
@ -93,7 +94,7 @@ public class ScaleSelector extends JPanel {
|
|||||||
setZoomText();
|
setZoomText();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
add(zoomSelector, "gap rel");
|
add(scaleSelector, "gap rel");
|
||||||
|
|
||||||
// Zoom in button
|
// Zoom in button
|
||||||
button = new JButton(Icons.ZOOM_IN);
|
button = new JButton(Icons.ZOOM_IN);
|
||||||
@ -101,7 +102,7 @@ public class ScaleSelector extends JPanel {
|
|||||||
@Override
|
@Override
|
||||||
public void actionPerformed(ActionEvent e) {
|
public void actionPerformed(ActionEvent e) {
|
||||||
double scale = scrollPane.getScaling();
|
double scale = scrollPane.getScaling();
|
||||||
scale = getNextScale(scale);
|
scale = getNextSmallerScale(scale);
|
||||||
scrollPane.setScaling(scale);
|
scrollPane.setScaling(scale);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -110,43 +111,41 @@ public class ScaleSelector extends JPanel {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void setZoomText() {
|
private void setZoomText() {
|
||||||
String text;
|
String text = PERCENT_FORMAT.format(scrollPane.getScaling());
|
||||||
double zoom = scrollPane.getScaling();
|
|
||||||
text = PERCENT_FORMAT.format(zoom);
|
|
||||||
if (scrollPane.isFitting()) {
|
if (scrollPane.isFitting()) {
|
||||||
text = "Fit (" + text + ")";
|
text = "Fit (" + text + ")";
|
||||||
}
|
}
|
||||||
if (!text.equals(zoomSelector.getSelectedItem()))
|
if (!text.equals(scaleSelector.getSelectedItem()))
|
||||||
zoomSelector.setSelectedItem(text);
|
scaleSelector.setSelectedItem(text);
|
||||||
}
|
}
|
||||||
|
|
||||||
private double getPreviousScale(double scale) {
|
private static double getNextLargerScale(final double currentScale) {
|
||||||
int i;
|
int i;
|
||||||
for (i = 0; i < ZOOM_LEVELS.length - 1; i++) {
|
for (i = 0; i < SCALE_LEVELS.length - 1; i++) {
|
||||||
if (scale > ZOOM_LEVELS[i] + 0.05 && scale < ZOOM_LEVELS[i + 1] + 0.05)
|
if (currentScale > SCALE_LEVELS[i] + 0.05 && currentScale < SCALE_LEVELS[i + 1] + 0.05)
|
||||||
return ZOOM_LEVELS[i];
|
return SCALE_LEVELS[i];
|
||||||
}
|
}
|
||||||
if (scale > ZOOM_LEVELS[ZOOM_LEVELS.length / 2]) {
|
if (currentScale > SCALE_LEVELS[SCALE_LEVELS.length / 2]) {
|
||||||
// scale is large, drop to next lowest full 100%
|
// scale is large, drop to next lowest full 100%
|
||||||
scale = Math.ceil(scale - 1.05);
|
double nextScale = Math.ceil(currentScale - 1.05);
|
||||||
return Math.max(scale, ZOOM_LEVELS[i]);
|
return Math.max(nextScale, SCALE_LEVELS[i]);
|
||||||
}
|
}
|
||||||
// scale is small
|
// scale is small
|
||||||
return scale / 1.5;
|
return currentScale / 1.5;
|
||||||
}
|
}
|
||||||
|
|
||||||
private double getNextScale(double scale) {
|
private static double getNextSmallerScale(final double currentScale) {
|
||||||
int i;
|
int i;
|
||||||
for (i = 0; i < ZOOM_LEVELS.length - 1; i++) {
|
for (i = 0; i < SCALE_LEVELS.length - 1; i++) {
|
||||||
if (scale > ZOOM_LEVELS[i] - 0.05 && scale < ZOOM_LEVELS[i + 1] - 0.05)
|
if (currentScale > SCALE_LEVELS[i] - 0.05 && currentScale < SCALE_LEVELS[i + 1] - 0.05)
|
||||||
return ZOOM_LEVELS[i + 1];
|
return SCALE_LEVELS[i + 1];
|
||||||
}
|
}
|
||||||
if (scale > ZOOM_LEVELS[ZOOM_LEVELS.length / 2]) {
|
if (currentScale > SCALE_LEVELS[SCALE_LEVELS.length / 2]) {
|
||||||
// scale is large, give next full 100%
|
// scale is large, give next full 100%
|
||||||
scale = Math.floor(scale + 1.05);
|
double nextScale = Math.floor(currentScale + 1.05);
|
||||||
return scale;
|
return nextScale;
|
||||||
}
|
}
|
||||||
return scale * 1.5;
|
return currentScale * 1.5;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
Loading…
x
Reference in New Issue
Block a user