This commit is contained in:
Martin Haye 2016-07-06 07:46:59 -07:00
commit 53723bf07a
13 changed files with 458 additions and 154 deletions

View File

@ -182,5 +182,15 @@
<artifactId>org.apache.felix.scr</artifactId>
<version>2.0.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.14</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.14</version>
</dependency>
</dependencies>
</project>

View File

@ -49,6 +49,7 @@ import org.badvision.outlaweditor.data.xml.Map;
import org.badvision.outlaweditor.data.xml.Script;
import org.badvision.outlaweditor.data.xml.Scripts;
import org.badvision.outlaweditor.data.xml.Tile;
import org.badvision.outlaweditor.ui.TileSelectModal;
import org.badvision.outlaweditor.ui.ToolType;
import org.badvision.outlaweditor.ui.UIAction;
@ -127,7 +128,7 @@ public class MapEditor extends Editor<Map, MapEditor.DrawMode> implements EventH
category = currentTile.getCategory();
}
if (this.equals(ApplicationState.getInstance().getController().getVisibleEditor())) {
UIAction.showTileSelectModal(anchorPane, category, this::setCurrentTile);
TileSelectModal.showTileSelectModal(anchorPane, category, this::setCurrentTile);
}
}
}

View File

@ -53,9 +53,6 @@ public class SheetEditor {
throw new RuntimeException(exception);
}
primaryStage.setOnCloseRequest((final WindowEvent t) -> {
t.consume();
});
primaryStage.show();
}

View File

@ -7,12 +7,15 @@
* ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
package org.badvision.outlaweditor.apple;
import java.util.Arrays;
import java.util.HashMap;
import javafx.scene.Group;
import javafx.scene.control.Menu;
import javafx.scene.image.WritableImage;
import javafx.scene.input.Clipboard;
import javafx.scene.input.DataFormat;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Pane;
@ -21,6 +24,7 @@ import javafx.scene.shape.Rectangle;
import javafx.scene.shape.Shape;
import org.badvision.outlaweditor.api.Platform;
import org.badvision.outlaweditor.TileEditor;
import org.badvision.outlaweditor.data.DataUtilities;
import org.badvision.outlaweditor.data.TileUtils;
import org.badvision.outlaweditor.data.xml.Tile;
@ -215,8 +219,8 @@ public class AppleTileEditor extends TileEditor {
} else {
grid[x][y].setStroke(
isHiBit
? (x % 2 == 1) ? Color.CHOCOLATE : Color.CORNFLOWERBLUE
: (x % 2 == 1) ? Color.GREEN : Color.VIOLET);
? (x % 2 == 1) ? Color.CHOCOLATE : Color.CORNFLOWERBLUE
: (x % 2 == 1) ? Color.GREEN : Color.VIOLET);
}
}
}
@ -224,7 +228,11 @@ public class AppleTileEditor extends TileEditor {
@Override
public void copy() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
java.util.Map<DataFormat, Object> clip = new HashMap<>();
clip.put(DataFormat.IMAGE, TileUtils.getImage(getEntity(), Platform.AppleII));
clip.put(DataFormat.PLAIN_TEXT, DataUtilities.hexDump(TileUtils.getPlatformData(getEntity(), Platform.AppleII)));
clip.put(DataFormat.HTML, buildHtmlExport());
Clipboard.getSystemClipboard().setContent(clip);
}
@Override
@ -248,4 +256,43 @@ public class AppleTileEditor extends TileEditor {
TileUtils.redrawTile(getEntity());
}
}
private String buildHtmlExport() {
StringBuilder export = new StringBuilder("<table>");
export.append("<tr><th>H</th><th>0</th><th>1</th><th>2</th><th>3</th><th>4</th><th>5</th><th>6</th>")
.append("<th>H</th><th>0</th><th>1</th><th>2</th><th>3</th><th>4</th><th>5</th><th>6</th>")
.append("<th>B1</th><th>B2</th></tr>");
byte[] data = TileUtils.getPlatformData(getEntity(), Platform.AppleII);
for (int row = 0; row < 16; row++) {
export.append("<tr>");
for (int col = 0; col < 2; col++) {
int b = data[row * 2 + col] & 0x0ff;
export.append("<td>")
.append(b>>7)
.append("</td>");
for (int bit = 0; bit < 7; bit++) {
export.append("<td style='background:#")
.append(getHexColor((Color) grid[bit + col * 7][row].getFill()))
.append("'>")
.append(b&1)
.append("</td>");
b >>=1;
}
}
export.append("<td>")
.append(DataUtilities.getHexValueFromByte(data[row*2]))
.append("</td><td>")
.append(DataUtilities.getHexValueFromByte(data[row*2+1]))
.append("</td>");
export.append("</tr>");
}
export.append("</table>");
return export.toString();
}
private String getHexColor(Color color) {
return DataUtilities.getHexValue((int) (color.getRed() * 255))
+ DataUtilities.getHexValue((int) (color.getGreen() * 255))
+ DataUtilities.getHexValue((int) (color.getBlue() * 255));
}
}

View File

@ -9,13 +9,30 @@
*/
package org.badvision.outlaweditor.data;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.xml.namespace.QName;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.badvision.outlaweditor.api.ApplicationState;
import org.badvision.outlaweditor.data.xml.Block;
import org.badvision.outlaweditor.data.xml.Field;
@ -24,6 +41,7 @@ import org.badvision.outlaweditor.data.xml.Map;
import org.badvision.outlaweditor.data.xml.NamedEntity;
import org.badvision.outlaweditor.data.xml.Scope;
import org.badvision.outlaweditor.data.xml.Script;
import org.badvision.outlaweditor.ui.UIAction;
public class DataUtilities {
@ -122,6 +140,106 @@ public class DataUtilities {
}
}
public static String getValue(java.util.Map<QName, String> map, String name) {
return map.entrySet().stream()
.filter((e) -> e.getKey().getLocalPart().equals(name))
.map(e -> e.getValue())
.findFirst().orElse(null);
}
public static void setValue(java.util.Map<QName, String> map, String name, String newValue) {
Optional<java.util.Map.Entry<QName, String>> attr = map.entrySet().stream()
.filter((e) -> e.getKey().getLocalPart().equals(name)).findFirst();
if (attr.isPresent()) {
attr.get().setValue(newValue);
} else {
map.put(new QName(name), newValue);
}
}
public static List<List<String>> readFromFile(File file) {
try {
if (file.getName().toLowerCase().endsWith("txt") ||
file.getName().toLowerCase().endsWith("tsv")) {
return readTextFile(file);
} else if (file.getName().toLowerCase().endsWith("xls")) {
return readLegacyExcel(file);
} else if (file.getName().toLowerCase().endsWith("xlsx")) {
return readExcel(file);
}
} catch (IOException | InvalidFormatException ex) {
Logger.getLogger(DataUtilities.class.getName()).log(Level.SEVERE, null, ex);
}
UIAction.alert("Couldn't figure out how to import file "+file.getName());
return Collections.EMPTY_LIST;
}
public static List<List<String>> readTextFile(File file) throws FileNotFoundException {
BufferedReader reader = new BufferedReader(new FileReader(file));
return reader.lines().map(line -> Arrays.asList(line.split("\\t"))).collect(Collectors.toList());
}
public static List<List<String>> readLegacyExcel(File file) throws FileNotFoundException, IOException {
return readSheet(new HSSFWorkbook(new FileInputStream(file)));
}
public static List<List<String>> readExcel(File file) throws FileNotFoundException, IOException, InvalidFormatException {
return readSheet(new XSSFWorkbook(file));
}
public static List<List<String>> readSheet(Workbook workbook) {
Sheet sheet = workbook.getSheetAt(0);
List<List<String>> data = new ArrayList<>();
sheet.forEach(row -> {
List<String> rowData = new ArrayList<>();
row.forEach(cell -> {
String col = getStringValueFromCell(cell);
rowData.add(col);
});
data.add(rowData);
});
return data;
}
public static String getStringValueFromCell(Cell cell) {
switch (cell.getCellType()) {
case Cell.CELL_TYPE_BOOLEAN:
return Boolean.toString(cell.getBooleanCellValue());
case Cell.CELL_TYPE_BLANK:
return null;
case Cell.CELL_TYPE_NUMERIC:
return Double.toString(cell.getNumericCellValue());
case Cell.CELL_TYPE_STRING:
return cell.getStringCellValue();
default:
return "???";
}
}
public static String hexDump(byte[] data) {
StringBuilder dump = new StringBuilder();
for (int i=0; i < data.length; i++) {
if (i > 0) {
dump.append(",");
}
dump.append(getHexValueFromByte(data[i]));
}
return dump.toString();
}
public static String getHexValueFromByte(byte val) {
return getHexValue(val & 0x0ff);
}
public static String getHexValue(int val) {
if (val < 16) {
return "0" + Integer.toHexString(val);
} else {
return Integer.toHexString(val);
}
}
//------------------------------ String comparators
/**
* Rank two strings similarity in terms of distance The lower the number,

View File

@ -14,13 +14,13 @@
package org.badvision.outlaweditor.ui;
import java.net.URL;
import java.util.Map;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import org.badvision.outlaweditor.data.xml.Rows.Row;
import org.badvision.outlaweditor.data.xml.UserType;
@ -40,12 +40,18 @@ public abstract class SheetEditorController implements Initializable {
@FXML
protected TableView<Row> table;
@FXML
protected TextField sheetNameField;
@FXML
abstract public void addColumnAction(ActionEvent event);
@FXML
abstract public void addRowAction(ActionEvent event);
@FXML
abstract public void doImport(ActionEvent event);
@FXML
protected void initialize() {
assert addColumn != null : "fx:id=\"addColumn\" was not injected: check your FXML file 'SheetEditor.fxml'.";

View File

@ -0,0 +1,139 @@
/*
* Copyright 2016 org.badvision.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.badvision.outlaweditor.ui;
import java.util.List;
import java.util.stream.Collectors;
import javafx.animation.ScaleTransition;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.effect.BlurType;
import javafx.scene.effect.DropShadow;
import javafx.scene.image.ImageView;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.CornerRadii;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.util.Callback;
import javafx.util.Duration;
import org.badvision.outlaweditor.api.ApplicationState;
import org.badvision.outlaweditor.data.TileUtils;
import org.badvision.outlaweditor.data.xml.Tile;
import static org.badvision.outlaweditor.ui.UIAction.fadeOut;
/**
* Create and manage a tile selection modal
* @author blurry
*/
public class TileSelectModal {
public static final int GRID_SPACING = 7;
public static final int MAX_TILES_PER_ROW = 16;
public static AnchorPane currentTileSelector;
public static void showTileSelectModal(Pane anchorPane, String category, Callback<Tile, ?> callback) {
if (currentTileSelector != null) {
return;
}
currentTileSelector = new AnchorPane();
int TILE_WIDTH = ApplicationState.getInstance().getCurrentPlatform().tileRenderer.getWidth();
int TILE_HEIGHT = ApplicationState.getInstance().getCurrentPlatform().tileRenderer.getHeight();
List<Tile> tiles = ApplicationState.getInstance().getGameData().getTile().stream().filter((Tile t) -> {
return category == null || t.getCategory().equals(category);
}).collect(Collectors.toList());
int tilesPerRow = (int) Math.min(tiles.size(), Math.min(MAX_TILES_PER_ROW, anchorPane.getWidth() / (TILE_WIDTH + GRID_SPACING)));
int numRows = (tiles.size() + tilesPerRow - 1) / tilesPerRow;
int prefWidth = tilesPerRow * (TILE_WIDTH + GRID_SPACING) + GRID_SPACING;
currentTileSelector.setPrefWidth(prefWidth);
currentTileSelector.setPrefHeight(Math.min(numRows * (TILE_HEIGHT + GRID_SPACING) + GRID_SPACING, prefWidth));
for (int i = 0; i < tiles.size(); i++) {
final Tile tile = tiles.get(i);
ImageView tileIcon = new ImageView(TileUtils.getImage(tile, ApplicationState.getInstance().getCurrentPlatform()));
currentTileSelector.getChildren().add(tileIcon);
tileIcon.setOnMouseClicked((e) -> {
e.consume();
callback.call(tile);
closeCurrentTileSelector();
});
tileIcon.setOnMouseEntered((e) -> {
tileIcon.setEffect(new DropShadow(BlurType.GAUSSIAN, Color.CORNSILK, 5.0, 0.5, 0, 0));
ScaleTransition st = new ScaleTransition(Duration.millis(150), tileIcon);
st.setAutoReverse(false);
st.setToX(1.25);
st.setToY(1.25);
st.play();
});
tileIcon.setOnMouseExited((e) -> {
tileIcon.setEffect(null);
ScaleTransition st = new ScaleTransition(Duration.millis(150), tileIcon);
st.setAutoReverse(false);
st.setToX(1);
st.setToY(1);
st.play();
});
tileIcon.setLayoutX(GRID_SPACING + (i % tilesPerRow) * (TILE_WIDTH + GRID_SPACING));
tileIcon.setLayoutY(GRID_SPACING + (i / tilesPerRow) * (TILE_HEIGHT + GRID_SPACING));
}
currentTileSelector.setLayoutX((anchorPane.getWidth() - currentTileSelector.getPrefWidth()) / 2);
currentTileSelector.setLayoutY((anchorPane.getHeight() - currentTileSelector.getPrefHeight()) / 2);
currentTileSelector.setBackground(
new Background(
new BackgroundFill(
new Color(0.7, 0.7, 0.9, 0.75),
new CornerRadii(10.0),
null)));
currentTileSelector.setEffect(new DropShadow(5.0, 1.0, 1.0, Color.BLACK));
anchorPane.getChildren().add(currentTileSelector);
ApplicationState.getInstance().getPrimaryStage().getScene().addEventHandler(KeyEvent.KEY_PRESSED, cancelTileSelectKeyHandler);
ApplicationState.getInstance().getPrimaryStage().getScene().addEventFilter(MouseEvent.MOUSE_PRESSED, cancelTileSelectMouseHandler);
}
public static void closeCurrentTileSelector() {
ApplicationState.getInstance().getPrimaryStage().getScene().removeEventHandler(KeyEvent.KEY_PRESSED, cancelTileSelectKeyHandler);
ApplicationState.getInstance().getPrimaryStage().getScene().removeEventFilter(MouseEvent.MOUSE_PRESSED, cancelTileSelectMouseHandler);
fadeOut(currentTileSelector, (ActionEvent ev) -> {
if (currentTileSelector != null) {
Pane parent = (Pane) currentTileSelector.getParent();
parent.getChildren().remove(currentTileSelector);
currentTileSelector = null;
}
});
}
private static final EventHandler<MouseEvent> cancelTileSelectMouseHandler = (MouseEvent e) -> {
if (!(e.getSource() instanceof ImageView)) {
e.consume();
}
closeCurrentTileSelector();
};
private static final EventHandler<KeyEvent> cancelTileSelectKeyHandler = (KeyEvent e) -> {
if (e.getCode() == KeyCode.ESCAPE) {
closeCurrentTileSelector();
}
};
private TileSelectModal() {
}
}

View File

@ -19,9 +19,7 @@ import java.util.Map;
import java.util.Optional;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import javafx.animation.FadeTransition;
import javafx.animation.ScaleTransition;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
@ -39,22 +37,12 @@ import javafx.scene.control.TableColumn;
import javafx.scene.control.TextInputDialog;
import javafx.scene.control.cell.ComboBoxTableCell;
import javafx.scene.control.cell.TextFieldTableCell;
import javafx.scene.effect.BlurType;
import javafx.scene.effect.DropShadow;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.image.WritableImage;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.CornerRadii;
import javafx.scene.layout.HBoxBuilder;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBoxBuilder;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.stage.Modality;
import javafx.stage.Stage;
@ -69,14 +57,12 @@ import org.badvision.outlaweditor.SheetEditor;
import org.badvision.outlaweditor.api.ApplicationState;
import org.badvision.outlaweditor.apple.ImageDitherEngine;
import org.badvision.outlaweditor.data.DataUtilities;
import org.badvision.outlaweditor.data.TileUtils;
import org.badvision.outlaweditor.data.TilesetUtils;
import org.badvision.outlaweditor.data.xml.GameData;
import org.badvision.outlaweditor.data.xml.Global;
import org.badvision.outlaweditor.data.xml.Scope;
import org.badvision.outlaweditor.data.xml.Script;
import org.badvision.outlaweditor.data.xml.Sheet;
import org.badvision.outlaweditor.data.xml.Tile;
import org.badvision.outlaweditor.data.xml.UserType;
import org.badvision.outlaweditor.data.xml.Variable;
import org.badvision.outlaweditor.data.xml.Variables;
@ -396,96 +382,6 @@ public class UIAction {
}
}
public static final int GRID_SPACING = 7;
public static final int MAX_TILES_PER_ROW = 16;
public static AnchorPane currentTileSelector;
public static void showTileSelectModal(Pane anchorPane, String category, Callback<Tile, ?> callback) {
if (currentTileSelector != null) {
return;
}
currentTileSelector = new AnchorPane();
int TILE_WIDTH = ApplicationState.getInstance().getCurrentPlatform().tileRenderer.getWidth();
int TILE_HEIGHT = ApplicationState.getInstance().getCurrentPlatform().tileRenderer.getHeight();
List<Tile> tiles = ApplicationState.getInstance().getGameData().getTile().stream().filter((Tile t) -> {
return category == null || t.getCategory().equals(category);
}).collect(Collectors.toList());
int tilesPerRow = (int) Math.min(tiles.size(), Math.min(MAX_TILES_PER_ROW, anchorPane.getWidth() / (TILE_WIDTH + GRID_SPACING)));
int numRows = (tiles.size() + tilesPerRow - 1) / tilesPerRow;
int prefWidth = tilesPerRow * (TILE_WIDTH + GRID_SPACING) + GRID_SPACING;
currentTileSelector.setPrefWidth(prefWidth);
currentTileSelector.setPrefHeight(Math.min(numRows * (TILE_HEIGHT + GRID_SPACING) + GRID_SPACING, prefWidth));
for (int i = 0; i < tiles.size(); i++) {
final Tile tile = tiles.get(i);
ImageView tileIcon = new ImageView(TileUtils.getImage(tile, ApplicationState.getInstance().getCurrentPlatform()));
currentTileSelector.getChildren().add(tileIcon);
tileIcon.setOnMouseClicked((e) -> {
e.consume();
callback.call(tile);
closeCurrentTileSelector();
});
tileIcon.setOnMouseEntered((e) -> {
tileIcon.setEffect(new DropShadow(BlurType.GAUSSIAN, Color.CORNSILK, 5.0, 0.5, 0, 0));
ScaleTransition st = new ScaleTransition(Duration.millis(150), tileIcon);
st.setAutoReverse(false);
st.setToX(1.25);
st.setToY(1.25);
st.play();
});
tileIcon.setOnMouseExited((e) -> {
tileIcon.setEffect(null);
ScaleTransition st = new ScaleTransition(Duration.millis(150), tileIcon);
st.setAutoReverse(false);
st.setToX(1);
st.setToY(1);
st.play();
});
tileIcon.setLayoutX(GRID_SPACING + (i % tilesPerRow) * (TILE_WIDTH + GRID_SPACING));
tileIcon.setLayoutY(GRID_SPACING + (i / tilesPerRow) * (TILE_HEIGHT + GRID_SPACING));
}
currentTileSelector.setLayoutX((anchorPane.getWidth() - currentTileSelector.getPrefWidth()) / 2);
currentTileSelector.setLayoutY((anchorPane.getHeight() - currentTileSelector.getPrefHeight()) / 2);
currentTileSelector.setBackground(
new Background(
new BackgroundFill(
new Color(0.7, 0.7, 0.9, 0.75),
new CornerRadii(10.0),
null)));
currentTileSelector.setEffect(new DropShadow(5.0, 1.0, 1.0, Color.BLACK));
anchorPane.getChildren().add(currentTileSelector);
ApplicationState.getInstance().getPrimaryStage().getScene().addEventHandler(KeyEvent.KEY_PRESSED, cancelTileSelectKeyHandler);
ApplicationState.getInstance().getPrimaryStage().getScene().addEventFilter(MouseEvent.MOUSE_PRESSED, cancelTileSelectMouseHandler);
}
private static final EventHandler<MouseEvent> cancelTileSelectMouseHandler = (MouseEvent e) -> {
if (!(e.getSource() instanceof ImageView)) {
e.consume();
}
closeCurrentTileSelector();
};
private static final EventHandler<KeyEvent> cancelTileSelectKeyHandler = (KeyEvent e) -> {
if (e.getCode() == KeyCode.ESCAPE) {
closeCurrentTileSelector();
}
};
public static void closeCurrentTileSelector() {
ApplicationState.getInstance().getPrimaryStage().getScene().removeEventHandler(KeyEvent.KEY_PRESSED, cancelTileSelectKeyHandler);
ApplicationState.getInstance().getPrimaryStage().getScene().removeEventFilter(MouseEvent.MOUSE_PRESSED, cancelTileSelectMouseHandler);
fadeOut(currentTileSelector, (ActionEvent ev) -> {
if (currentTileSelector != null) {
Pane parent = (Pane) currentTileSelector.getParent();
parent.getChildren().remove(currentTileSelector);
currentTileSelector = null;
}
});
}
public static void fadeOut(Node node, EventHandler<ActionEvent> callback) {
FadeTransition ft = new FadeTransition(Duration.millis(250), node);
ft.setFromValue(1.0);

View File

@ -66,9 +66,10 @@ public class ApplicationUIControllerImpl extends ApplicationUIController {
rebuildImageSelectors();
rebuildMapSelectors();
rebuildTileSelectors();
redrawScripts();
globalController.redrawGlobalDataTypes();
globalController.redrawGlobalVariables();
redrawScripts();
globalController.redrawGlobalSheets();
}
@Override

View File

@ -13,6 +13,7 @@ import java.beans.IntrospectionException;
import java.util.Optional;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.scene.control.ListCell;
import javafx.scene.control.Tooltip;
@ -115,6 +116,7 @@ public class GlobalEditorTabControllerImpl extends GlobalEditorTabController {
@Override
public void startEdit() {
Platform.runLater(sheetList.getSelectionModel()::clearSelection);
UIAction.editSheet(getItem());
cancelEdit();
updateItem(getItem(), false);

View File

@ -15,24 +15,32 @@
*/
package org.badvision.outlaweditor.ui.impl;
import java.io.File;
import java.net.URL;
import java.util.Map;
import java.util.Optional;
import java.util.List;
import java.util.ResourceBundle;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import javafx.beans.property.SimpleObjectProperty;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.MenuItem;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.TextFieldTableCell;
import javax.xml.namespace.QName;
import javafx.stage.FileChooser;
import javafx.util.converter.DefaultStringConverter;
import org.badvision.outlaweditor.SheetEditor;
import org.badvision.outlaweditor.data.DataUtilities;
import static org.badvision.outlaweditor.data.DataUtilities.getValue;
import static org.badvision.outlaweditor.data.DataUtilities.setValue;
import org.badvision.outlaweditor.data.xml.Columns;
import org.badvision.outlaweditor.data.xml.Rows;
import org.badvision.outlaweditor.data.xml.Rows.Row;
import org.badvision.outlaweditor.data.xml.UserType;
import org.badvision.outlaweditor.ui.ApplicationUIController;
import org.badvision.outlaweditor.ui.SheetEditorController;
import org.badvision.outlaweditor.ui.UIAction;
@ -40,6 +48,7 @@ public class SheetEditorControllerImpl extends SheetEditorController {
private SheetEditor editor;
private ObservableList<Row> tableData;
private final ListChangeListener columnChangeListener = c -> syncData();
/**
* Initializes the controller class.
@ -52,15 +61,45 @@ public class SheetEditorControllerImpl extends SheetEditorController {
table.setEditable(true);
}
@Override
public void doImport(ActionEvent event) {
FileChooser openFileDialog = new FileChooser();
openFileDialog.setTitle("Select either a text or an Excel file");
File selected = openFileDialog.showOpenDialog(null);
if (selected != null && selected.exists() && selected.isFile()) {
List<List<String>> data = DataUtilities.readFromFile(selected);
if (data != null && data.size() > 1) {
tableData.clear();
editor.getSheet().setColumns(new Columns());
data.get(0).stream().map(s -> s != null && !s.isEmpty() ? s : "---").map(name -> {
UserType type = new UserType();
type.setName(name);
return type;
}).collect(Collectors.toCollection(editor.getSheet().getColumns()::getColumn));
editor.getSheet().setRows(new Rows());
data.stream().skip(1)
.map(cols -> {
Row r = new Row();
for (int i = 0; i < cols.size(); i++) {
if (cols.get(i) != null) {
setValue(r.getOtherAttributes(), data.get(0).get(i), cols.get(i));
}
}
return r;
})
.filter(r -> !r.getOtherAttributes().isEmpty())
.collect(Collectors.toCollection(editor.getSheet().getRows()::getRow));
buildTableFromSheet();
}
}
}
public void setEditor(SheetEditor editor) {
tableData.clear();
this.editor = editor;
if (editor.getSheet().getColumns() != null) {
editor.getSheet().getColumns().getColumn().stream().forEach(this::insertViewColumn);
}
if (editor.getSheet().getRows() != null) {
editor.getSheet().getRows().getRow().forEach(this::insertViewRow);
}
buildTableFromSheet();
}
@Override
@ -82,25 +121,65 @@ public class SheetEditorControllerImpl extends SheetEditorController {
insertViewRow(new Row());
}
//--------
private void buildTableFromSheet() {
table.getColumns().removeListener(columnChangeListener);
if (editor.getSheet().getColumns() != null) {
editor.getSheet().getColumns().getColumn().stream().forEach(this::insertViewColumn);
}
if (editor.getSheet().getRows() != null) {
tableData.setAll(editor.getSheet().getRows().getRow());
}
sheetNameField.textProperty().set(editor.getSheet().getName());
sheetNameField.textProperty().addListener((value, oldValue, newValue) -> {
editor.getSheet().setName(newValue);
ApplicationUIController.getController().updateSelectors();
});
table.getColumns().addListener(columnChangeListener);
}
private void insertViewColumn(UserType col) {
insertViewColumn(col, -1);
}
private void insertViewColumn(UserType col, int pos) {
if (pos < 0) {
pos = table.getColumns().size();
}
TableColumn<Row, String> tableCol = new TableColumn<>(col.getName());
tableCol.setCellValueFactory((features) -> {
String val = getValue(features.getValue(), col.getName());
String val = getValue(features.getValue().getOtherAttributes(), col.getName());
if (val == null) {
val = "";
}
return new SimpleObjectProperty(val);
});
tableCol.setCellFactory(TextFieldTableCell.forTableColumn());
tableCol.setOnEditCommit((event)
-> setValue(event.getRowValue(), col.getName(), event.getNewValue()));
tableCol.setCellFactory((TableColumn<Row, String> param) -> {
TextFieldTableCell<Row, String> myCell = new TextFieldTableCell<Row, String>(new DefaultStringConverter()) {
@Override
/**
* Patch behavior so that any change is immediately persisted,
* enter is not required.
*/
public void startEdit() {
super.startEdit();
TextField textField = (TextField) getGraphic();
textField.textProperty().addListener((p, o, n) -> {
setItem(n);
int index = this.getTableRow().getIndex();
Row row = tableData.get(index);
setValue(row.getOtherAttributes(), col.getName(), n);
});
}
};
return myCell;
});
tableCol.setOnEditCommit((event) -> {
table.requestFocus();
table.getSelectionModel().clearSelection();
});
tableCol.setEditable(true);
tableCol.setContextMenu(new ContextMenu(
createMenuItem("Rename Column", () -> renameColumn(col)),
@ -111,24 +190,7 @@ public class SheetEditorControllerImpl extends SheetEditorController {
private void insertViewRow(Row row) {
tableData.add(row);
}
private String getValue(Row row, String name) {
return row.getOtherAttributes().entrySet().stream()
.filter((e) -> e.getKey().getLocalPart().equals(name))
.map(e -> e.getValue())
.findFirst().orElse(null);
}
private void setValue(Row row, String name, String newValue) {
Optional<Map.Entry<QName, String>> attr = row.getOtherAttributes().entrySet().stream()
.filter((e) -> e.getKey().getLocalPart().equals(name)).findFirst();
if (attr.isPresent()) {
attr.get().setValue(newValue);
} else {
row.getOtherAttributes().put(new QName(name), newValue);
}
syncData();
}
private MenuItem createMenuItem(String text, Runnable action) {
@ -149,7 +211,7 @@ public class SheetEditorControllerImpl extends SheetEditorController {
UserType newCol = new UserType();
newCol.setName(newColName);
editor.getSheet().getColumns().getColumn().add(newCol);
tableData.forEach(row -> setValue(row, newColName, getValue(row, col.getName())));
tableData.forEach(row -> setValue(row.getOtherAttributes(), newColName, getValue(row.getOtherAttributes(), col.getName())));
int oldPos = deleteColumn(col);
insertViewColumn(newCol, oldPos);
}
@ -162,9 +224,27 @@ public class SheetEditorControllerImpl extends SheetEditorController {
.forEach(
m -> m.keySet().removeIf(n -> n.getLocalPart().equals(col.getName()))
);
for (int i=0; i < table.getColumns().size(); i++) {
int colNumber = findColumn(col);
if (colNumber >= 0) {
table.getColumns().remove(colNumber);
}
return colNumber;
}
private void syncData() {
if (editor.getSheet().getRows() == null) {
editor.getSheet().setRows(new Rows());
}
editor.getSheet().getRows().getRow().clear();
editor.getSheet().getRows().getRow().addAll(tableData);
editor.getSheet().getColumns().getColumn().sort((t1, t2) -> {
return Integer.compare(findColumn(t1), findColumn(t2));
});
}
private int findColumn(UserType col) {
for (int i = 0; i < table.getColumns().size(); i++) {
if (table.getColumns().get(i).getText().equals(col.getName())) {
table.getColumns().remove(i);
return i;
}
}

View File

@ -20,7 +20,6 @@ import javafx.event.ActionEvent;
import javafx.scene.control.ListView;
import javafx.scene.image.ImageView;
import javafx.util.StringConverter;
import org.badvision.outlaweditor.Application;
import org.badvision.outlaweditor.TileEditor;
import org.badvision.outlaweditor.api.ApplicationState;
import static org.badvision.outlaweditor.data.PropertyHelper.bind;
@ -33,6 +32,7 @@ import org.badvision.outlaweditor.data.xml.Tile;
import org.badvision.outlaweditor.ui.ApplicationUIController;
import org.badvision.outlaweditor.ui.EntitySelectorCell;
import org.badvision.outlaweditor.ui.TileEditorTabController;
import org.badvision.outlaweditor.ui.UIAction;
import static org.badvision.outlaweditor.ui.UIAction.confirm;
/**
@ -131,7 +131,8 @@ public class TileEditorTabControllerImpl extends TileEditorTabController {
@Override
public void onTileExportPressed(ActionEvent event) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
getCurrentTileEditor().copy();
UIAction.alert("Tile copied to the clipboard; use Paste Special in your target application to import this tile as an image, data, or a styled table (HTML)");
}
/**

View File

@ -2,10 +2,13 @@
<?import java.net.URL?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.control.Separator?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.control.ToolBar?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.VBox?>
@ -21,7 +24,7 @@
<menus>
<Menu mnemonicParsing="false" text="File">
<items>
<MenuItem mnemonicParsing="false" text="Import sheet" />
<MenuItem mnemonicParsing="false" onAction="#doImport" text="Import sheet" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Edit">
@ -38,11 +41,14 @@
</MenuBar>
<ToolBar prefHeight="40.0" prefWidth="200.0">
<items>
<Label text="Sheet Name" />
<TextField fx:id="sheetNameField" />
<Separator orientation="VERTICAL" />
<Button mnemonicParsing="false" onAction="#addColumnAction" text="Add Column" />
<Button mnemonicParsing="false" onAction="#addRowAction" text="Add Row" />
</items>
</ToolBar>
<TableView fx:id="table" prefHeight="200.0" prefWidth="200.0" VBox.vgrow="ALWAYS" />
<TableView fx:id="table" editable="true" prefHeight="200.0" prefWidth="200.0" VBox.vgrow="ALWAYS" />
</children>
</VBox>
</children>