/* PermanentTextScroll.java * * Copyright (c) 2001, Ted Nelson and Tuomas Lukka * * This file is part of Gzz. * * Gzz is free software; you can redistribute it and/or modify it under * the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * Gzz is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General * Public License for more details. * * You should have received a copy of the GNU Lesser General * Public License along with Gzz; if not, write to the Free * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, * MA 02111-1307 USA * * */ /* * Written by Tuomas Lukka */ package org.nongnu.alph.impl; import org.nongnu.alph.*; import org.nongnu.storm.*; import java.io.*; /** A text scroll block loaded from somewhere. */ public class PermanentTextScroll implements TextScrollBlock, ScrollBlockManager.MediaserverScrollBlock { String rcsid = "$Id: PermanentTextScroll.java,v 1.1 2003/03/25 14:36:12 tjl Exp $"; char[] chars; public PermanentTextScroll(String text) { this.chars = text.toCharArray(); } Mediaserver ms; Mediaserver.Id msid; boolean loadingFailed; public PermanentTextScroll(Mediaserver ms, Mediaserver.Id msid) { this.ms = ms; this.msid = msid; } public String getID() { return msid.getString(); } public Mediaserver.Id saveOrGetId(Mediaserver ms) { return msid; } public boolean equals(Object o) { if(!(o instanceof PermanentTextScroll)) return false; ScrollBlock sb = (ScrollBlock)o; return sb.getID().equals(getID()); } public int hashCode() { return getID().hashCode(); } protected final void load() { if(chars != null || loadingFailed) return; MediaserverBlock block ; String ct; try { block = ms.getDatum(msid); ct = block.getContentType(); } catch(IOException e) { loadingFailed = true; e.printStackTrace(); throw new Error("Couldn't load block: "+e); } // Note: for the legacy string content to work, we need to be able // to load GZZ1 diffs as text blocks (see GZZ1Handler.LegacyContent // javadoc for more info). if(!ct.equals("text/plain; charset=UTF-8") && !ct.equals("application/x-gzigzag-GZZ1") && !ct.equals("message/rfc822")) { loadingFailed = true; throw new Error("Unknown text block '"+ct+"'"); } String string; try { string = new String(block.getBytes(), "UTF8"); } catch(Exception e) { loadingFailed = true; e.printStackTrace(); throw new Error("Exception while reading: "+e); } this.chars = string.toCharArray(); } public TextSpan append(char ch) throws ImmutableException { throw new ImmutableException("Can't append to permanent scroll block"); } public TextSpan append(String s) throws ImmutableException { throw new ImmutableException("Can't append to permanent scroll block"); } public Span getCurrent() { load(); return new ScrollBlockManager.SimpleTextSpan(this, 0, chars.length); } public Span getSpan(int offs1, int offs2) { return new ScrollBlockManager.SimpleTextSpan(this, offs1, offs2); } public boolean isFinalized() { return true; } public char[] getCharArray() { load(); return chars; } }