Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Peace #1

Open
wants to merge 24 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
f35a2c5
Pagination
Cyebukayire May 3, 2021
09351fa
Merge branch 'main' of https://github.com/Ghost-Lab-Official/spiral_d…
Cyebukayire May 3, 2021
b2f5165
Merge branch 'Constance' of https://github.com/Ghost-Lab-Official/spi…
Cyebukayire May 3, 2021
f385943
Merge branch 'main' of https://github.com/Ghost-Lab-Official/spiral_d…
Cyebukayire May 3, 2021
99fec66
Pagination
Cyebukayire May 3, 2021
ab565c4
Pagination
Cyebukayire May 3, 2021
6706f19
Merge branch 'main' of https://github.com/Ghost-Lab-Official/spiral_d…
Cyebukayire May 3, 2021
b5d0b5c
Working on pagination
Cyebukayire May 4, 2021
be8d4f2
pagination
Cyebukayire May 4, 2021
724fc98
Merge branch 'main' of https://github.com/Ghost-Lab-Official/spiral_d…
Cyebukayire May 4, 2021
eaf1336
Working on table pagination
Cyebukayire May 7, 2021
25dd8c5
Working on table pagination
Cyebukayire May 7, 2021
844b524
pagination
Cyebukayire May 29, 2021
03240b7
Table Pagination
Cyebukayire May 29, 2021
557dac5
Table pag
Cyebukayire May 31, 2021
34482b6
pulled from main
Cyebukayire Jun 9, 2021
9a26b71
bug fixing in pagination buttons
Cyebukayire Jun 13, 2021
602fa5c
adding titles to container
Cyebukayire Jun 13, 2021
0a6a5a4
adding mouse listeners
Cyebukayire Jun 13, 2021
6ca3cd4
Listers are working
Cyebukayire Jun 13, 2021
d6f53e3
Listers are working
Cyebukayire Jun 13, 2021
b545776
Listers are working
Cyebukayire Jun 13, 2021
b0a313d
Listeners are working
Cyebukayire Jun 13, 2021
b714256
merge main
Cyebukayire Jun 19, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions JTablePagination.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
Binary file not shown.
Binary file not shown.
1 change: 1 addition & 0 deletions spiral_desktop_version-main.iml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="libs" level="project" />

</component>
</module>
7 changes: 7 additions & 0 deletions spiral_desktop_version.iml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<<<<<<< HEAD
<orderEntry type="library" name="mysql-connector-java-8.0.21" level="project" />
<orderEntry type="library" name="libs" level="project" />
<orderEntry type="library" name="jcommon-1.0.8" level="project" />
<orderEntry type="library" name="jBCrypt-0.4.3" level="project" />
=======
<orderEntry type="library" name="jackson-annotations-2.12.1" level="project" />
<orderEntry type="library" name="jackson-core-2.12.1" level="project" />
<orderEntry type="library" name="jackson-databind-2.12.1" level="project" />
Expand All @@ -19,5 +25,6 @@
<orderEntry type="library" name="jjwt-orgjson-0.11.2" level="project" />
<orderEntry type="library" name="mysql-connector-java-5.1.491" level="project" />
<orderEntry type="library" name="activation-1.1" level="project" />
>>>>>>> 75f045b07f5a12579a38f90da84f0dddeddaf624
</component>
</module>
107 changes: 107 additions & 0 deletions src/CircleButton.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import javax.swing.JButton;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.geom.Point2D;
import java.awt.FontMetrics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class CircleButton extends JButton {

private boolean mouseOver = false;
private boolean mousePressed = false;

public CircleButton(String text){
super(text);
setOpaque(false);
setFocusPainted(false);
setBorderPainted(false);

MouseAdapter mouseListener = new MouseAdapter(){

@Override
public void mousePressed(MouseEvent me){
if(contains(me.getX(), me.getY())){
mousePressed = true;
repaint();
}
}

@Override
public void mouseReleased(MouseEvent me){
mousePressed = false;
repaint();
}

@Override
public void mouseExited(MouseEvent me){
mouseOver = false;
mousePressed = false;
repaint();
}

@Override
public void mouseMoved(MouseEvent me){
mouseOver = contains(me.getX(), me.getY());
repaint();
}
};

addMouseListener(mouseListener);
addMouseMotionListener(mouseListener);
}

private int getDiameter(){
int diameter = Math.min(getWidth(), getHeight());
return diameter;
}

@Override
public Dimension getPreferredSize(){
FontMetrics metrics = getGraphics().getFontMetrics(getFont());
int minDiameter = 15 + Math.max(metrics.stringWidth(getText()), metrics.getHeight());
return new Dimension(minDiameter, minDiameter);
}

@Override
public boolean contains(int x, int y){
int radius = getDiameter()/2;
return Point2D.distance(x, y, getWidth()/2, getHeight()/2) < radius;
}

@Override
public void paintComponent(Graphics g){

int diameter = getDiameter();
int radius = diameter/2;

if(mousePressed){
g.setColor(Color.blue);
// g.getText().setForeground(Color.white);
}
else{
g.setColor(Color.lightGray);
}
g.fillOval(getWidth()/2 - radius, getHeight()/2 - radius, diameter, diameter);

if(mouseOver){
g.setColor(Color.BLUE);
}
else{
g.setColor(Color.lightGray);
}
g.drawOval(getWidth()/2 - radius, getHeight()/2 - radius, diameter, diameter);

if(mousePressed)
g.setColor(Color.white);
else
g.setColor(Color.BLACK);

g.setFont(getFont());
FontMetrics metrics = g.getFontMetrics(getFont());
int stringWidth = metrics.stringWidth(getText());
int stringHeight = metrics.getHeight();
g.drawString(getText(), getWidth()/2 - stringWidth/2, getHeight()/2 + stringHeight/4);
}
}
38 changes: 38 additions & 0 deletions src/Employee.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
public class Employee {
private long id;
private String name;
private String phoneNumber;
private String address;

public long getId() {
return id;
}

public void setId(long id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getPhoneNumber() {
return phoneNumber;
}

public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}
}
41 changes: 41 additions & 0 deletions src/ObjectTableModel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@

import javax.swing.table.AbstractTableModel;
import java.util.ArrayList;
import java.util.List;

public abstract class ObjectTableModel<T> extends AbstractTableModel {
private List<T> objectRows = new ArrayList<>();

public List<T> getObjectRows() {
return objectRows;
}

public void setObjectRows(List<T> objectRows) {
this.objectRows = objectRows;
}

@Override
public int getRowCount() {
return objectRows.size();
}

@Override
public Object getValueAt(int rowIndex, int columnIndex) {
T t = objectRows.get(rowIndex);
return getValueAt(t, columnIndex);
}

@Override
public Class<?> getColumnClass(int columnIndex) {
if (objectRows.isEmpty()) {
return Object.class;
}
Object valueAt = getValueAt(0, columnIndex);
return valueAt != null ? valueAt.getClass() : Object.class;
}

public abstract Object getValueAt(T t, int columnIndex);

@Override
public abstract String getColumnName(int column);
}
158 changes: 158 additions & 0 deletions src/PaginatedTableDecorator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
import javax.swing.*;
import javax.swing.event.TableModelEvent;
import javax.swing.table.TableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.util.Arrays;
import java.util.List;

public class PaginatedTableDecorator<T> {
private JTable table;
private PaginationDataProvider<T> dataProvider;
private int[] pageSizes;
private JPanel contentPanel;
private int currentPageSize;
private int currentPage = 1;
private JPanel pageLinkPanel;
private ObjectTableModel objectTableModel;
private static final int MaxPagingCompToShow = 9;
private static final String Ellipses = "...";

private PaginatedTableDecorator(JTable table, PaginationDataProvider<T> dataProvider,
int[] pageSizes, int defaultPageSize) {
this.table = table;
this.dataProvider = dataProvider;
this.pageSizes = pageSizes;
this.currentPageSize = defaultPageSize;
}

public static <T> PaginatedTableDecorator<T> decorate(JTable table,
PaginationDataProvider<T> dataProvider,
int[] pageSizes, int defaultPageSize) {
PaginatedTableDecorator<T> decorator = new PaginatedTableDecorator<>(table, dataProvider,
pageSizes, defaultPageSize);
decorator.init();
return decorator;
}

public JPanel getContentPanel() {
return contentPanel;
}

private void init() {
initDataModel();
initPaginationComponents();
initListeners();
paginate();
}

private void initListeners() {
objectTableModel.addTableModelListener(this::refreshPageButtonPanel);
}

private void initDataModel() {
TableModel model = table.getModel();
if (!(model instanceof ObjectTableModel)) {
throw new IllegalArgumentException("TableModel must be a subclass of ObjectTableModel");
}
objectTableModel = (ObjectTableModel) model;
}

private void initPaginationComponents() {
contentPanel = new JPanel(new BorderLayout());
JPanel paginationPanel = createPaginationPanel();
contentPanel.add(paginationPanel, BorderLayout.NORTH);
contentPanel.add(new JScrollPane(table));
}

private JPanel createPaginationPanel() {
JPanel paginationPanel = new JPanel();
pageLinkPanel = new JPanel(new GridLayout(1, MaxPagingCompToShow, 3, 3));
paginationPanel.add(pageLinkPanel);

if (pageSizes != null) {
JComboBox<Integer> pageComboBox = new JComboBox<>(
Arrays.stream(pageSizes).boxed()
.toArray(Integer[]::new));
pageComboBox.addActionListener((ActionEvent e) -> {
//to preserve current rows position
int currentPageStartRow = ((currentPage - 1) * currentPageSize) + 1;
currentPageSize = (Integer) pageComboBox.getSelectedItem();
currentPage = ((currentPageStartRow - 1) / currentPageSize) + 1;
paginate();
});
paginationPanel.add(Box.createHorizontalStrut(15));
paginationPanel.add(new JLabel("Page Size: "));
paginationPanel.add(pageComboBox);
pageComboBox.setSelectedItem(currentPageSize);
}
return paginationPanel;
}

private void refreshPageButtonPanel(TableModelEvent tme) {
pageLinkPanel.removeAll();
int totalRows = dataProvider.getTotalRowCount();
int pages = (int) Math.ceil((double) totalRows / currentPageSize);
ButtonGroup buttonGroup = new ButtonGroup();
if (pages > MaxPagingCompToShow) {
addPageButton(pageLinkPanel, buttonGroup, 1);
if (currentPage > (pages - ((MaxPagingCompToShow + 1) / 2))) {
//case: 1 ... n->lastPage
pageLinkPanel.add(createEllipsesComponent());
addPageButtonRange(pageLinkPanel, buttonGroup, pages - MaxPagingCompToShow + 3, pages);
} else if (currentPage <= (MaxPagingCompToShow + 1) / 2) {
//case: 1->n ...lastPage
addPageButtonRange(pageLinkPanel, buttonGroup, 2, MaxPagingCompToShow - 2);
pageLinkPanel.add(createEllipsesComponent());
addPageButton(pageLinkPanel, buttonGroup, pages);
} else {//case: 1 .. x->n .. lastPage
pageLinkPanel.add(createEllipsesComponent());//first ellipses
//currentPage is approx mid point among total max-4 center links
int start = currentPage - (MaxPagingCompToShow - 4) / 2;
int end = start + MaxPagingCompToShow - 5;
addPageButtonRange(pageLinkPanel, buttonGroup, start, end);
pageLinkPanel.add(createEllipsesComponent());//last ellipsis
addPageButton(pageLinkPanel, buttonGroup, pages);//last page link
}
} else {
addPageButtonRange(pageLinkPanel, buttonGroup, 1, pages);
}
pageLinkPanel.getParent().validate();
pageLinkPanel.getParent().repaint();
}

private Component createEllipsesComponent() {
return new JLabel(Ellipses, SwingConstants.CENTER);
}

private void addPageButtonRange(JPanel parentPanel, ButtonGroup buttonGroup, int start, int end) {
for (; start <= end; start++) {
addPageButton(parentPanel, buttonGroup, start);
}
}

private void addPageButton(JPanel parentPanel, ButtonGroup buttonGroup, int pageNumber) {
JToggleButton toggleButton = new JToggleButton(Integer.toString(pageNumber));
toggleButton.setMargin(new Insets(1, 3, 1, 3));
buttonGroup.add(toggleButton);
parentPanel.add(toggleButton);
if (pageNumber == currentPage) {
toggleButton.setSelected(true);
}
toggleButton.addActionListener(ae -> {
currentPage = Integer.parseInt(ae.getActionCommand());
paginate();
});
}

private void paginate() {
int startIndex = (currentPage - 1) * currentPageSize;
int endIndex = startIndex + currentPageSize;
if (endIndex > dataProvider.getTotalRowCount()) {
endIndex = dataProvider.getTotalRowCount();
}
List<T> rows = dataProvider.getRows(startIndex, endIndex);
objectTableModel.setObjectRows(rows);
objectTableModel.fireTableDataChanged();
}
}
Loading