basic functionality

This commit is contained in:
Jörg Henke 2025-04-16 10:50:43 +02:00
parent cddc4951d4
commit 45af8e7904
9 changed files with 358 additions and 0 deletions

12
.classpath Normal file
View File

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="bin/main" path="src/main/java">
<attributes>
<attribute name="gradle_scope" value="main"/>
<attribute name="gradle_used_by_scope" value="main,test"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-17/"/>
<classpathentry kind="con" path="org.eclipse.buildship.core.gradleclasspathcontainer"/>
<classpathentry kind="output" path="bin/default"/>
</classpath>

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
/.gradle/
/bin/

23
.project Normal file
View File

@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>ClickMyText</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.buildship.core.gradleprojectbuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.buildship.core.gradleprojectnature</nature>
</natures>
</projectDescription>

View File

@ -0,0 +1,13 @@
arguments=
auto.sync=false
build.scans.enabled=false
connection.gradle.distribution=GRADLE_DISTRIBUTION(WRAPPER)
connection.project.dir=
eclipse.preferences.version=1
gradle.user.home=
java.home=
jvm.arguments=
offline.mode=false
override.workspace.settings=false
show.console.view=false
show.executions.view=false

View File

@ -0,0 +1,2 @@
eclipse.preferences.version=1
encoding/<project>=UTF-8

View File

@ -0,0 +1,4 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.targetPlatform=17
org.eclipse.jdt.core.compiler.compliance=17
org.eclipse.jdt.core.compiler.source=17

39
build.gradle Normal file
View File

@ -0,0 +1,39 @@
plugins {
id 'java'
id 'eclipse'
id 'application'
}
group 'de.jottyfan.guitools'
version '0.0.1'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.swinglabs.swingx:swingx-core:1.6.5-1'
implementation 'org.apache.commons:commons-lang3:3.12.0'
testImplementation 'org.junit.jupiter:junit-jupiter:5.9.0'
}
application {
mainClass = 'Main'
}
test {
useJUnitPlatform()
}
jar {
manifest {
attributes (
'Main-Class': 'Main',
'Implementation-Version': version
)
}
archiveBaseName = project.name
from {
configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
}
}

View File

@ -0,0 +1,239 @@
package de.jottyfan.guitools;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.HeadlessException;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
/**
*
* @author jotty
*
*/
public class Gui extends JFrame {
private static final long serialVersionUID = 1L;
private JTextArea clipboardContent;
/**
* for the first call
*
* @param args the args; the first one might contain a file path
*/
public Gui(String[] args) {
Set<String> list = new HashSet<>();
if (args != null && args.length > 0) {
String filename = args[1];
try {
list.addAll(readFileLines(filename));
} catch (IOException e) {
e.printStackTrace();
}
}
setup(list);
}
/**
* for the recreation after a new search term has been added
*
* @param list the list of search terms
*/
private Gui(Set<String> list) {
setup(list);
}
/**
* reads the lines of the file; filename is expected to be a text file with one
* word a line
*
* @param filename the file name to the text file
* @return the list of search terms
* @throws IOException on file reading errors
*/
private List<String> readFileLines(String filename) throws IOException {
List<String> list = new ArrayList<>();
try (BufferedReader reader = new BufferedReader(new FileReader(filename))) {
String line;
while ((line = reader.readLine()) != null) {
if (!line.isBlank()) {
list.add(line);
}
}
}
return list;
}
/**
* setup the window
*
* @param clickables the list of search terms
*/
public void setup(Set<String> clickables) {
this.getContentPane().removeAll();
Gui thisWindow = this;
setTitle("ClickMyText");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(600, 400);
setLayout(new BorderLayout());
JPanel menubar = new JPanel();
menubar.setLayout(new FlowLayout());
JTextField textline = new JTextField();
textline.setSize(20, 20);
Dimension d = new Dimension(200, 25);
textline.setMinimumSize(d);
textline.setPreferredSize(d);
textline.setMaximumSize(d);
menubar.add(textline);
JButton plusBtn = new JButton("Add");
plusBtn.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
clickables.add(textline.getText());
SwingUtilities.invokeLater(() -> {
dispose();
Gui gui = new Gui(clickables);
gui.setVisible(true);
});
}
});
menubar.add(plusBtn);
JButton saveBtn = new JButton("Save");
saveBtn.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
JFileChooser fileChooser = new JFileChooser();
int result = fileChooser.showSaveDialog(thisWindow);
if (result == JFileChooser.APPROVE_OPTION) {
String filename = fileChooser.getSelectedFile().getAbsolutePath();
try {
FileWriter writer = new FileWriter(filename);
for (String term : clickables) {
writer.write(term);
writer.write("\n");
}
writer.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
});
menubar.add(saveBtn);
JButton loadBtn = new JButton("Load");
loadBtn.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
JFileChooser fileChooser = new JFileChooser();
int result = fileChooser.showOpenDialog(thisWindow);
if (result == JFileChooser.APPROVE_OPTION) {
String filename = fileChooser.getSelectedFile().getAbsolutePath();
SwingUtilities.invokeLater(() -> {
dispose();
Gui gui = new Gui(new String[] { null, filename });
gui.setVisible(true);
});
}
}
});
menubar.add(loadBtn);
JButton exitBtn = new JButton("Exit");
exitBtn.addMouseListener(new MouseAdapter() {
@Override
public void mouseReleased(MouseEvent e) {
System.exit(0);
}
});
menubar.add(exitBtn);
JPanel linksPanel = new JPanel();
linksPanel.setLayout(new GridLayout(6, 2, 2, 2));
for (String clickable : clickables) {
JButton btn = new JButton(clickable);
btn.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
copyToClipboard(clickable);
}
});
linksPanel.add(btn);
}
clipboardContent = new JTextArea(3, 30);
clipboardContent.setEditable(false);
clipboardContent.setBorder(BorderFactory.createTitledBorder("Clipboard content"));
copyFromClipboard();
JScrollPane scrollPane = new JScrollPane(clipboardContent);
add(menubar, BorderLayout.NORTH);
add(linksPanel, BorderLayout.CENTER);
add(scrollPane, BorderLayout.SOUTH);
}
/**
* copy the clipboard content to the preview field
*
*/
private void copyFromClipboard() {
Object content;
try {
content = Toolkit.getDefaultToolkit().getSystemClipboard().getData(DataFlavor.stringFlavor);
if (content != null) {
clipboardContent.setText(content.toString());
}
} catch (HeadlessException | IOException e) {
e.printStackTrace();
} catch (UnsupportedFlavorException e) {
// ignore; this might be only an empty clipboard
System.out.println("no clipboard content found");
}
}
/**
* the text might be copied to the clipboard immediately
*
* @param text the text to copy
*/
private void copyToClipboard(String text) {
StringSelection selection = new StringSelection(text);
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(selection, null); // update the clipboard from the selection
clipboardContent.setText(text); // update the preview from the clipboard
}
}

View File

@ -0,0 +1,24 @@
package de.jottyfan.guitools;
import javax.swing.SwingUtilities;
/**
*
* @author jotty
*
*/
public class Main {
/**
* start the application; if args is given, the first argument is a filename for
* a list of strings
*
* @param args the args
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
Gui gui = new Gui(args);
gui.setVisible(true);
});
}
}