Start migrating to Strategy design pattern

MYC-Refactor
Hammy 3 years ago
parent a1156a4cef
commit 9228bb14b1

@ -7,7 +7,10 @@ import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import me.goudham.strategy.CopyStrategy;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -17,11 +20,17 @@ abstract class ClipboardListener {
Logger logger;
EventManager eventManager;
ClipboardUtils clipboardUtils;
Map<Class<?>, Class<? extends CopyStrategy>> supportedStrategies;
Map<Class<? extends CopyStrategy>, CopyStrategy> strategies;
private boolean imageMonitored = true;
private boolean textMonitored = true;
private boolean fileMonitored = true;
ClipboardListener() {
supportedStrategies = new HashMap<>();
strategies = new HashMap<>();
clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
logger = LoggerFactory.getLogger(getClass());
eventManager = new EventManager();
@ -52,24 +61,20 @@ abstract class ClipboardListener {
*/
abstract void stopListening();
/**
* Insert the given {@link String} into the system clipboard
*
* @param stringContent The given {@link String} to insert
* @see WindowsOrUnixClipboardListener#insert(String)
* @see MacClipboardListener#insert(String)
*/
abstract void insert(String stringContent);
abstract void insert(Object data);
abstract void insert(Image imageContent);
abstract void insertAndNotify(Object data);
abstract void insert(List<File> fileContent);
abstract void insertAndNotify(String stringContent);
abstract void insertAndNotify(Image imageContent);
void addSupport(Class<?> clazz, CopyStrategy copyStrategy) {
supportedStrategies.put(clazz, copyStrategy.getClass());
strategies.put(copyStrategy.getClass(), copyStrategy);
}
abstract void insertAndNotify(List<File> fileContent);
void removeSupport(Class<?> clazz) {
Class<? extends CopyStrategy> supportedClass = supportedStrategies.get(clazz);
strategies.remove(supportedClass);
supportedStrategies.remove(clazz);
}
void toggleTextMonitored() {
this.textMonitored = !textMonitored;

@ -9,6 +9,7 @@ import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import me.goudham.domain.OldClipboardContent;
import me.goudham.strategy.CopyStrategy;
import static java.lang.Thread.sleep;
import static me.goudham.Contents.FILELIST;
@ -116,52 +117,66 @@ class MacClipboardListener extends ClipboardListener implements Runnable {
}
}
@Override
void insert(String stringContent) {
insertAndNotify(stringContent);
}
@Override
void insert(Image imageContent) {
insertAndNotify(imageContent);
}
// @Override
// void insert(String stringContent) {
// insertAndNotify(stringContent);
// }
//
// @Override
// void insert(Image imageContent) {
// insertAndNotify(imageContent);
// }
//
// @Override
// void insert(List<File> fileContent) {
// insertAndNotify(fileContent);
// }
//
// @Override
// void insertAndNotify(String stringContent) {
// try {
// sleep(200);
// } catch (InterruptedException ie) {
// logger.error("Exception Thrown As Thread Cannot Sleep", ie);
// }
//
// clipboard.setContents(new StringSelection(stringContent), null);
// }
//
// @Override
// void insertAndNotify(Image imageContent) {
// try {
// sleep(200);
// } catch (InterruptedException ie) {
// logger.error("Exception Thrown As Thread Cannot Sleep", ie);
// }
//
// clipboard.setContents(new TransferableImage(imageContent), null);
// }
//
// @Override
// void insertAndNotify(List<File> fileContent) {
// try {
// sleep(200);
// } catch (InterruptedException ie) {
// logger.error("Exception Thrown As Thread Cannot Sleep", ie);
// }
//
// clipboard.setContents(new TransferableFileList(fileContent), null);
// }
@Override
void insert(List<File> fileContent) {
insertAndNotify(fileContent);
void insert(Object data) {
Class<? extends CopyStrategy> supportedClass = supportedStrategies.get(data.getClass());
CopyStrategy supportedStrategy = strategies.get(supportedClass);
supportedStrategy.macInsert(clipboard, data);
}
@Override
void insertAndNotify(String stringContent) {
try {
sleep(200);
} catch (InterruptedException ie) {
logger.error("Exception Thrown As Thread Cannot Sleep", ie);
}
clipboard.setContents(new StringSelection(stringContent), null);
}
@Override
void insertAndNotify(Image imageContent) {
try {
sleep(200);
} catch (InterruptedException ie) {
logger.error("Exception Thrown As Thread Cannot Sleep", ie);
}
clipboard.setContents(new TransferableImage(imageContent), null);
}
@Override
void insertAndNotify(List<File> fileContent) {
try {
sleep(200);
} catch (InterruptedException ie) {
logger.error("Exception Thrown As Thread Cannot Sleep", ie);
}
clipboard.setContents(new TransferableFileList(fileContent), null);
void insertAndNotify(Object data) {
Class<? extends CopyStrategy> supportedClass = supportedStrategies.get(data.getClass());
CopyStrategy supportedStrategy = strategies.get(supportedClass);
supportedStrategy.macInsertAndNotify(clipboard, data);
}
@Override

@ -1,12 +1,10 @@
package me.goudham;
import java.awt.Image;
import java.io.File;
import java.util.List;
import me.goudham.event.FileEvent;
import me.goudham.event.ImageEvent;
import me.goudham.event.TextEvent;
import me.goudham.exception.UnsupportedSystemException;
import me.goudham.strategy.CopyStrategy;
import org.jetbrains.annotations.NotNull;
/**
@ -68,28 +66,12 @@ public class MyClipboard {
clipboardListener.stopListening();
}
public void insert(String stringContent) {
clipboardListener.insert(stringContent);
public void insert(Object data) {
clipboardListener.insert(data);
}
public void insert(Image imageContent) {
clipboardListener.insert(imageContent);
}
public void insert(List<File> fileContent) {
clipboardListener.insert(fileContent);
}
public void insertAndNotify(String stringContent) {
clipboardListener.insertAndNotify(stringContent);
}
public void insertAndNotify(Image imageContent) {
clipboardListener.insertAndNotify(imageContent);
}
public void insertAndNotify(List<File> fileContent) {
clipboardListener.insertAndNotify(fileContent);
public void insertAndNotify(Object data) {
clipboardListener.insertAndNotify(data);
}
/**
@ -152,6 +134,14 @@ public class MyClipboard {
clipboardListener.getEventManager().removeEventListener(fileEvent);
}
void addSupport(Class<?> clazz, CopyStrategy copyStrategy) {
clipboardListener.addSupport(clazz, copyStrategy);
}
void removeSupport(Class<?> clazz) {
clipboardListener.removeSupport(clazz);
}
public void toggleTextMonitored() {
clipboardListener.toggleTextMonitored();
}

@ -10,6 +10,7 @@ import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import me.goudham.domain.OldClipboardContent;
import me.goudham.strategy.CopyStrategy;
import static java.lang.Thread.currentThread;
import static java.lang.Thread.sleep;
@ -103,32 +104,82 @@ class WindowsOrUnixClipboardListener extends ClipboardListener implements Runnab
}
}
@Override
void insert(String stringContent) {
try {
sleep(200);
} catch (InterruptedException ie) {
logger.error("Exception Thrown As Thread Cannot Sleep", ie);
}
try {
clipboard.setContents(new StringSelection(stringContent), this);
} catch (IllegalStateException ise) {
logger.error("Exception Thrown As Clipboard Cannot Be Accessed", ise);
executorService.submit(this);
}
}
// @Override
// void insert(String stringContent) {
// try {
// sleep(200);
// } catch (InterruptedException ie) {
// logger.error("Exception Thrown As Thread Cannot Sleep", ie);
// }
//
// try {
// clipboard.setContents(new StringSelection(stringContent), this);
// } catch (IllegalStateException ise) {
// logger.error("Exception Thrown As Clipboard Cannot Be Accessed", ise);
// executorService.submit(this);
// }
// }
//
// @Override
// void insert(Image imageContent) {
// try {
// sleep(200);
// } catch (InterruptedException ie) {
// logger.error("Exception Thrown As Thread Cannot Sleep", ie);
// }
//
// try {
// clipboard.setContents(new TransferableImage(imageContent), this);
// } catch (IllegalStateException ise) {
// logger.error("Exception Thrown As Clipboard Cannot Be Accessed", ise);
// executorService.submit(this);
// }
// }
//
// @Override
// void insert(List<File> fileContent) {
// try {
// sleep(200);
// } catch (InterruptedException ie) {
// logger.error("Exception Thrown As Thread Cannot Sleep", ie);
// }
//
// try {
// clipboard.setContents(new TransferableFileList(fileContent), this);
// } catch (IllegalStateException ise) {
// logger.error("Exception Thrown As Clipboard Cannot Be Accessed", ise);
// executorService.submit(this);
// }
// }
//
// @Override
// void insertAndNotify(String stringContent) {
// Transferable currentClipboardContents = clipboard.getContents(this);
// insert(stringContent);
// lostOwnership(clipboard, currentClipboardContents);
// }
//
// @Override
// void insertAndNotify(Image imageContent) {
// Transferable currentClipboardContents = clipboard.getContents(this);
// insert(imageContent);
// lostOwnership(clipboard, currentClipboardContents);
// }
//
// @Override
// void insertAndNotify(List<File> fileContent) {
// Transferable currentClipboardContents = clipboard.getContents(this);
// insert(fileContent);
// lostOwnership(clipboard, currentClipboardContents);
// }
@Override
void insert(Image imageContent) {
try {
sleep(200);
} catch (InterruptedException ie) {
logger.error("Exception Thrown As Thread Cannot Sleep", ie);
}
void insert(Object data) {
Class<? extends CopyStrategy> supportedClass = supportedStrategies.get(data.getClass());
CopyStrategy supportedStrategy = strategies.get(supportedClass);
try {
clipboard.setContents(new TransferableImage(imageContent), this);
supportedStrategy.windowsOrUnixInsert(clipboard, this, data);
} catch (IllegalStateException ise) {
logger.error("Exception Thrown As Clipboard Cannot Be Accessed", ise);
executorService.submit(this);
@ -136,42 +187,18 @@ class WindowsOrUnixClipboardListener extends ClipboardListener implements Runnab
}
@Override
void insert(List<File> fileContent) {
try {
sleep(200);
} catch (InterruptedException ie) {
logger.error("Exception Thrown As Thread Cannot Sleep", ie);
}
void insertAndNotify(Object data) {
Class<? extends CopyStrategy> supportedClass = supportedStrategies.get(data.getClass());
CopyStrategy supportedStrategy = strategies.get(supportedClass);
try {
clipboard.setContents(new TransferableFileList(fileContent), this);
supportedStrategy.windowsOrUnixInsertAndNotify(clipboard, this, data);
} catch (IllegalStateException ise) {
logger.error("Exception Thrown As Clipboard Cannot Be Accessed", ise);
executorService.submit(this);
}
}
@Override
void insertAndNotify(String stringContent) {
Transferable currentClipboardContents = clipboard.getContents(this);
insert(stringContent);
lostOwnership(clipboard, currentClipboardContents);
}
@Override
void insertAndNotify(Image imageContent) {
Transferable currentClipboardContents = clipboard.getContents(this);
insert(imageContent);
lostOwnership(clipboard, currentClipboardContents);
}
@Override
void insertAndNotify(List<File> fileContent) {
Transferable currentClipboardContents = clipboard.getContents(this);
insert(fileContent);
lostOwnership(clipboard, currentClipboardContents);
}
@Override
public void run() {
try {

@ -0,0 +1,11 @@
package me.goudham.strategy;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.ClipboardOwner;
public interface CopyStrategy {
void windowsOrUnixInsert(Clipboard clipboard, ClipboardOwner clipboardOwner, Object data);
void windowsOrUnixInsertAndNotify(Clipboard clipboard, ClipboardOwner clipboardOwner, Object data);
void macInsert(Clipboard clipboard, Object data);
void macInsertAndNotify(Clipboard clipboard, Object data);
}

@ -0,0 +1,48 @@
package me.goudham.strategy;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.ClipboardOwner;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static java.lang.Thread.sleep;
public class TextCopyStrategy implements CopyStrategy {
Logger logger = LoggerFactory.getLogger(TextCopyStrategy.class);
@Override
public void windowsOrUnixInsert(Clipboard clipboard, ClipboardOwner clipboardOwner, Object data) {
try {
sleep(200);
} catch (InterruptedException ie) {
logger.error("Exception Thrown As Thread Cannot Sleep", ie);
}
clipboard.setContents(new StringSelection((String) data), clipboardOwner);
}
@Override
public void windowsOrUnixInsertAndNotify(Clipboard clipboard, ClipboardOwner clipboardOwner, Object data) {
Transferable currentClipboardContents = clipboard.getContents(this);
windowsOrUnixInsert(clipboard, clipboardOwner, data);
clipboardOwner.lostOwnership(clipboard, currentClipboardContents);
}
@Override
public void macInsert(Clipboard clipboard, Object data) {
macInsertAndNotify(clipboard, data);
}
@Override
public void macInsertAndNotify(Clipboard clipboard, Object data) {
try {
sleep(200);
} catch (InterruptedException ie) {
logger.error("Exception Thrown As Thread Cannot Sleep", ie);
}
clipboard.setContents(new StringSelection((String) data), null);
}
}
Loading…
Cancel
Save