package com.c2kernel.gui.tree; import java.awt.datatransfer.DataFlavor; import java.awt.datatransfer.Transferable; import java.awt.datatransfer.UnsupportedFlavorException; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.IOException; import java.util.ArrayList; import java.util.StringTokenizer; import javax.swing.JMenuItem; import javax.swing.JPopupMenu; import com.c2kernel.collection.Aggregation; import com.c2kernel.collection.Collection; import com.c2kernel.collection.CollectionMember; import com.c2kernel.common.ObjectNotFoundException; import com.c2kernel.entity.agent.Job; import com.c2kernel.entity.proxy.ItemProxy; import com.c2kernel.gui.ItemDetails; import com.c2kernel.gui.ItemTabManager; import com.c2kernel.gui.MainFrame; import com.c2kernel.lookup.Path; import com.c2kernel.persistency.ClusterStorage; import com.c2kernel.process.Gateway; import com.c2kernel.utils.Language; import com.c2kernel.utils.Logger; /** * Structure for Item presence on the tree and ItemDetails boxes. Created by NodeFactory. * @author $Author: abranson $ * @version $Version$ */ public class NodeItem extends Node implements Transferable { protected ItemProxy myItem = null; public NodeItem(Path path, ItemTabManager desktop) { super(path, desktop); Logger.msg(2,"NodeEntity. - Creating item for '"+path.toString()+"'."); // if an item - resolve the item and get its properties try { myItem = Gateway.getProxyManager().getProxy(path); this.sysKey = path.getSysKey(); Logger.msg(2,"NodeEntity. - System key is "+this.sysKey); // Name should be the alias if present String alias = myItem.getName(); if (alias != null) this.name = alias; try { this.type = myItem.getProperty("Type"); } catch (ObjectNotFoundException e) { this.type = ""; } String iconString = this.type; if (type.equals("ActivityDesc")) try { iconString = myItem.getProperty("Complexity")+iconString; } catch (ObjectNotFoundException e) { iconString = "error"; } iconString = iconString.toLowerCase(); this.setIcon(iconString); } catch (ObjectNotFoundException e1) { this.sysKey = -1; this.type="Error"; this.name="Entity not found"; this.setIcon("error"); } createTreeNode(); makeExpandable(); } public ItemProxy getItem() { return myItem; } public void openItem() { desktop.add(this); } public Collection getParentCollection() { return parentCollection; } public Integer getSlotNo() { return slotNo; } Collection parentCollection; Integer slotNo = null; static DataFlavor dataFlavor = new DataFlavor(NodeItem.class, "NodeItem"); ItemProxy parentItem; static DataFlavor[] supportedFlavours = new DataFlavor[] { dataFlavor, new DataFlavor(Path.class, "Path"), DataFlavor.getTextPlainUnicodeFlavor() }; public void setCollection(Collection parentCollection, Integer slotNo, ItemProxy parentItem) { this.parentCollection = parentCollection; this.slotNo = slotNo; this.parentItem = parentItem; } @Override public void loadChildren() { try { String collections = myItem.queryData("Collection/all"); StringTokenizer tok = new StringTokenizer(collections, ","); while (tok.hasMoreTokens()) { NodeCollection newCollection = new NodeCollection(myItem, tok.nextToken(), desktop); add(newCollection); } end(false); } catch (Exception e) { Logger.error(e); } } @Override public JPopupMenu getPopupMenu() { JPopupMenu popup = super.getPopupMenu(); JMenuItem openItem = new JMenuItem(Language.translate("Open")); openItem.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { openItem(); } }); popup.addSeparator(); popup.add(openItem); popup.addSeparator(); if (parentCollection != null && MainFrame.isAdmin) { JMenuItem collMenuItem = new JMenuItem("Remove from collection"); //collMenuItem.setActionCommand("removeColl"); collMenuItem.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String[] params = { parentCollection.getName(), String.valueOf(slotNo) }; String predefStepName = parentCollection instanceof Aggregation?"ClearSlot":"RemoveSlotFromCollection"; try { MainFrame.userAgent.execute(parentItem, predefStepName, params); } catch (Exception e1) { MainFrame.exceptionDialog(e1); } } }); popup.add(collMenuItem); popup.addSeparator(); } try { ArrayList jobList = myItem.getJobList(MainFrame.userAgent); ArrayList already = new ArrayList(); if (jobList.size() > 0) { for (Job thisJob : jobList) { String stepName = thisJob.getStepName(); if (already.contains(stepName)) continue; already.add(stepName); JMenuItem menuItem = new JMenuItem(stepName); menuItem.setActionCommand(stepName); menuItem.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { execute(e.getActionCommand()); } }); popup.add(menuItem); } } else { JMenuItem noAct = new JMenuItem("No activities"); noAct.setEnabled(false); popup.add(noAct); } } catch (Exception ex) { JMenuItem error = new JMenuItem("Error querying jobs"); error.setEnabled(false); popup.add(error); } return popup; } public void execute(String stepName) { ItemDetails thisDetail = desktop.add(this); thisDetail.runCommand("Execution", stepName); } public ArrayList getTabs() { ArrayList requiredTabs = new ArrayList(); requiredTabs.add("Properties"); try { String collNames = myItem.queryData(ClusterStorage.COLLECTION+"/all"); if (collNames.length() > 0) requiredTabs.add("Collection"); } catch (Exception e) { } requiredTabs.add("Execution"); requiredTabs.add("History"); requiredTabs.add("Viewpoint"); requiredTabs.add("Workflow"); return requiredTabs; } @Override public DataFlavor[] getTransferDataFlavors() { return supportedFlavours; } @Override public boolean isDataFlavorSupported(DataFlavor flavor) { for (DataFlavor flavour : supportedFlavours) { if (flavour.equals(flavor)) return true; } return false; } @Override public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException { if (flavor.equals(supportedFlavours[0])) return this; if (flavor.equals(supportedFlavours[1])) return binding; if (flavor.equals(supportedFlavours[2])) return name; throw new UnsupportedFlavorException(flavor); } }