/*
(c) Copyright 2008, 2009 Hewlett-Packard Development Company, LP
All rights reserved.
$Id$
*/
/**
*
*/
package run.acceptance.gui;
import java.awt.*;
import java.io.*;
import javax.swing.*;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.StyledEditorKit;
import run.acceptance.gui.GUIMaster.NameChangeListener;
import com.hp.hpl.jena.util.FileUtils;
import com.hp.hpl.jena.shared.JenaException;;
public class TimedEditPanel extends JPanel
{
final JEditorPane ed = new JEditorPane();
String fileName;
final NameChangeListener ncl;
public TimedEditPanel( String pathName, GUIMaster.NameChangeListener ncl, Timer t )
{
this.ncl = ncl;
fileName = pathName;
setLayout( new BorderLayout() );
ed.setEditorKit( new HighlightKit() );
GUIUtils.restartTimerOnUpdate( ed, t );
loadInitialContent();
String title = pathName;
add( scrolledEditorPane( title ), BorderLayout.CENTER );
}
private JComponent scrolledEditorPane( String title )
{
JScrollPane paneScrollPane = new JScrollPane( ed );
paneScrollPane.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED );
paneScrollPane.setPreferredSize( new Dimension( 250, 155 ) );
return paneScrollPane;
}
String getFileName()
{ return fileName; }
public String getText()
{
try { return ed.getDocument().getText( 0, ed.getDocument().getLength() ); }
catch (BadLocationException e) { throw new RuntimeException( e ); }
}
public void saveContents()
{ GUIUtils.writeWholeFileAsUTF8( getText(), getFileName() ); }
public void loadFile( String fileName )
{
clearContents();
this.fileName = fileName;
ncl.newName( this, new File( fileName ).getName() );
loadContent( readContentFromFile() );
}
public void clearContents()
{
try { ed.getDocument().remove( 0, ed.getDocument().getLength() ); }
catch (BadLocationException e) { throw new JenaException( e ); }
}
private void loadInitialContent()
{ loadContent( readContentFromFile() ); }
private void loadContent( String contents )
{
try { ed.getDocument().insertString( 0, contents, null ); }
catch (BadLocationException e) { throw new JenaException( e ); }
}
private String readContentFromFile()
{
try { return FileUtils.readWholeFileAsUTF8( getFileName() ); }
catch (NullPointerException e) { throw new JenaException( "NullOopsie with " + getFileName(), e ); }
catch (FileNotFoundException e) { return "!comment cannot find '" + getFileName() + "'"; }
catch (IOException e) { throw new JenaException( e ); }
}
static class HighlightKit extends StyledEditorKit
{
@Override public Document createDefaultDocument()
{ return new HighlightDocument(); }
}
}
/*
(c) Copyright 2009 Hewlett-Packard Development Company, LP
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. The name of the author may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/