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.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException; import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.File; import java.io.File;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import me.goudham.strategy.CopyStrategy;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -17,11 +20,17 @@ abstract class ClipboardListener {
Logger logger; Logger logger;
EventManager eventManager; EventManager eventManager;
ClipboardUtils clipboardUtils; ClipboardUtils clipboardUtils;
Map<Class<?>, Class<? extends CopyStrategy>> supportedStrategies;
Map<Class<? extends CopyStrategy>, CopyStrategy> strategies;
private boolean imageMonitored = true; private boolean imageMonitored = true;
private boolean textMonitored = true; private boolean textMonitored = true;
private boolean fileMonitored = true; private boolean fileMonitored = true;
ClipboardListener() { ClipboardListener() {
supportedStrategies = new HashMap<>();
strategies = new HashMap<>();
clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
logger = LoggerFactory.getLogger(getClass()); logger = LoggerFactory.getLogger(getClass());
eventManager = new EventManager(); eventManager = new EventManager();
@ -52,24 +61,20 @@ abstract class ClipboardListener {
*/ */
abstract void stopListening(); abstract void stopListening();
/** abstract void insert(Object data);
* 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(Image imageContent); abstract void insertAndNotify(Object data);
abstract void insert(List<File> fileContent); void addSupport(Class<?> clazz, CopyStrategy copyStrategy) {
supportedStrategies.put(clazz, copyStrategy.getClass());
abstract void insertAndNotify(String stringContent); strategies.put(copyStrategy.getClass(), copyStrategy);
}
abstract void insertAndNotify(Image imageContent);
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() { void toggleTextMonitored() {
this.textMonitored = !textMonitored; this.textMonitored = !textMonitored;

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

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

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