Pārlūkot izejas kodu

Set up repository with initial classes

Sam Jaffe 8 gadi atpakaļ
vecāks
revīzija
b798b6ab54

+ 128 - 0
pom.xml

@@ -0,0 +1,128 @@
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+  <modelVersion>4.0.0</modelVersion>
+  
+  <groupId>org.leumasjaffe</groupId>
+  <artifactId>event</artifactId>
+  <version>0.1</version>
+  <packaging>jar</packaging>
+
+  <name>event</name>
+  <url>http://maven.apache.org</url>
+
+  <properties>
+    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+  </properties>
+
+  <build>
+    <pluginManagement>
+      <plugins>
+        <plugin>
+          <groupId>org.eclipse.m2e</groupId>
+          <artifactId>lifecycle-mapping</artifactId>
+          <version>1.0.0</version>
+          <configuration>
+            <lifecycleMappingMetadata>
+              <pluginExecutions>
+                <pluginExecution>
+                  <pluginExecutionFilter>
+                    <groupId>org.projectlombok</groupId>
+                    <artifactId>lombok-maven-plugin</artifactId>
+                    <versionRange>[1,)</versionRange>
+                    <goals>
+                      <goal>delombok</goal>
+                    </goals>
+                  </pluginExecutionFilter>
+                  <action>
+                    <ignore />
+                  </action>
+                </pluginExecution>
+              </pluginExecutions>
+            </lifecycleMappingMetadata>
+          </configuration>
+        </plugin>
+      </plugins>
+    </pluginManagement>
+    <sourceDirectory>target/generated-sources/delombok</sourceDirectory>
+    <plugins>
+      <plugin>
+        <artifactId>maven-compiler-plugin</artifactId>
+        <version>3.5.1</version>
+        <configuration>
+          <compilerVersion>1.8</compilerVersion>
+          <source>1.8</source>
+          <target>1.8</target>
+        </configuration>
+      </plugin>
+      <plugin>
+        <groupId>org.projectlombok</groupId>
+        <artifactId>lombok-maven-plugin</artifactId>
+        <version>1.16.18.0</version>
+        <executions>
+          <execution>
+            <id>delombok</id>
+            <phase>generate-sources</phase>
+            <goals>
+              <goal>delombok</goal>
+            </goals>
+          </execution>
+        </executions>
+        <configuration>
+          <addOutputDirectory>false</addOutputDirectory>
+        </configuration>
+      </plugin>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-jar-plugin</artifactId>
+        <version>2.4</version>
+        <configuration>
+          <archive>
+            <manifest>
+              <addClasspath>true</addClasspath>
+              <classpathPrefix>dependency-jars/</classpathPrefix>
+            </manifest>
+          </archive>
+        </configuration>
+      </plugin>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-dependency-plugin</artifactId>
+        <version>2.5.1</version>
+        <executions>
+          <execution>
+            <id>copy-dependencies</id>
+            <phase>package</phase>
+            <goals>
+              <goal>copy-dependencies</goal>
+            </goals>
+            <configuration>
+              <outputDirectory>
+                ${project.build.directory}/dependency-jars/
+              </outputDirectory>
+            </configuration>
+          </execution>
+        </executions>
+      </plugin>
+    </plugins>
+  </build>
+
+  <dependencies>
+    <dependency>
+      <groupId>junit</groupId>
+      <artifactId>junit</artifactId>
+      <version>3.8.1</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.projectlombok</groupId>
+      <artifactId>lombok</artifactId>
+      <version>1.16.18</version>
+    </dependency>
+    <dependency>
+      <groupId>org.projectlombok</groupId>
+      <artifactId>lombok-maven-plugin</artifactId>
+      <version>1.16.18.0</version>
+      <type>maven-plugin</type>
+    </dependency>
+  </dependencies>
+</project>

+ 83 - 0
src/main/lombok/org/leumasjaffe/event/AnyActionDocumentListener.java

@@ -0,0 +1,83 @@
+package org.leumasjaffe.event;
+
+import java.beans.PropertyChangeEvent;
+import java.util.Objects;
+import java.util.function.Consumer;
+
+import javax.swing.SwingUtilities;
+import javax.swing.event.ChangeEvent;
+import javax.swing.event.ChangeListener;
+import javax.swing.event.DocumentEvent;
+import javax.swing.event.DocumentListener;
+import javax.swing.text.Document;
+import javax.swing.text.JTextComponent;
+
+@FunctionalInterface
+public interface AnyActionDocumentListener extends DocumentListener {
+
+	public void update(DocumentEvent e);
+	
+	public default void insertUpdate(DocumentEvent e) { this.update(e); }
+    public default void removeUpdate(DocumentEvent e) { this.update(e); }
+    public default void changedUpdate(DocumentEvent e) { this.update(e); }
+    
+    public static void skipEmpty(final JTextComponent text, 
+    		final Consumer<ChangeEvent> in) {
+    	addChangeListener(text, e -> {
+    		if (text.getText().trim().isEmpty()) { return; }
+    		in.accept(e);
+    	});
+    }
+    
+    public static void emptyOrText(final JTextComponent text, 
+    		final Consumer<ChangeEvent> empty,
+    		final Consumer<ChangeEvent> in) {
+    	addChangeListener(text, e -> {
+    		if (text.getText().trim().isEmpty()) { empty.accept(e); }
+    		else { in.accept(e); }
+    	});
+    }
+    
+    /**
+	 * Installs a listener to receive notification when the text of any
+	 * {@code JTextComponent} is changed. Internally, it installs a
+	 * {@link DocumentListener} on the text component's {@link Document},
+	 * and a {@link PropertyChangeListener} on the text component to detect
+	 * if the {@code Document} itself is replaced.
+	 * 
+	 * @param text any text component, such as a {@link JTextField}
+	 *        or {@link JTextArea}
+	 * @param changeListener a listener to receieve {@link ChangeEvent}s
+	 *        when the text is changed; the source object for the events
+	 *        will be the text component
+	 * @throws NullPointerException if either parameter is null
+	 */
+	public static void addChangeListener(final JTextComponent text, 
+			final ChangeListener changeListener) {
+	    Objects.requireNonNull(text);
+	    Objects.requireNonNull(changeListener);
+	    final DocumentListener dl = new AnyActionDocumentListener() {
+	        private int lastChange = 0, lastNotifiedChange = 0;
+
+	        @Override
+	        public void update(DocumentEvent e) {
+	            lastChange++;
+	            SwingUtilities.invokeLater(() -> {
+	                if (lastNotifiedChange != lastChange) {
+	                    lastNotifiedChange = lastChange;
+	                    changeListener.stateChanged(new ChangeEvent(text));
+	                }
+	            });
+	        }
+	    };
+	    text.addPropertyChangeListener("document", (PropertyChangeEvent e) -> {
+	        Document d1 = (Document)e.getOldValue();
+	        Document d2 = (Document)e.getNewValue();
+	        if (d1 != null) d1.removeDocumentListener(dl);
+	        if (d2 != null) d2.addDocumentListener(dl);
+	        dl.changedUpdate(null);
+	    });
+	    Document d = text.getDocument();
+	    if (d != null) d.addDocumentListener(dl);
+	}
+}

+ 24 - 0
src/main/lombok/org/leumasjaffe/event/LeftRightClickListener.java

@@ -0,0 +1,24 @@
+package org.leumasjaffe.event;
+
+import java.awt.event.MouseEvent;
+
+import javax.swing.JPopupMenu;
+
+public abstract class LeftRightClickListener extends PopClickListener {
+	public LeftRightClickListener(JPopupMenu popup) {
+		super(popup);
+	}
+	
+	public void mousePressed(MouseEvent e){
+		if (e.isPopupTrigger()) super.mousePressed(e);
+		else implMousePressed(e);
+	}
+
+	public void mouseReleased(MouseEvent e){
+		if (e.isPopupTrigger()) super.mouseReleased(e);
+		else implMouseReleased(e);
+	}
+	
+	protected abstract void implMousePressed(MouseEvent e);
+	protected abstract void implMouseReleased(MouseEvent e);
+}

+ 35 - 0
src/main/lombok/org/leumasjaffe/event/LinkedListSelectionListener.java

@@ -0,0 +1,35 @@
+package org.leumasjaffe.event;
+
+import java.util.Collection;
+import java.util.function.Consumer;
+
+import javax.swing.JList;
+import javax.swing.event.ListSelectionEvent;
+import javax.swing.event.ListSelectionListener;
+
+import lombok.AccessLevel;
+import lombok.AllArgsConstructor;
+import lombok.experimental.FieldDefaults;
+
+@AllArgsConstructor 
+@FieldDefaults(level=AccessLevel.PRIVATE, makeFinal=true)
+public class LinkedListSelectionListener<T> implements ListSelectionListener {
+
+	@Override
+	public void valueChanged(ListSelectionEvent e) {
+		@SuppressWarnings("unchecked")
+		JList<T> list = ((JList<T>) e.getSource());
+		if (list.getSelectedIndex() >= 0 && e.getValueIsAdjusting() == false) {
+			for (JList<T> ol : friends) {
+				if (ol != list)
+					ol.clearSelection();
+			}
+			if (activation != null) {
+				activation.accept(list.getSelectedValue());
+			}
+		}
+	}
+	
+	Collection<JList<T>> friends;
+	Consumer<T> activation;
+}

+ 32 - 0
src/main/lombok/org/leumasjaffe/event/PopClickListener.java

@@ -0,0 +1,32 @@
+package org.leumasjaffe.event;
+
+import java.awt.event.MouseAdapter;
+import java.awt.event.MouseEvent;
+
+import javax.swing.JPopupMenu;
+
+import lombok.AccessLevel;
+import lombok.RequiredArgsConstructor;
+import lombok.experimental.FieldDefaults;
+
+@FieldDefaults(level=AccessLevel.PRIVATE, makeFinal=true)
+@RequiredArgsConstructor
+public class PopClickListener extends MouseAdapter {
+	JPopupMenu popup;
+	
+	public void mousePressed(MouseEvent e){
+		if (e.isPopupTrigger()) {
+			doPop(e);
+		}
+	}
+
+	public void mouseReleased(MouseEvent e){
+		if (e.isPopupTrigger()) {
+			doPop(e);
+		}
+	}
+
+	private void doPop(MouseEvent e){
+		popup.show(e.getComponent(), e.getX(), e.getY());
+	}
+}

+ 36 - 0
src/main/lombok/org/leumasjaffe/event/SelectTableRowPopupMenuListener.java

@@ -0,0 +1,36 @@
+package org.leumasjaffe.event;
+
+import java.awt.Point;
+
+import javax.swing.JPopupMenu;
+import javax.swing.JTable;
+import javax.swing.SwingUtilities;
+import javax.swing.event.PopupMenuEvent;
+import javax.swing.event.PopupMenuListener;
+
+import lombok.AllArgsConstructor;
+
+@AllArgsConstructor
+public final class SelectTableRowPopupMenuListener implements PopupMenuListener {
+	JPopupMenu popupMenu;
+	JTable table;
+
+	@Override
+	public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
+	    SwingUtilities.invokeLater(new Runnable() {
+	        @Override
+	        public void run() {
+	            int rowAtPoint = table.rowAtPoint(SwingUtilities.convertPoint(popupMenu, new Point(0, 0), table));
+	            if (rowAtPoint > -1) {
+	            	table.setRowSelectionInterval(rowAtPoint, rowAtPoint);
+	            }
+	        }
+	    });
+	}
+
+	@Override
+	public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {}
+
+	@Override
+	public void popupMenuCanceled(PopupMenuEvent e) {}
+}

+ 8 - 0
src/main/lombok/org/leumasjaffe/event/package-info.java

@@ -0,0 +1,8 @@
+/**
+ * 
+ */
+/**
+ * @author Sam Jaffe
+ *
+ */
+package org.leumasjaffe.event;