Object cloning now possible -- TransferHelper is the current holder of the functionality but it should probably be moved somewhere better

This commit is contained in:
Brendan Robert 2015-05-21 00:14:30 -05:00
parent 71f20dcf1b
commit 13ac184d89
5 changed files with 79 additions and 11 deletions

View File

@ -129,7 +129,6 @@ public class MythosEditor {
return;
}
script.setName(name);
System.out.println("Function title changed! >> " + name);
ApplicationUIController.getController().redrawScripts();
}
}

View File

@ -11,6 +11,13 @@ import javafx.scene.input.DragEvent;
import javafx.scene.input.Dragboard;
import javafx.scene.input.MouseEvent;
import javafx.scene.input.TransferMode;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.util.JAXBSource;
import javax.xml.namespace.QName;
import org.badvision.outlaweditor.data.xml.Script;
import org.badvision.outlaweditor.ui.ToolType;
/**
@ -98,4 +105,17 @@ public class TransferHelper<T> {
});
};
}
public static <U> U cloneObject(U source, Class<U> type, String nodeType) throws JAXBException {
JAXBContext sourceJAXBContext = JAXBContext.newInstance(source.getClass());
Marshaller jaxbMarshaller = sourceJAXBContext.createMarshaller();
// format the XML output
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
JAXBContext targetJAXBContext = JAXBContext.newInstance(source.getClass());
QName qName = new QName("info.source4code.jaxb.model", nodeType);
JAXBElement<U> root = new JAXBElement<>(qName, type, source);
JAXBElement<U> cloneRoot = (JAXBElement<U>) targetJAXBContext.createUnmarshaller().unmarshal(new JAXBSource(sourceJAXBContext, root), type);
return cloneRoot.getValue();
}
}

View File

@ -193,6 +193,10 @@ public class UIAction {
public static void confirm(String message, Runnable yes, Runnable no) {
choose(message, new Choice("Yes", yes), new Choice("No", no));
}
public static void alert(String message) {
choose(message, new Choice("Ok", null));
}
public static void choose(String message, Choice... choices) {
final Stage dialogStage = new Stage();

View File

@ -14,6 +14,7 @@ import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.image.WritableImage;
import javafx.util.Callback;
import javax.xml.bind.JAXBException;
import org.badvision.outlaweditor.Application;
import static org.badvision.outlaweditor.Application.currentPlatform;
import static org.badvision.outlaweditor.Application.gameData;
@ -25,18 +26,21 @@ import org.badvision.outlaweditor.data.TileUtils;
import org.badvision.outlaweditor.data.xml.Map;
import org.badvision.outlaweditor.data.xml.Script;
import org.badvision.outlaweditor.data.xml.Tile;
import org.badvision.outlaweditor.ui.ApplicationUIController;
import org.badvision.outlaweditor.ui.EntitySelectorCell;
import org.badvision.outlaweditor.ui.MapEditorTabController;
import org.badvision.outlaweditor.ui.ToolType;
import org.badvision.outlaweditor.ui.UIAction;
import static org.badvision.outlaweditor.ui.UIAction.confirm;
import static org.badvision.outlaweditor.ui.UIAction.createAndEditScript;
import static org.badvision.outlaweditor.ui.UIAction.editScript;
/**
*
* @author blurry
*/
public class MapEditorTabControllerImpl extends MapEditorTabController {
final TransferHelper<Script> scriptDragDrop = new TransferHelper<>(Script.class);
final TransferHelper<ToolType> toolDragDrop = new TransferHelper<>(ToolType.class);
@ -144,9 +148,46 @@ public class MapEditorTabControllerImpl extends MapEditorTabController {
createAndEditScript();
}
int errorCount = 0;
long gagTimeout = 0;
@Override
public void onMapScriptClonePressed(ActionEvent event) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
Script source = mapScriptsList.getSelectionModel().getSelectedItem();
if (source == null) {
String message = "First select a script and then press Clone";
if (gagTimeout == 0 || gagTimeout < System.currentTimeMillis()) {
gagTimeout = System.currentTimeMillis() + 15000;
errorCount = 1;
} else {
switch (++errorCount) {
case 3:
message = "Seriously, select a script first";
break;
case 4:
message = "By select, I mean move the mouse and click on something";
break;
case 5:
message = "I really can't help you";
break;
case 6:
message = "Bored? Lonely? Have you trolled any YouTube comments lately?";
break;
default:
}
}
UIAction.alert(message);
} else {
try {
Script script = TransferHelper.cloneObject(source, Script.class, "script");
script.setName(source.getName() + " CLONE");
getCurrentEditor().addScript(script);
editScript(script);
} catch (JAXBException ex) {
Logger.getLogger(MapEditorTabControllerImpl.class.getName()).log(Level.SEVERE, null, ex);
UIAction.alert("Error occured when attempting clone operation:\n" + ex.getMessage());
}
}
}
@Override
@ -292,13 +333,13 @@ public class MapEditorTabControllerImpl extends MapEditorTabController {
});
toolDragDrop.registerDragSupport(scriptEraseTool, ToolType.ERASER);
}
@Override
public void rebuildTileSelectors() {
mapSelectTile.getItems().clear();
ToggleGroup tileGroup = new ToggleGroup();
HashMap<String,Menu> submenus = new HashMap<>();
HashMap<String, Menu> submenus = new HashMap<>();
Application.gameData.getTile().stream().forEach((Tile t) -> {
WritableImage img = TileUtils.getImage(t, currentPlatform);
ImageView iv = new ImageView(img);
@ -349,7 +390,7 @@ public class MapEditorTabControllerImpl extends MapEditorTabController {
setText("");
} else {
ImageView visibleIcon = getVisibleIcon(item);
visibleIcon.setOnMouseClicked((e)->{
visibleIcon.setOnMouseClicked((e) -> {
toggleVisibility(visibleIcon, item);
mapScriptsList.getSelectionModel().clearSelection();
});
@ -375,10 +416,10 @@ public class MapEditorTabControllerImpl extends MapEditorTabController {
}
}
}
public static final Image VISIBLE_IMAGE = new Image("images/visible.png");
public static final Image INVISIBLE_IMAGE = new Image("images/not_visible.png");
private ImageView getVisibleIcon(Script script) {
if (getCurrentEditor().isScriptVisible(script)) {
return new ImageView(VISIBLE_IMAGE);
@ -386,9 +427,11 @@ public class MapEditorTabControllerImpl extends MapEditorTabController {
return new ImageView(INVISIBLE_IMAGE);
}
}
private void toggleVisibility(ImageView visibilityIcon, Script script) {
if (script.getName() == null) return;
if (script.getName() == null) {
return;
}
if (getCurrentEditor().isScriptVisible(script)) {
getCurrentEditor().setScriptVisible(script, false);
visibilityIcon.setImage(INVISIBLE_IMAGE);
@ -399,7 +442,7 @@ public class MapEditorTabControllerImpl extends MapEditorTabController {
}
private void sortScripts(Map m) {
m.getScripts().getScript().sort((a,b)-> {
m.getScripts().getScript().sort((a, b) -> {
if (a.getName().equalsIgnoreCase("init")) {
return -1;
} else if (b.getName().equalsIgnoreCase("init")) {

View File

@ -1,3 +1,5 @@
/* global Blockly */
if (typeof Mythos === "undefined") {
// Hook up the rename function to notify the java editor when changes occur
Blockly.Procedures.rename_old = Blockly.Procedures.rename;