001package com.ganteater.ae.desktop.ui; 002 003import java.awt.Cursor; 004import java.awt.Desktop; 005import java.awt.Dimension; 006import java.awt.Toolkit; 007import java.awt.datatransfer.Clipboard; 008import java.awt.datatransfer.StringSelection; 009import java.awt.event.ActionEvent; 010import java.awt.event.MouseAdapter; 011import java.awt.event.MouseEvent; 012import java.awt.event.WindowAdapter; 013import java.awt.event.WindowEvent; 014import java.io.File; 015import java.io.FileInputStream; 016import java.net.URI; 017 018import javax.swing.AbstractAction; 019import javax.swing.JDialog; 020import javax.swing.JEditorPane; 021import javax.swing.JLabel; 022import javax.swing.JMenuItem; 023import javax.swing.JOptionPane; 024import javax.swing.JPopupMenu; 025import javax.swing.JScrollPane; 026import javax.swing.WindowConstants; 027 028import org.apache.commons.io.IOUtils; 029import org.apache.commons.lang.StringUtils; 030 031import com.ganteater.ae.desktop.WorkPlace; 032import com.ganteater.ae.desktop.util.UIUtils; 033import com.ganteater.ae.processor.Processor; 034import com.ganteater.ae.util.xml.easyparser.Node; 035 036public class AttachButton extends JLabel { 037 038 private static final String ATTACH_FRAME = "attach_frame"; 039 private static final long serialVersionUID = 1L; 040 041 private String fileName; 042 private String description; 043 044 public AttachButton(WorkPlace workplace, Node aFileNode, String taskName) { 045 Processor processor = workplace.getTaskProcessor(); 046 String name = aFileNode.getAttribute("name"); 047 name = processor.replaceProperties(name); 048 049 try { 050 File file = workplace.getManager().getFile(name); 051 if (file != null && file.exists()) { 052 fileName = file.getAbsolutePath(); 053 } else { 054 fileName = name; 055 } 056 } catch (Exception e) { 057 setEnabled(false); 058 } 059 060 setToolTipText(name); 061 062 description = aFileNode.getAttribute("description"); 063 064 addMouseListener(new MouseAdapter() { 065 @Override 066 public void mouseClicked(MouseEvent e) { 067 if (e.getButton() == MouseEvent.BUTTON1 && isEnabled()) { 068 try { 069 String theType = null; 070 071 if (StringUtils.startsWithIgnoreCase(fileName, "http")) { 072 Desktop.getDesktop().browse(new URI(fileName)); 073 } else if (fileName.toUpperCase().endsWith(".HTM") 074 || fileName.toUpperCase().endsWith(".HTML")) { 075 theType = "text/html"; 076 } else if (fileName.toUpperCase().endsWith(".TXT")) { 077 theType = "text/plain"; 078 } else if (fileName.toUpperCase().endsWith(".RTF")) { 079 theType = "text/rtf"; 080 } else { 081 Desktop.getDesktop().open(new File(fileName)); 082 } 083 084 if (theType != null) { 085 JDialog theAttachFrame = new JDialog(workplace.getAeFrame(), description, false); 086 theAttachFrame.setTitle(StringUtils.defaultIfEmpty(description, 087 "About: \"" + taskName + "\" - " + fileName)); 088 089 theAttachFrame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); 090 String theText = IOUtils 091 .toString(new FileInputStream(workplace.getManager().getFile(fileName))); 092 JEditorPane editorPane = new JEditorPane(); 093 094 editorPane.setEditorKit(JEditorPane.createEditorKitForContentType(theType)); 095 editorPane.setText(theText); 096 editorPane.addHyperlinkListener(new HyperlinkAdapter(workplace.getManager())); 097 098 editorPane.setEditable(false); 099 JScrollPane theScroll = new JScrollPane(editorPane); 100 theAttachFrame.setContentPane(theScroll); 101 theAttachFrame.pack(); 102 103 Dimension size = theAttachFrame.getSize(); 104 if (size.getWidth() > 700) 105 size.width = 700; 106 if (size.getHeight() > 500) 107 size.height = 500; 108 109 UIUtils.applyPreferedView(theAttachFrame, ATTACH_FRAME + fileName, size.height, size.width); 110 111 theAttachFrame.addWindowListener(new WindowAdapter() { 112 @Override 113 public void windowClosing(WindowEvent e) { 114 UIUtils.saveDialogPreferedView(theAttachFrame, ATTACH_FRAME + fileName); 115 super.windowClosing(e); 116 } 117 118 @Override 119 public void windowDeactivated(WindowEvent e) { 120 UIUtils.saveDialogPreferedView(theAttachFrame, ATTACH_FRAME + fileName); 121 } 122 }); 123 124 theAttachFrame.setVisible(true); 125 } 126 127 } catch (Exception e1) { 128 JOptionPane.showMessageDialog(JOptionPane.getRootFrame(), e1.getMessage(), 129 "Attach opening is failed", JOptionPane.ERROR_MESSAGE); 130 workplace.getLogger().error("Throubles with coping attach.", e1); 131 } 132 } 133 134 if (e.getButton() == MouseEvent.BUTTON3) { 135 final JPopupMenu popup = new JPopupMenu(); 136 JMenuItem jMenuItem = new JMenuItem("Copy Path"); 137 jMenuItem.addActionListener(new AbstractAction() { 138 private static final long serialVersionUID = 1L; 139 140 @Override 141 public void actionPerformed(ActionEvent var1) { 142 StringSelection stringSelection = new StringSelection(fileName); 143 Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); 144 clipboard.setContents(stringSelection, null); 145 } 146 147 }); 148 popup.add(jMenuItem); 149 popup.show(e.getComponent(), e.getX(), e.getY()); 150 } 151 } 152 }); 153 154 setCursor(new Cursor(Cursor.HAND_CURSOR)); 155 setIcon(AEFrame.getIcon("doc.png")); 156 if (description != null) { 157 setToolTipText(description); 158 } 159 } 160 161}