/* (@)DynamicTextPaneTestGUI.java */
/* Copyright 2012 Sebastian Haufe
* Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
[url]http://www.apache.org/licenses/LICENSE-2.0[/url]
* Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
package com.ebenius.swing;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
import javax.swing.*;
import javax.swing.text.*;
/**
* Test GUI showing how to manipulate a document from a swing timer.
*
* @version $Revision$ as of $Date$
* @author Sebastian Haufe
* @since Playground-3.8
*/
public class DynamicTextPaneTestGUI {
/** Creates the GUI. Call on EDT, only! */
static void createAndShowGui() {
final DefaultStyledDocument doc = new DefaultStyledDocument();
final Style styleRoot = doc.addStyle("root", null);
StyleConstants.setFontFamily(styleRoot, "monospaced");
StyleConstants.setFontSize(styleRoot, 12);
StyleConstants.setForeground(styleRoot, Color.green);
final Style styleBlue = doc.addStyle("blue", styleRoot);
StyleConstants.setBold(styleBlue, true);
StyleConstants.setForeground(styleBlue, Color.cyan);
final JTextPane textPane = new JTextPane(doc);
textPane.setEditable(false);
textPane.setPreferredSize(new Dimension(600, 200));
textPane.setBackground(Color.black);
final int numSteps = 30;
try {
doc.insertString(0, "Deleting: [0%] ", styleRoot);
} catch (BadLocationException ex) {
ex.printStackTrace();
}
final int progressAt = doc.getLength();
try {
final char[] space = new char[numSteps];
Arrays.fill(space, ' ');
doc.insertString(doc.getLength(), new String(space), styleBlue);
doc.insertString(doc.getLength(), " [100%]", styleRoot);
} catch (BadLocationException ex) {
ex.printStackTrace();
}
javax.swing.Timer timer =
new javax.swing.Timer(200, new ActionListener() {
int updateIndex = 0;
public void actionPerformed(ActionEvent e) {
try {
if (updateIndex < numSteps) {
doc.replace(progressAt + updateIndex, 1, "#", styleBlue);
} else {
((javax.swing.Timer) e.getSource()).stop();
}
updateIndex++;
} catch (BadLocationException ex) {
ex.printStackTrace();
}
}
});
timer.start();
final JPanel contentPane = new JPanel(new BorderLayout(6, 6));
contentPane.add(new JScrollPane(textPane));
final JFrame f = new JFrame("Test Frame: DynamicTextPaneTestGUI"); //$NON-NLS-1$
f.setContentPane(contentPane);
f.pack();
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.setVisible(true);
}
/** @param args ignored */
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}