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