Changeset 29031

Show
Ignore:
Timestamp:
02.05.2014 20:11:21 (5 years ago)
Author:
ak19
Message:

Moved out the NumberedJTextArea into its own class. It now manages the associated Undo and RedoButtons? itself, so that the undo/redobutton code and their listeners are also moved out of FormatConversionDialog?. Tested that it all still works, including bugfix to previous commit by overrding undo/redoLastAction() in NumberedJTextArea.java. 2. Also, cleaned up import statements, minor change to comments in the StreamGobbler? classes, added the license text at the top of all these new classes.

Location:
main/trunk/gli/src/org/greenstone/gatherer
Files:
1 added
3 modified

Legend:

Unmodified
Added
Removed
  • main/trunk/gli/src/org/greenstone/gatherer/gui/FormatConversionDialog.java

    r29030 r29031  
     1/** 
     2 *######################################################################### 
     3 * 
     4 * A component of the Gatherer application, part of the Greenstone digital 
     5 * library suite from the New Zealand Digital Library Project at the 
     6 * University of Waikato, New Zealand. 
     7 * 
     8 * Copyright (C) 1999 New Zealand Digital Library Project 
     9 * 
     10 * This program is free software; you can redistribute it and/or modify 
     11 * it under the terms of the GNU General Public License as published by 
     12 * the Free Software Foundation; either version 2 of the License, or 
     13 * (at your option) any later version. 
     14 * 
     15 * This program is distributed in the hope that it will be useful, 
     16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 
     17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
     18 * GNU General Public License for more details. 
     19 * 
     20 * You should have received a copy of the GNU General Public License 
     21 * along with this program; if not, write to the Free Software 
     22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 
     23 *######################################################################## 
     24 */ 
     25 
    126package org.greenstone.gatherer.gui; 
    227 
     
    429import org.greenstone.gatherer.Dictionary; 
    530import org.greenstone.gatherer.Gatherer; 
    6 //import static org.greenstone.gatherer.cdm.Format4gs3Manager.NumberedJTextArea; 
    731import org.greenstone.gatherer.cdm.CollectionConfigXMLReadWrite; 
    832import org.greenstone.gatherer.util.Codec; 
     
    1034import org.greenstone.gatherer.util.OutputStreamGobbler; 
    1135import org.greenstone.gatherer.util.StaticStrings; 
    12 import org.greenstone.gatherer.util.Utility; 
    1336import org.greenstone.gatherer.util.XMLTools; 
    1437 
    15 import org.fife.ui.rsyntaxtextarea.*; 
    1638import org.w3c.dom.*; 
    1739 
    18 import java.io.BufferedReader; 
    19 import java.io.BufferedWriter; 
    20 import java.io.Closeable; 
    2140import java.io.File; 
    22 import java.io.InputStream; 
    23 import java.io.InputStreamReader; 
    2441import java.io.IOException; 
    25 import java.io.OutputStream; 
    26 import java.io.OutputStreamWriter; 
    27  
    28 //import java.awt.Dimension; 
     42 
    2943import java.awt.*; 
    3044import java.awt.event.*; 
     
    3347import javax.swing.border.*; 
    3448import javax.swing.event.*; 
    35 import javax.swing.undo.*; 
    3649 
    3750 
     
    3952// + StreamGobblers: http://www.javaworld.com/article/2071275/core-java/when-runtime-exec---won-t.html 
    4053// Need to test on Windows: the Ctrl-D/Ctrl-Z sent to formatconverter.exe. May not need to do anything special for Windows now that this code is using StreamGobblers to process the in/out streams of the process 
    41 // Need to use dictionary for labels 
    42 // convertFormat() for remote GS 
     54// + Need to use dictionary for labels 
     55// X convertFormat() for remote GS. Remote does not display Format Conversion wizard 
    4356// + HTML tidy (put all "<br/>" back to "<br>" in luce/etc/collectionConfig.xml and run again) 
    44 // Help tooltips on buttons 
     57// + Help tooltips on buttons 
    4558// + Undo, Redo buttons 
    4659// X Split class into dialog/widgets and data processing? 
     
    8093    private GLIButton next_button = null; 
    8194    private GLIButton accept_all_button = null; 
    82     private GLIButton gs2_undo_button = null; 
    83     private GLIButton gs2_redo_button = null; 
    84     private GLIButton gs3_undo_button = null; 
    85     private GLIButton gs3_redo_button = null; 
    8695    private GLIButton htmltidy_button = null; 
    8796    private GLIButton xmltidy_button = null; 
     97    // the 2 NumberedJTextAreas. Each provide their own undo and redo buttons already hooked up to listeners 
    8898    private NumberedJTextArea gs2_textarea = null; 
    8999    private NumberedJTextArea gs3_textarea = null; 
     
    104114    setDefaultCloseOperation(DISPOSE_ON_CLOSE); 
    105115     
    106  
    107     gs2_textarea = new NumberedJTextArea(); 
    108     gs3_textarea = new NumberedJTextArea(); 
     116    // The NumberedJTextArea provides its own undo and redo buttons, already hooked up to listeners 
     117    // Just need to place these buttons in the dialog 
     118    gs2_textarea = new NumberedJTextArea("gs2", Dictionary.get("FormatConversionDialog.GS2_Text_Tooltip")); 
     119    gs3_textarea = new NumberedJTextArea("gs3", Dictionary.get("FormatConversionDialog.GS3_Text_Tooltip")); 
    109120 
    110121 
     
    112123    midbutton_panel.setComponentOrientation(Dictionary.getOrientation()); 
    113124 
    114     gs2_undo_button = new GLIButton(Dictionary.get("General.Undo"), Dictionary.get("General.Undo_Tooltip")); 
    115     gs2_undo_button.setEnabled(false); 
    116  
    117     gs2_redo_button = new GLIButton(Dictionary.get("General.Redo"), Dictionary.get("General.Redo_Tooltip")); 
    118     gs2_redo_button.setEnabled(false); 
    119  
    120     gs2_undo_button.addActionListener(new UndoListener(gs2_textarea, gs2_undo_button, gs2_redo_button)); 
    121     gs2_redo_button.addActionListener(new RedoListener(gs2_textarea, gs2_undo_button, gs2_redo_button)); 
    122  
    123125    JButton reconvert_button = new GLIButton(Dictionary.get("FormatConversionDialog.Reconvert"), Dictionary.get("FormatConversionDialog.Reconvert_Tooltip")); 
    124126 
    125     midbutton_panel.add(gs2_undo_button); 
    126     midbutton_panel.add(gs2_redo_button); 
     127    midbutton_panel.add(gs2_textarea.undoButton); 
     128    midbutton_panel.add(gs2_textarea.redoButton); 
    127129    midbutton_panel.add(reconvert_button); 
    128130    reconvert_button.addActionListener(new ReconvertListener()); 
     
    135137    JPanel button1_panel = new JPanel(); 
    136138    button1_panel.setComponentOrientation(Dictionary.getOrientation()); 
    137     gs3_undo_button = new GLIButton(Dictionary.get("General.Undo"), Dictionary.get("General.Undo_Tooltip")); 
    138     gs3_undo_button.setEnabled(false); 
    139  
    140     gs3_redo_button = new GLIButton(Dictionary.get("General.Redo"), Dictionary.get("General.Redo_Tooltip")); 
    141     gs3_redo_button.setEnabled(false); 
    142  
    143     gs3_undo_button.addActionListener(new UndoListener(gs3_textarea, gs3_undo_button, gs3_redo_button)); 
    144     gs3_redo_button.addActionListener(new RedoListener(gs3_textarea, gs3_undo_button, gs3_redo_button)); 
    145      
    146139 
    147140    xmltidy_button = new GLIButton(Dictionary.get("FormatConversionDialog.XHTML_Tidy"), Dictionary.get("FormatConversionDialog.XHTML_Tidy_Tooltip")); 
    148141    xmltidy_button.addActionListener(new XMLTidyButtonListener()); 
    149142 
    150     button1_panel.add(gs3_undo_button); 
    151     button1_panel.add(gs3_redo_button); 
     143    button1_panel.add(gs3_textarea.undoButton); 
     144    button1_panel.add(gs3_textarea.redoButton); 
    152145    button1_panel.add(xmltidy_button); 
    153146 
     
    167160 
    168161 
    169     // The undoable text areas. Adding the UndoableEditListener has to come after instantiation 
    170     // of the undo and redo buttons, since the listener expects these buttons to already exist 
    171  
    172     gs2_textarea.getDocument().addUndoableEditListener(new CustomUndoableEditListener(gs2_undo_button, gs2_redo_button)); 
    173     gs3_textarea.getDocument().addUndoableEditListener(new CustomUndoableEditListener(gs3_undo_button, gs3_redo_button)); 
    174  
    175     initTextArea(gs2_textarea); 
    176     initTextArea(gs3_textarea); 
    177     gs2_textarea.setToolTipText(Dictionary.get("FormatConversionDialog.GS2_Text_Tooltip")); 
    178     gs3_textarea.setToolTipText(Dictionary.get("FormatConversionDialog.GS3_Text_Tooltip")); 
    179  
    180  
    181162    JPanel centre_panel = new JPanel(); 
    182163    centre_panel.setLayout(new BoxLayout(centre_panel, BoxLayout.Y_AXIS)); 
     
    221202    } 
    222203 
    223  
    224     public static void initTextArea(NumberedJTextArea editor_textarea) { 
    225     /* Fields specific to RSyntaxQuery inherited class */ 
    226     editor_textarea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_XML); 
    227     editor_textarea.setBracketMatchingEnabled(true); 
    228     editor_textarea.setAnimateBracketMatching(true); 
    229     editor_textarea.setAntiAliasingEnabled(true); 
    230     editor_textarea.setAutoIndentEnabled(true); 
    231     editor_textarea.setPaintMarkOccurrencesBorder(false); 
    232      
    233     /* Standard fields to JTextArea */ 
    234     editor_textarea.setOpaque(false); 
    235     editor_textarea.setBackground(Configuration.getColor("coloring.editable_background", false)); 
    236     editor_textarea.setCaretPosition(0); 
    237     editor_textarea.setLineWrap(true); 
    238     editor_textarea.setRows(11); 
    239     editor_textarea.setWrapStyleWord(false);     
    240     } 
    241204 
    242205    public int getDialogResult() { 
     
    654617 
    655618    // as we're on a new screen of dialog, need to clear all undo/redo history 
    656     gs2_undo_button.setEnabled(false); 
    657     gs2_redo_button.setEnabled(false); 
    658     gs3_undo_button.setEnabled(false); 
    659     gs3_redo_button.setEnabled(false); 
    660     gs2_textarea.discardAllEdits(); 
     619    gs2_textarea.discardAllEdits(); // will disable redo/undo buttons 
    661620    gs3_textarea.discardAllEdits(); 
    662621 
     
    750709 
    751710    //******************INNER CLASSES including LISTENERS and STREAMGOBBLERS****************// 
    752  
    753     /** 
    754      * A textarea with the line number next to each line of the text 
    755      */ 
    756     public class NumberedJTextArea extends RSyntaxTextArea /* JTextArea */ 
    757     { 
    758     public void paintComponent(Graphics g) 
    759     { 
    760         Insets insets = getInsets(); 
    761         Rectangle rectangle = g.getClipBounds(); 
    762         g.setColor(Color.white); 
    763         g.fillRect(rectangle.x, rectangle.y, rectangle.width, rectangle.height); 
    764          
    765         super.paintComponent(g); 
    766          
    767         if (rectangle.x < insets.left) 
    768         { 
    769             FontMetrics font_metrics = g.getFontMetrics(); 
    770             int font_height = font_metrics.getHeight(); 
    771             int y = font_metrics.getAscent() + insets.top; 
    772             int line_number_start_point = ((rectangle.y + insets.top) / font_height) + 1; 
    773             if (y < rectangle.y) 
    774             { 
    775                 y = line_number_start_point * font_height - (font_height - font_metrics.getAscent()); 
    776             } 
    777             int y_axis_end_point = y + rectangle.height + font_height; 
    778             int x_axis_start_point = insets.left; 
    779             x_axis_start_point -= getFontMetrics(getFont()).stringWidth(Math.max(getRows(), getLineCount() + 1) + " "); 
    780             if (!this.getText().trim().equals("")) 
    781             { 
    782                 g.setColor(Color.DARK_GRAY); 
    783             } 
    784             else 
    785             { 
    786                 g.setColor(Color.white); 
    787             } 
    788             int length = ("" + Math.max(getRows(), getLineCount() + 1)).length(); 
    789             while (y < y_axis_end_point) 
    790             { 
    791                 g.drawString(line_number_start_point + "  ", x_axis_start_point, y); 
    792                 y += font_height; 
    793                 line_number_start_point++; 
    794             } 
    795         } 
    796     } 
    797      
    798      
    799     public Insets getInsets() 
    800     { 
    801         Insets insets = super.getInsets(new Insets(0, 0, 0, 0)); 
    802         insets.left += getFontMetrics(getFont()).stringWidth(Math.max(getRows(), getLineCount() + 1) + " "); 
    803         return insets; 
    804     } 
    805     } 
    806711 
    807712    // windowClosing() is called when the user presses the top-right close button the dialog 
     
    951856    } 
    952857 
    953     private class UndoListener implements ActionListener 
    954     {    
    955     NumberedJTextArea textarea = null; 
    956     GLIButton undobutton, redobutton; 
    957  
    958     public UndoListener(NumberedJTextArea textarea, GLIButton undobutton, GLIButton redobutton) { 
    959          
    960         this.textarea = textarea; 
    961         this.undobutton = undobutton; 
    962         this.redobutton = redobutton;        
    963          
    964     } 
    965      
    966     public void actionPerformed(ActionEvent event) 
    967     { 
    968         try { 
    969         if (textarea.canUndo()) {                        
    970             redobutton.setEnabled(true); 
    971             textarea.undoLastAction();                   
    972         } 
    973          
    974         if (!textarea.canUndo()) { 
    975             undobutton.setEnabled(false); 
    976         } else { 
    977             undobutton.setEnabled(true); 
    978         } 
    979          
    980         } catch (Exception e) { 
    981         System.err.println("Exception trying to undo: " + e.getMessage()); 
    982         e.printStackTrace(); 
    983         } 
    984     } 
    985     } 
    986      
    987     private class RedoListener implements ActionListener 
    988     { 
    989     NumberedJTextArea textarea = null; 
    990     GLIButton undobutton, redobutton; 
    991  
    992     public RedoListener(NumberedJTextArea textarea, GLIButton undobutton, GLIButton redobutton) { 
    993  
    994         this.textarea = textarea; 
    995         this.undobutton = undobutton; 
    996         this.redobutton = redobutton; 
    997     } 
    998  
    999     public void actionPerformed(ActionEvent evt) 
    1000     { 
    1001         try { 
    1002         if (textarea.canRedo()) { 
    1003             undobutton.setEnabled(true); // the difference with Format4gs3Manager, and no DocumentListener 
    1004             textarea.redoLastAction(); 
    1005         } 
    1006          
    1007         if (!textarea.canRedo()) { 
    1008             redobutton.setEnabled(false); 
    1009         } else { 
    1010             redobutton.setEnabled(true); 
    1011         } 
    1012          
    1013         } catch (Exception e) { 
    1014         System.err.println("Exception trying to redo: " + e.getMessage()); 
    1015         e.printStackTrace(); 
    1016         } 
    1017     } 
    1018     } 
    1019  
    1020     private class CustomUndoableEditListener implements UndoableEditListener { 
    1021     GLIButton undobutton = null; 
    1022     GLIButton redobutton = null; 
    1023  
    1024     public CustomUndoableEditListener(GLIButton undo_button, GLIButton redo_button) { 
    1025         undobutton = undo_button; 
    1026         redobutton = redo_button; 
    1027     } 
    1028  
    1029     public void undoableEditHappened(UndoableEditEvent evt) 
    1030     {        
    1031         undobutton.setEnabled(true); 
    1032         redobutton.setEnabled(false);        
    1033     } 
    1034     } 
    1035858} 
  • main/trunk/gli/src/org/greenstone/gatherer/util/InputStreamGobbler.java

    r29017 r29031  
     1/** 
     2 *######################################################################### 
     3 * 
     4 * A component of the Gatherer application, part of the Greenstone digital 
     5 * library suite from the New Zealand Digital Library Project at the 
     6 * University of Waikato, New Zealand. 
     7 * 
     8 * Copyright (C) 1999 New Zealand Digital Library Project 
     9 * 
     10 * This program is free software; you can redistribute it and/or modify 
     11 * it under the terms of the GNU General Public License as published by 
     12 * the Free Software Foundation; either version 2 of the License, or 
     13 * (at your option) any later version. 
     14 * 
     15 * This program is distributed in the hope that it will be useful, 
     16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 
     17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
     18 * GNU General Public License for more details. 
     19 * 
     20 * You should have received a copy of the GNU General Public License 
     21 * along with this program; if not, write to the Free Software 
     22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 
     23 *######################################################################## 
     24 */ 
     25 
    126package org.greenstone.gatherer.util; 
    227 
     
    1136 
    1237// http://www.javaworld.com/article/2071275/core-java/when-runtime-exec---won-t.html?page=2 
    13 // This class is used in FormatConversionDialog to properly read from the output and error streams of a Process 
     38// This class is used in FormatConversionDialog to properly read from the stdout and stderr 
     39// streams of a Process, Process.getInputStream() and Process.getErrorSream() 
    1440public class InputStreamGobbler extends Thread 
    1541{ 
  • main/trunk/gli/src/org/greenstone/gatherer/util/OutputStreamGobbler.java

    r29017 r29031  
     1/** 
     2 *######################################################################### 
     3 * 
     4 * A component of the Gatherer application, part of the Greenstone digital 
     5 * library suite from the New Zealand Digital Library Project at the 
     6 * University of Waikato, New Zealand. 
     7 * 
     8 * Copyright (C) 1999 New Zealand Digital Library Project 
     9 * 
     10 * This program is free software; you can redistribute it and/or modify 
     11 * it under the terms of the GNU General Public License as published by 
     12 * the Free Software Foundation; either version 2 of the License, or 
     13 * (at your option) any later version. 
     14 * 
     15 * This program is distributed in the hope that it will be useful, 
     16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 
     17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
     18 * GNU General Public License for more details. 
     19 * 
     20 * You should have received a copy of the GNU General Public License 
     21 * along with this program; if not, write to the Free Software 
     22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 
     23 *######################################################################## 
     24 */ 
     25 
    126package org.greenstone.gatherer.util; 
    227 
     
    1237// http://www.javaworld.com/article/2071275/core-java/when-runtime-exec---won-t.html?page=2 
    1338// This class is used in FormatConversionDialog to properly write to the inputstream of a Process 
     39// Process.getOutputStream() 
    1440public class OutputStreamGobbler extends Thread 
    1541{