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 ;
3132import javafx .scene .control .TableColumn ;
3233import javafx .scene .control .TableView ;
3334import javafx .scene .control .Tooltip ;
35+ import javafx .scene .control .cell .ComboBoxTableCell ;
3436import javafx .scene .control .cell .TextFieldTableCell ;
3537import javafx .scene .layout .BorderPane ;
3638import javafx .scene .layout .VBox ;
4648@ SuppressWarnings ("nls" )
4749public class TitleDetailDelayTable extends BorderPane
4850{
51+ private enum Option_d {
52+ mailto , cmd
53+ };
54+
4955 private final ObservableList <TitleDetailDelay > items = FXCollections .observableArrayList ();
5056
5157 private final TableView <TitleDetailDelay > table = new TableView <>(items );
@@ -139,26 +145,49 @@ private void createTable()
139145 table .getColumns ().add (col );
140146
141147 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 );
148+ col .setSortable (false );
149+ table .getColumns ().add (col );
149150
151+ // Use a combo box to specified the action
152+ TableColumn <TitleDetailDelay , Option_d > tmpOptionCol = new TableColumn <>("Option" );
153+ tmpOptionCol .setCellFactory (ComboBoxTableCell .forTableColumn (Option_d .values ()));
154+ tmpOptionCol
155+ .setCellValueFactory (cell -> new SimpleObjectProperty <Option_d >(getOptionFromDetail (cell .getValue ())));
156+ tmpOptionCol .setOnEditCommit (edit -> {
157+ final int row = edit .getTablePosition ().getRow ();
158+ TitleDetailDelay tmpT = items .get (row );
159+ Option_d option = edit .getNewValue ();
160+ TitleDetailDelay newTitleDetailDelay = setOptionToDetail (tmpT , option );
161+ items .set (row , newTitleDetailDelay );
150162 // 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 );
163+ if (newTitleDetailDelay .hasDelay ())
164+ UpdateThrottle .TIMER .schedule (() -> Platform .runLater (() -> {
165+ table .getSelectionModel ().clearAndSelect (row );
166+ table .edit (row , table .getColumns ().get (2 ));
167+ }), 200 , TimeUnit .MILLISECONDS );
159168 });
160- col .setSortable (false );
161- table .getColumns ().add (col );
169+ tmpOptionCol .setEditable (true );
170+ col .getColumns ().add (tmpOptionCol );
171+
172+ // Use a textfield to set info for detail
173+ TableColumn <TitleDetailDelay , String > infoCol = new TableColumn <>("Info" );
174+ infoCol .setCellValueFactory (cell -> new SimpleStringProperty (getInfoFromDetail (cell .getValue ())));
175+ infoCol .setCellFactory (column -> new TextFieldTableCell <>(new DefaultStringConverter ()));
176+ infoCol .setOnEditCommit (event -> {
177+ final int row = event .getTablePosition ().getRow ();
178+ TitleDetailDelay tmpT = items .get (row );
179+ String newInfo = event .getNewValue ();
180+ TitleDetailDelay newTitleDetailDelay = setInfoToDetail (tmpT , newInfo );
181+ items .set (row , newTitleDetailDelay );
182+ // Trigger editing the delay.
183+ if (newTitleDetailDelay .hasDelay ())
184+ UpdateThrottle .TIMER .schedule (() -> Platform .runLater (() -> {
185+ table .getSelectionModel ().clearAndSelect (row );
186+ table .edit (row , table .getColumns ().get (2 ));
187+ }), 200 , TimeUnit .MILLISECONDS );
188+ });
189+ infoCol .setSortable (false );
190+ col .getColumns ().add (infoCol );
162191
163192 TableColumn <TitleDetailDelay , Integer > delayCol = new TableColumn <>("Delay" );
164193 delayCol .setCellValueFactory (cell -> new SimpleIntegerProperty (cell .getValue ().delay ).asObject ());
@@ -172,6 +201,72 @@ private void createTable()
172201 table .getColumns ().add (delayCol );
173202 }
174203
204+ /**
205+ * This function extract the command option from detail "option:info"
206+ *
207+ * @param titleDetailDelay
208+ * @return enum Option_d either mailto or cmd
209+ */
210+ private Option_d getOptionFromDetail (TitleDetailDelay titleDetailDelay ) {
211+ Option_d option = null ;
212+ String detail = titleDetailDelay != null ? titleDetailDelay .detail : null ;
213+ String [] split = detail != null ? detail .split (":" ) : null ;
214+ String optionString = split != null && split .length > 0 ? split [0 ] : null ;
215+ try {
216+ option = optionString != null ? Option_d .valueOf (optionString ) : Option_d .mailto ;
217+ } catch (Exception e ) {
218+ option = Option_d .mailto ;
219+ }
220+ return option ;
221+ }
222+
223+ /**
224+ * This function extract the info from detail "option:info"
225+ *
226+ * @param titleDetailDelay
227+ * @return information eg : mail or command
228+ */
229+ private String getInfoFromDetail (TitleDetailDelay titleDetailDelay ) {
230+ String info = "" ;
231+ String detail = titleDetailDelay != null ? titleDetailDelay .detail : null ;
232+ String [] split = detail != null ? detail .split (":" ) : null ;
233+ info = split != null && split .length > 1 ? split [1 ] : "" ;
234+ return info ;
235+ }
236+
237+ /**
238+ * Create a new TitleDetailDelay from a given option
239+ * @param titleDetailDelay
240+ * @param option
241+ * @return new TitleDetailDelay
242+ */
243+ private TitleDetailDelay setOptionToDetail (TitleDetailDelay titleDetailDelay , Option_d option ) {
244+ TitleDetailDelay newTitleDetailDelay = titleDetailDelay ;
245+ if (titleDetailDelay != null && option != null ) {
246+ String info = getInfoFromDetail (titleDetailDelay );
247+ String detail = option .toString () + ":" + info ;
248+ newTitleDetailDelay = new TitleDetailDelay (titleDetailDelay .title , detail , titleDetailDelay .delay );
249+ }
250+ return newTitleDetailDelay ;
251+ }
252+
253+ /**
254+ * Create a new TitleDetailDelay from a given info
255+ * @param titleDetailDelay
256+ * @param option
257+ * @return new TitleDetailDelay
258+ */
259+ private TitleDetailDelay setInfoToDetail (TitleDetailDelay titleDetailDelay , String info ) {
260+ TitleDetailDelay newTitleDetailDelay = titleDetailDelay ;
261+ if (titleDetailDelay != null && info != null ) {
262+ Option_d option = getOptionFromDetail (titleDetailDelay );
263+ String newInfo = info .replace ("\\ n" , "\n " );
264+ String detail = option .toString () + ":" + newInfo ;
265+ newTitleDetailDelay = new TitleDetailDelay (titleDetailDelay .title , detail , titleDetailDelay .delay );
266+ }
267+ return newTitleDetailDelay ;
268+ }
269+
175270 private void createButtons ()
176271 {
177272 add .setTooltip (new Tooltip ("Add a new table item." ));
0 commit comments