1 54 55 package com.mullassery.act.gui; 56 57 import java.awt.BorderLayout ; 58 import java.awt.Component ; 59 import java.awt.Cursor ; 60 import java.awt.FlowLayout ; 61 import java.awt.event.ActionEvent ; 62 import java.awt.event.ActionListener ; 63 import java.awt.event.MouseAdapter ; 64 import java.awt.event.MouseEvent ; 65 66 import javax.swing.AbstractAction ; 67 import javax.swing.JButton ; 68 import javax.swing.JComponent ; 69 import javax.swing.JDialog ; 70 import javax.swing.JFrame ; 71 import javax.swing.JOptionPane ; 72 import javax.swing.JPanel ; 73 import javax.swing.JPopupMenu ; 74 import javax.swing.JTabbedPane ; 75 import javax.swing.SwingUtilities ; 76 77 import org.w3c.dom.Element ; 78 import org.w3c.dom.NodeList ; 79 80 import com.mullassery.act.ACTException; 81 import com.mullassery.act.TaskMaster; 82 import com.mullassery.act.gui.components.TFileField; 83 import com.mullassery.act.gui.components.TLabel; 84 import com.mullassery.act.util.GUIUtil; 85 86 91 public class TaskPanel extends JDialog { 92 protected static int incX, incY; 93 protected TFileField baseDir; 94 protected OutputPanel op; 95 protected TaskMaster taskRunner; 96 protected DataPanel dataPanel; 97 protected Element taskElement; 98 protected JTabbedPane tPane = new JTabbedPane (); 99 100 public TaskPanel(final JFrame par, final TaskMaster tr, Element vals) 101 throws ACTException { 102 super(par, "Task " + vals.getAttribute(GUIUtil.TASK_NAME)); 103 this.getContentPane().setLayout(new BorderLayout ()); 104 105 final JPopupMenu popup = new JPopupMenu (); 106 popup.add(new TabCloseAction()); 107 tPane.addMouseListener(new MouseAdapter () { 108 public void mouseReleased(MouseEvent e) { 109 showPopup(e); 110 } 111 public void mousePressed(MouseEvent e) { 112 showPopup(e); 113 } 114 public void mouseClicked(MouseEvent e) { 115 showPopup(e); 116 } 117 private void showPopup(MouseEvent e) { 118 if (e.isPopupTrigger() 119 && !tPane.getSelectedComponent().equals(dataPanel)) { 120 popup.show(tPane, e.getX(), e.getY()); 121 } 122 } 123 }); 124 125 this.taskRunner = tr; 126 this.taskElement = vals; 127 String name = taskElement.getAttribute(GUIUtil.TASK_NAME); 128 String displayName = name; 129 130 NodeList nChildren = taskElement.getElementsByTagName(name); 131 Element dataElement; 132 if (nChildren.getLength() > 0) { 133 dataElement = (Element ) nChildren.item(0); 134 } else { 135 dataElement = taskElement.getOwnerDocument().createElement(name); 136 this.taskElement.appendChild(dataElement); 137 } 138 139 Class clazz = tr.getElementClass(name); 140 this.dataPanel = 141 new DataPanel(this, null, displayName, clazz, dataElement); 142 tPane.setSelectedIndex(0); 143 144 final JPanel bdp = new JPanel (new FlowLayout (FlowLayout.RIGHT, 2, 0)); 145 baseDir = new TFileField("."); 146 bdp.add(new TLabel(" Base Directory ")); 147 bdp.add(baseDir); 148 this.getContentPane().add(bdp, BorderLayout.NORTH); 149 150 this.getContentPane().add(tPane, BorderLayout.CENTER); 151 152 final JButton execute = new JButton ("Execute"); 153 execute.addActionListener(new ActionListener () { 154 public void actionPerformed(ActionEvent ae) { 155 SwingUtilities.invokeLater(new Runnable () { 156 public void run() { 157 executeTask(); 158 } 159 }); 160 } 161 }); 162 this.getContentPane().add(execute, BorderLayout.SOUTH); 163 164 setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); 165 GUIUtil.centralize(this); 166 pack(); 167 show(); 168 } 169 170 void addToTaskPanel(String tabTitle, JComponent dp) { 171 tPane.addTab(tabTitle, dp); 172 tPane.setSelectedIndex(tPane.indexOfTab(tabTitle)); 173 } 174 175 void removeFromTaskPanel(JComponent dp) { 176 tPane.remove(dp); 177 } 178 179 void executeTask() { 180 Cursor def = getCursor(); 181 try { 182 final Element val = dataPanel.getValues(); 183 if (op == null) 184 op = new OutputPanel(); 185 addToTaskPanel("Output: " + dataPanel.fullName, op); 186 pack(); 187 setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); 188 taskRunner.executeTask(val.getNodeName(), getBaseDir(), val, op); 189 } catch (Exception ex) { 190 JOptionPane.showMessageDialog( 191 this, 192 "Could execute task. " + ex.getMessage(), 193 "ACT Error", 194 JOptionPane.ERROR_MESSAGE); 195 } finally { 196 setCursor(def); 197 } 198 } 199 200 Element getTaskValues() { 201 dataPanel.getValues(); return this.taskElement; 203 } 204 205 String getBaseDir() { 206 return baseDir.getText(); 207 } 208 209 String getTaskName(){ 210 return dataPanel.fullName; 211 } 212 213 public class TabCloseAction extends AbstractAction { 214 TabCloseAction() { 215 this.putValue(NAME, "Remove"); 216 this.putValue(SHORT_DESCRIPTION, "Removes this element."); 217 } 218 219 public void actionPerformed(ActionEvent e) { 220 Component comp = tPane.getSelectedComponent(); 221 if (comp instanceof DataPanel 222 && GUIUtil.getConfirmation( 223 TaskPanel.this, 224 "Do you want to delete " 225 + tPane.getTitleAt(tPane.getSelectedIndex()) 226 + " and its children?")) { 227 DataPanel dp = (DataPanel) comp; 228 dp.par.removeChildPanel(dp); 229 } 230 tPane.remove(comp); 231 } 232 233 } 234 } | Popular Tags |