Revize a5d16f0a
Přidáno uživatelem Michal Horký před asi 4 roky(ů)
project/Deserializer/src/Deserializer.java | ||
---|---|---|
1 | 1 |
import java.io.File; |
2 |
import java.io.IOException; |
|
2 |
import java.net.URL; |
|
3 |
import java.util.concurrent.atomic.AtomicReference; |
|
3 | 4 |
|
4 | 5 |
import io.FileWorker; |
5 | 6 |
import javafx.application.Application; |
... | ... | |
18 | 19 |
import javafx.scene.control.MenuBar; |
19 | 20 |
import javafx.scene.control.MenuItem; |
20 | 21 |
import javafx.scene.control.ProgressIndicator; |
22 |
import javafx.scene.control.ScrollPane; |
|
21 | 23 |
import javafx.scene.control.Separator; |
22 | 24 |
import javafx.scene.control.SeparatorMenuItem; |
23 |
import javafx.scene.control.Alert.AlertType;
|
|
25 |
import javafx.scene.effect.BlendMode;
|
|
24 | 26 |
import javafx.scene.image.Image; |
25 | 27 |
import javafx.scene.image.ImageView; |
28 |
import javafx.scene.input.Clipboard; |
|
29 |
import javafx.scene.input.DataFormat; |
|
26 | 30 |
import javafx.scene.input.KeyCode; |
27 | 31 |
import javafx.scene.input.KeyCodeCombination; |
28 | 32 |
import javafx.scene.input.KeyCombination; |
... | ... | |
31 | 35 |
import javafx.scene.layout.Priority; |
32 | 36 |
import javafx.scene.layout.Region; |
33 | 37 |
import javafx.scene.layout.VBox; |
38 |
import javafx.scene.paint.Color; |
|
34 | 39 |
import javafx.scene.web.WebEngine; |
35 | 40 |
import javafx.scene.web.WebView; |
36 | 41 |
import javafx.stage.FileChooser; |
... | ... | |
41 | 46 |
*/ |
42 | 47 |
public class Deserializer extends Application implements IConversionResults { |
43 | 48 |
|
49 |
public static boolean blockFocusProperty = false; |
|
50 |
|
|
44 | 51 |
private Stage stage; |
45 | 52 |
private Label fullScreen; |
53 |
|
|
46 | 54 |
private WebEngine webEngine; |
47 | 55 |
private VBox outputLayout; |
48 | 56 |
|
57 |
private ProgressIndicator clipBoardIndicator; |
|
58 |
private Label clipBoardResults; |
|
59 |
private Button setInputFromCB; |
|
60 |
private String lastClipBoardData; |
|
61 |
|
|
49 | 62 |
private Converter converter; |
63 |
private AtomicReference<jDeserializeResults> results; |
|
50 | 64 |
|
51 | 65 |
// TODO delete after deserializer debugging is complete... |
52 | 66 |
private File defaultInput = new File("a2"); |
53 | 67 |
private File defaultOutput = new File("results.json"); |
54 | 68 |
private boolean testing; |
55 | 69 |
|
56 |
public static void main(String[] args) { |
|
57 |
launch(args); |
|
58 |
} |
|
59 |
|
|
60 | 70 |
@Override |
61 | 71 |
public void init() throws Exception { |
62 | 72 |
super.init(); |
63 |
testing = true; |
|
73 |
testing = false; |
|
74 |
lastClipBoardData = null; |
|
64 | 75 |
converter = new Converter(this); |
65 | 76 |
converter.start(); |
77 |
results = new AtomicReference<jDeserializeResults>(new jDeserializeResults()); |
|
66 | 78 |
} |
67 | 79 |
|
68 | 80 |
@Override |
... | ... | |
75 | 87 |
@Override |
76 | 88 |
public void start(Stage stage) throws Exception { |
77 | 89 |
this.stage = stage; |
78 |
stage.getIcons().add(new Image("img/logo.png"));
|
|
90 |
stage.getIcons().add(new Image(getResource("logo.png")));
|
|
79 | 91 |
stage.setTitle("Java Object Universal Deserializer"); |
80 | 92 |
stage.setScene(createScene()); |
81 | 93 |
stage.setMinWidth(400); |
... | ... | |
85 | 97 |
changeFullScreenMenuItem(); |
86 | 98 |
}); |
87 | 99 |
|
88 |
stage.focusedProperty().addListener(new ChangeListener<Boolean>() |
|
89 |
{ |
|
100 |
stage.focusedProperty().addListener(new ChangeListener<Boolean>() { |
|
90 | 101 |
@Override |
91 |
public void changed(ObservableValue<? extends Boolean> obs, Boolean oldValue, Boolean newValue) |
|
92 |
{ |
|
93 |
System.out.println(newValue); |
|
94 |
|
|
95 |
/* |
|
96 |
byte arr[] = FileWorker.loadByteArray(new File("b2")); |
|
97 |
byte arr2[] = FileWorker.loadByteArray(new File("b")); |
|
98 |
|
|
99 |
byte con[] = FileWorker.load(new File("b")); |
|
100 |
|
|
101 |
if (arr.length == arr2.length && arr2.length == con.length) { |
|
102 |
System.out.println("TRRUE"); |
|
103 |
for (int i = 0; i < arr.length; i++) { |
|
104 |
if (arr[i] != arr2[i] || arr2[i] != con[i]) { |
|
105 |
System.out.println("CHCHYBA!!!"); |
|
106 |
break; |
|
107 |
} |
|
108 |
} |
|
109 |
} |
|
110 |
System.out.println("COMPLETE"); |
|
111 |
*/ |
|
112 |
/*Set<DataFormat> types = Clipboard.getSystemClipboard().getContentTypes(); |
|
113 |
System.out.println("Detected types: " + types.size()); |
|
114 |
for (DataFormat s : types) { |
|
115 |
System.out.println(s); |
|
116 |
} |
|
117 |
|
|
118 |
try { |
|
119 |
byte[] orig = FileWorker.loadByteArray(new File("b")); |
|
120 |
//byte[] orig = FileWorker.getBBB(); |
|
121 |
|
|
102 |
public void changed(ObservableValue<? extends Boolean> obs, Boolean oldValue, Boolean newValue) { |
|
103 |
if (newValue && !blockFocusProperty) { |
|
104 |
// Available data formats. |
|
105 |
/*Set<DataFormat> types = Clipboard.getSystemClipboard().getContentTypes(); |
|
106 |
System.out.println("Detected types: " + types.size()); |
|
107 |
for (DataFormat s : types) { |
|
108 |
System.out.println(s); |
|
109 |
}*/ |
|
110 |
|
|
122 | 111 |
DataFormat df = DataFormat.lookupMimeType("text/plain"); |
123 | 112 |
if (df != null) { |
124 |
// System.out.println(Clipboard.getSystemClipboard().getContent(df).getClass()); |
|
125 | 113 |
String data = (String) Clipboard.getSystemClipboard().getContent(df); |
126 |
byte[] buffer = data.getBytes(); |
|
127 |
|
|
128 |
boolean changeBites = false; |
|
129 |
|
|
130 |
List<Byte> bl = new ArrayList<Byte>(); |
|
131 |
for (int i = 0; i < buffer.length; i++) { |
|
132 |
if (changeBites) { |
|
133 |
buffer[i] = (byte) ((buffer[i] & 0x0F) | 0xE0); |
|
134 |
changeBites = false; |
|
135 |
} else if (buffer[i] == (byte) 0xC2 || buffer[i] == (byte) 0x0D) { |
|
136 |
continue; |
|
137 |
} else if (buffer[i] == (byte) 0xC3) { |
|
138 |
changeBites = true; |
|
139 |
continue; |
|
140 |
} |
|
141 |
bl.add(buffer[i]); |
|
142 |
} |
|
143 | 114 |
|
144 |
System.out.println(bl.size() + " " + orig.length); |
|
145 |
for (int i = 0; i < orig.length; i++) { |
|
146 |
if (bl.get(i) != orig[i]) { |
|
147 |
System.out.println(i); |
|
148 |
for (int j = i - 1; j < i + 50; j++) { |
|
149 |
System.out.println(String.format("%02X", bl.get(j)) + " " + String.format("%02X", orig[j])); |
|
150 |
} |
|
151 |
break; |
|
152 |
} |
|
153 |
} |
|
115 |
if (lastClipBoardData == null) { |
|
116 |
lastClipBoardData = data; |
|
117 |
} else if (data.equals(lastClipBoardData)) { |
|
118 |
return; |
|
119 |
} |
|
120 |
|
|
121 |
changeIndicatorState(false, false); |
|
122 |
setInputFromCB.setDisable(true); |
|
123 |
converter.setInput(null, data, true); |
|
154 | 124 |
} |
155 |
} catch (Exception e) { |
|
156 |
e.printStackTrace(); |
|
157 |
}*/ |
|
125 |
} |
|
158 | 126 |
} |
159 | 127 |
}); |
160 |
|
|
161 |
//tests |
|
162 |
//Converter thread = new Converter(this, new File("simple_arrays.out"), new File("bcha.json")); |
|
163 |
//thread.start(); |
|
164 | 128 |
} |
165 | 129 |
|
166 | 130 |
private Scene createScene() { |
167 | 131 |
createMenu(); |
168 | 132 |
Scene scene = new Scene(createLayout()); |
169 |
scene.getStylesheets().add("css/main.css");
|
|
133 |
scene.getStylesheets().add(getResource("main.css"));
|
|
170 | 134 |
return scene; |
171 | 135 |
} |
172 | 136 |
|
... | ... | |
189 | 153 |
inputFile = new Label(defaultOutput.getAbsolutePath()); |
190 | 154 |
inputFile.setStyle("-fx-font-weight: bold;"); |
191 | 155 |
} |
192 |
// TODO inputFile.setPrefWidth(400);
|
|
193 |
inputFile.setWrapText(true);
|
|
156 |
inputFile.setPrefWidth(500); // Value to cut the text.
|
|
157 |
inputFile.setMaxWidth(Double.MAX_VALUE);
|
|
194 | 158 |
|
195 | 159 |
Button setInputFile = new Button("Vstupní soubor"); |
196 | 160 |
setInputFile.setOnAction(event -> { |
161 |
Deserializer.blockFocusProperty = true; // TODO Prohibition to process the selected file. |
|
197 | 162 |
if (testing) { |
198 |
converter.setInput(defaultInput); |
|
163 |
converter.setInput(defaultInput, null, false);
|
|
199 | 164 |
} else { |
200 | 165 |
FileChooser fCh = new FileChooser(); |
201 | 166 |
fCh.setInitialDirectory(new File(".")); |
... | ... | |
208 | 173 |
inputFile.setText(file.getAbsolutePath()); |
209 | 174 |
inputFile.setStyle("-fx-font-weight: bold;"); |
210 | 175 |
|
211 |
converter.setInput(file); |
|
176 |
converter.setInput(file, null, false);
|
|
212 | 177 |
} |
213 | 178 |
} |
179 |
Deserializer.blockFocusProperty = false; |
|
214 | 180 |
}); |
215 | 181 |
|
216 | 182 |
HBox bIF = new HBox(); |
... | ... | |
222 | 188 |
forInput.getChildren().addAll(forInputFile, inputFile, bIF); |
223 | 189 |
|
224 | 190 |
|
191 |
clipBoardIndicator = new ProgressIndicator(); |
|
192 |
clipBoardIndicator.setMaxSize(38, 38); |
|
193 |
clipBoardIndicator.setVisible(false); |
|
194 |
clipBoardResults = new Label(); |
|
195 |
clipBoardResults.setMaxSize(38, 38); |
|
196 |
clipBoardResults.setVisible(false); |
|
197 |
HBox clipBoardStateLayout = new HBox(); |
|
198 |
clipBoardStateLayout.setSpacing(0); |
|
199 |
clipBoardStateLayout.setMinSize(38, 38); |
|
200 |
clipBoardStateLayout.setMaxSize(38, 38); |
|
201 |
clipBoardStateLayout.getChildren().addAll(clipBoardResults, clipBoardIndicator); |
|
202 |
|
|
225 | 203 |
Label forClipBoardInput = new Label("Vstup ze schránky"); |
226 |
ProgressIndicator indicator = new ProgressIndicator(); |
|
227 |
indicator.setMaxHeight(38); // TODO |
|
228 |
Button setInputAccToCB = new Button("Použít"); |
|
229 |
setInputAccToCB.setOnAction(event -> { |
|
230 |
// TODO |
|
204 |
setInputFromCB = new Button("Použít"); |
|
205 |
setInputFromCB.setOnAction(event -> { |
|
206 |
setInputFromCB.setDisable(true); |
|
207 |
results.get().merge(); |
|
208 |
webEngine.loadContent(results.get().getHtml(), "text/html"); |
|
209 |
setOutputLayoutDisabled(false); |
|
231 | 210 |
}); |
211 |
setInputFromCB.setDisable(true); |
|
232 | 212 |
|
233 | 213 |
HBox indicatorLayout = new HBox(); |
234 | 214 |
Region region2 = new Region(); |
235 | 215 |
HBox.setHgrow(region2, Priority.ALWAYS); |
236 |
indicatorLayout.getChildren().addAll(forClipBoardInput, region2, indicator);
|
|
216 |
indicatorLayout.getChildren().addAll(forClipBoardInput, region2, clipBoardStateLayout);
|
|
237 | 217 |
|
238 | 218 |
HBox bInputCB = new HBox(); |
239 | 219 |
bInputCB.setAlignment(Pos.CENTER_RIGHT); |
240 |
bInputCB.getChildren().add(setInputAccToCB);
|
|
220 |
bInputCB.getChildren().add(setInputFromCB);
|
|
241 | 221 |
|
242 | 222 |
VBox forInput2 = new VBox(); |
243 | 223 |
forInput2.setSpacing(5.0); |
... | ... | |
258 | 238 |
|
259 | 239 |
Button setOutputFile = new Button("Uložit"); |
260 | 240 |
setOutputFile.setOnAction(event -> { |
241 |
Deserializer.blockFocusProperty = true; // TODO Prohibition to process the selected file. |
|
261 | 242 |
File jsonFile = null; |
262 | 243 |
|
263 | 244 |
if (testing) { |
... | ... | |
274 | 255 |
} |
275 | 256 |
} |
276 | 257 |
|
277 |
if (jsonFile != null && webEngine.getDocument() != null) {
|
|
258 |
if (jsonFile != null && results.get().contains(false)) {
|
|
278 | 259 |
String title = "Uložení JSON"; |
279 | 260 |
try { |
280 |
FileWorker.saveJson(jsonFile, "Ahoj"/* TODO webEngine.getDocument().getTextContent()*/);
|
|
261 |
FileWorker.saveJson(jsonFile, results.get().getJson());
|
|
281 | 262 |
Report.info(title, null, "Uložení JSON souboru proběhlo v pořádku."); |
282 | 263 |
} catch (Exception e) { |
283 | 264 |
Report.error(title, null, "Při ukládání JSON souboru nastala chyba."); |
284 | 265 |
} |
285 | 266 |
} |
267 |
Deserializer.blockFocusProperty = false; |
|
286 | 268 |
}); |
287 | 269 |
|
288 | 270 |
HBox bOF = new HBox(); |
... | ... | |
297 | 279 |
setOutputLayoutDisabled(true); |
298 | 280 |
|
299 | 281 |
|
300 |
WebView resultantJson = new WebView(); |
|
282 |
|
|
283 |
WebView resultantJson = new WebView(); |
|
301 | 284 |
resultantJson.setPrefHeight(400); |
302 | 285 |
webEngine = resultantJson.getEngine(); |
286 |
resultantJson.setBlendMode(BlendMode.DARKEN); |
|
287 |
|
|
303 | 288 |
|
304 | 289 |
|
305 | 290 |
|
... | ... | |
313 | 298 |
VBox layout = new VBox(); |
314 | 299 |
layout.setPadding(new Insets(10.0)); |
315 | 300 |
layout.setSpacing(10.0); |
301 |
VBox.setVgrow(resultantJson, Priority.ALWAYS); |
|
316 | 302 |
layout.getChildren().addAll(header, new Separator(), resultantJson, new Separator(), outputLayout); |
317 | 303 |
|
318 | 304 |
return layout; |
... | ... | |
328 | 314 |
this.fullScreen = (Label) fullScreen.getGraphic(); |
329 | 315 |
changeFullScreenMenuItem(); |
330 | 316 |
|
331 |
MenuItem close = createMenuItem("Ukončit", "img/close.png", new KeyCodeCombination(KeyCode.E, KeyCombination.CONTROL_DOWN));
|
|
317 |
MenuItem close = createMenuItem("Ukončit", getResource("close.png"), new KeyCodeCombination(KeyCode.E, KeyCombination.CONTROL_DOWN));
|
|
332 | 318 |
close.setOnAction(event -> { |
333 | 319 |
Platform.exit(); |
334 | 320 |
}); |
... | ... | |
342 | 328 |
|
343 | 329 |
private void changeFullScreenMenuItem() { |
344 | 330 |
fullScreen.setText(stage.isFullScreen() ? "Normální zobrazení" : "Plné zobrazení"); |
345 |
fullScreen.setGraphic(new ImageView(stage.isFullScreen() ? "img/normal.png" : "img/full.png"));
|
|
331 |
fullScreen.setGraphic(new ImageView(stage.isFullScreen() ? getResource("normal.png") : getResource("full.png")));
|
|
346 | 332 |
} |
347 | 333 |
|
348 | 334 |
private MenuItem createMenuItem(String name, String icon, KeyCodeCombination keyCodeComb) { |
... | ... | |
364 | 350 |
outputLayout.setDisable(!testing && disable); |
365 | 351 |
} |
366 | 352 |
|
353 |
private void changeIndicatorState(boolean showResults, boolean result) { |
|
354 |
clipBoardIndicator.setVisible(!showResults); |
|
355 |
if (showResults) { |
|
356 |
clipBoardResults.setGraphic(new ImageView(new Image(result ? getResource("ok.png") : getResource("error.png")))); |
|
357 |
} |
|
358 |
clipBoardResults.setVisible(showResults); |
|
359 |
} |
|
360 |
|
|
361 |
private String getResource(String path) { |
|
362 |
return Deserializer.class.getResource(path).toString(); |
|
363 |
} |
|
364 |
|
|
367 | 365 |
@Override |
368 | 366 |
public void loadingInputFileError() { |
369 | 367 |
Platform.runLater(()->{ |
... | ... | |
372 | 370 |
} |
373 | 371 |
|
374 | 372 |
@Override |
375 |
public void completed(final String json) { |
|
373 |
public void completed(String html, String json, boolean testing) { |
|
374 |
// HTML only for clip board content. |
|
375 |
results.get().setData(html, json, testing); |
|
376 |
|
|
376 | 377 |
Platform.runLater(()->{ |
377 |
String _json = json; |
|
378 |
setOutputLayoutDisabled(_json != null); |
|
379 |
if (_json == null) { |
|
380 |
_json = "--- Chyba při deserializaci! ---"; |
|
378 |
boolean result = results.get().contains(testing); |
|
379 |
if (testing) { |
|
380 |
changeIndicatorState(testing, result); |
|
381 |
setInputFromCB.setDisable(!result); |
|
382 |
} else { |
|
383 |
BadHttpServer.getIntance().setHtmlString(result ? results.get().getHtml() : "<b>--- CHYBA PŘI DESERIALIZACI! ---</b>"); |
|
384 |
Platform.runLater(() -> webEngine.load(BadHttpServer.getIntance().getURL())); |
|
385 |
// TODO webEngine.loadContent(html, "text/html"); |
|
386 |
setOutputLayoutDisabled(!result); |
|
381 | 387 |
} |
382 |
webEngine.loadContent(_json, "text/plain"); |
|
383 | 388 |
}); |
384 | 389 |
} |
385 | 390 |
|
Také k dispozici: Unified diff
re #7879 release v0.2
release v0.2