blob: 8d3504649d4354641c64f2836589ceaaf1593cd8 [file] [log] [blame]
adminbae64d82013-08-01 10:50:15 -07001/*
2
3 * To change this template, choose Tools | Templates
4 * and open the template in the editor.
5 */
6package tai_ofa;
7
8import java.util.ArrayList;
9import java.util.HashMap;
10import java.util.Iterator;
11import java.util.Map;
12import java.util.Observable;
13import java.util.logging.Level;
14import java.util.logging.Logger;
15import javafx.beans.property.SimpleStringProperty;
16import javafx.collections.FXCollections;
17import javafx.collections.ObservableList;
18import javafx.event.ActionEvent;
19import javafx.event.Event;
20import javafx.event.EventHandler;
21import javafx.geometry.Insets;
22import javafx.geometry.Orientation;
23import javafx.scene.control.Button;
24import javafx.scene.control.Label;
25import javafx.scene.control.SplitPane;
26import javafx.scene.control.Tab;
27import javafx.scene.control.TableColumn;
28import javafx.scene.control.TableColumn.CellEditEvent;
29import javafx.scene.control.TableView;
30import javafx.scene.control.TextField;
31import javafx.scene.control.TextFieldBuilder;
32import javafx.scene.control.TreeItem;
33import javafx.scene.control.TreeView;
34import javafx.scene.control.cell.PropertyValueFactory;
35import javafx.scene.control.cell.TextFieldTableCell;
36import javafx.scene.image.Image;
37import javafx.scene.image.ImageView;
38import javafx.scene.input.KeyEvent;
39import javafx.scene.input.MouseEvent;
40import javafx.scene.layout.GridPane;
41import javafx.scene.layout.HBox;
42import javafx.scene.layout.Pane;
43import javafx.scene.layout.VBox;
44import javafx.scene.paint.Color;
45import javafx.scene.text.Font;
46import javafx.scene.text.Text;
47import javafx.stage.Stage;
48
49/**
50 *
51 * @author Raghav Kashyap (raghavkashyap@paxterrasolutions.com)
52
53 * TestON is free software: you can redistribute it and/or modify
54 * it under the terms of the GNU General Public License as published by
55 * the Free Software Foundation, either version 2 of the License, or
56 * (at your option) any later version.
57
58 * TestON is distributed in the hope that it will be useful,
59 * but WITHOUT ANY WARRANTY; without even the implied warranty of
60 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
61 * GNU General Public License for more details.
62
63 * You should have received a copy of the GNU General Public License
64 * along with TestON. If not, see <http://www.gnu.org/licenses/>.
65
66
67 */
68public class AddParams {
69
70 TAI_OFA referenceOFA;
71 boolean splitFlag = false;
72 Map<String, Object> paramsHash;
73 TreeView<String> paramsTreeView;
74 TextField Value, tableAttrib, tableValue;
75 TableView<ParamsAttribute> attributeTable;
76 ObservableList<ParamsAttribute> data;
77 Button save, Cancel, edit, add, saveParams;
78 HBox buttonBox, tableViewBox, baseLeftPane;
79 VBox box, buttonTableVBox;
80 GridPane buttonBoxPane, baseRightPane;
81 TreeItem<String> params;
82 Pane tableViewPane;
83 Text Heading;
84 String tabValue, tabs;
85 OFAWizard wizard;
86 OFAFileOperations fileOperations;
87 Tab baseTab;
88 Button delete;
89
90 public void setOFA(TAI_OFA ofa) {
91 this.referenceOFA = ofa;
92 }
93
94 public void getNewParams() {
95 baseTab = new Tab();
96 paramsHash = new HashMap<String, Object>();
97 fileOperations = new OFAFileOperations();
98 final SplitPane basePane = new SplitPane();
99 basePane.setOrientation(Orientation.HORIZONTAL);
100 baseLeftPane = new HBox(30);
101 params = new TreeItem<String>();
102 params.setValue("params");
103 TreeItem<String> log_dir = new TreeItem<String>();
104 log_dir.setValue("log_dir");
105 ImageView logIView = new ImageView(new Image("images/parameter.jpg", 20, 20, true, true));
106 logIView.setId("/home/paxterra/");
107 log_dir.setGraphic(logIView);
108 TreeItem<String> mail = new TreeItem<String>();
109 mail.setValue("mail");
110 ImageView mailIView = new ImageView(new Image("images/parameter.jpg", 20, 20, true, true));
111 mailIView.setId("raghavkashyap@paxterrasolution.com");
112 mail.setGraphic(mailIView);
113 TreeItem<String> testcases = new TreeItem<String>();
114 testcases.setValue("testcases");
115 ImageView testIView = new ImageView(new Image("images/parameter.jpg", 20, 20, true, true));
116 testIView.setId("1");
117 testcases.setGraphic(testIView);
118 data = FXCollections.observableArrayList();
119 params.getChildren().addAll(testcases, mail, log_dir);
120 paramsTreeView = new TreeView<String>(params);
121 saveParams = new Button("Save");
122 delete = new Button("Delete");
123 baseLeftPane.getChildren().addAll(paramsTreeView, saveParams, delete);
124 baseRightPane = new GridPane();
125 Value = TextFieldBuilder.create().build();
126 delete.setOnAction(new EventHandler<ActionEvent>() {
127 @Override
128 public void handle(ActionEvent t) {
129 removeParamsValue(paramsTreeView.getSelectionModel().getSelectedItem());
130 }
131 });
132
133 saveParams.setOnAction(new EventHandler<ActionEvent>() {
134 @Override
135 public void handle(ActionEvent t) {
136 tabs = getParams(params);
137 referenceOFA.paramsFileContent = tabs;
138 wizard = new OFAWizard(referenceOFA.projectExplorerTree, 2, referenceOFA.projectExplorerTree.getChildren(), referenceOFA.projectExplorerTreeView);
139 wizard.setOFA(referenceOFA);
140 try {
141 wizard.start(new Stage());
142 } catch (Exception ex) {
143 Logger.getLogger(AddParams.class.getName()).log(Level.SEVERE, null, ex);
144 }
145
146 }
147 });
148 save = new Button("Save");
149 Cancel = new Button("Cancel");
150 edit = new Button("Edit");
151 attributeTable = new TableView<ParamsAttribute>();
152 attributeTable.setEditable(true);
153 TableColumn attribColumn = new TableColumn("Attribute");
154 attribColumn.setCellValueFactory(new PropertyValueFactory<ParamsAttribute, String>("Attribute"));
155 TableColumn valueColumn = new TableColumn("Value");
156 valueColumn.setCellValueFactory(new PropertyValueFactory<ParamsAttribute, String>("Values"));
157 attributeTable.setItems(data);
158 attributeTable.getColumns().addAll(attribColumn, valueColumn);
159 baseRightPane.setPadding(new Insets(30, 0, 10, 30));
160 baseRightPane.prefHeight(referenceOFA.scene.heightProperty().get());
161 baseRightPane.setVgap(9);
162 baseRightPane.add(new Label("Value :"), 4, 4);
163 baseRightPane.add(Value, 5, 4);
164 box = new VBox();
165 buttonBox = new HBox();
166 buttonBoxPane = new GridPane();
167 buttonBoxPane.setPadding(new Insets(30, 0, 10, 30));
168 buttonBoxPane.setHgap(3);
169 buttonBoxPane.add(save, 2, 7);
170 buttonBoxPane.add(Cancel, 4, 7);
171 buttonBoxPane.add(edit, 6, 7);
172 buttonBox.getChildren().addAll(buttonBoxPane);
173 tableAttrib = TextFieldBuilder.create().build();
174 tableValue = TextFieldBuilder.create().build();
175 add = new Button("Add");
176 tableViewPane = new Pane();
177 buttonTableVBox = new VBox();
178 tableViewBox = new HBox();
179 tableViewBox.getChildren().addAll(tableAttrib, tableValue, add);
180 buttonTableVBox.getChildren().addAll(attributeTable);
181 box.getChildren().addAll(baseRightPane, buttonTableVBox, buttonBoxPane);
182
183 attributeTable.setEditable(true);
184 attribColumn.setCellFactory(TextFieldTableCell.forTableColumn());
185 attribColumn.setOnEditCommit(new EventHandler<CellEditEvent<ParamsAttribute, String>>() {
186 @Override
187 public void handle(CellEditEvent<ParamsAttribute, String> t) {
188
189 for (int i = 0; i < paramsTreeView.getSelectionModel().getSelectedItem().getChildren().size(); i++) {
190
191 if (paramsTreeView.getSelectionModel().getSelectedItem().getChildren().get(i).getValue().equals(t.getOldValue())) {
192 paramsTreeView.getSelectionModel().getSelectedItem().getChildren().get(i).setValue(t.getNewValue());
193 }
194 }
195 ((ParamsAttribute) t.getTableView().getItems().get(
196 t.getTablePosition().getRow())).setAttribute(t.getNewValue());
197
198 }
199 });
200
201 valueColumn.setCellFactory(TextFieldTableCell.forTableColumn());
202 valueColumn.setOnEditCommit(new EventHandler<CellEditEvent<ParamsAttribute, String>>() {
203 @Override
204 public void handle(CellEditEvent<ParamsAttribute, String> t) {
205 for (int i = 0; i < paramsTreeView.getSelectionModel().getSelectedItem().getChildren().size(); i++) {
206 if (paramsTreeView.getSelectionModel().getSelectedItem().getChildren().get(i).getValue().equals(t.getOldValue())) {
207 paramsTreeView.getSelectionModel().getSelectedItem().getChildren().get(i).getGraphic().setId(t.getNewValue());
208 }
209 }
210 ((ParamsAttribute) t.getTableView().getItems().get(
211 t.getTablePosition().getRow())).setValues(t.getNewValue());
212 }
213 });
214
215 Value.setOnKeyReleased(new EventHandler<KeyEvent>() {
216 @Override
217 public void handle(KeyEvent t) {
218 if (Value.getText().isEmpty()) {
219 edit.setDisable(false);
220 } else {
221 edit.setDisable(true);
222 }
223 }
224 });
225
226 Cancel.setOnAction(new EventHandler<ActionEvent>() {
227 @Override
228 public void handle(ActionEvent t) {
229 basePane.getItems().removeAll(box);
230 splitFlag = false;
231 baseRightPane.getChildren().remove(Heading);
232 edit.setDisable(false);
233 }
234 });
235
236 save.setOnAction(new EventHandler<ActionEvent>() {
237 @Override
238 public void handle(ActionEvent t) {
239 String selected = paramsTreeView.getSelectionModel().getSelectedItem().getValue();
240 TreeItem<String> selectedTreeItem = paramsTreeView.getSelectionModel().getSelectedItem();
241 String attribVal = Value.getText();
242 basePane.getItems().removeAll(box);
243 splitFlag = false;
244 Map<String, Object> prevParent = new HashMap<String, Object>();
245 ArrayList<String> names = new ArrayList<String>();
246 if (!edit.isDisabled()) {
247 for (int i = 0; i < attributeTable.getItems().size(); i++) {
248 ParamsAttribute table = attributeTable.getItems().get(i);
249 if (selectedTreeItem.getChildren().size() == 0) {
250 TreeItem<String> childNode = new TreeItem<String>();
251 childNode.setValue(table.getAttribute());
252 selectedTreeItem.getChildren().add(childNode);
253 selectedTreeItem.setExpanded(true);
254 Image chidlImage = new Image("images/parameter.jpg", 20, 20, true, true);
255 ImageView childImageView = new ImageView();
256 childImageView.setImage(chidlImage);
257 childImageView.setId(table.getValues());
258 childNode.setGraphic(childImageView);
259 } else if (selectedTreeItem.getChildren().size() > 0) {
260 names.clear();
261 for (int index = 0; index < selectedTreeItem.getChildren().size(); index++) {
262 names.add(selectedTreeItem.getChildren().get(index).getValue());
263 }
264 if (!names.contains(table.getAttribute())) {
265 TreeItem<String> childNode = new TreeItem<String>();
266 childNode.setValue(table.getAttribute());
267 selectedTreeItem.getChildren().addAll(childNode);
268 selectedTreeItem.setExpanded(true);
269 Image chidlImage = new Image("images/parameter.jpg", 20, 20, true, true);
270 ImageView childImageView = new ImageView();
271 childImageView.setImage(chidlImage);
272 childImageView.setId(table.getValues());
273 childNode.setGraphic(childImageView);
274 }
275 }
276 }
277 } else {
278 TreeItem<String> selectTreeItem = paramsTreeView.getSelectionModel().getSelectedItem();
279 if (!Value.getText().isEmpty()) {
280 paramsTreeView.getSelectionModel().getSelectedItem().getGraphic().setId(Value.getText());
281 }
282 }
283 baseRightPane.getChildren().remove(Heading);
284 buttonTableVBox.getChildren().removeAll(tableViewBox);
285 Value.clear();
286 }
287 });
288
289 add.setOnAction(new EventHandler<ActionEvent>() {
290 @Override
291 public void handle(ActionEvent t) {
292
293 if (tableAttrib.getText().isEmpty() && tableValue.getText().isEmpty()) {
294 } else {
295 data.add(new ParamsAttribute(tableAttrib.getText(), tableValue.getText()));
296 tableAttrib.clear();
297 tableValue.clear();
298 }
299 }
300 });
301
302
303 edit.setOnAction(new EventHandler<ActionEvent>() {
304 @Override
305 public void handle(ActionEvent t) {
306
307 buttonTableVBox.getChildren().addAll(tableViewBox);
308
309 Value.setEditable(false);
310
311 }
312 });
313
314 basePane.getItems().addAll(baseLeftPane);
315 paramsTreeView.setOnMouseClicked(new EventHandler<MouseEvent>() {
316 @Override
317 public void handle(MouseEvent args0) {
318 String selected = paramsTreeView.getSelectionModel().getSelectedItem().getValue();
319 TreeItem<String> selectItem = paramsTreeView.getSelectionModel().getSelectedItem();
320 if (args0.getClickCount() == 2 & !splitFlag) {
321 Value.setEditable(true);
322 Heading = new Text(selected);
323 Heading.setFont(Font.font("Arial", 20));
324 Heading.setFill(Color.BLUE);
325 baseRightPane.add(Heading, 6, 1);
326
327 if (selectItem.isLeaf()) {
328 Value.setDisable(false);
329 if (!selectItem.getGraphic().getId().equals("")) {
330 basePane.getItems().addAll(box);
331 splitFlag = true;
332 Value.setText(selectItem.getGraphic().getId());
333 edit.setDisable(true);
334 data.clear();
335 } else {
336 basePane.getItems().addAll(box);
337 splitFlag = true;
338 Value.clear();
339 edit.setDisable(false);
340 data.clear();
341 }
342 } else if (!selectItem.isLeaf()) {
343 Value.clear();
344 Value.setDisable(true);
345 basePane.getItems().addAll(box);
346 splitFlag = true;
347 data.clear();
348 for (int i = 0; i < selectItem.getChildren().size(); i++) {
349
350 data.add(new ParamsAttribute(selectItem.getChildren().get(i).getValue(), selectItem.getChildren().get(i).getGraphic().getId()));
351 }
352 }
353
354 }
355 }
356 });
357 basePane.prefWidthProperty().bind(referenceOFA.scene.widthProperty().subtract(300));
358 basePane.prefHeightProperty().bind(referenceOFA.scene.heightProperty().subtract(120));
359 baseTab.setContent(basePane);
360 baseTab.setText("Unnamed.params");
361 referenceOFA.editorTabPane.getTabs().addAll(baseTab);
362 }
363
364 public String getParams(TreeItem<String> treeNode) {
365 tabValue = "";
366 tabValue = tabValue + "<" + treeNode.getValue() + ">\n";
367 if (!treeNode.isLeaf()) {
368 for (int i = 0; i < treeNode.getChildren().size(); i++) {
369 if (treeNode.getChildren().get(i).isLeaf()) {
370 tabValue = tabValue + "\n<" + treeNode.getChildren().get(i).getValue() + ">" + treeNode.getChildren().get(i).getGraphic().getId()
371 + "</" + treeNode.getChildren().get(i).getValue() + ">\n";
372 } else if (!treeNode.getChildren().get(i).isLeaf()) {
373 int index = 0;
374 tabValue = tabValue + "\n<" + treeNode.getChildren().get(i).getValue() + ">\n";
375 while (index < treeNode.getChildren().get(i).getChildren().size()) {
376 tabValue = tabValue + getParams(treeNode.getChildren().get(i).getChildren().get(index));
377 index++;
378 }
379 tabValue = tabValue + "\n</" + treeNode.getChildren().get(i).getValue() + ">\n";
380 }
381 }
382 } else if (treeNode.isLeaf()) {
383 tabValue = tabValue + treeNode.getGraphic().getId();
384 }
385 tabValue = tabValue + "\n</" + treeNode.getValue() + ">";
386 return tabValue;
387 }
388
389 public void removeParamsValue(TreeItem treeItem) {
390 for (int i = 0; i < paramsTreeView.getRoot().getChildren().size(); i++) {
391 if (paramsTreeView.getRoot().getChildren().get(i).isLeaf()) {
392 if (paramsTreeView.getRoot().getChildren().get(i).getValue().equals(treeItem.getValue())) {
393 paramsTreeView.getRoot().getChildren().remove(paramsTreeView.getRoot().getChildren().get(i));
394 }
395 } else if (!paramsTreeView.getRoot().getChildren().get(i).isLeaf()) {
396 if (paramsTreeView.getRoot().getChildren().get(i).getValue().equals(treeItem.getValue())) {
397 paramsTreeView.getRoot().getChildren().remove(paramsTreeView.getRoot().getChildren().get(i));
398 } else {
399 for (int index = 0; index < paramsTreeView.getRoot().getChildren().get(i).getChildren().size(); index++) {
400 if (paramsTreeView.getRoot().getChildren().get(i).getChildren().get(index).getValue().equals(treeItem.getValue())) {
401 paramsTreeView.getRoot().getChildren().get(i).getChildren().remove(paramsTreeView.getRoot().getChildren().get(i).getChildren().get(index));
402 }
403 }
404 }
405 }
406 }
407
408 }
409
410 public static class ParamsAttribute {
411 private final SimpleStringProperty Attributes;
412 private final SimpleStringProperty Values;
413
414 private ParamsAttribute(String attrib, String val) {
415 this.Attributes = new SimpleStringProperty(attrib);
416 this.Values = new SimpleStringProperty(val);
417 }
418
419 public String getAttribute() {
420 return Attributes.get();
421 }
422
423 public void setAttribute(String attrib) {
424 Attributes.set(attrib);
425 }
426
427 public String getValues() {
428 return Values.get();
429 }
430
431 public void setValues(String val) {
432 Values.set(val);
433 }
434 }
435}