37dd412d28f496215d1cea472f9be7407f8f41bd
[debian/jabref.git] / src / java / net / sf / jabref / PreviewPanel.java
1 package net.sf.jabref;
2
3 import java.awt.Dimension;
4 import java.awt.Graphics;
5 import java.awt.Graphics2D;
6 import java.awt.RenderingHints;
7 import java.awt.RenderingHints.Key;
8 import java.io.IOException;
9 import java.io.StringReader;
10
11 import javax.swing.JEditorPane;
12 import javax.swing.JScrollBar;
13 import javax.swing.JScrollPane;
14 import javax.swing.SwingUtilities;
15 import javax.swing.event.HyperlinkEvent;
16 import javax.swing.event.HyperlinkListener;
17
18 import net.sf.jabref.export.layout.Layout;
19 import net.sf.jabref.export.layout.LayoutHelper;
20
21 /**
22  * Displays an BibtexEntry using the given layout format.
23  * 
24  * @author $Author: coezbek $
25  * @version $Revision: 1.15 $ ($Date: 2006/09/03 19:09:59 $)
26  *
27  */
28 public class PreviewPanel extends JEditorPane {
29
30         public String CONTENT_TYPE = "text/html";
31
32         BibtexEntry entry;
33
34         MetaData metaData;
35
36         /**
37          * If a database is set, the preview will attempt to resolve strings in the
38          * previewed entry using that database.
39          */
40         BibtexDatabase database;
41
42         Layout layout;
43
44         String layoutFile;
45
46         JScrollPane sp;
47
48         public PreviewPanel(BibtexDatabase db, MetaData metaData, String layoutFile) {
49                 this(metaData, layoutFile);
50                 this.database = db;
51         }
52
53         public PreviewPanel(BibtexDatabase database, MetaData metaData, BibtexEntry entry, String layoutFile) {
54                 this(metaData, layoutFile);
55                 this.database = database;
56                 this.entry = entry;
57
58                 try {
59                         readLayout();
60                 } catch (Exception ex) {
61                         ex.printStackTrace();
62                 }
63                 update();
64         }
65
66         public PreviewPanel(BibtexEntry entry, MetaData metaData, String layoutFile) {
67                 this(null, metaData, entry, layoutFile);
68         }
69
70         /**
71          * Emtpy Preview Panel constructor
72          * 
73          * @param metaData
74          * @param layoutFile
75          */
76         public PreviewPanel(MetaData metaData, String layoutFile) {
77                 this.metaData = metaData;
78                 this.layoutFile = layoutFile;
79                 
80                 sp = new JScrollPane(this, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
81                         JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
82                 sp.setBorder(null);
83
84                 setEditable(false);
85                 setContentType(CONTENT_TYPE);
86                 addHyperlinkListener(new HyperlinkListener() {
87                         public void hyperlinkUpdate(HyperlinkEvent hyperlinkEvent) {
88                                 if (hyperlinkEvent.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
89                                         try {
90                                                 String address = hyperlinkEvent.getURL().toString();
91                                                 Util.openExternalViewer(PreviewPanel.this.metaData, address, "url");
92                                         } catch (IOException e) {
93                                                 e.printStackTrace();
94                                         }
95                                 }
96                         }
97                 });
98         }
99
100         public void setDatabase(BibtexDatabase db) {
101                 database = db;
102         }
103
104         public JScrollPane getPane() {
105                 return sp;
106         }
107
108         public void readLayout(String layoutFormat) throws Exception {
109                 layoutFile = layoutFormat;
110                 readLayout();
111         }
112
113         public void readLayout() throws Exception {
114                 StringReader sr = new StringReader(layoutFile.replaceAll("__NEWLINE__", "\n"));
115                 layout = new LayoutHelper(sr).getLayoutFromText(Globals.FORMATTER_PACKAGE);
116         }
117
118         public void setEntry(BibtexEntry newEntry) {
119                 entry = newEntry;
120                 try {
121                         readLayout();
122                         update();
123                 } catch (Exception ex) {
124                         ex.printStackTrace();
125                 }
126         }
127
128         public void update() {
129
130                 StringBuffer sb = new StringBuffer();
131                 sb.append(layout.doLayout(entry, database));
132                 setText(sb.toString());
133                 invalidate();
134                 revalidate();
135
136                 // Scroll to top:
137                 final JScrollBar bar = sp.getVerticalScrollBar();
138                 SwingUtilities.invokeLater(new Runnable() {
139                         public void run() {
140                                 bar.setValue(0);
141                         }
142                 });
143         }
144
145         public boolean hasEntry() {
146                 return (entry != null);
147         }
148
149         public Dimension getPreferredScrollableViewportSize() {
150                 return getPreferredSize();
151         }
152
153         public void paintComponent(Graphics g) {
154                 Graphics2D g2 = (Graphics2D) g;
155                 Object hint = g2.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
156                 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
157                 g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
158                 super.paintComponent(g2);
159                 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, hint);
160         }
161 }