I have a JSpinner with a SpinnerDateModel of "HH:mm" format. I want the user to be (for example) able to copy a date in "yyyy-MM-dd HH:mm:ss.SSS" from a table (or any other source) and paste it into the JSpinner - the HH:mm part only. Such full date string is normally invalid for the component but I still want to try the pasted string and get the desired info from from it (if it's there)... I thought that my validation method should look something like below but I don't know how to change the paste() behaviour so that I can add the validation and change of the pasted text...
private String validateAndReturnCorrected(String pastedText) {
DateFormat hoursMinutesFormat = new SimpleDateFormat("HH:mm");
try {
// trying to paste a full date string?
DateFormat fullDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
Date date = fullDateFormat.parse(pastedText);
return hoursMinutesFormat.format(date);
} catch (ParseException ex) {
}
// trying to paste hour and minutes?
try {
Date date = hoursMinutesFormat.parse(pastedText);
return hoursMinutesFormat.format(date);
} catch (ParseException ex1) {
}
// trying to paste date in HH:mm:ss format?
try {
DateFormat hoursMinutesSecondsFormat = new SimpleDateFormat("HH:mm:ss");
Date date = hoursMinutesSecondsFormat.parse(pastedText);
return hoursMinutesSecondsFormat.format(date);
} catch (ParseException ex2) {
}
// trying to paste date in HH:mm:ss.SSS format?
try {
DateFormat hoursMinutesSecondsMilisecondsFormat = new SimpleDateFormat("HH:mm:ss.SSS");
Date date = hoursMinutesSecondsMilisecondsFormat.parse(pastedText);
return hoursMinutesFormat.format(date);
} catch (ParseException ex3) {
}
// unable to correct the string...
return "";
}
UPDATE
Changing the googled question I found the following two sites which led me to get the problem solved:
- http://www.javapractices.com/topic/TopicAction.do?Id=82
- https://stackoverflow.com/a/25276224/5653483
So the solution looks something like this:
class ProxyAction extends TextAction implements ClipboardOwner {
private TextAction action;
public ProxyAction(TextAction action) {
super(action.toString());
this.action = action;
}
@Override
public void actionPerformed(ActionEvent e) {
String cbc=getClipboardContents();
setClipboardContents(validateAndReturnCorrected(cbc));
action.actionPerformed(e);
setClipboardContents(cbc);
System.out.println("Paste Occured...............................................................");
}
// here goes the validateAndReturnCorrected method
public String getClipboardContents() {
String result = "";
try {
result = (String) Toolkit.getDefaultToolkit().getSystemClipboard().getData(DataFlavor.stringFlavor);
} catch (UnsupportedFlavorException | IOException ex) {
ex.printStackTrace();
}
return result;
}
public void setClipboardContents(String aString) {
StringSelection stringSelection = new StringSelection(aString);
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(stringSelection, this);
}
@Override
public void lostOwnership(Clipboard clipboard, Transferable contents) {
}
}