@@ -522,23 +522,23 @@ public QualifiedCaretPosition(int formattedtextindex, int localcaretindex, Forma
522522
523523 }
524524
525- private QualifiedCaretPosition getTextAtCaret ( ) {
525+ private QualifiedCaretPosition getTextAtTextFlowPosition ( int textflowposition ) {
526526 int currenttextlayoutcharacter = 0 ;
527527 int currenttextindex = 0 ;
528528 FormattedText selectedtext = textlist .get (0 );
529- logger .finest ("-------------------------- getting qualified carret with selection = " + selectionintextflow
529+ logger .finest ("-------------------------- getting qualified carret with selection = " + textflowposition
530530 + "-----------------------" );
531- while ((currenttextlayoutcharacter < selectionintextflow ) && (currenttextindex < textlist .size ())) {
531+ while ((currenttextlayoutcharacter < textflowposition ) && (currenttextindex < textlist .size ())) {
532532 selectedtext = textlist .get (currenttextindex );
533533 currenttextlayoutcharacter += selectedtext .getTextPayload ().length ();
534534 logger .finest ("reviewing FormattedText " + currenttextindex + ", length = "
535535 + selectedtext .getTextPayload ().length () + ", total length = " + currenttextlayoutcharacter
536- + ", caretselection = " + selectionintextflow );
536+ + ", caretselection = " + textflowposition );
537537 currenttextindex ++;
538538 }
539- if (selectionintextflow == currenttextlayoutcharacter ) {
539+ if (textflowposition == currenttextlayoutcharacter ) {
540540 logger .finest ("found end of FormattedText , text length = " + selectedtext .getTextPayload ().length ()
541- + " selection in flow = " + selectionintextflow + ", current text layout = "
541+ + " selection in flow = " + textflowposition + ", current text layout = "
542542 + currenttextlayoutcharacter );
543543
544544 // just at the end of text, adding all elements of size 0
@@ -552,15 +552,18 @@ private QualifiedCaretPosition getTextAtCaret() {
552552 }
553553 }
554554 logger .finest ("-------------------------- qualified carret end (" + (currenttextindex - 1 ) + ","
555- + (selectionintextflow - currenttextlayoutcharacter + selectedtext .getTextPayload ().length ()) + ","
555+ + (textflowposition - currenttextlayoutcharacter + selectedtext .getTextPayload ().length ()) + ","
556556 + selectedtext .getTextPayload () + ")-----------------------" );
557557 // special case for first text of first paragraph, where it was adding letter at
558558 // the end.
559559 if (currenttextindex == 0 )
560560 return new QualifiedCaretPosition (0 , 0 , selectedtext );
561561 return new QualifiedCaretPosition (currenttextindex - 1 ,
562- selectionintextflow - currenttextlayoutcharacter + selectedtext .getTextPayload ().length (),
563- selectedtext );
562+ textflowposition - currenttextlayoutcharacter + selectedtext .getTextPayload ().length (), selectedtext );
563+ }
564+
565+ private QualifiedCaretPosition getTextAtCaret () {
566+ return getTextAtTextFlowPosition (selectionintextflow );
564567 }
565568
566569 /**
@@ -874,7 +877,6 @@ public void changed(ObservableValue<? extends Boolean> property, Boolean oldvalu
874877 public void handle (MouseEvent event ) {
875878 double x = event .getScreenX ();
876879 double y = event .getScreenY ();
877-
878880 selectionintextflow = thisparagraph .getCharSelectionOnCoordinates (x , y );
879881 textflow .requestFocus ();
880882
@@ -1185,6 +1187,7 @@ protected void hideSelectionAndResetIndex() {
11851187 hideSelection ();
11861188 this .dragstartindex = -1 ;
11871189 this .dragendindex = -1 ;
1190+ this .dragactive = false ;
11881191
11891192 }
11901193
@@ -1282,63 +1285,154 @@ public void dirtyhackcaretminus() {
12821285 displayCaretAt (selectionintextflow );
12831286 }
12841287
1285- private FormattedText insertSectionAtCaretIfRequired () {
1288+ private FormattedText insertSectionAtSelectionIfRequired () {
1289+ QualifiedCaretPosition startposition = this .getTextAtTextFlowPosition (dragstartindex );
1290+ QualifiedCaretPosition endposition = this .getTextAtTextFlowPosition (dragendindex );
1291+ FormattedText uniquetext = null ;
1292+ if (startposition .localcaretindex == startposition .selectedtext .getTextPayload ().length ())
1293+ if (this .textlist .size () > startposition .formattedtextindex + 1 ) {
1294+ QualifiedCaretPosition newstartposition = new QualifiedCaretPosition (
1295+ startposition .formattedtextindex + 1 , 0 ,
1296+ this .textlist .get (startposition .formattedtextindex + 1 ));
1297+ startposition = newstartposition ;
1298+ }
12861299
1287- QualifiedCaretPosition currentposition = this .getTextAtCaret ();
1300+ if (startposition .selectedtext == endposition .selectedtext )
1301+ uniquetext = startposition .selectedtext ;
1302+
1303+ if (uniquetext != null ) {
1304+
1305+ if (startposition .localcaretindex == 0 ) {
1306+ // selection starts at beginning of section
1307+ if (endposition .localcaretindex == uniquetext .getTextPayload ().length ()) {
1308+ // return current, full section is selected
1309+ return uniquetext ;
1310+ } else {
1311+ // split in 2 and return the first section
1312+ String starttext = uniquetext .getTextPayload ().substring (0 , endposition .localcaretindex );
1313+ String endtext = uniquetext .getTextPayload ().substring (endposition .localcaretindex );
1314+ uniquetext .setString (starttext );
1315+ FormattedText end = new FormattedText (uniquetext , this );
1316+ end .setString (endtext );
1317+ textlist .add (startposition .formattedtextindex + 1 , end );
1318+ textflow .getChildren ().add (startposition .formattedtextindex + 1 , end .getNode ());
1319+ return uniquetext ;
1320+ }
1321+ } else {
1322+ // selection starts at middle of section
1323+ if (endposition .localcaretindex == uniquetext .getTextPayload ().length ()) {
1324+ // split in 2 and return the second section
1325+ String starttext = uniquetext .getTextPayload ().substring (0 , startposition .localcaretindex );
1326+ String endtext = uniquetext .getTextPayload ().substring (startposition .localcaretindex );
1327+ uniquetext .setString (starttext );
1328+ FormattedText end = new FormattedText (uniquetext , this );
1329+ end .setString (endtext );
1330+ textlist .add (startposition .formattedtextindex + 1 , end );
1331+ textflow .getChildren ().add (startposition .formattedtextindex + 1 , end .getNode ());
1332+ return end ;
1333+ } else {
1334+ String starttext = uniquetext .getTextPayload ().substring (0 , startposition .localcaretindex );
1335+ String middletext = uniquetext .getTextPayload ().substring (startposition .localcaretindex ,
1336+ endposition .localcaretindex );
1337+ String endtext = uniquetext .getTextPayload ().substring (endposition .localcaretindex );
1338+ uniquetext .setString (starttext );
1339+ FormattedText middle = new FormattedText (uniquetext , this );
1340+ FormattedText end = new FormattedText (uniquetext , this );
1341+ middle .setString (middletext );
1342+ end .setString (endtext );
1343+ textlist .add (startposition .formattedtextindex + 1 , middle );
1344+ textflow .getChildren ().add (startposition .formattedtextindex + 1 , middle .getNode ());
1345+ textlist .add (startposition .formattedtextindex + 2 , end );
1346+ textflow .getChildren ().add (startposition .formattedtextindex + 2 , end .getNode ());
1347+ return middle ;
1348+ }
1349+ }
1350+ } else {
1351+ return null ;
1352+ }
1353+ }
1354+
1355+ private FormattedText insertSectionAtCaretPosition (QualifiedCaretPosition position ) {
12881356 // if already created a zero length text, stick on it.
1289- if (currentposition .selectedtext .getTextPayload ().length () == 0 ) {
1357+ if (position .selectedtext .getTextPayload ().length () == 0 ) {
12901358 logger .finer ("current text has zero length, do not modify it" );
1291- return currentposition .selectedtext ;
1359+ return position .selectedtext ;
12921360 }
12931361 // longer text 3 cases to mange
12941362 // at beginning of section, just insert text before
1295- if (currentposition .localcaretindex == 0 ) {
1363+ if (position .localcaretindex == 0 ) {
12961364 logger .finer ("new text will be inserted at the beginning of current text" );
12971365
1298- FormattedText formattedtext = new FormattedText (currentposition .selectedtext , this );
1299- textlist .add (currentposition .formattedtextindex , formattedtext );
1300- textflow .getChildren ().add (currentposition .formattedtextindex , formattedtext .getNode ());
1366+ FormattedText formattedtext = new FormattedText (position .selectedtext , this );
1367+ textlist .add (position .formattedtextindex , formattedtext );
1368+ textflow .getChildren ().add (position .formattedtextindex , formattedtext .getNode ());
13011369 return formattedtext ;
13021370 }
13031371 // at end of section, insert text at the end (case last text last char of
13041372 // paragraph
1305- if (currentposition .localcaretindex == currentposition .selectedtext .getTextPayload ().length ()) {
1373+ if (position .localcaretindex == position .selectedtext .getTextPayload ().length ()) {
13061374 logger .finer ("new text will be inserted at the end of current text" );
1307- FormattedText formattedtext = new FormattedText (currentposition .selectedtext , this );
1308- textlist .add (currentposition .formattedtextindex + 1 , formattedtext );
1309- textflow .getChildren ().add (currentposition .formattedtextindex + 1 , formattedtext .getNode ());
1375+ FormattedText formattedtext = new FormattedText (position .selectedtext , this );
1376+ textlist .add (position .formattedtextindex + 1 , formattedtext );
1377+ textflow .getChildren ().add (position .formattedtextindex + 1 , formattedtext .getNode ());
13101378 return formattedtext ;
13111379 }
13121380
13131381 // at middle of section - step 1 - add new text
1314- FormattedText newformattedtext = new FormattedText (currentposition .selectedtext , this );
1382+ FormattedText newformattedtext = new FormattedText (position .selectedtext , this );
13151383 newformattedtext .setString ("" );
1316- textlist .add (currentposition .formattedtextindex + 1 , newformattedtext );
1317- textflow .getChildren ().add (currentposition .formattedtextindex + 1 , newformattedtext .getNode ());
1384+ textlist .add (position .formattedtextindex + 1 , newformattedtext );
1385+ textflow .getChildren ().add (position .formattedtextindex + 1 , newformattedtext .getNode ());
13181386 // at middle of section - step 2 - reduce current text
1319- String beginning = currentposition .selectedtext .getTextPayload ().substring (0 , currentposition .localcaretindex );
1320- String end = currentposition .selectedtext .getTextPayload ().substring (currentposition .localcaretindex );
1321- currentposition .selectedtext .setString (beginning );
1387+ String beginning = position .selectedtext .getTextPayload ().substring (0 , position .localcaretindex );
1388+ String end = position .selectedtext .getTextPayload ().substring (position .localcaretindex );
1389+ position .selectedtext .setString (beginning );
13221390 // at middle of section - step 3 - add reminder of existing text
1323- FormattedText endofcurrentext = new FormattedText (currentposition .selectedtext , this );
1391+ FormattedText endofcurrentext = new FormattedText (position .selectedtext , this );
13241392 endofcurrentext .setString (end );
13251393 logger .finer ("splitting current text in 2 and inserting in the middle [" + beginning + "|" + end + "]" );
1326- textlist .add (currentposition .formattedtextindex + 2 , endofcurrentext );
1327- textflow .getChildren ().add (currentposition .formattedtextindex + 2 , endofcurrentext .getNode ());
1394+ textlist .add (position .formattedtextindex + 2 , endofcurrentext );
1395+ textflow .getChildren ().add (position .formattedtextindex + 2 , endofcurrentext .getNode ());
13281396 textflow .requestLayout ();
13291397 dropDescription ();
13301398 return newformattedtext ;
13311399 }
13321400
1401+ private FormattedText insertSectionAtCaretIfRequired () {
1402+
1403+ QualifiedCaretPosition currentposition = this .getTextAtCaret ();
1404+ return insertSectionAtCaretPosition (currentposition );
1405+ }
1406+
13331407 /**
13341408 * inserts a color indicator at the current caret position
13351409 *
13361410 * @param value color indicator
13371411 */
13381412 public void insertColorIndicator (Color value ) {
1339- FormattedText relevanttext = insertSectionAtCaretIfRequired ();
1340- relevanttext .setSpecialcolor (value );
1413+ if (this .dragactive ) {
1414+ if (this .dragstartindex > 0 )
1415+ if (this .dragendindex > 0 )
1416+ if (this .dragendindex > this .dragstartindex ) {
1417+ FormattedText relevanttext = insertSectionAtSelectionIfRequired ();
1418+ if (relevanttext != null )
1419+ relevanttext .setSpecialcolor (value );
1420+ Platform .runLater (new Runnable () {
1421+ @ Override
1422+ public void run () {
1423+ try {
1424+ Thread .sleep (5 );
1425+ } catch (Exception e ) {
1426+ }
1427+ displaySelection ();
13411428
1429+ }
1430+ });
1431+ }
1432+ } else {
1433+ FormattedText relevanttext = insertSectionAtCaretIfRequired ();
1434+ relevanttext .setSpecialcolor (value );
1435+ }
13421436 }
13431437
13441438 /**
@@ -1347,8 +1441,30 @@ public void insertColorIndicator(Color value) {
13471441 * @param selected true to put bold, false, to put back to normal
13481442 */
13491443 public void insertBoldIndicator (boolean selected ) {
1350- FormattedText relevanttext = insertSectionAtCaretIfRequired ();
1351- relevanttext .setBold (selected );
1444+ if (this .dragactive ) {
1445+ if (this .dragstartindex > 0 )
1446+ if (this .dragendindex > 0 )
1447+ if (this .dragendindex > this .dragstartindex ) {
1448+ FormattedText relevanttext = insertSectionAtSelectionIfRequired ();
1449+ if (relevanttext != null )
1450+ relevanttext .setBold (!relevanttext .isBold ());
1451+ Platform .runLater (new Runnable () {
1452+ @ Override
1453+ public void run () {
1454+ try {
1455+ Thread .sleep (5 );
1456+ } catch (Exception e ) {
1457+ }
1458+ displaySelection ();
1459+
1460+ }
1461+ });
1462+ }
1463+ } else {
1464+ FormattedText relevanttext = insertSectionAtCaretIfRequired ();
1465+ relevanttext .setBold (selected );
1466+
1467+ }
13521468 }
13531469
13541470 /**
@@ -1358,8 +1474,29 @@ public void insertBoldIndicator(boolean selected) {
13581474 * section
13591475 */
13601476 public void insertItalicIndicator (boolean selected ) {
1361- FormattedText relevanttext = insertSectionAtCaretIfRequired ();
1362- relevanttext .setItalic (selected );
1477+ if (this .dragactive ) {
1478+ if (this .dragstartindex > 0 )
1479+ if (this .dragendindex > 0 )
1480+ if (this .dragendindex > this .dragstartindex ) {
1481+ FormattedText relevanttext = insertSectionAtSelectionIfRequired ();
1482+ if (relevanttext != null )
1483+ relevanttext .setItalic (!relevanttext .isItalic ());
1484+ Platform .runLater (new Runnable () {
1485+ @ Override
1486+ public void run () {
1487+ try {
1488+ Thread .sleep (5 );
1489+ } catch (Exception e ) {
1490+ }
1491+ displaySelection ();
1492+
1493+ }
1494+ });
1495+ }
1496+ } else {
1497+ FormattedText relevanttext = insertSectionAtCaretIfRequired ();
1498+ relevanttext .setItalic (selected );
1499+ }
13631500 }
13641501
13651502 /**
0 commit comments