001 package nl.cwi.sen1.error.viewer; 002 003 import java.util.HashMap; 004 005 import javax.swing.JTree; 006 import javax.swing.event.TreeSelectionEvent; 007 import javax.swing.event.TreeSelectionListener; 008 import javax.swing.tree.TreePath; 009 010 import nl.cwi.sen1.error.model.SelectableNode; 011 import nl.cwi.sen1.gui.CloseAbortedException; 012 import nl.cwi.sen1.gui.Studio; 013 import nl.cwi.sen1.gui.StudioImplWithPredefinedLayout; 014 import nl.cwi.sen1.gui.StudioWithPredefinedLayout; 015 import nl.cwi.sen1.gui.component.StudioComponent; 016 import nl.cwi.sen1.gui.component.StudioComponentImpl; 017 import nl.cwi.sen1.gui.plugin.DefaultStudioPlugin; 018 import aterm.ATerm; 019 import aterm.ATermFactory; 020 import aterm.pure.PureFactory; 021 import errorapi.Factory; 022 import errorapi.types.Summary; 023 024 public class ErrorViewer extends DefaultStudioPlugin implements ErrorViewerTif { 025 private static final String TOOL_NAME = "error-viewer"; 026 027 private Studio studio; 028 029 private StudioComponent component; 030 031 ErrorViewerBridge bridge; 032 033 errorapi.Factory errorFactory; 034 035 private HashMap<String, ErrorPanel> panels = new HashMap<String, ErrorPanel>(); 036 037 public String getName() { 038 return TOOL_NAME; 039 } 040 041 public void initStudioPlugin(Studio studio) { 042 this.studio = studio; 043 044 ATermFactory factory = studio.getATermFactory(); 045 errorFactory = Factory.getInstance((PureFactory) factory); 046 047 bridge = new ErrorViewerBridge(factory, this); 048 bridge.setLockObject(this); 049 050 studio.connect(getName(), bridge); 051 } 052 053 public ErrorViewer() { 054 } 055 056 private void addListener(ErrorPanel panel) { 057 final JTree tree = panel.getTree(); 058 059 tree.addTreeSelectionListener(new TreeSelectionListener() { 060 public void valueChanged(TreeSelectionEvent e) { 061 TreePath path = tree.getSelectionPath(); 062 if (path != null) { 063 Object node = path.getLastPathComponent(); 064 if (node != null && node instanceof SelectableNode) { 065 ((SelectableNode) node).selected(studio, bridge); 066 tree.clearSelection(); 067 } 068 } 069 } 070 }); 071 } 072 073 private ErrorPanel createPanel(String panelId) { 074 ErrorPanel panel = new ErrorPanel(); 075 addListener(panel); 076 077 component = new StudioComponentImpl(panelId, panel) { 078 public void requestClose() throws CloseAbortedException { 079 throw new CloseAbortedException(); 080 } 081 }; 082 083 ((StudioWithPredefinedLayout) studio).addComponent(component, 084 StudioImplWithPredefinedLayout.BOTTOM_RIGHT); 085 086 return panel; 087 } 088 089 private ErrorPanel getPanel(String panelId) { 090 ErrorPanel panel = panels.get(panelId); 091 092 if (panel != null) { 093 return panel; 094 } 095 panel = createPanel(panelId); 096 panels.put(panelId, panel); 097 return panel; 098 } 099 100 public void showFeedbackSummary(String panelId, ATerm summaryTerm) { 101 try { 102 Summary summary = errorFactory.SummaryFromTerm(summaryTerm); 103 if (summary.getList().isEmpty() == false) { 104 getPanel(panelId).addError(summary); 105 studio.makeVisible(component); 106 } 107 } catch (aterm.ParseError ex) { 108 System.err.println("Summary is not a valid ATerm"); 109 } catch (IllegalArgumentException ex) { 110 System.err.println("Summary is not valid"); 111 } 112 } 113 114 public void refreshFeedbackSummary(String panelId, ATerm summaryTerm) { 115 try { 116 Summary summary = errorFactory.SummaryFromTerm(summaryTerm); 117 String producer = summary.getProducer(); 118 String id = summary.getId(); 119 120 getPanel(panelId).removeAllMatchingErrors(producer, id); 121 getPanel(panelId).addError(summary); 122 } catch (aterm.ParseError ex) { 123 System.err.println("Summary is not a valid ATerm"); 124 } catch (IllegalArgumentException ex) { 125 System.err.println("Summary is not valid"); 126 } 127 } 128 129 public void removeFeedbackSummary(String panelId, String producer, String id) { 130 getPanel(panelId).removeAllMatchingErrors(producer, id); 131 } 132 133 public void removeFeedbackSummary(String panelId, String path) { 134 getPanel(panelId).removeAllMatchingErrors(path); 135 } 136 137 public void recAckEvent(ATerm t0) { 138 } 139 140 public void recTerminate(ATerm t0) { 141 fireStudioPluginClosed(); 142 } 143 }