WIP code for ClipboardHistory

release/1.0.0
Hammy 3 years ago
parent c08511124c
commit 56c4d53290

@ -0,0 +1,116 @@
package me.goudham;
import dorkbox.systemTray.Checkbox;
import dorkbox.systemTray.Separator;
import dorkbox.systemTray.SystemTray;
import java.io.IOException;
import java.util.Objects;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.beans.binding.Bindings;
import javafx.embed.swing.SwingFXUtils;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.ListCell;
import javafx.scene.control.MenuItem;
import javafx.scene.image.Image;
import javafx.stage.Stage;
import me.goudham.event.ImageEvent;
import me.goudham.event.TextEvent;
import me.goudham.exception.UnsupportedSystemException;
public class ClipboardHistoryApplication extends Application {
private MyClipboard myClipboard;
@Override
public void start(Stage stage) throws IOException {
SystemTray.DEBUG = true;
Platform.setImplicitExit(false);
SystemTray systemTray = SystemTray.get();
if (systemTray == null) {
throw new RuntimeException("Unable to load SystemTray!");
}
systemTray.setImage(Objects.requireNonNull(getClass().getClassLoader().getResource("clipboard-white.png")));
systemTray.getMenu().add(new dorkbox.systemTray.MenuItem("Open MyClipboardHistory", e -> {
Platform.runLater(stage::show);
}));
systemTray.getMenu().add(new Separator());
systemTray.getMenu().add(new Checkbox("Monitor Text", e -> System.err.println("Am i checked? " + ((Checkbox) e.getSource()).getChecked())));
systemTray.getMenu().add(new Separator());
systemTray.getMenu().add(new dorkbox.systemTray.MenuItem("Quit", e -> {
systemTray.shutdown();
System.exit(0);
})).setShortcut('Q');
try {
myClipboard = MyClipboard.getSystemClipboard();
myClipboard.startListening();
} catch (UnsupportedSystemException e) {
e.printStackTrace();
}
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getClassLoader().getResource("clipboard-history.fxml"));
Scene scene = new Scene(fxmlLoader.load());
stage.getIcons().add(new Image(String.valueOf(getClass().getClassLoader().getResource("clipboard-black.png"))));
stage.setTitle("MyClipboardHistory");
stage.setHeight(529);
stage.setWidth(765);
stage.setResizable(false);
stage.setScene(scene);
ClipboardHistoryController clipboardHistoryController = fxmlLoader.getController();
clipboardHistoryController.setMyClipboard(myClipboard);
myClipboard.addEventListener((TextEvent) (oldContent, newContent) -> {
Platform.runLater(() -> clipboardHistoryController.textList.getItems().add(0, newContent));
});
myClipboard.addEventListener((ImageEvent) (oldContent, newContent) -> {
Platform.runLater(() -> {
clipboardHistoryController.clipboardImage.setImage(SwingFXUtils.toFXImage(newContent, null));
});
});
clipboardHistoryController.textList.setCellFactory(lv -> {
ListCell<String> cell = new ListCell<>();
ContextMenu contextMenu = new ContextMenu();
MenuItem editItem = new MenuItem();
editItem.textProperty().bind(Bindings.format("Copy", cell.itemProperty()));
editItem.setOnAction(event -> {
String contentToCopy = cell.getItem();
clipboardHistoryController.textList.getItems().remove(contentToCopy);
myClipboard.insertAndNotify(contentToCopy);
});
MenuItem deleteItem = new MenuItem();
deleteItem.textProperty().bind(Bindings.format("Remove", cell.itemProperty()));
deleteItem.setOnAction(event -> clipboardHistoryController.textList.getItems().remove(cell.getItem()));
contextMenu.getItems().addAll(editItem, deleteItem);
cell.textProperty().bind(cell.itemProperty());
cell.emptyProperty().addListener((obs, wasEmpty, isNowEmpty) -> {
if (isNowEmpty) {
cell.setContextMenu(null);
} else {
cell.setContextMenu(contextMenu);
}
});
return cell ;
});
stage.setOnCloseRequest(event -> stage.hide());
stage.show();
}
public static void main(String[] args) {
launch();
}
}

@ -0,0 +1,22 @@
package me.goudham;
import javafx.scene.control.ListView;
import javafx.scene.control.TabPane;
import javafx.scene.image.ImageView;
public class ClipboardHistoryController {
public TabPane tabPane;
public ListView<String> textList;
public ListView<String> fileList;
public ImageView clipboardImage;
private MyClipboard myClipboard;
public MyClipboard getMyClipboard() {
return myClipboard;
}
public void setMyClipboard(MyClipboard myClipboard) {
this.myClipboard = myClipboard;
}
}

@ -0,0 +1,69 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Rectangle2D?>
<?import javafx.scene.control.ListView?>
<?import javafx.scene.control.ScrollPane?>
<?import javafx.scene.control.Tab?>
<?import javafx.scene.control.TabPane?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<TabPane fx:id="tabPane" prefHeight="529.0" prefWidth="765.0" side="LEFT" style="-fx-background-color: #00000d;" stylesheets="@css/tabPane.css" tabClosingPolicy="UNAVAILABLE" xmlns="http://javafx.com/javafx/16" xmlns:fx="http://javafx.com/fxml/1" fx:controller="me.goudham.ClipboardHistoryController">
<tabs>
<Tab style="-fx-background-color: #00000d;">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="505.0" prefWidth="301.0">
<children>
<ListView fx:id="textList" layoutY="54.0" prefHeight="600.0" prefWidth="645.0" stylesheets="@css/list.css" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
</children>
</AnchorPane>
</content>
<graphic>
<ImageView fitHeight="62.0" fitWidth="62.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@icons8-text.png" />
</image>
</ImageView>
</graphic>
</Tab>
<Tab style="-fx-background-color: #00000d;">
<graphic>
<ImageView fitHeight="62.0" fitWidth="62.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@icons8-image.png" />
</image>
<viewport>
<Rectangle2D />
</viewport>
</ImageView>
</graphic>
<content>
<AnchorPane prefHeight="653.0" prefWidth="668.0">
<children>
<ScrollPane prefHeight="653.0" prefWidth="668.0" style="-fx-background-color: transparent; -fx-background: #00000d;" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<content>
<ImageView fx:id="clipboardImage" fitHeight="653.0" fitWidth="668.0" pickOnBounds="true" preserveRatio="true" />
</content>
</ScrollPane>
</children>
</AnchorPane>
</content>
</Tab>
<Tab fx:id="file" style="-fx-background-color: #00000d;">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="277.0">
<children>
<ListView fx:id="fileList" prefHeight="530.0" prefWidth="661.0" stylesheets="@css/list.css" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
</children></AnchorPane>
</content>
<graphic>
<ImageView fitHeight="62.0" fitWidth="62.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@icons8-file.png" />
</image>
</ImageView>
</graphic>
</Tab>
</tabs>
</TabPane>
Loading…
Cancel
Save