Skip to content

Commit

Permalink
feat: allow to add buttons to rotate document to toolbar-button
Browse files Browse the repository at this point in the history
Close #66
  • Loading branch information
paodb authored and mlopezFC committed Dec 6, 2024
1 parent cd99f8a commit fbbd43f
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,18 @@ public class PdfViewer extends Div {
private boolean addPrintButton = false;
private Button printButton;

/* Indicates if Rotate Clockwise button is added to toolbar or not */
private boolean addRotateClockwiseButton = false;
private Button rotateClockwiseButton;

/* Indicates if Rotate Counterclockwise button is added to toolbar or not */
private boolean addRotateCounterClockwiseButton = false;
private Button rotateCounterClockwiseButton;

private String printButtonTooltipText;
private String downloadButtonTooltipText;
private String rotateClockwiseButtonTooltipText;
private String rotateCounterClockwiseButtonTooltipText;

public PdfViewer() {}

Expand Down Expand Up @@ -360,6 +370,82 @@ public String getDownloadButtonTooltipText() {
return downloadButtonTooltipText;
}

/**
* Returns whether Rotate Clockwise button is added to the toolbar or not.
*
* @return true if the button is added to toolbar
*/
public boolean isAddRotateClockwiseButton() {
return addRotateClockwiseButton;
}

/**
* <p>Sets the flag to indicate if Rotate Clockwise button should be added to toolbar or not.
* By default the flag is set to false, so, by default the button is not added to the toolbar. </p>
* <p>This flag should be set on pdf viewer initialization time.</p>
*
* @param addRotateClockwiseButton true if rotate clockwise button should be added to toolbar
*/
public void setAddRotateClockwiseButton(boolean addRotateClockwiseButton) {
this.addRotateClockwiseButton = addRotateClockwiseButton;
}

/**
* Returns whether Rotate Counterclockwise button is added to the toolbar or not.
*
* @return true if the button is added to toolbar
*/
public boolean isAddRotateCounterClockwiseButton() {
return addRotateCounterClockwiseButton;
}

/**
* <p>Sets the flag to indicate if Rotate Counterclockwise button should be added to toolbar or not.
* By default the flag is set to false, so, by default the button is not added to the toolbar. </p>
* <p>This flag should be set on pdf viewer initialization time.</p>
*
* @param addRotateCounterClockwiseButton true if rotate counterclockwise button should be added to toolbar
*/
public void setAddRotateCounterClockwiseButton(boolean addRotateCounterClockwiseButton) {
this.addRotateCounterClockwiseButton = addRotateCounterClockwiseButton;
}

/**
* Sets tooltip text for the rotate clockwise button in toolbar.
*
* @param rotateClockwiseButtonTooltipText the rotateClockwiseButtonTooltipText to set
*/
public void setRotateClockwiseButtonTooltipText(String rotateClockwiseButtonTooltipText) {
this.rotateClockwiseButtonTooltipText = rotateClockwiseButtonTooltipText;
}

/**
* Returns the tooltip text defined for the rotate clockwise button.
*
* @return the rotate clockwise button tooltip text
*/
public String getRotateClockwiseButtonTooltipText() {
return rotateClockwiseButtonTooltipText;
}

/**
* Sets tooltip text for the rotate counterclockwise button in toolbar.
*
* @param rotateClockwiseButtonTooltipText the rotateClockwiseButtonTooltipText to set
*/
public void setRotateCounterClockwiseButtonTooltipText(String rotateCounterClockwiseButtonTooltipText) {
this.rotateCounterClockwiseButtonTooltipText = rotateCounterClockwiseButtonTooltipText;
}

/**
* Returns the tooltip text defined for the rotate counterclockwise button.
*
* @return the rotate counterclockwise button tooltip text
*/
public String getRotateCounterClockwiseButtonTooltipText() {
return rotateCounterClockwiseButtonTooltipText;
}

@Override
protected void onAttach(AttachEvent attachEvent) {
super.onAttach(attachEvent);
Expand All @@ -369,6 +455,12 @@ protected void onAttach(AttachEvent attachEvent) {
if(addPrintButton){
addPrintButton();
}
if(addRotateClockwiseButton){
addRotateClockwiseButton();
}
if(addRotateCounterClockwiseButton){
addRotateCounterClockwiseButton();
}
}

@Override
Expand All @@ -380,6 +472,12 @@ protected void onDetach(DetachEvent detachEvent) {
if(addPrintButton){
this.getElement().removeChild(printButton.getElement());
}
if(addRotateClockwiseButton){
this.getElement().removeChild(rotateClockwiseButton.getElement());
}
if(addRotateCounterClockwiseButton){
this.getElement().removeChild(rotateCounterClockwiseButton.getElement());
}
}

/**
Expand Down Expand Up @@ -428,4 +526,28 @@ private void addPrintButton() {
printButton.setTooltipText(this.getPrintButtonTooltipText());
}

/**
* Adds button to rotate pdf file clockwise.
*/
private void addRotateClockwiseButton() {
rotateClockwiseButton = new Button(new Icon(VaadinIcon.ROTATE_RIGHT));
rotateClockwiseButton.getElement().setAttribute("aria-label", "Rotate clockwise");
rotateClockwiseButton.setThemeName("rotate-button");
getElement().appendChild(rotateClockwiseButton.getElement());
rotateClockwiseButton.addClickListener(e -> this.getElement().executeJs("this.rotateCw()"));
rotateClockwiseButton.setTooltipText(this.getRotateClockwiseButtonTooltipText());
}

/**
* Adds button to rotate pdf file counterclockwise.
*/
private void addRotateCounterClockwiseButton() {
rotateCounterClockwiseButton = new Button(new Icon(VaadinIcon.ROTATE_LEFT));
rotateCounterClockwiseButton.getElement().setAttribute("aria-label", "Rotate counterclockwise");
rotateCounterClockwiseButton.setThemeName("rotate-button");
getElement().appendChild(rotateCounterClockwiseButton.getElement());
rotateCounterClockwiseButton.addClickListener(e -> this.getElement().executeJs("this.rotateCcw()"));
rotateCounterClockwiseButton.setTooltipText(this.getRotateCounterClockwiseButtonTooltipText());
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
:host([theme~="download-button"]), :host([theme~="print-button"]) {
:host([theme~="download-button"]), :host([theme~="print-button"]), :host([theme~="rotate-button"]) {
background: transparent;
color: var(--lumo-contrast-80pct);
min-width: 0;
Expand Down

0 comments on commit fbbd43f

Please sign in to comment.