Changeset 29037 for main


Ignore:
Timestamp:
2014-05-05T17:52:52+12:00 (10 years ago)
Author:
ak19
Message:

Uses the new separate NumberedJTextArea class that has its own undo and redo buttons that do the same as pressing Ctrl-Z and Ctrl-Y in the RSyntaxArea textarea. Until now the Undo and Redo buttons only did undo/redo a single character at a time, whereas the shortcuts belonged to RSyntaxArea and would perform compound edits.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/trunk/gli/src/org/greenstone/gatherer/cdm/Format4gs3Manager.java

    r28995 r29037  
    4242import org.greenstone.gatherer.gui.GLIButton;
    4343import org.greenstone.gatherer.gui.FormatPane;
     44import org.greenstone.gatherer.gui.NumberedJTextArea;
    4445import org.greenstone.gatherer.metadata.MetadataElement;
    4546import org.greenstone.gatherer.metadata.MetadataSetManager;
     
    403404        private JButton remove_button;
    404405        private JButton default_button;
    405         private JButton undo_button;
    406         private JButton redo_button;
    407406        private JComboBox feature_combobox;
    408407        private JList format_list;
     
    412411        private JPanel selection_pane;
    413412        private final Dimension FIELD_SIZE = new Dimension(200, 30);
    414         private final UndoManager undo = new UndoManager();
    415413        private boolean newtext = true;
    416414        private Format4gs3 previousFormat = null;
     
    440438            JPanel editor_pane = new JPanel();
    441439
    442             editor_textarea = new NumberedJTextArea();
    443 
    444             /* Fields specific to RSyntaxQuery inherited class */
    445             editor_textarea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_XML);
    446             editor_textarea.setBracketMatchingEnabled(true);
    447             editor_textarea.setAnimateBracketMatching(true);
    448             editor_textarea.setAntiAliasingEnabled(true);
    449             editor_textarea.setAutoIndentEnabled(true);
    450             editor_textarea.setPaintMarkOccurrencesBorder(false);
    451 
    452             /* Standard fields to JTextArea */
    453             editor_textarea.setOpaque(false);
    454             editor_textarea.setBackground(Configuration.getColor("coloring.editable_background", false));
    455             editor_textarea.setCaretPosition(0);
    456             editor_textarea.setLineWrap(true);
    457             editor_textarea.setRows(11);
    458             editor_textarea.setWrapStyleWord(false);
    459             editor_textarea.setToolTipText(Dictionary.get("CDM.FormatManager.Add_Tooltip"));
     440            // NumberedJTextArea comes with undo and redo buttons already hooked up to listeners
     441            editor_textarea = new NumberedJTextArea(Dictionary.get("CDM.FormatManager.Add_Tooltip"));
    460442
    461443            default_button = new GLIButton(Dictionary.get("CDM.FormatManager.Default"), Dictionary.get("CDM.FormatManager.Default_Tooltip"));
     
    467449            remove_button.setEnabled(false);
    468450
    469             undo_button = new GLIButton(Dictionary.get("General.Undo"), Dictionary.get("General.Undo_Tooltip"));
    470             undo_button.setEnabled(false);
    471 
    472             redo_button = new GLIButton(Dictionary.get("General.Redo"), Dictionary.get("General.Redo_Tooltip"));
    473             redo_button.setEnabled(false);
    474 
    475451            // Connect
    476452            add_button.addActionListener(new AddListener());
    477453            remove_button.addActionListener(new RemoveListener());
    478454            default_button.addActionListener(new DefaultListener());
    479             undo_button.addActionListener(new UndoListener());
    480             redo_button.addActionListener(new RedoListener());
    481455            feature_combobox.addActionListener(new FeatureListener());
    482456            editor_textarea.getDocument().addDocumentListener(new EditorListener());
    483             // Listen for undo and redo events
    484             editor_textarea.getDocument().addUndoableEditListener(new UndoableEditListener()
    485             {
    486                 public void undoableEditHappened(UndoableEditEvent evt)
    487                 {
    488                     undo.addEdit(evt.getEdit());
    489                 }
    490             });
    491457
    492458            format_list.addListSelectionListener(new FormatListListener());
     
    505471            JPanel rupanel = new JPanel();
    506472            rupanel.setLayout(new GridLayout(1, 2));
    507             rupanel.add(undo_button);
    508             rupanel.add(redo_button);
     473            rupanel.add(editor_textarea.undoButton);
     474            rupanel.add(editor_textarea.redoButton);
    509475
    510476            editor_pane.setLayout(new BorderLayout());
     
    651617                if (!newtext)
    652618                {
    653                     undo_button.setEnabled(true);
     619                    editor_textarea.undoButton.setEnabled(true);
    654620                }
    655621
     
    694660            public void actionPerformed(ActionEvent event)
    695661            {
    696                 undo_button.setEnabled(false);
    697                 redo_button.setEnabled(false);
     662                editor_textarea.undoButton.setEnabled(false);
     663                editor_textarea.redoButton.setEnabled(false);
    698664                default_button.setEnabled(true);
    699665                newtext = true;
     
    701667                if (ignore_event == true)
    702668                {
    703                     undo.discardAllEdits();
     669                        editor_textarea.discardAllEdits();
    704670                    return;
    705671                }
     
    768734                }
    769735                ignore_event = false;
    770                 undo.discardAllEdits();
     736                editor_textarea.discardAllEdits();
    771737            }
    772738        }
     
    776742            public void valueChanged(ListSelectionEvent event)
    777743            {
    778                 undo_button.setEnabled(false);
    779                 redo_button.setEnabled(false);
     744                    editor_textarea.undoButton.setEnabled(false);
     745                editor_textarea.redoButton.setEnabled(false);
    780746                default_button.setEnabled(true);
    781747                newtext = true;
     
    829795
    830796                }
    831                 undo.discardAllEdits();
     797                editor_textarea.discardAllEdits();
    832798            }
    833799
     
    888854        }
    889855
    890         private class UndoListener implements ActionListener
    891         {
    892 
    893             public void actionPerformed(ActionEvent event)
    894             {
    895                 try
    896                 {
    897                     if (undo.canUndo())
    898                     {
    899                         int pos = editor_textarea.getCaretPosition();
    900                         redo_button.setEnabled(true);
    901                         undo.undo();
    902                         if (pos > 0)
    903                             editor_textarea.setCaretPosition(pos - 1);
    904                         else
    905                             editor_textarea.setCaretPosition(pos);
    906                     }
    907                     if (!undo.canUndo())
    908                     {
    909                         undo_button.setEnabled(false);
    910                     }
    911                     else
    912                     {
    913                         undo_button.setEnabled(true);
    914                     }
    915 
    916                 }
    917                 catch (Exception e)
    918                 {
    919 
    920                 }
    921             }
    922         }
    923 
    924         private class RedoListener implements ActionListener
    925         {
    926             public void actionPerformed(ActionEvent evt)
    927             {
    928                 try
    929                 {
    930                     if (undo.canRedo())
    931                     {
    932                         int pos = editor_textarea.getCaretPosition();
    933                         undo.redo();
    934                         editor_textarea.setCaretPosition(pos);
    935                     }
    936                     if (!undo.canRedo())
    937                     {
    938                         redo_button.setEnabled(false);
    939                     }
    940                     else
    941                     {
    942                         redo_button.setEnabled(true);
    943                     }
    944 
    945                 }
    946                 catch (Exception e)
    947                 {
    948                 }
    949             }
    950         }
    951 
    952856        private void newFormat()
    953857        {
     
    956860            editor_textarea.setToolTipText(Dictionary.get("CDM.FormatManager.Editor_Disabled_Tooltip"));
    957861
    958             undo_button.setEnabled(false);
    959             redo_button.setEnabled(false);
     862            editor_textarea.undoButton.setEnabled(false);
     863            editor_textarea.redoButton.setEnabled(false);
    960864            add_button.setEnabled(true);
    961865            remove_button.setEnabled(false);
     
    974878            FormatPane.setPreviewButton(true);
    975879        }
    976 
    977         /**
    978          * A textarea with the line number next to each line of the text
    979          */
    980         public class NumberedJTextArea extends RSyntaxTextArea /* JTextArea */
    981         {
    982             public void paintComponent(Graphics g)
    983             {
    984                 Insets insets = getInsets();
    985                 Rectangle rectangle = g.getClipBounds();
    986                 g.setColor(Color.white);
    987                 g.fillRect(rectangle.x, rectangle.y, rectangle.width, rectangle.height);
    988 
    989                 super.paintComponent(g);
    990 
    991                 if (rectangle.x < insets.left)
    992                 {
    993                     FontMetrics font_metrics = g.getFontMetrics();
    994                     int font_height = font_metrics.getHeight();
    995                     int y = font_metrics.getAscent() + insets.top;
    996                     int line_number_start_point = ((rectangle.y + insets.top) / font_height) + 1;
    997                     if (y < rectangle.y)
    998                     {
    999                         y = line_number_start_point * font_height - (font_height - font_metrics.getAscent());
    1000                     }
    1001                     int y_axis_end_point = y + rectangle.height + font_height;
    1002                     int x_axis_start_point = insets.left;
    1003                     x_axis_start_point -= getFontMetrics(getFont()).stringWidth(Math.max(getRows(), getLineCount() + 1) + " ");
    1004                     if (!this.getText().trim().equals(""))
    1005                     {
    1006                         g.setColor(Color.DARK_GRAY);
    1007                     }
    1008                     else
    1009                     {
    1010                         g.setColor(Color.white);
    1011                     }
    1012                     int length = ("" + Math.max(getRows(), getLineCount() + 1)).length();
    1013                     while (y < y_axis_end_point)
    1014                     {
    1015                         g.drawString(line_number_start_point + "  ", x_axis_start_point, y);
    1016                         y += font_height;
    1017                         line_number_start_point++;
    1018                     }
    1019                 }
    1020             }
    1021 
    1022             public Insets getInsets()
    1023             {
    1024                 Insets insets = super.getInsets(new Insets(0, 0, 0, 0));
    1025                 insets.left += getFontMetrics(getFont()).stringWidth(Math.max(getRows(), getLineCount() + 1) + " ");
    1026                 return insets;
    1027             }
    1028         }
    1029880    }
    1030881
Note: See TracChangeset for help on using the changeset viewer.