@@ -73,7 +73,8 @@ public class UI extends JFrame implements ActionListener {
7373 private final JMenuItem newFile , openFile , saveFile , close , cut , copy , paste , clearFile , selectAll , quickFind ,
7474 aboutMe , aboutSoftware , wordWrap ;
7575 private final JToolBar mainToolbar ;
76- JButton newButton , openButton , saveButton , clearButton , quickButton , aboutMeButton , aboutButton , closeButton , boldButton , italicButton ;
76+ JButton newButton , openButton , saveButton , clearButton , quickButton , aboutMeButton , aboutButton , closeButton , boldButton , italicButton ,
77+ indentButton , unindentButton ;
7778 private final Action selectAllAction ;
7879
7980 //setup icons - Bold and Italic
@@ -100,6 +101,10 @@ public class UI extends JFrame implements ActionListener {
100101 // setup icons - Help Menu
101102 private final ImageIcon aboutMeIcon = new ImageIcon (UI .class .getResource ("icons/about_me.png" ));
102103 private final ImageIcon aboutIcon = new ImageIcon (UI .class .getResource ("icons/about.png" ));
104+
105+ // setup icons - Indentation Buttons
106+ private final ImageIcon indentIcon = new ImageIcon (UI .class .getResource ("icons/indent.png" ));
107+ private final ImageIcon unindentIcon = new ImageIcon (UI .class .getResource ("icons/unindent.png" ));
103108
104109 private SupportedKeywords kw = new SupportedKeywords ();
105110 private HighlightText languageHighlighter = new HighlightText (Color .YELLOW );
@@ -392,6 +397,19 @@ public void actionPerformed(ActionEvent ev) {
392397 italicButton .addActionListener (this );
393398 mainToolbar .add (italicButton );
394399 mainToolbar .addSeparator ();
400+
401+ //Initialization of Indentation Buttons
402+ indentButton = new JButton (indentIcon );
403+ indentButton .setToolTipText ("Move Text Right" );
404+ indentButton .addActionListener (this );
405+ mainToolbar .add (indentButton );
406+ mainToolbar .addSeparator ();
407+
408+ unindentButton = new JButton (unindentIcon );
409+ unindentButton .setToolTipText ("Move Text Left" );
410+ unindentButton .addActionListener (this );
411+ mainToolbar .add (unindentButton );
412+ mainToolbar .addSeparator ();
395413 /**
396414 * **************** FONT SETTINGS SECTION **********************
397415 */
@@ -445,7 +463,8 @@ public void actionPerformed(ActionEvent ev) {
445463 });
446464 //FONT SIZE SETTINGS SECTION END
447465 }
448-
466+
467+
449468 @ Override
450469 protected void processWindowEvent (WindowEvent e ) {
451470 if (e .getID () == WindowEvent .WINDOW_CLOSING ) {
@@ -465,6 +484,51 @@ protected void processWindowEvent(WindowEvent e) {
465484 }
466485 }
467486
487+ /*
488+ * Method Indents text by shifting selected block
489+ */
490+ public void indentText () {
491+ int front = textArea .getSelectionStart ();
492+ int back = textArea .getSelectionEnd ();
493+
494+ if (front != back ) {
495+ String selected = textArea .getText ().substring (front ,back );
496+ String indented = "\t " + selected .replace ("\n " , "\n \t " );
497+ textArea .replaceRange (indented , front , back );
498+ }
499+
500+ }
501+
502+ /*
503+ * Undoes previous indentation from selected text block
504+ */
505+ public void unindentText () {
506+ int front = textArea .getSelectionStart ();
507+ int back = textArea .getSelectionEnd ();
508+
509+ if (front != back ) {
510+ String selected = textArea .getText ().substring (front ,back );
511+ StringBuilder unindented = new StringBuilder ();
512+
513+ String [] lines = selected .split ("\n " );
514+ for (int i = 0 ; i < lines .length ; i ++) {
515+ String line = lines [i ];
516+ if (line .startsWith ("\t " )) {
517+ unindented .append (line .substring (1 ));
518+ }else if (line .startsWith (" " )){
519+ unindented .append (line .substring (4 ));
520+ }else {
521+ unindented .append (line );
522+ }
523+ if (i < lines .length - 1 ) {
524+ unindented .append ("\n " );
525+ }
526+ }
527+ textArea .replaceRange (unindented .toString (), front , back );
528+ }
529+ }
530+
531+
468532 private void highlightKeywords () {
469533 SwingUtilities .invokeLater (() -> {
470534 languageHighlighter .removeHighlights (textArea );
@@ -597,6 +661,10 @@ else if (e.getSource() == italicButton) {
597661 } else {
598662 textArea .setFont (textArea .getFont ().deriveFont (Font .ITALIC ));
599663 }
664+ } else if (e .getSource () == indentButton ) {
665+ indentText ();
666+ }else if (e .getSource () == unindentButton ) {
667+ unindentText ();
600668 }
601669 // Clear File (Code)
602670 if (e .getSource () == clearFile || e .getSource () == clearButton ) {
@@ -760,4 +828,4 @@ public void autoSave(JTextArea textArea) {
760828
761829 };
762830
763- }
831+ }
0 commit comments