Text Width Checker, useful to avoid line wrap

A

Andrew Thompson

After seeing 'one too many' code example posted to usenet
that was broken due to line wrap, I threw together this
simple little text width checker.

/I hope the Text Width Checker might be of use to others./

You can launch the Text Width Checker at
<http://www.physci.org/twc.jnlp>

The *latest version* of the code will be/is here
<http://www.physci.org/test/code/TextWidthCheck.java>

The *current version* of the code, right now, is..

<sscce>
package org.physci.tool.text.textwidthcheck;

import java.awt.Container;
import java.awt.Window;
import java.awt.FlowLayout;
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JToolBar;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;

/**
A text width checker. Useful for checking text that
might be broken or reformatted by reaching the maximum
line length available for the desired medium (e.g.
usenet, a command line), is within the constraints.

@author Andrew Thompson
@version 0.1
*/
public class TextWidthCheck extends JPanel {

public TextWidthCheck() {
super( new BorderLayout() );

int defaultWidth = 80;

String inputText =
getTextStringLineTens(defaultWidth) +
"\n" +
getTextStringLineUnits(defaultWidth);

JPanel textPanel = new JPanel( new BorderLayout() );

final JTextArea scale =
getTextArea( inputText,2,defaultWidth );
scale.setEnabled(false);
textPanel.add(
new JScrollPane(
scale,
ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER
),
BorderLayout.NORTH );

final JTextArea input = getTextArea(
"Paste text to be checked, here.\n\n" +
"If the width of any line is greater than "+
"the maximum number of\ncharacters set above, "+
"a horizontal scroll-bar will appear.\n\n" +
"The longest line of the current text, " +
"is 62 chars wide.\n",
15,defaultWidth );
input.setCaretPosition(0);
input.moveCaretPosition(input.getDocument().getLength());
textPanel.add(new JScrollPane(
input,
ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED
),
BorderLayout.CENTER );
add( textPanel, BorderLayout.CENTER );

JToolBar toolbar = new JToolBar(
"Options",
JToolBar.HORIZONTAL);
toolbar.setFloatable(false);
toolbar.setLayout( new FlowLayout(5) );

final JSpinner charWidth = new JSpinner(
new SpinnerNumberModel(
new Integer(defaultWidth),
new Integer(44),
new Integer(100),
new Integer(1) ));
charWidth.addChangeListener( new ChangeListener() {
public void stateChanged(ChangeEvent ce) {
int chars =
((Integer)charWidth.getValue())
.intValue();
scale.setColumns( chars );
scale.setText( getTextStringLine(chars) );
input.setColumns( chars );
validate();
Container root = getTopLevelAncestor();
if (root instanceof Window) {
((Window)root).pack();
}
}
} );
toolbar.add( charWidth );

toolbar.add( new JLabel("Target width (characters)") );

toolbar.addSeparator();

final String tabSpace = " ";
Integer[] tabSizes = new Integer[tabSpace.length()];
for (int ii=0; ii<tabSizes.length; ii++) {
tabSizes[ii] = ii+1;
}
final JComboBox tabSize = new JComboBox( tabSizes );
tabSize.setSelectedItem(4);

JButton replaceTabs = new JButton("Replace Tabs");
replaceTabs.setMnemonic('r');
replaceTabs.addActionListener( new ActionListener(){
public void actionPerformed(ActionEvent ae) {
int size = tabSize.getSelectedIndex()+1;
String space = tabSpace.substring( 0,size );
input.setText(
input.getText().replaceAll("\t",space) );
}
} );
toolbar.add(replaceTabs);

toolbar.add(new JLabel("Spaces per tab"));

toolbar.add(tabSize);

JPanel toolPanel = new JPanel(new BorderLayout(0,0));
toolPanel.add( toolbar, BorderLayout.CENTER );
add( toolPanel, BorderLayout.NORTH );

setBorder( new EmptyBorder(5,5,5,5) );
}

String getTextStringLine(int num) {
return getTextStringLineTens(num) +
"\n" +
getTextStringLineUnits(num);
}

String getTextStringLineTens(int num) {
StringBuffer buffer = new StringBuffer();
for (int ii=0; ii<num; ii++) {
if ((ii+1)%10==0) {
buffer.append(""+((ii/10)+1));
} else {
buffer.append(" ");
}
}
return buffer.toString();
}

String getTextStringLineUnits(int num) {
StringBuffer buffer = new StringBuffer();
for (int ii=0; ii<num; ii++) {
buffer.append(""+(ii+1)%10);
}
return buffer.toString();
}

JTextArea getTextArea(
String content,
int rows,
int cols) {

JTextArea temp = new JTextArea( content, rows, cols );
Font tempFont = temp.getFont();
Font font = new Font(
"Monospaced",
Font.PLAIN,
tempFont.getSize()+4);
temp.setFont(font);
return temp;
}

public static void main(String[] args) {
Thread t = new Thread() {
public void run() {
JFrame f = new JFrame("Text Width Check");
f.setContentPane( new TextWidthCheck() );
f.setLocationByPlatform(true);
f.setResizable(false);
f.pack();
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
};
SwingUtilities.invokeLater(t);
}
}
<sscce>

This message x-posted to comp.lang.java.programmer
& comp.lang.java.help, with follow-ups set to c.l.j.help
only. If you don't like the follow-ups, please feel free
to set them differently.

Andrew T.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top