/src/mpv5/ui/panels/PreviewPanel.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/ · Java · 498 lines · 399 code · 51 blank · 48 comment · 53 complexity · afbbf65b13ee597a04faefe2fd61e888 MD5 · raw file

  1. package mpv5.ui.panels;
  2. import com.sun.pdfview.PDFFile;
  3. import com.sun.pdfview.PDFPage;
  4. import com.sun.pdfview.PagePanel;
  5. import java.awt.BorderLayout;
  6. import java.awt.Cursor;
  7. import java.io.File;
  8. import java.io.IOException;
  9. import java.io.RandomAccessFile;
  10. import java.nio.ByteBuffer;
  11. import java.nio.channels.FileChannel;
  12. import java.util.List;
  13. import java.util.logging.Level;
  14. import java.util.logging.Logger;
  15. import javax.swing.JEditorPane;
  16. import javax.swing.JFrame;
  17. import javax.swing.JScrollPane;
  18. import mpv5.db.common.Context;
  19. import mpv5.db.common.DatabaseObject;
  20. import mpv5.db.common.Formattable;
  21. import mpv5.db.common.NodataFoundException;
  22. import mpv5.db.common.QueryCriteria;
  23. import mpv5.db.common.QueryHandler;
  24. import mpv5.db.objects.Contact;
  25. import mpv5.db.objects.Item;
  26. import mpv5.db.objects.MailMessage;
  27. import mpv5.db.objects.User;
  28. import mpv5.globals.Messages;
  29. import mpv5.handler.VariablesHandler;
  30. import mpv5.logging.Log;
  31. import mpv5.mail.SimpleMail;
  32. import mpv5.ui.dialogs.BigPopup;
  33. import mpv5.ui.dialogs.DialogForFile;
  34. import mpv5.ui.dialogs.Popup;
  35. import mpv5.ui.frames.MPView;
  36. import mpv5.utils.export.Export;
  37. import mpv5.utils.files.FileDirectoryHandler;
  38. import mpv5.utils.files.FileReaderWriter;
  39. import mpv5.utils.jobs.Job;
  40. import mpv5.utils.jobs.Waiter;
  41. import mpv5.utils.print.PrintJob;
  42. /**
  43. *
  44. *
  45. */
  46. public class PreviewPanel extends javax.swing.JPanel implements Waiter {
  47. private static final long serialVersionUID = 1L;
  48. private File file;
  49. private DatabaseObject dataOwner;
  50. private PDFFile pdffile;
  51. private PagePanel panel;
  52. private DataPanel parent;
  53. /** Creates new form
  54. */
  55. public PreviewPanel() {
  56. initComponents();
  57. }
  58. /** Creates new preview for the given file. Currently supported file types:
  59. * <li>PDF
  60. * <li>ODT
  61. * <li>TXT
  62. * @param file
  63. */
  64. public PreviewPanel(File file) {
  65. initComponents();
  66. openl(file);
  67. }
  68. /**
  69. * Shows the given pdf file in the preview panel
  70. * @param pdf
  71. */
  72. public void openPdf(File pdf) {
  73. PagePanel vp;
  74. this.file = pdf;
  75. if (pdf.isFile() && pdf.exists()) {
  76. try {
  77. RandomAccessFile raf = new RandomAccessFile(pdf, "r");
  78. FileChannel channel = raf.getChannel();
  79. ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
  80. pdffile = new PDFFile(buf);
  81. buf.clear();
  82. channel.close();
  83. raf.close();
  84. vp = new PagePanel();
  85. ppanel.removeAll();
  86. ppanel.add(vp, BorderLayout.CENTER);
  87. ppanel.validate();
  88. vp.showPage(pdffile.getPage(0));
  89. } catch (Exception ex) {
  90. Log.Debug(ex);
  91. }
  92. } else {
  93. throw new IllegalArgumentException("File is not existing or a directory: " + pdf);
  94. }
  95. }
  96. /** This me4thod is called from within the constructor to
  97. * initialize the form.
  98. * WARNING: Do NOT modify this code. The content of this method is
  99. * always regenerated by the Form Editor.
  100. */
  101. @SuppressWarnings("unchecked")
  102. // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  103. private void initComponents() {
  104. jSeparator5 = new javax.swing.JSeparator();
  105. jPanel1 = new javax.swing.JPanel();
  106. ppanel = new javax.swing.JPanel();
  107. toolbar = new javax.swing.JToolBar();
  108. jButton27 = new javax.swing.JButton();
  109. jButton28 = new javax.swing.JButton();
  110. jButton30 = new javax.swing.JButton();
  111. jButton31 = new javax.swing.JButton();
  112. jButton32 = new javax.swing.JButton();
  113. jSeparator5.setName("jSeparator5"); // NOI18N
  114. setName("Form"); // NOI18N
  115. java.util.ResourceBundle bundle = mpv5.i18n.LanguageManager.getBundle(); // NOI18N
  116. jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(bundle.getString("PreviewPanel.jPanel1.border.title"))); // NOI18N
  117. jPanel1.setName("jPanel1"); // NOI18N
  118. ppanel.setBorder(javax.swing.BorderFactory.createEtchedBorder());
  119. ppanel.setName("ppanel"); // NOI18N
  120. ppanel.setLayout(new java.awt.BorderLayout());
  121. toolbar.setRollover(true);
  122. toolbar.setName("toolbar"); // NOI18N
  123. jButton27.setIcon(new javax.swing.ImageIcon(getClass().getResource("/mpv5/resources/images/32/printer.png"))); // NOI18N
  124. jButton27.setText(bundle.getString("PreviewPanel.jButton27.text")); // NOI18N
  125. jButton27.setToolTipText(bundle.getString("PreviewPanel.jButton27.toolTipText")); // NOI18N
  126. jButton27.setContentAreaFilled(false);
  127. jButton27.setFocusable(false);
  128. jButton27.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
  129. jButton27.setName("jButton27"); // NOI18N
  130. jButton27.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
  131. jButton27.addActionListener(new java.awt.event.ActionListener() {
  132. public void actionPerformed(java.awt.event.ActionEvent evt) {
  133. jButton27ActionPerformed(evt);
  134. }
  135. });
  136. toolbar.add(jButton27);
  137. jButton28.setIcon(new javax.swing.ImageIcon(getClass().getResource("/mpv5/resources/images/32/mail_reply.png"))); // NOI18N
  138. jButton28.setText(bundle.getString("PreviewPanel.jButton28.text")); // NOI18N
  139. jButton28.setToolTipText(bundle.getString("PreviewPanel.jButton28.toolTipText")); // NOI18N
  140. jButton28.setContentAreaFilled(false);
  141. jButton28.setFocusable(false);
  142. jButton28.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
  143. jButton28.setName("jButton28"); // NOI18N
  144. jButton28.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
  145. jButton28.addActionListener(new java.awt.event.ActionListener() {
  146. public void actionPerformed(java.awt.event.ActionEvent evt) {
  147. jButton28ActionPerformed(evt);
  148. }
  149. });
  150. toolbar.add(jButton28);
  151. jButton30.setIcon(new javax.swing.ImageIcon(getClass().getResource("/mpv5/resources/images/32/acroread.png"))); // NOI18N
  152. jButton30.setText(bundle.getString("PreviewPanel.jButton30.text")); // NOI18N
  153. jButton30.setToolTipText(bundle.getString("PreviewPanel.jButton30.toolTipText")); // NOI18N
  154. jButton30.setContentAreaFilled(false);
  155. jButton30.setFocusable(false);
  156. jButton30.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
  157. jButton30.setName("jButton30"); // NOI18N
  158. jButton30.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
  159. jButton30.addActionListener(new java.awt.event.ActionListener() {
  160. public void actionPerformed(java.awt.event.ActionEvent evt) {
  161. jButton30ActionPerformed(evt);
  162. }
  163. });
  164. toolbar.add(jButton30);
  165. jButton31.setIcon(new javax.swing.ImageIcon(getClass().getResource("/mpv5/resources/images/32/ark.png"))); // NOI18N
  166. jButton31.setText(bundle.getString("PreviewPanel.jButton31.text")); // NOI18N
  167. jButton31.setToolTipText(bundle.getString("PreviewPanel.jButton31.toolTipText")); // NOI18N
  168. jButton31.setContentAreaFilled(false);
  169. jButton31.setFocusable(false);
  170. jButton31.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
  171. jButton31.setName("jButton31"); // NOI18N
  172. jButton31.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
  173. jButton31.addActionListener(new java.awt.event.ActionListener() {
  174. public void actionPerformed(java.awt.event.ActionEvent evt) {
  175. jButton31ActionPerformed(evt);
  176. }
  177. });
  178. toolbar.add(jButton31);
  179. jButton32.setIcon(new javax.swing.ImageIcon(getClass().getResource("/mpv5/resources/images/32/fileexport.png"))); // NOI18N
  180. jButton32.setText(bundle.getString("PreviewPanel.jButton32.text")); // NOI18N
  181. jButton32.setToolTipText(bundle.getString("PreviewPanel.jButton32.toolTipText")); // NOI18N
  182. jButton32.setContentAreaFilled(false);
  183. jButton32.setFocusable(false);
  184. jButton32.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
  185. jButton32.setName("jButton32"); // NOI18N
  186. jButton32.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
  187. jButton32.addActionListener(new java.awt.event.ActionListener() {
  188. public void actionPerformed(java.awt.event.ActionEvent evt) {
  189. jButton32ActionPerformed(evt);
  190. }
  191. });
  192. toolbar.add(jButton32);
  193. javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
  194. jPanel1.setLayout(jPanel1Layout);
  195. jPanel1Layout.setHorizontalGroup(
  196. jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  197. .addComponent(toolbar, javax.swing.GroupLayout.DEFAULT_SIZE, 547, Short.MAX_VALUE)
  198. .addComponent(ppanel, javax.swing.GroupLayout.DEFAULT_SIZE, 547, Short.MAX_VALUE)
  199. );
  200. jPanel1Layout.setVerticalGroup(
  201. jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  202. .addGroup(jPanel1Layout.createSequentialGroup()
  203. .addComponent(toolbar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  204. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  205. .addComponent(ppanel, javax.swing.GroupLayout.DEFAULT_SIZE, 45, Short.MAX_VALUE))
  206. );
  207. javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
  208. this.setLayout(layout);
  209. layout.setHorizontalGroup(
  210. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  211. .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  212. );
  213. layout.setVerticalGroup(
  214. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  215. .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  216. );
  217. }// </editor-fold>//GEN-END:initComponents
  218. private void jButton27ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton27ActionPerformed
  219. if (file != null) {
  220. try {
  221. new PrintJob().print(file);
  222. } catch (Exception ex) {
  223. Log.Debug(ex);
  224. }
  225. }
  226. }//GEN-LAST:event_jButton27ActionPerformed
  227. private void jButton28ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton28ActionPerformed
  228. if (dataOwner != null) {
  229. try {
  230. file = FileDirectoryHandler.copyFile2(file, new File(FileDirectoryHandler.getTempDir() + ((Formattable) dataOwner).getFormatHandler().toUserString() + ".pdf"));
  231. } catch (Exception ex) {
  232. Log.Debug(ex);
  233. }
  234. }
  235. MailMessage m = null;
  236. if (dataOwner != null && dataOwner.isExisting()) {
  237. QueryCriteria c = new QueryCriteria("usersids", mpv5.db.objects.User.getCurrentUser().__getIDS());
  238. try {
  239. m = (MailMessage) Popup.SelectValue(DatabaseObject.getObjects(Context.getMessage(), c), Messages.SELECT_A_TEMPLATE);
  240. } catch (Exception ex) {
  241. Log.Debug(this, ex.getMessage());
  242. }
  243. if (dataOwner instanceof Item) {
  244. try {
  245. Contact cont = ((Contact) Contact.getObject(Context.getContact(), ((Item) dataOwner).__getContactsids()));
  246. if (mpv5.db.objects.User.getCurrentUser().__getMail().contains("@") && mpv5.db.objects.User.getCurrentUser().__getMail().contains(".") && cont.__getMailaddress().contains("@") && cont.__getMailaddress().contains(".")) {
  247. SimpleMail pr = new SimpleMail();
  248. pr.setMailConfiguration(mpv5.db.objects.User.getCurrentUser().getMailConfiguration());
  249. pr.setRecipientsAddress(cont.__getMailaddress());
  250. if (m != null && m.__getCname() != null) {
  251. pr.setSubject(m.__getCname());
  252. pr.setText(VariablesHandler.parse(m.__getDescription(), dataOwner));
  253. }
  254. try {
  255. pr.set(file, (Exception) null);
  256. } catch (Exception ex) {
  257. Log.Debug(ex);
  258. }
  259. } else {
  260. Popup.notice(Messages.NO_MAIL_DEFINED);
  261. }
  262. } catch (NodataFoundException nodataFoundException) {
  263. Log.Debug(nodataFoundException);
  264. } catch (UnsupportedOperationException unsupportedOperationException) {
  265. Popup.notice(Messages.NO_MAIL_CONFIG);
  266. }
  267. }
  268. } else {
  269. Popup.notice(Messages.NO_TEMPLATE_LOADED + " (" + mpv5.db.objects.User.getCurrentUser() + ")");
  270. }
  271. }//GEN-LAST:event_jButton28ActionPerformed
  272. private void jButton31ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton31ActionPerformed
  273. if (dataOwner != null) {
  274. try {
  275. file = FileDirectoryHandler.copyFile2(file, new File(FileDirectoryHandler.getTempDir() + ((Formattable) dataOwner).getFormatHandler().toUserString() + ".pdf"));
  276. } catch (Exception ex) {
  277. Log.Debug(ex);
  278. }
  279. }
  280. if (dataOwner != null && dataOwner.isExisting()) {
  281. if (QueryHandler.instanceOf().clone(Context.getFiles()).insertFile(file, dataOwner, QueryCriteria.getSaveStringFor(((Formattable) dataOwner).getFormatHandler().toUserString().toString()))) {
  282. Popup.notice(Messages.FILE_SAVED.toString() + file.getName());
  283. if (parent != null) {
  284. parent.refresh();
  285. }
  286. }
  287. } else {
  288. Popup.notice(Messages.NOT_POSSIBLE);
  289. }
  290. }//GEN-LAST:event_jButton31ActionPerformed
  291. private void jButton32ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton32ActionPerformed
  292. DialogForFile d = new DialogForFile(DialogForFile.FILES_ONLY);
  293. if (dataOwner != null) {
  294. try {
  295. file = FileDirectoryHandler.copyFile2(file, new File(FileDirectoryHandler.getTempDir() + ((Formattable) dataOwner).getFormatHandler().toUserString() + ".pdf"));
  296. } catch (Exception ex) {
  297. Log.Debug(ex);
  298. }
  299. }
  300. if (dataOwner == null) {
  301. d.setSelectedFile(new File(file.getName()));
  302. } else {
  303. d.setSelectedFile(new File(((Formattable) dataOwner).getFormatHandler().toUserString() + ".pdf"));
  304. }
  305. if (d.saveFile()) {
  306. d.getFile().delete();
  307. if (file.renameTo(d.getFile())) {
  308. try {
  309. mpv5.YabsViewProxy.instance().addMessage(Messages.FILE_SAVED + d.getFile().getCanonicalPath());
  310. } catch (IOException ex) {
  311. Log.Debug(ex);
  312. }
  313. }
  314. }
  315. }//GEN-LAST:event_jButton32ActionPerformed
  316. private void jButton30ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton30ActionPerformed
  317. if (dataOwner != null) {
  318. try {
  319. file = FileDirectoryHandler.copyFile2(file, new File(FileDirectoryHandler.getTempDir() + ((Formattable) dataOwner).getFormatHandler().toUserString() + ".pdf"));
  320. } catch (Exception ex) {
  321. Log.Debug(ex);
  322. }
  323. }
  324. FileDirectoryHandler.open(file);
  325. }//GEN-LAST:event_jButton30ActionPerformed
  326. int pagen = 1;
  327. // Variables declaration - do not modify//GEN-BEGIN:variables
  328. private javax.swing.JButton jButton27;
  329. private javax.swing.JButton jButton28;
  330. private javax.swing.JButton jButton30;
  331. private javax.swing.JButton jButton31;
  332. private javax.swing.JButton jButton32;
  333. private javax.swing.JPanel jPanel1;
  334. private javax.swing.JSeparator jSeparator5;
  335. private javax.swing.JPanel ppanel;
  336. private javax.swing.JToolBar toolbar;
  337. // End of variables declaration//GEN-END:variables
  338. // public void openOdt(File file) {
  339. // this.file = file;
  340. // Log.Debug(this, "Preparing preview for: " + file);
  341. // OOOPanel op = new OOOPanel();
  342. // ppanel.removeAll();
  343. // ppanel.setLayout(new BorderLayout());
  344. // ppanel.add(op, BorderLayout.CENTER);
  345. //
  346. // op.constructOOOPanel(file);
  347. // this.validate();
  348. // }
  349. public void open(File file) {
  350. this.file = file;
  351. FileReaderWriter f = new FileReaderWriter(file);
  352. String t = f.read();
  353. JEditorPane p = new JEditorPane();
  354. p.setText(t);
  355. ppanel.removeAll();
  356. ppanel.add(p, BorderLayout.CENTER);
  357. ppanel.validate();
  358. }
  359. /**
  360. * Show this panel in a new frame
  361. * @param title
  362. */
  363. public void showInNewFrame(String title) {
  364. BigPopup.showPopup(this, jPanel1, title, mpv5.YabsViewProxy.instance().getIdentifierFrame().getHeight(), 800);
  365. }
  366. /**
  367. * @return the dataOwner
  368. */
  369. public DatabaseObject getDataOwner() {
  370. return dataOwner;
  371. }
  372. /**
  373. * @param dataOwner the dataOwner to set
  374. */
  375. public void setDataOwner(DatabaseObject dataOwner) {
  376. this.dataOwner = dataOwner;
  377. }
  378. public void set(Object object, Exception exception) throws Exception {
  379. if (exception == null) {
  380. try {
  381. setCursor(new Cursor(Cursor.WAIT_CURSOR));
  382. if (object instanceof List) {
  383. showInNewFrame("Export");
  384. openl(((Export) ((List) object).get(0)).getTargetFile());
  385. } else if (object instanceof Export) {
  386. showInNewFrame("Export");
  387. openl(((Export) object).getTargetFile());
  388. } else {
  389. showInNewFrame(((File) object).getName());
  390. openl((File) object);
  391. }
  392. } catch (Exception e) {
  393. throw e;
  394. } finally {
  395. setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
  396. }
  397. } else {
  398. throw exception;
  399. }
  400. }
  401. private void openl(File file) {
  402. if (file.getName().split("\\.").length < 2) {
  403. throw new UnsupportedOperationException("The file must have an extension: " + file);
  404. }
  405. this.file = file;
  406. String extension = file.getName().substring(file.getName().lastIndexOf("."), file.getName().length());
  407. Log.Debug(this, "Found extension: " + extension);
  408. if (extension.equalsIgnoreCase(".pdf")) {
  409. try {
  410. openPdf(file);
  411. } catch (Exception ex) {
  412. Log.Debug(ex);
  413. }
  414. } else if (extension.equalsIgnoreCase(".odt")) {
  415. try {
  416. FileDirectoryHandler.open(file);
  417. makeToolBar();
  418. } catch (Exception ex) {
  419. Log.Debug(ex);
  420. }
  421. } else if (extension.equalsIgnoreCase(".txt")) {
  422. try {
  423. open(file);
  424. } catch (Exception ex) {
  425. Log.Debug(ex);
  426. }
  427. } else {
  428. FileDirectoryHandler.open(file);
  429. makeToolBar();
  430. }
  431. }
  432. /**
  433. * @param parent the parent to set
  434. */
  435. public void setParent(DataPanel parent) {
  436. this.parent = parent;
  437. }
  438. private void makeToolBar() {
  439. jPanel1.remove(ppanel);
  440. validate();
  441. try {
  442. BigPopup.pack(this);
  443. BigPopup.setOnTop(this);
  444. } catch (Exception exception) {
  445. }
  446. }
  447. }