//This file is part of SECONDO. //Copyright (C) 2004, University in Hagen, Department of Computer Science, //Database Systems for New Applications. //SECONDO is free software; you can redistribute it and/or modify //it under the terms of the GNU General Public License as published by //the Free Software Foundation; either version 2 of the License, or //(at your option) any later version. //SECONDO 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 General Public License for more details. //You should have received a copy of the GNU General Public License //along with SECONDO; if not, write to the Free Software //Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA package viewer.hoese.algebras; import sj.lang.ListExpr; import viewer.*; import viewer.hoese.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.text.EditorKit; import javax.swing.text.StyledEditorKit; import javax.swing.text.Document; import tools.Base64Decoder; import tools.Reporter; import java.io.*; import java.util.SortedMap; import java.nio.charset.Charset; import java.util.Map; import java.util.Iterator; // for pdf display import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.rendering.PDFRenderer; /** * A displayclass for the html formatted code */ public class Dspltext extends DsplGeneric implements ExternDisplay{ private boolean defined; /** Creates a new Instance of this. */ public Dspltext(){ if(Display==null){ Display = new TextViewerFrame(); } } public String toString(){ return Entry; } public void init (String name, int nameWidth, int indent, ListExpr type,ListExpr value, QueryResult qr) { String T = name; String V; if (value.listLength()==1){ value = value.first(); } defined = !isUndefined(value); if(!defined){ V = "undefined"; theList = ListExpr.textAtom(V); }else{ if(value.atomType()!=ListExpr.TEXT_ATOM){ V = "error in value "; theList = ListExpr.textAtom(V); defined = false; } else{ V = value.textValue(); theList = value; } } T=extendString(T,nameWidth, indent); String Text = V; computeType(type, Text); if(Type==PLAIN_TYPE){ if(Text.length()<=MAX_DIRECT_DISPLAY_LENGTH){ // short Text Entry = T+" : "+ Text; qr.addEntry(Entry); // avoid the possibility to pop up a window return; } else{ // long plain text Entry = T+" : "+ Text.substring(0,MAX_DIRECT_DISPLAY_LENGTH-4).replace('\n',' ')+" ..."; } }else if(Type==HTML_TYPE){ Entry = T + " : ..."; } else if(Type==RTF_TYPE){ Entry = T + " : RTF ..."; } else if(Type==PDF_TYPE){ Entry = T + " : PDF ..."; } qr.addEntry(this); return; } public void displayExtern(){ Display.setSource(this); Display.setVisible(true); } public boolean isExternDisplayed(){ return Display.isVisible() && this.equals(Display.getSource()); } private boolean checkForHtml(String Text,int offset){ int restlength = Text.length()-offset-1; if(restlength<4) return false; String tag = Text.substring(offset,offset+5).toLowerCase(); if("=0; } private static TextViewerFrame Display=null; private String Entry; private ListExpr theList; protected int Type; // contains the type which is the text (probably) protected static final int PLAIN_TYPE=0; protected static final int HTML_TYPE=1; protected static final int RTF_TYPE=2; protected static final int PDF_TYPE=3; private static final int MAX_DIRECT_DISPLAY_LENGTH = 30; private static final String WHITESPACES = " \t\n\r"; private static class TextViewerFrame extends JFrame{ JComboBox encodingCB; private String encoding=stdEncoding; private static String stdEncoding; static { try{ stdEncoding = System.getProperty("file.encoding"); } catch(Exception e){ stdEncoding=null; } if(stdEncoding==null){ stdEncoding = "UTF-8"; } } public TextViewerFrame(){ getContentPane().setLayout(new BorderLayout()); Display = new JTextPane(); if(EKHtml==null){ EKHtml = JTextPane.createEditorKitForContentType("text/html"); EKRtf = JTextPane.createEditorKitForContentType("text/rtf"); } TextScrollPane = new JScrollPane(Display); TextPanel = new JPanel(new BorderLayout()); JPanel SearchPanel = new JPanel(); SearchPanel.add(CaseSensitive); SearchPanel.add(SearchField); SearchPanel.add(SearchBtn); SearchBtn.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent evt){ searchText(); } }); SearchField.addKeyListener(new KeyListener(){ public void keyPressed(KeyEvent evt){ if(evt.getKeyCode()==KeyEvent.VK_ENTER) searchText(); } public void keyTyped(KeyEvent evt){} public void keyReleased(KeyEvent evt){} }); TextPanel.add(TextScrollPane,BorderLayout.CENTER); TextPanel.add(SearchPanel,BorderLayout.SOUTH); getContentPane().add(TextPanel,BorderLayout.CENTER); CloseBtn = new JButton("Close"); CloseBtn.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent evt){ TheText=null; TextViewerFrame.this.setVisible(false); } } ); fileChooser = new JFileChooser(); SaveBtn = new JButton("Save"); SaveBtn.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent evt){ if(fileChooser.showSaveDialog(TextViewerFrame.this)==JFileChooser.APPROVE_OPTION){ File F = TextViewerFrame.this.fileChooser.getSelectedFile(); try{ BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(F)); if(TextViewerFrame.this.Source.Type!=PDF_TYPE){ out.write(TheText.getBytes()); } else{ out.write(Base64Decoder.decode(TheText)); } out.flush(); out.close(); }catch(Exception e){ Reporter.debug(e); Reporter.showError("Error in saving file "); } } } }); // create the pdf viewer if(pdf_viewer==null){ try{ pdf_viewer = new PDFPanel(); }catch(Exception e){ Reporter.writeError("cannot initialize pdf-viewer"); } } encodingCB = new JComboBox(); Iterator acs = Charset.availableCharsets().entrySet().iterator(); //for(int i=0; i0){ newpage = page -1; } else if(src.equals(NextBtn) && page < NumberOfPages-1){ newpage = page+1; } else if(src.equals(LastBtn)){ newpage=NumberOfPages-1; } setPage(newpage); } }; pageField.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ String p = pageField.getText().trim(); try{ int pn = Integer.parseInt(p); setPage(pn-1); } catch(NumberFormatException a ){ Reporter.showError("Invalid page number"); } } }); FirstBtn.addActionListener(Control); PrevBtn.addActionListener(Control); NextBtn.addActionListener(Control); LastBtn.addActionListener(Control); ActionListener Magnifier = new ActionListener(){ public void actionPerformed(ActionEvent evt){ Object src = evt.getSource(); if(src.equals(GBtn)) scale=scale*SCALEFACTOR; if(src.equals(LBtn)) scale=Math.max(MINSCALE,scale/SCALEFACTOR); if(src.equals(FitBtn)) scale = getFitSF(); if(!dataAvailable){ return; } try{ CurrentPage.setImage(pdfRenderer.renderImage(page,scale)); PdfScrollPane.invalidate(); TextViewerFrame.this.invalidate(); TextViewerFrame.this.validate(); TextViewerFrame.this.repaint(); }catch(Exception e){ Reporter.debug(e); CurrentPage.setImage(null); PdfScrollPane.invalidate(); PDFPanel.this.repaint(); } }}; GBtn.addActionListener(Magnifier); LBtn.addActionListener(Magnifier); FitBtn.addActionListener(Magnifier); } private void setPage(int newpage){ if(newpage<0) newpage = 0; if(newpage>NumberOfPages-1) newpage = NumberOfPages-1; pageField.setText(""+(newpage+1)); if(newpage!=page){ page=newpage; try{ pageField.setText(""+(page+1)); CurrentPage.setImage(pdfRenderer.renderImage(page,scale)); PdfScrollPane.invalidate(); TextViewerFrame.this.invalidate(); TextViewerFrame.this.validate(); TextViewerFrame.this.repaint(); } catch(Exception e){ Reporter.debug(e); CurrentPage.setImage(null); PdfScrollPane.invalidate(); TextViewerFrame.this.invalidate(); TextViewerFrame.this.validate(); TextViewerFrame.this.repaint(); } } } private class PicturePanel extends JPanel{ public void paint(Graphics g){ if(image!=null) g.drawImage(image,0,0,null); } public Dimension getPreferredSize(){ return MyDimension; } public Dimension getMaximumSize(){ return MyDimension; } public Dimension getMinimumSize(){ return MyDimension; } public void setImage(Image image){ this.image = image; if(image!=null){ MyDimension.width = image.getWidth(null); MyDimension.height = image.getHeight(null); }else{ MyDimension.width=MyDimension.height=10; } } Image image; Dimension MyDimension = new Dimension(); }; private PicturePanel CurrentPage= new PicturePanel(); public boolean setPdfData(byte[] data){ try{ if(pdfDocument!=null){ pdfDocument.close(); pdfDocument = null; } pdfDocument = PDDocument.load(data); NumberOfPages = pdfDocument.getNumberOfPages(); dataAvailable=true; pdfRenderer = new PDFRenderer(pdfDocument); page = 0; scale = getFitSF(); pageField.setText(""+1); CurrentPage.setImage(pdfRenderer.renderImage(0,scale)); String title = pdfDocument.getDocumentInformation().getTitle(); if(title==null){ title = ""; } String author = pdfDocument.getDocumentInformation().getAuthor(); if(author==null){ author = ""; } String sep = (author.length()>0 && title.length()>0)?" : " :""; TextViewerFrame.this.setTitle(author + sep + title);; TextViewerFrame.this.invalidate(); TextViewerFrame.this.validate(); TextViewerFrame.this.repaint(); return true; } catch(Exception e){ TextViewerFrame.this.setTitle(""); Reporter.debug(e); NumberOfPages = -1; dataAvailable=false; return false; } } private float getFitSF(){ if(pdfRenderer == null){ return 1; } try{ Image img = pdfRenderer.renderImage(page,1); Dimension dim = PdfScrollPane.getSize(); Insets in = PdfScrollPane.getInsets(); float w = dim.width - (in.left + in.right); float h = dim.height - (in.top + in.bottom); float sx = w / (float) img.getWidth(null); float sy = h / (float) img.getHeight(null); float s = Math.min(sx,sy); return s>=MINSCALE?s:1; } catch(Exception e){ return 1; } } private PDDocument pdfDocument = null; private PDFRenderer pdfRenderer = null; private JScrollPane PdfScrollPane; private JButton FirstBtn = new JButton("|<"); private JButton PrevBtn = new JButton("<"); private JButton NextBtn = new JButton(">"); private JTextField pageField = new JTextField(" "){ public Dimension getPreferredSize(){ Dimension r = super.getPreferredSize(); if(r.width < 100){ r.width = 100; } return r; } public Dimension getMinimumSize(){ Dimension r = super.getMinimumSize(); if(r.width < 100){ r.width = 100; } return r; } }; private JButton LastBtn = new JButton(">|"); private JButton GBtn = new JButton("(+)"); private JButton LBtn = new JButton("(-)"); private JButton FitBtn = new JButton("[]"); private JLabel Pages = new JLabel("# pages "); private int NumberOfPages=-1; private boolean dataAvailable = false; private int page; // the number of the current page private float scale=1.0F; private final float MINSCALE = 0.001F; static final float SCALEFACTOR=1.2F; } private JTextPane Display; private JButton CloseBtn; private JButton SaveBtn; private JFileChooser fileChooser; private Dspltext Source; private JButton PlainBtn; private JButton resetBtn; private JButton HtmlBtn; private JButton RtfBtn; private JButton PdfBtn; private String TheText; private static EditorKit EKHtml=null; private static EditorKit EKRtf=null; private static JScrollPane TextScrollPane; private static PDFPanel pdf_viewer; // Panel for displaying pdf contents private static JPanel TextPanel; // Panel for displaying text in plain, html and rtf format private boolean ISPDF = false; private JCheckBox CaseSensitive = new JCheckBox("case sensitive"); private JTextField SearchField = new JTextField(20); private JButton SearchBtn = new JButton("search"); private int LastSearchPos=0; } }