2121import javafx .application .Platform ;
2222import javafx .beans .InvalidationListener ;
2323import javafx .beans .property .SimpleIntegerProperty ;
24+ import javafx .beans .property .SimpleObjectProperty ;
2425import javafx .beans .property .SimpleStringProperty ;
2526import javafx .collections .FXCollections ;
2627import javafx .collections .ObservableList ;
2728import javafx .geometry .Insets ;
2829import javafx .scene .control .Button ;
2930import javafx .scene .control .Label ;
3031import javafx .scene .control .SelectionMode ;
32+ import javafx .scene .control .Spinner ;
33+ import javafx .scene .control .TableCell ;
3134import javafx .scene .control .TableColumn ;
3235import javafx .scene .control .TableView ;
3336import javafx .scene .control .Tooltip ;
37+ import javafx .scene .control .cell .ComboBoxTableCell ;
3438import javafx .scene .control .cell .TextFieldTableCell ;
3539import javafx .scene .layout .BorderPane ;
3640import javafx .scene .layout .VBox ;
37- import javafx .scene .paint .Color ;
3841import javafx .util .converter .DefaultStringConverter ;
39- import javafx . util . converter . IntegerStringConverter ;
42+
4043
4144/** Table for editing list of {@link TitleDetailDelay}
4245 *
4649@ SuppressWarnings ("nls" )
4750public class TitleDetailDelayTable extends BorderPane
4851{
52+ private enum Option_d {
53+ mailto , cmd , sevrpv
54+ };
55+
4956 private final ObservableList <TitleDetailDelay > items = FXCollections .observableArrayList ();
5057
5158 private final TableView <TitleDetailDelay > table = new TableView <>(items );
@@ -82,11 +89,15 @@ public List<TitleDetailDelay> getItems()
8289 /** Table cell for 'delay'
8390 * Disables for actions that don't use the delay
8491 */
85- class DelayTableCell extends TextFieldTableCell <TitleDetailDelay , Integer >
92+ class DelayTableCell extends TableCell <TitleDetailDelay , Integer >
8693 {
94+ private final Spinner <Integer > spinner ;
95+
8796 public DelayTableCell ()
8897 {
89- super (new IntegerStringConverter ());
98+ this .spinner = new Spinner <>(0 , 10000 , 1 );
99+ spinner .setEditable (true );
100+ this .spinner .valueProperty ().addListener ((observable , oldValue , newValue ) -> commitEdit (newValue ));
90101 }
91102
92103 @ Override
@@ -96,18 +107,26 @@ public void updateItem(Integer item, boolean empty)
96107
97108 if (empty ||
98109 getTableRow () == null ||
99- getTableRow ().getItem () == null )
110+ getTableRow ().getItem () == null ||
111+ spinner == null ) {
112+ setGraphic (null );
100113 return ;
114+ }
101115 if (getTableRow ().getItem ().hasDelay ())
102116 {
103- setDisable (false );
104- setTextFill (Color .BLACK );
117+ spinner .setDisable (false );
118+ spinner .getEditor ().setStyle ("-fx-text-inner-color: black;" );
119+ //spinner.getEditor().setTextFill(Color.BLACK);
105120 }
106121 else
107122 {
108- setDisable (true );
109- setTextFill (Color .LIGHTGRAY );
123+ spinner .setDisable (true );
124+ spinner .getEditor ().setStyle ("-fx-text-inner-color: lightgray;" );
125+ //spinner.getEditor().setTextFill(Color.LIGHTGRAY);
110126 }
127+
128+ this .spinner .getValueFactory ().setValue (item );
129+ setGraphic (spinner );
111130 }
112131 }
113132
@@ -139,26 +158,49 @@ private void createTable()
139158 table .getColumns ().add (col );
140159
141160 col = new TableColumn <>("Detail" );
142- col .setCellValueFactory (cell -> new SimpleStringProperty (cell .getValue ().detail .replace ("\n " , "\\ n" )));
143- col .setCellFactory (column -> new TextFieldTableCell <>(new DefaultStringConverter ()));
144- col .setOnEditCommit (event ->
145- {
146- final int row = event .getTablePosition ().getRow ();
147- final TitleDetailDelay item = new TitleDetailDelay (items .get (row ).title , event .getNewValue ().replace ("\\ n" , "\n " ), items .get (row ).delay );
148- items .set (row , item );
161+ col .setSortable (false );
162+ table .getColumns ().add (col );
149163
164+ // Use a combo box to specified the action
165+ TableColumn <TitleDetailDelay , Option_d > tmpOptionCol = new TableColumn <>("Option" );
166+ tmpOptionCol .setCellFactory (ComboBoxTableCell .forTableColumn (Option_d .values ()));
167+ tmpOptionCol
168+ .setCellValueFactory (cell -> new SimpleObjectProperty <Option_d >(getOptionFromDetail (cell .getValue ())));
169+ tmpOptionCol .setOnEditCommit (edit -> {
170+ final int row = edit .getTablePosition ().getRow ();
171+ TitleDetailDelay tmpT = items .get (row );
172+ Option_d option = edit .getNewValue ();
173+ TitleDetailDelay newTitleDetailDelay = setOptionToDetail (tmpT , option );
174+ items .set (row , newTitleDetailDelay );
150175 // Trigger editing the delay.
151- if (item .hasDelay ())
152- UpdateThrottle .TIMER .schedule (() ->
153- Platform .runLater (() ->
154- {
155- table .getSelectionModel ().clearAndSelect (row );
156- table .edit (row , table .getColumns ().get (2 ));
157- }),
158- 200 , TimeUnit .MILLISECONDS );
176+ if (newTitleDetailDelay .hasDelay ())
177+ UpdateThrottle .TIMER .schedule (() -> Platform .runLater (() -> {
178+ table .getSelectionModel ().clearAndSelect (row );
179+ table .edit (row , table .getColumns ().get (2 ));
180+ }), 200 , TimeUnit .MILLISECONDS );
159181 });
160- col .setSortable (false );
161- table .getColumns ().add (col );
182+ tmpOptionCol .setEditable (true );
183+ col .getColumns ().add (tmpOptionCol );
184+
185+ // Use a textfield to set info for detail
186+ TableColumn <TitleDetailDelay , String > infoCol = new TableColumn <>("Info" );
187+ infoCol .setCellValueFactory (cell -> new SimpleStringProperty (getInfoFromDetail (cell .getValue ())));
188+ infoCol .setCellFactory (column -> new TextFieldTableCell <>(new DefaultStringConverter ()));
189+ infoCol .setOnEditCommit (event -> {
190+ final int row = event .getTablePosition ().getRow ();
191+ TitleDetailDelay tmpT = items .get (row );
192+ String newInfo = event .getNewValue ();
193+ TitleDetailDelay newTitleDetailDelay = setInfoToDetail (tmpT , newInfo );
194+ items .set (row , newTitleDetailDelay );
195+ // Trigger editing the delay.
196+ if (newTitleDetailDelay .hasDelay ())
197+ UpdateThrottle .TIMER .schedule (() -> Platform .runLater (() -> {
198+ table .getSelectionModel ().clearAndSelect (row );
199+ table .edit (row , table .getColumns ().get (2 ));
200+ }), 200 , TimeUnit .MILLISECONDS );
201+ });
202+ infoCol .setSortable (false );
203+ col .getColumns ().add (infoCol );
162204
163205 TableColumn <TitleDetailDelay , Integer > delayCol = new TableColumn <>("Delay" );
164206 delayCol .setCellValueFactory (cell -> new SimpleIntegerProperty (cell .getValue ().delay ).asObject ());
@@ -172,6 +214,72 @@ private void createTable()
172214 table .getColumns ().add (delayCol );
173215 }
174216
217+ /**
218+ * This function extract the command option from detail "option:info"
219+ *
220+ * @param titleDetailDelay
221+ * @return enum Option_d either mailto or cmd
222+ */
223+ private Option_d getOptionFromDetail (TitleDetailDelay titleDetailDelay ) {
224+ Option_d option = null ;
225+ String detail = titleDetailDelay != null ? titleDetailDelay .detail : null ;
226+ String [] split = detail != null ? detail .split (":" ) : null ;
227+ String optionString = split != null && split .length > 0 ? split [0 ] : null ;
228+ try {
229+ option = optionString != null ? Option_d .valueOf (optionString ) : Option_d .mailto ;
230+ } catch (Exception e ) {
231+ option = Option_d .mailto ;
232+ }
233+ return option ;
234+ }
235+
236+ /**
237+ * This function extract the info from detail "option:info"
238+ *
239+ * @param titleDetailDelay
240+ * @return information eg : mail or command
241+ */
242+ private String getInfoFromDetail (TitleDetailDelay titleDetailDelay ) {
243+ String info = "" ;
244+ String detail = titleDetailDelay != null ? titleDetailDelay .detail : null ;
245+ String [] split = detail != null ? detail .split (":" ) : null ;
246+ info = split != null && split .length > 1 ? split [1 ] : "" ;
247+ return info ;
248+ }
249+
250+ /**
251+ * Create a new TitleDetailDelay from a given option
252+ * @param titleDetailDelay
253+ * @param option
254+ * @return new TitleDetailDelay
255+ */
256+ private TitleDetailDelay setOptionToDetail (TitleDetailDelay titleDetailDelay , Option_d option ) {
257+ TitleDetailDelay newTitleDetailDelay = titleDetailDelay ;
258+ if (titleDetailDelay != null && option != null ) {
259+ String info = getInfoFromDetail (titleDetailDelay );
260+ String detail = option .toString () + ":" + info ;
261+ newTitleDetailDelay = new TitleDetailDelay (titleDetailDelay .title , detail , titleDetailDelay .delay );
262+ }
263+ return newTitleDetailDelay ;
264+ }
265+
266+ /**
267+ * Create a new TitleDetailDelay from a given info
268+ * @param titleDetailDelay
269+ * @param option
270+ * @return new TitleDetailDelay
271+ */
272+ private TitleDetailDelay setInfoToDetail (TitleDetailDelay titleDetailDelay , String info ) {
273+ TitleDetailDelay newTitleDetailDelay = titleDetailDelay ;
274+ if (titleDetailDelay != null && info != null ) {
275+ Option_d option = getOptionFromDetail (titleDetailDelay );
276+ String newInfo = info .replace ("\\ n" , "\n " );
277+ String detail = option .toString () + ":" + newInfo ;
278+ newTitleDetailDelay = new TitleDetailDelay (titleDetailDelay .title , detail , titleDetailDelay .delay );
279+ }
280+ return newTitleDetailDelay ;
281+ }
282+
175283 private void createButtons ()
176284 {
177285 add .setTooltip (new Tooltip ("Add a new table item." ));
0 commit comments