package be.nikiroo.utils.ui;
import java.awt.Dimension;
-import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
+import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.Icon;
import javax.swing.JButton;
public static final String PAGE_CHANGED = "page changed";
private JTextField page;
+ private JLabel pageLabel;
private JLabel maxPage;
private JLabel label;
private int max = 0;
private String extraLabel = null;
+ private boolean vertical;
+
private JButton first;
private JButton previous;
private JButton next;
/**
* Create a new navigation bar.
* <p>
- * The minimum must be lower or equal to the maximum.
+ * The minimum must be lower or equal to the maximum, but a max of "-1"
+ * means "infinite".
* <p>
- * Note than a max of "-1" means "infinite".
+ * A {@link NavBar#PAGE_CHANGED} event will be fired on startup.
*
* @param min
* the minimum page number (cannot be negative)
String.format("min (%d) > max (%d)", min, max));
}
- LayoutManager layout = new BoxLayout(this, BoxLayout.X_AXIS);
- setLayout(layout);
-
// Page navigation
first = new JButton();
first.addActionListener(new ActionListener() {
}
});
+ final int defaultHeight = new JButton("dummy")
+ .getPreferredSize().height;
+ final int width4 = new JButton("1234").getPreferredSize().width;
page = new JTextField(Integer.toString(min));
- page.setPreferredSize(
- new Dimension(new JButton("1234").getPreferredSize().width,
- new JButton("dummy").getPreferredSize().height));
- page.setMaximumSize(new Dimension(Integer.MAX_VALUE,
- page.getPreferredSize().height));
+ page.setPreferredSize(new Dimension(width4, defaultHeight));
page.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
}
});
+ pageLabel = new JLabel(Integer.toString(min));
+ pageLabel.setBorder(BorderFactory.createEmptyBorder(0, 10, 0, 10));
+
maxPage = new JLabel("of " + max);
+ maxPage.setBorder(BorderFactory.createEmptyBorder(0, 10, 0, 10));
next = new JButton();
next.addActionListener(new ActionListener() {
}
});
+ label = new JLabel("");
+
// Set the << < > >> "icons"
setIcons(null, null, null, null);
- this.add(first);
- this.add(previous);
- this.add(new JLabel(" "));
- this.add(page);
- this.add(new JLabel(" "));
- this.add(maxPage);
- this.add(new JLabel(" "));
- this.add(next);
- this.add(last);
-
- this.add(label = new JLabel(""));
-
this.min = min;
this.max = max;
this.index = min;
updateEnabled();
updateLabel();
+ setOrientation(vertical);
+
fireActionPerformed(PAGE_CHANGED);
}
this.last.setText(last == null ? ">>" : "");
}
+ /**
+ * The general orientation of the component.
+ *
+ * @return TRUE for vertical orientation, FALSE for horisontal orientation
+ */
+ public boolean getOrientation() {
+ return vertical;
+ }
+
+ /**
+ * Update the general orientation of the component.
+ *
+ * @param vertical
+ * TRUE for vertical orientation, FALSE for horisontal
+ * orientation
+ *
+ * @return TRUE if it changed something
+ */
+ public boolean setOrientation(boolean vertical) {
+ if (getWidth() == 0 || this.vertical != vertical) {
+ this.vertical = vertical;
+
+ BoxLayout layout = new BoxLayout(this,
+ vertical ? BoxLayout.Y_AXIS : BoxLayout.X_AXIS);
+ this.removeAll();
+ setLayout(layout);
+
+ this.add(first);
+ this.add(previous);
+ if (vertical) {
+ this.add(pageLabel);
+ } else {
+ this.add(page);
+ }
+ this.add(maxPage);
+ this.add(next);
+ this.add(last);
+
+ if (!vertical) {
+ this.add(label);
+ }
+
+ this.revalidate();
+ this.repaint();
+
+ return true;
+ }
+
+ return false;
+ }
+
/**
* Update the label displayed in the UI.
*/
private void updateLabel() {
label.setText(getExtraLabel());
+ pageLabel.setText(Integer.toString(index));
page.setText(Integer.toString(index));
}