Skip to content

Commit

Permalink
Merge pull request #40 from Nikola-Mircic/gui
Browse files Browse the repository at this point in the history
Gui
  • Loading branch information
Nikola-Mircic authored Nov 26, 2024
2 parents cd4e58b + 18c9333 commit 670e12d
Show file tree
Hide file tree
Showing 8 changed files with 372 additions and 132 deletions.
5 changes: 0 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,3 @@

<hr>

### TO-DO list:
- [x] Multiple types of plots
- [x] Total income/outcome/tax calculation
- [ ] Improved PDF file display

Binary file added icons/zoom-in.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added icons/zoom-out.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 4 additions & 2 deletions src/efakturaplus/gui/panels/InvoicePanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,8 @@ public void actionPerformed(ActionEvent e) {
pdfDisplay1 = new PDFDisplay(invoice.pdfInvoice);
frame1.add(pdfDisplay1);
frame1.setSize(700, 1000);
frame1.addKeyListener(pdfDisplay1);
frame1.addKeyListener(pdfDisplay1.inputListener);
frame1.setFocusable(true);
frame1.setVisible(true);
} catch (IOException ex) {
throw new RuntimeException(ex);
Expand All @@ -259,7 +260,8 @@ public void actionPerformed(ActionEvent e) {
pdfDisplay2 = new PDFDisplay(invoice.pdfAttachment);
frame2.add(pdfDisplay2);
frame2.setSize(700, 1000);
frame2.addKeyListener(pdfDisplay2);
frame2.addKeyListener(pdfDisplay2.inputListener);
frame2.setFocusable(true);
frame2.setVisible(true);
} catch (IOException ex) {
throw new RuntimeException(ex);
Expand Down
161 changes: 39 additions & 122 deletions src/efakturaplus/gui/pdf/PDFDisplay.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,179 +11,96 @@

import javax.swing.*;

public class PDFDisplay extends JPanel implements KeyListener, MouseListener, MouseMotionListener {
public class PDFDisplay extends JPanel{
@Serial
private static final long serialVersionUID = 1L;
// byte array containing the PDF file content
private int pageIndex = 0;
private float scale = 1.0F;
private int x_offset = 0;
private int y_offset = 0;

private int mouse_x_origin = -1;
private int mouse_y_origin = -1;
public ArrayList<BufferedImage> pages;
public JPanel pagesPanel;

private ArrayList<BufferedImage> pages;
private JPanel pagesPanel;
public int pageIndex = 0;
public float scale = 1.0F;
public int x_offset = 0;
public int y_offset = 0;

private final PDDocument pdfDocument;
private final PDFRenderer renderer;

private PDFNavigator navigator;
public PDFInputListener inputListener;

public PDFDisplay(PDDocument pdfDocument) throws IOException {
this.pdfDocument = pdfDocument;
this.renderer = new PDFRenderer(pdfDocument);

this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

this.addMouseListener(this);
this.addMouseMotionListener(this);
this.setLayout(new BorderLayout());

drawPages();
updateLayout();

this.inputListener = new PDFInputListener(navigator);

this.addMouseListener(inputListener);
this.addMouseMotionListener(inputListener);
}

private void drawPages() throws IOException {
public void drawPages() throws IOException {
pages = new ArrayList<>();
for(int i = 0; i<pdfDocument.getNumberOfPages(); ++i){
pages.add(renderer.renderImage(i, scale));
}
}

private void updateLayout(){
public void updateLayout(){
this.removeAll();

this.navigator = new PDFNavigator(this);
this.add(navigator, BorderLayout.NORTH);

this.pagesPanel = new JPanel(){
@Override
public void paint(Graphics g) {
super.paint(g);

int pageWidth = pages.getFirst().getWidth();
int pageHeight = pages.getFirst().getHeight() + 5;

System.out.println("Page height: " + pageHeight);
System.out.println("[x_offest, y_offset]: " + x_offset + "," + y_offset);

for(int i = 0; i<pages.size(); ++i) {
g.drawImage(pages.get(i), x_offset, (i - pageIndex) * pageHeight + y_offset, null);
g.drawImage(pages.get(i),
x_offset + (getWidth() - pageWidth)/2,
(i - pageIndex) * pageHeight + y_offset,
null);
}

g.dispose();
}

/*@Override
@Override
public Dimension getPreferredSize() {
return new Dimension(pages.getFirst().getWidth(), pages.getFirst().getHeight());
}*/
};

this.add(pagesPanel);
}

@Override
public void keyTyped(KeyEvent e) {

}

@Override
public void keyPressed(KeyEvent e) {
if(e.isControlDown()){
// Zooming and moving displayed PDF file
switch (e.getKeyCode()){
case KeyEvent.VK_ADD -> {
this.scale += 0.05F;
try {
drawPages();
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}
case KeyEvent.VK_SUBTRACT -> {
this.scale -= 0.05F;
try {
drawPages();
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}
case KeyEvent.VK_LEFT -> {
x_offset -= 20;
}
case KeyEvent.VK_RIGHT -> {
x_offset += 20;
}
}
}else{
// Switching between pages
switch (e.getKeyCode()){
case KeyEvent.VK_RIGHT -> {
this.pageIndex++;

this.pageIndex = Math.min(this.pageIndex, this.pdfDocument.getNumberOfPages() - 1);
y_offset = 0;

System.out.println(pageIndex);
}
case KeyEvent.VK_LEFT -> {
this.pageIndex--;

this.pageIndex = Math.max(this.pageIndex, 0);
y_offset = 0;

System.out.println(pageIndex);
}
case KeyEvent.VK_UP -> {
y_offset += 20;
}
case KeyEvent.VK_DOWN -> {
y_offset -= 20;
}
}
}

this.pagesPanel.repaint();
this.repaint();
}

@Override
public void keyReleased(KeyEvent e) {
};

this.add(pagesPanel, BorderLayout.CENTER);
}

@Override
public void mouseDragged(MouseEvent e) {
x_offset += e.getX() - mouse_x_origin;
y_offset += e.getY() - mouse_y_origin;
mouse_x_origin = e.getX();
mouse_y_origin = e.getY();

this.pagesPanel.repaint();
this.repaint();
public ArrayList<BufferedImage> getPages(){
return pages;
}

@Override
public void mouseMoved(MouseEvent e) {}

@Override
public void mouseClicked(MouseEvent e) {}

@Override
public void mousePressed(MouseEvent e) {
mouse_x_origin = e.getX();
mouse_y_origin = e.getY();
public JPanel getPagesPanel(){
return pagesPanel;
}

@Override
public void mouseReleased(MouseEvent e) {
mouse_x_origin = -1;
mouse_y_origin = -1;
public PDFInputListener getInputListener() {
return inputListener;
}

@Override
public void mouseEntered(MouseEvent e) {

public int getX_offset() {
return x_offset;
}

@Override
public void mouseExited(MouseEvent e) {

public int getY_offset() {
return y_offset;
}
}
101 changes: 101 additions & 0 deletions src/efakturaplus/gui/pdf/PDFInputListener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package efakturaplus.gui.pdf;

import java.awt.event.*;

public class PDFInputListener implements KeyListener, MouseListener, MouseMotionListener {
private PDFNavigator navigator;

private int mouse_x_origin = -1;
private int mouse_y_origin = -1;

public PDFInputListener(PDFNavigator navigator){
this.navigator = navigator;
}

@Override
public void keyTyped(KeyEvent e) {

}

@Override
public void keyPressed(KeyEvent e) {
System.out.println("Key pressed: " + e.getKeyChar());
if(e.isControlDown()){
// Zooming and moving displayed PDF file
switch (e.getKeyCode()){
case KeyEvent.VK_ADD -> {
navigator.changeScale(0.05f);
}
case KeyEvent.VK_SUBTRACT -> {
navigator.changeScale(-0.05f);
}
case KeyEvent.VK_LEFT -> {
navigator.changeOffset(-20, 0);
}
case KeyEvent.VK_RIGHT -> {
navigator.changeOffset(20, 0);
}
}
}else{
// Switching between pages
switch (e.getKeyCode()){
case KeyEvent.VK_RIGHT -> {
navigator.nextPage();
}
case KeyEvent.VK_LEFT -> {
navigator.previousPage();
}
case KeyEvent.VK_UP -> {
navigator.changeOffset(0, 20);
}
case KeyEvent.VK_DOWN -> {
navigator.changeOffset(0, -20);
}
}
}
}

@Override
public void keyReleased(KeyEvent e) {

}

@Override
public void mouseDragged(MouseEvent e) {
int dx = e.getX() - mouse_x_origin;
int dy = e.getY() - mouse_y_origin;

navigator.changeOffset(dx, dy);

mouse_x_origin = e.getX();
mouse_y_origin = e.getY();
}

@Override
public void mouseMoved(MouseEvent e) {}

@Override
public void mouseClicked(MouseEvent e) {}

@Override
public void mousePressed(MouseEvent e) {
mouse_x_origin = e.getX();
mouse_y_origin = e.getY();
}

@Override
public void mouseReleased(MouseEvent e) {
mouse_x_origin = -1;
mouse_y_origin = -1;
}

@Override
public void mouseEntered(MouseEvent e) {

}

@Override
public void mouseExited(MouseEvent e) {

}
}
Loading

0 comments on commit 670e12d

Please sign in to comment.