📄 quickviewer.java
字号:
JDialog dlg = new PropertiesDialog( null, part, false ); dlg.show(); } private void viewMessagePart( Part part ) { if ( Package.DEBUG && Package.isTraceable( "QuickViewer" ) ) { System.out.println( "QuickViewer.viewMessagePart(p)" ); } String action = ICEMail.getBundle().getString( "QuickViewer.viewing" ); if ( ! okLargeAttachment( part, action ) ) return; MainFrame.getInstance().setWaitCursor(); try { DataHandler dh = part.getDataHandler(); Object viewer = getMailcapViewer( dh ); if ( viewer != null ) { if ( viewer instanceof Component ) { String title = MessageUtilities.getFileName( part ); if ( title == null || title.length() < 1 ) title = "Attachment"; ComponentFrame frame = new ComponentFrame( (Component)viewer, title, dh.getDataSource() ); frame.show(); } } } catch ( MessagingException ex ) { ex.printStackTrace( System.err ); } MainFrame.getInstance().resetCursor(); } private void viewMessagePartAs( Part part ) { if ( Package.DEBUG && Package.isTraceable( "QuickViewer" ) ) { System.out.println( "QuickViewer.viewMessagePartAs(p)" ); } String action = ICEMail.getBundle().getString( "QuickViewer.viewing" ); if ( ! okLargeAttachment( part, action ) ) return; try { DataHandler dh = part.getDataHandler(); ViewAsDialog dlg = new ViewAsDialog( (Frame) getTopLevelAncestor(), part ); dlg.show(); String viewAsType = dlg.getViewAsType(); if ( viewAsType != null ) { ViewAsDataSource newSource = new ViewAsDataSource( viewAsType, dh ); DataHandler newDh = new DataHandler( newSource ); Object viewer = getMailcapViewer( newDh ); if ( viewer != null ) { if ( viewer instanceof Component ) { ComponentFrame frame = new ComponentFrame( (Component)viewer, "Attachment", newSource ); frame.show(); } } } } catch ( MessagingException ex ) { ex.printStackTrace( System.err ); } catch ( MimeTypeParseException ex ) { ex.printStackTrace( System.err ); } } private Object getMailcapViewer( DataHandler dh ) { if ( Package.DEBUG && Package.isTraceable( "QuickViewer" ) ) { System.out.println( "QuickViewer.getMailcapViewer(d): " + dh ); } Object bean = null; String xtext = null; CommandInfo ci = dh.getCommand( "view" ); if ( Package.DEBUG && Debug_ > 0 ) { System.out.println( "QuickViewer.getMailcapViewer(d): CommandInfo - " + ci ); } if ( ci == null ) { String contentType = dh.getContentType(); try { MimeType mime = new MimeType( contentType ); contentType = mime.getBaseType(); } catch ( MimeTypeParseException ex ) { } Object[] xargs = new Object[1]; xargs[0] = contentType; xtext = ICEMail.getBundle().getFormatString( "QuickViewer.noviewcommand", xargs ); } else { try { bean = dh.getBean( ci ); } catch ( Exception ex ) { if ( Package.DEBUG && Debug_ > 0 ) { System.out.println( "QuickViewer.getMailcapViewer(d): getBean(ci) failed - " + ex ); } bean = null; } if ( bean == null ) { Object[] xargs = new Object[2]; xargs[0] = ci.getCommandName(); xargs[1] = ci.getCommandClass(); xtext = ICEMail.getBundle().getFormatString( "QuickViewer.noviewer", xargs ); } } if ( bean == null ) { bean = new JTextArea( xtext ); } return bean; } private ImageIcon getMailcapIcon( DataHandler dh ) { if ( Package.DEBUG && Package.isTraceable( "QuickViewer" ) ) { System.out.println( "QuickViewer.getMailcapIcon(d): " + dh ); } ImageIcon result = QuickViewer.AttUnknownIcon; CommandInfo ci = dh.getCommand( "icon" ); if ( ci != null ) { result = (ImageIcon) dh.getBean( ci ); } return result; } private void saveMessagePartToFile( Part part ) { if ( Package.DEBUG && Package.isTraceable( "QuickViewer" ) ) { System.out.println( "QuickViewer.saveMessagePartToFile(p)" ); } String action = ICEMail.getBundle().getString( "QuickViewer.saving" ); if ( ! okLargeAttachment( part, action ) ) return; try { String fileName = MessageUtilities.getFileName( part ); FileDialog dialog = new FileDialog( (Frame)getTopLevelAncestor(), ICEMail.getBundle().getString( "QuickViewer.SaveAttachment" ), FileDialog.SAVE ); dialog.setFile( fileName ); dialog.show(); fileName = dialog.getFile(); String dirName = dialog.getDirectory(); if ( fileName != null && dirName != null ) { MainFrame.getInstance().setWaitCursor(); InputStream is = null; FileOutputStream os = null; File outputFile = new File( dirName, fileName ); try { is = part.getInputStream(); os = new FileOutputStream( outputFile ); byte[] buf = new byte[ 16 * 1024 ]; for ( ; ; ) { int bytes = is.read( buf ); if ( bytes < 0 ) break; if ( bytes > 0 ) os.write( buf, 0, bytes ); } is.close(); os.close(); } catch ( IOException ex ) { ex.printStackTrace( System.err ); try { if ( is != null ) is.close(); if ( os != null ) os.close(); } catch ( IOException iox ) { } Object[] xargs = new Object[2]; xargs[0] = new String( "IOException" ); xargs[1] = ex.getMessage(); ComponentFactory.showDialog( ICEMail.getBundle(), "QuickViewer.SaveError", 0, JOptionPane.ERROR_MESSAGE, xargs ); } MainFrame.getInstance().resetCursor(); } } catch ( MessagingException ex ) { Object[] xargs = new Object[2]; xargs[0] = new String( "MessagingException" ); xargs[1] = ex.getMessage(); ComponentFactory.showDialog( ICEMail.getBundle(), "QuickViewer.SaveError", 0, JOptionPane.ERROR_MESSAGE, xargs ); } } private boolean okLargeAttachment( Part part, String action ) { boolean result = true; try { int partSize = part.getSize(); if ( partSize > (2048 * 1024) ) { double megs = ((double)partSize) / ( 1024.0 * 1024.0 ); DecimalFormat fmt = new DecimalFormat( "#,##0.00" ); Object[] xargs = new Object[2]; xargs[0] = fmt.format( megs ); xargs[1] = action; int option = ComponentFactory.showDialog( ICEMail.getBundle(), "QuickViewer.LargeAttach", 0, JOptionPane.QUESTION_MESSAGE, xargs ); if ( option == JOptionPane.YES_OPTION ) result = true; else result = false; } } catch ( MessagingException ex ) { } return result; }//............................................................// I N N E R C L A S S E S//............................................................ private class IActionListener implements ActionListener { /** * Invoked when an action occurs. * <p> * Implementation of java.awt.event.ActionListener.actionPerformed() * * @param event the action event that occured */ public void actionPerformed( ActionEvent event ) { String command = event.getActionCommand(); if ( Package.DEBUG && Package.isTraceable( "QuickViewer" ) ) { System.out.println( "IActionListener.actionPerformed(ae): " + command ); } if ( command.startsWith( "POPUP:" ) ) { if ( command.equals( "POPUP:SAVE" ) ) { saveMessagePartToFile( popupAttachPart_ ); } else if ( command.equals( "POPUP:VIEW" ) ) { viewMessagePart( popupAttachPart_ ); } else if ( command.equals( "POPUP:VIEWAS" ) ) { viewMessagePartAs( popupAttachPart_ ); } else if ( command.equals( "POPUP:PROPS" ) ) { viewPartProperties( popupAttachPart_ ); } } else if ( command.equals( "CUT" ) ) { if ( editorPane_ != null ) { editorPane_.cut(); } } else if ( command.equals( "COPY" ) ) { if ( editorPane_ != null ) { editorPane_.copy(); } } else if ( command.equals( "PASTE" ) ) { if ( editorPane_ != null ) { editorPane_.paste(); } } else if ( command.equals( "UNDO" ) ) { if ( undo_ != null && undo_.canUndoOrRedo() ) { undo_.undoOrRedo(); } } } }//............................................................ private class SaveAttachementAction implements ActionListener { Part i_part_; public SaveAttachementAction( Part part ) { i_part_ = part; } public void actionPerformed( ActionEvent e ) { saveMessagePartToFile( i_part_ ); } }//............................................................ private class ViewAttachementAction implements ActionListener { Part i_part_; public ViewAttachementAction( Part part ) { i_part_ = part; } public void actionPerformed( ActionEvent e ) { viewMessagePart( i_part_ ); } }//............................................................ private class IMouseAdapter extends MouseAdapter { private Component i_parent_ = null; private JPopupMenu i_popupmenu_ = null; private Part i_part_ = null; private boolean i_isPopupTrigger_ = false; public IMouseAdapter( Component parent, JPopupMenu menu, Part bodyPart ) { super(); i_parent_ = parent; i_popupmenu_ = menu; i_part_ = bodyPart; } public void mousePressed( MouseEvent event ) { i_isPopupTrigger_ = false; if ( event.isPopupTrigger() ) { i_isPopupTrigger_ = true; popupAttachPart_ = i_part_; i_popupmenu_.show( i_parent_, event.getX(), event.getY() ); } } public void mouseReleased( MouseEvent event ) { if ( i_isPopupTrigger_ ) return; if ( event.isPopupTrigger() ) { i_isPopupTrigger_ = true; popupAttachPart_ = i_part_; i_popupmenu_.show( i_parent_, event.getX(), event.getY() ); } } public void mouseClicked( MouseEvent event ) { if ( i_isPopupTrigger_ ) { i_isPopupTrigger_ = false; } } }//............................................................ private class IHyperlinkListener implements HyperlinkListener { public void hyperlinkUpdate( HyperlinkEvent event ) { HyperlinkEvent.EventType type = event.getEventType(); Object source = event.getSource(); URL url = event.getURL(); if ( type == HyperlinkEvent.EventType.ACTIVATED ) { MainFrame.getInstance().setWaitCursor(); HotJavaMailBrowser.setBrowserDocumentURL( url ); MainFrame.getInstance().resetCursor(); } else if ( type == HyperlinkEvent.EventType.ENTERED ) { // This and EXITED do not work (why?)! When they // do, we will probably want to toggle the cursor. } else if ( type == HyperlinkEvent.EventType.EXITED ) { } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -