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 File file = workplace.getManager().getFile(name); 050 if (file != null && file.exists()) { 051 fileName = file.getAbsolutePath(); 052 } else { 053 fileName = name; 054 } 055 056 setToolTipText(name); 057 058 description = aFileNode.getAttribute("description"); 059 060 addMouseListener(new MouseAdapter() { 061 @Override 062 public void mouseClicked(MouseEvent e) { 063 if (e.getButton() == MouseEvent.BUTTON1) { 064 try { 065 String theType = null; 066 067 if (StringUtils.startsWithIgnoreCase(fileName, "http")) { 068 Desktop.getDesktop().browse(new URI(fileName)); 069 } else if (fileName.toUpperCase().endsWith(".HTM") 070 || fileName.toUpperCase().endsWith(".HTML")) { 071 theType = "text/html"; 072 } else if (fileName.toUpperCase().endsWith(".TXT")) { 073 theType = "text/plain"; 074 } else if (fileName.toUpperCase().endsWith(".RTF")) { 075 theType = "text/rtf"; 076 } else { 077 Desktop.getDesktop().open(new File(fileName)); 078 } 079 080 if (theType != null) { 081 JDialog theAttachFrame = new JDialog(workplace.getAeFrame(), description, false); 082 theAttachFrame.setTitle(StringUtils.defaultIfEmpty(description, 083 "About: \"" + taskName + "\" - " + fileName)); 084 085 theAttachFrame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); 086 String theText = IOUtils 087 .toString(new FileInputStream(workplace.getManager().getFile(fileName))); 088 JEditorPane editorPane = new JEditorPane(); 089 090 editorPane.setEditorKit(JEditorPane.createEditorKitForContentType(theType)); 091 editorPane.setText(theText); 092 editorPane.addHyperlinkListener(new HyperlinkAdapter(workplace.getManager())); 093 094 editorPane.setEditable(false); 095 JScrollPane theScroll = new JScrollPane(editorPane); 096 theAttachFrame.setContentPane(theScroll); 097 theAttachFrame.pack(); 098 099 Dimension size = theAttachFrame.getSize(); 100 if (size.getWidth() > 700) 101 size.width = 700; 102 if (size.getHeight() > 500) 103 size.height = 500; 104 105 UIUtils.applyPreferedView(theAttachFrame, ATTACH_FRAME + fileName, size.height, size.width); 106 107 theAttachFrame.addWindowListener(new WindowAdapter() { 108 @Override 109 public void windowClosing(WindowEvent e) { 110 UIUtils.saveDialogPreferedView(theAttachFrame, ATTACH_FRAME + fileName); 111 super.windowClosing(e); 112 } 113 114 @Override 115 public void windowDeactivated(WindowEvent e) { 116 UIUtils.saveDialogPreferedView(theAttachFrame, ATTACH_FRAME + fileName); 117 } 118 }); 119 120 theAttachFrame.setVisible(true); 121 } 122 123 } catch (Exception e1) { 124 JOptionPane.showMessageDialog(JOptionPane.getRootFrame(), e1.getMessage(), 125 "Attach opening is failed", JOptionPane.ERROR_MESSAGE); 126 workplace.getLogger().error("Throubles with coping attach.", e1); 127 } 128 } 129 130 if (e.getButton() == MouseEvent.BUTTON3) { 131 final JPopupMenu popup = new JPopupMenu(); 132 JMenuItem jMenuItem = new JMenuItem("Copy Path"); 133 jMenuItem.addActionListener(new AbstractAction() { 134 private static final long serialVersionUID = 1L; 135 136 @Override 137 public void actionPerformed(ActionEvent var1) { 138 StringSelection stringSelection = new StringSelection(fileName); 139 Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); 140 clipboard.setContents(stringSelection, null); 141 } 142 143 }); 144 popup.add(jMenuItem); 145 popup.show(e.getComponent(), e.getX(), e.getY()); 146 } 147 } 148 }); 149 150 setCursor(new Cursor(Cursor.HAND_CURSOR)); 151 setIcon(AEFrame.getIcon("doc.png")); 152 if (description != null) { 153 setToolTipText(description); 154 } 155 } 156 157}