Menu

[r10]: / XMLTool / src / xmltool / MainController.java  Maximize  Restore  History

Download this file

334 lines (303 with data), 13.6 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
package xmltool;
import com.sun.javafx.scene.control.behavior.TabPaneBehavior;
import com.sun.javafx.scene.control.skin.TabPaneSkin;
import java.awt.Desktop;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.net.URL;
import java.util.Arrays;
import java.util.ResourceBundle;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.geometry.Rectangle2D;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Pane;
import javafx.stage.Screen;
import javafx.stage.Stage;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.apache.fop.apps.FOPException;
import org.fxmisc.richtext.CodeArea;
import xmltool.transformation.FOTransformer;
import xmltool.transformation.NormTransformer;
import xmltool.utils.FileChooserUtil;
import xmltool.utils.FileReaderUtil;
import xmltool.utils.FileSaverUtil;
import xmltool.utils.UIHelperUtil;
import xmltool.validation.Validator;
public class MainController implements Initializable {
private double mouseDragOffsetX = 0;
private double mouseDragOffsetY = 0;
private Rectangle2D backupWindowBounds = null;
private boolean maximized = false;
private final Image restore = new Image("/xmltool/resources/restore.png");
private final Image maximize = new Image("/xmltool/resources/maximize.png");
private final String fileList = "XML/XSD/XSL File";
private final String[] fileArray = {"*.xml", "*.xsd", "*.xsl"};
//fields from FXML
@FXML
private Pane anchorPane;//use this to get ref to stage like this: (Stage)anchorPane.getScene().getWindow()
@FXML
private TabPane tabPane;
@FXML
private ImageView maximizeRestoreImg;
/**
* Wait for Java 8 to implement
*
* @param event
*/
@FXML
protected void textAreaOnKeyTyped(KeyEvent event) {
// int textLength = textArea.getText().length();
// char keyTyped = event.getCharacter().charAt(0);
// for (char c : GREEN) {
// if (c == keyTyped) {
// textArea.getText(textLength - 1, textLength);
// }
// }
}
@FXML
protected void fileNew() {
try {
Tab newTab = FXMLLoader.load(getClass().getResource("Tab.fxml"));
tabPane.getTabs().add(newTab);
TabPaneSkin skin = (TabPaneSkin) tabPane.getSkin();
TabPaneBehavior tabPaneBehavior = skin.getBehavior();
tabPaneBehavior.selectTab(newTab);
CodeArea textArea = (CodeArea) newTab.getContent().lookup("#textArea");
textArea.requestFocus();
UIHelperUtil.setOnCloseRequest(tabPane, (Stage) anchorPane.getScene().getWindow());
} catch (IOException ex) {
Logger.getLogger(MainController.class.getName()).log(Level.SEVERE, null, ex);
}
}
@FXML
protected void fileOpen() {
FileChooserUtil.setFilter(fileList, fileArray);
File choosenFile = FileChooserUtil.getFile((Stage) anchorPane.getScene().getWindow());
if (choosenFile != null && choosenFile.isFile()) {
CodeArea textArea = UIBean.getCurCodeArea();
if (!textArea.getText().isEmpty()) {
fileNew();
textArea = UIBean.getCurCodeArea();
}
Tab currTab = tabPane.getSelectionModel().getSelectedItem();
currTab.setText(choosenFile.getName());
textArea.replaceText(FileReaderUtil.getTextFromXML(choosenFile));
UIHelperUtil.addFile(choosenFile);
}
}
public void fileOpenByString(String filePath) {
File choosenFile = new File(filePath);
if (choosenFile.isFile()) {
CodeArea textArea = UIBean.getCurCodeArea();
if (!textArea.getText().isEmpty()) {
fileNew();
textArea = UIBean.getCurCodeArea();
}
Tab currTab = tabPane.getSelectionModel().getSelectedItem();
currTab.setText(choosenFile.getName());
textArea.replaceText(FileReaderUtil.getTextFromXML(choosenFile));
UIHelperUtil.addFile(choosenFile);
}
}
@FXML
protected void fileSave() {
CodeArea textArea = UIBean.getCurCodeArea();
Tab currTab = tabPane.getSelectionModel().getSelectedItem();
FileSaverUtil.setFilter(fileList, fileArray);
boolean isFileSaved = FileSaverUtil.saveFile((Stage) anchorPane.getScene().getWindow(), textArea.getText());
if (isFileSaved) {
currTab.setText(FileSaverUtil.getFile().getName());
}
}
@FXML
protected void exitProgram() {
UIHelperUtil.exitProgram();
}
@FXML
protected void dragWindowStarted(MouseEvent event) {
mouseDragOffsetX = event.getSceneX();
mouseDragOffsetY = event.getSceneY();
}
@FXML
protected void dragWindowDragged(MouseEvent event) {
if (!maximized) {
anchorPane.getScene().getWindow().setX(event.getScreenX() - mouseDragOffsetX);
anchorPane.getScene().getWindow().setY(event.getScreenY() - mouseDragOffsetY);
}
}
@FXML
protected void minimize() {
Stage stage = (Stage) anchorPane.getScene().getWindow();
stage.setIconified(true);
}
@FXML
protected void toogleMaximize() {
Stage stage = (Stage) anchorPane.getScene().getWindow();
final Screen screen = Screen.getScreensForRectangle(stage.getX(), stage.getY(), 1, 1).get(0);
if (maximized) {
maximized = false;
if (backupWindowBounds != null) {
stage.setX(backupWindowBounds.getMinX());
stage.setY(backupWindowBounds.getMinY());
stage.setWidth(backupWindowBounds.getWidth());
stage.setHeight(backupWindowBounds.getHeight());
maximizeRestoreImg.setImage(maximize);
}
} else {
maximized = true;
backupWindowBounds = new Rectangle2D(stage.getX(), stage.getY(), stage.getWidth(), stage.getHeight());
stage.setX(screen.getVisualBounds().getMinX());
stage.setY(screen.getVisualBounds().getMinY());
stage.setWidth(screen.getVisualBounds().getWidth());
stage.setHeight(screen.getVisualBounds().getHeight());
maximizeRestoreImg.setImage(restore);
}
}
@FXML
protected void toogleMaximizeDoubleClick(MouseEvent event) {
if (event.getClickCount() == 2) {
toogleMaximize();
}
}
@FXML
protected void stripWhiteSpaces() {
CodeArea textArea = UIBean.getCurCodeArea();
String text = textArea.getText();
StringBuilder sb = new StringBuilder();
Scanner sc = new Scanner(text);
while (sc.hasNextLine()) {
sb.append(sc.nextLine().trim());
}
textArea.replaceText(sb.toString());
}
@FXML
protected void prettyPrint() {
stripWhiteSpaces();
CodeArea textArea = UIBean.getCurCodeArea();
String text = textArea.getText();
Source xmlInput = new StreamSource(new StringReader(text));
StreamResult xmlOutput = new StreamResult(new StringWriter());
try {
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
transformer.transform(xmlInput, xmlOutput);
textArea.replaceText(xmlOutput.getWriter().toString());
} catch (TransformerException ex) {
Logger.getLogger(MainController.class.getName()).log(Level.SEVERE, null, ex);
}
}
@FXML
protected void validate() {
Stage stage = (Stage) anchorPane.getScene().getWindow();
String filePathXSD = MessageBoxFactory.showDropDownListDialog(stage, "", "Please choose XSD to validate the file", "Select file", UIHelperUtil.getOpenFiles());
String fileShortPathXML = tabPane.getSelectionModel().getSelectedItem().getText();
String filePathXML = UIHelperUtil.getAbsolutePathFromOpenFiles(fileShortPathXML);
try {
String result = Validator.doValidate(filePathXML, filePathXSD);
if (result != null && !result.isEmpty()) {
String[] lines = result.split("<br/>");
MessageBoxFactory.showErrorDialog(stage, "Check Details for all validation errors", "The file is NOT valid for this XSD", "Invalid XML", Arrays.toString(lines));
// MessageBoxFactory.showErrorDialog(stage, "Check Details for all validation errors", "The file is NOT valid for this XSD", "Invalid XML", ve);
} else {
MessageBoxFactory.showInfoDialog(stage, "", "The file is valid according to the selected XSD", "Valid XML");
}
} catch (FileNotFoundException ex) {
Logger.getLogger(MainController.class.getName()).log(Level.SEVERE, null, ex);
}
}
@FXML
protected void transform() {
NormTransformer normTransformer = new NormTransformer();
String fileShortPathXML = tabPane.getSelectionModel().getSelectedItem().getText();
String filePathXML = UIHelperUtil.getAbsolutePathFromOpenFiles(fileShortPathXML);
normTransformer.setInPath(filePathXML);
Stage stage = (Stage) anchorPane.getScene().getWindow();
String filePathXSL = MessageBoxFactory.showDropDownListDialog(stage, "", "Please choose XSL to transform the file", "Select file", UIHelperUtil.getOpenFiles());
normTransformer.setTransformatorPath(filePathXSL);
//REFACTOR THIS SHIT:
File file = new File(filePathXML);
file = file.getParentFile();
normTransformer.setResultDir(file.getAbsolutePath());
try {
String transformedFile = normTransformer.normaliseXSLT();
fileOpenByString(transformedFile);
prettyPrint();
} catch (TransformerException | IOException ex) {
MessageBoxFactory.showErrorDialog(stage, "Check Details for more info", "The transformation was unsuccessful", "Tranformation failed", ex);
Logger.getLogger(MainController.class.getName()).log(Level.SEVERE, null, ex);
}
}
@FXML
protected void FOTransform() {
FOTransformer foTransformer = new FOTransformer();
String fileShortPathXML = tabPane.getSelectionModel().getSelectedItem().getText();
String filePathXML = UIHelperUtil.getAbsolutePathFromOpenFiles(fileShortPathXML);
foTransformer.setXmlFilePath(filePathXML);
Stage stage = (Stage) anchorPane.getScene().getWindow();
String filePathXSL = MessageBoxFactory.showDropDownListDialog(stage, "", "Please choose XSL for FO transformation", "Select file", UIHelperUtil.getOpenFiles());
foTransformer.setXslFilePath(filePathXSL);
//REFACTOR THIS SHIT:
File file = new File(filePathXML);
file = file.getParentFile();
foTransformer.setPdfFilePath(file.getAbsolutePath());
try {
//transform and show the file
File pdf = foTransformer.transform();
Desktop desktop = Desktop.getDesktop();
desktop.open(pdf);
} catch (IOException | IllegalArgumentException | FOPException ex) {
MessageBoxFactory.showErrorDialog(stage, "Check Details for more info", "The FO transformation was unsuccessful", "PDF generation failed", ex);
Logger.getLogger(MainController.class.getName()).log(Level.SEVERE, null, ex);
}
}
@FXML
protected void cut() {
CodeArea textArea = UIBean.getCurCodeArea();
textArea.cut();
}
@FXML
protected void copy() {
CodeArea textArea = UIBean.getCurCodeArea();
textArea.copy();
}
@FXML
protected void paste() {
CodeArea textArea = UIBean.getCurCodeArea();
textArea.paste();
}
@FXML
protected void undo() {
CodeArea textArea = UIBean.getCurCodeArea();
textArea.undo();
// ((TextInputControlBehavior) ((TextInputControlSkin) textArea.getSkin()).getBehavior()).callAction("Undo");
}
@FXML
protected void redo() {
CodeArea textArea = UIBean.getCurCodeArea();
textArea.redo();
// ((TextInputControlBehavior) ((TextInputControlSkin) textArea.getSkin()).getBehavior()).callAction("Redo");
}
@Override
public void initialize(URL url, ResourceBundle rb) {
}
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.